npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

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)

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 = gross on 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-ro

Usage

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:

  1. Sum the gross amounts of all lines in the group (integer bani).

  2. Compute the group VAT:

    VAT = round(gross × rate / (100 + rate))
    net = gross − VAT
  3. Give each line a provisional VAT of floor(gross × rate / (100 + rate)).

  4. 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 ESLint

License

MIT