From 43ed3e69291833ca82cd1257ade021862997828c Mon Sep 17 00:00:00 2001
From: oco-odoo <oco@odoo.com>
Date: Thu, 14 Sep 2023 13:01:55 +0200
Subject: [PATCH] [FIX] account: reports: translate line codes properly in
 copied aggregation formulas

Example: Copy the Belgian P&L, try to open the copied report => error message saying some aggregation terms cannot be expanded. If you open the its form view, you can see that line BE_PL_14's balance formula has been translated from this

BE_9906.balance + BE_791.balance - BE_691_2.balance + BE_794.balance - BE_694_6.balance

to this on the copy:

BE_9906_COPY.balance + BE_791_COPY.balance - BE_691_2_COPY.balance + BE_794.balance - BE_694_6.balance

This is buggy ; BE_794.balance and BE_694_6.balance should be BE_794_COPY.balance - BE_694_6_COPY.balance, in order for them to match the other lines of the copied report properly.

This bug touches other reports more generally. It was due to the fact we replaced the codes in aggregations while we were copying the lines, and not after copying all the lines. Because of that, lines that were referrenced by an aggregation expression belonging to a line coming before them in sequence were not taken into account, as they were not part of the code_mapping dict yet.

OPW-3505592

closes odoo/odoo#135467

Signed-off-by: John Laterre (jol) <jol@odoo.com>
---
 addons/account/models/account_report.py | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/addons/account/models/account_report.py b/addons/account/models/account_report.py
index 6792079a6bad..6987c07c9405 100644
--- a/addons/account/models/account_report.py
+++ b/addons/account/models/account_report.py
@@ -193,8 +193,18 @@ class AccountReport(models.Model):
         code_mapping = {}
         for line in self.line_ids.filtered(lambda x: not x.parent_id):
             line._copy_hierarchy(copied_report, code_mapping=code_mapping)
+
+        # Replace line codes by their copy in aggregation formulas
+        for expression in copied_report.line_ids.expression_ids:
+            if expression.engine == 'aggregation':
+                copied_formula = f" {expression.formula} " # Add spaces so that the lookahead/lookbehind of the regex can work (we can't do a | in those)
+                for old_code, new_code in code_mapping.items():
+                    copied_formula = re.sub(f"(?<=\\W){old_code}(?=\\W)", new_code, copied_formula)
+                expression.formula = copied_formula.strip() # Remove the spaces introduced for lookahead/lookbehind
+
         for column in self.column_ids:
             column.copy({'report_id': copied_report.id})
+
         return copied_report
 
     @api.ondelete(at_uninstall=False)
@@ -329,13 +339,6 @@ class AccountReportLine(models.Model):
         # Update aggregation expressions, so that they use the copied lines
         for expression in self.expression_ids:
             copy_defaults = {'report_line_id': copied_line.id}
-
-            if expression.engine == 'aggregation':
-                copied_formula = f" {expression.formula} " # Add spaces so that the lookahead/lookbehind of the regex can work (we can't do a | in those)
-                for old_code, new_code in code_mapping.items():
-                    copied_formula = re.sub(f"(?<=\\W){old_code}(?=\\W)", new_code, copied_formula)
-                copy_defaults['formula'] = copied_formula.strip() # Remove the spaces introduced for lookahead/lookbehind
-
             expression.copy(copy_defaults)
 
     def _get_copied_code(self):
-- 
GitLab