From f6640aff042288016de2c30cefce46692322a96b Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Tue, 10 Dec 2024 12:33:14 +0100 Subject: [PATCH] [FIX] account_move_update_analytic: Don't choke with analytic taxes --- .../tests/__init__.py | 1 + .../test_account_move_update_analytic.py | 92 +++++++++++++++++++ .../wizards/account_move_update_analytic.py | 9 ++ 3 files changed, 102 insertions(+) create mode 100644 account_move_update_analytic/tests/__init__.py create mode 100644 account_move_update_analytic/tests/test_account_move_update_analytic.py diff --git a/account_move_update_analytic/tests/__init__.py b/account_move_update_analytic/tests/__init__.py new file mode 100644 index 0000000000..c63d7b5a3d --- /dev/null +++ b/account_move_update_analytic/tests/__init__.py @@ -0,0 +1 @@ +from . import test_account_move_update_analytic 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 new file mode 100644 index 0000000000..b691f0d4cf --- /dev/null +++ b/account_move_update_analytic/tests/test_account_move_update_analytic.py @@ -0,0 +1,92 @@ +from odoo.fields import Command +from odoo.tests.common import TransactionCase, tagged + + +@tagged("-at_install", "post_install") +class TestAccountMoveUpdateAnalytic(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.line = cls.env["account.move.line"].search( + [ + ("product_id", "!=", False), + ("move_id.move_type", "=", "in_invoice"), + ("move_id.state", "=", "posted"), + ("analytic_distribution", "=", False), + ], + limit=1, + ) + cls.plan = cls.env.ref("analytic.analytic_plan_projects") + cls.account1 = cls.env["account.analytic.account"].search( + [("plan_id", "=", cls.plan.id)], limit=1 + ) + cls.account2 = cls.env["account.analytic.account"].search( + [("plan_id", "=", cls.plan.id), ("id", "!=", cls.account1.id)], limit=1 + ) + + def test_analytic_line_created(self): + """ + Test that changing analytic distribution recreates lines + """ + wizard = ( + self.env["account.move.update.analytic.wizard"] + .with_context(active_id=self.line.id) + .create( + { + "analytic_distribution": {str(self.account1.id): 100}, + } + ) + ) + wizard.update_analytic_lines() + self.assertEqual(self.line.analytic_line_ids.account_id, self.account1) + wizard = ( + self.env["account.move.update.analytic.wizard"] + .with_context(active_id=self.line.id) + .create( + { + "analytic_distribution": {str(self.account2.id): 100}, + } + ) + ) + wizard.update_analytic_lines() + self.assertEqual(self.line.analytic_line_ids.account_id, self.account2) + + def test_tax_distribution_added(self): + """ + Test that changing analytic distribution on a line with a tax with analytic + enabled changes the tax' analytic lines too + """ + tax = self.env["account.tax"].search( + [("type_tax_use", "=", "purchase")], limit=1 + ) + tax.analytic = True + move = self.line.move_id.copy({"invoice_date": self.line.move_id.invoice_date}) + move.invoice_line_ids[1:].unlink() + line = move.invoice_line_ids + line.write( + { + "tax_ids": [Command.set(tax.ids)], + "analytic_distribution": {self.account1.id: 100}, + } + ) + move.action_post() + self.assertEqual(line.analytic_line_ids.account_id, self.account1) + self.assertEqual( + move.line_ids.filtered("tax_line_id").analytic_line_ids.account_id, + self.account1, + ) + wizard = ( + self.env["account.move.update.analytic.wizard"] + .with_context(active_id=line.id) + .create( + { + "analytic_distribution": {self.account2.id: 100}, + } + ) + ) + wizard.update_analytic_lines() + self.assertEqual(line.analytic_line_ids.account_id, self.account2) + self.assertEqual( + move.line_ids.filtered("tax_line_id").analytic_line_ids.account_id, + 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 db3c7355c3..b1a4effa9c 100644 --- a/account_move_update_analytic/wizards/account_move_update_analytic.py +++ b/account_move_update_analytic/wizards/account_move_update_analytic.py @@ -40,4 +40,13 @@ def default_get(self, fields): def update_analytic_lines(self): self.ensure_one() + self = self.with_context(skip_invoice_sync=True) + + taxes_with_analytic = self.line_id.tax_ids.filtered("analytic") + for tax_line in self.line_id.move_id.line_ids.filtered( + lambda x: x.tax_line_id in taxes_with_analytic + and x.analytic_distribution == self.current_analytic_distribution + ): + tax_line.analytic_distribution = self.analytic_distribution + self.line_id.analytic_distribution = self.analytic_distribution