details:   https://code.tryton.org/tryton/commit/5d4c650b38a4
branch:    7.8
user:      Cédric Krier <[email protected]>
date:      Mon Aug 31 12:06:40 2026 +0200
description:
        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)
diffstat:

 modules/edocument_ubl/edocument.py                               |  47 
++++++---
 modules/edocument_ubl/tests/scenario_ubl_2_credit_note_parse.rst |   2 +-
 modules/edocument_ubl/tests/scenario_ubl_2_invoice_parse.rst     |   2 +-
 3 files changed, 31 insertions(+), 20 deletions(-)

diffs (109 lines):

diff -r 787aae35ed50 -r 5d4c650b38a4 modules/edocument_ubl/edocument.py
--- a/modules/edocument_ubl/edocument.py        Fri Sep 04 11:53:57 2026 +0200
+++ b/modules/edocument_ubl/edocument.py        Mon Aug 31 12:06:40 2026 +0200
@@ -272,11 +272,24 @@
                     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
         invoice.set_journal()
         invoice.on_change_party()
         invoice.invoice_address = cls._parse_2_address(
@@ -291,17 +304,6 @@
             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)
@@ -528,22 +530,31 @@
                     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
         invoice.set_journal()
         invoice.on_change_party()
         if (seller := root.find('./{*}SellerSupplierParty')) is not None:
             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 -r 787aae35ed50 -r 5d4c650b38a4 
modules/edocument_ubl/tests/scenario_ubl_2_credit_note_parse.rst
--- a/modules/edocument_ubl/tests/scenario_ubl_2_credit_note_parse.rst  Fri Sep 
04 11:53:57 2026 +0200
+++ b/modules/edocument_ubl/tests/scenario_ubl_2_credit_note_parse.rst  Mon Aug 
31 12:06:40 2026 +0200
@@ -62,7 +62,7 @@
     >>> 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 -r 787aae35ed50 -r 5d4c650b38a4 
modules/edocument_ubl/tests/scenario_ubl_2_invoice_parse.rst
--- a/modules/edocument_ubl/tests/scenario_ubl_2_invoice_parse.rst      Fri Sep 
04 11:53:57 2026 +0200
+++ b/modules/edocument_ubl/tests/scenario_ubl_2_invoice_parse.rst      Mon Aug 
31 12:06:40 2026 +0200
@@ -62,7 +62,7 @@
     >>> 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)
 

Reply via email to