Skip to content

Commit

Permalink
[FIX] account_move_update_analytic: Update analytic tax distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn committed Dec 17, 2024
1 parent bb7a38c commit 01bec2b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions account_move_update_analytic/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_account_move_update_analytic
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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, and splits tax lines if multiple
lines have the same tax but a different analytic distribution
"""
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()
line1 = move.invoice_line_ids
line1.write(
{
"tax_ids": [Command.set(tax.ids)],
"analytic_distribution": {self.account1.id: 100},
}
)
line2 = line1.copy({})
move.action_post()
self.assertEqual(line1.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=line1.id)
.create(
{
"analytic_distribution": {self.account2.id: 100},
}
)
)
wizard.update_analytic_lines()
self.assertEqual(line1.analytic_line_ids.account_id, self.account2)
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.assertItemsEqual(
tax_lines.analytic_line_ids.account_id,
self.account1 + self.account2,
)
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ def default_get(self, fields):

def update_analytic_lines(self):
self.ensure_one()

# force recreating lines with analytic taxes
taxes_with_analytic = self.line_id.tax_ids.filtered("analytic")
self.line_id.move_id.line_ids.filtered(
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

0 comments on commit 01bec2b

Please sign in to comment.