# How konto works

> Accounts, not contracts: what konto keeps for every sector, what it leaves to the sector engines, and how the crates and the service fit together.

A German Stadtwerk sells electricity, gas, water, broadband and charging, each under its
own law and usually in its own system. The customer still expects one account, one bill
and one direct debit. konto is that account.

## Accounts, not contracts

konto keeps what every sector shares:

- the party, household and site;
- the contract account, the SEPA mandate and how payments are allocated;
- the ledger: receivables, advance payments, credits;
- the bill, its documents and their delivery;
- dunning, up to a *proposed* suspension that a person decides.

The sector contract, its rating and its law stay in the **engines**: an energy engine
rates electricity under the Energiewirtschaftsgesetz, a telecom engine rates broadband
under the Telekommunikationsgesetz, a charging engine rates sessions. An engine sends
konto priced charges and the bill section its law requires. konto places that section
and never edits it, so it never needs to know what a Konzessionsabgabe or a
Sperrandrohung is.

## The contracts between engine and konto

konto meets the engines through the Open Utility Stack's profiles only:

| Profile | Direction | Carries |
|---|---|---|
| `rated-charge@1` | engine → konto | one priced line: contract, product, service period with its zone, net amount, quantity and unit price, VAT category, rate and exemption, the line it corrects, evidence digest |
| `bill-section@1` | engine → konto | per contract: the section's title, the norms it implements, its mandatory figures and texts |
| `collection-terms@1` | engine → konto | per contract: minimum arrears, exclusions, leads, whether it suspends, plan ranges, letter texts, legal basis |
| `bill@1` | konto → anyone | the sealed bill, with the stack's digest |
| `erasure@1` | konto → engines | an erased account, with every engine contract it had |

The published crates know konto's own types only; `konto-stack`, which only kontod uses,
maps the profiles onto them. Because the profiles are the only coupling, either side can
be replaced: a utility can keep its existing billing system and send the same charges.

## The pieces

| Piece | Does |
|---|---|
| `konto-core` | shared vocabulary: identifiers, periods, German money, refusals, the canonical digest, review-then-confirm |
| `konto-bill` | composes, seals and recomputes the bill; explains the change between two bills |
| `konto-ledger` | the books: open items, payments, allocation, the Kontoauszug, claims |
| `konto-collect` | evaluates collection terms, proposes and decides, plans instalments |
| `konto-render` | XRechnung, the PDF, the HTML bill and the delivery notice |
| `konto` | the command line: check, render and compare bills offline (`cargo install konto`) |
| `konto-stack` | the stack's profiles mapped onto konto's types, and the stack's digest; not published |
| `kontod` | the HTTP service: tenancy, persistence, roles, customer sign-in, the charge inbox, delivery, erasure |
| `web/` | the Kundenportal (React, Tailwind) and the recompute module for the browser |

The crates are **pure**: no clock, no network, no files, no randomness and no
floating-point numbers. Dates, keys and identifiers come in as arguments. That makes
them deterministic, testable to the cent, and portable: `konto-core`, `konto-bill` and
`konto-collect` build for WebAssembly, so the browser recomputes a bill with the code
that sealed it.

## A month in the life of an account

1. The engines deliver their charges for January and their sections.
2. A clerk asks for a preview of the next bill, then reviews and confirms it. Confirming
   seals the bill with the next number, books it, takes its charges out of the inbox and
   queues its delivery, in one transaction.
3. The courier sends the PDF, with the e-invoice inside, by e-mail or post.
4. Payments arrive and are allocated to the open items in the statutory order.
5. If a contract falls behind, konto assesses it against the engine's terms: a reminder,
   a threat, a proposal, a plan. A suspension is decided by a person and performed by the
   engine.

## Review, then confirm

Every action with a money or legal effect is two calls. **Review** computes the effect
without performing it and binds it with HMAC-SHA-256 under the deployment's key to the
actor, the action, the request, the revision of every input it read and the result.
**Confirm** recomputes and accepts only if the review was issued by this deployment, has
not been used, and nothing it read has changed. Otherwise it is stale and the caller
reviews again. The same primitive backs a clerk's preview, a customer's check-your-answers
page and the receipt.

## Money is exact

Amounts are decimals, never binary floats, and travel as strings in JSON (`"27.71"`). A
JSON number for money is refused. VAT is derived once per VAT group and every amount is
rounded once, half away from zero, by the `billing` crate.
