From f85ddd596491015820f6e355e640c196a545dc08 Mon Sep 17 00:00:00 2001 From: hupo-odoo Date: Tue, 9 May 2023 11:57:07 +0000 Subject: [PATCH] [IMP] account : tax_tags switch on accounting reports Improves the update of accounting report expression when switching the engine to 'tax_tags'. To reproduce: 1) Create a new report line in a report with an expression using the 'external' engine, giving it "dudu" as formula. 2) Save your changes. 3) Modify your expression to use 'tax_tags' instead of 'external'. Save your changes. => +dudu and -dudu tax tags are now created (with this commit), as expected task : 3270506 closes odoo/odoo#128053 X-original-commit: a7dfd79ec13bae3d43cda7be06ebd07ba3ce135c Related: odoo/enterprise#43918 Signed-off-by: Olivier Colson (oco) Signed-off-by: Hugo Poncelet (hupo) --- addons/account/models/account_report.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/addons/account/models/account_report.py b/addons/account/models/account_report.py index 5d267d553c0..a0ae244fb32 100644 --- a/addons/account/models/account_report.py +++ b/addons/account/models/account_report.py @@ -476,6 +476,13 @@ class AccountReportExpression(models.Model): if 'formula' in vals and isinstance(vals['formula'], str): vals['formula'] = re.sub(r'\s+', ' ', vals['formula'].strip()) + def _create_tax_tags(self, tag_name, country): + existing_tags = self.env['account.account.tag']._get_tax_tags(tag_name, country.id) + if len(existing_tags) < 2: + # We can have only one tag in case we archived it and deleted its unused complement sign + tag_vals = self._get_tags_create_vals(tag_name, country.id, existing_tag=existing_tags) + self.env['account.account.tag'].create(tag_vals) + @api.model_create_multi def create(self, vals_list): # Overridden so that we create the corresponding account.account.tag objects when instantiating an expression @@ -489,21 +496,23 @@ class AccountReportExpression(models.Model): tag_name = expression.formula if expression.engine == 'tax_tags' else None if tag_name: country = expression.report_line_id.report_id.country_id - existing_tags = self.env['account.account.tag']._get_tax_tags(tag_name, country.id) - - if len(existing_tags) < 2: - # We can have only one tag in case we archived it and deleted its unused complement sign - tag_vals = self._get_tags_create_vals(tag_name, country.id, existing_tag=existing_tags) - self.env['account.account.tag'].create(tag_vals) + self._create_tax_tags(tag_name, country) return result def write(self, vals): - if 'formula' not in vals: - return super().write(vals) self._strip_formula(vals) + if vals.get('engine') == 'tax_tags': + tag_name = vals.get('formula') or self.formula + country = self.report_line_id.report_id.country_id + self._create_tax_tags(tag_name, country) + return super().write(vals) + + if 'formula' not in vals: + return super().write(vals) + tax_tags_expressions = self.filtered(lambda x: x.engine == 'tax_tags') former_formulas_by_country = defaultdict(lambda: []) for expr in tax_tags_expressions: