~openerp-chinese-team/openerp-china/openerp-china

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# -*- encoding: utf-8 -*-
# __author__ = jeff@openerp.cn,joshua@openerp.cn
# __thanks__ = [oldrev@gmail.com]
##############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
import rml_parse
from report import report_sxw
from tools.translate import _

class account_move_parser(rml_parse.rml_parse):
    '''
    Parser for account move report
    '''
    def __init__(self, cr, uid, name, context):
        super(account_move_parser, self).__init__(cr, uid, name, context)
        self.localcontext.update({
            'time': time,
            'rate': self._get_exchange_rate,
            'price':self._get_unit_price,
        })
    
    def _get_exchange_rate(self, line):
        '''
        Exchange rate: Debit or Credit / currency ammount
        Why not get it from currency code + date ?
        '''
        exchange_rate = False 
        if line.amount_currency: 
             if line.debit > 0: 
                 exchange_rate = line.debit/line.amount_currency 
             if line.credit > 0: 
                 exchange_rate = line.credit/line.amount_currency 
        return exchange_rate 

    def _get_unit_price(self, line):
        '''
        Unit price:Debit or Credit / Quantity
        '''
        unit_price = False
        if line.quantity:
            if line.debit > 0:
                unit_price = line.debit/line.quantity
            if line.credit > 0:
                unit_price = line.credit/line.quantity
        return unit_price

#Register report parser
report_sxw.report_sxw('report.account.move', 'account.move',               \
                      'addons/oecn_account_print/report/account_move.sxw', \
                      parser=account_move_parser)


class general_ledger_parser(report_sxw.rml_parse):
    """
    总账-General Ledger
    """
    def __init__(self, cr, uid, name, context):
        super(general_ledger_parser, self).__init__(cr, uid, name, context=context)
        self.query = ""
        self.preiod = {}
        self.localcontext.update({
            'time': time,
            'general_ledger_line':self._general_ledger_line,
            'beginning_balance_accounts':self._beginning_balance_accounts,
        })

    def set_context(self, objects, data, ids, report_type = None):
        """
        设置 OE context
        """
        
        self.all_date = self._get_date(data)
        data['all_date'] = self.all_date
        new_ids = ids
        print 'ids:%s date:%s'%(ids, data)
        objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)

        super(general_ledger_parser, self).set_context(objects, data, new_ids, report_type)

    def _get_date(self,data):
        """
        根据wizard获取日期:期间开始日期,期间结束日期,会计年开始日期
        """
        #print 'self-->',data
        period_from_id = data['period_from']
        period_to_id = data['period_to']
        period_obj = self.pool.get('account.period')
        period_start_obj = period_obj.read(self.cr, self.uid, period_from_id, ['date_start','fiscalyear_id'])
        #print 'period_start',period_start_obj
        period_end_obj = period_obj.read(self.cr, self.uid, period_to_id, ['date_stop'])
        #print 'period_end',period_end_obj
        fiscalyear_obj = self.pool.get('account.fiscalyear').read(self.cr, self.uid, period_start_obj['fiscalyear_id'][0], ['date_start'])
        #print 'fiscalyear_obj',fiscalyear_obj
        all_date = {
            'period_start_date_start':period_start_obj['date_start'],
            'period_end_date_stop':period_end_obj['date_stop'],
            'fiscalyear_obj_date_start':fiscalyear_obj['date_start'],
        }
       # print 'date-->',all_date
        return all_date

    def _get_periods(self):
        """
        根据日期段获取里面的期间
        """
        print 'in_get_period'
        self.cr.execute(("select date_start,date_stop,name as period_name "\
                    "from account_period where "\
                    " date_start>='%s' and date_stop<='%s' "\
                    " order by date_start")% (self.all_date['period_start_date_start'], self.all_date['period_end_date_stop']))
        periods = self.cr.dictfetchall()
        print 'peirod:',periods
        return periods

    def _beginning_balance_accounts(self, account):
        """
        计算年初,借、贷、余额
        """
        print '_beginning_balance_accounts'
        periods = self._get_periods()
        print 'account:',account
        account_ids = account.id
        result={
            'debit':0,
            'credit':0,
            'balance':0,
            'debit_or_credit':u'平',
            'month':self.all_date['fiscalyear_obj_date_start'][5:7],
            'day':self.all_date['fiscalyear_obj_date_start'][8:10],
        }
        if account_ids:
            self.cr.execute(("SELECT l.account_id as id, " \
                        "a.code as code," \
                        "a.name as name," \
                        "COALESCE(SUM(l.debit),0) - COALESCE(SUM(l.credit), 0) as balance , " \
                        "COALESCE(SUM(l.debit), 0) as debit, " \
                        "COALESCE(SUM(l.credit), 0) as credit " \
                        "FROM " \
                            "account_move_line l " \
                        "JOIN " \
                            "account_account a on (l.account_id=a.id)"
                        "WHERE " \
                            "l.account_id IN (%s) " \
                            "and l.date< '%s' " \
                        " GROUP BY l.account_id,a.code,a.name " \
                        " ORDER BY a.code") % (account_ids,self.all_date['fiscalyear_obj_date_start']))
            res = self.cr.dictfetchall()
            if(res):
                if (int(res[0]['balance']) == 0):
                    result['debit_or_credit'] = u'平'
                elif (int(res[0]['balance']) > 0):
                    result['debit_or_credit'] = u'借'
                else :
                    result['debit_or_credit'] = u'贷'
                result['debit']=res[0]['debit']
                result['credit']=res[0]['credit']
                result['balance']=res[0]['balance']
        print 'result:',result
        return result

    def _general_ledger_line(self, account):
        """
        生成总账
        """
        result=[]
        month_debit_or_credit = u'平'
        year_debit_or_credit = u'平'
        i = 0
        month_amount_balance = 0
        periods = self._get_periods()
        account_ids = account.id
        month_amount_accounts={}
        beginning_balance_accounts={}
        year_amount_accounts={}
        for period in periods:
            if account_ids:
                self.cr.execute(("SELECT l.account_id as id, " \
                        "a.code as code," \
                        "a.name as name," \
                        "COALESCE(SUM(l.debit),0) - COALESCE(SUM(l.credit), 0) as month_amount_balance , " \
                        "COALESCE(SUM(l.debit), 0) as month_amount_debit, " \
                        "COALESCE(SUM(l.credit), 0) as month_amount_credit " \
                        "FROM " \
                            "account_move_line l " \
                        "JOIN " \
                            "account_account a on (l.account_id=a.id)"
                        "WHERE " \
                            "l.account_id IN (%s) " \
                            "and l.date<= '%s' " \
                            "and l.date>= '%s' "
                        " GROUP BY l.account_id,a.code,a.name " \
                        " ORDER BY a.code") % (account_ids,period['date_stop'], period['date_start']))
                month_amount_accounts = self.cr.dictfetchall()

                self.cr.execute(("SELECT l.account_id as id, " \
                        "a.code as code," \
                        "a.name as name," \
                        "COALESCE(SUM(l.debit),0) - COALESCE(SUM(l.credit), 0) as year_amount_balance , " \
                        "COALESCE(SUM(l.debit), 0) as year_amount_debit, " \
                        "COALESCE(SUM(l.credit), 0) as year_amount_credit " \
                        "FROM " \
                            "account_move_line l " \
                        "JOIN " \
                            "account_account a on (l.account_id=a.id)"
                        "WHERE " \
                            "l.account_id IN (%s) " \
                            "and l.date<= '%s' " \
                            "and l.date>= '%s' "
                        " GROUP BY l.account_id,a.code,a.name " \
                        " ORDER BY a.code") % (account_ids,period['date_stop'], self.all_date['fiscalyear_obj_date_start']))
                year_amount_accounts = self.cr.dictfetchall()

            if (month_amount_accounts):
                if (int(month_amount_accounts[0]['month_amount_balance']) == 0):
                    month_debit_or_credit = u'平'
                elif (int(month_amount_accounts[0]['month_amount_balance']) > 0):
                    month_debit_or_credit = u'借'
                else:
                    month_debit_or_credit = u'贷'
                if (int(year_amount_accounts[0]['year_amount_balance']) == 0):
                    year_debit_or_credit = u'平'
                elif (int(year_amount_accounts[0]['year_amount_balance']) > 0):
                    year_debit_or_credit = u'借'
                else:
                    year_debit_or_credit = u'贷'

                month_amount_balance = month_amount_balance + month_amount_accounts[0]['month_amount_balance']
                sums = {
                    'period_month':period['date_stop'][5:7],
                    'period_day': period['date_stop'][8:10],
                    'code': month_amount_accounts[0]['code'],
                    'name': month_amount_accounts[0]['name'],
                    'month_amount_debit': month_amount_accounts[0]['month_amount_debit'],
                    'month_amount_credit': month_amount_accounts[0]['month_amount_credit'],
                    'month_amount_balance': month_amount_balance,
                    'month_debit_or_credit':month_debit_or_credit,
                    'year_amount_debit': year_amount_accounts[0]['year_amount_debit'],
                    'year_amount_credit': year_amount_accounts[0]['year_amount_credit'],
                    'year_amount_balance':year_amount_accounts[0]['year_amount_balance'],
                    'year_debit_or_credit':year_debit_or_credit,
                }
                result.append(sums)
        return result

report_sxw.report_sxw('report.account.general_ledger', 'account.account', 'addons/oecn_account_print/report/general_ledger.rml', parser=general_ledger_parser, header=False)