klirr-core 0.2.1

Zero-maintenance and smart FOSS generating beautiful invoices for services and expenses.
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
use std::{borrow::Borrow, ops::Mul};

use crate::prelude::*;

impl Year {
    /// Checks if this year is a leap year, if it is, `true` is returned, else
    /// `false`
    pub fn is_leap(&self) -> bool {
        let year = **self;
        (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
    }
}

impl Month {
    pub fn last_day(&self, is_leap_year: bool) -> Day {
        use Month::*;
        match self {
            January | March | May | July | August | October | December => {
                Day::try_from(31).expect("LEQ 31 days")
            }
            April | June | September | November => Day::try_from(30).expect("LEQ 31 days"),
            February => {
                if is_leap_year {
                    Day::try_from(29).expect("LEQ 31 days")
                } else {
                    Day::try_from(28).expect("LEQ 31 days")
                }
            }
        }
    }
}

impl YearMonthAndFortnight {
    pub fn current() -> Self {
        let today = chrono::Local::now().date_naive();
        Self::builder()
            .year(Year::from(today.year()))
            .month(Month::try_from(today.month() as i32).expect("Chrono should return valid month"))
            .half(MonthHalf::from(today))
            .build()
    }

    pub fn last() -> Self {
        Self::current().one_half_earlier()
    }

    pub fn one_half_earlier(&self) -> Self {
        match self.half() {
            MonthHalf::First => {
                let ym = YearAndMonth::from(*self);
                let ym = ym.one_month_earlier();
                Self::year_and_month_with_half(ym, MonthHalf::Second)
            }
            MonthHalf::Second => Self::builder()
                .year(*self.year())
                .month(*self.month())
                .half(MonthHalf::First)
                .build(),
        }
    }
}

impl YearAndMonth {
    /// Returns the last day of the month for this `YearAndMonth`, e.g. if the
    /// year is not a leap year, February will return 28, and for leap year
    /// 29 is returned.
    /// ```
    /// extern crate klirr_core;
    /// use klirr_core::prelude::*;
    ///
    /// let year_and_month = YearAndMonth::january(2025);
    /// assert_eq!(year_and_month.last_day_of_month(), Day::try_from(31).unwrap());
    /// ```
    pub fn last_day_of_month(&self) -> Day {
        self.month().last_day(self.year().is_leap())
    }

    /// Converts this `YearAndMonth` to a `Date` representing the last day of the month.
    ///
    /// ```
    /// extern crate klirr_core;
    /// use klirr_core::prelude::*;
    /// let month = YearAndMonth::january(2025);
    /// let date = month.to_date_end_of_month();
    /// assert_eq!(date.year(), &Year::from(2025));
    /// assert_eq!(date.month(), &Month::January);
    /// assert_eq!(date.day(), &Day::try_from(31).unwrap());
    /// ```
    pub fn to_date_end_of_month(&self) -> Date {
        Date::builder()
            .year(*self.year())
            .month(*self.month())
            .day(self.last_day_of_month())
            .build()
    }

    pub fn current() -> Self {
        Self::from(YearMonthAndFortnight::current())
    }

    /// Returns a new `YearAndMonth` that is one month earlier than this one.
    /// If the month is January, it will return December of the previous year.
    /// ```
    /// extern crate klirr_core;
    /// use klirr_core::prelude::*;
    /// let month = YearAndMonth::january(2025);
    /// let one_month_earlier = month.one_month_earlier();
    /// assert_eq!(one_month_earlier, YearAndMonth::december(2024));
    /// ```
    pub fn one_month_earlier(&self) -> Self {
        let mut year = **self.year();
        let mut month = **self.month();

        if month == 1 {
            year -= 1;
            month = 12
        } else {
            month -= 1
        }

        Self::builder()
            .year(Year::from(year))
            .month(Month::try_from(month).expect("Should return valid month"))
            .build()
    }

    /// Returns a new `YearAndMonth` that is one month later than this one - by
    /// reading the calendar - if the current month is December, it will return
    /// January of the next year.
    pub fn last() -> Self {
        Self::current().one_month_earlier()
    }

    /// Returns the number of months elapsed between this `YearAndMonth` and
    /// another `YearAndMonth`.
    ///
    /// # Examples
    /// ```
    /// extern crate klirr_core;
    /// use klirr_core::prelude::*;
    /// let start = YearAndMonth::january(2025);
    /// let end = YearAndMonth::april(2025);
    /// assert_eq!(end.elapsed_months_since(start).unwrap(), 3);
    /// ```
    ///
    /// # Throws
    /// Throws an error if the `start` month is after the `end` month.
    pub fn elapsed_months_since(&self, start: impl Borrow<Self>) -> Result<u16> {
        let end = self;
        let start = start.borrow();
        if start > end {
            return Err(Error::StartPeriodAfterEndPeriod {
                start: start.to_string(),
                end: end.to_string(),
            });
        }
        let start_year = **start.year();
        let start_month = **start.month() as u16;
        let end_year = **end.year();
        let end_month = **end.month() as u16;
        // When we perform arithmetic below we need to consider
        // e.g. Start: 2024-12, End: 2025-03, where start month can come later
        // in the year than the end month.
        let months_per_year = 12;
        let start_months = start_year * months_per_year + start_month;
        let end_months = end_year * months_per_year + end_month;
        let months_elapsed = end_months - start_months;
        Ok(months_elapsed)
    }
}

impl TryInto<YearAndMonth> for PeriodAnno {
    type Error = crate::Error;

    fn try_into(self) -> std::result::Result<YearAndMonth, Self::Error> {
        todo!()
    }
}

impl TryFromPeriodAnno for YearMonthAndFortnight {
    fn try_from_period_anno(period: PeriodAnno) -> Result<Self> {
        period
            .try_unwrap_year_month_and_fortnight()
            .map_err(|_| Error::PeriodIsNotYearMonthAndFortnight)
    }
}

impl TryFromPeriodAnno for YearAndMonth {
    fn try_from_period_anno(period: PeriodAnno) -> Result<Self> {
        period
            .try_unwrap_year_and_month()
            .map_err(|_| Error::PeriodIsNotYearAndMonth)
    }
}

impl IsPeriod for YearAndMonth {
    fn max_granularity(&self) -> Granularity {
        Granularity::Month
    }

    fn elapsed_periods_since(&self, start: impl Borrow<Self>) -> Result<u16> {
        self.elapsed_months_since(start)
    }

    fn to_date_end_of_period(&self) -> Date {
        self.to_date_end_of_month()
    }

    fn year(&self) -> &Year {
        self.year()
    }

    fn month(&self) -> &Month {
        self.month()
    }
}

/// Calculates the invoice number based on the offset, target month, whether
/// the items are expenses, and the months off record.
/// This function assumes that the `ProtoInvoiceInfo` has already been validated
/// to ensure that the target month is not in the record of months off.
/// It computes the invoice number by considering the elapsed months since
/// the offset month, adjusting for any months that are off record, and
/// adding an additional increment if the items are expenses.
///
/// ```
/// extern crate klirr_core;
/// use klirr_core::prelude::*;
/// let offset = TimestampedInvoiceNumber::<YearAndMonth>::builder().offset(100.into()).period(YearAndMonth::january(2024)).build();
/// let target_month = YearAndMonth::august(2024);
/// let is_expenses = true;
/// let months_off_record = RecordOfPeriodsOff::new([
///   YearAndMonth::march(2024),
///   YearAndMonth::april(2024),
/// ]);
/// let invoice_number = calculate_invoice_number(
///     &offset,
///     &target_month,
///     is_expenses,
///     &months_off_record,
/// ).unwrap();
///
/// // The expected invoice number is calculated as follows:
/// // - Offset is 100
/// // - Target month is August 2024, which is 7 months after January
/// // - Months off record are March and April, which are 2 months off
/// // - Since this is for expenses, we add 1 to the final invoice number.
/// // - Therefore, the invoice number should be 100 + 7 - 2 + 1 = 106
/// let expected = InvoiceNumber::from(106);
/// assert_eq!(invoice_number, expected);
/// ```
pub fn calculate_invoice_number<Period: IsPeriod>(
    offset: &TimestampedInvoiceNumber<Period>,
    target_period: &Period,
    is_expenses: bool,
    record_of_periods_off: &RecordOfPeriodsOff<Period>,
) -> Result<InvoiceNumber> {
    if record_of_periods_off.contains(offset.period()) {
        return Err(Error::RecordsOffMustNotContainOffsetPeriod {
            offset_period: format!("{:?}", offset.period()),
        });
    }
    let periods_elapsed_since_offset = target_period.elapsed_periods_since(offset.period())?;

    let mut periods_off_to_subtract = 0;
    for period_off in record_of_periods_off.iter() {
        if period_off > offset.period() && period_off <= target_period {
            // If the period is recorded as off, we need to adjust the invoice number
            // by subtracting the number of periods off
            periods_off_to_subtract += 1;
        }
    }
    let mut invoice_number =
        **offset.offset() + periods_elapsed_since_offset - periods_off_to_subtract;
    if is_expenses {
        // For expenses we add 1, ensuring that if we invoice for services and
        // expenses the same month, the expense invoice number is always higher.
        invoice_number += 1;
    }
    Ok(InvoiceNumber::from(invoice_number))
}

/// Calculates the number of working days in a given month, excluding weekends.
///
/// # Errors
/// Returns an error if the target month is in the record of months off.
///
/// ```
/// extern crate klirr_core;
/// use klirr_core::prelude::*;
///
/// let target_month = YearAndMonth::january(2024);
/// let working_days = quantity_in_period(&target_month, Granularity::Day, Cadence::Monthly, &RecordOfPeriodsOff::default());
/// assert_eq!(*working_days.unwrap(), dec!(23)); // January 2024 has 23
/// ```
pub fn quantity_in_period<Period: IsPeriod>(
    target_period: &Period,
    granularity: Granularity,
    cadence: Cadence,
    record_of_periods_off: &RecordOfPeriodsOff<Period>,
) -> Result<Quantity> {
    if record_of_periods_off.contains(target_period) {
        return Err(Error::TargetPeriodMustNotBeInRecordOfPeriodsOff {
            target_period: format!("{:?}", target_period),
        });
    }

    if granularity > target_period.max_granularity() {
        return Err(Error::GranularityTooCoarse {
            granularity,
            max_granularity: target_period.max_granularity(),
            target_period: format!("{:?}", target_period),
        });
    }

    let daily = || working_days_in_period(target_period);
    let hourly = || {
        // TODO maybe 8h per day should be configurable
        Result::Ok(Quantity::EIGHT.mul(*working_days_in_period(target_period)?))
    };

    match granularity {
        Granularity::Month => match cadence {
            Cadence::Monthly => Ok(Quantity::ONE),
            Cadence::BiWeekly => Err(Error::CannotInvoiceForMonthWhenCadenceIsBiWeekly),
        },
        Granularity::Fortnight => match cadence {
            Cadence::Monthly => Ok(Quantity::TWO), // Two fortnights in a month
            Cadence::BiWeekly => Ok(Quantity::ONE),
        },
        Granularity::Day => daily(),
        Granularity::Hour => hourly(),
    }
}

trait FromYmd: Sized {
    fn ymd(year: impl Into<i32>, month: impl Into<u32>, day: impl Into<u32>) -> Result<Self>;
}
impl FromYmd for NaiveDate {
    fn ymd(year: impl Into<i32>, month: impl Into<u32>, day: impl Into<u32>) -> Result<Self> {
        let year = year.into();
        let month = month.into();
        let day = day.into();
        Self::from_ymd_opt(year, month, day)
            .ok_or(Error::InvalidDate {
                underlying: "Failed to create date from year and month".to_owned(),
            })?
            .pred_opt()
            .ok_or(Error::InvalidDate {
                underlying: "Failed to create date from year and month".to_owned(),
            })
    }
}

/// Calculates the number of working days in a given month, excluding weekends.
///
/// # Errors
/// Returns an error if the target month is in the record of months off.
fn working_days_in_period<Period: IsPeriod>(target_period: &Period) -> Result<Quantity> {
    let year = **target_period.year() as i32;
    let month = **target_period.month() as u32;

    // Start from the 1st of the month
    let mut day = NaiveDate::ymd(year, month, 1u32)?;

    // Get the last day of the month
    let last_day = if month == 12 {
        NaiveDate::ymd(year + 1, 1u32, 1u32)
    } else {
        NaiveDate::ymd(year, month + 1, 1u32)
    }?;

    let mut working_days = 0;
    while day <= last_day {
        match day.weekday() {
            Weekday::Mon | Weekday::Tue | Weekday::Wed | Weekday::Thu | Weekday::Fri => {
                working_days += 1;
            }
            _ => {}
        }
        day = day.succ_opt().ok_or(Error::InvalidDate {
            underlying: "Failed to get next day".to_owned(),
        })?
    }

    Ok(Quantity::from(working_days))
}

#[cfg(test)]
mod tests {
    #![allow(non_snake_case)]
    #![allow(unused)]
    use super::*;
    use test_log::test;

    /// 2025 is not a leap year
    const JAN_2025: YearAndMonth = YearAndMonth::january(2025);
    /// 2025 is not a leap year
    const FEB_2025: YearAndMonth = YearAndMonth::february(2025);
    /// 2025 is not a leap year
    const APR_2025: YearAndMonth = YearAndMonth::april(2025);
    /// 2025 is not a leap year
    const MAY_2025: YearAndMonth = YearAndMonth::may(2025);
    /// 2025 is not a leap year
    const JUNE_2025: YearAndMonth = YearAndMonth::june(2025);
    const JUNE_2025_FIRST: YearMonthAndFortnight =
        YearMonthAndFortnight::year_and_month_with_half(JUNE_2025, MonthHalf::First);
    /// 2025 is not a leap year
    const JULY_2025: YearAndMonth = YearAndMonth::july(2025);
    const JULY_2025_FIRST: YearMonthAndFortnight =
        YearMonthAndFortnight::year_and_month_with_half(JULY_2025, MonthHalf::First);
    /// 2025 is not a leap year
    const AUG_2025: YearAndMonth = YearAndMonth::august(2025);
    const AUG_2025_FIRST: YearMonthAndFortnight =
        YearMonthAndFortnight::year_and_month_with_half(AUG_2025, MonthHalf::First);
    /// 2025 is not a leap year
    const SEPT_2025: YearAndMonth = YearAndMonth::september(2025);
    const SEPT_2025_FIRST: YearMonthAndFortnight =
        YearMonthAndFortnight::year_and_month_with_half(SEPT_2025, MonthHalf::First);
    /// 2025 is not a leap year
    const OCT_2025: YearAndMonth = YearAndMonth::october(2025);
    /// 2025 is not a leap year
    const NOV_2025: YearAndMonth = YearAndMonth::november(2025);
    /// 2025 is not a leap year
    const DEC_2025: YearAndMonth = YearAndMonth::december(2025);
    const DEC_2025_FIRST: YearMonthAndFortnight =
        YearMonthAndFortnight::year_and_month_with_half(DEC_2025, MonthHalf::First);

    /// 2026 is not a leap year
    const JAN_2026: YearAndMonth = YearAndMonth::january(2026);
    /// 2026 is not a leap year
    const JUNE_2026: YearAndMonth = YearAndMonth::june(2026);
    /// 2026 is not a leap year
    const JULY_2026: YearAndMonth = YearAndMonth::july(2026);
    /// 2026 is not a leap year
    const AUG_2026: YearAndMonth = YearAndMonth::august(2026);

    /// 2028 is a leap year
    const JAN_2028: YearAndMonth = YearAndMonth::january(2028);
    /// 2028 is a leap year
    const FEB_2028: YearAndMonth = YearAndMonth::february(2028);
    /// 2028 is a leap year
    const MAR_2028: YearAndMonth = YearAndMonth::march(2028);

    #[test]
    fn test_quantity_in_period_year_and_month_with_month_granularity() {
        // YearAndMonth has max_granularity of Month, so Month granularity should work
        let target_period = JAN_2025;
        let record_of_periods_off = RecordOfPeriodsOff::new([]);

        let result = quantity_in_period(
            &target_period,
            Granularity::Month,
            Cadence::Monthly,
            &record_of_periods_off,
        );

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Quantity::ONE);
    }

    #[test]
    fn test_quantity_in_period_year_and_month_with_day_granularity() {
        // Day granularity should work with YearAndMonth (less coarse than Month)
        let target_period = JAN_2025;
        let record_of_periods_off = RecordOfPeriodsOff::new([]);

        let result = quantity_in_period(
            &target_period,
            Granularity::Day,
            Cadence::Monthly,
            &record_of_periods_off,
        );

        assert!(result.is_ok());
    }

    #[test]
    fn test_quantity_in_period_year_and_month_with_hour_granularity() {
        // Hour granularity should work with YearAndMonth (less coarse than Month)
        let target_period = JAN_2025;
        let record_of_periods_off = RecordOfPeriodsOff::new([]);

        let result = quantity_in_period(
            &target_period,
            Granularity::Hour,
            Cadence::Monthly,
            &record_of_periods_off,
        );

        assert!(result.is_ok());
    }

    #[test]
    fn test_quantity_in_period_fortnight_with_month_granularity_should_fail() {
        // YearMonthAndFortnight has max_granularity of Fortnight
        // Month granularity should fail because it's coarser than Fortnight
        let fortnight_period = YearMonthAndFortnight::builder()
            .year(Year::from(2025))
            .month(Month::January)
            .half(MonthHalf::First)
            .build();
        let fortnight_record = RecordOfPeriodsOff::new([]);

        let result = quantity_in_period(
            &fortnight_period,
            Granularity::Month,
            Cadence::Monthly,
            &fortnight_record,
        );

        assert!(result.is_err());
        if let Err(Error::GranularityTooCoarse {
            granularity,
            max_granularity,
            ..
        }) = result
        {
            assert_eq!(granularity, Granularity::Month);
            assert_eq!(max_granularity, Granularity::Fortnight);
        } else {
            panic!("Expected GranularityTooCoarse error");
        }
    }

    #[test]
    fn test_quantity_in_period_fortnight_with_fortnight_granularity() {
        // Fortnight granularity should work for fortnight period (exactly the max granularity)
        let fortnight_period = YearMonthAndFortnight::builder()
            .year(Year::from(2025))
            .month(Month::January)
            .half(MonthHalf::First)
            .build();
        let fortnight_record = RecordOfPeriodsOff::new([]);

        let result = quantity_in_period(
            &fortnight_period,
            Granularity::Fortnight,
            Cadence::Monthly,
            &fortnight_record,
        );

        assert!(result.is_ok());
    }

    #[test]
    fn test_quantity_in_period_fortnight_with_hour_granularity() {
        // Hour granularity should work for fortnight period (less coarse than Day)
        let fortnight_period = YearMonthAndFortnight::builder()
            .year(Year::from(2025))
            .month(Month::January)
            .half(MonthHalf::First)
            .build();
        let fortnight_record = RecordOfPeriodsOff::new([]);

        let result = quantity_in_period(
            &fortnight_period,
            Granularity::Hour,
            Cadence::Monthly,
            &fortnight_record,
        );

        assert!(result.is_ok());
    }

    fn test_invoice_number(
        offset_no: impl Into<InvoiceNumber>,
        offset_month: YearAndMonth,
        target_period: YearMonthAndFortnight,
        months_off: impl IntoIterator<Item = YearAndMonth>,
        is_expenses: bool,
        expected: impl Into<InvoiceNumber>,
    ) {
        let input = ValidInput::builder()
            .period(target_period)
            .items(if is_expenses {
                InvoicedItems::Expenses
            } else {
                InvoicedItems::Service { time_off: None }
            })
            .build();
        let information = ProtoInvoiceInfo::builder()
            .purchase_order(PurchaseOrder::from("PO"))
            .record_of_periods_off(RecordOfPeriodsOff::new(months_off))
            .offset(
                TimestampedInvoiceNumber::builder()
                    .offset(offset_no.into())
                    .period(offset_month)
                    .build(),
            )
            .build();

        let invoice_number = calculate_invoice_number(
            information.offset(),
            &YearAndMonth::from(target_period),
            is_expenses,
            information.record_of_periods_off(),
        )
        .unwrap();
        assert_eq!(invoice_number, expected.into());
    }

    mod services {
        use super::*;
        use test_log::test;

        mod no_months_off {

            use super::*;
            use test_log::test;

            #[test]
            fn when__target_month_eq_offset_month__then__invoice_num_eq_offset_num() {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        JUNE_2025,
                        JUNE_2025_FIRST,
                        [],
                        false,
                        invoice_no_offset,
                    );
                }
            }

            #[test]
            fn when__target_month_is_3_months_after_offset_month__then__invoice_num_eq_offset_num_plus_3()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        JUNE_2025,
                        SEPT_2025_FIRST,
                        [],
                        false,
                        invoice_no_offset + 3,
                    );
                }
            }
        }

        mod months_off {

            use super::*;
            use test_log::test;

            #[test]
            fn when__target_month_eq_offset_month_and_months_off_is_in_past__then__invoice_num_eq_offset_num()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        JUNE_2025,
                        JUNE_2025_FIRST,
                        [APR_2025, MAY_2025],
                        false,
                        invoice_no_offset,
                    );
                }
            }

            #[test]
            fn when__target_month_eq_offset_month_and_months_off_is_in_future__then__invoice_num_eq_offset_num()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        JUNE_2025,
                        JUNE_2025_FIRST,
                        [JULY_2026, AUG_2026],
                        false,
                        invoice_no_offset,
                    );
                }
            }

            #[test]
            fn when__target_month_is_3_months_after_offset_month_with_all_months_off__then__invoice_num_eq_offset_num()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        APR_2025,
                        JULY_2025_FIRST,
                        [MAY_2025, JUNE_2025, JULY_2025],
                        false,
                        invoice_no_offset,
                    );
                }
            }

            #[test]
            fn when__target_month_is_4_months_after_offset_month_with_3_months_off__then__invoice_num_eq_offset_num_plus_1()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        APR_2025,
                        AUG_2025_FIRST,
                        [MAY_2025, JUNE_2025, JULY_2025],
                        false,
                        invoice_no_offset + 1,
                    );
                }
            }

            #[test]
            fn when__target_month_is_8_months_after_offset_month_with_3_months_off__then__invoice_num_eq_offset_num_plus_5()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        APR_2025,
                        DEC_2025_FIRST,
                        // Add some months off before offset_month and after target_month
                        [JAN_2025, MAY_2025, JULY_2025, SEPT_2025, JAN_2028],
                        false,
                        invoice_no_offset + 5,
                    );
                }
            }
        }
    }

    mod expenses {
        use super::*;
        use test_log::test;

        mod no_months_off {

            use super::*;
            use test_log::test;

            #[test]
            fn when__target_month_eq_offset_month__then__invoice_num_eq_offset_num_plus_1() {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        JUNE_2025,
                        JUNE_2025_FIRST,
                        [],
                        true,
                        invoice_no_offset + 1,
                    );
                }
            }

            #[test]
            fn when__target_month_is_3_months_after_offset_month__then__invoice_num_eq_offset_num_plus_4()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        JUNE_2025,
                        SEPT_2025_FIRST,
                        [],
                        true,
                        invoice_no_offset + 4,
                    );
                }
            }
        }

        mod months_off {

            use super::*;
            use test_log::test;

            #[test]
            fn when__target_month_eq_offset_month_and_months_off_is_in_past__then__invoice_num_eq_offset_num_plus_1()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        JUNE_2025,
                        JUNE_2025_FIRST,
                        [APR_2025, MAY_2025],
                        true,
                        invoice_no_offset + 1,
                    );
                }
            }

            #[test]
            fn when__target_month_eq_offset_month_and_months_off_is_in_future__then__invoice_num_eq_offset_num_plus_1()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        JUNE_2025,
                        JUNE_2025_FIRST,
                        [JULY_2026, AUG_2026],
                        true,
                        invoice_no_offset + 1,
                    );
                }
            }

            #[test]
            fn when__target_month_is_3_months_after_offset_month_with_all_months_off__then__invoice_num_eq_offset_num_plus_1()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        APR_2025,
                        JULY_2025_FIRST,
                        [MAY_2025, JUNE_2025, JULY_2025],
                        true,
                        invoice_no_offset + 1,
                    );
                }
            }

            #[test]
            fn when__target_month_is_4_months_after_offset_month_with_3_months_off__then__invoice_num_eq_offset_num_plus_2()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        APR_2025,
                        AUG_2025_FIRST,
                        [MAY_2025, JUNE_2025, JULY_2025],
                        true,
                        invoice_no_offset + 2,
                    );
                }
            }

            #[test]
            fn when__target_month_is_8_months_after_offset_month_with_3_months_off__then__invoice_num_eq_offset_num_plus_6()
             {
                for invoice_no_offset in 0..10 {
                    test_invoice_number(
                        invoice_no_offset,
                        APR_2025,
                        DEC_2025_FIRST,
                        // Add some months off before offset_month and after target_month
                        [JAN_2025, MAY_2025, JULY_2025, SEPT_2025, JAN_2028],
                        true,
                        invoice_no_offset + 6,
                    );
                }
            }
        }
    }

    #[test]
    fn test_last_day_of_month() {
        assert_eq!(JAN_2025.last_day_of_month(), Day::try_from(31).unwrap());
        assert_eq!(FEB_2028.last_day_of_month(), Day::try_from(29).unwrap());
        assert_eq!(MAR_2028.last_day_of_month(), Day::try_from(31).unwrap());
        assert_eq!(APR_2025.last_day_of_month(), Day::try_from(30).unwrap());
        assert_eq!(MAY_2025.last_day_of_month(), Day::try_from(31).unwrap());
        assert_eq!(JUNE_2025.last_day_of_month(), Day::try_from(30).unwrap());
        assert_eq!(JULY_2025.last_day_of_month(), Day::try_from(31).unwrap());
        assert_eq!(AUG_2025.last_day_of_month(), Day::try_from(31).unwrap());
        assert_eq!(SEPT_2025.last_day_of_month(), Day::try_from(30).unwrap());
        assert_eq!(OCT_2025.last_day_of_month(), Day::try_from(31).unwrap());
        assert_eq!(NOV_2025.last_day_of_month(), Day::try_from(30).unwrap());
        assert_eq!(DEC_2025.last_day_of_month(), Day::try_from(31).unwrap());

        assert_eq!(FEB_2025.last_day_of_month(), Day::try_from(28).unwrap());
    }

    #[test]
    fn test_elapsed_months_since_when_start_month_is_later_in_the_year_than_end_month() {
        let start = YearAndMonth::december(2024);
        let end = YearAndMonth::april(2025);
        assert_eq!(end.elapsed_months_since(start).unwrap(), 4);
        assert!(start < end);
    }

    #[test]
    fn test_one_month_earlier_of_january() {
        let january_2025 = YearAndMonth::january(2025);
        let december_2024 = YearAndMonth::december(2024);
        assert_eq!(january_2025.one_month_earlier(), december_2024);
    }

    #[test]
    fn elapsed_months_since_throws_when_start_month_is_later_than_end_month() {
        let start = YearAndMonth::april(2025);
        let end = YearAndMonth::march(2025);
        assert!(end.elapsed_months_since(start).is_err());
    }

    #[test]
    fn test_calculate_invoice_number_throws_for_invalid_input() {
        let month = YearAndMonth::may(2025);
        let invoice_info = ProtoInvoiceInfo::builder()
            .offset(
                TimestampedInvoiceNumber::builder()
                    .period(month)
                    .offset(237.into())
                    .build(),
            )
            .record_of_periods_off(RecordOfPeriodsOff::new([month]))
            .purchase_order(PurchaseOrder::sample())
            .build();

        let result = calculate_invoice_number(
            invoice_info.offset(),
            &YearAndMonth::december(2025),
            true,
            invoice_info.record_of_periods_off(),
        );
        assert!(result.is_err());
    }

    #[test]
    fn quantity_in_period_target_month_is_in_record_of_months_off() {
        let target_month = YearAndMonth::january(2024);
        let months_off_record = RecordOfPeriodsOff::new([target_month]);
        let result = quantity_in_period(
            &target_month,
            Granularity::Day,
            Cadence::Monthly,
            &months_off_record,
        );
        assert!(result.is_err());
    }

    #[test]
    fn quantity_in_period_target_month_december() {
        let target_month = YearAndMonth::december(2025);
        let months_off_record = RecordOfPeriodsOff::new([]);
        let result = quantity_in_period(
            &target_month,
            Granularity::Day,
            Cadence::Monthly,
            &months_off_record,
        );
        assert!(result.is_ok());
    }

    #[test]
    fn try_from_period_anno_for_year_and_month() {
        let sut = YearAndMonth::sample();
        let period_anno = PeriodAnno::YearAndMonth(sut);
        let outcome = YearAndMonth::try_from_period_anno(period_anno).unwrap();
        assert_eq!(outcome, sut);
    }

    #[test]
    fn try_from_period_anno_for_year_month_and_fortnight() {
        let sut = YearMonthAndFortnight::sample();
        let period_anno = PeriodAnno::YearMonthAndFortnight(sut);
        let outcome = YearMonthAndFortnight::try_from_period_anno(period_anno).unwrap();
        assert_eq!(outcome, sut);
    }

    #[test]
    fn test_quantity_in_period_throws_when_cadence_is_biweekly_and_granularity_is_month() {
        let target_period = YearAndMonth::january(2025);
        let record_of_periods_off = RecordOfPeriodsOff::new([]);
        let result = quantity_in_period(
            &target_period,
            Granularity::Month,
            Cadence::BiWeekly,
            &record_of_periods_off,
        );
        assert!(result.is_err());
        if let Err(Error::CannotInvoiceForMonthWhenCadenceIsBiWeekly) = result {
            // Expected error
        } else {
            panic!("Expected CannotInvoiceForMonthWhenCadenceIsBiWeekly error");
        }
    }

    #[test]
    fn test_quantity_in_period_granularity_fortnight_cadence_monthly_is_two() {
        let target_period = YearAndMonth::january(2025);
        let record_of_periods_off = RecordOfPeriodsOff::new([]);
        let result = quantity_in_period(
            &target_period,
            Granularity::Fortnight,
            Cadence::Monthly,
            &record_of_periods_off,
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Quantity::TWO);
    }

    #[test]
    fn test_quantity_in_period_granularity_fortnight_cadence_biweekly_is_one() {
        let target_period = YearAndMonth::january(2025);
        let record_of_periods_off = RecordOfPeriodsOff::new([]);
        let result = quantity_in_period(
            &target_period,
            Granularity::Fortnight,
            Cadence::BiWeekly,
            &record_of_periods_off,
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Quantity::ONE);
    }

    #[test]
    fn ymf_one_half_earlier_month_half_first_half() {
        let ym = YearAndMonth::january(2025);
        let ymf = YearMonthAndFortnight::year_and_month_with_half(ym, MonthHalf::First);
        let one_half_earlier = ymf.one_half_earlier();
        assert_eq!(
            one_half_earlier,
            YearMonthAndFortnight::year_and_month_with_half(
                YearAndMonth::december(2024),
                MonthHalf::Second
            )
        );
    }

    #[test]
    fn ymf_one_half_earlier_month_half_second_half() {
        let ym = YearAndMonth::january(2025);
        let ymf = YearMonthAndFortnight::year_and_month_with_half(ym, MonthHalf::Second);
        let one_half_earlier = ymf.one_half_earlier();
        assert_eq!(
            one_half_earlier,
            YearMonthAndFortnight::year_and_month_with_half(ym, MonthHalf::First)
        );
    }
}