From f3a8695c0c8f6895d606f7e33cb611254165c340 Mon Sep 17 00:00:00 2001 From: SilvioC2C Date: Thu, 29 Aug 2024 14:41:21 +0200 Subject: [PATCH] [FIX] account_financial_report: fix ValueError ``analytic_distribution`` is a JSON field where keys are 'account.id' or 'account.id,account.id' Eg: https://github.com/odoo/odoo/blob/8479b4e/addons/sale/models/account_move_line.py#L158 Previous implementation tried to convert to ``int`` the ``analytic_distribution`` dict keys, leading to a ``ValueError``. This commit fixes this bug. --- .../models/account_move_line.py | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/account_financial_report/models/account_move_line.py b/account_financial_report/models/account_move_line.py index 284ab52d..d6b475d1 100644 --- a/account_financial_report/models/account_move_line.py +++ b/account_financial_report/models/account_move_line.py @@ -3,6 +3,7 @@ from collections import defaultdict from odoo import api, fields, models +from odoo.fields import Command class AccountMoveLine(models.Model): @@ -15,25 +16,25 @@ class AccountMoveLine(models.Model): @api.depends("analytic_distribution") def _compute_analytic_account_ids(self): # Prefetch all involved analytic accounts - with_distribution = self.filtered("analytic_distribution") - batch_by_analytic_account = defaultdict(list) - for record in with_distribution: - for account_id in map(int, record.analytic_distribution): - batch_by_analytic_account[account_id].append(record.id) + batch_by_analytic_account = defaultdict(lambda: self.env["account.move.line"]) + for record in self.filtered("analytic_distribution"): + # NB: ``analytic_distribution`` is a JSON field where keys can be either + # 'account.id' or 'account.id,account.id' + # Eg: https://github.com/odoo/odoo/blob/8479b4e/addons/sale/models/account_move_line.py#L158 + for key in record.analytic_distribution: + for account_id in map(int, key.split(",")): + batch_by_analytic_account[account_id] += record existing_account_ids = set( self.env["account.analytic.account"] - .browse(map(int, batch_by_analytic_account)) + .browse(batch_by_analytic_account) .exists() .ids ) # Store them - self.analytic_account_ids = False - for account_id, record_ids in batch_by_analytic_account.items(): - if account_id not in existing_account_ids: - continue - self.browse(record_ids).analytic_account_ids = [ - fields.Command.link(account_id) - ] + self.analytic_account_ids = [Command.clear()] + for account_id, records in batch_by_analytic_account.items(): + if account_id in existing_account_ids: + records.analytic_account_ids = [Command.link(account_id)] def init(self): """