# Deliver bills

> How sealing queues a bill's delivery, how a courier sends it by e-mail or post and reports back, and how consent for electronic invoices is recorded.

A sealed bill that never reaches the customer is a missed payment and a complaint. kontod
queues every bill's delivery in the same transaction that seals it, and a courier, your
mail, print or portal adapter, sends it.

## How a holder receives bills

A clerk records who bills are addressed to:

```json
PUT /v1/accounts/VK-1000123/holder
{
  "buyer": {
    "name": "Erika Mustermann",
    "address": { "lines": ["Musterstraße 4"], "post_code": "12345", "city": "Musterstadt", "country": "DE" }
  }
}
```

A new holder receives bills by post; the holder or a clerk switches to e-mail with consent
([Payment and delivery](self-service.md)). Sending an invoice electronically needs
the recipient's consent, unless the B2B e-invoice obligation applies [UStG § 14 Abs. 1
Satz 5]. Energy suppliers must offer free electronic transmission and paper at least once
a year [EnWG § 40b Abs. 1 Satz 2 Nr. 2, 3].

## What a delivery carries

| Field | Value |
|---|---|
| `number`, `account` | the bill |
| `channel` | `email` or `post` |
| `address` | the e-mail address, or the postal address as lines |
| `document` | `pdf`, or `xrechnung` for a public buyer (with Leitweg-ID) on the e-mail channel |
| `subject`, `text` | the German message: „Ihre Rechnung R-2027-000001“, amount, due date and how it is paid |
| `status` | `pending`, `sent` or `failed`, with the courier's `reference` or `reason` |

The PDF always carries the e-invoice inside, so one attachment serves people and
accounting software.

## The courier

A token with the role `courier` sees only deliveries, the bills' PDF and XRechnung, and
its reports.

```sh
K=(-H "Authorization: Bearer $COURIER" -H 'Content-Type: application/json')
curl -s "${K[@]}" localhost:8080/v1/deliveries          # pending, oldest first, at most 500
curl -s "${K[@]}" localhost:8080/v1/bills/R-2027-000001/pdf > R-2027-000001.pdf
curl -s "${K[@]}" -d '{"reference":"<message-id>"}' localhost:8080/v1/deliveries/R-2027-000001/sent
curl -s "${K[@]}" -d '{"reason":"Postfach existiert nicht"}' localhost:8080/v1/deliveries/R-2027-000001/failed
```

Reports are idempotent: the same report again answers the delivery unchanged; a
different report on a settled delivery answers `409 delivery_settled`. References are at
most 200 and reasons at most 500 characters, without control characters.

## When a delivery fails

A clerk sees failed deliveries with `GET /v1/deliveries?status=failed` and sends one
again, by post if the e-mail address no longer works:

```sh
C=(-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json')   # a clerk's token
curl -s "${C[@]}" -d '{"channel":"post"}' localhost:8080/v1/deliveries/R-2027-000001/requeue
```

konto never retries on its own; the courier decides how often to try a channel.

## Transport

konto does no network I/O. The courier's channel carries the bill: use TLS for SMTP and
sign your mail (DKIM). A forged PDF would still recompute: see
[what a digest proves](bill.md#the-digest).
