From 2bb60bd148c39c42262492684e1d7d23baa96b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 29 Apr 2023 12:56:51 +0200 Subject: [PATCH] Set invoice account only when changing party or dates Using on_change_with does not work correctly for editable field because the value may be changed when the user does not expect it. Closes #12239 (grafted from 8557c57f05896c8e976d88d68500ec8d5ab6f255) --HG-- branch : 6.0 --- modules/account_invoice/invoice.py | 37 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/modules/account_invoice/invoice.py b/modules/account_invoice/invoice.py index 3a4ba4e499..0d01a2fb4a 100644 --- a/modules/account_invoice/invoice.py +++ b/modules/account_invoice/invoice.py @@ -436,21 +436,6 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin): if self.company: return self.company.party.id - @fields.depends( - 'state', 'account', 'party', 'type', 'accounting_date', 'invoice_date') - def on_change_with_account(self): - if self.state != 'draft': - return self.account - account = None - if self.party: - with Transaction().set_context( - date=self.accounting_date or self.invoice_date): - if self.type == 'out': - account = self.party.account_receivable_used - elif self.type == 'in': - account = self.party.account_payable_used - return account.id if account else None - @fields.depends('type') def on_change_type(self): Journal = Pool().get('account.journal') @@ -476,16 +461,36 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin): table, _ = tables[None] return [Coalesce(table.accounting_date, table.invoice_date)] - @fields.depends('party', 'type') + @fields.depends('party', 'type', methods=['_update_account']) def on_change_party(self): self.invoice_address = None if self.party: self.invoice_address = self.party.address_get(type='invoice') self.party_tax_identifier = self.party.tax_identifier if self.type == 'out': + self.account = self.party.account_receivable_used self.payment_term = self.party.customer_payment_term elif self.type == 'in': + self.account = self.party.account_payable_used self.payment_term = self.party.supplier_payment_term + self._update_account() + + @fields.depends(methods=['_update_account']) + def on_change_accounting_date(self): + self._update_account() + + @fields.depends(methods=['_update_account']) + def on_change_invoice_date(self): + self._update_account() + + @fields.depends('account', 'accounting_date', 'invoice_date') + def _update_account(self): + "Update account to current account" + if self.account: + account = self.account.current( + date=self.accounting_date or self.invoice_date) + if account != self.account: + self.account = account @fields.depends('currency') def on_change_with_currency_digits(self, name=None):