Calculate invoice amounts
The "Calculate invoice amounts" service task automatically computes all relevant totals from invoice line items. Based on quantity, unit price (net), and VAT rate, the service calculates line item totals, VAT breakdowns, and grand totals. This allows invoices to be generated fully automatically without any manual calculation.
How it works
The task processes all invoice line items and calculates:
- Per line item: Net total, gross total, VAT amount
- Aggregated: Grand total net, grand total gross, VAT grouped by rate (0 %, 7 %, 19 %)
The calculated values are written directly into the invoice object and are available for subsequent steps (e.g. PDF generation or e-invoicing).
Input parameters
The invoice object with its line items is provided as input:
{
"vat_rounding": "per_rate",
"app_invoice_items_custom": [
{
"app_item_description": "Consulting",
"app_amount": 10,
"app_item_price_net": 150.00,
"app_vat_percentage": 19,
"app_unit": "Hours"
},
{
"app_item_description": "Materials",
"app_amount": 5,
"app_item_price_net": 25.00,
"app_vat_percentage": 7,
"app_unit": "Pieces"
}
]
}
Explanation:
vat_rounding: VAT rounding method, see VAT rounding.price_basis(optional):grossfor gross prices, see Invoice from gross prices.app_invoice_items_custom: List of invoice line items.app_amount: Quantity for the line item.app_item_price_net: Unit price (net).app_vat_percentage: VAT rate (0, 7, or 19).app_unit: Unit of measure (e.g. "Hours", "Pieces", "Flat rate").
Output
The invoice object is enriched with calculated fields:
Per line item:
| Field | Description |
|---|---|
app_invoice_item_no |
Sequential item number (1, 2, 3, …) |
app_items_total_sum_net |
Net total for the line item (quantity × unit price) |
app_items_total_sum_gross |
Gross total for the line item |
app_item_price_gross |
Unit price (gross) |
Aggregated fields:
| Field | Description |
|---|---|
app_net_total |
Grand total (net) |
app_gross_total |
Grand total (gross) |
app_vat_total |
Total VAT amount |
app_vat_nineteen |
VAT at 19 % (amount) |
app_vat_seven |
VAT at 7 % (amount) |
app_vat_zero |
VAT at 0 % (amount) |
app_has_vat_nineteen |
true if any line item has 19 % VAT |
app_has_vat_seven |
true if any line item has 7 % VAT |
app_has_vat_zero |
true if any line item has 0 % VAT |
app_has_reverse_charge |
true if the invoice is issued without VAT under Section 13b UStG |
app_net_reverse_charge |
Net amount for reverse charge (otherwise 0) |
app_prepayments |
deducted partial invoices with number, date, net, VAT and gross (otherwise empty) |
app_prepaid_net, app_prepaid_vat, app_prepaid_total |
sum of the partial invoices net, VAT, gross |
app_has_prepayments |
true if partial invoices are deducted |
app_amount_due |
amount still due: gross total minus partial invoices |
app_skonto_date, app_skonto_amount, app_amount_due_skonto |
discount date, discount amount, amount with discount (only with a discount) |
Example
An invoice contains two line items: 10 hours of consulting at €150.00 net (19 %) and 5 pieces of material at €25.00 net (7 %). The service automatically calculates: net total €1,625.00, VAT 19 %: €285.00, VAT 7 %: €8.75, gross total €1,918.75.
JSONata example
{
"vat_rounding": "per_rate",
"app_invoice_items_custom": $.app_invoice_items_custom
}
All other invoice fields (invoice number, date, recipient, etc.) can be included — they are passed through unchanged.
VAT rounding
The field app_vat_rounding (or vat_rounding) defines how VAT is rounded:
| Value | Calculation |
|---|---|
per_rate |
For each line item, the net total (quantity × unit price) is rounded to the cent. VAT is then calculated and rounded once per rate from the sum of these net totals. This is how the EN 16931 standard, on which ZUGFeRD and XRechnung are based, calculates. |
| (empty) | Previous method: VAT is rounded per unit and multiplied by the quantity. Deprecated and will be removed on January 1, 2027. After that, the service always calculates as with per_rate. |
Any other value results in an error.
Switch from the previous method by January 1, 2027
With the previous method, the rounding error of a single unit is multiplied by the quantity. Example: 1,000 pieces at €0.99 net with 19 % result in €190.00 VAT instead of €188.10. If the VAT of a rate deviates by €1.00 or more, the e-invoice is invalid (validation rules BR-S-09 and BR-CO-17) and may be rejected by the recipient.
What changes when switching:
- Most invoices stay the same to the cent.
- With several line items at the same rate or quantities above 1, the gross amount may change by one cent, e.g. €89.51 instead of €89.50. This mainly affects net prices that were calculated back from a desired gross price.
- VAT and gross total per line item are still calculated for the PDF template. Their sum may differ from the grand total by one cent; the grand total is authoritative.
- Invoices already issued remain unchanged.
How to switch: Set the field vat_rounding (or app_vat_rounding) to per_rate in the invoice data – for the single service in the JSONata input as in the example, for Complete e-invoice alternatively via the service task's inbound mapping.
Invoice from gross prices
For end-customer prices where the gross price is agreed and paid, for example in an online shop or an app with fixed prices. With app_price_basis (or price_basis) = gross, the service calculates from the gross price. The invoice then always adds up to exactly the sum of the gross prices.
Why: If you calculate the net price back from a gross price and add the VAT again, a cent is often lost. Example: €2.79 gross ÷ 1.19 = €2.34 net, plus 19 % = €0.44 – the invoice shows €2.78, but €2.79 was paid. With large quantities the error grows: 100 × €2.34 net results in €278.46 instead of €279.00.
How the service calculates with gross:
- Each line item brings its gross price:
app_item_price_grossfor custom line items, the article's gross price (app_gross_price) for articles. - Gross total of the line item = quantity × gross price. This is the amount the line item shows on the invoice.
- Per VAT rate, the VAT is extracted once from the gross total: net = gross total ÷ (1 + rate), rounded to the cent; VAT = gross total − net.
- The net is spread over the line items so that their net totals add up to exactly the net. The net unit price (
app_item_price_net) is derived from it and has more than two decimal places if needed, for example €2.3445 for 100 × €2.79.
| Example | Gross | Net | VAT 19 % |
|---|---|---|---|
| 1 letter at €2.79 | €2.79 | €2.34 | €0.45 |
| 100 letters at €2.79 | €279.00 | €234.45 | €44.55 |
| €10.00 + €1.99 + €5.29 | €17.28 | €14.52 | €2.76 |
The e-invoice stays valid: the VAT deviates from "net × rate" by at most one cent, however large the quantity. The EN 16931 validation rules allow this deviation. Some validation tools show a calculation notice in this case, but the invoice remains valid.
Also note:
- If the gross price of a line item or article is missing, the service stops with an error.
- With reverse charge (
app_tax_mode=reverse_charge_13b),grossdoes not apply: the net prices are used, and each line item needs a net price. - Sub-lines, a labor share under Section 35a EStG and partial or final invoices are not yet possible with gross prices and result in an error.
- Store
app_price_basiswith the invoice (field "Preisbasis" of the "Rechnungen" template from version 1.8.0). A cancellation invoice then takes it over and results in exactly the negative amount of the original invoice. - Without
price_basis, the service calculates with net prices as before. Any other value results in an error.
Notes
- Supports VAT rates of 0 %, 7 %, and 19 %. Other rates will result in an error.
- If the tax mode
app_tax_mode(ortax_mode) is set toreverse_charge_13b, all line items are calculated at 0 %, regardless of the stored VAT rate. Details in Reverse charge (Section 13b UStG) in e-invoices. app_partial_invoicesdeducts partial invoices already issued,app_skonto_percentandapp_skonto_dayscalculate an early payment discount. Details in Partial and final invoices, early payment discount in e-invoices.- With a labor share on articles and line items (
app_labor_cost_mode), labor and material costs are calculated separately, see Labor costs (Section 35a EStG). - All amounts are rounded to two decimal places using commercial rounding. How VAT is rounded is described under VAT rounding.
- For end-customer prices including VAT, the service calculates from the gross price with
app_price_basis=gross, see Invoice from gross prices. - In addition to custom line items (
app_invoice_items_custom), article-based line items (app_invoice_items_articles) are also supported — both types can be combined within a single invoice. - Item numbers are assigned sequentially: custom items first, then article items.
Tip
This service is typically used as the first step in an invoicing pipeline — followed by Generate PDF from Handlebars template and optionally Create e-invoice (ZUGFeRD). Alternatively, the entire chain can be executed in a single step using the Complete e-invoice service.