[FIX] Account: Compare_context added in wizard
[odoo/odoo.git] / addons / account / wizard / account_report_common.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 from lxml import etree
24
25 from osv import fields, osv
26 from tools.translate import _
27
28 class account_common_report(osv.osv_memory):
29     _name = "account.common.report"
30     _description = "Account Common Report"
31
32     _columns = {
33         'chart_account_id': fields.many2one('account.account', 'Chart of Account', help='Select Charts of Accounts', required=True, domain = [('parent_id','=',False)]),
34         'fiscalyear_id': fields.many2one('account.fiscalyear', 'Fiscal Year', help='Keep empty for all open fiscal year'),
35         'filter': fields.selection([('filter_no', 'No Filters'), ('filter_date', 'Date'), ('filter_period', 'Periods')], "Filter by", required=True),
36         'period_from': fields.many2one('account.period', 'Start Period'),
37         'period_to': fields.many2one('account.period', 'End Period'),
38         'journal_ids': fields.many2many('account.journal', 'account_common_journal_rel', 'account_id', 'journal_id', 'Journals', required=True),
39         'date_from': fields.date("Start Date"),
40         'date_to': fields.date("End Date"),
41         'target_move': fields.selection([('posted', 'All Posted Entries'),
42                                          ('all', 'All Entries'),
43                                         ], 'Target Moves', required=True),
44
45         }
46
47     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
48         res = super(account_common_report, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=False)
49         if context.get('active_model', False) == 'account.account' and view_id:
50             doc = etree.XML(res['arch'])
51             nodes = doc.xpath("//field[@name='chart_account_id']")
52             for node in nodes:
53                 node.set('readonly', '1')
54                 node.set('help', 'If you print the report from Account list/form view it will not consider Charts of account')
55             res['arch'] = etree.tostring(doc)
56         return res
57
58     def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
59         res = {'value': {}}
60         if filter == 'filter_no':
61             res['value'] = {'period_from': False, 'period_to': False, 'date_from': False ,'date_to': False}
62         if filter == 'filter_date':
63             res['value'] = {'period_from': False, 'period_to': False, 'date_from': time.strftime('%Y-01-01'), 'date_to': time.strftime('%Y-%m-%d')}
64         if filter == 'filter_period' and fiscalyear_id:
65             start_period = end_period = False
66             cr.execute('''
67                 SELECT * FROM (SELECT p.id
68                                FROM account_period p
69                                LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
70                                WHERE f.id = %s
71                                ORDER BY p.date_start ASC, p.special ASC
72                                LIMIT 1) AS period_start
73                 UNION
74                 SELECT * FROM (SELECT p.id
75                                FROM account_period p
76                                LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
77                                WHERE f.id = %s
78                                AND p.date_start < NOW()
79                                ORDER BY p.date_stop DESC
80                                LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
81             periods =  [i[0] for i in cr.fetchall()]
82             if periods and len(periods) > 1:
83                 start_period = periods[0]
84                 end_period = periods[1]
85             res['value'] = {'period_from': start_period, 'period_to': end_period, 'date_from': False, 'date_to': False}
86         return res
87
88     def _get_account(self, cr, uid, context=None):
89         accounts = self.pool.get('account.account').search(cr, uid, [('parent_id', '=', False)], limit=1)
90         return accounts and accounts[0] or False
91
92     def _get_fiscalyear(self, cr, uid, context=None):
93         now = time.strftime('%Y-%m-%d')
94         fiscalyears = self.pool.get('account.fiscalyear').search(cr, uid, [('date_start', '<', now), ('date_stop', '>', now)], limit=1 )
95         return fiscalyears and fiscalyears[0] or False
96
97     def _get_all_journal(self, cr, uid, context=None):
98         return self.pool.get('account.journal').search(cr, uid ,[])
99
100     _defaults = {
101             'fiscalyear_id': _get_fiscalyear,
102             'journal_ids': _get_all_journal,
103             'filter': 'filter_no',
104             'chart_account_id': _get_account,
105             'target_move': 'posted',
106     }
107
108     def _build_contexts(self, cr, uid, ids, data, context=None):
109         if context is None:
110             context = {}
111         result = {}
112         result['fiscalyear'] = 'fiscalyear_id' in data['form'] and data['form']['fiscalyear_id'] or False
113         result['journal_ids'] = 'journal_ids' in data['form'] and data['form']['journal_ids'] or False
114         result['chart_account_id'] = 'chart_account_id' in data['form'] and data['form']['chart_account_id'] or False
115         if data['form']['filter'] == 'filter_date':
116             result['date_from'] = data['form']['date_from']
117             result['date_to'] = data['form']['date_to']
118         elif data['form']['filter'] == 'filter_period':
119             if not data['form']['period_from'] or not data['form']['period_to']:
120                 raise osv.except_osv(_('Error'),_('Select a starting and an ending period'))
121             result['period_from'] = data['form']['period_from']
122             result['period_to'] = data['form']['period_to']
123         return result
124
125     def _print_report(self, cr, uid, ids, data, context=None):
126         raise (_('Error'), _('not implemented'))
127
128     def check_report(self, cr, uid, ids, context=None):
129         if context is None:
130             context = {}
131         data = {}
132         data['ids'] = context.get('active_ids', [])
133         data['model'] = context.get('active_model', 'ir.ui.menu')
134         data['form'] = self.read(cr, uid, ids, ['date_from',  'date_to',  'fiscalyear_id', 'journal_ids', 'period_from', 'period_to',  'filter',  'chart_account_id', 'target_move'], context=context)[0]
135         for field in ['fiscalyear_id', 'chart_account_id', 'period_from', 'period_to']:
136             if isinstance(data['form'][field], tuple):
137                 data['form'][field] = data['form'][field][0]
138         used_context = self._build_contexts(cr, uid, ids, data, context=context)
139         data['form']['periods'] = used_context.get('periods', False) and used_context['periods'] or []
140         data['form']['used_context'] = used_context
141         return self._print_report(cr, uid, ids, data, context=context)
142
143 account_common_report()
144
145 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: