lavaloon-eg / ksa_compliance

KSA Compliance App for KSA E-invoice
https://www.lavaloon.com/
GNU Affero General Public License v3.0
37 stars 33 forks source link

Validation Error BR-CO-17 , BR-S-09 #137

Closed infowaysa closed 1 month ago

infowaysa commented 1 month ago

Facing this error with an invoice I am trying to create.

ZATCA Validation Error Errors Warnings BR-CO-17: [BR-CO-17]-VAT category tax amount (BT-117) = VAT category taxable amount (BT-116) x (VAT category rate (BT-119) / 100), rounded to two decimals. BR-S-09: [BR-S-09]-The VAT category tax amount (BT-117) in a VAT breakdown (BG-23) where VAT category code (BT-118) is "Standard rated" shall equal the VAT category taxable amount (BT-116) multiplied by the VAT category rate (BT-119) / 100), rounded to two decimals.

The strange thing is that I've created other invoices with the same line item and for the same customer and didn't face this issue. I thought maybe the issue with something I've done wrong with that specific invoice so I recreated the invoice and still got the same error.

mhaggag commented 1 month ago

If you check logs/zatca.log, you should see the command that's validating the invoice looking like this:

2024-10-02 11:26:03,599 INFO zatca Running: ['/home/muhammad/erp-einvoice-test/sites/erp-einvoice-test/zatca-tools/zatca-cli-2.1.1/bin/zatca-cli', 'validate', '-b', '/home/muhammad/erp-einvoice-test/sites/erp-einvoice-test/zatca-tools/zatca-cli-2.1.1', '-c', './erp-einvoice-test/zatca-files/Demo for ZATCA-Saudi Arabia-SAR.pem', '-p', 'Xpj5wUDDpKVLk7vja6YXzMPrwc9nOOYXqXa1OamaqmE=', '/tmp/tmp9hdvyw6k-signed_invoice.xml']

The last element in the list ('/tmp/tmp9hdvyw6k-signed_invoice.xml' in the example above) is the path to the generated XML that failed validation. Please share it so we can compare it with the invoice and see what went wrong.

infowaysa commented 1 month ago

Apologies for taking so long.

No, the tax was not included in the basic rate.

XML saved as TXT as I couldn't upload as is: tmpjyk1gvtl-signed_invoice.xml.txt

mhaggag commented 1 month ago

I've run the SDK under a debugger locally and found out the problem. The SDK uses schematron to define the validations, and these warnings were fired from this part in CEN-EN16931-UBL.xsl which is based on the CEN/EN 16931:2017 standard. The validation definition is buggy and won't work for large numbers because it uses xs:float (32-bit floating point number) to perform the VAT calculations, leading to potential loss of precision.

While troubleshooting, I updated the warning to include the expected/computed values and got this (computed values bolded):

[BR-CO-17]-VAT category tax amount (BT-117) (405062.72) = VAT category taxable amount (BT-116) (2700418.0) x (VAT category rate (BT-119) (15) / 100), rounded to two decimals.

The computed amount is .72 instead of .7. I verified this is the result of 32-bit floating computation by testing with numpy's float32:

Python 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import float32
>>> float32(2700418) * float32(.15)
405062.72
>>> 2700418 * .15
405062.7

As you can see, 32-bit computation is erroneous. Regular computation (in Python, 64-bit AFAIK) results in the correct value. The schematron should use xs:decimal instead of xs:float, but it's not our fix to make.

We'll report this to ZATCA.

mhaggag commented 1 month ago

Note that you can sort of work around this by issuing "smaller" invoices (e.g. one for 2,000,000 and one for 700,418) but I wouldn't recommend it--these are just warnings, and it's not your fault. It'll likely be fixed in a future SDK version.

mhaggag commented 1 month ago

This has already been reported to ZATCA and they promised a fix in an upcoming release: Getting Warning in BR-CO-17 and BR-S-09

I'll close this issue since it's external.

infowaysa commented 1 month ago

Thank you very much for the support and the very detailed and clarification. I will advise our finance team to either wait on issuing the invoice or breaking it down to smaller ones if in a rush.

Would you maybe be able to direct me on where I can find out when the SDK would be updated and and would that require an update for the app as well or how would that be fixed eventually?

Thanks again.

mhaggag commented 1 month ago

No worries, thank you for using the app.

I'm not aware of any posted SDK update schedule. The version history on the download page doesn't list release dates. This is the result page for searching for BR-CO-17 on the developer community. There are reports from March, April, July, and August; I don't think we can count on a quick resolution.

This post suggests contacting ZATCA relationship management; if you do have a ZATCA contact, reaching out to escalate may speed things up.

The SDK is used twice:

infowaysa commented 1 month ago

Thank you very much. Will ask finance to reach out to their contact in ZATCA to look into this.