# Monthly billing

> Trace advance charges, usage and corrections through invoice sealing and ledger posting.


A bill is never edited and never accumulated by hand. It is **composed** from the facts
every time it is needed.

The bill for month M carries, per subscription:

| Lines | What | Example |
|---|---|---|
| **Advance** | M's monthly charges, one-offs and discounts, for the days the subscription is active under each plan assignment | „Glasfaser 1000 49,99 €" |
| **Corrections** | Month M−1 re-rated with today's facts, minus what M−1's bill charged | „Korrektur November 2026: Glasfaser 2000 5,00 €" |
| **Usage** | The usage dated in M−1, in arrears | „Anrufe in Mobilfunknetze (13 Min.) 2,47 €" |

A plan change, a termination or a late activation therefore settles on the next bill
without anyone writing a correction. Corrections compare per item and VAT rate, so a
plan change shows the net change of the base fee as one line, and items that did not
change stay off the bill.

```rust
use telco_core::bill::{BillInput, PlanSlice, SubscriptionInput, compose};
use time::macros::date;

# let book = telco_core::compile(&[("book.toml".to_owned(), br#"
# [book]
# id = "fibre"
# version = "2026-11"
# prices = "gross"
# currency = "EUR"
# [tax.standard]
# rates = [{ from = "2021-01-01", percent = "19" }]
# [plan.g1000]
# name = "Glasfaser 1000"
# tax = "standard"
# recurring.base = { label = "Glasfaser 1000", amount = "49.99" }
# "#.to_vec())])?;
let books = |reference: &str| (reference == "fibre@2026-11").then_some(&book);
let running = SubscriptionInput {
    id: "s1".into(),
    active_from: Some(date!(2026 - 01 - 01)),
    ends_on: None,
    slices: vec![PlanSlice { plan: "g1000".into(), book_version: "fibre@2026-11".into(), from: date!(2026 - 01 - 01), to: None }],
};
// December is billed in advance on 1 December…
let dec = compose(&books, &BillInput { period: "2026-12".parse()?, subscriptions: std::slice::from_ref(&running), usage: &[], billed_previous: &[] })?;
// …the contract then ends on 15 December: January credits the rest.
let ended = SubscriptionInput { ends_on: Some(date!(2026 - 12 - 15)), ..running };
let jan = compose(&books, &BillInput { period: "2027-01".parse()?, subscriptions: &[ended], usage: &[], billed_previous: &dec.charges })?;
assert_eq!(jan.totals.lines[0].label, "Korrektur Dezember 2026: Glasfaser 1000");
assert_eq!(jan.totals.lines[0].amount.to_string(), "-25.80"); // 16 of 31 days
# Ok::<(), telco_core::Refusal>(())
```

## The open bill

"Your bill so far" is the same composition, run for the next month without sealing
(`GET /billing-accounts/{id}/open-bill`). When nothing changes in between, the sealed
bill equals the preview.

## Sealing

billd seals each billing account's month once, from the first of the month (or from
the first activation). Missed months are caught up in order. Each seal is one
transaction:

1. lock the account;
2. compose;
3. allocate the next number of a gapless series (`RE-2026-000001`), after composition,
   so a refused bill returns its number;
4. store the bill and its charges with a SHA-256 of its compact UTF-8 JSON content, sorting object keys recursively and preserving array order;
5. mark the usage as billed;
6. stage the booking;
7. emit `de.telco.bill.sealed`.

Sealed bills cannot be updated or deleted by any role.

- **Held:** if a subscription pins a price-book version that is not loaded, the
  account's bill is held, recorded once as `de.telco.bill.held`, and other accounts carry
  on.
- **Final:** when every subscription ended before the month, the bill is marked
  `final`, and nothing is sealed after it.
- **Credit notes:** `POST /bills/{id}/credit-note` reverses a whole bill in its own
  series (`ST-2026-000001`), cites it and needs a reason. A bill is credited at most
  once. On paper it is a „Stornorechnung": in German VAT law „Gutschrift" means a
  self-billed invoice (§ 14 Abs. 2 Satz 5 UStG), so telco never prints that word.

## The ledger

Every sealed document is booked by a posting pass into a
[doubleentry](https://hupe1980.github.io/doubleentry) ledger in its own schema:

- the billing account's receivable (gross);
- against revenue (net) and VAT per rate;
- carrying the document number and its hash.

The number is the entry's idempotency key, so a retry never books twice. A refused
booking is parked with its reason, and the bill stays sealed.
`GET /billing-accounts/{id}/balance` reads the receivable from the ledger.

## Investigate a subscription’s usage

In the Desk, open **Konten**, choose the account, then select the usage link under **Verträge**. Choose the month when the service was **used**. Each row shows the source record ID, tariff usage kind, exact base-unit quantity and receipt date. For example, a call can have 61 measured seconds even when the tariff charges for two minutes. **Offen** means the record has no invoice assignment yet. **Rechnung öffnen** opens the invoice that actually consumed it.

Starting from an invoice, use **Verbrauchsnachweise** to open the subscription and month recorded on a usage charge. This also works when the invoice was issued in a later month.

For the same lookup in billd, send

```http
GET /subscriptions/{id}/usage?period=2026-10&limit=50
```

Omit `period` for the month on the service clock. Records are sorted by source ID. To continue, send the returned `period` and URL-encode `next` as the `after` parameter. A null `next` marks the last page. Each read reflects the latest accepted records and invoice assignments. An empty month returns an empty list. An unknown subscription returns 404.

## Customer readings and line objections

With an account-scoped customer credential, `GET /selfcare/billing` returns the next open invoice estimate, its calculation date and the account's sealed documents. Read a sealed invoice at `GET /selfcare/bills/{id}?lang=de-easy` or use `de` or `en`. Readings retain the invoice number, line positions, service dates, tax basis, totals and payment date. The HTML original is available at `/selfcare/bills/{id}/original`.

A customer submits a reason and a zero-based line position to `PUT /selfcare/bills/{id}/objections/{case_id}` with a client-generated UUID. Reuse that UUID and body after an interrupted response. The receipt records the amount on that line and whether it includes VAT. Operators can read the queue at `GET /billing-objections` and answer once at `PUT /billing-objections/{id}/decision`. To document a reversal, supply the UUID of the already issued credit note in `remedy_bill`. An answer alone keeps the evidence hold; record the final decision or withdrawal with its reference at `PUT /billing-objections/{id}/resolution`. The customer receives the answer and can open the linked document. Opening a case leaves the payment ledger untouched. For arrears review, use the bill and line identifiers to count each contested line once, even if it has several messages.

The mailbox tracks opening an invoice, reading an objection receipt and reading its answer separately. The operator's dispatch and financial records have their own evidence. [Embed the customer kit](../docs/selfcare/)
