From 1eef0f681761eb4f48574baf19da43dacabc1310 Mon Sep 17 00:00:00 2001 From: "Luis J. Salvatierra" Date: Mon, 12 Aug 2024 13:02:18 +0200 Subject: [PATCH] [FIX] product_analytic: analytic distribution compute When the inverse product compute method is triggered, the current analytic distribution is removed, for example, if you modify the default distribution for an invoice line and duplicate the invoice. --- product_analytic/models/account_move.py | 7 ++- product_analytic/tests/test_account_move.py | 51 +++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/product_analytic/models/account_move.py b/product_analytic/models/account_move.py index b8df083397..684c1661e3 100644 --- a/product_analytic/models/account_move.py +++ b/product_analytic/models/account_move.py @@ -26,7 +26,12 @@ def _inverse_product_id(self): return res for line in self: inv_type = line.move_id.move_type - if line.product_id and inv_type and inv_type != "entry": + if ( + line.product_id + and not line.analytic_distribution + and inv_type + and inv_type != "entry" + ): ana_accounts = ( line.product_id.product_tmpl_id._get_product_analytic_accounts() ) diff --git a/product_analytic/tests/test_account_move.py b/product_analytic/tests/test_account_move.py index edab6d9cf0..57a11d4188 100644 --- a/product_analytic/tests/test_account_move.py +++ b/product_analytic/tests/test_account_move.py @@ -240,3 +240,54 @@ def test_create_out_category(self): int(analytic_account_id[0]), self.analytic_account2.id, ) + + def test_create_out_copy(self): + # Create invoice with product default analytic distribution + invoice = self.env["account.move"].create( + [ + { + "partner_id": self.partner.id, + "journal_id": self.journal_sale.id, + "move_type": "out_invoice", + "invoice_line_ids": [ + Command.create( + { + "name": "Test line", + "quantity": 1, + "price_unit": 50, + "account_id": self.account_out.id, + "product_id": self.product.id, + } + ) + ], + } + ] + ) + invoice_line = invoice.invoice_line_ids[0] + analytic_account_id = [key for key in invoice_line.analytic_distribution] + self.assertEqual( + int(analytic_account_id[0]), + self.product.income_analytic_account_id.id, + ) + # Modify the analytic distribution + invoice_line.analytic_distribution = { + self.analytic_account2.id: 100, + } + analytic_account_id = [key for key in invoice_line.analytic_distribution] + self.assertEqual( + int(analytic_account_id[0]), + self.analytic_account2.id, + ) + # Duplicate the invoice to check the analytic distribution is copied + new_invoice = invoice.copy() + new_invoice_line = new_invoice.invoice_line_ids[0] + analytic_account_id = [key for key in new_invoice_line.analytic_distribution] + self.assertNotEqual( + int(analytic_account_id[0]), + self.product.income_analytic_account_id.id, + "The analytic distribution should be copied, not recomputed.", + ) + self.assertEqual( + int(analytic_account_id[0]), + self.analytic_account2.id, + )