From 7e13cddca05b01d01c71f964ffcff7fdcced3afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Tue, 28 Apr 2015 16:19:22 +0200 Subject: [PATCH] [IMP] mis_builder: refactor drilldown to reuse period computation and work for all modes --- mis_builder/models/aep.py | 30 ++++++++++++++++++-------- mis_builder/models/mis_builder.py | 36 +++++++------------------------ 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/mis_builder/models/aep.py b/mis_builder/models/aep.py index 9b77d525..d181fa02 100644 --- a/mis_builder/models/aep.py +++ b/mis_builder/models/aep.py @@ -154,7 +154,13 @@ class AccountingExpressionProcessor(object): account_ids.update(self._account_ids_by_code[account_code]) self._map_account_ids[key] = list(account_ids) - def get_aml_domain_for_expr(self, expr): + def has_account_var(self, expr): + return bool(self.ACC_RE.match(expr)) + + def get_aml_domain_for_expr(self, expr, + date_from, date_to, + period_from, period_to, + target_move): """ Get a domain on account.move.line for an expression. Prerequisite: done_parsing() must have been invoked. @@ -164,20 +170,26 @@ class AccountingExpressionProcessor(object): obtained from get_aml_domain_for_dates() """ aml_domains = [] + domain_by_mode = {} for mo in self.ACC_RE.finditer(expr): - field, _, account_codes, domain = self._parse_match_object(mo) + field, mode, account_codes, domain = self._parse_match_object(mo) aml_domain = list(domain) - if account_codes: - account_ids = set() - for account_code in account_codes: - account_ids.update(self._account_ids_by_code[account_code]) - aml_domain.append(('account_id', 'in', tuple(account_ids))) + account_ids = set() + for account_code in account_codes: + account_ids.update(self._account_ids_by_code[account_code]) + aml_domain.append(('account_id', 'in', tuple(account_ids))) if field == 'crd': aml_domain.append(('credit', '>', 0)) elif field == 'deb': aml_domain.append(('debit', '>', 0)) aml_domains.append(expression.normalize_domain(aml_domain)) - return expression.OR(aml_domains) + 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) + return expression.OR(aml_domains) + \ + expression.OR(domain_by_mode.values()) def _period_has_moves(self, period): move_model = self.env['account.move'] @@ -282,7 +294,7 @@ class AccountingExpressionProcessor(object): domain = [('date', '>=', date_from), ('date', '<=', date_to)] if target_move == 'posted': domain.append(('move_id.state', '=', 'posted')) - return domain + return expression.normalize_domain(domain) def do_queries(self, date_from, date_to, period_from, period_to, target_move): diff --git a/mis_builder/models/mis_builder.py b/mis_builder/models/mis_builder.py index 0912eac2..04b75b4b 100644 --- a/mis_builder/models/mis_builder.py +++ b/mis_builder/models/mis_builder.py @@ -482,33 +482,13 @@ class mis_report_instance_period(orm.Model): this = self.browse(cr, uid, _id, context=context)[0] env = Environment(cr, uid, {}) aep = AccountingExpressionProcessor(env) - aep.parse_expr(expr) - aep.done_parsing(this.report_instance_id.root_account) - domain = aep.get_aml_domain_for_expr(expr) - if domain: - # TODO: reuse compute_period_domain - # compute date/period - period_ids = [] - date_from = None - date_to = None - period_obj = self.pool['account.period'] - target_move = this.report_instance_id.target_move - if target_move == 'posted': - domain.append(('move_id.state', '=', target_move)) - if this.period_from: - compute_period_ids = period_obj.build_ctx_periods( - cr, uid, this.period_from.id, this.period_to.id) - period_ids.extend(compute_period_ids) - else: - date_from = this.date_from - date_to = this.date_to - if period_ids: - if date_from: - domain.append('|') - domain.append(('period_id', 'in', period_ids)) - if date_from: - domain.extend([('date', '>=', date_from), - ('date', '<=', date_to)]) + if aep.has_account_var(expr): + aep.parse_expr(expr) + aep.done_parsing(this.report_instance_id.root_account) + domain = aep.get_aml_domain_for_expr( + expr, this.date_from, this.date_to, + this.period_from, this.period_to, + this.report_instance_id.target_move) return { 'name': expr + ' - ' + this.name, 'domain': domain, @@ -605,7 +585,7 @@ class mis_report_instance_period(orm.Model): kpi_style = None drilldown = (kpi_val is not None and - bool(aep.get_aml_domain_for_expr(kpi.expression))) + aep.has_account_var(kpi.expression)) res[kpi.name] = { 'val': kpi_val,