From 04af335284bae5a5fba5ccf70b08daee6bf2e3cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 28 Oct 2023 14:04:23 +0200 Subject: [PATCH] Copy alternative payees of invoice if it is the only party on lines to pay Only one alternative payee is permitted on draft invoice so we can copy it only if there is only one and it is the one that was already set when posting the invoice (only party that appears in the lines to pay). Closes #12528 --- modules/account_invoice/invoice.py | 15 +++++++++++++++ .../tests/scenario_invoice_alternative_payee.rst | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/modules/account_invoice/invoice.py b/modules/account_invoice/invoice.py index b70ac02923..3f8ed329e2 100644 --- a/modules/account_invoice/invoice.py +++ b/modules/account_invoice/invoice.py @@ -1399,6 +1399,20 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin, InvoiceReportMixin): default = {} else: default = default.copy() + + alternative_payees2copy = set() + for invoice in invoices: + if len(invoice.alternative_payees) == 1: + parties = {l.party for l in invoice.lines_to_pay} + if parties <= set(invoice.alternative_payees): + alternative_payees2copy.add(invoice.id) + + def copy_alternative_payees(data): + if data['id'] in alternative_payees2copy: + return data.get('alternative_payees', []) + else: + return [] + default.setdefault('number', None) default.setdefault('sequence') default.setdefault('move', None) @@ -1407,6 +1421,7 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin, InvoiceReportMixin): default.setdefault('invoice_report_cache', None) default.setdefault('invoice_report_cache_id', None) default.setdefault('invoice_report_format', None) + default.setdefault('alternative_payees', copy_alternative_payees) default.setdefault('payment_lines', None) default.setdefault('invoice_date', None) default.setdefault('accounting_date', None) diff --git a/modules/account_invoice/tests/scenario_invoice_alternative_payee.rst b/modules/account_invoice/tests/scenario_invoice_alternative_payee.rst index 3c565375aa..03ee0968a2 100644 --- a/modules/account_invoice/tests/scenario_invoice_alternative_payee.rst +++ b/modules/account_invoice/tests/scenario_invoice_alternative_payee.rst @@ -88,6 +88,12 @@ Post customer invoice:: >>> party3.receivable Decimal('0.0') +Copying invoice with single alternative payee is kept:: + + >>> duplicate_inv, = invoice.duplicate() + >>> duplicate_inv.alternative_payees == invoice.alternative_payees + True + Set another payee:: >>> delegate = Wizard( @@ -129,3 +135,9 @@ Pay the invoice:: 1 >>> len(invoice.reconciliation_lines) 1 + +Copying invoice with many alternative payees remove them:: + + >>> duplicate_inv, = invoice.duplicate() + >>> duplicate_inv.alternative_payees + []