[MERGE] merged with main trunk
[odoo/odoo.git] / addons / account / report / account_profit_loss.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 import pooler
23 from report import report_sxw
24 from common_report_header import common_report_header
25 from tools.translate import _
26
27 class report_pl_account_horizontal(report_sxw.rml_parse, common_report_header):
28
29     def __init__(self, cr, uid, name, context=None):
30         super(report_pl_account_horizontal, self).__init__(cr, uid, name, context=context)
31         self.result_sum_dr = 0.0
32         self.result_sum_cr = 0.0
33         self.res_pl = {}
34         self.result = {}
35         self.result_temp = []
36         self.localcontext.update( {
37             'time': time,
38             'get_lines': self.get_lines,
39             'get_lines_another': self.get_lines_another,
40             'get_currency': self._get_currency,
41             'get_data': self.get_data,
42             'sum_dr': self.sum_dr,
43             'sum_cr': self.sum_cr,
44             'final_result': self.final_result,
45             'get_fiscalyear': self._get_fiscalyear,
46             'get_account': self._get_account,
47             'get_start_period': self.get_start_period,
48             'get_end_period': self.get_end_period,
49             'get_sortby': self._get_sortby,
50             'get_filter': self._get_filter,
51             'get_journal': self._get_journal,
52             'get_start_date':self._get_start_date,
53             'get_end_date':self._get_end_date,
54             'get_company':self._get_company,
55             'get_target_move': self._get_target_move,
56         })
57         self.context = context
58
59     def set_context(self, objects, data, ids, report_type=None):
60         new_ids = ids
61         if (data['model'] == 'ir.ui.menu'):
62             new_ids = 'chart_account_id' in data['form'] and [data['form']['chart_account_id']] or []
63             objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
64         return super(report_pl_account_horizontal, self).set_context(objects, data, new_ids, report_type=report_type)
65
66
67     def final_result(self):
68         return self.res_pl
69
70     def sum_dr(self):
71         if self.res_pl['type'] == _('Net Profit'):
72             self.result_sum_dr += self.res_pl['balance']
73         return self.result_sum_dr
74
75     def sum_cr(self):
76         if self.res_pl['type'] == _('Net Loss'):
77             self.result_sum_cr += self.res_pl['balance']
78         return self.result_sum_cr
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         account_pool = db_pool.get('account.account')
85         currency_pool = db_pool.get('res.currency')
86
87         types = [
88             'expense',
89             'income'
90                 ]
91
92         ctx = self.context.copy()
93         ctx['fiscalyear'] = data['form'].get('fiscalyear_id', False)
94
95         if data['form']['filter'] == 'filter_period':
96             ctx['period_from'] = data['form'].get('period_from', False)
97             ctx['period_to'] =  data['form'].get('period_to', False)
98         elif data['form']['filter'] == 'filter_date':
99             ctx['date_from'] = data['form'].get('date_from', False)
100             ctx['date_to'] =  data['form'].get('date_to', False)
101
102         cal_list = {}
103         account_id = data['form'].get('chart_account_id', False)
104         company_currency = account_pool.browse(self.cr, self.uid, account_id).company_id.currency_id
105         account_ids = account_pool._get_children_and_consol(cr, uid, account_id, context=ctx)
106         accounts = account_pool.browse(cr, uid, account_ids, context=ctx)
107
108         for typ in types:
109             accounts_temp = []
110             for account in accounts:
111                 if (account.user_type.report_type) and (account.user_type.report_type == typ):
112                     currency = account.currency_id and account.currency_id or account.company_id.currency_id
113                     if typ == 'expense' and account.type <> 'view' and (account.debit <> account.credit):
114                         self.result_sum_dr += account.debit - account.credit
115                     if typ == 'income' and account.type <> 'view' and (account.debit <> account.credit):
116                         self.result_sum_cr += account.credit - account.debit
117                     if data['form']['display_account'] == 'movement':
118                         if not currency_pool.is_zero(self.cr, self.uid, currency, account.credit) or not currency_pool.is_zero(self.cr, self.uid, currency, account.debit) or not currency_pool.is_zero(self.cr, self.uid, currency, account.balance):
119                             accounts_temp.append(account)
120                     elif data['form']['display_account'] == 'not_zero':
121                         if not currency_pool.is_zero(self.cr, self.uid, currency, account.balance):
122                             accounts_temp.append(account)
123                     else:
124                         accounts_temp.append(account)
125             if currency_pool.is_zero(self.cr, self.uid, company_currency, (self.result_sum_dr-self.result_sum_cr)):
126                 self.res_pl['type'] = None
127                 self.res_pl['balance'] = 0.0
128             elif self.result_sum_dr > self.result_sum_cr:
129                 self.res_pl['type'] = _('Net Loss')
130                 self.res_pl['balance'] = (self.result_sum_dr - self.result_sum_cr)
131             else:
132                 self.res_pl['type'] = _('Net Profit')
133                 self.res_pl['balance'] = (self.result_sum_cr - self.result_sum_dr)
134             self.result[typ] = accounts_temp
135             cal_list[typ] = self.result[typ]
136         if cal_list:
137             temp = {}
138             for i in range(0,max(len(cal_list['expense']),len(cal_list['income']))):
139                 if i < len(cal_list['expense']) and i < len(cal_list['income']):
140                     temp={
141                           'code': cal_list['expense'][i].code,
142                           'name': cal_list['expense'][i].name,
143                           'level': cal_list['expense'][i].level,
144                           'balance': cal_list['expense'][i].balance != 0 and \
145                                     cal_list['expense'][i].balance * cal_list['expense'][i].user_type.sign or \
146                                     cal_list['expense'][i].balance,
147                           'code1': cal_list['income'][i].code,
148                           'name1': cal_list['income'][i].name,
149                           'level1': cal_list['income'][i].level,
150                           'balance1': cal_list['income'][i].balance != 0 and \
151                                         cal_list['income'][i].balance * cal_list['income'][i].user_type.sign or \
152                                         cal_list['income'][i].balance,
153                           }
154                     self.result_temp.append(temp)
155                 else:
156                     if i < len(cal_list['income']):
157                         temp={
158                               'code': '',
159                               'name': '',
160                               'level': False,
161                               'balance':False,
162                               'code1': cal_list['income'][i].code,
163                               'name1': cal_list['income'][i].name,
164                               'level1': cal_list['income'][i].level,
165                               'balance1': cal_list['income'][i].balance != 0 and \
166                                             cal_list['income'][i].balance * cal_list['income'][i].user_type.sign \
167                                             or cal_list['income'][i].balance,
168                               }
169                         self.result_temp.append(temp)
170                     if  i < len(cal_list['expense']):
171                         temp={
172                               'code': cal_list['expense'][i].code,
173                               'name': cal_list['expense'][i].name,
174                               'level': cal_list['expense'][i].level,
175                               'balance': cal_list['expense'][i].balance != 0 and \
176                                             cal_list['expense'][i].balance * cal_list['expense'][i].user_type.sign \
177                                             or cal_list['expense'][i].balance,
178                               'code1': '',
179                               'name1': '',
180                               'level1': False,
181                               'balance1':False,
182                               }
183                         self.result_temp.append(temp)
184         return None
185
186     def get_lines(self):
187         return self.result_temp
188
189     def get_lines_another(self, group):
190         return self.result.get(group, [])
191
192 report_sxw.report_sxw('report.pl.account.horizontal', 'account.account',
193     'addons/account/report/account_profit_horizontal.rml',parser=report_pl_account_horizontal, header='internal landscape')
194
195 report_sxw.report_sxw('report.pl.account', 'account.account',
196     'addons/account/report/account_profit_loss.rml',parser=report_pl_account_horizontal, header='internal')
197
198 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: