From b4da4c9032e99db8c70479877df307029bdb99d5 Mon Sep 17 00:00:00 2001 From: "Adrien Peiffer (ACSONE)" Date: Thu, 23 Apr 2015 23:40:47 +0200 Subject: [PATCH] [IMP] Refactor improvements using AEP --- mis_builder/models/aep.py | 3 +- mis_builder/models/mis_builder.py | 117 ++++++----------------- mis_builder/static/src/js/mis_builder.js | 22 +++-- 3 files changed, 43 insertions(+), 99 deletions(-) diff --git a/mis_builder/models/aep.py b/mis_builder/models/aep.py index fda4cec7..d81bb914 100644 --- a/mis_builder/models/aep.py +++ b/mis_builder/models/aep.py @@ -141,13 +141,14 @@ class AccountingExpressionProcessor(object): """ domains = [] for mo in self.ACC_RE.finditer(expr): - field, mode, account_codes, domain = self._parse_mo(mo) + field, mode, account_codes, domain_partial = self._parse_mo(mo) if mode == 'i': continue account_ids = set() for account_code in account_codes: account_ids.update(self._account_ids_by_code[account_code]) domain = [('account_id', 'in', tuple(account_ids))] + domain.extend(list(domain_partial)) if field == 'crd': domain.append(('credit', '>', 0)) elif field == 'deb': diff --git a/mis_builder/models/mis_builder.py b/mis_builder/models/mis_builder.py index fb019a39..9f9f6054 100644 --- a/mis_builder/models/mis_builder.py +++ b/mis_builder/models/mis_builder.py @@ -76,51 +76,6 @@ def _python_var(var_str): return re.sub(r'\W|^(?=\d)', '_', var_str).lower() -def _get_sufix(is_solde=False, is_initial=False): - if is_solde: - return 's' - elif is_initial: - return 'i' - else: - return '' - - -def _get_prefix(function, is_solde=False, is_initial=False): - return function + _get_sufix(is_solde=is_solde, is_initial=is_initial) - - -# TODO : To review -def _get_account_vars_in_expr(expr, res_vars, domain_mapping, is_solde=False, - is_initial=False): - for function in FUNCTION_LIST: - prefix = _get_prefix(function, is_solde=is_solde, - is_initial=is_initial) - find_res = re.findall(r'\b%s(?:_[0-9]+|\[.*?\])(?:\[.+?\])?' % prefix, - expr) - for item in find_res: - match = re.match(r'\b(%s)(_[0-9]+|\[.*?\])(\[.+?\])?' % prefix, - item) - var_tuple = match.groups() - domain = "" if var_tuple[2] is None else var_tuple[2] - key = "" - if domain != "": - if domain not in domain_mapping: - key = 'd' + str(len(res_vars.keys())) - domain_mapping[domain] = key - else: - key = domain_mapping[domain] - key_domain = (key, domain) - if not res_vars.get(key_domain, False): - res_vars[key_domain] = set() - if var_tuple[1].startswith('_'): - account_codes = var_tuple[1][1:] - else: - account_codes = var_tuple[1][1:-1] - account_codes = [a.strip() for a in account_codes.split(',')] - account_codes_set = set(account_codes) - res_vars[key_domain] |= account_codes_set - - def _is_valid_python_var(name): for item in FUNCTION_LIST: for param in PARAMETERS: @@ -535,57 +490,43 @@ class mis_report_instance_period(orm.Model): 'Period name should be unique by report'), ] - # TODO : To review def compute_domain(self, cr, uid, ids, account_, context=None): if isinstance(ids, (int, long)): ids = [ids] - domain_list = [] - # extract all bal code - res_vars = {} - domain_mapping = {} - _get_account_vars_in_expr(account_, res_vars, domain_mapping, - is_solde=False, is_initial=False) - for key_domain, account_code in res_vars.iteritems(): - key, domain = key_domain - partial_domain = [('account_id.code', 'in', list(account_code))] - if domain != '': - partial_domain.insert(0, '&') - domain_eval = safe_eval(domain) - partial_domain.extend(domain_eval) - domain_list.extend(partial_domain) - nb_or = len(res_vars) - 1 - while nb_or > 0: - domain_list.insert(0, '|') - nb_or -= 1 - - # compute date/period - period_ids = [] - date_from = None - date_to = None - - period_obj = self.pool['account.period'] - - for c in self.browse(cr, uid, ids, context=context): - target_move = c.report_instance_id.target_move + domain = [] + this = self.browse(cr, uid, ids, context=context)[0] + env = Environment(cr, uid, {}) + aep = AccountingExpressionProcessor(env) + aep.parse_expr(account_) + aep.done_parsing([('company_id', '=', + this.report_instance_id.company_id.id)]) + domain.extend(aep.get_aml_domain_for_expr(account_)) + if 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_list.append(('move_id.state', '=', target_move)) - if c.period_from: + domain.append(('move_id.state', '=', target_move)) + if this.period_from: compute_period_ids = period_obj.build_ctx_periods( - cr, uid, c.period_from.id, c.period_to.id) + cr, uid, this.period_from.id, this.period_to.id) period_ids.extend(compute_period_ids) else: - if not date_from or date_from > c.date_from: - date_from = c.date_from - if not date_to or date_to < c.date_to: - date_to = c.date_to - if period_ids: + 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_list.append('|') - domain_list.append(('period_id', 'in', period_ids)) - if date_from: - domain_list.extend([('date', '>=', c.date_from), - ('date', '<=', c.date_to)]) - return domain_list + domain.extend([('date', '>=', date_from), + ('date', '<=', date_to)]) + else: + domain = False + return domain def compute_period_domain(self, cr, uid, period_report, is_solde, is_initial, context=None): diff --git a/mis_builder/static/src/js/mis_builder.js b/mis_builder/static/src/js/mis_builder.js index 48e71efb..a9b7b1d2 100644 --- a/mis_builder/static/src/js/mis_builder.js +++ b/mis_builder/static/src/js/mis_builder.js @@ -35,16 +35,18 @@ openerp.mis_builder = function(instance) { [period_id, val_c], {'context': new instance.web.CompoundContext()} ).then(function(result){ - self.do_action({ - name: val_c + ' - ' + period_name, - domain: JSON.stringify(result), - type: 'ir.actions.act_window', - res_model: "account.move.line", - views: [[false, 'list'], [false, 'form']], - view_type : "list", - view_mode : "list", - target: 'current', - }); + if (result != false){ + self.do_action({ + name: val_c + ' - ' + period_name, + domain: JSON.stringify(result), + type: 'ir.actions.act_window', + res_model: "account.move.line", + views: [[false, 'list'], [false, 'form']], + view_type : "list", + view_mode : "list", + target: 'current', + }); + } }); } },