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, + )