[MODULE] +v1.1.0 from https://www.odoo.com/apps/7.0/account_financial_report_webkit/
[burette/account_financial_report_webkit.git] / wizard / print_journal.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 # account_financial_report_webkit module for OpenERP
5 # Copyright (C) 2012 SYLEAM Info Services (<http://www.syleam.fr/>)
6 # Sebastien LANGE <sebastien.lange@syleam.fr>
7 #
8 # This file is a part of account_financial_report_webkit
9 #
10 # account_financial_report_webkit is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU Affero General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # account_financial_report_webkit is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Affero General Public License for more details.
19 #
20 # You should have received a copy of the GNU Affero General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #
23 ##############################################################################
24
25 from openerp.osv import fields, orm
26 import time
27 from lxml import etree
28
29
30 class AccountReportPrintJournalWizard(orm.TransientModel):
31 """Will launch print journal report and pass requiered args"""
32
33 _inherit = "account.common.account.report"
34 _name = "print.journal.webkit"
35 _description = "Journals Report"
36
37 _columns = {
38 'amount_currency': fields.boolean("With Currency", help="It adds the currency column"),
39 }
40
41 _defaults = {
42 'amount_currency': False,
43 'journal_ids': False,
44 'filter': 'filter_period',
45 }
46
47 def _check_fiscalyear(self, cr, uid, ids, context=None):
48 obj = self.read(cr, uid, ids[0], ['fiscalyear_id', 'filter'], context=context)
49 if not obj['fiscalyear_id'] and obj['filter'] == 'filter_no':
50 return False
51 return True
52
53 _constraints = [
54 (_check_fiscalyear, 'When no Fiscal year is selected, you must choose to filter by periods or by date.', ['filter']),
55 ]
56
57 def pre_print_report(self, cr, uid, ids, data, context=None):
58 data = super(AccountReportPrintJournalWizard, self).pre_print_report(cr, uid, ids, data, context)
59 # will be used to attach the report on the main account
60 data['ids'] = [data['form']['chart_account_id']]
61 vals = self.read(cr, uid, ids,
62 ['amount_currency',
63 'display_account',
64 'journal_ids'],
65 context=context)[0]
66 data['form'].update(vals)
67 return data
68
69 def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
70 res = {}
71 if filter == 'filter_no':
72 res['value'] = {'period_from': False, 'period_to': False, 'date_from': False, 'date_to': False}
73 if filter == 'filter_date':
74 if fiscalyear_id:
75 fyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_id, context=context)
76 date_from = fyear.date_start
77 date_to = fyear.date_stop > time.strftime('%Y-%m-%d') and time.strftime('%Y-%m-%d') or fyear.date_stop
78 else:
79 date_from, date_to = time.strftime('%Y-01-01'), time.strftime('%Y-%m-%d')
80 res['value'] = {'period_from': False, 'period_to': False, 'date_from': date_from, 'date_to': date_to}
81 if filter == 'filter_period' and fiscalyear_id:
82 start_period = end_period = False
83 cr.execute('''
84 SELECT * FROM (SELECT p.id
85 FROM account_period p
86 LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
87 WHERE f.id = %s
88 AND COALESCE(p.special, FALSE) = FALSE
89 ORDER BY p.date_start ASC
90 LIMIT 1) AS period_start
91 UNION ALL
92 SELECT * FROM (SELECT p.id
93 FROM account_period p
94 LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
95 WHERE f.id = %s
96 AND p.date_start < NOW()
97 AND COALESCE(p.special, FALSE) = FALSE
98 ORDER BY p.date_stop DESC
99 LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
100 periods = [i[0] for i in cr.fetchall()]
101 if periods:
102 start_period = end_period = periods[0]
103 if len(periods) > 1:
104 end_period = periods[1]
105 res['value'] = {'period_from': start_period, 'period_to': end_period, 'date_from': False, 'date_to': False}
106 return res
107
108 def _print_report(self, cursor, uid, ids, data, context=None):
109 context = context or {}
110 # we update form with display account value
111 data = self.pre_print_report(cursor, uid, ids, data, context=context)
112 return {'type': 'ir.actions.report.xml',
113 'report_name': 'account.account_report_print_journal_webkit',
114 'datas': data}
115
116 AccountReportPrintJournalWizard()
117
118 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: