mako-mabis 0.20.0

MABIS process engine for German electricity market balance group accounting (Marktprozesse Bilanzkreisverantwortlicher)
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
//! The MaBiS Fristenkalender — BK6-24-174 Anlage 3 Kap. 3.10, Tabelle 2.
//!
//! # There is no per-message Prüfmitteilung deadline
//!
//! This module exists because the obvious model is wrong. A Summenzeitreihe
//! arrives and a Prüfmitteilung goes back, so it is tempting to hang a
//! response Frist off the arrival the way GPKE and WiM do. MaBiS does not work
//! that way, and the Festlegung says so twice:
//!
//! - **Kap. 9.8.2 Nr. 1** — „Prüfmitteilung BG-SZR (Kategorie B)", Frist **„–"**.
//!   The NB *may* („kann") answer positively or negatively. Every other
//!   Prüfmitteilung use case in the document carries the same empty Frist cell.
//! - **Kap. 13.8.2** — the only other section a Prüfmitteilungs-Frist could come
//!   from — defines no BKV answer at all. Its two rows are the **BIKO's own**
//!   dispatch Fristen
//!   (18. WT vorläufig / 42. WT endgültig) and „Abrechnungssummenzeitreihe
//!   fehlerhaft — im Bedarfsfall", which has no Frist either.
//!
//! What actually bounds a Prüfmitteilung is the **clearing window** of Tabelle 2:
//! once it closes, no further version and no further Prüfmitteilung can change
//! the settlement. That window is a date range anchored on the end of the
//! Bilanzierungsmonat, not a countdown from an arrival instant, and it differs
//! between the BG-SZR and the BK-SZR by two Werktage at each end.
//!
//! The two places a **1 Werktag** Frist genuinely appears are both obligations
//! of the **BIKO**, not of the answering party:
//!
//! | Obligation | Frist | Source |
//! |---|---|---|
//! | BIKO forwards a Prüfmitteilung to the responsible party | „Spätestens am folgenden WT" | Kap. 9.8.2 Nr. 3 |
//! | BIKO sends the Datenstatus | „Spätestens am folgenden WT" | Kap. 9.9.2 Nr. 1 |
//!
//! [`BIKO_WEITERLEITUNG_WERKTAGE`] and [`BIKO_DATENSTATUS_WERKTAGE`] carry them.
//!
//! # Tabelle 2
//!
//! Every Frist below is anchored on the **end of the Bilanzierungsmonat** and
//! counted in Werktage per the GPKE Werktagsdefinition (Kap. 3.1). „Sie beziehen
//! sich auf den Zeitpunkt des **Eingangs** einer Meldung beim BIKO" — the clock
//! measures arrival at the BIKO, not dispatch.
//!
//! | Zeitreihe | BKA Erstaufschlag | BKA Clearing | KBKA |
//! |---|---|---|---|
//! | BG-SZR (Kategorie B) | 1.–10. WT | 11.–30. WT | 31. WT – Ende 7. Monat |
//! | BK-SZR (Kategorie A und B) | 1.–12. WT | 13.–30. WT | 31. WT – Ende 7. Monat |
//! | DZÜ | — | 31.–34. WT | 1.–8. WT des 8. Monats |
//!
//! # Kapitel 17 has a second Fristentabelle
//!
//! The Redispatch-Ausfallarbeit series do **not** appear in Tabelle 2 — they have
//! their own table in Kap. 17.3.1.3, with the BK-SZR windows and one row that has
//! no analogue anywhere else:
//!
//! | Zeitreihe | BKA Erstaufschlag | BKA Clearing | KBKA |
//! |---|---|---|---|
//! | monatliche AAÜZ · LF-AASZR | 1.–12. WT | 13.–30. WT | 31. WT – Ende 7. Monat |
//! | **tägliche AAÜZ** | **Folgetag (täglich)** | — | — |
//!
//! The tägliche AAÜZ is the only MaBiS series with a *daily* Frist: it is due the
//! day after the Liefertag, and it has no Clearingphase because Kap. 17.2 is
//! Bilanzkreismonitoring rather than settlement. It is also the series that
//! disappears on 30.09.2026 ([`crate::zeitreihen::KAPITEL_17_2_ENDE`]).
//!
//! | Abrechnungsstichtag | BKA | KBKA |
//! |---|---|---|
//! | Vorläufige Bilanzierung | 18. WT, Datenstand 15. WT | 8. WT des 5. Monats, Datenstand Ende 4. Monat |
//! | Abrechnungsrelevante Bilanzierung | 42. WT, Datenstand 30. WT | Ende 8. Monat, Datenstand Ende 7. Monat |
//!
//! # Why the Erstaufschlag window is load-bearing
//!
//! Kap. 3.8.3: a version that reaches the BIKO **inside** the Erstaufschlag
//! window is assigned „Abrechnungsdaten" automatically. One that arrives after
//! it gets „Prüfdaten" and only a **positive** Prüfmitteilung promotes it. Filing
//! a day late therefore does not merely miss a deadline — it changes the
//! settlement path, and silently, because the message is accepted either way.
//! [`Bilanzierungsmonat::phase`] is what tells the two apart.

use time::Date;

use crate::zeitreihen::{Familie, Kategorie, Zeitreihe};

/// Werktag calendar MaBiS counts in — the GPKE definition (Kap. 3.1).
pub const KALENDER: mako_fristen::HolidayCalendar = mako_fristen::HolidayCalendar::BdewMaKo;

// ── The two genuine 1-Werktag obligations ───────────────────────────────────

/// The BIKO forwards a received Prüfmitteilung „spätestens am folgenden WT"
/// (Kap. 9.8.2 Nr. 3). An **abgewiesene** Prüfmitteilung is not forwarded at all
/// (Nr. 2), so this Frist only starts once the Abweisung check has passed.
pub const BIKO_WEITERLEITUNG_WERKTAGE: u32 = 1;

/// The BIKO sends the Datenstatus „spätestens am folgenden WT" (Kap. 9.9.2
/// Nr. 1), and sends it „unabhängig davon, ob er sich geändert hat oder nicht".
pub const BIKO_DATENSTATUS_WERKTAGE: u32 = 1;

// No deadline label accompanies the two 1-Werktag Fristen above: they are the
// **BIKO's** obligations, and mako does not play BIKO. The constants are here
// because the numbers matter for reasoning about what the counterparty owes —
// a Datenstatus that has not arrived a Werktag after a Prüfmitteilung is late,
// and that is worth knowing — but registering a deadline for an obligation this
// participant does not hold would fire into a workflow with no arm to answer it.

/// Deadline label for the close of a clearing window — the point after which a
/// Summenzeitreihe version can no longer change the settlement.
pub const CLEARING_ENDE_LABEL: &str = "mabis-clearingfenster-ende";

// ── Abrechnungslauf ─────────────────────────────────────────────────────────

/// Which settlement run a Frist belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Abrechnungslauf {
    /// Bilanzkreisabrechnung ohne Korrektur — the ordinary monthly run.
    Bka,
    /// Korrekturbilanzkreisabrechnung — the later correction run.
    Kbka,
}

// ── Phase ───────────────────────────────────────────────────────────────────

/// Where a date sits in the settlement lifecycle of one Bilanzierungsmonat.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Phase {
    /// Before the Bilanzierungsmonat has ended — nothing is due yet.
    Vorlaufend,
    /// Erstaufschlag: a version arriving here is assigned „Abrechnungsdaten"
    /// automatically (Kap. 3.8.3).
    Erstaufschlag,
    /// BKA clearing: a new version arrives as „Prüfdaten" and needs a positive
    /// Prüfmitteilung to be promoted.
    Clearing,
    /// Between the close of the BKA clearing window and the start of the KBKA.
    ZwischenLaeufen,
    /// KBKA clearing.
    Kbka,
    /// Both windows have closed; no version can still change the settlement.
    Geschlossen,
}

impl Phase {
    /// Whether a version arriving in this phase is assigned „Abrechnungsdaten"
    /// automatically under the Erstaufschlagsrecht (Kap. 3.8.3).
    #[must_use]
    pub fn ist_erstaufschlag(self) -> bool {
        self == Self::Erstaufschlag
    }

    /// Whether a new version may still be filed at all.
    #[must_use]
    pub fn nimmt_versionen_an(self) -> bool {
        matches!(self, Self::Erstaufschlag | Self::Clearing | Self::Kbka)
    }
}

// ── Fenster ─────────────────────────────────────────────────────────────────

/// A closed date window `[von, bis]`, both bounds inclusive.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Fenster {
    /// First day of the window.
    pub von: Date,
    /// Last day of the window.
    pub bis: Date,
}

impl Fenster {
    /// Whether `date` falls inside the window.
    #[must_use]
    pub fn enthaelt(self, date: Date) -> bool {
        self.von <= date && date <= self.bis
    }
}

// ── Bilanzierungsmonat ──────────────────────────────────────────────────────

/// One settlement month, and every Frist Tabelle 2 hangs off it.
///
/// Construct from the **last day** of the Bilanzierungsmonat; all Werktag counts
/// start there, so the *n*-th Werktag is `n` Werktage after that day.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Bilanzierungsmonat {
    /// Last calendar day of the Bilanzierungsmonat.
    monatsende: Date,
}

impl Bilanzierungsmonat {
    /// Build from the last calendar day of the month.
    #[must_use]
    pub fn new(monatsende: Date) -> Self {
        Self { monatsende }
    }

    /// Build from any day inside the month.
    ///
    /// # Panics
    ///
    /// Never for a date the `time` crate can represent — the month length is
    /// looked up from the calendar.
    #[must_use]
    pub fn enthaltend(tag: Date) -> Self {
        let letzter = time::util::days_in_month(tag.month(), tag.year());
        Self::new(
            Date::from_calendar_date(tag.year(), tag.month(), letzter)
                .expect("last day of a real month"),
        )
    }

    /// The last calendar day of the Bilanzierungsmonat.
    #[must_use]
    pub fn monatsende(self) -> Date {
        self.monatsende
    }

    /// The *n*-th Werktag after the Bilanzierungsmonat.
    #[must_use]
    pub fn werktag(self, n: u32) -> Date {
        mako_fristen::add_werktage(self.monatsende, n, KALENDER)
    }

    /// The last calendar day of the *n*-th month after the Bilanzierungsmonat.
    ///
    /// Tabelle 2 states the KBKA bounds as „Ende 7. Monat" / „Ende 8. Monat",
    /// counted from the Bilanzierungsmonat itself: month 1 is the following
    /// month, so „Ende 7. Monat" is the end of the seventh month after it.
    ///
    /// # Panics
    ///
    /// Never for a Bilanzierungsmonat in the representable calendar range.
    #[must_use]
    pub fn monatsende_nach(self, n: u32) -> Date {
        let mut d = self.monatsende;
        for _ in 0..n {
            // `replace_day(1)` first so a 31-day month does not overflow into
            // the month after next when the following month is shorter.
            d = d
                .replace_day(1)
                .expect("day 1 is valid in every month")
                .checked_add(time::Duration::days(32))
                .expect("date overflow")
                .replace_day(1)
                .expect("day 1 is valid in every month");
            let letzter = time::util::days_in_month(d.month(), d.year());
            d = d.replace_day(letzter).expect("last day of a real month");
        }
        d
    }

    /// The *n*-th Werktag of the *m*-th month after the Bilanzierungsmonat.
    ///
    /// Used for the KBKA DZÜ window, which Tabelle 2 states as „1. WT des
    /// 8. Monats – 8. WT des 8. Monats".
    #[must_use]
    pub fn werktag_im_monat(self, monat: u32, n: u32) -> Date {
        let vormonatsende = self.monatsende_nach(monat.saturating_sub(1));
        mako_fristen::add_werktage(vormonatsende, n, KALENDER)
    }

    // ── Datenlieferungsfristen (Tabelle 2, upper block) ─────────────────────

    /// The Erstaufschlag window for `zeitreihe`, or `None` where Tabelle 2
    /// defines none.
    ///
    /// Only the BKA has an Erstaufschlag; the KBKA column reads „./." for both
    /// Summenzeitreihen rows, and the DZÜ has no Erstaufschlag in either run.
    #[must_use]
    pub fn erstaufschlag(self, zeitreihe: Zeitreihe, lauf: Abrechnungslauf) -> Option<Fenster> {
        if lauf != Abrechnungslauf::Bka {
            return None;
        }
        let bis = match tabellenzeile(zeitreihe)? {
            Zeile::BgSzr => 10,
            Zeile::BkSzr => 12,
            Zeile::Dzue => return None,
            // „Folgetag (täglich)" — the whole obligation is one day wide, and
            // there is no later phase to fall into.
            Zeile::TaeglicheAauez => {
                return Some(Fenster {
                    von: self.monatsende.next_day()?,
                    bis: self.monatsende.next_day()?,
                });
            }
        };
        Some(Fenster {
            von: self.werktag(1),
            bis: self.werktag(bis),
        })
    }

    /// The clearing window for `zeitreihe` in `lauf`, or `None` where Tabelle 2
    /// defines none.
    #[must_use]
    pub fn clearing(self, zeitreihe: Zeitreihe, lauf: Abrechnungslauf) -> Option<Fenster> {
        let zeile = tabellenzeile(zeitreihe)?;
        Some(match (zeile, lauf) {
            (Zeile::BgSzr, Abrechnungslauf::Bka) => Fenster {
                von: self.werktag(11),
                bis: self.werktag(30),
            },
            (Zeile::BkSzr, Abrechnungslauf::Bka) => Fenster {
                von: self.werktag(13),
                bis: self.werktag(30),
            },
            (Zeile::Dzue, Abrechnungslauf::Bka) => Fenster {
                von: self.werktag(31),
                bis: self.werktag(34),
            },
            (Zeile::BgSzr | Zeile::BkSzr, Abrechnungslauf::Kbka) => Fenster {
                von: self.werktag(31),
                bis: self.monatsende_nach(7),
            },
            (Zeile::Dzue, Abrechnungslauf::Kbka) => Fenster {
                von: self.werktag_im_monat(8, 1),
                bis: self.werktag_im_monat(8, 8),
            },
            // Kap. 17.3.1.3 gives the tägliche AAÜZ no Clearingphase: Kap. 17.2
            // is Bilanzkreismonitoring, one direction only, and it carries
            // neither Prüfmitteilung nor Datenstatus.
            (Zeile::TaeglicheAauez, _) => return None,
        })
    }

    // ── Abrechnungsstichtage (Tabelle 2, lower block) ───────────────────────

    /// Vorläufige Bilanzierung — the day by which the BIKO must have dispatched
    /// the preliminary Abrechnungssummenzeitreihen, and the Datenstand it is
    /// computed on (Kap. 13.8.2 Nr. 1).
    #[must_use]
    pub fn vorlaeufige_bilanzierung(self, lauf: Abrechnungslauf) -> Stichtag {
        match lauf {
            Abrechnungslauf::Bka => Stichtag {
                faellig: self.werktag(18),
                datenstand: self.werktag(15),
            },
            Abrechnungslauf::Kbka => Stichtag {
                faellig: self.werktag_im_monat(5, 8),
                datenstand: self.monatsende_nach(4),
            },
        }
    }

    /// Abrechnungsrelevante Bilanzierung — the day the settled versions receive
    /// the Datenstatus „abgerechnete Daten" bzw. „abgerechnete Daten KBKA".
    #[must_use]
    pub fn abrechnungsrelevante_bilanzierung(self, lauf: Abrechnungslauf) -> Stichtag {
        match lauf {
            Abrechnungslauf::Bka => Stichtag {
                faellig: self.werktag(42),
                datenstand: self.werktag(30),
            },
            Abrechnungslauf::Kbka => Stichtag {
                faellig: self.monatsende_nach(8),
                datenstand: self.monatsende_nach(7),
            },
        }
    }

    // ── Phase ───────────────────────────────────────────────────────────────

    /// Where `date` sits in the lifecycle of this month for `zeitreihe`.
    ///
    /// The answer drives the Datenstatus a newly filed version receives
    /// (Kap. 3.8.3), so it must come from the calendar rather than from a flag
    /// on the message.
    ///
    /// # The phases partition the filing period
    ///
    /// Tabelle 2 states every window in Werktage — the BG-SZR Erstaufschlag runs
    /// 1.–10. WT and its clearing 11.–30., the BK-SZR clearing closes on the 30.
    /// and the KBKA opens on the 31. Read as raw calendar dates those windows do
    /// not touch: the weekend and Feiertage between two adjacent Werktage sit
    /// inside neither, and a filing arriving then would be answered „the
    /// settlement is closed" a fortnight after the Bilanzierungsmonat ended.
    ///
    /// A window whose first day is the Werktag after the previous window's last
    /// one therefore **opens on the calendar day after that last one**, absorbing
    /// the non-Werktage between them. The later window takes them, not the
    /// earlier: the Erstaufschlagsrecht is what the boundary decides, and a
    /// version that arrives once the 10. WT has passed is „Prüfdaten" a positive
    /// Prüfmitteilung can still promote, where the opposite error settles a
    /// figure nobody checked.
    ///
    /// Two windows that are *not* adjacent Werktage keep their stated bounds, so
    /// the DZÜ — BKA clearing to the 34. WT, KBKA from the 1. WT of the 8. Monat
    /// — keeps the genuine [`Phase::ZwischenLaeufen`] gap Tabelle 2 gives it.
    #[must_use]
    pub fn phase(self, zeitreihe: Zeitreihe, date: Date) -> Phase {
        if date <= self.monatsende {
            return Phase::Vorlaufend;
        }

        let erstaufschlag = self.erstaufschlag(zeitreihe, Abrechnungslauf::Bka);
        let bka = self.clearing(zeitreihe, Abrechnungslauf::Bka);
        let kbka = self.clearing(zeitreihe, Abrechnungslauf::Kbka);

        // The Erstaufschlag opens on the 1. WT, which is by construction the
        // Werktag after the Monatsende — so it absorbs a Monatsende falling on a
        // Friday and the weekend behind it.
        if erstaufschlag.is_some_and(|f| ab_dem_vortag(f, Some(self.monatsende)).enthaelt(date)) {
            return Phase::Erstaufschlag;
        }
        if bka.is_some_and(|f| ab_dem_vortag(f, erstaufschlag.map(|e| e.bis)).enthaelt(date)) {
            return Phase::Clearing;
        }
        if kbka.is_some_and(|f| ab_dem_vortag(f, bka.map(|c| c.bis)).enthaelt(date)) {
            return Phase::Kbka;
        }
        match (bka.map(|f| f.bis), kbka.map(|f| f.von)) {
            (Some(ende), Some(start)) if date > ende && date < start => Phase::ZwischenLaeufen,
            _ => Phase::Geschlossen,
        }
    }
}

/// `fenster`, opened back over the non-Werktage that separate it from the
/// window closing on `vorheriges_ende`.
///
/// Only applies where the two are **adjacent Werktage**, which is how Tabelle 2
/// says „and the next phase begins": 10. WT then 11. WT. Where the table leaves
/// a real gap — the DZÜ between the 34. WT and the 8. Monat — nothing is
/// absorbed and the gap stays a gap.
fn ab_dem_vortag(fenster: Fenster, vorheriges_ende: Option<Date>) -> Fenster {
    match vorheriges_ende {
        Some(ende)
            if fenster.von == mako_fristen::add_werktage(ende, 1, KALENDER)
                && ende < fenster.von =>
        {
            Fenster {
                von: ende.next_day().unwrap_or(fenster.von),
                bis: fenster.bis,
            }
        }
        _ => fenster,
    }
}

/// A settlement milestone: the day it is due and the data cut-off it uses.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Stichtag {
    /// Latest day the dispatch may happen.
    pub faellig: Date,
    /// Datenstand the figures are computed on.
    pub datenstand: Date,
}

// ── Tabelle-2 rows ──────────────────────────────────────────────────────────

/// The Datenlieferungs rows of the two Fristentabellen.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Zeile {
    /// Tabelle 2: „Erstaufschlag der abrechnungsrelevanten BG-SZR (Kategorie B)".
    BgSzr,
    /// Tabelle 2: „Erstaufschlag der abrechnungsrelevanten BK-SZR (Kategorie A
    /// und Kategorie B)". Kap. 17.3.1.3 gives the monatliche AAÜZ and the
    /// LF-AASZR the same windows.
    BkSzr,
    /// Tabelle 2: „Clearingphase für DZÜ".
    Dzue,
    /// Kap. 17.3.1.3: the tägliche AAÜZ, due „Folgetag (täglich)".
    TaeglicheAauez,
}

/// Which Fristentabellen row governs `zeitreihe`, or `None` if it has none.
///
/// Tabelle 2 names only the **abrechnungsrelevanten** monthly series, and
/// Kap. 17.3.1.3 adds the three Ausfallarbeit ones. A Kategorie-C series is
/// daily and settles nothing (Tabelle 1), the LF-SZR is never
/// settlement-relevant, the NZR's Abstimmung rides the BG-SZR row, and the
/// Abrechnungssummenzeitreihe is what the BIKO *produces* at the
/// Abrechnungsstichtage rather than something filed into a window.
fn tabellenzeile(zeitreihe: Zeitreihe) -> Option<Zeile> {
    match (zeitreihe.familie(), zeitreihe.kategorie()) {
        // „Abstimmung und Übermittlung der NZR" shares the 1.–10. WT cell with
        // the BG-SZR (Kategorie B) Erstaufschlag.
        (Familie::Nzr, _) => Some(Zeile::BgSzr),
        (Familie::BgSzr, Some(Kategorie::B)) => Some(Zeile::BgSzr),
        (Familie::BkSzr, Some(Kategorie::A | Kategorie::B)) => Some(Zeile::BkSzr),
        (Familie::Dzue, _) => Some(Zeile::Dzue),
        // Kap. 17.3.1.3 puts the monatliche AAÜZ and the LF-AASZR on the
        // BK-SZR windows.
        (Familie::Aauez | Familie::LfAaszr, _) => Some(Zeile::BkSzr),
        (Familie::TaeglicheAauez, _) => Some(Zeile::TaeglicheAauez),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::zeitreihen::Familie;
    use time::Month;

    fn d(y: i32, m: Month, day: u8) -> Date {
        Date::from_calendar_date(y, m, day).expect("valid date")
    }

    fn bg() -> Zeitreihe {
        Zeitreihe::new(Familie::BgSzr, Some(Kategorie::B)).unwrap()
    }

    fn bk() -> Zeitreihe {
        Zeitreihe::new(Familie::BkSzr, Some(Kategorie::B)).unwrap()
    }

    fn dzue() -> Zeitreihe {
        Zeitreihe::new(Familie::Dzue, None).unwrap()
    }

    #[test]
    fn enthaltend_finds_the_month_end() {
        assert_eq!(
            Bilanzierungsmonat::enthaltend(d(2026, Month::February, 9)).monatsende(),
            d(2026, Month::February, 28)
        );
        // Leap year.
        assert_eq!(
            Bilanzierungsmonat::enthaltend(d(2028, Month::February, 1)).monatsende(),
            d(2028, Month::February, 29)
        );
    }

    #[test]
    fn bg_and_bk_erstaufschlag_windows_differ_by_two_werktage() {
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::September, 15));
        let gebiet = m.erstaufschlag(bg(), Abrechnungslauf::Bka).unwrap();
        let kreis = m.erstaufschlag(bk(), Abrechnungslauf::Bka).unwrap();
        assert_eq!(gebiet.von, kreis.von, "both open on the 1. WT");
        assert_eq!(gebiet.bis, m.werktag(10));
        assert_eq!(kreis.bis, m.werktag(12));
        assert_ne!(gebiet.bis, kreis.bis, "10. WT vs 12. WT");
    }

    #[test]
    fn clearing_starts_the_werktag_after_the_erstaufschlag_closes() {
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::September, 15));
        for z in [bg(), bk()] {
            let e = m.erstaufschlag(z, Abrechnungslauf::Bka).unwrap();
            let c = m.clearing(z, Abrechnungslauf::Bka).unwrap();
            assert_eq!(
                c.von,
                mako_fristen::add_werktage(e.bis, 1, KALENDER),
                "no gap and no overlap between the two windows for {z}"
            );
            assert_eq!(c.bis, m.werktag(30), "both close on the 30. WT");
        }
    }

    #[test]
    fn kbka_opens_on_the_31st_werktag_and_closes_at_the_end_of_month_seven() {
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::January, 10));
        let k = m.clearing(bg(), Abrechnungslauf::Kbka).unwrap();
        assert_eq!(k.von, m.werktag(31));
        assert_eq!(
            k.bis,
            d(2026, Month::August, 31),
            "Ende 7. Monat nach Januar"
        );
    }

    #[test]
    fn dzue_has_no_erstaufschlag_and_its_own_windows() {
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::January, 10));
        assert!(m.erstaufschlag(dzue(), Abrechnungslauf::Bka).is_none());
        let bka = m.clearing(dzue(), Abrechnungslauf::Bka).unwrap();
        assert_eq!(bka.von, m.werktag(31));
        assert_eq!(bka.bis, m.werktag(34));
        let kbka = m.clearing(dzue(), Abrechnungslauf::Kbka).unwrap();
        // „1. WT des 8. Monats – 8. WT des 8. Monats": month 8 after January is
        // September 2026, so the window opens on the first Werktag of September.
        assert_eq!(kbka.von, d(2026, Month::September, 1));
        assert_eq!(kbka.bis, d(2026, Month::September, 10));
    }

    #[test]
    fn abrechnungsstichtage_match_tabelle_2() {
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::January, 10));
        let vor = m.vorlaeufige_bilanzierung(Abrechnungslauf::Bka);
        assert_eq!(vor.faellig, m.werktag(18));
        assert_eq!(vor.datenstand, m.werktag(15));
        let end = m.abrechnungsrelevante_bilanzierung(Abrechnungslauf::Bka);
        assert_eq!(end.faellig, m.werktag(42));
        assert_eq!(end.datenstand, m.werktag(30));

        let kvor = m.vorlaeufige_bilanzierung(Abrechnungslauf::Kbka);
        assert_eq!(kvor.datenstand, d(2026, Month::May, 31), "Ende 4. Monat");
        let kend = m.abrechnungsrelevante_bilanzierung(Abrechnungslauf::Kbka);
        assert_eq!(kend.faellig, d(2026, Month::September, 30), "Ende 8. Monat");
        assert_eq!(kend.datenstand, d(2026, Month::August, 31), "Ende 7. Monat");
    }

    #[test]
    fn monatsende_nach_survives_short_months() {
        // From 31 January, one month on must be 28/29 February, not 3 March.
        let m = Bilanzierungsmonat::new(d(2026, Month::January, 31));
        assert_eq!(m.monatsende_nach(1), d(2026, Month::February, 28));
        assert_eq!(m.monatsende_nach(2), d(2026, Month::March, 31));
        let leap = Bilanzierungsmonat::new(d(2028, Month::January, 31));
        assert_eq!(leap.monatsende_nach(1), d(2028, Month::February, 29));
    }

    #[test]
    fn phase_separates_erstaufschlag_from_clearing() {
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::September, 15));
        assert_eq!(m.phase(bg(), m.monatsende()), Phase::Vorlaufend);
        assert_eq!(m.phase(bg(), m.werktag(1)), Phase::Erstaufschlag);
        assert_eq!(m.phase(bg(), m.werktag(10)), Phase::Erstaufschlag);
        assert_eq!(m.phase(bg(), m.werktag(11)), Phase::Clearing);
        // The 11. and 12. WT are still Erstaufschlag for the BK-SZR — the
        // two-Werktag offset is exactly what a single shared window would lose.
        assert_eq!(m.phase(bk(), m.werktag(11)), Phase::Erstaufschlag);
        assert_eq!(m.phase(bk(), m.werktag(12)), Phase::Erstaufschlag);
        assert_eq!(m.phase(bk(), m.werktag(13)), Phase::Clearing);
    }

    #[test]
    fn phase_closes_after_the_kbka() {
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::January, 10));
        assert_eq!(m.phase(bg(), m.werktag(31)), Phase::Kbka);
        assert_eq!(m.phase(bg(), d(2026, Month::August, 31)), Phase::Kbka);
        assert_eq!(
            m.phase(bg(), d(2026, Month::September, 1)),
            Phase::Geschlossen
        );
    }

    /// Every Tabelle-2 window is stated in Werktage, so the phases are
    /// contiguous in Werktagen — and a weekend or Feiertag between two adjacent
    /// Werktage must not fall out of both. Read on the raw calendar the
    /// BG-SZR Erstaufschlag closes on the 10. WT and its clearing opens on the
    /// 11., which leaves the Saturday and Sunday between them inside neither:
    /// a filing arriving then reaches the BIKO on the following Werktag, which
    /// is the window that measures it.
    #[test]
    fn a_non_werktag_between_two_windows_belongs_to_the_next_one() {
        for z in [bg(), bk()] {
            for monat in [
                Month::January,
                Month::April,
                Month::July,
                Month::September,
                Month::December,
            ] {
                let m = Bilanzierungsmonat::enthaltend(d(2026, monat, 15));
                let mut tag = m.monatsende().next_day().unwrap();
                let ende = m.clearing(z, Abrechnungslauf::Kbka).unwrap().bis;
                while tag <= ende {
                    assert!(
                        m.phase(z, tag).nimmt_versionen_an(),
                        "{z} {monat:?}: {tag} lies between the 1. WT and the close of \
                         the KBKA and must take a version, got {:?}",
                        m.phase(z, tag)
                    );
                    tag = tag.next_day().unwrap();
                }
            }
        }
    }

    /// The boundary itself: the non-Werktage after a window's last Werktag
    /// belong to the window that opens next, not to the one that closed.
    #[test]
    fn the_weekend_after_a_window_closes_belongs_to_the_next_phase() {
        // The BG-SZR Erstaufschlag closes on the 10. WT; pick a month whose
        // 10. WT is a Friday so a weekend separates it from the 11.
        for jahr in 2026..2031 {
            for monat in [Month::January, Month::May, Month::August] {
                let m = Bilanzierungsmonat::enthaltend(d(jahr, monat, 15));
                let zehnter = m.werktag(10);
                let elfter = m.werktag(11);
                if elfter == zehnter.next_day().unwrap() {
                    continue; // no non-Werktag between them
                }
                let dazwischen = zehnter.next_day().unwrap();
                assert_eq!(
                    m.phase(bg(), dazwischen),
                    Phase::Clearing,
                    "{jahr}-{monat:?}: {dazwischen} follows the 10. WT, so the \
                     Erstaufschlagsrecht has passed"
                );
                assert!(!m.phase(bg(), dazwischen).ist_erstaufschlag());
            }
        }
    }

    /// The tägliche AAÜZ is the one row Kap. 17.3.1.3 states in calendar days
    /// („Folgetag (täglich)"), so it must not roll onto the next Werktag.
    #[test]
    fn the_taegliche_aauez_stays_on_the_calendar_day() {
        let taeglich = Zeitreihe::new(Familie::TaeglicheAauez, None).unwrap();
        // October 2026 ends on a Saturday, so the Folgetag is a Sunday.
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::October, 15));
        let folgetag = m.monatsende().next_day().unwrap();
        assert_eq!(folgetag.weekday(), time::Weekday::Sunday);
        assert_eq!(m.phase(taeglich, folgetag), Phase::Erstaufschlag);
        assert_eq!(
            m.phase(taeglich, folgetag.next_day().unwrap()),
            Phase::Geschlossen,
            "the obligation is one day wide"
        );
    }

    #[test]
    fn dzue_has_a_gap_between_its_two_runs() {
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::January, 10));
        let after_bka = mako_fristen::add_werktage(m.werktag(34), 1, KALENDER);
        assert_eq!(m.phase(dzue(), after_bka), Phase::ZwischenLaeufen);
    }

    #[test]
    fn the_ausfallarbeit_series_have_their_own_table() {
        // Kap. 17.3.1.3, not Tabelle 2. The monatliche AAÜZ and the LF-AASZR
        // ride the BK-SZR windows; the tägliche AAÜZ is due the following day
        // and has no Clearingphase at all.
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::September, 15));
        for familie in [Familie::Aauez, Familie::LfAaszr] {
            let z = Zeitreihe::new(familie, None).unwrap();
            assert_eq!(
                m.erstaufschlag(z, Abrechnungslauf::Bka),
                m.erstaufschlag(bk(), Abrechnungslauf::Bka),
                "{z}"
            );
            assert_eq!(
                m.clearing(z, Abrechnungslauf::Bka),
                m.clearing(bk(), Abrechnungslauf::Bka),
                "{z}"
            );
        }

        let taeglich = Zeitreihe::new(Familie::TaeglicheAauez, None).unwrap();
        let fenster = m.erstaufschlag(taeglich, Abrechnungslauf::Bka).unwrap();
        assert_eq!(fenster.von, fenster.bis, "Folgetag is one day wide");
        assert_eq!(fenster.von, m.monatsende().next_day().unwrap());
        assert!(m.clearing(taeglich, Abrechnungslauf::Bka).is_none());
        assert!(m.clearing(taeglich, Abrechnungslauf::Kbka).is_none());
    }

    #[test]
    fn series_without_a_tabelle_2_row_are_always_closed() {
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::January, 10));
        for z in [
            Zeitreihe::new(Familie::BgSzr, Some(Kategorie::C)).unwrap(),
            Zeitreihe::new(Familie::LfSzr, Some(Kategorie::A)).unwrap(),
            Zeitreihe::new(Familie::Abrechnungssummenzeitreihe, None).unwrap(),
        ] {
            assert!(m.erstaufschlag(z, Abrechnungslauf::Bka).is_none(), "{z}");
            assert!(m.clearing(z, Abrechnungslauf::Bka).is_none(), "{z}");
            assert_eq!(m.phase(z, m.werktag(5)), Phase::Geschlossen, "{z}");
        }
    }

    #[test]
    fn nzr_shares_the_bg_szr_row() {
        let m = Bilanzierungsmonat::enthaltend(d(2026, Month::January, 10));
        let nzr = Zeitreihe::new(Familie::Nzr, None).unwrap();
        assert_eq!(
            m.erstaufschlag(nzr, Abrechnungslauf::Bka),
            m.erstaufschlag(bg(), Abrechnungslauf::Bka)
        );
    }

    #[test]
    fn phase_predicates_agree_with_the_variants() {
        assert!(Phase::Erstaufschlag.ist_erstaufschlag());
        assert!(!Phase::Clearing.ist_erstaufschlag());
        for p in [Phase::Erstaufschlag, Phase::Clearing, Phase::Kbka] {
            assert!(p.nimmt_versionen_an(), "{p:?}");
        }
        for p in [
            Phase::Vorlaufend,
            Phase::ZwischenLaeufen,
            Phase::Geschlossen,
        ] {
            assert!(!p.nimmt_versionen_an(), "{p:?}");
        }
    }
}