From f6053f7e3579b65c1c5fcc4b4b8913a61cd55c07 Mon Sep 17 00:00:00 2001 From: Sergio Bustamante Date: Fri, 18 Oct 2024 12:51:32 +0200 Subject: [PATCH] [FIX] account_financial_report: Remove inactive analytic account relations --- .../models/account_move_line.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/account_financial_report/models/account_move_line.py b/account_financial_report/models/account_move_line.py index 284ab52d7115..cb734bf68a66 100644 --- a/account_financial_report/models/account_move_line.py +++ b/account_financial_report/models/account_move_line.py @@ -28,6 +28,7 @@ def _compute_analytic_account_ids(self): ) # Store them self.analytic_account_ids = False + self._remove_lost_relations(tuple(self.ids)) for account_id, record_ids in batch_by_analytic_account.items(): if account_id not in existing_account_ids: continue @@ -67,3 +68,19 @@ def search_count(self, domain, limit=None): if self.env.context.get("skip_search_count"): return 0 return super().search_count(domain, limit=limit) + + def _remove_lost_relations(self, account_move_line_ids): + # When some linked analytic account is active=False, + # the relation in account_analytic_account_account_move_line_rel + # is not removed. This method is used to remove the relation in those cases. + inactive_analytic_account_ids = tuple( + self.env["account.analytic.account"].search([("active", "=", False)]).ids + ) + if inactive_analytic_account_ids: + self._cr.execute( + "DELETE " + "FROM account_analytic_account_account_move_line_rel " + "WHERE account_move_line_id in %s " + "AND account_analytic_account_id in %s", + (account_move_line_ids, inactive_analytic_account_ids), + )