# Invoices and documents

> A legal German invoice per § 14 UStG: an EN 16931 e-invoice (XRechnung 3.0 where the buyer can receive one) in CII and UBL, one PDF that is PDF/A-3b, PDF/UA-1 and ZUGFeRD, and an accessible HTML bill.

A sealed bill becomes four documents, all from the same computation, so none of them can
state another amount than the bill.

| Form | For | kontod route |
|---|---|---|
| E-invoice (XRechnung 3.0 or EN 16931), CII or UBL | accounting software, public buyers | `GET /v1/bills/{number}/xrechnung?syntax=cii` |
| PDF with the e-invoice embedded | every customer: read, archive, import | `GET /v1/bills/{number}/pdf` |
| HTML | the bill in a browser or e-mail | `GET /v1/bills/{number}/html` |
| JSON with konto's verdict | your own rendering, the recompute | `GET /v1/bills/{number}` |

## A legal invoice

An invoice names the supplier and the recipient with full name and address and the
supplier's tax number or VAT ID [UStG § 14 Abs. 4 Satz 1 Nr. 1, 2]; it carries a unique,
sequential number [UStG § 14 Abs. 4 Satz 1 Nr. 4]. konto refuses a bill without them.

- **The seller** comes from `KONTO_CREDITORS` only
  ([Run kontod](kontod.md#the-invoicing-profile)).
- **The buyer** is the account's holder, recorded by a clerk. A sealed bill keeps the
  holder it was sealed with; a later change of address affects only later bills.

Both are inside the bill's digest, so a changed address is caught by recompute like a
changed amount.

## The e-invoice

The e-invoice is EN 16931. konto builds it from the same `billing` document it composed
the bill from, through the `en16931` crate, and validates it against every rule of its
profile before writing it:

- **XRechnung 3.0**, the German CIUS, when the buyer has an electronic address: the
  e-mail address bills go to, or a public buyer's Leitweg-ID (scheme `0204`).
- **EN 16931** itself for a household that gets its bills by post. XRechnung requires the
  buyer's electronic address (`PEPPOL-EN16931-R010`), which such a household has not given.

A bill that breaks a rule is not written; the refusal (`einvoice_rejected`) names each
rule, for example `BR-AE-02` for reverse charge without the buyer's VAT ID. The download is
named `R-2027-000001-xrechnung.xml` or `R-2027-000001-en16931.xml`.

| Field | Value |
|---|---|
| Specification (BT-24) | `urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0`, or `urn:cen.eu:en16931:2017` |
| Business process (BT-23) | the Peppol billing process (XRechnung) |
| Buyer electronic address (BT-49) | the e-mail address, or the Leitweg-ID |
| Exemption (BT-120, BT-121) | for an exempt or reverse-charge line: its reason, its VATEX code, or both |
| Buyer reference (BT-10) | the Leitweg-ID of a public buyer, otherwise the contract account |
| Payment means | 59 SEPA direct debit with mandate and creditor identifier, or 58 credit transfer |

Between businesses in Germany an invoice must be an electronic invoice in a structured
format [UStG § 14 Abs. 2 Satz 2 Nr. 1]; XRechnung is one.

## The PDF

One file for people and machines:

- **PDF/A-3b**, so it can be archived for the retention period;
- **PDF/UA-1**, tagged, with the document language set, so screen readers read it in
  order;
- **ZUGFeRD**: the validated e-invoice is embedded, as `xrechnung.xml` (profile XRECHNUNG)
  or `factur-x.xml` (profile EN 16931), so accounting software, and a household's own
  assistant, read the bill exactly;
- a **GiroCode** (EPC QR) when the customer pays by transfer: name, IBAN, amount and the
  bill number for the banking app. A direct debit or a Guthaben gets none.

The PDF is typeset by Typst in a sandbox that sees only its template and the bill as
data, so no producer text is ever evaluated as markup. The same bill gives the same
bytes: the file's timestamps are the issue date.

## The HTML bill

The order follows what a customer asks first: how much, by when and how it is paid; then
what changed; then the sections, VAT and totals; and how to check it. It has no script,
escapes every text it did not write, works at 320 px, in light and dark, with forced
colours and on A4 paper. kontod serves it with a Content-Security-Policy that allows no
script, no foreign resource and no framing.

## On the command line

```sh
konto render rechnung.json -o rechnung.pdf        # or -f cii, ubl, html
```

See [Command line](cli.md).

## In Rust

```rust
use konto_render::{Syntax, html, pdf, xrechnung};

let xml = xrechnung(&bill, Syntax::Cii)?;      // validated, or refused naming the rules
let file = pdf(&bill, Some(&change))?;         // PDF/A-3b + PDF/UA-1, e-invoice inside
let page = html(&bill, Some(&change));         // „Was hat sich geändert?“ included
```
