[IMP] crm_parner_assign, crm: Corrected history[whole, laetst, case info] for lead...
[odoo/odoo.git] / addons / account / report / account_balance_sheet.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 from report import report_sxw
26 from account.report import account_profit_loss
27 from common_report_header import common_report_header
28 from tools.translate import _
29
30 class report_balancesheet_horizontal(report_sxw.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 = account_profit_loss.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_lines': self.get_lines,
42             'get_lines_another': self.get_lines_another,
43             'get_company': self._get_company,
44             'get_currency': self._get_currency,
45             'sum_dr': self.sum_dr,
46             'sum_cr': self.sum_cr,
47             'get_data':self.get_data,
48             'get_pl_balance':self.get_pl_balance,
49             'get_fiscalyear': self._get_fiscalyear,
50             'get_account': self._get_account,
51             'get_start_period': self.get_start_period,
52             'get_end_period': self.get_end_period,
53             'get_sortby': self._get_sortby,
54             'get_filter': self._get_filter,
55             'get_journal': self._get_journal,
56             'get_start_date':self._get_start_date,
57             'get_end_date':self._get_end_date,
58             'get_company':self._get_company,
59             'get_target_move': self._get_target_move,
60         })
61         self.context = context
62
63     def set_context(self, objects, data, ids, report_type=None):
64         new_ids = ids
65         if (data['model'] == 'ir.ui.menu'):
66             new_ids = 'chart_account_id' in data['form'] and [data['form']['chart_account_id']] or []
67             objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
68         return super(report_balancesheet_horizontal, self).set_context(objects, data, new_ids, report_type=report_type)
69
70     def sum_dr(self):
71         if self.res_bl['type'] == _('Net Profit'):
72             self.result_sum_dr += self.res_bl['balance']*-1
73         return self.result_sum_dr
74
75     def sum_cr(self):
76         if self.res_bl['type'] == _('Net Loss'):
77             self.result_sum_cr += self.res_bl['balance']
78         return self.result_sum_cr
79
80     def get_pl_balance(self):
81         return self.res_bl
82
83     def get_data(self,data):
84         cr, uid = self.cr, self.uid
85         db_pool = pooler.get_pool(self.cr.dbname)
86
87         #Getting Profit or Loss Balance from profit and Loss report
88         self.obj_pl.get_data(data)
89         self.res_bl = self.obj_pl.final_result()
90
91         account_pool = db_pool.get('account.account')
92         currency_pool = db_pool.get('res.currency')
93
94         types = [
95             'liability',
96             'asset'
97         ]
98
99         ctx = self.context.copy()
100         ctx['fiscalyear'] = data['form'].get('fiscalyear_id', False)
101
102         if data['form']['filter'] == 'filter_period':
103             ctx['period_from'] = data['form'].get('period_from', False)
104             ctx['period_to'] =  data['form'].get('period_to', False)
105         elif data['form']['filter'] == 'filter_date':
106             ctx['date_from'] = data['form'].get('date_from', False)
107             ctx['date_to'] =  data['form'].get('date_to', False)
108         ctx['state'] = data['form'].get('target_move', 'all')
109         cal_list = {}
110         pl_dict = {}
111         account_dict = {}
112         account_id = data['form'].get('chart_account_id', False)
113         account_ids = account_pool._get_children_and_consol(cr, uid, account_id, context=ctx)
114         accounts = account_pool.browse(cr, uid, account_ids, context=ctx)
115
116         if not self.res_bl:
117             self.res_bl['type'] = _('Net Profit')
118             self.res_bl['balance'] = 0.0
119
120         if self.res_bl['type'] == _('Net Profit'):
121             self.res_bl['type'] = _('Net Profit')
122         else:
123             self.res_bl['type'] = _('Net Loss')
124         pl_dict  = {
125             'code': self.res_bl['type'],
126             'name': self.res_bl['type'],
127             'level': False,
128             'balance':self.res_bl['balance'],
129         }
130         for typ in types:
131             accounts_temp = []
132             for account in accounts:
133                 if (account.user_type.report_type) and (account.user_type.report_type == typ):
134                     account_dict = {
135                         'id': account.id,
136                         'code': account.code,
137                         'name': account.name,
138                         'level': account.level,
139                         'balance': account.balance != 0 and account.balance * account.user_type.sign or account.balance,
140                         'type': account.type,
141                     }
142                     currency = account.currency_id and account.currency_id or account.company_id.currency_id
143                     if typ == 'liability' and account.type <> 'view' and (account.debit <> account.credit):
144                         self.result_sum_dr += account.balance
145                     if typ == 'asset' and account.type <> 'view' and (account.debit <> account.credit):
146                         self.result_sum_cr += account.balance
147                     if data['form']['display_account'] == 'movement':
148                         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):
149                             accounts_temp.append(account_dict)
150                     elif data['form']['display_account'] == 'not_zero':
151                         if not currency_pool.is_zero(self.cr, self.uid, currency, account.balance):
152                             accounts_temp.append(account_dict)
153                     else:
154                         accounts_temp.append(account_dict)
155                     if account.id == data['form']['reserve_account_id']:
156                         pl_dict['level'] = account['level'] + 1
157                         accounts_temp.append(pl_dict)
158
159             self.result[typ] = accounts_temp
160             cal_list[typ]=self.result[typ]
161
162         if cal_list:
163             temp = {}
164             for i in range(0,max(len(cal_list['liability']),len(cal_list['asset']))):
165                 if i < len(cal_list['liability']) and i < len(cal_list['asset']):
166                     temp={
167                           'type': cal_list['liability'][i]['type'],
168                           'code': cal_list['liability'][i]['code'],
169                           'name': cal_list['liability'][i]['name'],
170                           'level': cal_list['liability'][i]['level'],
171                           'balance':cal_list['liability'][i]['balance'],
172                           'type1': cal_list['asset'][i]['type'],
173                           'code1': cal_list['asset'][i]['code'],
174                           'name1': cal_list['asset'][i]['name'],
175                           'level1': cal_list['asset'][i]['level'],
176                           'balance1':cal_list['asset'][i]['balance'],
177                           }
178                     self.result_temp.append(temp)
179                 else:
180                     if i < len(cal_list['asset']):
181                         temp={
182                               'type': '',
183                               'code': '',
184                               'name': '',
185                               'level': False,
186                               'balance':False,
187                               'type1': cal_list['asset'][i]['type'],
188                               'code1': cal_list['asset'][i]['code'],
189                               'name1': cal_list['asset'][i]['name'],
190                               'level1': cal_list['asset'][i]['level'],
191                               'balance1':cal_list['asset'][i]['balance'],
192                           }
193                         self.result_temp.append(temp)
194                     if  i < len(cal_list['liability']):
195                         temp={
196                               'type': cal_list['liability'][i]['type'],
197                               'code': cal_list['liability'][i]['code'],
198                               'name': cal_list['liability'][i]['name'],
199                               'level': cal_list['liability'][i]['level'],
200                               'balance':cal_list['liability'][i]['balance'],
201                               'type1': '',
202                               'code1': '',
203                               'name1': '',
204                               'level1': False,
205                               'balance1':False,
206                           }
207                         self.result_temp.append(temp)
208         return None
209
210     def get_lines(self):
211         return self.result_temp
212
213     def get_lines_another(self, group):
214         return self.result.get(group, [])
215
216 report_sxw.report_sxw('report.account.balancesheet.horizontal', 'account.account',
217     'addons/account/report/account_balance_sheet_horizontal.rml',parser=report_balancesheet_horizontal,
218     header='internal landscape')
219
220 report_sxw.report_sxw('report.account.balancesheet', 'account.account',
221     'addons/account/report/account_balance_sheet.rml',parser=report_balancesheet_horizontal,
222     header='internal')
223
224 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: