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

« back to all changes in this revision

Viewing changes to oecn_account_print/report/generic_report.py

  • Committer: JoshuaJan
  • Date: 2012-08-22 09:24:05 UTC
  • Revision ID: popkar77@gmail.com-20120822092405-349syo9uytkkzeit
修改报表样式

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- encoding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution   
5
 
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6
 
#    $Id$
7
 
#
8
 
#    This program is free software: you can redistribute it and/or modify
9
 
#    it under the terms of the GNU General Public License as published by
10
 
#    the Free Software Foundation, either version 3 of the License, or
11
 
#    (at your option) any later version.
12
 
#
13
 
#    This program is distributed in the hope that it will be useful,
14
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
#    GNU General Public License for more details.
17
 
#
18
 
#    You should have received a copy of the GNU General Public License
19
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
#
21
 
##############################################################################
22
 
 
23
 
import time
24
 
from report import report_sxw
25
 
from osv import osv
26
 
import pooler
27
 
 
28
 
class generic_report(report_sxw.rml_parse):
29
 
    """
30
 
    余额表,资产负债表
31
 
    """
32
 
 
33
 
    def __init__(self, cr, uid, name, context):
34
 
        super(generic_report, self).__init__(cr, uid, name, context=context)
35
 
        self.query = ""
36
 
        self.localcontext.update({
37
 
            'time': time,
38
 
            'balance_line':self._balance_line,
39
 
            'report_imformation':self._get_report_imformation,
40
 
        })
41
 
 
42
 
    def set_context(self, objects, data, ids, report_type = None):
43
 
        """
44
 
        设置 OE context
45
 
        """
46
 
        new_ids = []
47
 
        if (data['model'] == 'account.account'):
48
 
            new_ids = ids
49
 
        else:
50
 
            new_ids = self.pool.get('account.account').search(self.cr, self.uid, [('id','!=','0')], context={})
51
 
            objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
52
 
 
53
 
        super(generic_report, self).set_context(objects, data, new_ids, report_type)
54
 
 
55
 
    def _get_report_imformation(self, form):
56
 
        """
57
 
        获取打印日期,wizard选择的公司名称
58
 
        """
59
 
        res={
60
 
            'date':'',
61
 
            'company':'',
62
 
        }
63
 
        res['date']=form['date']
64
 
        companyObj = self.pool.get('res.company').read(self.cr,self.uid,form['company_id'],['name'])
65
 
        res['company'] = companyObj['name']
66
 
        return res
67
 
 
68
 
    def _get_children_and_consol(self, cr, uid, ids, context={}):
69
 
        """
70
 
        this function search for all the children and all consolidated children (recursively) of the given account ids
71
 
        """
72
 
        ids2 = self.pool.get('account.account').search(cr, uid, [('parent_id', 'child_of', ids)], context=context)
73
 
        ids3 = []
74
 
        for rec in self.pool.get('account.account').browse(cr, uid, ids2, context=context):
75
 
            for child in rec.child_consol_ids:
76
 
                ids3.append(child.id)
77
 
        if ids3:
78
 
            ids3 = self._get_children_and_consol(cr, uid, ids3, context)
79
 
        return ids2 + ids3
80
 
 
81
 
    def _balance_line(self,form):
82
 
        """
83
 
        生成余额表
84
 
        """
85
 
        result={}
86
 
        i=0
87
 
        beginning_field_names = ['beginning_balance','beginning_debit','beginning_credit']
88
 
        year_amount_field_names = ['year_amount_balance','year_amount_debit','year_amount_credit']
89
 
        month_amount_field_names = ['month_amount_balance','month_amount_debit','month_amount_credit']
90
 
        ending_field_names = ['ending_balance','ending_debit','ending_credit']
91
 
        pool = pooler.get_pool(self.cr.dbname)
92
 
 
93
 
        end_dete =form['date']
94
 
        year_begin_date = end_dete[0:4]+'-01-01'
95
 
        month_begin_date = end_dete[0:7]+'-01'
96
 
 
97
 
        account_ids = pool.get('account.account').search(self.cr, self.uid, [('id','!=','0')], context={})
98
 
        ids2 = self._get_children_and_consol(self.cr, self.uid, account_ids, context={})
99
 
        acc_set = ",".join(map(str, ids2))
100
 
        #compute for each account the balance/debit/credit from the move lines for Ending Blance
101
 
        beginning_accounts = {}
102
 
        year_amount_accounts = {}
103
 
        month_amount_accounts = {}
104
 
        ending_accounts = {}
105
 
 
106
 
        #期初
107
 
        if ids2:
108
 
            
109
 
            self.cr.execute(("SELECT l.account_id as id, " \
110
 
                    "a.code as code," \
111
 
                    "a.name as name," \
112
 
                    "COALESCE(SUM(l.debit),0) - COALESCE(SUM(l.credit), 0) as beginning_balance , " \
113
 
                    "COALESCE(SUM(l.debit), 0) as beginning_debit, " \
114
 
                    "COALESCE(SUM(l.credit), 0) as beginning_credit " \
115
 
                    "FROM " \
116
 
                        "account_move_line l " \
117
 
                    "JOIN " \
118
 
                        "account_account a on (l.account_id=a.id)"
119
 
                    "WHERE " \
120
 
                        "l.account_id IN (%s) " \
121
 
                        "and l.date<= '%s'"
122
 
                    " GROUP BY l.account_id,a.code,a.name " \
123
 
                    " ORDER BY a.code") % (acc_set, year_begin_date))
124
 
 
125
 
            for res in self.cr.dictfetchall():
126
 
                beginning_accounts[res['id']] = res
127
 
 
128
 
        #本年发生额
129
 
        if ids2:
130
 
            self.cr.execute(("SELECT l.account_id as id, " \
131
 
                    "a.code as code," \
132
 
                    "a.name as name," \
133
 
                    "COALESCE(SUM(l.debit),0) - COALESCE(SUM(l.credit), 0) as year_amount_balance , " \
134
 
                    "COALESCE(SUM(l.debit), 0) as year_amount_debit, " \
135
 
                    "COALESCE(SUM(l.credit), 0) as year_amount_credit " \
136
 
                    "FROM " \
137
 
                        "account_move_line l " \
138
 
                    "JOIN " \
139
 
                        "account_account a on (l.account_id=a.id)"
140
 
                    "WHERE " \
141
 
                        "l.account_id IN (%s) " \
142
 
                        "and l.date<= '%s' " \
143
 
                        "and l.date>= '%s' "
144
 
                    " GROUP BY l.account_id,a.code,a.name " \
145
 
                    " ORDER BY a.code") % (acc_set, end_dete,year_begin_date))
146
 
 
147
 
            for res in self.cr.dictfetchall():
148
 
                year_amount_accounts[res['id']] = res
149
 
 
150
 
        #本月发生额
151
 
        if ids2:
152
 
            self.cr.execute(("SELECT l.account_id as id, " \
153
 
                    "a.code as code," \
154
 
                    "a.name as name," \
155
 
                    "COALESCE(SUM(l.debit),0) - COALESCE(SUM(l.credit), 0) as month_amount_balance , " \
156
 
                    "COALESCE(SUM(l.debit), 0) as month_amount_debit, " \
157
 
                    "COALESCE(SUM(l.credit), 0) as month_amount_credit " \
158
 
                    "FROM " \
159
 
                        "account_move_line l " \
160
 
                    "JOIN " \
161
 
                        "account_account a on (l.account_id=a.id)"
162
 
                    "WHERE " \
163
 
                        "l.account_id IN (%s) " \
164
 
                        "and l.date<= '%s' " \
165
 
                        "and l.date>= '%s' "
166
 
                    " GROUP BY l.account_id,a.code,a.name " \
167
 
                    " ORDER BY a.code") % (acc_set, end_dete,month_begin_date))
168
 
 
169
 
            for res in self.cr.dictfetchall():
170
 
                month_amount_accounts[res['id']] = res
171
 
 
172
 
        #期末
173
 
        if ids2:
174
 
            self.cr.execute(("SELECT l.account_id as id, " \
175
 
                    "a.code as code," \
176
 
                    "a.name as name," \
177
 
                    "COALESCE(SUM(l.debit),0) - COALESCE(SUM(l.credit), 0) as ending_balance , " \
178
 
                    "COALESCE(SUM(l.debit), 0) as ending_debit, " \
179
 
                    "COALESCE(SUM(l.credit), 0) as ending_credit " \
180
 
                    "FROM " \
181
 
                        "account_move_line l " \
182
 
                    "JOIN " \
183
 
                        "account_account a on (l.account_id=a.id)"
184
 
                    "WHERE " \
185
 
                        "l.account_id IN (%s) " \
186
 
                        "and l.date<= '%s'"
187
 
                    " GROUP BY l.account_id,a.code,a.name " \
188
 
                    " ORDER BY a.code") % (acc_set, end_dete))
189
 
 
190
 
            for res in self.cr.dictfetchall():
191
 
                ending_accounts[res['id']] = res
192
 
        # consolidate accounts with direct children
193
 
        brs = list(self.pool.get('account.account').browse(self.cr, self.uid, ids2, context={}))
194
 
        sums = {}
195
 
        while brs:
196
 
            current = brs[0]
197
 
            can_compute = True
198
 
            for child in current.child_id:
199
 
                if child.id not in sums:
200
 
                    can_compute = False
201
 
                    try:
202
 
                        brs.insert(0, brs.pop(brs.index(child)))
203
 
                    except ValueError:
204
 
                        brs.insert(0, child)
205
 
            if can_compute:
206
 
                brs.pop(0)
207
 
                sums.setdefault(current.id, {})['code'] = current.code
208
 
                sums.setdefault(current.id, {})['name'] = current.name
209
 
                for fn in beginning_field_names:
210
 
                    sums.setdefault(current.id, {})[fn] = beginning_accounts.get(current.id, {}).get(fn, 0.0)
211
 
                    if current.child_id:
212
 
                        sums[current.id][fn] += sum(sums[child.id][fn] for child in current.child_id)
213
 
                        
214
 
                for fn in year_amount_field_names:
215
 
                    sums.setdefault(current.id, {})[fn] = year_amount_accounts.get(current.id, {}).get(fn, 0.0)
216
 
                    if current.child_id:
217
 
                        sums[current.id][fn] += sum(sums[child.id][fn] for child in current.child_id)
218
 
                        
219
 
                for fn in month_amount_field_names:
220
 
                    sums.setdefault(current.id, {})[fn] = month_amount_accounts.get(current.id, {}).get(fn, 0.0)
221
 
                    if current.child_id:
222
 
                        sums[current.id][fn] += sum(sums[child.id][fn] for child in current.child_id)
223
 
                        
224
 
                for fn in ending_field_names:
225
 
                    sums.setdefault(current.id, {})[fn] = ending_accounts.get(current.id, {}).get(fn, 0.0)
226
 
                    if current.child_id:
227
 
                        sums[current.id][fn] += sum(sums[child.id][fn] for child in current.child_id)
228
 
 
229
 
        res = []
230
 
        for s in sums:
231
 
             res.append(sums.get(s,{}))
232
 
        return res
233
 
 
234
 
report_sxw.report_sxw('report.account.balance_sheet','account.account','addons/l10n_account_cn/report/balance_sheet.odt',parser=generic_report)
235
 
 
236
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: