ZUGFeRD / mustangproject

Open Source Java e-Invoicing library, validator and tool (Factur-X/ZUGFeRD, UNCEFACT/CII XRechnung)
http://www.mustangproject.org
Apache License 2.0
237 stars 132 forks source link

<GrandTotalAmount> does not meet invoice amount #493

Closed frankMizzer closed 1 month ago

frankMizzer commented 2 months ago

Hi, I am using the mustang library 2.11.0 in order to implement a Zugferd-XML to our invoices. Currently I am running a test with an invoice of 156.35 EUR including a tax of 24.96. This one consists of three positions 79.20, 32.95 and 44.20. These values lead to net amounts of 66.55, 27.69 and 37.14 with a tax rate of 19%. This adds up to 131.38 while calculated on the grand total it should be 131.39. Anyway, what I see in the XML is: grafik which is completely wrong as I would either expect 131.38 + 24.97 = 156.35 or 131.39 + 24.96 = 156.35, but why is it only 156.34? Meanwhile I added some log to my code in order to find out where the problem comes from: grafik The first line comes from the TransactionCalculator itself. The lines with the *** are printed while looping the ZFItems and for adding up everything I used the algorithm, that I have found inside the TransactionCalculator. How can I make sure, that my XML attachment gives me the correct values?

andyx1975 commented 1 month ago

you can add a rounding amount. Those differences are coming from different tax calculation variations. https://github.com/ZUGFeRD/mustangproject/issues/480

This libary only supports a calculation where the tax amount is calculated from the sum of all net amounts. You calculate all values line by line. In your case it leads to this difference.

frankMizzer commented 1 month ago

Meanwhile I had a look into the source code. It is rather obvious, that the missing cent comes from the LineCalculator constructor: itemTotalNetAmount = currentItem.getQuantity().multiply(getPrice()).divide(currentItem.getBasisQuantity()) .subtract(allowanceItemTotal).setScale(2, BigDecimal.ROUND_HALF_UP); itemTotalVATAmount = itemTotalNetAmount.multiply(multiplicator); itemTotalNetAmount is rounded to the scale of 2, just before using it for the VAT amount calculation. For more accuracy, it would be better to do the calculation with, say, a scale of 4. The necessary rounding should be done afterwards on both figures.

My suggestion for solving the bug looks like that: grafik

jstaerk commented 1 month ago

Hello @andyx1975

you can add a rounding amount. Those differences are coming from different tax calculation variations. #480

You can not. The rounding amount is very limited to very specific cases, e.g. if you have countries/currencies that round to 5ct like the netherlands.

@frankMizzer could you please attach the complete xml? that would make it much easier to reproduce/fix. thanks Jochen

frankMizzer commented 1 month ago

factur-x-R24-0252.txt Hello @jstaerk , please find my xml attached.

jstaerk commented 1 month ago

Apologies for the late reply. "I would either expect 131.38 + 24.97 = 156.35": as mentioned the tax is 24,96 only. You have to add the base amount by vat rate (on cent level), then apply the tax, presumably because otherwise one would be able to "optimize" taxes by distributing over positions. Please refer to EN16931-1 for calculation rules, in particular their example 1 .