Launchpad automatic translations update.
[odoo/odoo.git] / addons / account / report / account_balance.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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 from openerp.report import report_sxw
25 from common_report_header import common_report_header
26
27 class account_balance(report_sxw.rml_parse, common_report_header):
28     _name = 'report.account.account.balance'
29
30     def __init__(self, cr, uid, name, context=None):
31         super(account_balance, self).__init__(cr, uid, name, context=context)
32         self.sum_debit = 0.00
33         self.sum_credit = 0.00
34         self.date_lst = []
35         self.date_lst_string = ''
36         self.result_acc = []
37         self.localcontext.update({
38             'time': time,
39             'lines': self.lines,
40             'sum_debit': self._sum_debit,
41             'sum_credit': self._sum_credit,
42             'get_fiscalyear':self._get_fiscalyear,
43             'get_filter': self._get_filter,
44             'get_start_period': self.get_start_period,
45             'get_end_period': self.get_end_period ,
46             'get_account': self._get_account,
47             'get_journal': self._get_journal,
48             'get_start_date':self._get_start_date,
49             'get_end_date':self._get_end_date,
50             'get_target_move': self._get_target_move,
51         })
52         self.context = context
53
54     def set_context(self, objects, data, ids, report_type=None):
55         new_ids = ids
56         if (data['model'] == 'ir.ui.menu'):
57             new_ids = 'chart_account_id' in data['form'] and [data['form']['chart_account_id']] or []
58             objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
59         return super(account_balance, self).set_context(objects, data, new_ids, report_type=report_type)
60
61     #def _add_header(self, node, header=1):
62     #    if header == 0:
63     #        self.rml_header = ""
64     #    return True
65
66     def _get_account(self, data):
67         if data['model']=='account.account':
68             return self.pool.get('account.account').browse(self.cr, self.uid, data['form']['id']).company_id.name
69         return super(account_balance ,self)._get_account(data)
70
71     def lines(self, form, ids=None, done=None):
72         def _process_child(accounts, disp_acc, parent):
73                 account_rec = [acct for acct in accounts if acct['id']==parent][0]
74                 currency_obj = self.pool.get('res.currency')
75                 acc_id = self.pool.get('account.account').browse(self.cr, self.uid, account_rec['id'])
76                 currency = acc_id.currency_id and acc_id.currency_id or acc_id.company_id.currency_id
77                 res = {
78                     'id': account_rec['id'],
79                     'type': account_rec['type'],
80                     'code': account_rec['code'],
81                     'name': account_rec['name'],
82                     'level': account_rec['level'],
83                     'debit': account_rec['debit'],
84                     'credit': account_rec['credit'],
85                     'balance': account_rec['balance'],
86                     'parent_id': account_rec['parent_id'],
87                     'bal_type': '',
88                 }
89                 self.sum_debit += account_rec['debit']
90                 self.sum_credit += account_rec['credit']
91                 if disp_acc == 'movement':
92                     if not currency_obj.is_zero(self.cr, self.uid, currency, res['credit']) or not currency_obj.is_zero(self.cr, self.uid, currency, res['debit']) or not currency_obj.is_zero(self.cr, self.uid, currency, res['balance']):
93                         self.result_acc.append(res)
94                 elif disp_acc == 'not_zero':
95                     if not currency_obj.is_zero(self.cr, self.uid, currency, res['balance']):
96                         self.result_acc.append(res)
97                 else:
98                     self.result_acc.append(res)
99                 if account_rec['child_id']:
100                     for child in account_rec['child_id']:
101                         _process_child(accounts,disp_acc,child)
102
103         obj_account = self.pool.get('account.account')
104         if not ids:
105             ids = self.ids
106         if not ids:
107             return []
108         if not done:
109             done={}
110
111         ctx = self.context.copy()
112
113         ctx['fiscalyear'] = form['fiscalyear_id']
114         if form['filter'] == 'filter_period':
115             ctx['period_from'] = form['period_from']
116             ctx['period_to'] = form['period_to']
117         elif form['filter'] == 'filter_date':
118             ctx['date_from'] = form['date_from']
119             ctx['date_to'] =  form['date_to']
120         ctx['state'] = form['target_move']
121         parents = ids
122         child_ids = obj_account._get_children_and_consol(self.cr, self.uid, ids, ctx)
123         if child_ids:
124             ids = child_ids
125         accounts = obj_account.read(self.cr, self.uid, ids, ['type','code','name','debit','credit','balance','parent_id','level','child_id'], ctx)
126
127         for parent in parents:
128                 if parent in done:
129                     continue
130                 done[parent] = 1
131                 _process_child(accounts,form['display_account'],parent)
132         return self.result_acc
133
134 report_sxw.report_sxw('report.account.account.balance', 'account.account', 'addons/account/report/account_balance.rml', parser=account_balance, header="internal")
135
136 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: