# A bill that proves itself

> How konto composes and seals a bill, what its digest covers, how anyone recomputes it to the cent, and how „Was hat sich geändert?“ explains a change.

A customer who doubts a bill should not have to trust the company that wrote it. A konto
bill carries all of its inputs, its results and a digest over both, so anyone can
recompute it, in a browser too, and see that it holds to the cent.

## Compose

A bill request names the account, the creditor, the period, the issue and due dates,
seller and buyer, the sections, the charges and the payments already received. konto
then:

- places each engine's section in its order, with the texts the engine supplies, and
  never edits them;
- derives VAT once per VAT group (category and rate) and rounds every amount once;
- subtracts payments received (Abschläge); a negative amount payable is a Guthaben;
- refuses what it cannot bill: an unknown section, a charge outside the period, an
  exempt charge without a reason, a fee that is not subject to VAT on an invoice.

Every refusal names what is wrong in German, with a stable code
([Refusal codes](refusals.md)).

## Seal or preview

`preview` and `seal` are the same computation. A preview has no number and is not
booked; it answers „Was kommt auf die nächste Rechnung?“. Sealing takes the next number of
a gapless series (`R-2027-000001`, `R-2027-000002`, …) and refuses a number that is not
the next one.

In kontod, sealing is always reviewed and then confirmed, and the confirm re-assembles the
bill: if a charge, section or payment changed after the review, the confirm is stale and
nothing is sealed.

## The digest

A bill's digest is the Open Utility Stack's: SHA-256 over the RFC 8785 (JSON
Canonicalization Scheme) form of the bill as a `bill@1` document, written `sha256:<hex>`.
Anyone recomputes it with a JCS library and no konto code. kontod serves the document at
`GET /v1/bills/{number}/document`, names its digest in `GET /v1/bills/{number}`, and the
portal shows it as the Prüfsumme. The household bill in
`crates/konto-bill/tests/fixtures/household.json` is the stack's `bill@1` example; its
digest is `sha256:29910f2fa36bb69a2f419edfead0be64622d9ae4b4153b5d261c1a0787555e23`.

Inside, `konto-bill` seals each bill with its own encoding, so the crate needs no
knowledge of the stack. „Rechnung nachrechnen“ checks that seal:

| Value | Bytes |
|---|---|
| domain tag | the tag as a text, first (`konto/bill/v1`) |
| text | u64 big-endian byte length, then the UTF-8 bytes |
| decimal | the text of its normalised form: no trailing zeros, `-` for negatives, `0` for zero |
| date | the text `YYYY-MM-DD` |
| u64, u32 | big-endian, 8 or 4 bytes |
| tag | one byte, the variant index |
| option | `0x00` for none; `0x01` then the value |
| list | u64 big-endian count, then each element |
| digest | its 32 raw bytes |

A digest proves that the bill is consistent, not who issued it: anyone who changes a bill
can compute a new digest. Who issued a bill follows from where it came from, the
authenticated portal or the creditor's own delivery.

## Recompute (Nachrechnen)

`recompute` takes a bill, composes it again from its own inputs and compares line by
line, VAT group by VAT group, total by total and finally the digest. It answers
„Stimmt auf den Cent.“ or names the first difference:

```text
Abweichung bei Zeile strom-2027-01-arbeit: angegeben 1,00 €, nachgerechnet 97,47 €.
```

The same function runs in kontod (`GET /v1/bills/{number}` returns its verdict), in the
Rust crate, and in the browser as WebAssembly
([Recompute in the browser](browser-recompute.md)). Try it on the
[start page](../).

## What changed (Was hat sich geändert?)

`variance` compares two bills of the same account and creditor and splits each section's
change into effects that sum exactly to the cent:

| Effect | Means |
|---|---|
| Zeitraum | a longer or shorter billing period |
| Menge | more or less consumption |
| Preis | a changed price or fixed amount |
| Neu | a product that was not on the older bill |
| Entfallen | a product that is no longer billed |
| Korrektur | corrections of earlier charges |

Its German summary names the total change and, for the three sections that changed most,
their two largest effects:

```text
Der Rechnungsbetrag ist um 22,54 € höher als die Rechnung R-2027-000001.
Strom +18,94 € netto (Zeitraum +9,36 €, Preis +6,46 €).
```

Bills of different period lengths or with other sections are not compared; the account
screen then simply shows no sentence.
