# billingd Operator Guide

> Operator guide for billingd, the LF multi-product billing engine: 13 product categories, §41a dynamic tariffs, §14a modules and EN 16931 e-invoicing.

Source: https://hupe1980.github.io/mako/docs/services/billingd/

`billingd` is the LF's **billing service**, on port **`:9280`**. It holds no
grid topology and no business policy: prices come from the product in
`productd`, quantities from `edmd`, pass-through grid charges from `marktd`, the
customer from `vertragd`. Given the same inputs a run produces the same
`Rechnung`, so an auditor can re-run it (§ 147 AO / GoBD).

| Term | Meaning |
|---|---|
| **LF** | Lieferant, the role billingd runs as; see [Party Roles](https://hupe1980.github.io/mako/docs/architecture/domain-model/#party-roles-marktrollen) |
| **MaLo** | The delivery point billed, not the meter — see [MaLo vs MeLo](https://hupe1980.github.io/mako/docs/architecture/domain-model/#malo-vs-melo-the-critical-distinction) |
| **NNE** | Netzentgelt, a pass-through priced from the NB's Preisblatt |
| **EN 16931** | The European e-invoice model; XRechnung (German B2G CIUS) and ZUGFeRD (PDF/A-3 hybrid) are profiles of it, CII and UBL its syntaxes |
| **BO4E** | Every issued document is stored as a BO4E `Rechnung` and checked by [the BO4E gate](https://hupe1980.github.io/mako/docs/architecture/domain-model/#the-bo4e-gate) |

---

## Rated charges

Preview returns one `rated-charge@1` document per priced line in `rated_charges`. Each carries the signed net amount, supply period, tax category, source and evidence digest. Issuance publishes the same lines on `de.mako.billing.charge.rated`. The subject identifies the line and the partition key identifies its settlement source. A held invoice publishes on release.

| Operation | Effect on rated charges |
|---|---|
| Standalone Storno | Reverse each booked line. |
| Replacement | Emit the difference from the preceding valuation. Each `corrects` names an original issued line in the same tenant. |
| Revised reading | Keep the booked VAT rate for matching taxable lines. A change of tax category reverses the old line and opens a new one. |

Line references are unique per tenant. Quantity and price appear together when their exact product equals the net amount. Calendar fees use the agreed tariff times the share of active days. A recurring share converts once to decimal working precision. A product or correction difference that exceeds decimal precision stops issuance with HTTP 422 (`RATED_PRODUCT_UNREPRESENTABLE` or `RATED_DIFFERENCE_UNREPRESENTABLE`).

## The `energy-billing` engine

The arithmetic lives in the pure **`energy-billing`** crate (no I/O, no async):

```
billingd (HTTP service)
    │   config · persistence · CloudEvents · EN 16931 rendering · upstream clients
    └── energy-billing
            Product (13 variants) · Quantities · BillingContext · RegulatoryRates
            BillingEngine → Invoice { positions, warnings, netto, mwst, brutto }
            ├── ElectricityProvider          § 41a EPEX; HT/NT; block tariffs; RLM demand
            ├── ControllableLoadProvider     § 14a Modul 1/2/3 (WAERMEPUMPE, WALLBOX)
            ├── GasProvider                  § 25 Nr. 4 MessEV Brennwertkorrektur; BEHG CO₂
            ├── HeatProvider                 Fernwärme
            ├── WaterProvider                Trinkwasser 7 %; gesplittete Abwassergebühr
            ├── SolarProvider                Mieterstrom (§ 42a EnWG); GGV (§ 42b EnWG)
            ├── EegProvider                  LF-side Gutschrift
            ├── EinspeisungProvider          Direktvermarktung Marktwert
            ├── HemsProvider · EmobilityProvider · ServiceProvider
            ├── DynamicElectricityProvider   § 41a per-interval EPEX (iMSys guard)
            ├── EnergyShareProvider          § 42c Energiegemeinschaft credit
            └── MwStProvider                 per-position VAT (19 / 7 / 0 %)
```

`BillingEngine::bill()` returns `Result<Invoice, EngineError>`; every error has a
stable `code()` that billingd surfaces. `Invoice::to_rechnung()` yields a typed
`rubo4e::current::Rechnung` (`rechnungstyp`, `istStorno`,
`originalRechnungsnummer`, `faelligkeitsdatum`, `zuZahlen`, typed
`marktlokation`/`zaehler`/`vertrag`; mako facts as `zusatzAttribute`).

**`rechnungspositionen` are net supply lines only.** Tax goes to
`steuerbetraege`/`gesamtsteuer` and advances to `vorauszahlungen`/`zuZahlen`, so
the engine's `Tax` and `Abschlag` positions are not wire positions
(`BillingPosition::is_rechnungsposition`). `Info` positions stay — they carry
`net_eur == 0` and the § 40 EnWG Zählerstand and Brennwert lines.

**One rounding point per invoice line.** Rating retains the priced amount.
`Invoice::from_positions` rounds each position's net to the cent **once**
(kaufmännisch, DIN 1333, `RoundingStrategy::MidpointAwayFromZero`), and the BO4E
`Rechnung`, the EN 16931 model and the tax breakdown are all derived from those
rounded lines — so they state the same totals and each Steuerbetrag is its
printed base × rate.

Quantity and unit price keep full precision.
`crates/energy-billing/tests/it/one_rounding_point.rs` pins it; `cargo xtask
check-rounding` refuses banker's rounding workspace-wide.

**Inputs are strict.** `GridInput` — the `grid` override on `/calculate` —
denies unknown fields: every field is optional and adds a position only when
set, so a mistyped key would otherwise bill no Netzentgelt at all.

```mermaid
graph TB
    ERP["POST /api/v1/billing/{malo_id}/calculate"]
    vertragd["vertragd :9780<br/>product assignment, buyer"]
    productd["productd :9080<br/>product prices"]
    edmd["edmd :8380<br/>billing period, Lastgang"]
    marktd["marktd :8180<br/>Preisblätter NNE / KA"]
    engine["energy-billing"]
    pg[("billing_records")]
    accountingd["accountingd :9380"]

    ERP --> vertragd & productd & edmd & marktd --> engine --> pg
    pg -->|"de.mako.billing.rechnung.erstellt (outbox)"| accountingd
```

---

## Product categories

All commercial prices come from `productd`. Statutory rates come from dated
series in `energy_billing::rates`; `billingd.toml` `[rates]` only fills a period
a series does not cover. A product may override the Stromsteuer and
Energiesteuer rate.

| Category | Positions |
|---|---|
| `STROM` | Grundpreis (ct/day), Arbeitspreis (ct/kWh), Leistungspreis (RLM, ct/kW/month); NNE Grund-/Arbeits-/Leistungspreis and Konzessionsabgabe from marktd; § 14a modules; Stromsteuer; MwSt. Eintarif, Zweitarif, Mehrtarif, § 41a dynamic |
| `WAERMEPUMPE` · `WALLBOX` | As `STROM` — its fields under `strom` — plus the § 14a positions |
| `GAS` | Brennwertkorrektur (`kWh_Hs = m³ × Hs × Z`), Grund-/Arbeitspreis, Gasnetzentgelt, Konzessionsabgabe, Bilanzierungsumlage, Energiesteuer, BEHG CO₂, MwSt |
| `WAERME` | Grundpreis (EUR/month), Leistungspreis (EUR/kW/month), Arbeitspreis (ct/kWh_th), CO₂ cost line |
| `WASSER` | Trinkwasser 7 %, gesplittete Abwassergebühr, Absetzungen |
| `SOLAR` | Mieterstrom / GGV supply price, contractual GGV-Preisvorteil, § 42a Abs. 4 cap |
| `EEG` | LF-side feed-in Gutschrift (Vergütung, Marktprämie) |
| `EINSPEISUNG` | Direktvermarktung: Marktwert − Vermarktungsgebühr |
| `HEMS` · `EMOBILITY` · `ENERGIEDIENSTLEISTUNG` | Platform/operating fees plus per-event or per-kWh charges |
| `SHARING` | A `STROM` supply plus a credit for edmd’s allocation. Record the plant MeLo in the contract component’s `sharing_community_id`. Billing checks the plant, participant, exact bounds and quarter count. A measured zero is billable. edmd refusals retain their code and fields. |

`productd`'s `BUNDLE` is decomposed into its components; it never appears as a
billed category. `Product` deserialises from productd's JSON on the
`"category"` discriminator, and each category refuses a key it does not define:
a misspelt price in a posted `tariff` is a `422`, never a product billed without
it. A catalog position whose category has no field for it is logged as dropped.
`product.build_engine(&grid, &rates)` builds the engine.

### STROM

The supplier's Leistungspreis is **monthly**
(`leistungspreis_strom_ct_per_kw_month` × months, on `spitzenleistung_kw` from
edmd; RLM or iMSys). The passed-through NNE demand charge is **annual**
(`nne_leistungspreis_eur_per_kw_year` × the billed fraction of a year, § 17 Abs.
2 StromNEV Jahresleistungspreis).

Mixing the two under-bills twelvefold.

### § 14a EnWG modules

Numbered by **BK8-22/010-A** (NSAVER); BK6-22-300 governs the netzorientierte
Steuerung and numbers nothing.

| Modul | What it is | Field |
|---|---|---|
| 1 | Pauschale Reduzierung des Netzentgelts — flat annual amount, billed as quantity 1 „Jahr" × billed years | `sect14a_modul1_pauschale_eur_per_year` |
| 2 | Prozentuale Reduzierung des Arbeitspreises on the device's separately metered energy; Tenor 2. b) fixes the reduced price at **40 %** of the Niederspannungs-Arbeitspreis ohne Leistungsmessung | `sect14a_modul2_nne_reduktion_ct_per_kwh` |
| 3 | Zeitvariable Netzentgelte from 01.04.2025, Tarifstufen HT/ST/NT, iMSys required | `sect14a_modul3_nne_ht/st/nt_ct_per_kwh` |

Modul 1 is `80 EUR + 3 750 kWh × Arbeitspreis × 0,2` nationwide — no per-kW
component, so it fits an SLP heat pump. **Modul 1 + Modul 3 is the only pair.**
Refusals: `MODUL1_AND_MODUL2` (two forms of the base module),
`MODUL2_AND_MODUL3` (both re-price the Arbeitspreis), `MODUL3_AND_FLAT_NNE`,
`MODUL3_OHNE_MODUL1` (BK8-22/010-A offers Modul 3 only with Modul 1) and
`MODUL3_IMSYS_REQUIRED`.

**Which module a customer holds is vertragd's record, not the product's.**
For `WAERMEPUMPE`/`WALLBOX` billingd reads `GET /api/v1/malo/{malo_id}/sect14a`
for the period and bills only the reductions the recorded modules name; the
product states what each is worth. Refused: no state over the whole period
(`SECT14A_MODUL_UNBEKANNT`), a change inside it
(`SECT14A_MODULWECHSEL_IM_ZEITRAUM`), a recorded module the product prices no
reduction for (`SECT14A_MODUL_OHNE_PREIS`), and Modul 3, whose band rates and
band quantities billingd does not resolve (`SECT14A_MODUL3_OHNE_BAENDER`).
An unoffered combination reaches the engine and its guards refuse it.

A Steuerungsentschädigung (`sect14a_steuerungsentschaedigung_ct_per_kwh` /
`_eur_per_kw_year`, catalogue Preistyp `STEUERUNGSENTSCHAEDIGUNG`) pays for a
dispatch that happened and carries no module number.

### GAS

```
Brennwertkorrektur      [info]       m³ × Hs × Z → kWh_Hs  (§ 25 Nr. 4 MessEV)
Grundpreis / Arbeitspreis            productd
Gasnetzentgelt GP / AP, KA, Bilanzierungsumlage   marktd
Energiesteuer Erdgas    series       § 2 Abs. 3 S. 1 Nr. 4 EnergieStG, 0.55 ct/kWh_Hs
                        or exemption notice (energiesteuer_tarif = BEFREIUNG,
                        §§ 25–28 EnergieStG, Erlaubnis nach § 24 Abs. 2)
Entlastungshinweis      [info]       § 53a / § 54 EnergieStG — billed in full; the customer files
CO₂-Abgabe BEHG         series       e.g. 60 EUR/t in 2026
MwSt
```

Supply `gas_meter.messung_qm3`, `brennwert_kwh_per_qm3` and `zustandszahl`.
`gasqualitaet` (`H_GAS` / `L_GAS`, from `marktd.malo.gasqualitaet`) is recorded
as a `ZusatzAttribut` and does not change the amount — the measured Brennwert
already reflects the gas (DVGW G 260). An H2-blend quality has no BO4E wire value
and is not representable.

### The nEHS price (BEHG)

The certificate price is auction-formed from 2026 (§ 10 Abs. 1 BEHG: weekly EEX
auctions from 01.07.2026 inside the § 10 Abs. 2 corridor of 55–65 EUR/t, then a
Verkaufsphase at 68 EUR/t). Resolution for a Gas or Fernwärme period:

1. productd's `GET /api/v1/nehs-prices/latest?date={period_from}` — only a point
   set in the period's own year;
2. the § 10 BEHG table (`energy_billing::behg_ct_per_kwh_for_year`): Festpreise
   2021–2025 and, for 2026, the § 4 Abs. 1 Nr. 2 CO2KostAufG Mittelwert of
   60 EUR/t;
3. a `[[rates.behg_eur_per_t]]` row covering the whole period;
4. otherwise **`RATE_REFUSED`**, naming the year and the missing price.

Before 2021 there is no CO₂ component. EUR/t becomes ct/kWh with the **delivery
year's EBeV Emissionsfaktor**, Brennwert-based because kWh_Hs is billed: Anlage 2
EBeV 2030 Nr. 6 (`3,2508 GJ/MWh` × `0,0558 t CO₂/GJ`) → **0.18139464 kg
CO₂/kWh_Hs**; EBeV 2022 (`0,056`) → 0.18204480 for 2021–2022. § 3 Abs. 2
CO2KostAufG requires the BEHG Standardwerte, so only the price is the
operator's.

#### `[policy] behg_regime` {#policy-behg-regime}

**2027 has no statutory price.** `[policy] behg_regime` names the regime that
binds. It carries the review date 01.01.2027: from then the startup log warns
until the 3. BEHG-ÄndG (BT-Drs. 21/7869) is enacted or lapses and one regime
is fixed; the value never changes by itself.

| `behg_regime` | Legal basis | A supplied 2027 price must |
|---|---|---|
| `rvo_bezugspreis` (default) | § 10 Abs. 3 Satz 2 Nr. 4/Nr. 5 BEHG in force — the „mengengewichteten Durchschnittspreis der Versteigerungen von Berechtigungen nach § 10 des Treibhausgas-Emissionshandelsgesetzes in dem jeweils vorletzten vorangegangenen Quartal" | be positive |
| `korridor` | § 10 Abs. 2 Satz 4 BEHG as the 3. BEHG-ÄndG (BT-Drs. 21/7869, a Gesetzentwurf) would word it: „Für die Jahre 2026 und 2027 … Mindestpreis von 55 Euro … Höchstpreis von 65 Euro" | lie within 55–65 EUR/t |

A 2027 Gas period with neither a 2027 series point nor a `behg_eur_per_t` row
is **refused** under either regime. Under `korridor` an out-of-corridor point is
refused, and an out-of-corridor row already at startup. The CO₂ line's legal
basis names the regime. `behg_gas_ct_per_kwh_override` on a product applies only
where the period has a lawful price. The same 70 EUR/t point that prices the
CO₂ line under `rvo_bezugspreis` is refused under `korridor`, so no statement
below is issued at a price the regime rejects. A 2027 invoice the regime does
price is still refused until the year's § 4 CO2KostAufG price is recorded (the
statement below).

productd prices a Gas quotation for such a year under this setting as well: it
reads it from `GET /api/v1/policy` (Cedar `read-policy`, a same-tenant service
key) and carries no regime key of its own. A productd that cannot read it
refuses the position `BEHG_PREIS_FEHLT`.

**No ETS2 cut-over.** VO (EU) 2026/667 postpones Chapter IVa of 2003/87/EG
until 2028, and billingd has no ETS2 price path. A 2028 Gas period is refused
like a 2027 one until a price is supplied. `cargo xtask check-behg-prices` (in
`just ci`) refuses a BEHG or nEHS price-table row for 2027 or later, so no
year's figure can be guessed into a table.

**The § 3 CO2KostAufG statement.** Every Gas and Fernwärme invoice carries,
beside the levy, informational `co2kostaufg` positions: Nr. 1
Brennstoffemissionen (kg CO₂, derived from Nr. 3 × Nr. 4), the § 4 Abs. 1
price of the delivery year, Nr. 2 the Preisbestandteil, Nr. 3 heizwertbezogener
Emissionsfaktor (kg CO₂/kWh; a Wärmeprodukt's g/kWh is converted), Nr. 4
Energiegehalt (kWh), Nr. 5 Hinweis auf die Erstattungsansprüche nach § 6
Abs. 2 / § 8 Abs. 2. The positions move no money.

Nr. 6 concerns the Vermieter's building (§ 43 GEG) and is not emitted.

**The CO2KostAufG price is not the auction price.** § 4 Abs. 1 CO2KostAufG
fixes the maßgeblicher Preis per year: Nr. 1 the Festpreis to 2025, Nr. 2 the
corridor midpoint of 60 EUR/t in 2026, Nr. 3 from 2027 the average of the § 10
Abs. 1 BEHG auctions from 1 July to 30 November of the preceding year, which
the Umweltbundesamt publishes no later than ten Werktage before the year
(Abs. 2). `energy_billing::co2kostaufg_preis_eur_per_t` holds that figure by
year and has none for 2027 until the UBA publication is recorded.

The levy line prices the supplier's own CO₂ cost from its series point, and
its legal basis names that point's source — in 2026 `nEHS-Zertifikatspreis vom
{datum} (Versteigerung nach § 10 Abs. 1 BEHG)`, never § 4. § 3 Abs. 3 makes
the Preisbestandteil the Nr. 1 emissions times the § 4 Abs. 1 price at the time
of delivery, plus Umsatzsteuer, and the statement states it separately: a 2026
invoice whose CO₂ line is priced from a 63 EUR/t point discloses 60 EUR/t and,
for 1000 kWh_Hs, `0.18139 t CO₂ × 60 EUR/t = 10.88 EUR netto, 12.95 EUR mit
Umsatzsteuer (19 %)`.

A delivery year without a recorded § 4 price is refused **`RATE_REFUSED`**
(`satz` `CO2KostAufG`), and so is a period running into a year with another
§ 4 price (split at 31.12.); a 2027 Gas or Fernwärme invoice with an
Emissionsfaktor is refused until the UBA's 2027 figure is recorded. productd's
quotation shows the § 4 figure too, and quotes a year without one with a
`co2kostaufg_hinweis` instead of refusing.

### Statutory rates by year

| Tax | Rate | Series |
|---|---|---|
| Stromsteuer, § 3 StromStG | 2,05 ct/kWh (20,50 EUR/MWh) | 2003–2027 |
| Energiesteuer Erdgas, § 2 Abs. 3 Satz 1 Nr. 4 EnergieStG | 0,55 ct/kWh_Hs (5,50 EUR/MWh) | 2006–2027 |

`STROMSTEUER_SERIE` and `ENERGIESTEUER_GAS_SERIE` end at the last year the
statute was read for — each row names the provision and the reading date
(2026-10-05, no announced change for 2027).

**A 2028 period is refused** (`RATE_REFUSED`, naming the tax and year) by the
provider that bills that tax; a Strom invoice is never refused for a gas rate.
Nothing stands in: not the last row, not a default. Extend the series after
re-reading the statute, or add a dated `[rates]` row with its source. A product
override still wins.

Heating gas has been 0,55 ct/kWh_Hs throughout — the 2022
Energiesteuersenkungsgesetz and the time-limited Fassungen of § 68 EnergieStG
reach motor fuels only. VAT: `mwst_rate_for_period()` covers the 2020 16 %
window; `mwst_rate_for_gas_waerme_period()` adds the **7 % gas/Fernwärme window
01.10.2022–31.03.2024** (§ 28 Abs. 5/6 UStG).

### A period crossing a rate boundary is refused

A period straddling a statutory Stichtag has no correct single rate:

```json
{
  "error": {
    "code": "ZEITRAUM_UEBERSCHREITET_SATZGRENZE",
    "category": "GAS",
    "period_from": "2024-03-01",
    "period_to": "2024-04-30",
    "stichtage": ["2024-04-01"],
    "legal_basis": "§28 Abs. 5/6 UStG (Gas/Fernwärme), §10 BEHG"
  }
}
```

`energy_billing::steuer_stichtage_im_zeitraum` computes the Stichtage: the USt
windows (commodity-aware), the BEHG price step at every year boundary for gas,
and the 1 January after which a rate series ends. A pinned `[rates]
mwst_rate` waives none of them: it is the rate each leg is billed at.
`energy-billing` refuses a straddling period itself
(`MWST_STICHTAG_IM_ZEITRAUM`, `BEHG_JAHRESGRENZE_IM_ZEITRAUM`, both `Error`),
whichever path hands it one. The billing pipeline normally splits such periods
itself — see [legs](#a-period-is-billed-in-legs).

### SOLAR — Mieterstrom and GGV

```
Arbeitspreis Solar      productd   ct/kWh
GGV-Preisvorteil        productd   contractual, § 42b Abs. 2 Nr. 2 EnWG
Preisobergrenze         [info]     § 42a Abs. 4 EnWG — 90 % of the Grundversorgungs-
                                   Arbeitspreis; a price above it is refused
Stromsteuer             productd   § 3 or § 9 Abs. 1 Nr. 3 StromStG, ground and Erlaubnis
                                   stated on the invoice
MwSt
```

A `SOLAR` product states its `stromsteuer_tarif` in productd's
[`abrechnung`](https://hupe1980.github.io/mako/docs/services/productd/#billing-settings-abrechnung); one
without it is refused (`SOLAR_STROMSTEUER_UNBESTIMMT`). A `KLEINANLAGE` claim
whose facts do not carry § 9 Abs. 1 Nr. 3 blocks the run
(`STROMSTEUER_KLEINANLAGE_NICHT_GETRAGEN`).

On a GGV invoice the allocated PV is exempt and the residual grid draw carries
full Stromsteuer. The Mieterstromzuschlag (§ 21 Abs. 3 EEG 2023) is the
Anlagenbetreiber's claim against the NB, settled by `einsd`, never a tenant
surcharge.

### EEG and EINSPEISUNG (LF side)

The `EEG` category bills **private** contracts where the LF is the contracting
party — the statutory § 51 regime is `einsd`'s. A contract that mirrors § 51
supplies `eeg_meter.kwh_during_negative_epex`. A `KWKG_ZUSCHLAG` catalog position
is reported, not billed: § 6 Abs. 1 KWKG gives the KWK-Anlagenbetreiber the
claim „gegenüber dem Netzbetreiber", which `einsd` settles.

A Kleinunternehmer (§ 19 UStG) operator's Gutschrift is 0 % with
`kleinunternehmer_19_ustg: true` — an election, not a function of plant size (§
12 Abs. 3 UStG zero-rates the PV system supply, which this engine does not
bill).

### Regulatory fields

| Field / finding | Law |
|---|---|
| `kleinunternehmer_19_ustg` → 0 % on a feed-in Gutschrift | § 19 UStG |
| `stromsteuer_tarif` → Kleinanlage, Befreiung or Ermäßigung | § 9 Abs. 1 / 2 / 3 StromStG, §§ 10, 12b StromStV |
| `steuerentlastungen` → note, levy billed in full | § 9a/9b/9c StromStG, §§ 53a, 54 EnergieStG |
| `grundversorgung_arbeitspreis_ct_per_kwh` → Mieterstrom cap | § 42a Abs. 4 EnWG |
| `waerme_co2_kosten_ct_per_kwh` → CO₂ line + § 3 Abs. 1 statement | CO2KostAufG § 3 |
| `settlement_form` → Endrechnung (BT-113 paid) or Restrechnung | § 14 Abs. 5 Satz 2 UStG; BMF 15.10.2024 Rn. 48 |
| `en16931_blocked` → paper or refusal | EN 16931 BR-O-11 ff.; § 27 Abs. 38 UStG |
| Statutory `Info` disclosures → BT-22 notes | CO2KostAufG § 3 · § 53a EnergieStG · § 40 Abs. 2 Nr. 6 EnWG · § 14 WPG |
| `ablesungsart` beside the readings | § 40 Abs. 2 Nr. 6 EnWG |
| `ZWEITARIF_OHNE_HT_NT_AUFTEILUNG` / `HT_NT_SUMME_WEICHT_AB` refused | § 41 EnWG |
| `preisgarantie_bis` disclosed | § 41 Abs. 1 Nr. 4 EnWG |
| `MeteringMode` (SLP/RLM/iMSys) | § 29 Abs. 1 Nr. 1 MsbG |
| `is_estimated` → hervorgehobener Hinweis | § 40a Abs. 2 Satz 3 EnWG |
| `zaehler_replaced` → Zählerwechsel notice | § 41 EnWG |
| `Sect41aAnnualComparison` → informational note, no legal basis of its own (§ 41a Abs. 6 EnWG is a duty to inform about costs, advantages and risks, not an annual comparison) | — |

### A product that cannot price its commodity is refused

`KEIN_ARBEITSPREIS` (Error severity) refuses a Strom, Gas or Fernwärme product
with no way to price energy — a renamed `preistyp` maps to `None` silently and
would bill only the Grundpreis and taxes. An operator who charges nothing states
`0.0`. `KEIN_TRINKWASSERPREIS` does the same for water priced only on the
Abwasser side.

### A public-law Gebühr is a Bescheid, beside the invoice {#gebuehrenbescheid}

A period carrying taxable supply and an öffentlich-rechtliche Abwassergebühr
(`abwasser_regime = PUBLIC_LAW_FEE`, EN 16931 category `O`) is issued as two
records under one `billingRunId` — on `/calculate` and in the § 40b runs: the
supply invoice, which carries its `en16931_json`, and a **Gebührenbescheid**
(`dokumentart = GEBUEHRENBESCHEID`, its own `GB` number, `product_code` suffixed
`:GEBUEHR`). A Bescheid is no invoice: it has no EN 16931 model, stores the view
outputd's `GEBUEHRENBESCHEID` kind renders (`bescheid_json`), and
`GET /api/v1/billing/{id}/pdf` renders it through that kind. Following § 157
Abs. 1 AO it states each Gebühr by Art and Betrag, the Schuldner, and a
Rechtsbehelfsbelehrung naming the Rechtsbehelf, its Frist and the Behörde,
which the operator configures:

```toml
[gebuehrenbescheid]
rechtsbehelf       = "Widerspruch"
frist              = "innerhalb eines Monats nach Bekanntgabe"
behoerde           = "Stadtwerke Musterstadt AöR, Musterstraße 1, 12345 Musterstadt"
zahlungsfrist_tage = 30
```

A period with a Gebühr is refused without it
(`GEBUEHRENBESCHEID_OHNE_RECHTSBEHELFSBELEHRUNG`), and without a
Rechnungsempfänger to name as Schuldner (`GEBUEHRENBESCHEID_OHNE_SCHULDNER`).

---

## Billing a period

```http
POST /api/v1/billing/51238696012/calculate
Content-Type: application/json

{
  "lf_mp_id":   "9910000000002",
  "nb_mp_id":   "9900000000001",
  "period_from": "2026-06-01",
  "period_to":   "2026-06-30",
  "rechnungsnummer": "R2026-06-001"
}
```

billingd fetches the product assignment from vertragd (`GET
/api/v1/malo/{malo_id}/produkte?from=&to=`) priced by productd (`POST
/api/v1/products/{lf_mp_id}/resolve`), the reading from edmd (`GET
/api/v1/billing-period/{malo_id}`), and the NNE and KA sheets from marktd
(`/api/v1/preisblaetter/{nb_mp_id}`, `/api/v1/preisblaetter-ka/{nb_mp_id}`).

Any input can be passed in the body instead — `tariff`, `meter`, `grid`,
`gas_meter`, `waerme_meter`, `wasser_meter`, `solar_meter`, `eeg_meter`,
`hems_meter`, `emobility_meter`, `service_meter`, `eeg_gutschrift_eur`:

```http
POST /api/v1/billing/51238696012/calculate
Content-Type: application/json

{
  "lf_mp_id": "9910000000002",
  "nb_mp_id": "9900000000001",
  "period_from": "2026-06-01",
  "period_to": "2026-06-30",
  "meter": { "arbeitsmenge_kwh": "312.5", "sparte": "STROM" },
  "tariff": {
    "category": "STROM",
    "grundpreis_ct_per_day": "20.0",
    "arbeitspreis_ct_per_kwh": "32.0"
  }
}
```

`POST …/preview` runs the same pipeline without persisting, emitting or
consuming a number (placeholder `PREVIEW-{malo}-{period_from}`) and returns
`{ "preview": true, "netto_eur", "brutto_eur", "warnings", "rechnung" }`.

The Grundpreis is pro-rated when `vertragsbeginn` or `vertragsende` falls in
the period. Every run has a `billing_run_id`, stored on the record and as
`zusatzAttribute["mako:billing_run_id"]`.

### A period is billed in legs

| Split at | Detected from |
|---|---|
| a **Tarifwechsel** | vertragd reports several product slices in the period |
| a **statutory Stichtag** | a VAT or levy regime changes inside the period |

Each leg is billed under its own product and rates, with its **own meter
reading**, and the legs merge into one document (§ 41 Abs. 1 Nr. 4 EnWG asks for
old and new price with their periods).

The record is filed as `category = TARIFWECHSEL` with a `product_code` naming
every product (`STROM-ALT+STROM-NEU`). A leg whose reading the caller supplied
cannot be split further and is refused with the Stichtage named. The § 40b
sweep, `/calculate` and `/tarifwechsel` all take this path.

For a switch whose two readings the operator already holds:

```http
POST /api/v1/billing/{malo_id}/tarifwechsel
Content-Type: application/json

{
  "lf_mp_id":    "9910000000002",
  "period_from": "2026-01-01",
  "period_to":   "2026-01-31",
  "switch_date": "2026-01-15",
  "old_tariff":  { "category": "STROM", "arbeitspreis_ct_per_kwh": "28.0" },
  "new_tariff":  { "category": "STROM", "arbeitspreis_ct_per_kwh": "32.0" },
  "old_meter":   { "arbeitsmenge_kwh": "140" },
  "new_meter":   { "arbeitsmenge_kwh": "170" }
}
```

### § 41a dynamic tariff (iMSys)

With `dynamic_epex: true` on the product, billingd:

1. fetches the 15-minute **Bezug** from edmd
   (`GET /api/v1/energy/{malo_id}?direction=BEZUG&from=&to=`, Berlin-midnight
   bounds) — the register projection, never `/lastgang`, which would add
   Einspeisung, double-count `1.8.1`/`1.8.2` and price a kW register as energy;
2. fetches the 15-minute EPEX prices from productd
   (`GET /api/v1/epex-prices/{date}/quarter-hourly`, keyed on each MTU's UTC
   start);
3. bills `Σ kWh_MTU × (EPEX_MTU + auf_abschlag_ct_per_kwh) / 100`, with an
   optional `dynamic_epex_floor_ct_kwh` (`null` full pass-through, `0` no
   credit);
4. adds NNE, KA and Stromsteuer as usual. `arbeitspreis_ct_per_kwh` is ignored.

There is **no fallback**:

| Situation | Answer |
|---|---|
| No 15-minute series | `422 SECT41A_NO_LASTGANG` |
| edmd unreachable | `502 UPSTREAM_UNAVAILABLE` |
| Consumption intervals without a price | `422 VALIDATION_BLOCKED` / `SECT41A_MISSING_EPEX_PRICES` |
| Not an iMSys | `422 VALIDATION_BLOCKED` / `SECT41A_IMSYS_REQUIRED` (§ 41a Abs. 1) |
| Series empty against a meter reporting consumption | `SECT41A_KEINE_INTERVALLE` |
| Series misses the register total by > 0,5 % (1 kWh floor) | `SECT41A_INTERVALLSUMME_WEICHT_AB` |

Import EPEX prices daily into productd (`PUT /api/v1/epex-prices/{date}`).

---

## Issuing, numbering and correcting

### The Rechnungsnummer is a counter

`invoice_number_series (tenant, series, year)` issues the **fortlaufende**
number § 14 Abs. 4 Nr. 4 UStG requires; `year` is the billed period's year.

| Series | Document |
|---|---|
| `RE-2026-000123` | Rechnung — `/calculate`, Tarifwechsel, § 40b sweep, each participant line of a bundle |
| `SR-2026-000004` | Consolidated — Sammelrechnung, § 42b GGV bundle |
| `ST-2026-000002` | Storno |
| `VG-2026-000017` | § 41e Gutschrift |

A caller may state its own number. Numbers are drawn **before** the engine runs
under a row lock, so a refused calculation leaves a gap — lawful, since UStAE
14.5 Abs. 11 requires uniqueness, not gaplessness.

- **`br_unique_rechnungsnummer (tenant, rechnungsnummer)`** — a collision is
  `409 RECHNUNGSNUMMER_IN_USE`.
- **`br_unique_original`** — one live original per `(malo_id, lf_mp_id,
  period_from, period_to, product_code, tenant)`, excluding corrections, the
  children of a Sammelrechnung, `cancelled` rows and `category = 'VPP'`. Upserts
  repeat the predicate.

Re-running a request replaces the record **only while it is withheld**
(`outcome = 'generated'`). An issued record answers `409 PERIOD_ALREADY_BILLED`
naming `record_id`, `rechnungsnummer` and `outcome`. `outcome` becomes
`dispatched` on every release, with or without an ERP webhook.

### Korrekturrechnung (§ 147 AO / GoBD)

`POST /api/v1/billing/{id}/correction` with `{ "reason": "…" }` writes, in one
transaction, a record with every monetary field negated (`is_correction = true`,
`originalRechnungsnummer`, an `ST` number), enqueues
`de.mako.billing.rechnung.erstellt` with `is_correction: true` (accountingd books the
credit, and cancels the payout item of a Schlussrechnung's Guthaben), and sets
the original's `outcome = cancelled`.

The invoice event identifies the deployment in `source` (`urn:mako:billingd:tenant:<tenant>`) and the invoice issuer in `data.lf_mp_id`. accountingd checks both before booking and requires a nonblank event ID and account reference. The reference identifies the Marktlokation or the collective account of a Sammelrechnung.

Every `de.mako.billing.rechnung.erstellt` carries `schlussrechnung`, `true` for a
Schlussrechnung's own event — read off the issued document's `rechnungstyp`
(`ABSCHLUSSRECHNUNG`), never on a Storno. accountingd raises the § 40c Abs. 3
EnWG payout item for a Schlussrechnung's Guthaben from it.

The negated document crosses the same
[`ensure_conformant`](https://hupe1980.github.io/mako/docs/architecture/domain-model/#the-bo4e-gate) gate as
an invoice; the § 41e VPP-Gutschrift does too.

For a revised reading, send `reason` and `replacement` to the correction endpoint.
The replacement takes the same inputs as `/calculate` for the original issuer and
period. The response contains the new invoice and its full `correction` document.
Send the replacement via `/{id}/versenden` to queue both documents, cancellation first.
Both financial records commit together. A failed calculation or a risk hold leaves the issued invoice
in force. For a further reading, correct the replacement’s `id`. A cancellation
with only `reason` releases the period for a separate calculation. A second Storno is
`409 ALREADY_CANCELLED`; a record never issued is `409 NOT_YET_ISSUED` (no
receivable stands behind it — re-bill or release it). The original's content is
never modified; both documents stay for **eight years** (§ 147 Abs. 3 AO, from
01.01.2025). The Storno's `steuerzeilen` are the original's, negated, so a
cancelled document's tax lines net to zero.

### Risk gate

Every outgoing document — including Sammelrechnung and GGV bundles — is scored
by `billingd::risk` (`[risk]`, on by default) from coded findings: tax-sum
reconciliation, VAT-rate validity, zero energy or negative consumption (moved
energy, so feed-in credits count), estimate chains (§ 40a Abs. 2 EnWG), period
overlap or gap to the previous invoice, rolling deviation. The score is 0–100.

| Band | Default threshold | Effect |
|---|---|---|
| SAMPLE | `sample_at = 20` | Dispatched |
| REVIEW | `review_at = 50` | Dispatched, and listed in the review queue until `POST …/reviewed` |
| HELD | `hold_at = 80` | Withheld until `POST …/release` |

`MWST_STICHTAG_IM_ZEITRAUM` and `BEHG_JAHRESGRENZE_IM_ZEITRAUM` are **blocking**
findings: they hold the invoice regardless of score, because no single rate is
correct for such a period. The engine already refuses such a period, so the
finding is the backstop for an invoice that reaches scoring carrying one. Thresholds are validated at start (`sample_at <
review_at < hold_at ≤ 100`).

`hold_dispatch = false` runs the gate in shadow mode. `GET
/api/v1/billing/review-queue` lists the undecided REVIEW and HELD records; a
release stamps `outcome = dispatched`, `released_by`, `released_at` and sends
the event, a review stamps `reviewed_by`, `reviewed_at` and sends nothing.
Either takes the record off the queue.

---

## E-invoicing (EN 16931)

At bill time `Invoice::to_en16931(profile, seller, buyer)` builds the EN 16931
model from the rounded lines and billingd stores it in
`billing_records.en16931_json`.

The [`en16931`](https://docs.rs/en16931) crate derives BG-23 and BG-22 via
`reconcile`, and [`en16931-formats`](https://docs.rs/en16931-formats) writes CII
and UBL. Every render reads the stored model and answers `422` when it is
missing; nothing rebuilds it from BO4E.

| Render | Output |
|---|---|
| `GET …/{id}/xrechnung` | CII; profile `urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0` on the B2G path |
| `GET …/{id}/ubl` | PEPPOL BIS Billing 3.0 UBL 2.1 |
| `GET …/{id}/pdf` | ZUGFeRD PDF/A-3 (page + `factur-x.xml`) via outputd |

**BT-24 declares plain EN 16931** for a retail invoice: XRechnung needs a
Leitweg-ID (BT-10) and a Peppol endpoint (BT-49) a household lacks, and § 14
UStG requires EN 16931 conformance.

`POST …/submit-b2g` supplies the buyer and the `reference` Leitweg-ID, proves
the model against the full XRechnung 3.0 profile (`Validated<XRechnung>`),
writes CII from that proof with `en16931_formats::write` — so BT-24 names the
profile whose rules ran — and on success emits
`de.mako.billing.xrechnung.b2g.ready` for the ERP's PEPPOL gateway; a violation is
`422` with `violated_rules` and `buyer_gaps`.

**Per-line VAT.** Each line carries its own BT-151/BT-152; `Tax`, `Abschlag` and
`Info` positions are outside the base, and zero-rated bases (category `Z`) are
included so the bases sum to the net.

BO4E `steuerbetraege` come from `tax_subtotals_of` over the same lines and are
derived, never stored. For domestic electricity reverse charge, billingd checks the issuer’s and recipient’s reseller evidence. The BMF describes both parties’ status and the USt 1 TH form. [BMF, 9 April 2026, section I, para. 1, pp. 1–2](https://www.bundesfinanzministerium.de/Content/DE/Downloads/BMF_Schreiben/Steuerarten/Umsatzsteuer/2026-04-09-Nachweis-USt-1-TH.html)

Configure the issuer’s documents by MP-ID. Record the recipient’s `electricity_reseller_evidence` on the customer in vertragd. Each entry has a document reference and two inclusive dates.

```toml
[electricity_reseller_issuers]
"9910000000002" = [{ reference = "VAT-2026-04", valid_from = "2026-01-01", valid_until = "2026-12-31" }]
```

Set `recipient_electricity_reseller` on a calculation to request the treatment explicitly. The customer’s `stromwiederverkaeufer` flag also requests it for electricity. Preview and issuance require evidence covering each billed supply leg. Adjacent renewals count. If billingd cannot read the electricity contract, it stops with `502 UPSTREAM_UNAVAILABLE`. A gap in the evidence returns `422 RESELLER_EVIDENCE_REQUIRED`. Its `evidence` array names the issuer, recipient or both, with the reason. No invoice is booked.

| Rate | Case |
|---|---|
| 19 % | Standard supply |
| 7 % | Trinkwasser (§ 12 Abs. 2 Nr. 1 UStG); gas/Fernwärme only 01.10.2022–31.03.2024 |
| 0 % | Kleinunternehmer feed-in Gutschrift, § 19 UStG |

**Advances.** A Jahresabrechnung lists each paid Abschlag as a BO4E
`Vorauszahlung`; in CII, BT-113 `TotalPrepaidAmount` is their gross sum and
BT-115 `DuePayableAmount` = BT-112 − BT-113 (BR-CO-16).
`Invoice::abschlag_ust_eur` is the tax § 14 Abs. 5 Satz 2 UStG requires an
Endrechnung to state.

#### `[policy] settlement_form` {#policy-settlement-form}

`[policy] settlement_form` chooses `endrechnung` (default: the German paper
norm, § 14 Abs. 5 Satz 2 UStG) or `restrechnung` (BMF-Schreiben v. 15.10.2024,
Rn. 48 — only the remainder, no advances listed); a request may override it per
invoice. Both are lawful and the customer pays the same amount.

**Buyer (BG-7).** One recipient on the `BillingContext` feeds both the BO4E
document and the EN 16931 model. A partly known address is dropped to the name
plus `mako:adresse_unvollstaendig`. A vertragd outage falls back to naming the
supply site.

| Path | Buyer |
|---|---|
| Retail, GGV per Teilnehmer, VPP, Sammelrechnung line | The MaLo's customer — `vertragd` `GET /vertraege/by-malo/{id}` |
| Sammelrechnung document | The Rahmenvertrag holder — `GET /api/v1/rahmenvertraege/{id}/malos` |
| GGV bundle | The § 42b operator — `GET /api/v1/ggv/{ggv_id}/betreiber` (set with `PUT`) |

**Seller.** BT-23, BG-16 (means code 58 + `seller_iban`) and the `[seller]`
address/contact (BR-DE-2..7) are stamped from config; BT-9 is issue + 14 days (§
40c Abs. 1 EnWG); BG-14 carries the Leistungszeitraum (§ 14 Abs. 4 Nr. 6 UStG).

BT-34 is the operator's MP-ID under EAS `0088` only when `Identifier::eas`
accepts it as a GLN (length and GS1 check digit); otherwise it is omitted and
logged (the B2G path then refuses). billingd runs `einvoice::validate` on every
model against its declared profile and logs findings.

### Paper only where § 27 Abs. 38 UStG admits it

A document with no EN 16931 representation (`en16931_blocked`) cannot be an
E-Rechnung; a Gebührenbescheid is none and is issued as its own record. The
E-Rechnung is owed „für eine Leistung an einen anderen Unternehmer für dessen
Unternehmen" (§ 14 Abs. 2 Satz 2 Nr. 1 UStG); § 27 Abs. 38 UStG grants paper
graces keyed on when the Umsatz was **ausgeführt** (the record's last day) and
when the paper is **übermittelt** (today, Europe/Berlin):

| Supply executed | Paper until | Condition |
|---|---|---|
| before 01.01.2025 | — | outside the new § 14 (§ 27 Abs. 1 Satz 1 UStG) |
| 2025–2026 | 31.12.2026 (Nr. 1) | — |
| 2027 | 31.12.2027 (Nr. 2) | the issuer's 2026 Gesamtumsatz (§ 19 Abs. 2 UStG) „nicht mehr als 800 000 Euro" |
| otherwise | — | an E-Rechnung is owed |

The render endpoints answer `422 MODEL_NOT_REPRESENTABLE` where paper is
admissible, naming the ground, and **`422 PAPIER_UNZULAESSIG`** where it is not.
A recipient who is no Unternehmer is outside the duty. Neither fact is assumed
when missing: the recipient comes from `billing_records.empfaenger_unternehmer`
(vertragd's `unternehmer` in the `rechnungsempfaenger` block — the
Rechnungsempfänger's own `kundentyp`, `B2C` no, `B2B_*` yes, not the
contract's), the Gesamtumsatz from `[e_rechnung]`, selected by the tax identity
the record was issued under (`billing_records.aussteller_ust_id` /
`aussteller_steuernummer`, stamped from `seller_vat_id` / `seller_tax_number`).
An issuer or a year with no entry never admits paper.

```toml
[[e_rechnung.gesamtumsatz]]
ust_id = "DE123456789"   # or: steuernummer = "123/456/78901" — exactly one
jahr   = 2026
eur    = "…"
```

One figure per Rechnungsaussteller and calendar year: an entry naming no or
both identities, an (identity, year) listed twice, or a negative figure refuses
startup, so a corrected figure replaces the old entry rather than sitting
beside it unread. billingd issues on paper only, so the recipient's Zustimmung
to another electronic format, which vertragd also records, does not enter this
path.

The EDI grace of Nr. 3 belongs to the INVOIC issuers, not this path.

### Only a Rechnungsempfänger in Germany is invoiced

Everything billingd settles is a supply of gas, electricity or heating/cooling,
taxed as a domestic supply. Whether a supply crosses a border is read from the
Rechnungsempfänger's country code (BT-55, vertragd's `country` in the
`rechnungsempfaenger` block), and billingd refuses the invoice rather than tax
it as domestic where that code is not `DE`:

| Recipient's country | Answer |
|---|---|
| `DE` | invoiced |
| another EU Member State (`AT`, `FR`, …) | **`422 CROSS_BORDER_SUPPLY_UNSUPPORTED`** — the OSS treatment under Art. 369aa MwStSystRL is not built |
| outside the EU (`CH`, `US`, …) | **`422 EXPORT_SUPPLY_UNSUPPORTED`** — an export's treatment is not built |
| not two letters | **`422 RECIPIENT_COUNTRY_INVALID`** |

A recipient whose address carries no country is `DE`, the default the BT-55 of
the document states too; so is an invoice with no recipient on file, which is
addressed to the Marktlokation it settles. Set the `landescode` on a foreign
Kunde's address in vertragd: without it the invoice goes out as domestic.

Every invoice-producing path (`/calculate`, the billing runs, Sammelrechnung,
GGV and the § 41e VPP settlement) asks the rule before the record is written,
so a refused invoice leaves no record and no `de.mako.billing.rechnung.erstellt`;
`/preview` answers the same refusal. The rule is
`mako_fristen::empfaengerland::pruefe_empfaengerland`, the one outputd's issue
path asks.

### The PDF and sending it

`GET …/pdf` sends the stored model, the CII XML and BT-24 to
[outputd](https://hupe1980.github.io/mako/docs/services/outputd/) (`POST /api/v1/render/INVOICE`), which
renders with the tenant's template and answers the PDF plus
`X-Mako-Template-Hash`. billingd validates the payload first (a B2G document
through `einvoice::render_xrechnung_cii`); an invalid model is `422`.

For an **issued** record the first render pins `billing_records.template_hash`
(`COALESCE`, never overwritten), so the PDF reproduces the sent document; a
pinned document answers `ETag` + `Cache-Control: immutable`. A draft pins
nothing.

`POST …/{id}/versenden` issues the invoice: outputd's
`POST /api/v1/documents/issue/INVOICE` renders inside the call that stores the bytes
(§ 147 Abs. 1 Nr. 2–3 AO, eight years) and queues them on the customer's
channels. Idempotent on the Rechnungsnummer; a held draft is `409`. A
Gebührenbescheid record is issued the same way through
`POST /api/v1/documents/issue/GEBUEHRENBESCHEID`, on the channels its invoice
takes.

### Invoice content

- **§ 40c Abs. 2 EnWG issue deadline:** six weeks after the billed period; six
  weeks after the end of the Lieferverhältnis for a Schlussrechnung; **three
  weeks** where § 40b Abs. 1 monthly billing applies — the agreed cadence
  (`"monatliche_abrechnung": true`, set by the sweep from `abrechnungszyklus`),
  never inferred from the period's length. Missing it raises
  `SECT40C_DEADLINE_EXCEEDED`.
- **§ 40c Abs. 1 Fälligkeit:** at the earliest two weeks after the request
  reaches the customer, so the due date runs from the issue date.
- **§ 40c Abs. 3 Guthaben:** offset against the next Abschlag or paid out within
  two weeks (always paid out from an Abschlussrechnung); stated as the
  `guthabenerstattung` ZusatzAttribut.
- **Schlussrechnung:** `"schlussrechnung": true` on `/calculate`, with
  `"abschlaege": [{datum, betrag_eur, ust_satz}]` each at its own rate (§ 14
  Abs. 5 UStG).
- **§ 40 Abs. 2 EnWG:** supplier identity plus the statutory hints
  (Schlichtungsstelle Energie § 111b EnWG, BNetzA Verbraucherservice,
  Energieberatung, the § 40 Abs. 2 Satz 1 Nr. 12 Wechselhinweis with the § 41c
  Vergleichsinstrumente) on every document.

### E-invoicing mandates

| Regime | Basis | Status |
|---|---|---|
| B2G (federal contracting authorities) | § 4a EGovG + ERechV (in force 27.11.2018), Directive 2014/55/EU | Mandatory since 27.11.2020; orders up to EUR 1 000 exempt (§ 3 Abs. 3 ERechV); Länder have their own rules |
| B2B (domestic) | § 14 UStG as amended by the Wachstumschancengesetz (27.03.2024) | Receiving since 01.01.2025; issuing from 01.01.2027 above EUR 800 000 prior-year turnover, from 01.01.2028 for all |

---

## § 40b scheduled runs

`[billing_runs]` (default off) sweeps daily after `run_hour_utc` (default 4).
vertragd supplies the active contracts and their `abrechnungszyklus` (`GET
/api/v1/vertraege/billing-candidates`).

For each contract the sweep walks back from the most recently completed period
(month, quarter, half-year, or the year before the `vertragsbeginn` anniversary
for `JAEHRLICH`) until it finds one already billed — a `cancelled` row does not
count — at most `catch_up_periods` (default 13), billing each through the
`/calculate` pipeline. `billing_run_log` keeps one row per tenant/LF/month with
`records_count`, `errors_count` (any error marks the month `failed`) and
`skipped_count`.

**Abschlussrechnung.** vertragd keeps an ended supply in the candidates for
twelve weeks after its `lieferende`, flagged `beendet`. The sweep bills it once:
the days after the last billed one up to `lieferende`, as a `SCHLUSSRECHNUNG`
(§ 40c Abs. 2 Satz 1 EnWG), whose credit § 40c Abs. 3 Satz 2 pays out. The
cadence period the supply ends in is never billed as an ordinary invoice, and a
supply already billed through its end gets none. A `lieferende` already past
ends the supply even before vertragd's Ablauf worker flags it.

### Billing watch and recovery

Open **Heute** in the Lieferant workspace for open periods and **Überblick** for the per-Lieferant totals. Expand the discovery result to inspect when billingd asked vertragd for the roster, the range of ended supplies it covered and its next scheduled attempt. A completed run with two candidates assessed two supply components. Each component can yield several billing periods. A current or failed run has an unknown candidate count. A missing attempt means the installation has recorded none.

Both `GET /api/v1/billing/unbilled` and `GET /api/v1/billing/deadline-summary` return the latest attempt and last recorded failure. Their MCP twins return the same evidence. A held invoice leaves its period open. Only issuance of an eligible live retail original for the same tenant, Lieferant, MaLo and exact dates discharges it. A Storno makes that issuer’s period open again when there is no other eligible issued original. The Desk shows the refusal and the due date. Issuance and the authorized output channel's delivery evidence appear separately on the invoice. The statutory duty to make the invoice available to the customer is described in § 40c Abs. 2 EnWG. [EnWG, § 40c Abs. 2](https://www.gesetze-im-internet.de/enwg_2005/__40c.html)

To recover, an LF operator can call `POST /api/v1/billing/runs/sweep` with JSON body `{}` and a UUID `Idempotency-Key`. This runs the installation's ordinary billing and delivery policy. Reuse the key to reconcile a lost response. Use a fresh key for a new attempt after correcting the reported failure. A concurrent sweep returns a conflict. Automatic billing can be disabled independently of manual recovery.

Each attempt stores the source window before contacting vertragd. A missing roster, a mismatched range or count, an unknown billing cadence, or an inconsistent supply interval leaves coverage open. After restart, the next worker records an abandoned attempt as interrupted. After a long outage each successful run covers one outstanding inclusive interval of at most 85 dates, oldest first, alongside current active supplies. Inspect `ended_through` and repeat until it reaches the current Berlin date. A successfully examined empty list advances that interval. Only dates since the installation's first discovery window can be reconstructed automatically.

For a retail invoice, the product assignments, § 14a module choice and customer contract are selected for the invoice’s issuer and supply interval. Delivery resolves the recipient with the stored period and issuer. Overlapping supply components and product assignments that leave a gap or bill a day twice are refused.

The operational watch stores a stable refusal code. Each run can remove up to 500 settled entries whose latest eligible issuance is at least 84 days old. Open obligations and the canonical invoices survive cleanup. The limit is an engineering choice for these working records.

**Advances.** A Jahresrechnung deducts the paid Abschläge, each at its own rate
(§ 40 Abs. 1 EnWG, § 14 Abs. 5 Satz 2 UStG), read from accountingd
(`GET /api/v1/accounts/{malo}/abschlaege` with the invoice’s `lf_mp_id`, `from` and `to`). An unreachable accountingd is an
error. Without `accountingd_url` a settling cadence is recorded as
`ABSCHLAEGE_UNBEKANNT` in `unbilled_periods` unless `jahresrechnung = true`
declares that no advances are collected.

**Sending.** `versand` (default **true**) issues each document of a period
through outputd outside the billing transaction — the invoice and, beside it,
its Gebührenbescheid, on the same channels to the same recipient: a Bescheid
never sent starts no Rechtsbehelfsfrist. A failure is logged at `error` and
retried with `POST …/versenden`. A held period is never sent.

**§ 40b Abs. 3 EnWG.** With `abrechnungsinformation` (default true) iMSys MaLos
get the free monthly `de.mako.billing.abrechnungsinformation.monatlich`, claimed once
per tenant, Lieferant, MaLo and month in `abrechnungsinfo_log`. The claim and its outbound event commit together.

---

## VPP dispatch settlement (§ 41e EnWG)

A confirmed WiM Steuerungsauftrag — JSON over the BDEW API-Webdienste Strom
`controlMeasuresV1` channel, run by makod as `wim-steuerungsauftrag`, no EDIFACT
and no Prüfidentifikator — reaches billingd as `de.mako.vpp.dispatch.confirmed`. The
settlement is a **Gutschrift** (§ 14 Abs. 2 Satz 2 UStG): the aggregator pays the
flexibility provider. § 41e EnWG governs the contract; the remuneration is
contractual.

```mermaid
sequenceDiagram
    participant NB
    participant makod
    participant billingd
    participant accountingd
    NB->>makod: Steuerungsauftrag (controlMeasuresV1)
    makod--)billingd: de.mako.vpp.dispatch.confirmed {tx_id, location_id, max_power_kw, execution_time_from/until}
    billingd->>billingd: signature + tx_id idempotency, Aggregatorvertrag for the SR
    billingd--)accountingd: de.mako.vpp.settlement.berechnet
```

`POST /api/v1/webhooks/vpp-dispatch` settles **only when `sender_mp_id` is the
contracted `aggregator_mp_id`**: a § 14a Steuerung by the NB rides the same
channel and is recorded, not paid.

1. Register the Aggregatorvertrag in vertragd (an overlapping window for the
   same SR is `409`, `agg_no_overlap`):

   ```bash
   curl -s -X PUT "http://vertragd:9780/api/v1/aggregatorvertraege/C0001234567890" \
     -H "Authorization: Bearer <token>" \
     -H "Content-Type: application/json" \
     -d '{
       "vpp_id": "VPP-PORTFOLIO-001",
       "malo_id": "51238696012",
       "aggregator_mp_id": "9910000000002",
       "capacity_price_eur_per_kwh": "0.12",
       "vertragsbeginn": "2026-01-01",
       "vertragsende": null
     }'
   ```

2. Set `vpp_auto_billing = true` and `inbound_webhook_secret` in `billingd.toml`.
3. Name billingd as an endpoint in marktd's config, with the same secret:

   ```toml
   [[subscriptions.endpoints]]
   name        = "billingd-vpp"
   url         = "http://billingd:9280/api/v1/webhooks/vpp-dispatch"
   secret      = "env:BILLINGD_INBOUND_HMAC_SECRET"
   event_types = ["de.mako.vpp.dispatch.confirmed"]
   ```

Automatic settlement requires decimal `max_power_kw` and a bounded execution window. Billing multiplies the stated power by the elapsed hours. A missing or invalid input returns `422 DISPATCH_EVIDENCE_INVALID` and leaves the dispatch available for correction.

The Gutschrift has `category = "VPP"`, `product_code = "VPP_{vpp_id}"`,
`rechnungsart = GUTSCHRIFT`, one `vpp_dispatch` credit position (legal basis `§
41e EnWG, Art. 17 RL (EU) 2019/944, VPP-Vertrag`), `zusatzAttribute` `vpp_id`,
`tx_id`, `sr_id`, `flexibility_kwh`, and a `VG` number; VAT uses the period's statutory rate
(`try_regulatory_rates_for_period`, category `VPP`) on both the webhook and the
manual run. Neither the Aggregatorvertrag nor the request body carries a VAT
rate — a body naming `mwst_rate_override` is refused — and a manual period
straddling a USt Stichtag is refused `422` `ZEITRAUM_UEBERSCHREITET_SATZGRENZE`
with its `stichtage`.

The webhook and manual endpoint share one dispatch ledger. Supply a distinct, nonempty `tx_id` for each manual dispatch. The response counts previously settled IDs in `skipped_already_settled`. If a concurrent settlement wins, retry the batch after `409 DISPATCH_ALREADY_SETTLED`. A repeated webhook delivery answers `202`.

An event with neither `data.tx_id` nor a CloudEvent `id` is `400`. With automatic billing disabled, an eligible dispatch stays available for manual settlement under the same ID. A dispatch without a contract is recorded as excluded. agentd's `vpp-billing-agent` checks completeness, arithmetic and the
Art. 17 audit fields.

---

## Rechnung nachrechnen {#nachrechnen}

Every invoice can be recomputed to the cent from what it states
(`energy_billing::recompute`): each line as quantity × unit price, `gesamtnetto`
from the lines, each Steuerbetrag from base × rate, `gesamtsteuer`,
`gesamtbrutto` and `zuZahlen` less the Vorauszahlungen. Each line carries its
inputs — quantity and how it was obtained (`quantity_source`, `estimated` for
an estimate or Ersatzwert), unit price and its source, the rules. Recomputing
proves the arithmetic, not the inputs.

At issue the record is **sealed**: SHA-256 over the RFC 8785 form of
`rechnung_json` (`seal_sha256`); the verdict says whether it still holds. With
`[nachrechnen]` the record also gets a **verify token** — HMAC-SHA256 of
`token_key` over the record id, 128 bits — stored only as its hash. The public
link is `public_url` + token; portald serves it (`/nachrechnen/{token}`) from
the masked view, which names no customer and masks the MaLo-ID. `POST
/api/v1/billing/{id}/verify-token/revoke` (`correct-billing`) withdraws it; a
revoked token answers 404 like an unknown one. The link travels to outputd
with every invoice render and issue (`nachrechnen_url`), so the letter prints
it with its QR code, and the operator view (`GET …/nachrechnung`, MCP
`recompute_invoice`) carries it as `public_link`.

```toml
[nachrechnen]
token_key  = "env:BILLINGD_NACHRECHNEN_KEY"   # ≥ 32 bytes
public_url = "https://kundenportal.example.de/nachrechnen/"
```

---

## Endpoints

| Method | Path | Description |
|---|---|---|
| `POST` | `/api/v1/billing/{malo_id}/calculate` | Bill, persist, emit |
| `POST` | `/api/v1/billing/{malo_id}/preview` | Dry run |
| `GET` | `/api/v1/billing` | Records (`?malo_id=&lf_mp_id=&outcome=&category=&is_correction=`, filtered in the query) |
| `GET` | `/api/v1/billing/{id}` | One record with its `Rechnung` |
| `GET` | `/api/v1/billing/{id}/xrechnung` · `/ubl` · `/pdf` | Renderings of the stored model |
| `POST` | `/api/v1/billing/{id}/versenden` | Issue to the customer via outputd |
| `POST` | `/api/v1/billing/{id}/correction` | Stornorechnung |
| `POST` | `/api/v1/billing/{malo_id}/tarifwechsel` | Hand-split Tarifwechsel |
| `POST` | `/api/v1/billing/sammelrechnung/{rahmenvertrag_id}` | B2B Sammelrechnung, one transaction, scored as a bundle |
| `POST` | `/api/v1/billing/ggv/{ggv_id}` | § 42b EnWG GGV run, one transaction |
| `POST` | `/api/v1/billing/vpp/{vpp_id}` | Manual § 41e settlement |
| `POST` | `/api/v1/webhooks/vpp-dispatch` | Signed `de.mako.vpp.dispatch.confirmed` |
| `GET` | `/api/v1/billing/review-queue` | REVIEW and HELD, highest risk first |
| `POST` | `/api/v1/billing/runs/sweep` | Run or reconcile one manual sweep using a UUID Idempotency-Key |
| `GET` | `/api/v1/billing/unbilled` | The § 40c watch, earliest `due_on` first (`?open=false` includes settled) |
| `GET` | `/api/v1/billing/deadline-summary` | Per Lieferant: owed, issued on time, issued late, overdue |
| `GET` | `/api/v1/billing/{id}/nachrechnung` | The record recomputed, with the seal verdict |
| `POST` | `/api/v1/billing/nachrechnen` | `{"token"}` → the masked recomputation the public page renders; the token rides in the body, never in a path. Cedar `read-billing-by-token`: portald's service principal alone (`[[oidc.service_keys]] name = "portald"`) |
| `POST` | `/api/v1/billing/{id}/verify-token/revoke` | Revoke the verify token |
| `POST` | `/api/v1/billing/{id}/release` | Release a held record |
| `POST` | `/api/v1/billing/{id}/reviewed` | Record the review of a REVIEW record; sends nothing |
| `POST` | `/api/v1/billing/{id}/submit-b2g` | XRechnung B2G (§ 4a EGovG i.V.m. ERechV) |
| `GET`/`POST`/… | `/api/v1/webhooks/endpoints/**` · `/api/v1/events` | Webhook endpoints, deliveries, replay and the event feed — see [Webhooks](#webhooks) |
| `GET` | `/api/v1/openapi.json` | The OpenAPI document; `services/billingd/openapi.json` is its committed copy |
| `GET` | `/api/v1/policy` | Operator policy (Cedar `read-policy`) |
| `GET` | `/admin/einrichtung` | Configuration findings and policy (Cedar `read-config`) |
| `GET` | `/health/live` · `/health/ready` | Liveness; readiness |
| `POST` | `/mcp` | MCP |

### Webhooks

Every CloudEvent billingd emits is enqueued in `event_outbox` with the write
it announces. `erp_webhook_url` is the webhook endpoint `default`, signed with
`erp_hmac_secret` (Standard Webhooks); further endpoints are registered under
`/api/v1/webhooks/endpoints` (Cedar `manage-subscription`, ADMIN), and
`GET /api/v1/events` (`read-events`) reads the events as a feed. The routes,
signatures, ordering, replay and retention (`[subscriptions] retention_days`)
are the shared module's:
[Webhook subscriptions](https://hupe1980.github.io/mako/docs/architecture/erp-integration/#webhook-subscriptions).

### Errors

One envelope, `{ "error": { "code", "message", … } }`:

| Code | Status | Meaning |
|---|---|---|
| `INVALID_PERIOD`, `INVALID_DATE`, `SWITCH_DATE_OUTSIDE_PERIOD` | 400 | Malformed, reversed or misplaced date |
| `FORBIDDEN` | 403 | Cedar denied |
| `RECORD_NOT_FOUND` | 404 | No such record in this tenant |
| `PERIOD_ALREADY_BILLED` | 409 | An issued document holds the period |
| `RECHNUNGSNUMMER_IN_USE` | 409 | § 14 Abs. 4 Nr. 4 UStG collision |
| `NOT_HELD`, `ALREADY_CANCELLED`, `NOT_YET_ISSUED` | 409 | Wrong record state |
| `VERIFY_TOKEN_NOT_FOUND` | 404 | The record carries no verify token |
| `NOT_AN_ORIGINAL` | 422 | A Storno is recomputed through its original |
| `ZEITRAUM_UEBERSCHREITET_SATZGRENZE` | 422 | Period straddles a Stichtag |
| `RATE_REFUSED` | 422 | No lawful statutory rate for the period (e.g. a 2028 tax, a 2027 nEHS price) |
| `VALIDATION_BLOCKED` | 422 | The engine refused; the body lists every blocking warning |
| `SECT41A_NO_LASTGANG` | 422 | Dynamic tariff without interval data |
| `NO_METER_DATA`, `NO_ACTIVE_PRODUCT` | 422 | edmd / productd has nothing |
| `TARIFSTAFFEL_OHNE_MENGE` | 422 | No Preisstaffel tier selectable (no or too low `jahresverbrauch_kwh`, or kW tiers) |
| `MODEL_MISSING`, `XRECHNUNG_NOT_CONFORMANT`, `BT24_NOT_AN_INVOICE` | 422 | Stored EN 16931 model absent or inconsistent |
| `MODEL_NOT_REPRESENTABLE` · `PAPIER_UNZULAESSIG` | 422 | No EN 16931 form; paper admissible · not admissible |
| `CROSS_BORDER_SUPPLY_UNSUPPORTED` · `EXPORT_SUPPLY_UNSUPPORTED` · `RECIPIENT_COUNTRY_INVALID` | 422 | Rechnungsempfänger in another EU Member State · outside the EU · country code not two letters |
| `NUTZUNGSPLAN_INVALID`, `NUTZUNGSPLAN_INCOMPLETE`, `RABATT_EXCEEDS_ARBEITSPREIS` | 422 | GGV input that would mis-bill |
| `UPSTREAM_UNAVAILABLE` | 502 | An upstream did not answer; the body names it |
| `NO_ERP_WEBHOOK` | 503 | B2G submission with nothing to transmit it |

---

## Authorization

`policies/billingd.cedar`; every business route checks one action, tenant
equality is part of every rule, and Cedar is default-deny.

| Action | Routes | Roles |
|---|---|---|
| `read-billing` | Billing records, review queue, renderings and Nachrechnung | any caller of the tenant |
| `read-billing-watch` | Unbilled periods and deadline summary, including their MCP reads | `LF` |
| `run-billing-sweep` | Manual billing sweep | `LF` |
| `preview-billing` | `…/preview` | any caller of the tenant |
| `run-billing` | `/calculate`, `/tarifwechsel`, `/sammelrechnung/…`, `/ggv/…` | `LF`, `MSB`, `ESA` |
| `issue-billing` | `…/versenden` | `LF`, `MSB`, `ESA` |
| `settle-flexibility` | `/billing/vpp/{vpp_id}` | `LF`, `MSB`, `ESA` |
| `correct-billing` | `…/correction`, `…/verify-token/revoke` | `LF`, `MSB`, `ESA` |
| `release-billing` | `…/release` | `LF`, `MSB`, `ESA` |
| `review-billing` | `…/reviewed` | `LF`, `MSB`, `ESA` |
| `submit-b2g` | `…/submit-b2g` | `LF`, `MSB`, `ESA` |
| `use-mcp` | `/mcp`, after the `[mcp]` / OIDC credential check | any caller of the tenant |
| `manage-subscription` · `read-events` | `/api/v1/webhooks/**`, the `list_webhook_*` tools · `GET /api/v1/events` | `ADMIN` |

The VPP webhook is signature-authenticated and carries no Cedar action.
`mako_roles` are market roles, so the policy cannot separate „may bill" from
„may release"; `released_by` / `released_at` and `reviewed_by` / `reviewed_at`
keep releases and reviews attributable.

---

## MCP server

`/mcp` (Streamable HTTP, revisions 2026-07-28 and 2025-11-25, no session): **eighteen tools** and six prompts, every
tool **read-only**, run in process. There is no `calculate_billing`: issuing a
Rechnung is a legally binding act for an OIDC identity, not a model.

| Tool | Description |
|---|---|
| `get_policy` | The operator policy and posture the deployment runs under |
| `list_billing_records` | Records of a MaLo (summary) |
| `get_billing_record` | Full `Rechnung` of one record |
| `preview_billing` | The `/preview` pipeline |
| `get_xrechnung` | CII from the stored model |
| `check_billing_anomaly` | Rolling 3-month deviation check |
| `list_vpp_settlements` | § 41e settlements (filtered in the query) |
| `list_corrections` | Storno chains (filtered in the query) |
| `list_product_categories` | The 13 categories and their fields |
| `get_billing_summary` | Aggregates per MaLo or LF, each euro once |
| `validate_tariff_config` | Engine validation (incl. `KEIN_ARBEITSPREIS`), § 41a iMSys guard, § 42 Energiemix disclosure |
| `explain_invoice_position` | `PositionTrace` of a position |
| `list_review_queue` | The undecided REVIEW + HELD records with their risk findings, typed (`Pruefposten`) |
| `list_unbilled_periods` | The § 40c watch, typed (`UnbilledPeriods`) |
| `summarize_billing_deadlines` | Per Lieferant: owed, on time, late, overdue, typed (`DeadlineSummary`) |
| `recompute_invoice` | Rechnung nachrechnen for one record, typed (`Nachrechnung`) |
| `list_webhook_endpoints` | The webhook endpoints, the twin of `GET /api/v1/webhooks/endpoints` (`manage-subscription`) |
| `list_webhook_deliveries` | One endpoint's deliveries, the twin of `GET …/endpoints/{id}/deliveries` (`manage-subscription`) |

Prompts: `order-to-cash`, `preview-invoice`, `check-dynamic-tariff`,
`14a-steuerungsrabatt`, `eeg-billing`, `gas-billing`. agentd's
`tariff-optimization-agent` uses `list_billing_records` and
`get_billing_summary`.

---

## Database

| Table | Purpose |
|---|---|
| `billing_records` | `id`, `malo_id`, `lf_mp_id`, `product_code`, `category`, `rechnungsnummer`, `period_from/to`, `rechnung_json` (BO4E), `en16931_json` (model), `en16931_blocked`, `dokumentart` (`RECHNUNG`/`GEBUEHRENBESCHEID`), `bescheid_json`, `empfaenger_unternehmer`, `bo4e_version`, totals, `outcome` (`generated` → `dispatched` → `paid`/`partial`/`disputed`; `cancelled`), `risk_score/band/findings`, `released_by/at`, `reviewed_by/at`, `template_hash`, `billing_run_id`, `due_on` (§ 40c Abs. 2), `seal_sha256`, `verify_token_sha256`, `verify_revoked_at` |
| `steuerzeilen` | One typed Strom-/Energiesteuer line per document: `steuer`, `art` (`REGELSATZ`/`ERMAESSIGUNG`/`BEFREIUNG`), `rechtsgrundlage`, `menge`, `einheit`, `satz_ct`, `betrag_eur`, the supply period — written by the statement that writes the `billing_records` row (a re-run draft's lines are replaced); a consolidated document carries none, its children do |
| `unbilled_periods` | Refused periods keyed by tenant, issuer, MaLo and dates. `unbilled_period_status` derives settlement from live issued supply invoices. |
| `billing_discovery` · `billing_discovery_issuers` | Attempt, coverage window, source outcome and each enumerated issuer’s results |
| `invoice_number_series` | `(tenant, series, year)` → `last_value`, allocated by upsert under a row lock |
| `vpp_dispatch_ledger` | One receipt per `(tenant, tx_id)`. `pending_manual` admits a manual settlement and `record_id` identifies the issued credit. |
| `billing_run_log` | Monthly sweep audit |
| `abrechnungsinfo_log` | § 40b Abs. 3 claims |

`template_hash` is a plain value because it crosses a service boundary;
outputd's append-only template store keeps it resolvable.

---

## Configuration

```toml
tenant        = "stadtwerke-musterstadt"   # isolation key, never printed

productd_url  = "http://productd:9080"    # required
edmd_url      = "http://edmd:8380"        # required
marktd_url    = "http://marktd:8180"      # required
vertragd_url    = "http://vertragd:9780"  # product assignment, buyer
outputd_url     = "http://outputd:9880"   # render and issue
accountingd_url = "http://accountingd:9380" # paid Abschläge

# Required: the seller (BT-27) every Rechnung and Gebührenbescheid names.
seller_name       = "Stadtwerke Musterstadt GmbH"
# seller_gln      = "4006381333931"        # BT-34, EAS 0088 — a GS1 GLN only
# § 14 Abs. 4 Nr. 2 UStG: one of the two is required, or billingd refuses to start.
seller_vat_id     = "DE123456789"          # BT-31
seller_tax_number = "123/456/78901"        # BT-32
seller_iban       = "DE89370400440532013000" # BT-84, BG-16
seller_bic        = "COBADEFFXXX"          # BT-86

erp_webhook_url        = "http://erp:8000/webhooks/billing"  # the webhook endpoint `default`
vpp_auto_billing       = false
inbound_webhook_secret = "env:BILLINGD_INBOUND_HMAC_SECRET"

[subscriptions]
retention_days = 30        # how long emitted events stay replayable
# allow_loopback = true    # `just dev` only: admit http and loopback endpoints

[http]
addr = "0.0.0.0:9280"

# ── Tables follow; TOML binds a bare key to the most recent header ──────
[seller]   # BG-5 / BG-6, one EN 16931 term per key (BR-DE-2..7)
street    = "Musterweg 1"
post_code = "12345"
city      = "Musterstadt"
country   = "DE"
phone     = "+49 30 123456"
email     = "rechnung@example.de"

[policy]
behg_regime     = "rvo_bezugspreis"   # or "korridor"
settlement_form = "endrechnung"       # or "restrechnung"

[rates]
mwst_rate    = "0.19"
# A dated, sourced row fills a period no statutory series covers. A row for a
# covered year, an overlapping row or a row without `quelle` is refused at
# startup — by `--print-config` as well; a BEHG row is checked against
# [policy] behg_regime.
# [[rates.behg_eur_per_t]]
# von    = "2027-01-01"
# bis    = "2027-03-31"
# wert   = "…"            # EUR/t
# quelle = "…"
# [[rates.stromsteuer_ct_per_kwh]]        # ct/kWh, same shape
# [[rates.energiesteuer_gas_ct_per_kwh]]  # ct/kWh_Hs, same shape

[billing_runs]
enabled = false

[database]
url = "postgresql://billingd:secret@db:5432/billingd"
```

Further keys: `*_api_key` for each upstream, `erp_hmac_secret`,
`min_meter_coverage_pct`, `vergleichsgruppe_kwh_pro_jahr` /
`vergleichsgruppe_label`, `[e_rechnung]`, `[risk]`
(`enabled`, `hold_dispatch`, `sample_at`, `review_at`, `hold_at`),
[`[gebuehrenbescheid]`](#gebuehrenbescheid),
`[mcp]`, `[oidc]`, `allow_insecure_no_auth`.

`[rates] behg_regime`, `[billing]`, `seller_address` and `seller_contact` are
refused at startup, each naming the key that replaces it. Every setting
under `[policy]` is listed in
[Operator policy](https://hupe1980.github.io/mako/docs/reference/operator-policy/), logged at startup with
its origin and source, exported as `mako_policy_info` and served at
`GET /api/v1/policy` (Cedar `read-policy`).

`[rates]`, `[seller]`, `[e_rechnung]`, `[billing_runs]`, `[risk]` and
`[policy]` deny unknown fields — a year-less
`stromsteuer_ct_per_kwh = "2.05"` is refused at startup. The top level does not:
a `mwst_rate` written above the first table header binds to nothing.
