# Rate a month

> Apply a price book to a subscription and carry allowance and tier usage across batches.


`rate` prices **one subscription for one calendar month**. It is a pure function of
the compiled book, the subscription, the period state and the usage records.

## What a call emits

On the first call for a period — a fresh `PeriodState` — it emits:

1. **Recurring charges**, billed in advance for the days of the month the contract is
   active, prorated by calendar day (15 of 30 days is exactly half). If the VAT rate
   changes inside the month, each charge splits at the change date.
2. **One-off charges** whose date (contract start or end) falls in the month.
3. **Discounts** in their months: month 1 is the month the contract starts, even if
   it starts mid-month. A discount is prorated like the charge it reduces and never
   exceeds it.
4. **Usage**, in arrears: each record's quantity is rounded by the charge's Takt,
   drawn from an allowance, priced in its tier. Records are processed in (date, id)
   order whatever order you pass them in. Usage of a kind in a flat becomes a
   zero-amount *included* charge, so it is still traced.

Later calls with the returned state rate usage only.

## Batches

Usage may arrive in several batches. Pass the state from one call to the next; the
allowance and tier positions carry over, and the result equals rating everything at
once — as long as batches are chronological. A batch earlier than the last one is
refused with `rate.out-of-order`.

```rust
use telco_core::{PeriodState, Subscription, UsageRecord, compile, rate};
use time::macros::date;

let book = compile(&[("book.toml".to_owned(), r#"
[book]
id = "m"
version = "1"
prices = "gross"
currency = "EUR"
[tax.standard]
rates = [{ from = "2021-01-01", percent = "19" }]
[kind.data]
label = "Datenvolumen"
measure = "bytes"
[plan.smart]
name = "Smart"
tax = "standard"
allowance.data = { label = "Inklusivvolumen", kinds = ["data"], units = "25 GB" }
usage.data = { label = "Datenvolumen über 25 GB", price = "2.00", per = "1 GB", round = "1 KB" }
"#.as_bytes().to_vec())])?;

let sub = Subscription { window: None, plan: "smart".into(), start: date!(2026 - 01 - 01), end: None };
let rec = |id: &str, day, gb: u64| UsageRecord {
    id: id.into(), kind: "data".into(), date: day, quantity: gb * 1_000_000_000,
};

let first = rate(&book, &sub, &PeriodState::new("2026-11".parse()?), &[rec("a", date!(2026 - 11 - 03), 20)])?;
let second = rate(&book, &sub, &first.state, &[rec("b", date!(2026 - 11 - 20), 7)])?;

assert_eq!(first.state.allowance_used["data"], 20_000_000_000);
let overage = second.charges.iter().find(|c| c.label == "Datenvolumen über 25 GB").unwrap();
assert_eq!(overage.amount.to_string(), "4.00"); // 2 GB beyond the allowance
# Ok::<(), telco_core::Refusal>(())
```

## Deterministic

The crate reads no clock, file, environment or network, uses no floats and iterates
only ordered collections. Rating the same inputs gives byte-identical serialized
output — property-tested over thousands of random cases. That is what makes a
re-rating, a dispute answer or an accuracy audit reproducible.

## What it refuses

Usage outside the month or the contract, duplicate or empty ids, kinds the book does
not define or the plan does not price, a date with no VAT rate, and quantities that
would overflow. Each is a [refusal](../docs/refusals/) naming the record — never a
silent drop.
