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