From ecf07eda1412f8d63d6f1f7a98bc2d6684f26158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nshimiyimana=20S=C3=A9na?= Date: Mon, 18 Sep 2023 15:28:43 +0200 Subject: [PATCH] [FIX] account: prevent unnecessary recompute of invoice line account_id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary ------ Currently, when removing a product from an invoice line, the account for that invoice line is automatically reset to a default value. Steps to reproduce ------------------ * install `account_accountant` * create an invoice and add a product * set a different account on that invoice line * remove the product from the line You should see that the account is automatically changed. opw-3092556 closes odoo/odoo#139361 X-original-commit: 39b44dbfef0d533722ce2c96d375ada113d32b00 Signed-off-by: de Wouters de Bouchout Jean-Benoît (jbw) Signed-off-by: Séna Serge Nshimiyimana (sesn) --- addons/account/models/account_move_line.py | 1 + .../tests/test_account_move_out_invoice.py | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/addons/account/models/account_move_line.py b/addons/account/models/account_move_line.py index 1ec61c3c954..d3373bcf715 100644 --- a/addons/account/models/account_move_line.py +++ b/addons/account/models/account_move_line.py @@ -1135,6 +1135,7 @@ class AccountMoveLine(models.Model): @api.onchange('product_id') def _inverse_product_id(self): self._conditional_add_to_compute('account_id', lambda line: ( + (self.product_id or not self.account_id) and line.display_type == 'product' and line.move_id.is_invoice(True) )) diff --git a/addons/account/tests/test_account_move_out_invoice.py b/addons/account/tests/test_account_move_out_invoice.py index ebd24f1a4a4..c9b9ece5dcc 100644 --- a/addons/account/tests/test_account_move_out_invoice.py +++ b/addons/account/tests/test_account_move_out_invoice.py @@ -3771,3 +3771,26 @@ class TestAccountMoveOutInvoiceOnchanges(AccountTestInvoicingCommon): duplicate_invoice.line_ids.filtered(lambda l: l.display_type == 'payment_term').account_id, other_receivable_account ) + + def test_account_on_invoice_line_product_removal(self): + """Removing a product from an invoice line should preserve that line's account.""" + other_income_account = self.product_a.property_account_income_id.copy() + + invoice = self.env['account.move'].create({ + 'move_type': 'out_invoice', + 'partner_id': self.partner_a.id, + 'invoice_date': fields.Date.from_string('2019-01-01'), + 'invoice_line_ids': [ + Command.create({ + 'product_id': self.product_a.id, + }), + ] + }) + invoice.invoice_line_ids.account_id = other_income_account + invoice.invoice_line_ids.product_id = False + + self.assertEqual( + invoice.invoice_line_ids.account_id, + other_income_account, + "Removing a product from an invoice line should no change the account." + )