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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
//! Förderdauer (subsidy duration) calculation helpers.
//!
//! All functions are pure and deterministic — no I/O, no clock access.
//!
//! ## EEG Förderdauer rules
//!
//! | Condition | Rule | Legal basis |
//! |---|---|---|
//! | Standard commissioning | 20 years + rest of the commissioning year | §25 Abs. 1 EEG 2023 |
//! | Repowering (Vollrepowering) | a fresh Inbetriebnahme restarts §25 | §3 Nr. 30 i.V.m. §25 EEG 2023 |
//! | §24 Zusammenlegung | parent's `foerderendedatum` unchanged | §24 EEG 2023 |
//!
//! ## KWKG Förderdauer
//!
//! § 8 KWKG measures the Förderdauer in **Vollbenutzungsstunden** for every
//! plant, whatever its capacity, and caps what a single calendar year may be
//! paid for on top of that. Neither figure is a number of years, and § 8 has no
//! capacity threshold at all — [`crate::kwkg`] holds both. A KWK plant therefore
//! has no calendar Förderende: it stops when its kWh counter reaches
//! `kwk_max_kwh = leistung_kW_el × Vollbenutzungsstunden`.
use crateRoundMoney;
use Decimal;
use Date;
use ComponentRange;
/// Compute the `foerderendedatum` for a **statutory** (nicht-Ausschreibungs) EEG plant.
///
/// ## Legal basis — §25 Abs. 1 Satz 1+2 EEG 2023 (unchanged since EEG 2017)
///
/// > "Marktprämien, Einspeisevergütungen oder Mieterstromzuschläge sind jeweils für
/// > die Dauer von 20 Jahren zu zahlen. **Bei Anlagen, deren anzulegender Wert gesetzlich
/// > bestimmt wird, verlängert sich dieser Zeitraum bis zum 31. Dezember des zwanzigsten
/// > Jahres der Zahlung.**"
///
/// For **statutory** AW plants (no BNetzA tender), the 20-year period always extends
/// to **31 December of the 20th calendar year**.
///
/// | Inbetriebnahme | foerderendedatum |
/// |---|---|
/// | 2010-05-15 | **2030-12-31** (not 2030-05-15) |
/// | 2023-01-01 | 2043-12-31 |
/// | 2023-12-31 | 2043-12-31 |
///
/// For tender plants (Ausschreibungsanlagen) the period ends at the exact 20-year
/// anniversary — use [`foerderendedatum_eeg_ausschreibung`] instead.
///
/// ## Which plants are statutory?
///
/// Whichever ones §22 Abs. 2–5 EEG 2023 leaves out of the Ausschreibung — that
/// is the *only* thing that decides it. §21 Abs. 1 Satz 1's Nummern are the four
/// Varianten of the Einspeisevergütung, not a per-technology threshold table.
///
/// - Solar, erstes Segment (Freifläche) ≤ 1 MW · zweites Segment (Gebäude,
/// Lärmschutzwand) ≤ 750 kW — §22 Abs. 3 Satz 2
/// - Wind an Land ≤ 1 MW — §22 Abs. 2 Satz 2 Nr. 1
/// - Biomasse ≤ 150 kW — §22 Abs. 4 Satz 2
/// - Wasserkraft, Deponie-, Klär- und Grubengas, Geothermie at **any** size —
/// §22 Abs. 5 Satz 2 keeps their AW gesetzlich bestimmt
///
/// Plants with a BNetzA Zuschlag (`ausschreibungs_zuschlag_id` set) use tender rules.
///
/// # Example
/// ```rust
/// use eeg_billing::foerderendedatum_eeg;
/// use time::macros::date;
/// // Statutory plant commissioned May 2010 → ends 2030-12-31, NOT 2030-05-15
/// assert_eq!(
/// foerderendedatum_eeg(date!(2010-05-15)).unwrap(),
/// date!(2030-12-31)
/// );
/// // Plant commissioned Dec 31, 2023 → still ends 2043-12-31
/// assert_eq!(
/// foerderendedatum_eeg(date!(2023-12-31)).unwrap(),
/// date!(2043-12-31)
/// );
/// ```
/// Compute the `foerderendedatum` for a **tender** (Ausschreibungs) EEG plant.
///
/// ## Legal basis — §25 Abs. 1 Satz 1 EEG 2023
///
/// For plants whose AW was determined by BNetzA tender (`ausschreibungs_zuschlag_id` set),
/// Satz 2 does NOT apply — the period is exactly **20 years from `inbetriebnahme`**.
///
/// # Example
/// ```rust
/// use eeg_billing::foerderendedatum_eeg_ausschreibung;
/// use time::macros::date;
/// // Tender plant commissioned 2020-04-01 → exactly 20 years
/// assert_eq!(
/// foerderendedatum_eeg_ausschreibung(date!(2020-04-01)).unwrap(),
/// date!(2040-04-01)
/// );
/// // § 188 Abs. 3 BGB: a Frist that starts on 29 February ends on the 28th
/// // where the target month has no 29th — 2080 is a leap year, 2100 is not.
/// assert_eq!(
/// foerderendedatum_eeg_ausschreibung(date!(2080-02-29)).unwrap(),
/// date!(2100-02-28)
/// );
/// ```
/// The date `jahre` years after `von`, with § 188 Abs. 3 BGB applied.
///
/// „Fehlt bei einer nach Monaten bestimmten Frist in dem letzten Monat der für
/// ihren Ablauf maßgebende Tag, so endigt die Frist mit dem Ablauf des letzten
/// Tages dieses Monats." A period starting on 29 February therefore ends on
/// 28 February of a target year that has no 29th. `Date::replace_year` cannot
/// express that — it answers `Err` for the missing day — so the clamp is applied
/// here.
///
/// # Errors
/// [`ComponentRange`] when the target year leaves the representable range.
/// Compute the `foerderendedatum` after a repowering event.
///
/// # Legal basis
/// A repowered generator is a fresh **Inbetriebnahme** (§3 Nr. 30 EEG 2023 — the
/// first commissioning of the *generator* after its establishment or renewal), so
/// its 20-year Förderdauer under §25 restarts. The new `foerderendedatum` =
/// `repowering_datum + 20 years` (statutory: extends to Dec 31). (§22 EEG 2023
/// governs the Ausschreibungs-Anspruchsvoraussetzungen, not the Förderdauer reset.)
///
/// **Important**: This function is only correct for **Vollrepowering**
/// (complete replacement of the turbine unit, `RepoweringScope::Full` or
/// `RepoweringScope::FullWithCapacityIncrease`).
///
/// For partial repowering (rotor-only, nacelle replacement), the original
/// commissioning date continues to govern — use `foerderendedatum_eeg`
/// with the **original** commissioning date instead.
/// See [`crate::technology::RepoweringScope`] for the legal distinctions.
///
/// The original commissioning date must be preserved in `ursprungs_inbetriebnahme`
/// for audit-trail purposes.
///
/// # Example
/// ```rust
/// use eeg_billing::foerderendedatum_repowering;
/// use time::macros::date;
/// // Full repowering in 2025 resets to 2045-12-31:
/// assert_eq!(
/// foerderendedatum_repowering(date!(2025-03-01)).unwrap(),
/// date!(2045-12-31)
/// );
/// ```
/// The total kWh a KWK plant may be paid the Zuschlag for over its lifetime.
///
/// # Legal basis
/// § 8 Abs. 1–3 KWKG expresses the Förderdauer of every KWK plant in
/// Vollbenutzungsstunden — 30 000 for a new plant, and for a modernisierte or
/// nachgerüstete one a figure keyed on the share of the Neuerrichtungskosten the
/// work cost. Resolve it with
/// [`crate::kwkg::foerderdauer_vollbenutzungsstunden`]; the corresponding kWh
/// limit is
///
/// `kwk_max_kwh = leistung_kW_el × Vollbenutzungsstunden`
///
/// # Example
/// ```rust
/// use eeg_billing::kwk_max_kwh;
/// use rust_decimal::Decimal;
/// use std::str::FromStr;
/// // 2.5 MW plant, 30 000 full-load hours → 75 000 000 kWh cap
/// let limit = kwk_max_kwh(Decimal::from_str("2500").unwrap(), 30_000);
/// assert_eq!(limit, Decimal::from_str("75000000").unwrap());
/// ```
/// Determine how many kWh are eligible for KWK-Zuschlag this period,
/// enforcing a Vollbenutzungsstunden limit — the § 8 Abs. 1–3 lifetime figure or
/// the § 8 Abs. 4 cap on the calendar year, whichever the caller is applying.
///
/// Returns `(eligible_kwh, limit_reached)`.
///
/// - `eligible_kwh` ≤ `produced_kwh`; may be 0 when limit is already exhausted.
/// - `limit_reached` is `true` when this period exhausts or exceeds the cap,
/// triggering a status transition to `FoerderungBeendet`.
///
/// # Example
/// ```rust
/// use eeg_billing::kwk_eligible_kwh;
/// use rust_decimal::Decimal;
/// use std::str::FromStr;
///
/// let d = |s| Decimal::from_str(s).unwrap();
/// // 29 900 kWh paid, 30 000 kWh cap, 400 kWh this period → prorated to 100 kWh
/// let (eligible, done) = kwk_eligible_kwh(d("400"), d("29900"), d("30000"));
/// assert_eq!(eligible, d("100"));
/// assert!(done, "limit reached in this period");
/// ```
/// §51a EEG 2021/2023 — Vergütungszeitraum-Verlängerung (extension for lost periods).
///
/// When §51 reduces the Vergütung to zero, the Förderdauer is extended to compensate.
///
/// ## EEG version differences
///
/// | EEG version | Unit tracked | Plants covered | Solar factor |
/// |---|---|---|---|
/// | EEG 2021 §51a | **Stunden** (hours) | Ausschreibungsanlagen only | none |
/// | EEG 2023 §51a | **Viertelstunden** (QH) | ALL plants (§51a Abs. 1) | ×0.5 (§51a Abs. 2) |
///
/// This function works in **quarter-hours** (EEG 2023 unit).
/// For EEG 2021 Ausschreibungsanlagen (which track hours), multiply `lost_hours × 4`
/// before calling, and use `is_solar = false` (EEG 2021 §51a had no solar factor).
///
/// ## §51a EEG 2023 Abs. 2 — Solar PV Volllastviertelstunden
///
/// For solar PV, the extension QH are multiplied by **0.5** (halved), rounded up to
/// the next full QH. This accounts for the lower effective utilization of solar plants.
/// The result is called "Volllastviertelstunden". The extension months are then
/// determined from a statutory monthly QH table (January = 87 QH, etc.).
///
/// # Returns
/// Number of additional **quarter-hours** to add to the Förderdauer.
///
/// # Example
/// ```rust
/// use eeg_billing::verguetungszeitraum_verlaengerung_qh;
/// // 40 quarter-hours lost; solar PV → 20 quarter-hours added (×0.5, rounded up)
/// assert_eq!(verguetungszeitraum_verlaengerung_qh(40, true), 20);
/// // 41 quarter-hours; solar → ceiling(41×0.5) = 21
/// assert_eq!(verguetungszeitraum_verlaengerung_qh(41, true), 21);
/// // Wind/biomass/other: §51a Abs. 1 Satz 2 rounds the lost QH UP to the next
/// // full calendar day (96 QH) — 40 QH → 96 QH (one day), 100 QH → 192 QH (two).
/// assert_eq!(verguetungszeitraum_verlaengerung_qh(40, false), 96);
/// assert_eq!(verguetungszeitraum_verlaengerung_qh(100, false), 192);
/// assert_eq!(verguetungszeitraum_verlaengerung_qh(0, false), 0);
/// ```
/// §51a Abs. 2 EEG 2023 — the solar halving denominator.
///
/// For Solaranlagen the Vergütungszeitraum is extended by only **half** the lost
/// quarter-hours, rounded up; wind, biomass and the rest extend 1:1 (rounded up
/// to a full calendar day by Abs. 1 Satz 2). This is the only place the factor
/// appears.
pub const SECT51A_SOLAR_FACTOR_DENOMINATOR: u64 = 2;
/// §51a Abs. 2 Satz 3 EEG 2023 — Volllastviertelstunden per calendar month.
///
/// The statutory table. Index 0 is January.
///
/// A solar plant's extension is not a span of wall-clock time but a *contingent*
/// of Volllastviertelstunden, drawn down at a different rate in each month —
/// which is what makes a December extension take far longer than a June one.
const VOLLLASTVIERTELSTUNDEN_JE_MONAT: =
;
/// The Volllastviertelstunden a whole calendar month contributes (§51a Abs. 2 Satz 3).
///
/// # Panics
///
/// Does not panic: `time::Month` is always one of the twelve.
/// §51a Abs. 2 Satz 4–6 EEG 2023 — the date a solar plant's extended
/// Vergütungszeitraum ends.
///
/// The contingent from [`verguetungszeitraum_verlaengerung_qh`] is drawn down
/// month by month at the statutory rate until it is exhausted; the
/// Vergütungszeitraum then runs **to the end of the month** in which the last
/// Volllastviertelstunde falls (Satz 6).
///
/// The month in which the original period ends contributes only pro rata for its
/// remaining days (Satz 4): `verbleibende Tage / Tage des Monats × VLVh des Monats`.
///
/// Returns `original_ende` unchanged when the contingent is zero — no negative
/// prices means no extension.
///
/// ## Why this is not `original_ende + n days`
///
/// A contingent of 500 Volllastviertelstunden is roughly one month if it starts
/// in June (508) and roughly seven if it starts in December (73, 87, 189, …).
/// Converting the contingent to a fixed number of days would misstate the end of
/// the Förderung by months for exactly the plants that lost the most revenue.
///
/// # Errors
///
/// Returns [`ComponentRange`] if the computed end date falls outside the
/// representable range.
/// §51a EEG 2023 — the plant's **effective** Förderende after the accrued
/// negative-price extension is applied.
///
/// `negative_qh_gesamt` is the TOTAL raw quarter-hours in which the anzulegender
/// Wert was reduced to null under §51 across the commissioning year and the
/// following 19 calendar years (§51a Abs. 1 Satz 1) — summed over every monthly
/// settlement. Rounding is applied **once over the total**, so accruing raw QH
/// (not the per-month rounded extension) is essential: `ceil(40/96)+ceil(40/96)`
/// would extend by two days where the single 80-QH total extends by one.
///
/// - **Solar** (Abs. 2): the contingent `ceil(qh/2)` Volllastviertelstunden is
/// drawn down month by month via [`solar_verlaengerung_ende`].
/// - **Others** (Abs. 1 Satz 2): the total rounds up to whole calendar days
/// (96 QH per day) added to the original Förderende.
///
/// Returns `original_ende` unchanged when nothing accrued.
///
/// # Errors
/// [`ComponentRange`] if the extended date leaves the representable range.
// ── §52 EEG 2023 Pflichtzahlung ───────────────────────────────────────────────
/// Compute the §52 EEG 2023 penalty payment owed by the plant operator to the NB.
///
/// Returns the EUR amount for one violation over `monate_des_verstosses`.
///
/// ## The rate — §52 Abs. 2 and Abs. 3
///
/// The base rate is **€10/kW/month** for every violation in §52 Abs. 1, for each
/// calendar month the violation is wholly or partly in effect. Abs. 3 reduces it:
///
/// | Rule | Violation types | Rate |
/// |---|---|---|
/// | Abs. 3 Nr. 1 — retroactive on fulfilment | Nr. 1 (Fernsteuerbarkeit), Nr. 3 (iMSys), Nr. 4 (Direktverm.), Nr. 11 (MaStR) | €10 → **€2** retroactively |
/// | Abs. 3 Nr. 2 — always | Nr. 9a (§37 Abs. 1a / §48), Nr. 10 (Volleinspeisung) | **€2** |
/// | otherwise | Nr. 2, 5, 6, 7, 8, 9, 12 | **€10**, not reducible |
///
/// ## What the caller must supply
///
/// Three rules act on the *months*, not the rate, so they are the caller's to
/// fold into `monate_des_verstosses`:
///
/// - **Abs. 3 Satz 2 (from 01.01.2024)** — for Nr. 1, 3, 4, 8 caused by a
/// technical defect, the month of the defect and the following one are waived.
/// The operator carries the Darlegungs- und Beweislast, so exclude those months.
/// - **Abs. 4** — some violations run past the violation itself: Nr. 7
/// (§21b Abs. 2) +3 months, Nr. 9 (§21b/§21c notification) +1, Nr. 12 (§80) +6,
/// and Nr. 10 (Volleinspeisung) covers every calendar month of the year.
/// - **Abs. 5** — concurrent violations are capped at **€10/kW/month in total**.
/// This function prices one violation, so cap the sum outside it.
///
/// ## Bestandsschutz (§100 Abs. 3 EEG 2023)
///
/// A plant commissioned before 01.01.2023 keeps Bestandsschutz for the
/// Fernsteuerbarkeit obligation: legacy equipment (full shutdown only, no
/// modulation) counts as compliant until an iMSys is installed and tested, so
/// `FernsteuerbarkeitFehlend` does not apply to it.
///
/// # Example
///
/// ```rust
/// use eeg_billing::{Pflichtverstoss, SanktionsTyp};
/// use eeg_billing::foerderdauer::calculate_pflichtzahlung;
/// use rust_decimal::dec;
///
/// // 500 kW plant, MaStR not registered for 3 months, obligation NOT yet fulfilled
/// let violation = Pflichtverstoss {
/// typ: SanktionsTyp::MastrNichtRegistriert,
/// leistung_kw: dec!(500),
/// monate_des_verstosses: 3,
/// beginn: None,
/// nachtraeglich_erfuellt: false,
/// technischer_defekt: false,
/// };
/// let penalty = calculate_pflichtzahlung(&violation);
/// assert_eq!(penalty, dec!(15000)); // 500 kW × €10 × 3 months
///
/// // Same violation, but obligation was later fulfilled → retroactively €2/kW
/// let fulfilled = Pflichtverstoss { nachtraeglich_erfuellt: true, ..violation };
/// let reduced = calculate_pflichtzahlung(&fulfilled);
/// assert_eq!(reduced, dec!(3000)); // 500 kW × €2 × 3 months
///
/// // Nr. 9a / Nr. 10: ALWAYS €2/kW regardless of fulfillment
/// let nr10 = Pflichtverstoss {
/// typ: SanktionsTyp::VolleinspeisungspflichtVerletzt,
/// leistung_kw: dec!(500),
/// monate_des_verstosses: 12,
/// beginn: None,
/// nachtraeglich_erfuellt: false,
/// technischer_defekt: false, // has no effect for this type
/// };
/// assert_eq!(calculate_pflichtzahlung(&nr10), dec!(12000)); // 500 × €2 × 12
/// ```
/// §52 EEG 2023 — what one calendar month of this violation costs, and how many
/// months it is charged for.
///
/// §52 Abs. 5 caps „pro Kilowatt installierter Leistung der Anlage **und
/// Kalendermonat**", which needs the monthly amount rather than the total, so
/// the two are computed together and
/// [`calculate_pflichtzahlung`] multiplies them back out.
///
/// The months are the ones actually charged: Abs. 3 Satz 2's technical-defect
/// grace has already been taken off, and they run from the violation's
/// [`beginn`](crate::Pflichtverstoss::beginn).
///
/// ```rust
/// use eeg_billing::{Pflichtverstoss, SanktionsTyp};
/// use eeg_billing::foerderdauer::pflichtzahlung_je_monat;
/// use rust_decimal::dec;
///
/// // §52 Abs. 2: 500 kW × 10 EUR for each of three months.
/// let violation = Pflichtverstoss {
/// typ: SanktionsTyp::MastrNichtRegistriert,
/// leistung_kw: dec!(500),
/// monate_des_verstosses: 3,
/// beginn: None,
/// nachtraeglich_erfuellt: false,
/// technischer_defekt: false,
/// };
/// assert_eq!(pflichtzahlung_je_monat(&violation), (dec!(5000), 3));
/// ```
/// §36h EEG 2023 — Corrected Anzulegender Wert for wind onshore plants.
///
/// Multiplies the statutory base AW by the certified Korrekturfaktor to obtain
/// the effective AW for the current settlement period.
///
/// ## Legal basis
/// §36h EEG 2023: the AW for wind onshore is adjusted by a location-specific
/// Korrekturfaktor that reflects the ratio of local to reference yield.
/// Factors are certified by a BNetzA-accredited Gutachter.
///
/// ## Korrekturfaktor interpretation
///
/// | Gütegrad (local/reference yield) | Korrekturfaktor | Effect |
/// |---|---|---|
/// | ≥ 150 % | 0.70–0.84 | Lower AW (excellent wind site) |
/// | 100 % | 1.00 | No change (reference site) |
/// | 80 % | 1.10–1.15 | Higher AW (poor wind site) |
///
/// ## When to use
///
/// Supply the certified Korrekturfaktor for `SettleInput.wind_korrekturfaktor`
/// and the uncorrected statutory AW for `direktverm_aw_ct`. The engine applies
/// this function automatically.
///
/// OR: apply this function when storing the initial plant record in `einsd`
/// to pre-compute the corrected AW for the `direktverm_aw_ct` column.
///
/// ## Pre-2017 Bestandsschutz
/// §36h does not apply to EEG ≤2012 plants (§100 Abs. 1 EEG 2023). Do not
/// supply a Korrekturfaktor for these plants.
///
/// # Example
/// ```rust
/// use eeg_billing::foerderdauer::wind_onshore_korrekturfaktor_corrected_aw;
/// use rust_decimal::dec;
///
/// // Statutory base AW = 7.35 ct/kWh, Korrekturfaktor = 1.08 (low-wind site)
/// let corrected = wind_onshore_korrekturfaktor_corrected_aw(dec!(7.35), dec!(1.08));
/// // 7.35 × 1.08 = 7.938 (rounded to 5 decimal places)
/// assert_eq!(corrected, dec!(7.938));
/// ```
// ── §52a Netztrennung ─────────────────────────────────────────────────────────
/// §52a EEG 2023 — Check whether mandatory grid disconnection warning must be issued.
///
/// Under §52a Abs. 1 EEG 2023, the NB **must** disconnect the plant (or issue a
/// one-month warning first per §52a Abs. 2) when the operator has violated §9 Abs. 1/2
/// or §10b in **at least 6 out of the last 12 calendar months**.
///
/// ## Parameters
///
/// `violation_months_in_12_month_window` — number of distinct calendar months in the
/// last 12 months in which the plant was in violation of §9 Abs. 1/2 (Fernsteuerbarkeit)
/// or §10b (Direktvermarktungspflicht). The caller tracks this from the violation start
/// dates and the billing history.
///
/// ## Return value
///
/// `true` when `violation_months_in_12_month_window >= 6`.
///
/// ## Legal basis
///
/// §52a Abs. 1 EEG 2023:
/// *„Der Netzbetreiber... muss die Anlage... vom Netz trennen... wenn der Anlagenbetreiber
/// hinsichtlich dieser Anlage in einem Zeitraum von zwölf Monaten in insgesamt mindestens
/// sechs Monaten jeweils mindestens einmal gegen §9 Absatz 1 oder Absatz 2 oder gegen
/// §10b Absatz 1 verstoßen hat..."*
///
/// # Example
///
/// ```rust
/// use eeg_billing::foerderdauer::sect52a_netztrennung_erforderlich;
/// assert!(sect52a_netztrennung_erforderlich(6));
/// assert!(!sect52a_netztrennung_erforderlich(5));
/// ```
// ── §52 Abs. 6 Satz 3 Verjährung ─────────────────────────────────────────────
/// §52 Abs. 6 Satz 3 EEG 2023 — Compute the Verjährungsdatum for a §52 penalty claim.
///
/// The NB's claim for §52 Pflichtzahlung expires at the end of the **second calendar
/// year following the year of the violation**.
///
/// Legal basis: §52 Abs. 6 Satz 3 EEG 2023:
/// *„Der Anspruch auf die Zahlung verjährt mit Ablauf des zweiten Kalenderjahres,
/// das auf den Pflichtverstoß nach Absatz 1 folgt."*
///
/// | Violation year | Verjährungsdatum |
/// |---|---|
/// | 2023 | **2025-12-31** |
/// | 2024 | **2026-12-31** |
/// | 2025 | **2027-12-31** |
///
/// # Example
///
/// ```rust
/// use eeg_billing::foerderdauer::pflichtzahlung_verjaehrt_am;
/// use time::macros::date;
///
/// assert_eq!(pflichtzahlung_verjaehrt_am(2024).unwrap(), date!(2026-12-31));
/// assert_eq!(pflichtzahlung_verjaehrt_am(2023).unwrap(), date!(2025-12-31));
/// ```
// ── §25 billing_days_fraction ─────────────────────────────────────────────────
/// §25 Abs. 1 Satz 3 / §26 Abs. 1 EEG 2023 — the partial-month billing fraction
/// for the **Förderende** month.
///
/// Returns `Some(day / days_in_month)` when the Förderdauer ends part-way through
/// the billing month, `None` for full months (the common case).
///
/// ## Why the commissioning month is *not* prorated
///
/// Proration exists to cut a month's *entitlement* down to the eligible days. In
/// the Förderende month the meter keeps running past the entitlement end, so the
/// reading covers more than the plant may be paid for and must be narrowed. The
/// commissioning month is the opposite case: §25 Abs. 1 Satz 3 starts the period
/// at the Inbetriebnahme, and the meter only starts recording then — a plant
/// commissioned on 15 June reports the 500 kWh it fed in from 15 June, already
/// the partial month. Scaling that by 16/30 would bill a fraction of a fraction
/// and underpay the operator by roughly half of their first month.
///
/// ## Legal basis
///
/// §25 Abs. 1 Satz 3 EEG 2023: "Beginn der Frist… ist der Zeitpunkt der Inbetriebnahme."
/// §26 Abs. 1 EEG 2023: monthly advance payments for the billing month.
///
/// # Example
///
/// ```rust
/// use eeg_billing::foerderdauer::compute_billing_days_fraction;
/// use rust_decimal::dec;
/// use time::macros::date;
///
/// // Förderende on 20 June → 20/30 of the month is still entitled.
/// let fraction = compute_billing_days_fraction(Some(date!(2024-06-20)), Some(date!(2024-06-01)));
/// assert_eq!(fraction, Some(dec!(20) / dec!(30)));
///
/// // Full month → None.
/// assert!(compute_billing_days_fraction(None, Some(date!(2024-06-01))).is_none());
/// ```