diff --git a/account_move_update_analytic/__init__.py b/account_move_update_analytic/__init__.py index 5cb1c4914..aee8895e7 100644 --- a/account_move_update_analytic/__init__.py +++ b/account_move_update_analytic/__init__.py @@ -1 +1,2 @@ +from . import models from . import wizards diff --git a/account_move_update_analytic/models/account_move_line.py b/account_move_update_analytic/models/account_move_line.py new file mode 100644 index 000000000..45d710f0d --- /dev/null +++ b/account_move_update_analytic/models/account_move_line.py @@ -0,0 +1,30 @@ +# Copyright 2024 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + +force_state_sentinel = object() + + +class AccountMoveLine(models.Model): + _inherit = "account.move.line" + + def _compute_all_tax(self): + """ + super() doesn't write the analytic distribution when the move is posted. + For our purposes, we need that, so we manipulate the cache for the move to + look like it is in draft state when we're called by the update wizard + """ + + cache = self.env.cache._data[self.move_id._fields["state"]] + + if self.env.context.get("account_move_update_analytic") == force_state_sentinel: + cache[self.move_id.id] = "draft" + + result = super()._compute_all_tax() + + if self.env.context.get("account_move_update_analytic") == force_state_sentinel: + if self.move_id.id in cache: + del cache[self.move_id.id] + + return result diff --git a/account_move_update_analytic/tests/test_account_move_update_analytic.py b/account_move_update_analytic/tests/test_account_move_update_analytic.py index ebd233906..5f0132317 100644 --- a/account_move_update_analytic/tests/test_account_move_update_analytic.py +++ b/account_move_update_analytic/tests/test_account_move_update_analytic.py @@ -91,6 +91,9 @@ def test_tax_distribution_added(self): self.assertEqual(line2.analytic_line_ids.account_id, self.account1) tax_lines = move.line_ids.filtered(lambda x: x.tax_line_id == tax) self.assertEqual(len(tax_lines), 2) + self.assertEqual( + len(list(filter(None, tax_lines.mapped("analytic_distribution")))), 2 + ) self.assertItemsEqual( tax_lines.analytic_line_ids.account_id, self.account1 + self.account2, diff --git a/account_move_update_analytic/wizards/account_move_update_analytic.py b/account_move_update_analytic/wizards/account_move_update_analytic.py index 055633a48..2308906cb 100644 --- a/account_move_update_analytic/wizards/account_move_update_analytic.py +++ b/account_move_update_analytic/wizards/account_move_update_analytic.py @@ -4,6 +4,8 @@ from odoo import api, fields, models +from ..models.account_move_line import force_state_sentinel + class AccountMoveUpdateAnalytic(models.TransientModel): _name = "account.move.update.analytic.wizard" @@ -47,4 +49,6 @@ def update_analytic_lines(self): lambda x: x.tax_line_id in taxes_with_analytic ).with_context(dynamic_unlink=True, force_delete=True).unlink() - self.line_id.analytic_distribution = self.analytic_distribution + self.line_id.with_context( + account_move_update_analytic=force_state_sentinel + ).analytic_distribution = self.analytic_distribution