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)