lavaloon-eg / ksa_compliance

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

Total VAT amount and Tax category calculation mismatch #174

Closed meaziz closed 1 week ago

meaziz commented 1 week ago
"errorMessages":[{"type":"ERROR","code":"BR-CO-15","category":"EN_16931","message":"Invoice total amount with VAT (BT-112) ","status":"ERROR"},{"type":"ERROR","code":"BR-CO-14","category":"EN_16931","message":"Invoice total VAT amount (BT-110) ","status":"ERROR"}]
 <cac:TaxTotal>
        <cbc:TaxAmount currencyID="SAR">163.73</cbc:TaxAmount>
            <cac:TaxSubtotal>
                <cbc:TaxableAmount currencyID="SAR">1091.5</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="SAR">163.72</cbc:TaxAmount>
                <cac:TaxCategory>
                    <cbc:ID>S</cbc:ID>
                    <cbc:Percent>15.0</cbc:Percent>
                    <cac:TaxScheme>
                        <cbc:ID>VAT</cbc:ID>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </cac:TaxSubtotal>
    </cac:TaxTotal>

The Invoice total is 1,091.50 Calculating the tax amount at 15% * 1,091.50 = 163.725 which is rounded to in ERPNext The Total Taxes and Charges (SAR) on ERPNext Invoice field shows 163.73
and the XML 163.73</cbc:TaxAmount>

It appears to be a rounding issue when creating the tax category sum, So a theorey is that the the Tax category calculation is rounded pre maturely (probably while being created)

But, checking the item-wise tax sum it adds up 81 + 27 + 32.40 + 10.73 + 12.60 = 163.73

السلعة المبلغ الخاضع للضريبة VAT 15%
ITM-00198 540.00 (15.0%) 81.00
ITM-00448 180.00 (15.0%) 27.00
ITM-00414 216.00 (15.0%) 32.40
ITM-00013 71.50 (15.0%) 10.73
ITM-00303 84.00 (15.0%) 12.60

https://github.com/lavaloon-eg/ksa_compliance/blob/03781557a8b35e2b5ec0d1b124b167b61bb2a944/ksa_compliance/output_models/e_invoice_output_model.py#L76

Any workarounds or other paths we can explore?

meaziz commented 1 week ago

Enforcing a system wide currency precision of (2) didn't solve the issue.

meaziz commented 1 week ago

ITM-00013 VAT amount for 71.50 * 15% = 10.725

(540 15%) + ( 180 15%) + (216 15%) + (71.50 15%) + ( 84 * 15%) = 163.725

which when rounded should result the 163.73

So a theory is the item-wise-tax recalculates the tax amounts line by line and messes up tax_amount with the rounding each value prematurely which then when are summed creates the missing (halala).

@mhaggag appreciate your input.

mhaggag commented 1 week ago

Hello,

We've seen similar issues before with clients. Usually it comes down to the following settings:

In [1]: from frappe.utils.data import rounded

In [2]: rounded(163.725, 2, "Banker's Rounding (legacy)")
Out[2]: 163.73

In [3]: rounded(-163.725, 2, "Banker's Rounding (legacy)")
Out[3]: -163.72

In [4]: rounded(-163.725, 2, "Banker's Rounding")
Out[4]: -163.72

In [5]: rounded(163.725, 2, "Banker's Rounding")
Out[5]: 163.72

There's a third issue we've seen that is there's sometime a difference between the total in the Sales Taxes and Charges table and the grand total. That one is fixed by frappe but not yet backported to version 15.

meaziz commented 1 week ago

Thanks Appreciate your prompt response. Will enable those and monitor the result and confirm back to the thread.

meaziz commented 1 week ago

Thanks 🙏 After updating to latest version and setting the mentioned configuration. Invoices seems to be CLEARED.

Will keep monitoring it.

Note Maybe these configuration should be mentioned in the documentation and maybe a general configuration check could be developed into the app .. (Just a suggestion) We can design it based on common cases observed.

mhaggag commented 1 week ago

Note Maybe these configuration should be mentioned in the documentation and maybe a general configuration check could be developed into the app .. (Just a suggestion) We can design it based on common cases observed.

I agree, that's a good idea. We can't require the settings to be a certain way, but we can update the Prerequisites and add a validation check in ZATCA Business Settings to inform users whether certain settings could cause problems. @omarashrafsarhan Let's track and schedule this please.