[IMP] Account: Profit and loss and balance sheet report improved
[odoo/odoo.git] / addons / account / report / report_bs.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import time
23
24 import pooler
25 import rml_parse
26 from report import report_sxw
27 from account.report import report_pl
28 from common_report_header import common_report_header
29
30 class report_balancesheet_horizontal(rml_parse.rml_parse, common_report_header):
31     def __init__(self, cr, uid, name, context=None):
32         super(report_balancesheet_horizontal, self).__init__(cr, uid, name, context=context)
33         self.obj_pl = report_pl.report_pl_account_horizontal(cr, uid, name, context=context)
34         self.result_sum_dr = 0.0
35         self.result_sum_cr = 0.0
36         self.result = {}
37         self.res_bl = {}
38         self.result_temp = []
39         self.localcontext.update({
40             'time': time,
41             'get_abs' : self.get_abs,
42             'get_lines' : self.get_lines,
43             'get_lines_another' : self.get_lines_another,
44             'get_company': self._get_company,
45             'get_currency': self._get_currency,
46             'sum_dr' : self.sum_dr,
47             'sum_cr' : self.sum_cr,
48             'get_data':self.get_data,
49             'get_pl_balance':self.get_pl_balance,
50             'get_fiscalyear': self._get_fiscalyear,
51             'get_account': self._get_account,
52             'get_start_period': self.get_start_period,
53             'get_end_period': self.get_end_period,
54             'get_sortby': self._get_sortby,
55             'get_filter': self._get_filter,
56             'get_journal': self._get_journal,
57             'get_start_date':self._get_start_date,
58             'get_end_date':self._get_end_date,
59             'get_company':self._get_company,
60
61         })
62         self.context = context
63
64     def get_abs(self,amount):
65         return abs(amount)
66
67     def sum_dr(self):
68         if self.res_bl['type'] == 'Net Profit':
69             self.result_sum_dr += self.res_bl['balance']
70         return self.result_sum_dr or 0.0
71
72     def sum_cr(self):
73         if self.res_bl['type'] == 'Net Loss':
74             self.result_sum_cr += self.res_bl['balance']
75         return self.result_sum_cr or 0.0
76
77     def get_pl_balance(self):
78         return self.res_bl or 0.0
79
80     def get_data(self,data):
81         cr, uid = self.cr, self.uid
82         db_pool = pooler.get_pool(self.cr.dbname)
83
84         #Getting Profit or Loss Balance from profit and Loss report
85         result_pl=self.obj_pl.get_data(data)
86         self.res_bl = self.obj_pl.final_result()
87
88         type_pool = db_pool.get('account.account.type')
89         account_pool = db_pool.get('account.account')
90         year_pool = db_pool.get('account.fiscalyear')
91
92         types = [
93             'liability',
94             'asset'
95         ]
96
97         ctx = self.context.copy()
98 #        ctx['state'] = form['context'].get('state','filter_no')
99         ctx['fiscalyear'] = data['form'].get('fiscalyear_id', False)
100
101         if data['form']['filter'] == 'filter_period' :
102             ctx['periods'] = data['form'].get('periods', False)
103         elif data['form']['filter'] == 'filter_date':
104             ctx['date_from'] = data['form'].get('date_from', False)
105             ctx['date_to'] =  data['form'].get('date_to', False)
106
107         cal_list = {}
108         pl_dict = {}
109         account_dict = {}
110         account_id = data['form'].get('chart_account_id', False)
111         account_ids = account_pool._get_children_and_consol(cr, uid, account_id, context=ctx)
112         accounts = account_pool.browse(cr, uid, account_ids, context=ctx)
113
114         if self.res_bl['type'] == 'Net Profit C.F.B.L.':
115             self.res_bl['type'] = 'Net Profit'
116         else:
117             self.res_bl['type'] = 'Net Loss'
118         pl_dict  = {
119             'code' : False,
120             'name' : self.res_bl['type'],
121             'level': False,
122             'balance':self.res_bl['balance'],
123         }
124         for typ in types:
125             accounts_temp = []
126             for account in accounts:
127                 if (account.user_type.report_type) and (account.user_type.report_type == typ):
128                     account_dict = {
129                         'id'   : account.id,
130                         'code' : account.code,
131                         'name' : account.name,
132                         'level': account.level,
133                         'balance':account.balance,
134                     }
135                     if typ == 'liability' and account.type <> 'view' and (account.debit <> account.credit):
136                         self.result_sum_dr += abs(account.debit - account.credit)
137                     if typ == 'asset' and account.type <> 'view' and (account.debit <> account.credit):
138                         self.result_sum_cr += abs(account.debit - account.credit)
139                     if data['form']['display_account'] == 'bal_movement':
140                         if account.credit > 0 or account.debit > 0 or account.balance > 0 :
141                             accounts_temp.append(account_dict)
142                     elif data['form']['display_account'] == 'bal_solde':
143                         if account.balance != 0:
144                             accounts_temp.append(account_dict)
145                     else:
146                         accounts_temp.append(account_dict)
147                     if account.id == data['form']['reserve_account_id']:
148                         pl_dict['level'] = account['level'] + 1
149                         accounts_temp.append(pl_dict)
150
151             self.result[typ] = accounts_temp
152             cal_list[typ]=self.result[typ]
153
154         if cal_list:
155             temp = {}
156             for i in range(0,max(len(cal_list['liability']),len(cal_list['asset']))):
157                 if i < len(cal_list['liability']) and i < len(cal_list['asset']):
158                     temp={
159                           'code' : cal_list['liability'][i]['code'],
160                           'name' : cal_list['liability'][i]['name'],
161                           'level': cal_list['liability'][i]['level'],
162                           'balance':cal_list['liability'][i]['balance'],
163                           'code1' : cal_list['asset'][i]['code'],
164                           'name1' : cal_list['asset'][i]['name'],
165                           'level1': cal_list['asset'][i]['level'],
166                           'balance1':cal_list['asset'][i]['balance'],
167                           }
168                     self.result_temp.append(temp)
169                 else:
170                     if i < len(cal_list['asset']):
171                         temp={
172                               'code' : '',
173                               'name' : '',
174                               'level': False,
175                               'balance':False,
176                               'code1' : cal_list['asset'][i]['code'],
177                               'name1' : cal_list['asset'][i]['name'],
178                               'level1': cal_list['asset'][i]['level'],
179                               'balance1':cal_list['asset'][i]['balance'],
180                           }
181                         self.result_temp.append(temp)
182                     if  i < len(cal_list['liability']):
183                         temp={
184                               'code' : cal_list['liability'][i]['code'],
185                               'name' : cal_list['liability'][i]['name'],
186                               'level': cal_list['liability'][i]['level'],
187                               'balance':cal_list['liability'][i]['balance'],
188                               'code1' : '',
189                               'name1' : '',
190                               'level1': False,
191                               'balance1':False,
192                           }
193                         self.result_temp.append(temp)
194         return None
195
196     def get_lines(self):
197         return self.result_temp
198
199     def get_lines_another(self, group):
200         return self.result.get(group, [])
201
202 report_sxw.report_sxw('report.account.balancesheet.horizontal', 'account.account',
203     'addons/account/report/report_balance_sheet_horizontal.rml',parser=report_balancesheet_horizontal,
204     header='internal')
205
206 report_sxw.report_sxw('report.account.balancesheet', 'account.account',
207     'addons/account/report/report_balance_sheet.rml',parser=report_balancesheet_horizontal,
208     header='internal')
209
210 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: