[FIX] Security fixes for sql injections
[odoo/odoo.git] / addons / account / wizard / account_balance_report.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 import time
22
23 from osv import osv, fields
24 from tools.translate import _
25
26 class account_balance_report(osv.osv_memory):
27     """
28     This wizard will provide the account balance report by periods, between any two dates.
29     """
30     _name = 'account.balance.report'
31     _description = 'Account Balance Report'
32     _columns = {
33         'Account_list': fields.many2one('account.account', 'Chart account',
34                                 required=True, domain = [('parent_id','=',False)]),
35         'company_id': fields.many2one('res.company', 'Company', required=True),
36         'display_account': fields.selection([('bal_mouvement','With movements'),
37                                  ('bal_all','All'),
38                                  ('bal_solde','With balance is not equal to 0'),
39                                  ],'Display accounts'),
40         'fiscalyear': fields.many2one('account.fiscalyear', 'Fiscal year', help='Keep empty for all open fiscal year'),
41         'state': fields.selection([('bydate','By Date'),
42                                  ('byperiod','By Period'),
43                                  ('all','By Date and Period'),
44                                  ('none','No Filter')
45                                  ],'Date/Period Filter'),
46         'periods': fields.many2many('account.period', 'period_account_balance_rel',
47                                     'report_id', 'period_id', 'Periods',
48                                     help='Keep empty for all open fiscal year'),
49         'date_from': fields.date('Start date', required=True),
50         'date_to': fields.date('End date', required=True),
51         }
52
53     def _get_company(self, cr, uid, context=None):
54         user_obj = self.pool.get('res.users')
55         company_obj = self.pool.get('res.company')
56         user = user_obj.browse(cr, uid, uid, context=context)
57         if user.company_id:
58             return user.company_id.id
59         else:
60             return company_obj.search(cr, uid, [('parent_id', '=', False)])[0]
61
62     _defaults={
63         'state' : 'none',
64         'date_from' : time.strftime('%Y-01-01'),
65         'date_to' : time.strftime('%Y-%m-%d'),
66         'company_id' : _get_company,
67         'fiscalyear' : False,
68         'display_account': 'bal_all',
69         }
70
71     def next_view(self, cr, uid, ids, context=None):
72         obj_model = self.pool.get('ir.model.data')
73         if context is None:
74             context = {}
75         data = self.read(cr, uid, ids, [])[0]
76         context.update({'Account_list': data['Account_list']})
77         model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','account_balance_report_view')])
78         resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])[0]['res_id']
79         return {
80             'view_type': 'form',
81             'view_mode': 'form',
82             'res_model': 'account.balance.report',
83             'views': [(resource_id,'form')],
84             'type': 'ir.actions.act_window',
85             'target': 'new',
86             'context': context
87         }
88
89     def check_state(self, cr, uid, ids, context=None):
90         if context is None:
91             context = {}
92         data={}
93         data['ids'] = context['active_ids']
94         data['form'] = self.read(cr, uid, ids, ['date_from',  'company_id',  'state', 'periods', 'date_to',  'display_account',  'fiscalyear'])[0]
95         data['form']['Account_list'] = context.get('Account_list',[])
96         data['form']['context'] = context
97         if data['form']['Account_list']:
98             data['model'] = 'ir.ui.menu'
99         else:
100             data['model'] = 'account.account'
101
102         if data['form']['state'] == 'bydate'  :
103            return self._check_date(cr, uid, data, context)
104         elif data['form']['state'] == 'byperiod':
105             if not data['form']['periods']:
106                 raise  osv.except_osv(_('Warning'),_('Please Enter Periods ! '))
107
108         return {
109             'type': 'ir.actions.report.xml',
110             'report_name': 'account.account.balance',
111             'datas': data,
112             'nodestroy':True,
113             }
114
115     def _check_date(self, cr, uid, data, context=None):
116         if context is None:
117             context = {}
118         sql = """
119             SELECT f.id, f.date_start, f.date_stop FROM account_fiscalyear f  Where %s between f.date_start and f.date_stop """
120         cr.execute(sql, (data['form']['date_from'],))
121         res = cr.dictfetchall()
122         if res:
123
124             if (data['form']['date_to'] > res[0]['date_stop'] or data['form']['date_to'] < res[0]['date_start']):
125                 raise  osv.except_osv(_('UserError'),_('Date to must be set between %s and %s') % (res[0]['date_start'], res[0]['date_stop']))
126             else:
127                 return {
128                 'type': 'ir.actions.report.xml',
129                 'report_name': 'account.account.balance',
130                 'datas': data,
131                 'nodestroy':True,
132                     }
133         else:
134             raise osv.except_osv(_('UserError'),_('Date not in a defined fiscal year'))
135
136 account_balance_report()
137
138 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: