From c816feb80d7ca65a2236cf0f745f5fd1651bc677 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Fri, 7 Oct 2022 13:01:12 +0200 Subject: [PATCH] [IMP] account_payment_partner: Make payment mode editable on journal item This field was editable in previous version before invoice > move refactoring, and it's logic to allow to change payment mode once the invoice has been posted without the need of resetting it to draft. Thus, the field has been changed to computed writable field, taking care of the consequences at model level (compute) and view level (add the field in views + proper attrs). This commits adds tracking=True to the payment mode field as well to be aware when and who the payment mode is changed in the invoice, no matter if directly changed in draft, or through the change on the journal item. TT39850 --- .../models/account_move.py | 1 + .../models/account_move_line.py | 18 ++++++++++++- .../tests/test_account_payment_partner.py | 4 +++ .../views/account_move_line.xml | 27 ++++++++++++++++++- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/account_payment_partner/models/account_move.py b/account_payment_partner/models/account_move.py index 3e36db944..70ec9e8ea 100644 --- a/account_payment_partner/models/account_move.py +++ b/account_payment_partner/models/account_move.py @@ -22,6 +22,7 @@ class AccountMove(models.Model): ondelete="restrict", readonly=False, check_company=True, + tracking=True, ) bank_account_required = fields.Boolean( related="payment_mode_id.payment_method_id.bank_account_required", readonly=True diff --git a/account_payment_partner/models/account_move_line.py b/account_payment_partner/models/account_move_line.py index a49a1be58..0fb81d617 100644 --- a/account_payment_partner/models/account_move_line.py +++ b/account_payment_partner/models/account_move_line.py @@ -13,9 +13,10 @@ class AccountMoveLine(models.Model): store=True, ondelete="restrict", index=True, + readonly=False, ) - @api.depends("move_id.payment_mode_id") + @api.depends("move_id", "move_id.payment_mode_id") def _compute_payment_mode(self): for line in self: if line.move_id.is_invoice() and line.account_type in ( @@ -25,3 +26,18 @@ class AccountMoveLine(models.Model): line.payment_mode_id = line.move_id.payment_mode_id else: line.payment_mode_id = False + + def write(self, vals): + """Propagate up to the move the payment mode if applies.""" + if "payment_mode_id" in vals: + for record in self: + move = ( + self.env["account.move"].browse(vals.get("move_id", 0)) + or record.move_id + ) + if ( + move.payment_mode_id.id != vals["payment_mode_id"] + and move.is_invoice() + ): + move.payment_mode_id = vals["payment_mode_id"] + return super().write(vals) diff --git a/account_payment_partner/tests/test_account_payment_partner.py b/account_payment_partner/tests/test_account_payment_partner.py index 55e4794d5..19ccb1ee2 100644 --- a/account_payment_partner/tests/test_account_payment_partner.py +++ b/account_payment_partner/tests/test_account_payment_partner.py @@ -254,6 +254,10 @@ class TestAccountPaymentPartner(TransactionCase): lambda l: l.account_id.account_type == "liability_payable" ) self.assertEqual(invoice.payment_mode_id, aml[0].payment_mode_id) + # Test payment mode change on aml + mode = self.supplier_payment_mode.copy() + aml.payment_mode_id = mode + self.assertEqual(invoice.payment_mode_id, mode) def test_invoice_create_out_invoice(self): invoice = self._create_invoice( diff --git a/account_payment_partner/views/account_move_line.xml b/account_payment_partner/views/account_move_line.xml index 959b9429e..f14a261e9 100644 --- a/account_payment_partner/views/account_move_line.xml +++ b/account_payment_partner/views/account_move_line.xml @@ -11,9 +11,34 @@ - + + + + + account.move.line.tree - Add payment mode + account.move.line + + + + + + + + +