# Deliver charges

> Send rated-charge@1 and bill-section@1 documents to kontod, then preview, review and confirm the bill konto assembles from them.

An engine reports what it rated; konto assembles the bill. The engine needs a token with
the role `producer`.
## Send charges

Each charge is a `rated-charge@1` document, the Open Utility Stack's profile for one rated
line. Its account is the document's `account`, or else the delivery's:

```sh
curl -s -H "Authorization: Bearer $ENGINE" -H 'Content-Type: application/json' \
  -d '{"account":"VK-1000123","charges":[{
        "profile": "rated-charge@1",
        "client_reference": "strom-2027-01-arbeit",
        "source_reference": "mako:vertrag/V-77",
        "product": "strom-arbeitspreis",
        "section": "strom",
        "service_period": { "from": "2027-01-01", "to": "2027-01-31", "zone": "Europe/Berlin" },
        "net_amount": { "currency": "EUR", "value": "97.4688", "scale": 4 },
        "quantity": { "value": "312.4", "unit": "KWH" },
        "unit_price": { "currency": "EUR", "value": "0.3120", "scale": 4 },
        "tax_category": "S",
        "tax_rate": "0.19",
        "description": "Arbeitspreis 312,4 kWh × 31,20 ct",
        "rating_evidence_digest": "sha256:…"
      }]}' \
  localhost:8080/v1/charges
# {"received":1,"replayed":0}
```

- **Immutable by reference.** A charge is identified by its `client_reference` per
  creditor. Sending the same document again is a replay; sending other content under the
  same reference is refused (`409 duplicate_id`). A correction is a new charge that names
  the one it corrects in `corrects`.
- **Checked before it is stored.** Quantity times unit price must give the net amount. A
  line in category `E`, `AE` or `O` needs its `tax_exemption` (text, VATEX code or both),
  which the e-invoice carries as BT-120 and BT-121. A currency other than EUR, an unknown
  field, or a document naming another account than its delivery is refused, and nothing
  enters the inbox.
- **The zone travels.** The service period's zone becomes the bill period's zone; a bill
  whose charges were rated in different zones is refused.
- **Your extensions stay yours.** Sector `extensions` in the document are accepted and
  never read.

## Send sections

A section is what the engine's law requires on the bill of one contract, as
`bill-section@1`: the title, the norms it implements, the mandatory figures (`facts`) and
texts (`blocks`).

```sh
curl -s -H "Authorization: Bearer $ENGINE" -H 'Content-Type: application/json' \
  -d '{"sections":[{"profile":"bill-section@1","key":"strom","producer":"mako",
       "contract":"mako:vertrag/V-77","ordinal":1,"title":"Strom","legal_basis":["§ 40 EnWG"],
       "facts":[{"id":"consumption","label":"Verbrauch","value":"312,4 kWh"}],
       "blocks":[{"id":"lieferstelle","text":"Lieferstelle Musterstraße 4"}]}]}' \
  localhost:8080/v1/sections
```

A bill takes, for each section its charges name, the section of the contract they came
from. A newer delivery for a contract and key replaces the older one for bills sealed
afterwards; a sealed bill keeps the section it was sealed with, and records the digest
of the document. `ordinal` is a preference: ties break by key, and a section without one
goes last. Two contracts on one bill need two keys.


## Preview, review, confirm

A bill takes the account's open charges whose service period lies within the bill
period, the sections they name, and the payments with an open amount dated on or before
the period's end. Charges for a later period wait.

```sh
C=(-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json')   # a clerk's token
JAN='"period":{"start":"2027-01-01","end":"2027-02-01"},"issue_date":"2027-02-03","due_date":"2027-02-17"'
curl -s "${C[@]}" -d "{$JAN}" localhost:8080/v1/accounts/VK-1000123/bills/preview
R=$(curl -s "${C[@]}" -d "{\"series\":\"R-2027\",$JAN}" localhost:8080/v1/accounts/VK-1000123/bills/review)
curl -s "${C[@]}" -d "$(jq -n --argjson r "$R" "{series:\"R-2027\",number:\$r.number,review:\$r.review,$JAN}")" \
  localhost:8080/v1/accounts/VK-1000123/bills/confirm
```

Periods are half-open: `end` is the first day after the period. The confirm assembles the
bill again; if a charge, section or payment changed since the review, it answers
`409 stale_evidence` and nothing is sealed. On success the bill is sealed, booked, its
charges leave the inbox and its delivery is queued, all in one transaction.

The account needs a holder first: `PUT /v1/accounts/{account}/holder` with the buyer
([Deliver bills](delivery.md)); a mandate and e-mail delivery are optional
([Payment and delivery](self-service.md)).

## Check a section on its own

A producer can preview an explicit bill request with `POST /v1/bills/preview` to see how
its section renders, without the inbox and without booking.
