[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.
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from odoo import api, fields, models
|
from odoo import api, fields, models
|
||||||
|
from odoo.fields import Command
|
||||||
|
|
||||||
|
|
||||||
class AccountMoveLine(models.Model):
|
class AccountMoveLine(models.Model):
|
||||||
@@ -15,25 +16,25 @@ class AccountMoveLine(models.Model):
|
|||||||
@api.depends("analytic_distribution")
|
@api.depends("analytic_distribution")
|
||||||
def _compute_analytic_account_ids(self):
|
def _compute_analytic_account_ids(self):
|
||||||
# Prefetch all involved analytic accounts
|
# Prefetch all involved analytic accounts
|
||||||
with_distribution = self.filtered("analytic_distribution")
|
batch_by_analytic_account = defaultdict(lambda: self.env["account.move.line"])
|
||||||
batch_by_analytic_account = defaultdict(list)
|
for record in self.filtered("analytic_distribution"):
|
||||||
for record in with_distribution:
|
# NB: ``analytic_distribution`` is a JSON field where keys can be either
|
||||||
for account_id in map(int, record.analytic_distribution):
|
# 'account.id' or 'account.id,account.id'
|
||||||
batch_by_analytic_account[account_id].append(record.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(
|
existing_account_ids = set(
|
||||||
self.env["account.analytic.account"]
|
self.env["account.analytic.account"]
|
||||||
.browse(map(int, batch_by_analytic_account))
|
.browse(batch_by_analytic_account)
|
||||||
.exists()
|
.exists()
|
||||||
.ids
|
.ids
|
||||||
)
|
)
|
||||||
# Store them
|
# Store them
|
||||||
self.analytic_account_ids = False
|
self.analytic_account_ids = [Command.clear()]
|
||||||
for account_id, record_ids in batch_by_analytic_account.items():
|
for account_id, records in batch_by_analytic_account.items():
|
||||||
if account_id not in existing_account_ids:
|
if account_id in existing_account_ids:
|
||||||
continue
|
records.analytic_account_ids = [Command.link(account_id)]
|
||||||
self.browse(record_ids).analytic_account_ids = [
|
|
||||||
fields.Command.link(account_id)
|
|
||||||
]
|
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user