From 43a242a083a47d11ed2a8003a7e216b642b25925 Mon Sep 17 00:00:00 2001 From: "qdp-launchpad@tinyerp.com" <> Date: Tue, 28 Sep 2010 11:06:38 +0200 Subject: [PATCH] [REF] account: some code refactoring + fix for multi company bzr revid: qdp-launchpad@tinyerp.com-20100928090638-smdqvhifn7upcz9r --- addons/account/account.py | 13 +++++++++++++ addons/account/wizard/account_chart.py | 14 +++----------- addons/account/wizard/account_report_common.py | 15 ++++----------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/addons/account/account.py b/addons/account/account.py index 909b1a1203c..5a0ce67b8a2 100644 --- a/addons/account/account.py +++ b/addons/account/account.py @@ -931,6 +931,19 @@ class account_period(osv.osv): raise osv.except_osv(_('Warning !'), _('You cannot modify company of this period as its related record exist in Entry Lines')) return super(account_period, self).write(cr, uid, ids, vals, context=context) + def build_ctx_periods(self, cr, uid, period_from_id, period_to_id): + period_from = self.browse(cr, uid, period_from_id) + period_date_start = period_from.date_start + company1_id = period_from.company_id.id + period_to = self.browse(cr, uid, period_to_id) + period_date_stop = period_to.date_stop + company2_id = period_to.company_id.id + if company1_id != company2_id: + raise osv.except_osv(_('Error'), _('You should have chosen periods that belongs to the same company')) + if period_date_start > period_date_stop: + raise osv.except_osv(_('Error'), _('Start period should be smaller then End period')) + return self.search(cr, uid, [('date_start', '>=', period_date_start), ('date_stop', '<=', period_date_stop), ('company_id', '=', company1_id)]) + account_period() class account_journal_period(osv.osv): diff --git a/addons/account/wizard/account_chart.py b/addons/account/wizard/account_chart.py index bf10838b9c8..b8e18659327 100644 --- a/addons/account/wizard/account_chart.py +++ b/addons/account/wizard/account_chart.py @@ -44,14 +44,6 @@ class account_chart(osv.osv_memory): fiscalyears = self.pool.get('account.fiscalyear').search(cr, uid, [('date_start', '<', now), ('date_stop', '>', now)], limit=1 ) return fiscalyears and fiscalyears[0] or False - def _build_periods(self, cr, uid, period_from, period_to): - period_obj = self.pool.get('account.period') - period_date_start = period_obj.read(cr, uid, period_from, ['date_start'])['date_start'] - period_date_stop = period_obj.read(cr, uid, period_to, ['date_stop'])['date_stop'] - if period_date_start > period_date_stop: - raise osv.except_osv(_('Error'),_('Start period should be smaller then End period')) - return period_obj.search(cr, uid, [('date_start', '>=', period_date_start), ('date_stop', '<=', period_date_stop)]) - def onchange_fiscalyear(self, cr, uid, ids, fiscalyear_id=False, context=None): res = {} res['value'] = {} @@ -95,9 +87,9 @@ class account_chart(osv.osv_memory): result = mod_obj._get_id(cr, uid, 'account', 'action_account_tree') id = mod_obj.read(cr, uid, [result], ['res_id'], context=context)[0]['res_id'] result = act_obj.read(cr, uid, [id], context=context)[0] - result['periods'] = [] + result['periods'] = [] if data['period_from'] and data['period_to']: - result['periods'] = self._build_periods(cr, uid, data['period_from'], data['period_to']) + result['periods'] = self.pool.get('account.period').build_ctx_periods(cr, uid, data['period_from'], data['period_to']) result['context'] = str({'fiscalyear': data['fiscalyear'], 'periods': result['periods'], \ 'state': data['target_move']}) if data['fiscalyear']: @@ -111,4 +103,4 @@ class account_chart(osv.osv_memory): account_chart() -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: \ No newline at end of file +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/account/wizard/account_report_common.py b/addons/account/wizard/account_report_common.py index e5ed39427b4..67e3900e208 100644 --- a/addons/account/wizard/account_report_common.py +++ b/addons/account/wizard/account_report_common.py @@ -103,14 +103,6 @@ class account_common_report(osv.osv_memory): 'chart_account_id': _get_account, } - def _build_periods(self, cr, uid, period_from, period_to): - period_obj = self.pool.get('account.period') - period_date_start = period_obj.read(cr, uid, period_from, ['date_start'])['date_start'] - period_date_stop = period_obj.read(cr, uid, period_to, ['date_stop'])['date_stop'] - if period_date_start > period_date_stop: - raise osv.except_osv(_('Error'),_('Start period should be smaller then End period')) - return period_obj.search(cr, uid, [('date_start', '>=', period_date_start), ('date_stop', '<=', period_date_stop)]) - def _build_contexts(self, cr, uid, ids, data, context=None): if context is None: context = {} @@ -129,9 +121,10 @@ class account_common_report(osv.osv_memory): elif data['form']['filter'] == 'filter_period': if not data['form']['period_from'] or not data['form']['period_to']: raise osv.except_osv(_('Error'),_('Select a starting and an ending period')) - result['periods'] = self._build_periods(cr, uid, data['form']['period_from'], data['form']['period_to']) - first_period = self.pool.get('account.period').search(cr, uid, [], order='date_start', limit=1)[0] - result_initial_bal['periods'] = self._build_periods(cr, uid, first_period, data['form']['period_from']) + company_id = period_obj.browse(cr, uid, data['form']['period_from'], context=context).company_id + result['periods'] = period_obj.build_ctx_periods(cr, uid, data['form']['period_from'], data['form']['period_to']) + first_period = self.pool.get('account.period').search(cr, uid, [('company_id', '=', company_id)], order='date_start', limit=1)[0] + result_initial_bal['periods'] = period_obj.build_ctx_periods(cr, uid, first_period, data['form']['period_from']) else: if data['form']['fiscalyear_id']: fiscal_date_start = fiscal_obj.browse(cr, uid, [data['form']['fiscalyear_id']], context=context)[0].date_start