vat-split-ro
v1.0.0
Published
Romanian VAT split library — largest-remainder method for splitting gross amounts into net + VAT per rate group (0%, 5%, 9%, 19%), conforming to eFactura vendor reference (HG 602/2025)
Maintainers
Readme
vat-split-ro
Romanian VAT split library for receipts and invoices. Splits a whole receipt's gross amounts into net + VAT per rate group (0%, 5%, 9%, 11%, 19%, 21%, …) using the largest-remainder method, so every line, every rate group, and the invoice totals reconcile to the ban.
Implements the eFactura vendor reference vat-split.js ("procedeul sutei mărite", pct. 36 lit. b) of the Fiscal Code norms, as updated by HG 602/2025.
Features
- Exact reconciliation —
net + VAT = grosson every line, and line sums equal the group and invoice totals, in integer bani. - Group-first calculation — VAT is computed from each rate group's summed gross, then distributed across lines by largest remainder. Per-line splitting is exactly the mistake this library prevents.
- Negative amounts supported — credit notes and storno receipts work, including positive net unit prices.
- Multi-rate receipts — 0%, 5%, 9%, 11%, 19%, 21%, and any other non-negative integer rate.
- Zero dependencies — safe to import from anywhere, including TSX templates.
- TypeScript — typed inputs and outputs, declaration files included.
Installation
npm install vat-split-roUsage
Call splitReceiptVat once with the complete receipt — all line items, discounts, and SGR lines included. Never call it per item or on partial data: the per-rate VAT targets and largest-remainder redistribution require the whole receipt.
import { splitReceiptVat } from "vat-split-ro";
const result = splitReceiptVat([
{ quantity: 1, value: 23.97, vat: 21 },
{ quantity: 1, value: 10, vat: 21 },
{ quantity: 0.75, value: 7.49, vat: 11 },
]);
// result.lines
// [
// { quantity: 1, grossAmount: 23.97, ratePercent: 21, gross: 23.97, vat: 4.16, net: 19.81, netUnitPrice: 19.81 },
// ...
// ]
// result.perRate
// { 11: { base: 6.75, vat: 0.74, gross: 7.49 },
// 21: { base: 28.07, vat: 5.9, gross: 33.97 } }
// result.totals
// { net: 34.82, vat: 6.64, gross: 41.46 }Input shape
interface ReceiptVatItem {
quantity: number; // must be finite and non-zero
value: number; // gross line amount in lei, VAT included (full line value, not unit price)
vat: number; // integer VAT rate percent (0, 5, 9, 11, 19, 21, ...)
}Any additional fields (articleText, articleCode, unit, lineType, …) are carried through to the output lines unchanged.
Note: pass plain objects, not hydrated Mongoose (sub)documents. The library uses object rest/spread, which only copies own enumerable properties — schema fields on a Mongoose document are prototype getters and would be silently dropped. Map documents to plain objects first.
Output shape
{
lines: Array<{
// ...original fields
ratePercent: number; // VAT rate percent
gross: number; // gross line amount (lei)
vat: number; // monetary VAT allocated to the line (lei)
net: number; // net amount (lei)
netUnitPrice: number;// net / quantity, 4 decimals (display only)
}>;
perRate: Record<number, { base: number; vat: number; gross: number }>;
totals: { net: number; vat: number; gross: number };
}The input value (gross) and vat (rate) fields are replaced by gross and ratePercent; vat on an output line is the monetary VAT, not the rate.
The returned amounts are final — do not re-round or recalculate them downstream.
Errors
Invalid input throws VatSplitError:
import { splitReceiptVat, VatSplitError } from "vat-split-ro";
try {
splitReceiptVat([]);
} catch (err) {
if (err instanceof VatSplitError) {
console.error(err.message); // "Cannot split VAT for a receipt with no items"
}
}Validation covers empty receipts, non-finite gross values, non-integer or negative VAT rates, and zero or non-finite quantities. A final reconciliation check guards against corrupted input.
How it works
For each rate group:
Sum the gross amounts of all lines in the group (integer bani).
Compute the group VAT:
VAT = round(gross × rate / (100 + rate)) net = gross − VATGive each line a provisional VAT of
floor(gross × rate / (100 + rate)).Distribute the missing bani to the lines with the largest fractional remainders (ties broken by line order).
Because VAT is derived from the group total and then conserved across lines, the sums always match — including for negative amounts.
Development
npm run build # compile TypeScript to dist/
npm test # run the test suite (node:test + tsx)
npm run lint # run ESLintLicense
MIT
