added from extra-addons
[odoo/odoo.git] / addons / account_reporting / report / account_report_bs.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 import time
30 import pooler
31 import locale
32 from report import report_sxw
33
34 #from addons.account.wizard import wizard_account_balance_report
35
36 parents = {
37     'tr':1,
38     'li':1,
39     'story': 0,
40     'section': 0
41 }
42
43 class account_report_bs(report_sxw.rml_parse):
44     def __init__(self, cr, uid, name, context):
45         super(account_report_bs, self).__init__(cr, uid, name, context)
46         self.localcontext.update({
47             'time': time,
48             'lines': self.lines,
49         })
50         self.context = context
51
52
53     def line_total(self,line_id,ctx):
54         _total = 0
55         bsline= self.pool.get('account.report.bs').browse(self.cr,self.uid,[line_id])[0]
56         bsline_accids = bsline.account_id
57         res =self.pool.get('account.report.bs').read(self.cr,self.uid,[line_id],['account_id','child_id'])[0]
58         for acc_id in res['account_id']:
59             acc = self.pool.get('account.account').browse(self.cr,self.uid,[acc_id],ctx)[0]
60             _total += acc.balance
61         bsline_reportbs = res['child_id']
62
63         for report in bsline_reportbs:
64             _total +=self.line_total(report,ctx)
65         return  _total
66
67     def lines(self, form, ids={}, done=None, level=1):
68         if not ids:
69             ids = self.ids
70         if not ids:
71             return []
72         if not done:
73             done={}
74         result = []
75         ctx = self.context.copy()
76         ctx['fiscalyear'] = form['fiscalyear']
77         ctx['periods'] = form['periods'][0][2]
78         report_objs = self.pool.get('account.report.bs').browse(self.cr, self.uid, ids)
79         title_name = False
80         if level==1:
81             title_name = report_objs[0].name
82         def cmp_code(x, y):
83             return cmp(x.code, y.code)
84         report_objs.sort(cmp_code)
85         
86         for report_obj in report_objs:
87             if report_obj.id in done:
88                 continue
89             done[report_obj.id] = 1
90             color_font = ''
91             color_back = ''
92             if report_obj.color_font:
93                 color_font = report_obj.color_font.name
94             if report_obj.color_back:
95                 color_back = report_obj.color_back.name
96                 
97             res = {
98                 'code': report_obj.code,
99                 'name': report_obj.name,
100                 'level': level,
101                 'balance': self.line_total(report_obj.id,ctx),
102                 'color_font':color_font,
103                 'color_back':color_back,
104                 'font_style' : report_obj.font_style
105             }
106             result.append(res)
107             report_type = report_obj.report_type
108             if report_type != 'only_obj':
109                 account_ids = self.pool.get('account.report.bs').read(self.cr,self.uid,[report_obj.id],['account_id'])[0]['account_id']
110                 for account_id in account_ids:
111                     res1 = self.check_child_id(account_id,level,ctx,report_type)
112                     result += res1
113
114             if report_obj.child_id:
115                 ids2 = [(x.code,x.id) for x in report_obj.child_id]
116                 ids2.sort()
117                 result += self.lines(form, [x[1] for x in ids2], done, level+1)
118
119         return result
120
121     def check_child_id(self,account_id,level,ctx,report_type):
122         account = self.pool.get('account.account').browse(self.cr,self.uid,[account_id],ctx)[0]
123         result = []
124         res = {
125             'code': account.code,
126             'name': account.name,
127             'level': level+1,
128             'balance': account.balance,
129             'color_font' : 'black',
130             'color_back' :'pink',
131             'font_style' : 'Helvetica-BoldOblique',
132             }
133         result.append(res)
134         if report_type != 'with_account':
135             acc_child_id  = self.pool.get('account.account').search(self.cr,self.uid,[('parent_id','=',[account_id]),('type','=','view')])
136             for child_id in acc_child_id :
137                 result += self.check_child_id(child_id,level+1,ctx,report_type)
138         return result
139
140
141
142 #   def _sum_credit(self):
143 #       return self.sum_credit
144 #
145 #   def _sum_debit(self):
146 #       return self.sum_debit
147 report_sxw.report_sxw('report.account.report.bs', 'account.report.bs', 'addons/account_reporting/report/account_report_bs.rml', parser=account_report_bs)
148 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
149