[MODULE] +v1.1.0 from https://www.odoo.com/apps/7.0/account_financial_report_webkit/
[burette/account_financial_report_webkit.git] / wizard / general_ledger_wizard.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Author: Nicolas Bessi, Guewen Baconnier
5 # Copyright Camptocamp SA 2011
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.osv import fields, orm
25
26
27 class AccountReportGeneralLedgerWizard(orm.TransientModel):
28 """Will launch general ledger report and pass required args"""
29
30 _inherit = "account.common.account.report"
31 _name = "general.ledger.webkit"
32 _description = "General Ledger Report"
33
34 def _get_account_ids(self, cr, uid, context=None):
35 res = False
36 if context.get('active_model', False) == 'account.account' and context.get('active_ids', False):
37 res = context['active_ids']
38 return res
39
40 _columns = {
41 'amount_currency': fields.boolean("With Currency",
42 help="It adds the currency column"),
43
44 'display_account': fields.selection([('bal_all', 'All'),
45 ('bal_mix', 'With transactions or non zero balance')],
46 'Display accounts',
47 required=True),
48 'account_ids': fields.many2many('account.account', string='Filter on accounts',
49 help="""Only selected accounts will be printed. Leave empty to print all accounts."""),
50 'centralize': fields.boolean('Activate Centralization', help='Uncheck to display all the details of centralized accounts.')
51 }
52 _defaults = {
53 'amount_currency': False,
54 'display_account': 'bal_mix',
55 'account_ids': _get_account_ids,
56 'centralize': True,
57 }
58
59 def _check_fiscalyear(self, cr, uid, ids, context=None):
60 obj = self.read(cr, uid, ids[0], ['fiscalyear_id', 'filter'], context=context)
61 if not obj['fiscalyear_id'] and obj['filter'] == 'filter_no':
62 return False
63 return True
64
65 _constraints = [
66 (_check_fiscalyear, 'When no Fiscal year is selected, you must choose to filter by periods or by date.', ['filter']),
67 ]
68
69 def pre_print_report(self, cr, uid, ids, data, context=None):
70 data = super(AccountReportGeneralLedgerWizard, self).pre_print_report(cr, uid, ids, data, context)
71 # will be used to attach the report on the main account
72 data['ids'] = [data['form']['chart_account_id']]
73 vals = self.read(cr, uid, ids,
74 ['amount_currency',
75 'display_account',
76 'account_ids',
77 'centralize'],
78 context=context)[0]
79 data['form'].update(vals)
80 return data
81
82 def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
83 res = {}
84 if filter == 'filter_no':
85 res['value'] = {
86 'period_from': False,
87 'period_to': False,
88 'date_from': False,
89 'date_to': False,
90 }
91 if filter == 'filter_date':
92 if fiscalyear_id:
93 fyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_id, context=context)
94 date_from = fyear.date_start
95 date_to = fyear.date_stop > time.strftime('%Y-%m-%d') and time.strftime('%Y-%m-%d') or fyear.date_stop
96 else:
97 date_from, date_to = time.strftime('%Y-01-01'), time.strftime('%Y-%m-%d')
98 res['value'] = {
99 'period_from': False,
100 'period_to': False,
101 'date_from': date_from,
102 'date_to': date_to
103 }
104 if filter == 'filter_period' and fiscalyear_id:
105 start_period = end_period = False
106 cr.execute('''
107 SELECT * FROM (SELECT p.id
108 FROM account_period p
109 LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
110 WHERE f.id = %s
111 AND COALESCE(p.special, FALSE) = FALSE
112 ORDER BY p.date_start ASC
113 LIMIT 1) AS period_start
114 UNION ALL
115 SELECT * FROM (SELECT p.id
116 FROM account_period p
117 LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
118 WHERE f.id = %s
119 AND p.date_start < NOW()
120 AND COALESCE(p.special, FALSE) = FALSE
121 ORDER BY p.date_stop DESC
122 LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
123 periods = [i[0] for i in cr.fetchall()]
124 if periods:
125 start_period = end_period = periods[0]
126 if len(periods) > 1:
127 end_period = periods[1]
128 res['value'] = {'period_from': start_period, 'period_to': end_period, 'date_from': False, 'date_to': False}
129 return res
130
131 def _print_report(self, cursor, uid, ids, data, context=None):
132 # we update form with display account value
133 data = self.pre_print_report(cursor, uid, ids, data, context=context)
134 return {'type': 'ir.actions.report.xml',
135 'report_name': 'account.account_report_general_ledger_webkit',
136 'datas': data}