[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
This commit is contained in:
Pedro M. Baeza
2022-10-07 13:01:12 +02:00
parent 9e43c8b27c
commit da8ca5bb62
4 changed files with 48 additions and 2 deletions

View File

@@ -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

View File

@@ -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_internal_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)

View File

@@ -294,6 +294,10 @@ class TestAccountPaymentPartner(SavepointCase):
lambda l: l.account_id.user_type_id == self.acct_type_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(

View File

@@ -14,9 +14,34 @@
position="after"
>
<group name="payments" string="Payments">
<field name="payment_mode_id" widget="selection" />
<field name="account_internal_type" invisible="1" />
<field name="reconciled" invisible="1" />
<field
name="payment_mode_id"
widget="selection"
force_save="1"
attrs="{'invisible': [('account_internal_type', 'not in', ['receivable', 'payable'])], 'readonly': ['|', ('account_internal_type', 'not in', ['receivable', 'payable']), ('reconciled', '=', True)]}"
/>
</group>
</xpath>
</field>
</record>
<record id="view_move_line_tree" model="ir.ui.view">
<field name="name">account.move.line.tree - Add payment mode</field>
<field name="model">account.move.line</field>
<field name="inherit_id" ref="account.view_move_line_tree" />
<field name="arch" type="xml">
<field name="date_maturity" position="after">
<field name="account_internal_type" invisible="1" />
<field name="reconciled" invisible="1" />
<field
name="payment_mode_id"
optional="hide"
widget="selection"
force_save="1"
attrs="{'invisible': [('account_internal_type', 'not in', ['receivable', 'payable'])], 'readonly': ['|', ('account_internal_type', 'not in', ['receivable', 'payable']), ('reconciled', '=', True)]}"
/>
</field>
</field>
</record>
</odoo>