[FIX] account, financial reports: display flat must not print view accounts
[odoo/odoo.git] / addons / account / report / account_financial_report.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 from report import report_sxw
24 from common_report_header import common_report_header
25 from tools.translate import _
26
27 class report_account_common(report_sxw.rml_parse, common_report_header):
28
29     def __init__(self, cr, uid, name, context=None):
30         super(report_account_common, self).__init__(cr, uid, name, context=context)
31         self.localcontext.update( {
32             'get_lines': self.get_lines,
33             'time': time,
34             'get_fiscalyear': self._get_fiscalyear,
35             'get_account': self._get_account,
36             'get_start_period': self.get_start_period,
37             'get_end_period': self.get_end_period,
38             'get_filter': self._get_filter,
39             'get_start_date':self._get_start_date,
40             'get_end_date':self._get_end_date,
41         })
42         self.context = context
43
44     def set_context(self, objects, data, ids, report_type=None):
45         new_ids = ids
46         if (data['model'] == 'ir.ui.menu'):
47             new_ids = 'chart_account_id' in data['form'] and [data['form']['chart_account_id']] or []
48             objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
49         return super(report_account_common, self).set_context(objects, data, new_ids, report_type=report_type)
50
51     def get_lines(self, data):
52         lines = []
53         account_obj = self.pool.get('account.account')
54         currency_obj = self.pool.get('res.currency')
55         ids2 = self.pool.get('account.financial.report')._get_children_by_order(self.cr, self.uid, [data['form']['account_report_id'][0]], context=data['form']['used_context'])
56         for report in self.pool.get('account.financial.report').browse(self.cr, self.uid, ids2, context=data['form']['used_context']):
57             vals = {
58                 'name': report.name,
59                 'balance': report.balance,
60                 'type': 'report',
61                 'level': report.level,
62             }
63             if data['form']['enable_filter']:
64                 vals['balance_cmp'] = self.pool.get('account.financial.report').browse(self.cr, self.uid, report.id, context=data['form']['comparison_context']).balance
65             lines.append(vals)
66             account_ids = []
67             if report.type == 'accounts' and report.account_ids:
68                 account_ids = account_obj._get_children_and_consol(self.cr, self.uid, [x.id for x in report.account_ids])
69             elif report.type == 'account_type' and report.account_type_ids:
70                 account_ids = account_obj.search(self.cr, self.uid, [('user_type','in', [x.id for x in report.account_type_ids])])
71             if account_ids:
72                 for account in account_obj.browse(self.cr, self.uid, account_ids, context=data['form']['used_context']):
73                     if report.display_detail == 'only_detail' and account.type == 'view':
74                         continue
75                     flag = False
76                     vals = {
77                         'name': account.code + ' ' + account.name,
78                         'balance':  account.balance != 0 and account.balance * report.sign or account.balance,
79                         'type': 'account',
80                         'level': report.display_detail == 'detail_with_hierarchy' and min(account.level,6) or 6,
81                         'account_type': account.type,
82                     }
83                     if not currency_obj.is_zero(self.cr, self.uid, account.company_id.currency_id, vals['balance']):
84                         flag = True
85                     if data['form']['enable_filter']:
86                         vals['balance_cmp'] = account_obj.browse(self.cr, self.uid, account.id, context=data['form']['comparison_context']).balance
87                         if not currency_obj.is_zero(self.cr, self.uid, account.company_id.currency_id, vals['balance_cmp']):
88                             flag = True
89                     if flag:
90                         lines.append(vals)
91         return lines
92
93 report_sxw.report_sxw('report.account.financial.report', 'account.financial.report',
94     'addons/account/report/account_financial_report.rml', parser=report_account_common, header='internal')
95
96
97 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: