2

I am trying to use validate my xml with UBL 2.1 standards in line with Saudi Arabia ZATCA(Zakat, Tax and Customs Authority) specifications.The url of this validatoe : https://sandbox.zatca.gov.sa/TestXML .But I can't validate that as my xml looking great but I don't understand what's going wrong .I used python json2xml package for creating xml.This package generate xml from json.

Erros list what I am getting from ZATCA XML Validator:

category : XSD_SCHEMA_ERROR

code :SAXParseException

message : Schema validation failed; XML does not comply with UBL 2.1 standards in line with ZATCA specifications

Here is my Xml code:

   <?xml version="1.0" ?>
<Invoice>
    <ProfileID>reporting:1.0</ProfileID>
    <ID>INV004</ID>
    <UUID>fd5a7cc4-2316-49ee-ac07-6f4be4be3731</UUID>
    <IssueDate>2022-08-13</IssueDate>
    <IssueTime>23:46:07</IssueTime>
    <InvoiceTypeCode>388</InvoiceTypeCode>
    <InvoiceTypeCodeName>0101001</InvoiceTypeCodeName>
    <DocumentCurrencyCode>SAR</DocumentCurrencyCode>
    <TaxCurrencyCode>SAR</TaxCurrencyCode>
    <Note/>
    <OrderReference>
        <ID/>
    </OrderReference>
    <ContractDocumentReference>
        <ID/>
    </ContractDocumentReference>
    <AdditionalDocumentReference>
        <UUID>4</UUID>
        <PIH>
            <Attachment>
                <EmbeddedDocumentBinaryObject>ET05jV7roub7D66wOAQ49TQ8mCkyldhmH7B8CV3Rc6g=</EmbeddedDocumentBinaryObject>
            </Attachment>
        </PIH>
        <QR>
            <Attachment>
                <EmbeddedDocumentBinaryObject>5D6ZU7f6nb+s1szmMw46l4NZ7yTy0p1wi0ZUMsdQWBE=</EmbeddedDocumentBinaryObject>
            </Attachment>
        </QR>
    </AdditionalDocumentReference>
    <Signature>
        <ID>urn:oasis:names:specification: ubl:signature:Invoice</ID>
        <SignatureMethod>urn:oasis:names:specification:ubl:dsig:enveloped: xades</SignatureMethod>
    </Signature>
    <AccountingSupplierParty>
        <Party>
            <PartyLegalEntity>
                <RegistrationName>Altaf Miazee</RegistrationName>
            </PartyLegalEntity>
            <PartyIdentification>
                <ID/>
            </PartyIdentification>
            <PartyTaxScheme>
                <CompanyID>300600363600003</CompanyID>
            </PartyTaxScheme>
            <PostalAddress>
                <Country>
                    <IdentificationCode>BD</IdentificationCode>
                </Country>
                <AdditionalStreetName>Altafbari</AdditionalStreetName>
                <StreetName>dhaka</StreetName>
                <BuildingNumber>1233</BuildingNumber>
                <PlotIdentification>1233</PlotIdentification>
                <CityName>Dhaka</CityName>
                <PostalZone>12302</PostalZone>
                <CountrySubentity>Dhaka</CountrySubentity>
                <CitySubdivisionName>miazee</CitySubdivisionName>
            </PostalAddress>
        </Party>
    </AccountingSupplierParty>
    <AccountingCustomerParty>
        <Party>
            <PartyLegalEntity>
                <RegistrationName>Hosen MD Altaf</RegistrationName>
            </PartyLegalEntity>
            <PartyIdentification>
                <ID>398765409876333</ID>
            </PartyIdentification>
            <PartyTaxScheme>
                <CompanyID>398765409876333</CompanyID>
            </PartyTaxScheme>
            <PostalAddress>
                <StreetName>الملك سلمان</StreetName>
                <AdditionalStreetName>الملك سلمان</AdditionalStreetName>
                <BuildingNumber>1234</BuildingNumber>
                <PlotIdentification>1234</PlotIdentification>
                <CityName>dhaka</CityName>
                <PostalZone>12234</PostalZone>
                <CountrySubentity>Dhaka</CountrySubentity>
                <CitySubdivisionName>الملك سلمان</CitySubdivisionName>
                <Country>
                    <IdentificationCode>BD</IdentificationCode>
                </Country>
            </PostalAddress>
        </Party>
    </AccountingCustomerParty>
    <Delivery>
        <ActualDeliveryDate>2022-08-25</ActualDeliveryDate>
        <LatestDeliveryDate/>
    </Delivery>
    <PaymentMeans>
        <PaymentMeansCode>10</PaymentMeansCode>
        <PayeeFinancialAccount>
            <PaymentNote/>
        </PayeeFinancialAccount>
    </PaymentMeans>
    <AllowanceCharge>
        <TaxCategory>
            <ID>S</ID>
            <Percent>0.0</Percent>
            <TaxScheme>
                <ID>VAT</ID>
            </TaxScheme>
        </TaxCategory>
        <ChargeIndicator>False</ChargeIndicator>
        <MultiplierFactorNumeric>52.1</MultiplierFactorNumeric>
        <Amount>0.00</Amount>
        <AmountcurrencyID>SAR</AmountcurrencyID>
        <BaseAmount>164263.68</BaseAmount>
        <BaseAmountcurrencyID>SAR</BaseAmountcurrencyID>
    </AllowanceCharge>
    <LegalMonetaryTotal>
        <LineExtensionAmount>164263.68</LineExtensionAmount>
        <LineExtensionAmountCurrencyID>SAR</LineExtensionAmountCurrencyID>
        <AllowanceTotalAmount>0.00</AllowanceTotalAmount>
        <AllowanceTotalAmountcurrencyID>SAR</AllowanceTotalAmountcurrencyID>
        <TaxExclusiveAmount>146664.00</TaxExclusiveAmount>
        <TaxExclusiveAmountcurrencyID>SAR</TaxExclusiveAmountcurrencyID>
        <TaxInclusiveAmount>164263.68</TaxInclusiveAmount>
        <TaxInclusiveAmountcurrencyID>SAR</TaxInclusiveAmountcurrencyID>
        <PrepaidAmount/>
        <PrepaidAmountcurrencyID>SAR</PrepaidAmountcurrencyID>
        <PayableAmount>164263.68</PayableAmount>
        <PayableAmountcurrencyID>SAR</PayableAmountcurrencyID>
    </LegalMonetaryTotal>
    <TaxTotal>
        <TaxAmount>17599.68</TaxAmount>
        <VatAmountCurrency>SAR</VatAmountCurrency>
        <VatAmountInAccountingCurrency>17599.68</VatAmountInAccountingCurrency>
        <CurrencyForVatAmountInAccountingCurrency>SAR</CurrencyForVatAmountInAccountingCurrency>
        <TaxSubtotal>
            <TaxableAmount>146664.0</TaxableAmount>
            <TaxableAmountcurrencyID>SAR</TaxableAmountcurrencyID>
            <TaxCategory>
                <ID>E</ID>
                <Percent>1</Percent>
                <TaxExemptionReasonCode>TYIEWE</TaxExemptionReasonCode>
                <TaxExemptionReason>lksdkskak</TaxExemptionReason>
                <TaxScheme>
                    <ID>10.12</ID>
                </TaxScheme>
            </TaxCategory>
            <TaxAmountcurrencyID>SAR</TaxAmountcurrencyID>
        </TaxSubtotal>
        <TaxAmountcurrencyID>SAR</TaxAmountcurrencyID>
    </TaxTotal>
    <InvoiceLine>
        <item>
            <ID>1</ID>
            <InvoicedQuantity>12</InvoicedQuantity>
            <InvoicedQuantityUnitCode>nos</InvoicedQuantityUnitCode>
            <LineExtensionAmount>146664.0</LineExtensionAmount>
            <LineExtensionAmountcurrencyID>SAR</LineExtensionAmountcurrencyID>
            <AllowanceCharge>
                <ChargeIndicator>False</ChargeIndicator>
                <MultiplierFactorNumeric>0.0</MultiplierFactorNumeric>
                <Amount>0</Amount>
                <AmountcurrencyID>SAR</AmountcurrencyID>
                <BaseAmount>146664.0</BaseAmount>
                <BaseAmountcurrencyID>SAR</BaseAmountcurrencyID>
            </AllowanceCharge>
            <TaxTotal>
                <TaxAmount>17599.68</TaxAmount>
                <TaxAmountcurrencyID>SAR</TaxAmountcurrencyID>
                <RoundingAmount>164263.68</RoundingAmount>
                <RoundingAmountcurrencyID>SAR</RoundingAmountcurrencyID>
            </TaxTotal>
            <Item>
                <Name>altaf</Name>
                <BuyersItemIdentification>
                    <ID/>
                </BuyersItemIdentification>
                <SellersItemIdentification>
                    <ID>ITM9</ID>
                </SellersItemIdentification>
                <StandardItemIdentification>
                    <ID/>
                </StandardItemIdentification>
                <ClassifiedTaxCategory>
                    <ID>2</ID>
                    <Percent>12.00</Percent>
                    <TaxScheme>
                        <ID>VAT</ID>
                    </TaxScheme>
                </ClassifiedTaxCategory>
            </Item>
            <Price>
                <PriceAmount>12222.00</PriceAmount>
                <PriceAmountschemeID>SAR</PriceAmountschemeID>
                <AllowanceCharge>
                    <ChargeIdicator>False</ChargeIdicator>
                    <Amount>0</Amount>
                    <AmountcurrencyID>SAR</AmountcurrencyID>
                    <BaseAmount>164263.68</BaseAmount>
                    <BaseAmountcurrencyID>SAR</BaseAmountcurrencyID>
                </AllowanceCharge>
                <BaseQuantity>12</BaseQuantity>
                <BaseQuantityUnitCode/>
            </Price>
        </item>
    </InvoiceLine>
</Invoice>

Here is the python code I wrote for creating XML:

def download_invoice_xml(request, invoice_type, invoice_no):
in_data = InvoiceReport.objects.filter(invoice_type=invoice_type, invoice_number=invoice_no)
for inv in in_data:
    jsonData = inv.invoice_data
    mode = inv.creation_mode  

# Issuetime = jsonData['IssueTime']
# d1 = datetime.strptime(Issuetime, "%Y-%m-%dT%H:%M:%S")
# dt_to_string = d1.strftime('%d-%m-%Y %H:%M:%S')
# jsonData['IssueTime'] = dt_to_string
#
# Issuedate = jsonData['IssueDate']
# d2 = datetime.strptime(Issuedate, "%Y-%m-%d")
# dt_to_string2 = d1.strftime('%d-%m-%Y')
# jsonData['IssueDate'] = dt_to_string2
if jsonData.get('AllowanceCharge').get('ChargeIndicator') == 0:
    jsonData['AllowanceCharge']['ChargeIndicator'] = False
for i in jsonData.get('InvoiceLine'):
    if i.get('AllowanceCharge').get('ChargeIndicator') == 0:
        i['AllowanceCharge']['ChargeIndicator'] = False
    if i.get('Price').get('AllowanceCharge').get('ChargeIndicator') == 0:
        i['Price']['AllowanceCharge']['ChargeIndicator'] = False
xml_output = json2xml.Json2xml(jsonData, wrapper="Invoice", pretty=True, attr_type=False).to_xml()

now = datetime.now()
if xml_output:
    response = HttpResponse(xml_output, content_type='application/xml')
    xml_date = jsonData['IssueDate'].replace("-", "")
    xml_time = jsonData['IssueTime'].replace(":", "")
    filename = jsonData['AccountingSupplierParty']['Party']['PartyTaxScheme']['CompanyID'] + "_" + \
               xml_date + "T" + xml_time + "_" + str(jsonData['ID']) + ".xml"
    # filename = jsonData['AccountingSupplierParty']['Party']['PartyIdentification']['ID'] + "_%s_%s.xml" % (
    #     str((now.strftime("%Y%m%d%H%M%S"))), jsonData['ID'])      
    content = "attachment; filename=%s" % (filename)
    response['Content-Disposition'] = content
    return response
return HttpResponse("Not found")

This is part of ZATCA E-invoice system and the project code is writen with python django.

2 Answers2

2

UBL specifications that aren't met generally fall into two categories:

  1. An Attribute is required but you are not providing it.
  2. The order of the attributes inside the element is wrong.

To solve your problem, you should inspect each element of your XML against the UBL Schema specification and find required attributes that you're not providing or attributes out of order..

  • Go to this site that lists the UBL Specifications: http://www.datypic.com/sc/ubl21/ss.html

  • In the search box at the top, enter the name of the element followed by the word "Type" (e.g.: Invoice --> search for InvoiceType

enter image description here

  • In the element specifications, find each attribute that its relationship starts with [1 .. this attribute is mandatory, make sure you provide a value for it. enter image description here
  • Check the order of the attribute and make sure they are added in the correct order. enter image description here

Doing that will ensure that all your elements are compliant to the UBL specifications.

Ahmad Kelany
  • 376
  • 3
  • 8
  • Thank you for details explains .Do you have any format of the valid UBL 2.1 version of xml.I look forward but don’t find any. – Hosen MD Altaf Aug 18 '22 at 13:07
  • 1
    You're welcome, If you find the answer helpful, please accept it as an answer to your question.. Here is an example invoice that passes the UBL specifications: https://1drv.ms/u/s!AtaJtdUF4g4liddrcWYu0XHZnj_haA?e=TUBrmY – Ahmad Kelany Aug 19 '22 at 11:14
  • @Ahmad Kelany, Are you sure the XML you provided works with zatka? – Dev Sep 28 '22 at 06:31
  • @pepe, I am sure that is passes the UBL specifications as I said in the comment, ZATCA specifications and their validation API are not complete at the date of this comment. – Ahmad Kelany Sep 28 '22 at 07:55
1

There are many problems in : https://sandbox.zatca.gov.sa/ As the results in coding and sharing the invoice have many errors Zakat has been warned over and over again

clerk
  • 33
  • 6