Rust · no_std · zero I/O · no unsafe

🔗Invoice arithmetic a validator will accept

billing computes the numbers on an EN 16931 invoice — line nets, allowances and charges, the VAT breakdown, the totals chain, payments and cash rounding. You state the inputs; every total is derived once, at the document’s scale.

Get started API reference GitHub

cargo add billing

🔗The problem it solves

EN 16931 caps every amount at two decimals and requires the totals to reconcile exactly at that precision. Rounding at the serialiser breaks that (BR-CO-10, BR-CO-15). billing rounds each leaf once and derives every total from the rounded leaves.

How the precision model works →

use billing::{Amount, AmountScale, Currency, Document, Line, Percent, Price, Quantity, UnitPrice, Vat};
use rust_decimal::dec;

// 1234.567 kWh × 0.28901 EUR/kWh = 356.80221 — five decimals.
let doc = Document::builder(Currency::EUR, AmountScale::EN16931)
    .default_vat(Vat::standard(Percent::new(dec!(19)))?)
    .line(Line::new(
        "Arbeit",
        Quantity::new(dec!(1234.567), "KWH"),
        UnitPrice::new(Price::new(dec!(0.28901))),
    ))
    .build()?;

assert_eq!(doc.line_net(0), Amount::parse("356.80")?); // BT-131, rounded once
assert_eq!(doc.vat_total(), Amount::parse("67.79")?);  // BT-110: 356.80 × 19 %
assert_eq!(doc.payable(), Amount::parse("424.59")?);   // BT-115

🔗What is in the box

🔗Fixed-point money

Amount<P> is an i64 count of 10⁻ᴾ, with four sign-symmetric rounding modes and typed Percents. Totals come out at the document’s scale (97.47, or cents). Overflow never wraps.

Money and rounding →

🔗Metering and tariffs

Ten aggregations with exact averages; per-unit, graduated, volume and package tariffs; spot series, a tariff per key, transaction fees, progressive billing and 95th percentiles.

Pricing →

🔗VAT as an attribute

Every line and adjustment carries a Vat; the breakdown (BG-23) is a group-by with the tax rounded once per group.

VAT →

🔗Gross prices

split_gross turns a shelf price into net and tax once; the halves re-add exactly.

Gross prices →

🔗Exact splitting

Largest remainder in integer arithmetic, down to Document::split. No cent is created or lost.

Splitting →

🔗Corrections and settlement

negated, concat, minimum and maximum bounds, advances, customer credits, cash rounding, Skonto and late-payment interest.

Settlement →

🔗Designed to be checkable

🔗Derived, never stored

A Document exists only through build(); every accessor is infallible and there is no validate().

🔗Rules, cited

Every error says why, where and which EN 16931 or Peppol rule it enforces; each rule id is hash-locked against the pinned Schematron.

🔗Mutation-tested and fuzzed

Property tests state the algebraic laws, fuzzers attack the parser, splitter and serde, and cargo-mutants checks the suite.

Design and how correctness is checked →

🔗What it is not

A library of arithmetic — not a billing platform, not an invoice model. Invoice numbers, dates, parties, type codes and XML belong to en16931 and en16931-formats.

Where this crate stops →

Documentation

  • Getting started Install billing, rate a tiered water tariff, attach VAT, and read every EN 16931 total from a document that derived them once.
  • Money and rounding Amount<P>, the four sign-symmetric rounding modes, AmountScale and reducing once, and why a Price is not money until it is multiplied by a quantity.
  • Metering Readings reduced to a billable quantity as data — sum, count, unique count, min, max, latest, mean, weighted sum, weighted mean and percentile — exactly, with averages kept as fractions all the way into the invoice line.
  • Pricing Tariffs as data — per-unit, graduated, volume and package — plus interval series rated exactly, a tariff per key, transaction fees, progressive billing and nearest-rank percentiles.
  • VAT VAT as an attribute of every line and adjustment: the ten UNCL 5305 categories, exemption reasons, and the BG-23 breakdown derived by grouping with the tax rounded once per group.
  • Gross prices Consumer prices are quoted gross and EN 16931 is net. split_gross applies the VAT fraction once — and the invoice still will not always total the shelf prices.
  • Allowances, charges and levies Line allowances and charges (BG-27/28), document allowances and charges (BG-20/21), apportioning a discount across VAT groups, and per-unit levies as ordinary lines.
  • The totals chain BT-106 to BT-115 mapped onto Document's accessors, the order they are derived in, and which EN 16931 rules therefore hold by construction rather than by a check.
  • Splitting Exact largest-remainder splits — split, split_matrix, Document::split and Line::segments — what they guarantee and what they cannot.
  • Corrections and bounds Document::negated for credits, Document::concat for merging with VAT rounded once over the union, and minimum and maximum bounds settled into an ordinary line.
  • Settlement BT-113 payments and advances, BT-114 cash rounding, BT-111 VAT in an accounting currency, customer credits drawn before or after tax, and the residual VAT of a final invoice.
  • Payment terms The arithmetic of payment terms: the Skonto amount and the per-group VAT correction it implies, and statutory late-payment interest rounded once across rate changes. The BT-20 text is the format's.
  • Errors One Error type: kind() says why, location() says where (0-based), rule() names the EN 16931 or Peppol rule it enforces. Match on those, not on the text.
  • EN 16931 and scope Why rounding at the serialiser cannot work, how a Document rounds the leaves once, why BT-146 must never be rounded, and what belongs to en16931 instead.
  • Design The model — inputs in, every total derived once, nothing stored that a caller could edit — the invariants the crate holds itself to, and how correctness is checked.
  • Migrating What to change when upgrading: 0.16 → 0.17, then 0.15 → 0.16. Breaking releases have no deprecation shims; the CHANGELOG has the full old → new tables.