eeg-billing 0.20.0

Pure EEG/KWKG feed-in settlement for German energy markets — EEG 2017–2024 and § 7 KWKG 2023. Zero I/O, no float money.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
//! Monthly settlement lifecycle state machine for EEG plants.
//!
//! Every EEG plant has a **per-period settlement state** that describes whether
//! the full Vergütung, a reduced amount, or no payment at all can be disbursed.
//!
//! ## State machine
//!
//! ```text
//!                ┌─────────────────────────────────────────┐
//!                │               NORMAL FLOW               │
//!                └─────────────────────────────────────────┘
//!
//!   PlantCommissioned ──→ Active (Vergütung flows normally)
//!//!                            ├──→ Reduced (§52 sanction, §53b, technical defect)
//!                            │        └──→ Active (when violation resolved)
//!//!                            ├──→ Suspended (no payment, §52 EEG ≤2021 MaStR)
//!                            │        └──→ Active (when MaStR registered)
//!//!                            ├──→ Interrupted (temporary: negative prices, force majeure)
//!                            │        └──→ Active (next period)
//!//!                            ├──→ PostEeg (Förderdauer expired, EPEX basis)
//!//!                            └──→ Ended (plant decommissioned or Förderdauer expired + no PostEEG)
//! ```
//!
//! ## Relationship to `SettlementStatus`
//!
//! `SettlementStatus` in `SettleOutput` reflects the **calculation result** for
//! a single period. `SettlementPeriodState` is the **persistent plant-level state**
//! stored in `einsd`'s DB and used as context for the next month's settlement.
//!
//! | SettlementStatus | Typical SettlementPeriodState |
//! |---|---|
//! | `Calculated` | `Active` or `Reduced` |
//! | `NoData` | `Active` (data pending) |
//! | `PriceMissing` | `Active` (EPEX data pending) |
//! | `Sanctioned` | `Suspended` or `Reduced` |
//! | `FoerderungBeendet` | `Ended` or `PostEeg` |

use crate::technology::ErzeugungsArt;
use rust_decimal::Decimal;
use rust_decimal::dec;
use time::Date;

// ── SettlementPeriodState ─────────────────────────────────────────────────────

/// Persistent per-plant monthly settlement lifecycle state.
///
/// Stored in `einsd`'s `eeg_anlagen.settlement_state` column.
/// Determines how the next billing period is processed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "SCREAMING_SNAKE_CASE"))]
pub enum SettlementPeriodState {
    /// Normal: full Vergütung / Marktprämie flows as per the applicable scheme.
    ///
    /// `SettleInput::sanktion` should be `None` and `pflichtverstoss` empty.
    Active,

    /// Vergütung reduced to a fraction or different basis due to ongoing sanction.
    ///
    /// Examples:
    /// - §52 Abs. 3 EEG ≤2021: 20% reduction (SanktionAlt::VerguetungReduziert20Prozent)
    /// - §52 Abs. 2 EEG ≤2021: reduced to EPEX Marktwert (SanktionAlt::VerguetungAufMarktwert)
    /// - §52 EEG 2023 Pflichtzahlungen active but Vergütung still flows
    /// - §53b regional reduction in effect
    Reduced,

    /// No EEG payment disbursed.
    ///
    /// Examples:
    /// - §52 Abs. 1 EEG ≤2021: MaStR not registered (VerguetungAufNull)
    /// - §52 Abs. 1 EEG ≤2021: Direktvermarktungspflicht not met (VerguetungAufNull)
    Suspended,

    /// Temporarily no payment this period (data, price, or force-majeure related).
    ///
    /// Unlike `Suspended`, this is not a regulatory sanction — the plant is healthy
    /// and will resume normally next period. No operator action required.
    ///
    /// Examples:
    /// - Meter data not yet available (`SettlementStatus::NoData`)
    /// - EPEX monthly price not yet imported (`SettlementStatus::PriceMissing`)
    Interrupted,

    /// 20-year Förderdauer expired; plant now eligible for post-EEG remuneration.
    ///
    /// Settlement continues but at EPEX spot price (`SettlementScheme::PostEeg`).
    /// The plant's `foerderendedatum` has passed.
    PostEeg,

    /// Plant has no further EEG billing (decommissioned or no post-EEG continuation).
    ///
    /// Terminal state. No more settlement periods expected.
    Ended,
}

impl SettlementPeriodState {
    /// Returns `true` when the plant can potentially receive a payment this period.
    #[must_use]
    pub fn is_payable(self) -> bool {
        matches!(
            self,
            Self::Active | Self::Reduced | Self::PostEeg | Self::Interrupted
        )
    }

    /// Returns `true` when this state represents a regulatory sanction that requires
    /// operator action to resolve.
    #[must_use]
    pub fn requires_operator_action(self) -> bool {
        matches!(self, Self::Suspended | Self::Reduced)
    }

    /// Returns `true` when this is a terminal state (no future settlements).
    #[must_use]
    pub fn is_terminal(self) -> bool {
        self == Self::Ended
    }

    /// Convert to the DB string representation.
    ///
    /// Used for `eeg_anlagen.settlement_state` column.
    #[must_use]
    pub fn to_db_str(self) -> &'static str {
        match self {
            Self::Active => "active",
            Self::Reduced => "reduced",
            Self::Suspended => "suspended",
            Self::Interrupted => "interrupted",
            Self::PostEeg => "post_eeg",
            Self::Ended => "ended",
        }
    }

    /// Parse from DB string.
    ///
    /// # Errors
    ///
    /// Returns `Err` for unknown values.
    pub fn from_db_str(s: &str) -> Result<Self, InvalidSettlementPeriodState> {
        match s {
            "active" => Ok(Self::Active),
            "reduced" => Ok(Self::Reduced),
            "suspended" => Ok(Self::Suspended),
            "interrupted" => Ok(Self::Interrupted),
            "post_eeg" => Ok(Self::PostEeg),
            "ended" => Ok(Self::Ended),
            other => Err(InvalidSettlementPeriodState(other.to_owned())),
        }
    }
}

/// Error returned when a DB string cannot be parsed as [`SettlementPeriodState`].
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("invalid settlement_period_state: '{0}'")]
pub struct InvalidSettlementPeriodState(pub String);

// ── StateTransition ───────────────────────────────────────────────────────────

/// A recorded transition of a plant's settlement state.
///
/// Stored in `einsd`'s `settlement_state_transitions` audit table.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StateTransition {
    /// State before the transition.
    pub from: SettlementPeriodState,
    /// State after the transition.
    pub to: SettlementPeriodState,
    /// First billing period in the new state (year-month).
    pub effective_from: Date,
    /// Human-readable reason for the transition.
    pub reason: StateTransitionReason,
}

/// Reason for a settlement state change.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "SCREAMING_SNAKE_CASE"))]
pub enum StateTransitionReason {
    /// Plant first commissioned and registered in einsd.
    InitialCommissioning,
    /// MaStR registration confirmed → suspending sanction lifted.
    MastrRegistered,
    /// §9 EEG Fernsteuerbarkeit installed.
    FernsteuerbarkeitInstalled,
    /// Direktvermarktung started (§20 / §21 EEG).
    DirektvermarktungStarted,
    /// Direktvermarktung ended, switched back to Einspeisevergütung.
    DirektvermarktungEnded,
    /// §52 violation detected.
    Sect52ViolationDetected,
    /// §52 violation resolved retroactively.
    Sect52ViolationResolved,
    /// Förderdauer expired.
    FoerderungExpired,
    /// Post-EEG operation started (EPEX spot basis).
    PostEegStarted,
    /// Plant decommissioned.
    Decommissioned,
    /// Repowering — new Förderdauer begins.
    Repowering,
}

// ── State derivation helpers ──────────────────────────────────────────────────

/// The compliance facts a settlement state is derived from.
///
/// A struct rather than a row of positional arguments, so a call site says which
/// fact it is passing.
#[derive(Debug, Clone, Copy)]
pub struct SettlementStateFacts {
    /// Whether the plant has a confirmed MaStR registration.
    pub mastr_registriert: bool,
    /// The plant facts § 9 Abs. 2 stages its obligation by — capacity,
    /// technology, Vergütungsform.
    pub sect9_anlage: Sect9Anlage,
    /// Which of the two § 9 Abs. 2 routes the plant actually carries.
    pub sect9_erfuellung: Sect9Erfuellung,
    /// Subsidy end date; `None` = never expires.
    pub foerderendedatum: Option<Date>,
    /// First day of the billing period being evaluated.
    pub billing_date: Date,
    /// EEG law year (0 = KWKG).
    pub eeg_gesetz_year: i16,
}

/// Derive the expected [`SettlementPeriodState`] from plant compliance facts.
///
/// This is a **deterministic helper** — it does not access the DB. The state
/// stored in `einsd` may lag by one billing period, since it is written after
/// each month's settlement run.
///
/// # Example
///
/// ```rust
/// use eeg_billing::settlement_state::{
///     derive_settlement_state, Sect9Anlage, Sect9Erfuellung, SettlementPeriodState,
///     SettlementStateFacts,
/// };
/// use rust_decimal::dec;
/// use time::macros::date;
///
/// let facts = SettlementStateFacts {
///     mastr_registriert: true,
///     sect9_anlage: Sect9Anlage {
///         leistung_kwp: dec!(50),
///         erzeugungsart: None,
///         einspeiseverguetung_oder_mieterstrom: true,
///         ist_kwk_anlage: false,
///         wechselrichterleistung_va: None,
///     },
///     sect9_erfuellung: Sect9Erfuellung::BEIDES,
///     foerderendedatum: Some(date!(2040-12-31)),
///     billing_date: date!(2026-07-01),
///     eeg_gesetz_year: 2023,
/// };
/// assert_eq!(derive_settlement_state(&facts), SettlementPeriodState::Active);
///
/// // § 9 Abs. 2 Satz 1 Nr. 2 asks a geförderte Anlage of this size for lit. a
/// // **and** lit. b, so the 60 % cap alone leaves the Fernsteuerbarkeit open.
/// let cap = SettlementStateFacts {
///     sect9_erfuellung: Sect9Erfuellung::BEGRENZUNG_60,
///     ..facts
/// };
/// assert_eq!(derive_settlement_state(&cap), SettlementPeriodState::Reduced);
///
/// // MaStR not registered, EEG 2023 → Reduced (Pflichtzahlung, not suspension)
/// let no_mastr = SettlementStateFacts { mastr_registriert: false, ..facts };
/// assert_eq!(derive_settlement_state(&no_mastr), SettlementPeriodState::Reduced);
///
/// // The same under EEG ≤2021 → Suspended (Vergütung auf null)
/// let old = SettlementStateFacts { eeg_gesetz_year: 2017, ..no_mastr };
/// assert_eq!(derive_settlement_state(&old), SettlementPeriodState::Suspended);
///
/// // Förderdauer expired → PostEeg
/// let expired = SettlementStateFacts { foerderendedatum: Some(date!(2020-12-31)), ..facts };
/// assert_eq!(derive_settlement_state(&expired), SettlementPeriodState::PostEeg);
/// ```
#[must_use]
pub fn derive_settlement_state(facts: &SettlementStateFacts) -> SettlementPeriodState {
    let &SettlementStateFacts {
        mastr_registriert,
        sect9_anlage,
        sect9_erfuellung,
        foerderendedatum,
        billing_date,
        eeg_gesetz_year,
    } = facts;

    // ── Förderdauer expired ───────────────────────────────────────────────────
    if let Some(fed) = foerderendedatum
        && billing_date > fed
    {
        return SettlementPeriodState::PostEeg;
    }

    // ── MaStR not registered ──────────────────────────────────────────────────
    if !mastr_registriert {
        return if eeg_gesetz_year >= 2023 {
            // EEG 2023: Pflichtzahlung, Vergütung still flows (§52 Abs. 1 Nr. 11)
            SettlementPeriodState::Reduced
        } else {
            // EEG ≤2021 via §100: VerguetungAufNull (§47 EEG 2021 old regime)
            SettlementPeriodState::Suspended
        };
    }

    // ── §9 EEG not satisfied ──────────────────────────────────────────────────
    // The obligation is staged by capacity and gated on the Vergütungsform;
    // `sect9_pflicht` carries the reading.
    if sect9_verletzt(sect9_anlage, sect9_erfuellung) {
        return if eeg_gesetz_year >= 2023 {
            // EEG 2023: Pflichtzahlung €10/kW/month (§52 Abs. 1 Nr. 1)
            SettlementPeriodState::Reduced
        } else {
            // EEG ≤2021: VerguetungAufMarktwert (§52 Abs. 2 old regime)
            SettlementPeriodState::Reduced // reduced to EPEX Marktwert
        };
    }

    // ── All checks pass → Active ──────────────────────────────────────────────
    SettlementPeriodState::Active
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use rust_decimal::dec;
    use time::macros::date;

    #[test]
    fn db_roundtrip_all_states() {
        let states = [
            SettlementPeriodState::Active,
            SettlementPeriodState::Reduced,
            SettlementPeriodState::Suspended,
            SettlementPeriodState::Interrupted,
            SettlementPeriodState::PostEeg,
            SettlementPeriodState::Ended,
        ];
        for s in states {
            let db = s.to_db_str();
            let parsed = SettlementPeriodState::from_db_str(db).unwrap();
            assert_eq!(s, parsed, "roundtrip failed for {s:?}");
        }
    }

    #[test]
    fn unknown_db_str_returns_error() {
        assert!(SettlementPeriodState::from_db_str("unknown").is_err());
    }

    #[test]
    fn is_payable_states() {
        assert!(SettlementPeriodState::Active.is_payable());
        assert!(SettlementPeriodState::Reduced.is_payable());
        assert!(SettlementPeriodState::PostEeg.is_payable());
        assert!(SettlementPeriodState::Interrupted.is_payable());
        assert!(!SettlementPeriodState::Suspended.is_payable());
        assert!(!SettlementPeriodState::Ended.is_payable());
    }

    /// A healthy geförderte 50-kW plant, and the facts that move it off `Active`.
    fn gesund() -> SettlementStateFacts {
        SettlementStateFacts {
            mastr_registriert: true,
            sect9_anlage: Sect9Anlage {
                leistung_kwp: dec!(50),
                erzeugungsart: None,
                einspeiseverguetung_oder_mieterstrom: true,
                ist_kwk_anlage: false,
                wechselrichterleistung_va: None,
            },
            sect9_erfuellung: Sect9Erfuellung::BEIDES,
            foerderendedatum: Some(date!(2040 - 12 - 31)),
            billing_date: date!(2026 - 07 - 01),
            eeg_gesetz_year: 2023,
        }
    }

    fn mit_leistung(kwp: rust_decimal::Decimal) -> Sect9Anlage {
        Sect9Anlage {
            leistung_kwp: kwp,
            ..gesund().sect9_anlage
        }
    }

    #[test]
    fn derive_active_healthy_plant() {
        assert_eq!(
            derive_settlement_state(&gesund()),
            SettlementPeriodState::Active
        );
    }

    #[test]
    fn derive_post_eeg_expired() {
        let facts = SettlementStateFacts {
            foerderendedatum: Some(date!(2020 - 12 - 31)),
            ..gesund()
        };
        assert_eq!(
            derive_settlement_state(&facts),
            SettlementPeriodState::PostEeg
        );
    }

    #[test]
    fn derive_reduced_eeg2023_mastr_missing() {
        let facts = SettlementStateFacts {
            mastr_registriert: false,
            ..gesund()
        };
        assert_eq!(
            derive_settlement_state(&facts),
            SettlementPeriodState::Reduced
        );
    }

    #[test]
    fn derive_suspended_eeg2017_mastr_missing() {
        let facts = SettlementStateFacts {
            mastr_registriert: false,
            eeg_gesetz_year: 2017,
            ..gesund()
        };
        assert_eq!(
            derive_settlement_state(&facts),
            SettlementPeriodState::Suspended
        );
    }

    #[test]
    fn derive_reduced_when_sect9_is_not_satisfied_at_all() {
        let facts = SettlementStateFacts {
            sect9_erfuellung: Sect9Erfuellung::KEINE,
            ..gesund()
        };
        assert_eq!(
            derive_settlement_state(&facts),
            SettlementPeriodState::Reduced
        );
    }

    /// § 9 Abs. 2 Satz 1 Nr. 2 binds lit. a and lit. b together, so a geförderte
    /// Anlage in the 25-bis-100-kW band that carries only the 60 % cap is in
    /// breach — the § 52 Abs. 1 Nr. 1 Zahlung is 10 €/kW/Kalendermonat.
    #[test]
    fn the_sixty_percent_cap_alone_does_not_carry_a_50kw_plant() {
        let facts = SettlementStateFacts {
            sect9_erfuellung: Sect9Erfuellung::BEGRENZUNG_60,
            ..gesund()
        };
        assert_eq!(
            derive_settlement_state(&facts),
            SettlementPeriodState::Reduced
        );
    }

    /// The same plant in der Direktvermarktung owes lit. a alone, so the
    /// Fernsteuerbarkeit on its own keeps it `Active`.
    #[test]
    fn a_directly_marketed_50kw_plant_needs_only_the_remote_control() {
        let facts = SettlementStateFacts {
            sect9_anlage: Sect9Anlage {
                einspeiseverguetung_oder_mieterstrom: false,
                ..gesund().sect9_anlage
            },
            sect9_erfuellung: Sect9Erfuellung::FERNSTEUERBARKEIT,
            ..gesund()
        };
        assert_eq!(
            derive_settlement_state(&facts),
            SettlementPeriodState::Active
        );
    }

    /// From 100 kW the 60 % route is gone (§ 9 Abs. 2 Satz 1 Nr. 1).
    #[test]
    fn the_sixty_percent_cap_does_not_carry_a_100kw_plant() {
        let facts = SettlementStateFacts {
            sect9_anlage: mit_leistung(dec!(100)),
            sect9_erfuellung: Sect9Erfuellung::BEGRENZUNG_60,
            ..gesund()
        };
        assert_eq!(
            derive_settlement_state(&facts),
            SettlementPeriodState::Reduced
        );
    }

    #[test]
    fn derive_active_small_plant_on_the_cap() {
        let facts = SettlementStateFacts {
            sect9_anlage: mit_leistung(dec!(5)),
            sect9_erfuellung: Sect9Erfuellung::BEGRENZUNG_60,
            ..gesund()
        };
        assert_eq!(
            derive_settlement_state(&facts),
            SettlementPeriodState::Active
        );
    }
}

// ── §9 EEG — Steuerbarkeit ────────────────────────────────────────────────────

/// Which of the two § 9 Abs. 2 technical routes a plant has in place.
///
/// The two are **not** alternatives. § 9 Abs. 2 Satz 1 Nr. 2 joins them with
/// „und" — lit. a the ferngesteuerte Reduzierung, lit. b the 60-%-Begrenzung —
/// and only Nr. 3 stands alone. A plant can therefore carry both, one or
/// neither, which is a pair of facts and not a choice.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Sect9Erfuellung {
    /// Technische Einrichtung nach § 9 Abs. 2 Satz 1 Nr. 1 bzw. Nr. 2 lit. a:
    /// the Netzbetreiber can reduce the Einspeiseleistung remotely — and, in the
    /// Nr. 1 band, read the Ist-Einspeisung as well.
    pub fernsteuerbarkeit: bool,
    /// Maximale Wirkleistungseinspeisung am Verknüpfungspunkt auf 60 % der
    /// installierten Leistung begrenzt — § 9 Abs. 2 Satz 1 Nr. 2 lit. b bzw. Nr. 3.
    pub begrenzung_60: bool,
}

impl Sect9Erfuellung {
    /// Nothing installed. A breach wherever § 9 Abs. 2 requires anything.
    pub const KEINE: Self = Self {
        fernsteuerbarkeit: false,
        begrenzung_60: false,
    };
    /// The ferngesteuerte Reduzierung alone.
    pub const FERNSTEUERBARKEIT: Self = Self {
        fernsteuerbarkeit: true,
        begrenzung_60: false,
    };
    /// The 60 % cap alone.
    pub const BEGRENZUNG_60: Self = Self {
        fernsteuerbarkeit: false,
        begrenzung_60: true,
    };
    /// Both routes — what a geförderte Anlage in the 25-bis-100-kW band owes.
    pub const BEIDES: Self = Self {
        fernsteuerbarkeit: true,
        begrenzung_60: true,
    };
}

/// The plant facts § 9 Abs. 2 stages its obligation by.
///
/// Capacity alone does not decide it: lit. b of Nr. 2 and the whole of Nr. 3
/// bind only „Anlagen, die der Einspeisevergütung oder dem Mieterstromzuschlag
/// nach § 19 Absatz 1 Nummer 2 oder Nummer 3 zugeordnet sind" — and Nr. 3
/// additionally every KWK-Anlage of that size. A directly-marketed Solaranlage
/// below 25 kW owes nothing at all under § 9 Abs. 2.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Sect9Anlage {
    /// Installed capacity in kW — the Nr. 1 / Nr. 2 / Nr. 3 staging.
    pub leistung_kwp: Decimal,
    /// Technology, for the Steckersolar carve-out.
    pub erzeugungsart: Option<ErzeugungsArt>,
    /// The plant is settled under § 19 Abs. 1 Nr. 2 (Einspeisevergütung, in any
    /// of its Varianten) or Nr. 3 (Mieterstromzuschlag) — the gate on the 60 %
    /// cap in Nr. 2 lit. b and Nr. 3.
    pub einspeiseverguetung_oder_mieterstrom: bool,
    /// A KWK-Anlage. Nr. 3 names it beside the geförderten Anlagen, so a
    /// KWK-Anlage below 25 kW carries the cap whatever it is paid under.
    pub ist_kwk_anlage: bool,
    /// Wechselrichterleistung in Voltampere. The Steckersolar carve-out is a
    /// two-part test — „bis zu 2 Kilowatt **und** … bis zu 800 Voltampere" — so
    /// `None` leaves it closed rather than assuming the standard build.
    pub wechselrichterleistung_va: Option<Decimal>,
}

/// What § 9 Abs. 2 Satz 1 requires of this plant.
///
/// The same shape as [`Sect9Erfuellung`], so a breach is the pointwise
/// comparison of the two and no band has to be re-derived to report one.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Sect9Pflicht {
    /// Nr. 1 (ab 100 kW) resp. Nr. 2 lit. a (ab 25 kW) — the ferngesteuerte
    /// Reduzierung.
    pub fernsteuerbarkeit: bool,
    /// Nr. 2 lit. b resp. Nr. 3 — the 60 % Wirkleistungsbegrenzung.
    pub begrenzung_60: bool,
}

impl Sect9Pflicht {
    /// Whether § 9 Abs. 2 asks anything of this plant at all.
    #[must_use]
    pub const fn ist_leer(self) -> bool {
        !self.fernsteuerbarkeit && !self.begrenzung_60
    }
}

/// The installed capacity up to which a Steckersolargerät escapes Nr. 3.
pub const STECKERSOLAR_LEISTUNG_KW: Decimal = dec!(2);

/// The Wechselrichterleistung up to which a Steckersolargerät escapes Nr. 3.
pub const STECKERSOLAR_WECHSELRICHTER_VA: Decimal = dec!(800);

/// Which § 9 Abs. 2 Satz 1 obligations apply to a plant.
///
/// | Installed capacity | Fernsteuerbarkeit | 60 % cap | Basis |
/// |---|---|---|---|
/// | Steckersolargerät ≤ 2 kW und ≤ 800 VA | – | – | Abs. 2 Satz 4 |
/// | < 25 kW | – | nur geförderte Anlagen und KWK-Anlagen | Nr. 3 |
/// | 25 kW bis < 100 kW | ja | nur geförderte Anlagen | Nr. 2 lit. a + b |
/// | ab 100 kW | ja | – | Nr. 1 |
///
/// „ab 25 Kilowatt" and „mindestens 100 Kilowatt" are inclusive, „weniger als"
/// exclusive, so both boundaries fall into the higher band.
///
/// # Example
///
/// ```rust
/// use eeg_billing::settlement_state::{Sect9Anlage, Sect9Pflicht, sect9_pflicht};
/// use rust_decimal::dec;
///
/// let gefoerdert = |kwp| Sect9Anlage {
///     leistung_kwp: kwp,
///     erzeugungsart: None,
///     einspeiseverguetung_oder_mieterstrom: true,
///     ist_kwk_anlage: false,
///     wechselrichterleistung_va: None,
/// };
///
/// // 50 kW in der Einspeisevergütung: Nr. 2 lit. a **und** lit. b.
/// assert_eq!(
///     sect9_pflicht(gefoerdert(dec!(50))),
///     Sect9Pflicht { fernsteuerbarkeit: true, begrenzung_60: true },
/// );
///
/// // Dieselbe Anlage in der Direktvermarktung: lit. b entfällt.
/// let direkt = Sect9Anlage { einspeiseverguetung_oder_mieterstrom: false, ..gefoerdert(dec!(50)) };
/// assert_eq!(
///     sect9_pflicht(direkt),
///     Sect9Pflicht { fernsteuerbarkeit: true, begrenzung_60: false },
/// );
///
/// // 10 kW in der Direktvermarktung: § 9 Abs. 2 verlangt nichts.
/// let klein = Sect9Anlage { einspeiseverguetung_oder_mieterstrom: false, ..gefoerdert(dec!(10)) };
/// assert!(sect9_pflicht(klein).ist_leer());
/// ```
#[must_use]
pub fn sect9_pflicht(anlage: Sect9Anlage) -> Sect9Pflicht {
    let Sect9Anlage {
        leistung_kwp,
        erzeugungsart,
        einspeiseverguetung_oder_mieterstrom,
        ist_kwk_anlage,
        wechselrichterleistung_va,
    } = anlage;

    // Abs. 2 Satz 4 lifts **Satz 1 Nr. 3** — and only Nr. 3 — for a
    // Steckersolargerät „bis zu 2 Kilowatt und mit einer Wechselrichterleistung
    // von insgesamt bis zu 800 Voltampere". Both halves are conditions: an
    // unknown Wechselrichterleistung leaves the carve-out shut, because
    // claiming it would suppress a real § 52 Abs. 1 Nr. 1 charge.
    let steckersolar_ausgenommen = erzeugungsart == Some(ErzeugungsArt::SolarStecker)
        && leistung_kwp <= STECKERSOLAR_LEISTUNG_KW
        && wechselrichterleistung_va.is_some_and(|va| va <= STECKERSOLAR_WECHSELRICHTER_VA);

    // The 60 % cap is owed by geförderte Anlagen throughout, and below 25 kW by
    // every KWK-Anlage as well.
    let cap_adressat = einspeiseverguetung_oder_mieterstrom;

    if leistung_kwp >= dec!(100) {
        // Nr. 1 — „mindestens 100 Kilowatt": Ist-Einspeisung abrufen und
        // ferngesteuert reduzieren. No 60 % route, and no Vergütungsform gate.
        Sect9Pflicht {
            fernsteuerbarkeit: true,
            begrenzung_60: false,
        }
    } else if leistung_kwp >= dec!(25) {
        // Nr. 2 — „ab 25 Kilowatt und von weniger als 100 Kilowatt": lit. a for
        // every plant, lit. b additionally for a geförderte one.
        Sect9Pflicht {
            fernsteuerbarkeit: true,
            begrenzung_60: cap_adressat,
        }
    } else {
        // Nr. 3 — „weniger als 25 Kilowatt": the cap alone, and only for a
        // geförderte Anlage or a KWK-Anlage.
        Sect9Pflicht {
            fernsteuerbarkeit: false,
            begrenzung_60: (cap_adressat || ist_kwk_anlage) && !steckersolar_ausgenommen,
        }
    }
}

/// Whether the plant is in breach of § 9 Abs. 2 — the § 52 Abs. 1 Nr. 1 trigger.
///
/// A breach is any obligation [`sect9_pflicht`] states and
/// [`Sect9Erfuellung`] does not carry. Having *more* than is owed is never a
/// breach: a directly-marketed plant that limits itself to 60 % anyway is
/// compliant, and so is a small plant with a Fernsteuerbarkeit it did not have
/// to install.
///
/// # Example
///
/// ```rust
/// use eeg_billing::settlement_state::{Sect9Anlage, Sect9Erfuellung, sect9_verletzt};
/// use rust_decimal::dec;
///
/// let anlage = Sect9Anlage {
///     leistung_kwp: dec!(50),
///     erzeugungsart: None,
///     einspeiseverguetung_oder_mieterstrom: true,
///     ist_kwk_anlage: false,
///     wechselrichterleistung_va: None,
/// };
///
/// // Nr. 2 asks for both, so the 60 % cap on its own is a breach of lit. a.
/// assert!(sect9_verletzt(anlage, Sect9Erfuellung::BEGRENZUNG_60));
/// assert!(sect9_verletzt(anlage, Sect9Erfuellung::FERNSTEUERBARKEIT));
/// assert!(!sect9_verletzt(anlage, Sect9Erfuellung::BEIDES));
/// ```
#[must_use]
pub fn sect9_verletzt(anlage: Sect9Anlage, erfuellung: Sect9Erfuellung) -> bool {
    let pflicht = sect9_pflicht(anlage);
    (pflicht.fernsteuerbarkeit && !erfuellung.fernsteuerbarkeit)
        || (pflicht.begrenzung_60 && !erfuellung.begrenzung_60)
}

#[cfg(test)]
mod sect9_tests {
    use super::*;
    use rust_decimal::dec;

    fn anlage(kwp: Decimal) -> Sect9Anlage {
        Sect9Anlage {
            leistung_kwp: kwp,
            erzeugungsart: None,
            einspeiseverguetung_oder_mieterstrom: true,
            ist_kwk_anlage: false,
            wechselrichterleistung_va: None,
        }
    }

    /// § 9 Abs. 2 Satz 1 Nr. 2 is „a) … **und** b) …" — the trailing „oder"
    /// separates Nr. 2 from Nr. 3, it does not offer the two lit. as a choice.
    /// Reading it as an alternative lets a geförderte Anlage in the middle band
    /// escape lit. a, which is the whole point of the Nummer.
    #[test]
    fn the_middle_band_owes_both_routes() {
        let a = anlage(dec!(50));
        assert_eq!(
            sect9_pflicht(a),
            Sect9Pflicht {
                fernsteuerbarkeit: true,
                begrenzung_60: true
            }
        );
        assert!(sect9_verletzt(a, Sect9Erfuellung::FERNSTEUERBARKEIT));
        assert!(sect9_verletzt(a, Sect9Erfuellung::BEGRENZUNG_60));
        assert!(sect9_verletzt(a, Sect9Erfuellung::KEINE));
        assert!(!sect9_verletzt(a, Sect9Erfuellung::BEIDES));
    }

    /// lit. b binds „Anlagen, die der Einspeisevergütung oder dem
    /// Mieterstromzuschlag … zugeordnet sind" — a Marktprämien-Anlage owes
    /// lit. a alone.
    #[test]
    fn the_middle_band_in_direktvermarktung_owes_only_the_remote_control() {
        let a = Sect9Anlage {
            einspeiseverguetung_oder_mieterstrom: false,
            ..anlage(dec!(50))
        };
        assert_eq!(
            sect9_pflicht(a),
            Sect9Pflicht {
                fernsteuerbarkeit: true,
                begrenzung_60: false
            }
        );
        assert!(!sect9_verletzt(a, Sect9Erfuellung::FERNSTEUERBARKEIT));
        assert!(sect9_verletzt(a, Sect9Erfuellung::BEGRENZUNG_60));
    }

    /// Nr. 1 has no Vergütungsform gate and no 60 % route.
    #[test]
    fn from_100_kw_only_the_remote_control_satisfies_sect9() {
        for gefoerdert in [true, false] {
            let a = Sect9Anlage {
                einspeiseverguetung_oder_mieterstrom: gefoerdert,
                ..anlage(dec!(100))
            };
            assert_eq!(
                sect9_pflicht(a),
                Sect9Pflicht {
                    fernsteuerbarkeit: true,
                    begrenzung_60: false
                }
            );
            assert!(sect9_verletzt(a, Sect9Erfuellung::BEGRENZUNG_60));
            assert!(!sect9_verletzt(a, Sect9Erfuellung::FERNSTEUERBARKEIT));
        }
    }

    /// Nr. 3 names „Anlagen, die der Einspeisevergütung oder dem
    /// Mieterstromzuschlag … zugeordnet sind" and „KWK-Anlagen"; a
    /// direktvermarktende Solaranlage below 25 kW is in neither list.
    #[test]
    fn below_25_kw_the_cap_is_owed_only_by_the_addressees_of_nr_3() {
        assert_eq!(
            sect9_pflicht(anlage(dec!(10))),
            Sect9Pflicht {
                fernsteuerbarkeit: false,
                begrenzung_60: true
            }
        );
        assert!(sect9_verletzt(anlage(dec!(10)), Sect9Erfuellung::KEINE));

        let direkt = Sect9Anlage {
            einspeiseverguetung_oder_mieterstrom: false,
            ..anlage(dec!(10))
        };
        assert!(sect9_pflicht(direkt).ist_leer());
        assert!(!sect9_verletzt(direkt, Sect9Erfuellung::KEINE));

        let kwk = Sect9Anlage {
            ist_kwk_anlage: true,
            ..direkt
        };
        assert!(sect9_verletzt(kwk, Sect9Erfuellung::KEINE));
        assert!(!sect9_verletzt(kwk, Sect9Erfuellung::BEGRENZUNG_60));
    }

    /// Abs. 2 Satz 4 — „bis zu 2 Kilowatt **und** … bis zu 800 Voltampere". Both
    /// halves are conditions, and an unstated Wechselrichterleistung keeps the
    /// carve-out shut.
    #[test]
    fn the_steckersolar_carve_out_is_a_two_part_test() {
        let stecker = |kwp, va| Sect9Anlage {
            erzeugungsart: Some(ErzeugungsArt::SolarStecker),
            wechselrichterleistung_va: va,
            ..anlage(kwp)
        };
        for kwp in [dec!(0.8), dec!(2)] {
            assert!(
                sect9_pflicht(stecker(kwp, Some(dec!(800)))).ist_leer(),
                "{kwp} kWp"
            );
        }
        // Above either limit the exemption stops and Nr. 3 applies again.
        assert!(!sect9_pflicht(stecker(dec!(2.01), Some(dec!(800)))).ist_leer());
        assert!(!sect9_pflicht(stecker(dec!(2), Some(dec!(1200)))).ist_leer());
        // Unknown inverter rating: no carve-out.
        assert!(!sect9_pflicht(stecker(dec!(2), None)).ist_leer());
    }

    /// The carve-out is for Steckersolargeräte only — a 2 kWp roof array is not one.
    #[test]
    fn the_carve_out_does_not_reach_an_ordinary_small_plant() {
        assert!(!sect9_pflicht(anlage(dec!(2))).ist_leer());
    }

    /// More than is owed is never a breach.
    #[test]
    fn exceeding_the_obligation_is_compliant() {
        assert!(!sect9_verletzt(anlage(dec!(10)), Sect9Erfuellung::BEIDES));
        assert!(!sect9_verletzt(anlage(dec!(120)), Sect9Erfuellung::BEIDES));
    }
}