invinet / python-en16931

A Python 3 package to parse, generate and manage the EN16931 Invoice format.
https://invinet.github.io/python-en16931/
Apache License 2.0
24 stars 8 forks source link

using invoice.from_xml buyer_party invalid entity #11

Open Johnreidsilver opened 3 days ago

Johnreidsilver commented 3 days ago

Hi, First and foremost thanks for the python code to import EN16931 data.

It's my first time trying it out actually first time trying to parse XML for that matter.

invoice = Invoice.from_xml('/0000620.XML')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.local/lib/python3.10/site-packages/en16931/invoice.py", line 193, in from_xml
    invoice.buyer_party = get_entity(root, kind='buyer')
  File "/home/user/.local/lib/python3.10/site-packages/en16931/invoice.py", line 485, in buyer_party
    raise ValueError("Invalid Entity")
ValueError: Invalid Entity

Looking at the XML file it has AccountingSupplierParty and AccountingCustomerParty fields I tried to edit invoice.py to go for those but no luck: invoice.seller_party = get_entity(root, kind='AccountingSupplierParty')


<cac:AccountingSupplierParty>
    <cac:Party>
      <cbc:EndpointID schemeID="EMAIL">clientes@axpoportugal.pt</cbc:EndpointID>
      <cac:PartyIdentification>
        <cbc:ID schemeID="0001">PT514286652</cbc:ID>
      </cac:PartyIdentification>
      <cac:PartyName>
        <cbc:Name>AXPO ENERGIA PORTUGAL, UNIPESSOAL LDA</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
        <cbc:StreetName>Avenida Jose Malhoa, Nº 27, 8º Piso </cbc:StreetName>
        <cbc:CityName>Lisboa</cbc:CityName>
        <cbc:PostalZone>1070-156</cbc:PostalZone>
        <cbc:CountrySubentity>Lisboa</cbc:CountrySubentity>
        <cac:Country>
          <cbc:IdentificationCode>PT</cbc:IdentificationCode>
        </cac:Country>
      </cac:PostalAddress>
      <cac:PartyTaxScheme>
        <cbc:CompanyID>PT514286652</cbc:CompanyID>
        <cac:TaxScheme>
          <cbc:ID>VAT</cbc:ID>
        </cac:TaxScheme>
      </cac:PartyTaxScheme>
      <cac:PartyLegalEntity>
        <cbc:RegistrationName>AXPO ENERGIA PORTUGAL, UNIPESSOAL LDA</cbc:RegistrationName>
        <cbc:CompanyID schemeID="0001">PT514286652</cbc:CompanyID>
        <cbc:CompanyLegalForm>50.000€</cbc:CompanyLegalForm>
      </cac:PartyLegalEntity>
      <cac:Contact>
        <cbc:Telephone>+351 218 222 098</cbc:Telephone>
        <cbc:ElectronicMail>clientes@axpoportugal.pt</cbc:ElectronicMail>
      </cac:Contact>
    </cac:Party>
  </cac:AccountingSupplierParty>

I also commented those parts out to see how far I could load the invoice and got to this error:


File "/home/user/.local/lib/python3.10/site-packages/en16931/invoice_line.py", line 264, in unit_code
    raise ValueError("Unsupported unit code {}".format(code))
ValueError: Unsupported unit code KWH

The InvoiceLine includes quantity in KWH and amount in EUR


<cac:InvoiceLine>
    <cbc:ID>10000</cbc:ID>
    <cbc:Note>G/L Account</cbc:Note>
    <cbc:InvoicedQuantity unitCode="KWH">2893.00</cbc:InvoicedQuantity>
    <cbc:LineExtensionAmount currencyID="EUR">236.40</cbc:LineExtensionAmount>
    <cac:InvoicePeriod>
      <cbc:StartDate>2024-09-08</cbc:StartDate>
      <cbc:EndDate>2024-10-07</cbc:EndDate>
    </cac:InvoicePeriod>
    <cac:Item>
      <cbc:Description>Energia fornecida (Ponta)</cbc:Description>
      <cbc:Name>Energia fornecida (Ponta)</cbc:Name>
      <cac:SellersItemIdentification>
        <cbc:ID>711113101</cbc:ID>
      </cac:SellersItemIdentification>
      <cac:ClassifiedTaxCategory>
        <cbc:ID>NOR</cbc:ID>
        <cbc:Percent>23.00</cbc:Percent>
        <cac:TaxScheme>
          <cbc:ID>VAT</cbc:ID>
        </cac:TaxScheme>
      </cac:ClassifiedTaxCategory>
      <cac:AdditionalItemProperty>
        <cbc:Name>CICLO_HORARIO</cbc:Name>
        <cbc:Value>SEMANAL</cbc:Value>
      </cac:AdditionalItemProperty>
      <cac:AdditionalItemProperty>
        <cbc:Name>TARIFARIO</cbc:Name>
        <cbc:Value>Tetra-horario</cbc:Value>
      </cac:AdditionalItemProperty>
      <cac:AdditionalItemProperty>
        <cbc:Name>TENSAO</cbc:Name>
        <cbc:Value>MT</cbc:Value>
      </cac:AdditionalItemProperty>
      <cac:AdditionalItemProperty>
        <cbc:Name>TIPO_LEITURA</cbc:Name>
        <cbc:Value>REAL</cbc:Value>
      </cac:AdditionalItemProperty>
      <cac:AdditionalItemProperty>
        <cbc:Name>FATOR</cbc:Name>
        <cbc:Value>1</cbc:Value>
      </cac:AdditionalItemProperty>
    </cac:Item>
    <cac:Price>
      <cbc:PriceAmount currencyID="EUR">0.08171400</cbc:PriceAmount>
      <cbc:BaseQuantity unitCode="KWH">1.00</cbc:BaseQuantity>
    </cac:Price>
  </cac:InvoiceLine>

I would fork and fix this if I knew how to, but need to start with some examples on how to do it. Thanks for you help.

jtorrents commented 19 hours ago

Hi @Johnreidsilver

Note that in Portugal they don't use the EN16931 format as is, but the closely related UBL 2.1 "CIUS-PT". You can check the global namespaces of your XML file to determine the format.

If your invoice is indeed in EN16931 format, it would be very useful for debugging the problem if you could share the XML file. Feel free to remove any sensitive information (especially payment means).