From 55b31f9cbd3e4c33cf15f9fc6761ea2761f715fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Tue, 28 Apr 2015 15:57:52 +0200 Subject: [PATCH] [IMP] mis_builder: refactor to compute period only for modes actually used --- mis_builder/models/aep.py | 50 ++++++++++++++++--------------- mis_builder/models/mis_builder.py | 36 +++------------------- 2 files changed, 30 insertions(+), 56 deletions(-) diff --git a/mis_builder/models/aep.py b/mis_builder/models/aep.py index 500174ec..9b77d525 100644 --- a/mis_builder/models/aep.py +++ b/mis_builder/models/aep.py @@ -157,17 +157,15 @@ class AccountingExpressionProcessor(object): def get_aml_domain_for_expr(self, expr): """ Get a domain on account.move.line for an expression. - Only accounting expression in mode 'p' and 'e' are considered. - Prerequisite: done_parsing() must have been invoked. Returns a domain that can be used to search on account.move.line. + To be meaningful, this domain must be extended with a domain + obtained from get_aml_domain_for_dates() """ aml_domains = [] for mo in self.ACC_RE.finditer(expr): - field, mode, account_codes, domain = self._parse_match_object(mo) - if mode == MODE_INITIAL: - continue + field, _, account_codes, domain = self._parse_match_object(mo) aml_domain = list(domain) if account_codes: account_ids = set() @@ -181,11 +179,6 @@ class AccountingExpressionProcessor(object): aml_domains.append(expression.normalize_domain(aml_domain)) return expression.OR(aml_domains) - def get_aml_domain_for_dates(self, date_start, date_end, mode): - if mode != MODE_VARIATION: - raise RuntimeError("") # TODO - return [('date', '>=', date_start), ('date', '<=', date_end)] - def _period_has_moves(self, period): move_model = self.env['account.move'] return bool(move_model.search([('period_id', '=', period.id)], @@ -277,27 +270,36 @@ class AccountingExpressionProcessor(object): period_from, period_to, company_id)) return period_ids - def get_aml_domain_for_periods(self, period_from, period_to, mode): - period_ids = self._get_period_ids_for_mode( - period_from, period_to, mode) - return [('period_id', 'in', period_ids)] + def get_aml_domain_for_dates(self, date_from, date_to, + period_from, period_to, + mode, + target_move): + if period_from and period_to: + period_ids = self._get_period_ids_for_mode( + period_from, period_to, mode) + domain = [('period_id', 'in', period_ids)] + else: + domain = [('date', '>=', date_from), ('date', '<=', date_to)] + if target_move == 'posted': + domain.append(('move_id.state', '=', 'posted')) + return domain - def do_queries(self, period_domain, period_domain_i, period_domain_e): + def do_queries(self, date_from, date_to, period_from, period_to, + target_move): aml_model = self.env['account.move.line'] # {(domain, mode): {account_id: (debit, credit)}} self._data = defaultdict(dict) - # fetch sum of debit/credit, grouped by account_id + domain_by_mode = {} for key in self._map_account_ids: domain, mode = key - if mode == MODE_VARIATION: - domain = list(domain) + period_domain - elif mode == MODE_INITIAL: - domain = list(domain) + period_domain_i - elif mode == MODE_END: - domain = list(domain) + period_domain_e - else: - raise RuntimeError("unexpected mode %s" % (mode,)) + if mode not in domain_by_mode: + domain_by_mode[mode] = \ + self.get_aml_domain_for_dates(date_from, date_to, + period_from, period_to, + mode, target_move) + domain = list(domain) + domain_by_mode[mode] domain.append(('account_id', 'in', self._map_account_ids[key])) + # fetch sum of debit/credit, grouped by account_id accs = aml_model.read_group(domain, ['debit', 'credit', 'account_id'], ['account_id']) diff --git a/mis_builder/models/mis_builder.py b/mis_builder/models/mis_builder.py index 767d40ca..0912eac2 100644 --- a/mis_builder/models/mis_builder.py +++ b/mis_builder/models/mis_builder.py @@ -37,9 +37,6 @@ from openerp.tools.safe_eval import safe_eval from openerp.tools.translate import _ from .aep import AccountingExpressionProcessor -from .aep import MODE_VARIATION -from .aep import MODE_END -from .aep import MODE_INITIAL class AutoStruct(object): @@ -379,7 +376,6 @@ class mis_report_instance_period(orm.Model): all_period_ids = period_obj.search( cr, uid, [('special', '=', False), - '|', ('company_id', '=', False), ('company_id', '=', c.company_id.id)], order='date_start', context=context) @@ -388,7 +384,6 @@ class mis_report_instance_period(orm.Model): [('special', '=', False), ('date_start', '<=', d), ('date_stop', '>=', d), - '|', ('company_id', '=', False), ('company_id', '=', c.company_id.id)], context=context) if not current_period_ids: @@ -527,26 +522,6 @@ class mis_report_instance_period(orm.Model): else: return False - def compute_period_domain(self, cr, uid, period_report, aep, mode, - context=None): - domain = [] - target_move = period_report.report_instance_id.target_move - if target_move == 'posted': - domain.append(('move_id.state', '=', target_move)) - if not period_report.period_from.id or not period_report.period_to.id: - aml_domain = aep\ - .get_aml_domain_for_periods(period_report.date_from, - period_report.date_to, - mode) - domain.extend(aml_domain) - elif period_report.period_from.id and period_report.period_to.id: - aml_domain = aep\ - .get_aml_domain_for_periods(period_report.period_from, - period_report.period_to, - mode) - domain.extend(aml_domain) - return domain - def _fetch_queries(self, cr, uid, c, context): res = {} report = c.report_instance_id.report_id @@ -589,16 +564,13 @@ class mis_report_instance_period(orm.Model): 'len': len, 'avg': lambda l: sum(l) / float(len(l)), } - domain_p = self.compute_period_domain(cr, uid, c, aep, MODE_VARIATION, - context=context) - domain_e = self.compute_period_domain(cr, uid, c, aep, MODE_END, - context=context) - domain_i = self.compute_period_domain(cr, uid, c, aep, MODE_INITIAL, - context=context) - aep.do_queries(domain_p, domain_i, domain_e) localdict.update(self._fetch_queries(cr, uid, c, context=context)) + aep.do_queries(c.date_from, c.date_to, + c.period_from, c.period_to, + c.report_instance_id.target_move) + compute_queue = c.report_instance_id.report_id.kpi_ids recompute_queue = [] while True: