josemmo / einvoicing

Library for reading and creating European-compliant electronic invoices (EN 16931)
https://josemmo.github.io/einvoicing/
MIT License
112 stars 29 forks source link

Move rounding to last writing step #18

Closed ChristianVermeulen closed 2 years ago

ChristianVermeulen commented 2 years ago

Case:

Imagine a document with the following line (quantity = 2, price excl. = € 15,7025, vat = 21%)

Price Excl. Quantity Total Excl. Vatpercentage Vat amount Total incl.
CALCULATED € 15,7025 2 € 31,405 21% € 6,59505 € 38,00005
VISUAL € 15,70 2 € 31,41 21% € 6,60 € 38,00

Problem

We've been having issues where the xml has differences with the actual pdf document being sent to the customer. Due to this, the amount paid by the customer is different from the amount being booked into the book keeping software. As it turns out, this is due to the fact that amounts are rounded too soon.

The XML turns out like this:

Price Excl. Quantity Total Excl. Vatpercentage Vat amount Total incl.
XML € 15,70 2 € 31,41 21% € 6,60 € 38,01

As you can see, the total is € 38,01 instead of € 38,00. This means our customer needs to change it back to € 38,00 because that is what their customer has actually paid from the original invoice.

Solution

Normally when bookkeeping you are not allowed to round your numbers until the very end. So basically only once you start to visualise the number. Therefore I have moved the round calls to the very end in the UblWriter. This will make sure all calculations are done with the best precision before being outputted in XML.

josemmo commented 2 years ago

Hi @ChristianVermeulen,

Thanks for your PR! I agree with your comments, better to round just once at the end.

Instead of always rounding to 2 decimals, I've added a few changes so that the rounding matrix from the invoice instance is taken into account when:

  1. Exporting the invoice document (in UblWriter)
  2. Calling $invoice->getTotals() as some users may rely on this method to get the invoice totals before exporting it

Let me know your thoughts.