Skip to main content

grid_billing/
lib.rs

1//! Role-neutral **German grid settlement calculation** engine.
2//!
3//! Covers all DSO/TSO-side INVOIC documents:
4//! - **NNE** (Netznutzungsentgelt) — PIDs 31001, 31005, 31006, 31011
5//! - **MMM** (Mehr-/Mindermengensaldo) — PID 31002
6//! - **MSB** (Messstellenbetrieb) — PID 31009
7//!
8//! ## Calculation flow
9//!
10//! ```text
11//! Input → validation → Settlement Engine → GridSettlement → into_rechnung() (service layer)
12//! ```
13//!
14//! [`GridSettlement`] is the canonical output. The service layer (`netzbilanzd`,
15//! `invoicd`) converts it to BO4E `Rechnung`. This keeps `grid-billing`
16//! publishable to crates.io without pulling in the internal `rubo4e` crate.
17//!
18//! ## Explainability
19//!
20//! Every [`InvoicePosition`] carries a [`CalculationTrace`] with:
21//! - input values (quantity, unit price)
22//! - gross intermediate result before rounding
23//! - applicable [`LegalReference`]s (e.g. `StromNEV §17`, `KAV §2`)
24//! - the [`TariffSource`] justifying each rate
25//!
26//! ## No float money
27//!
28//! All amounts use `rust_decimal::Decimal` and the `billing::EuroAmount` newtype.
29//!
30//! ## Example
31//!
32//! ```rust,no_run
33//! use grid_billing::{NneInput, calculate_nne_invoice};
34//! use rust_decimal::Decimal;
35//! use time::macros::date;
36//!
37//! fn d(s: &str) -> Decimal { Decimal::from_str_exact(s).unwrap() }
38//!
39//! let result = calculate_nne_invoice(&NneInput {
40//!     malo_id: "51238696780".into(),
41//!     nb_mp_id: "9900357000004".into(),
42//!     lf_mp_id: "9900012345678".into(),
43//!     rechnungsnummer: "NNE-2025-001".into(),
44//!     period_from: date!(2025-01-01),
45//!     period_to:   date!(2025-01-31),
46//!     invoice_date: date!(2025-02-15),
47//!     due_date: date!(2025-03-15),
48//!     arbeitsmenge_kwh: d("1500"),
49//!     arbeitspreis_ct_per_kwh: d("3.5"),
50//!     arbeitsmenge_ht_kwh: None,
51//!     arbeitspreis_ht_ct_per_kwh: None,
52//!     arbeitsmenge_nt_kwh: None,
53//!     arbeitspreis_nt_ct_per_kwh: None,
54//!     spitzenleistung_kw: None,
55//!     leistungspreis_eur_per_kw: None,
56//!     ka_satz_ct_per_kwh: Some(d("0.11")),
57//!     tariff_sheet_id: Some("Preisblatt-NNE-2025-Q1".into()),
58//!     sparte: grid_billing::Sparte::Strom,
59//!     ka_klasse: Some(grid_billing::KaKlasse::TarifkundeLow),
60//! }).expect("valid billing input");
61//!
62//! // Every position explains itself:
63//! for pos in &result.positions {
64//!     println!("{}: {}", pos.text, pos.trace.explanation);
65//! }
66//!
67//! // Legal references used:
68//! for r in result.all_legal_refs() {
69//!     println!("  → {r}");
70//! }
71//! ```
72#![deny(unsafe_code)]
73#![warn(missing_docs)]
74
75pub mod billing;
76pub mod error;
77pub mod types;
78
79pub use billing::{
80    calculate_mmm_invoice, calculate_msb_invoice, calculate_nne_invoice, calculate_reversal,
81};
82pub use error::BillingError;
83pub use types::{
84    // Domain types for explainability + audit
85    CalculationTrace,
86    // Backward-compatible alias for GridSettlement — same type, kept for call-site stability
87    GridInvoice,
88    // Core settlement output
89    GridSettlement,
90    InvoicePosition,
91    KaKlasse,
92    LegalReference,
93    // Input types
94    MmmInput,
95    MsbInput,
96    NneInput,
97    QuantityUnit,
98    SettlementStatus,
99    SettlementType,
100    SettlementWarning,
101    Sparte,
102    TariffSource,
103    ValidationResult,
104    WarningSeverity,
105    // Validation functions
106    validate_mmm_input,
107    validate_msb_input,
108    validate_nne_input,
109};