Invoice Totalization¶
This guide explains how Gestix calculates invoice totals from API input, especially when rounding, VAT-inclusive prices, discounts, and Peppol/EN 16931 electronic invoicing rules are involved.
Gestix totalizes invoices on the server. API clients should send the commercial inputs that define the document, then use the values returned by Gestix as the authoritative result.
Related background: Arredondamentos, IVA e Peppol.
Why totals can differ by one cent¶
A document total is not only quantity * price + VAT. The final amount depends on several decisions:
- Whether
unit_priceincludes VAT - How line discounts are combined
- Whether VAT is calculated at line level or document level
- When intermediate values are rounded to cents
- How document-level discounts are distributed across VAT rates
- Whether the resulting values must satisfy Peppol/EN 16931 validation rules
Two systems may receive the same quantities, prices, discounts, and VAT rates, but still produce a one-cent difference if they round at different stages.
For API integrations, this means the external system should not treat its own pre-calculated totals as the source of truth. The Gestix document totalizer determines the persisted net_amount, vat, rounding, and total_amount.
Fields that affect totalization¶
Line fields:
| Field | Meaning |
|---|---|
qty |
Quantity |
unit_price |
Unit price, interpreted according to vat_included |
discount |
First line discount percentage |
discount_2 |
Second line discount percentage |
discount_3 |
Third line discount percentage |
vat |
VAT rate percentage |
vat_included |
0: unit_price excludes VAT; any other value: it includes VAT |
Document fields:
| Field | Meaning |
|---|---|
discount |
Global document discount percentage |
discount_amount |
Global document discount amount |
net_amount |
Returned net amount before VAT |
vat |
Returned VAT amount |
rounding |
Returned rounding adjustment, when applicable |
total_amount |
Returned total amount including taxes |
amount_paid |
Paid or prepaid amount, when applicable |
amount_tendered |
Tendered amount, usually for cash payments |
Shipping, eco tax, stamp duty, IRS retention, and other document charges or retentions may also affect totals when present in the document type or company configuration.
Line discounts are cumulative¶
Gestix applies line discounts successively. They are cumulative, not additive.
The effective line multiplier is:
For example, a 10% discount followed by a 5% discount is:
The effective discount is therefore 14.5%, not 15%.
VAT-exclusive prices¶
When vat_included is 0 or absent, unit_price is treated as a net price.
Conceptually, each line is calculated as:
discounted_unit_price =
unit_price
* (1 - discount / 100)
* (1 - discount_2 / 100)
* (1 - discount_3 / 100)
line_taxable_amount = round2(qty * discounted_unit_price)
line_vat_amount = round2(line_taxable_amount * vat / 100)
line_gross_amount = line_taxable_amount + line_vat_amount
The document total is then built from the accumulated line values, document-level allowances and charges, VAT totals, retentions, and any rounding adjustment.
VAT-inclusive prices¶
When vat_included is any other value (for example 1), unit_price is treated as a gross price.
For VAT-inclusive lines, Gestix first determines the discounted gross line amount and then extracts the taxable amount and VAT in a way that preserves the relationship between the rounded gross and rounded taxable values:
discounted_unit_price =
unit_price
* (1 - discount / 100)
* (1 - discount_2 / 100)
* (1 - discount_3 / 100)
line_gross_amount = round2(qty * discounted_unit_price)
line_taxable_amount = round2((qty * discounted_unit_price) / (1 + vat / 100))
line_vat_amount = line_gross_amount - line_taxable_amount
The important detail is that VAT is the residual between the rounded gross amount and the rounded taxable amount. This avoids one-cent drift in cases where another system recalculates VAT from an already rounded gross value.
Example with unit_price = 24.95, discount = 30.63, vat = 21, qty = 1, and VAT included:
gross = round2(24.95 * 0.6937) = 17.31
taxable = round2((24.95 * 0.6937) / 1.21) = 14.30
vat = 17.31 - 14.30 = 3.01
An external system that rounds the gross amount first and then derives VAT from that rounded value may reach a different result.
Document-level discounts¶
Document-level discounts are applied after line values are known.
When a document contains multiple VAT rates, a global discount must be distributed across the relevant VAT bases. This is important for Peppol/EN 16931 because document-level allowances are represented separately and participate in each VAT breakdown.
Gestix performs the allocation needed to keep the document coherent:
- Line discounts are applied before document discounts.
- A percentage document discount is applied proportionally to the line basis.
- A
discount_amountis treated as a document-level allowance. - For VAT-inclusive documents, the allowance is handled consistently with the gross amounts and VAT rates involved.
- Residual cents from proportional allocation are handled in a controlled way so the persisted document totals remain consistent.
Peppol and EN 16931¶
Peppol BIS Billing is based on EN 16931 and validates the relationships between invoice lines, VAT breakdowns, allowances, charges, and totals. The document must do more than display the correct final total: the intermediate values must also agree with the semantic invoice model.
For Peppol-sensitive documents, Gestix uses line-level totalization so that rounded line values feed the VAT subtotals and document totals. This matches the way Peppol/EN 16931 expects invoice values to be related.
In practice:
- The invoice line net amount is rounded at line level.
- VAT subtotals are built from the relevant line and allowance values per VAT category/rate.
- The invoice total VAT amount is the sum of the VAT category amounts.
- Rounding is represented explicitly when it is needed to reconcile the expected payable amount.
See the official Peppol BIS Billing specification for the normative model.
API integration guidance¶
When creating documents through the API:
- Send quantities, prices, discounts, VAT rates, and related commercial data.
- Avoid pre-rounding intermediate values in a way that changes the commercial input.
- Do not add line discount percentages together.
- Do not calculate VAT at document level when comparing with a Gestix line-level result.
- Treat the returned
net_amount,vat,rounding, andtotal_amountas the Gestix source of truth. - If your application shows estimated totals before submission, label them internally as estimates and reconcile them with the values returned by the API.
This keeps the ERP document, the API response, and any Peppol/EN 16931 electronic invoice representation aligned.