From 4f9a5a73f4d220ffaef779082aea6834ce4fef07 Mon Sep 17 00:00:00 2001 From: gawa-odoo Date: Mon, 9 Jan 2023 14:49:07 +0000 Subject: [PATCH] [FIX] account_report: change formula's update of expression How to reproduce: - Have a report line with a custom creation with a 'balance' label - Replace the expression by an `account_code_formula` in the xml => The 'balance' expression does not exist anymore in the xml, so the ORM removes it, and a new one is not created. We changed the update so only 'balance' label expressions are changed: - if we have an expression with an xml_id, we remove the expression and recreate a new one. - if we don't (custom created or coming already from a formula shortcut), we just update its values. We also created a way to remove a formula shortcut (making it falsy will unlink the expression that was linked to it) closes odoo/odoo#110119 X-original-commit: d01a7b76bee4471444eeedacc05bd52fe9c0399c Signed-off-by: Olivier Colson (oco) Signed-off-by: Wala Gauthier (gawa) --- addons/account/models/account_report.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/addons/account/models/account_report.py b/addons/account/models/account_report.py index 90ce288d90b..fe436737506 100644 --- a/addons/account/models/account_report.py +++ b/addons/account/models/account_report.py @@ -342,6 +342,7 @@ class AccountReportLine(models.Model): # create account.report.expression for each report line based on the formula provided to each # engine-related field. This makes xmls a bit shorter vals_list = [] + xml_ids = self.expression_ids.filtered(lambda exp: exp.label == 'balance').get_external_id() for report_line in self: if engine == 'domain' and report_line.domain_formula: subformula, formula = DOMAIN_REGEX.match(report_line.domain_formula or '').groups() @@ -352,6 +353,10 @@ class AccountReportLine(models.Model): elif engine == 'aggregation' and report_line.aggregation_formula: subformula, formula = None, report_line.aggregation_formula else: + # If we want to replace a formula shortcut with a full-syntax expression, we need to make the formula field falsy + # We can't simply remove it from the xml because it won't be updated + # If the formula field is falsy, we need to remove the expression that it generated + report_line.expression_ids.filtered(lambda exp: exp.engine == engine and exp.label == 'balance' and not xml_ids.get(exp.id)).unlink() continue vals = { @@ -365,8 +370,15 @@ class AccountReportLine(models.Model): # expressions already exists, update the first expression with the right engine # since syntactic sugar aren't meant to be used with multiple expressions for expression in report_line.expression_ids: - if expression.engine == engine: - expression.write(vals) + if expression.label == 'balance': + # If we had a 'balance' expression coming from the xml and are using a formula shortcut on top of it, + # we expect the shortcut to replace the original expression. The full declaration should also + # be removed from the data file, leading to the ORM deleting it automatically. + if xml_ids.get(expression.id): + expression.unlink() + vals_list.append(vals) + else: + expression.write(vals) break else: # else prepare batch creation