From 366327464ba6910aa73ca1fc3e9be387ad9bed62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 31 Aug 2026 12:06:40 +0200 Subject: [PATCH] Set the company on the invoice before assigning the party The party of the invoice must have the invoice's company in the context to correctly set the account when calling on_change_party. Closes #15049 (grafted from 9b78bdacf182dfa9e5ab3c5206fb8fb5adfb2868) --HG-- branch : 8.0 --- modules/edocument_ubl/edocument.py | 47 ++++++++++++------- .../scenario_ubl_2_credit_note_parse.rst | 2 +- .../tests/scenario_ubl_2_invoice_parse.rst | 2 +- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/modules/edocument_ubl/edocument.py b/modules/edocument_ubl/edocument.py index cb5fbe98eb..6f38e40ce2 100644 --- a/modules/edocument_ubl/edocument.py +++ b/modules/edocument_ubl/edocument.py @@ -321,11 +321,24 @@ class Invoice(Model): type_code=type_code)) invoice = Invoice(type='in') + + if (customer_party := root.find('./{*}AccountingCustomerParty') + ) is not None: + invoice.company = cls._parse_2_company(customer_party) + else: + invoice.company = Invoice.default_company() + if not invoice.company: + raise InvoiceError(gettext( + 'edocument_ubl.msg_company_not_found', + company=etree.tostring( + customer_party, pretty_print=True).decode() + if customer_party else '')) + invoice.reference = root.findtext('./{*}ID') invoice.invoice_date = dt.date.fromisoformat( root.findtext('./{*}IssueDate')) invoice.party = cls._parse_2_supplier( - root.find('./{*}AccountingSupplierParty'), create=True) + root.find('./{*}AccountingSupplierParty'), create=True).id payees = [invoice.party] invoice.set_journal() invoice.on_change_party() @@ -341,17 +354,6 @@ class Invoice(Model): supplier = cls._parse_2_supplier(seller) else: supplier = invoice.party - if (customer_party := root.find('./{*}AccountingCustomerParty') - ) is not None: - invoice.company = cls._parse_2_company(customer_party) - else: - invoice.company = Invoice.default_company() - if not invoice.company: - raise InvoiceError(gettext( - 'edocument_ubl.msg_company_not_found', - company=etree.tostring( - customer_party, pretty_print=True).decode() - if customer_party else '')) if (payee_party := root.find('./{*}PayeeParty')) is not None: party = cls._parse_2_party(payee_party) @@ -659,11 +661,24 @@ class Invoice(Model): type_code=type_code)) invoice = Invoice(type='in') + + if (customer_party := root.find('./{*}AccountingCustomerParty') + ) is not None: + invoice.company = cls._parse_2_company(customer_party) + else: + invoice.company = Invoice.default_company() + if not invoice.company: + raise InvoiceError(gettext( + 'edocument_ubl.msg_company_not_found', + company=etree.tostring( + customer_party, pretty_print=True).decode() + if customer_party else '')) + invoice.reference = root.findtext('./{*}ID') invoice.invoice_date = dt.date.fromisoformat( root.findtext('./{*}IssueDate')) invoice.party = cls._parse_2_supplier( - root.find('./{*}AccountingSupplierParty'), create=True) + root.find('./{*}AccountingSupplierParty'), create=True).id payees = [invoice.party] invoice.set_journal() invoice.on_change_party() @@ -671,11 +686,7 @@ class Invoice(Model): supplier = cls._parse_2_supplier(seller) else: supplier = invoice.party - if (customer_party := root.find('./{*}AccountingCustomerParty') - ) is not None: - invoice.company = cls._parse_2_company(customer_party) - else: - invoice.company = Invoice.default_company() + if (payee_party := root.find('./{*}PayeeParty')) is not None: party = cls._parse_2_party(payee_party) if not party: diff --git a/modules/edocument_ubl/tests/scenario_ubl_2_credit_note_parse.rst b/modules/edocument_ubl/tests/scenario_ubl_2_credit_note_parse.rst index 9b560a7396..ca63a173ad 100644 --- a/modules/edocument_ubl/tests/scenario_ubl_2_credit_note_parse.rst +++ b/modules/edocument_ubl/tests/scenario_ubl_2_credit_note_parse.rst @@ -62,7 +62,7 @@ Parse the UBL invoice:: >>> with file_open( ... 'edocument_ubl/tests/UBL-CreditNote-2.1-Example.xml', ... mode='rb') as fp: - ... invoice_id = EInvoice.parse(fp.read(), config.context) + ... invoice_id = EInvoice.parse(fp.read(), {**config.context, 'company': None}) >>> invoice = Invoice(invoice_id) diff --git a/modules/edocument_ubl/tests/scenario_ubl_2_invoice_parse.rst b/modules/edocument_ubl/tests/scenario_ubl_2_invoice_parse.rst index db6a0c4920..5f719570b6 100644 --- a/modules/edocument_ubl/tests/scenario_ubl_2_invoice_parse.rst +++ b/modules/edocument_ubl/tests/scenario_ubl_2_invoice_parse.rst @@ -62,7 +62,7 @@ Parse the UBL invoice:: >>> with file_open( ... 'edocument_ubl/tests/UBL-Invoice-2.1-Example.xml', ... mode='rb') as fp: - ... invoice_id = EInvoice.parse(fp.read(), config.context) + ... invoice_id = EInvoice.parse(fp.read(), {**config.context, 'company': None}) >>> invoice = Invoice(invoice_id)