[MODULE] +v1.1.0 from https://www.odoo.com/apps/7.0/account_financial_report_webkit/
[burette/account_financial_report_webkit.git] / wizard / open_invoices_wizard.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Author: Guewen Baconnier
5 # Copyright Camptocamp SA 2012
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 from openerp.osv import fields, orm
22
23
24 class AccountReportOpenInvoicesWizard(orm.TransientModel):
25 """Will launch partner ledger report and pass required args"""
26
27 _inherit = "partners.ledger.webkit"
28 _name = "open.invoices.webkit"
29 _description = "Open Invoices Report"
30
31 _columns = {
32 'group_by_currency': fields.boolean('Group Partner by currency'),
33 'until_date': fields.date("Clearance date",
34 required=True,
35 help="""The clearance date is essentially a tool used for debtors provisionning calculation.
36
37 By default, this date is equal to the the end date (ie: 31/12/2011 if you select fy 2011).
38
39 By amending the clearance date, you will be, for instance, able to answer the question : 'based on my last year end debtors open invoices, which invoices are still unpaid today (today is my clearance date)?'
40 """)}
41
42 def _check_until_date(self, cr, uid, ids, context=None):
43 def get_key_id(obj, field):
44 return obj.get(field) and obj[field][0] or False
45
46 obj = self.read(cr, uid, ids[0], ['fiscalyear_id', 'period_to', 'date_to', 'until_date'], context=context)
47 min_date = self.default_until_date(cr, uid, ids,
48 get_key_id(obj, 'fiscalyear_id'),
49 get_key_id(obj, 'period_to'),
50 obj['date_to'],
51 context=context)
52 if min_date and obj['until_date'] < min_date:
53 return False
54 return True
55
56 _constraints = [
57 (_check_until_date, 'Clearance date must be the very last date of the last period or later.', ['until_date']),
58 ]
59
60 def default_until_date(self, cr, uid, ids, fiscalyear_id=False, period_id=False, date_to=False, context=None):
61 res_date = False
62 # first priority: period or date filters
63 if period_id:
64 res_date = self.pool.get('account.period').read(cr, uid, period_id, ['date_stop'], context=context)['date_stop']
65 elif date_to:
66 res_date = date_to
67 elif fiscalyear_id:
68 res_date = self.pool.get('account.fiscalyear').read(cr, uid, fiscalyear_id, ['date_stop'], context=context)['date_stop']
69 return res_date
70
71 def onchange_fiscalyear(self, cr, uid, ids, fiscalyear=False, period_id=False, date_to=False, until_date=False, context=None):
72 res = {'value': {}}
73 res['value']['until_date'] = self.default_until_date(cr, uid, ids,
74 fiscalyear_id=fiscalyear,
75 period_id=period_id,
76 date_to=date_to,
77 context=context)
78 return res
79
80 def onchange_date_to(self, cr, uid, ids, fiscalyear=False, period_id=False, date_to=False, until_date=False, context=None):
81 res = {'value': {}}
82 res['value']['until_date'] = self.default_until_date(cr, uid, ids,
83 fiscalyear_id=fiscalyear,
84 period_id=period_id,
85 date_to=date_to,
86 context=context)
87 return res
88
89 def onchange_period_to(self, cr, uid, ids, fiscalyear=False, period_id=False, date_to=False, until_date=False, context=None):
90 res = {'value': {}}
91 res['value']['until_date'] = self.default_until_date(cr, uid, ids,
92 fiscalyear_id=fiscalyear,
93 period_id=period_id,
94 date_to=date_to,
95 context=context)
96 return res
97
98 def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
99 res = super(AccountReportOpenInvoicesWizard, self).onchange_filter(cr, uid, ids, filter=filter, fiscalyear_id=fiscalyear_id, context=context)
100 if res.get('value', False):
101 res['value']['until_date'] = self.default_until_date(cr, uid, ids,
102 fiscalyear_id=fiscalyear_id,
103 period_id=res['value'].get('period_to', False),
104 date_to=res['value'].get('date_to', False),
105 context=context)
106 return res
107
108 def pre_print_report(self, cr, uid, ids, data, context=None):
109 data = super(AccountReportOpenInvoicesWizard, self).pre_print_report(cr, uid, ids, data, context)
110 vals = self.read(cr, uid, ids,
111 ['until_date', 'group_by_currency'],
112 context=context)[0]
113 data['form'].update(vals)
114 return data
115
116 def _print_report(self, cr, uid, ids, data, context=None):
117 # we update form with display account value
118 data = self.pre_print_report(cr, uid, ids, data, context=context)
119 return {'type': 'ir.actions.report.xml',
120 'report_name': 'account.account_report_open_invoices_webkit',
121 'datas': data}