datasynth-core 2.4.0

Core domain models, traits, and distributions for synthetic enterprise data generation
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
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
//! Fixed asset master data model.
//!
//! Provides fixed asset master data including depreciation schedules
//! for realistic fixed asset accounting simulation.

use std::str::FromStr;

use chrono::{Datelike, NaiveDate};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

/// MACRS GDS half-year convention percentages (IRS Publication 946).
/// Stored as string slices so they convert to `Decimal` without floating-point artefacts.
const MACRS_GDS_3_YEAR: &[&str] = &["33.33", "44.45", "14.81", "7.41"];
const MACRS_GDS_5_YEAR: &[&str] = &["20.00", "32.00", "19.20", "11.52", "11.52", "5.76"];
const MACRS_GDS_7_YEAR: &[&str] = &[
    "14.29", "24.49", "17.49", "12.49", "8.93", "8.92", "8.93", "4.46",
];
const MACRS_GDS_10_YEAR: &[&str] = &[
    "10.00", "18.00", "14.40", "11.52", "9.22", "7.37", "6.55", "6.55", "6.56", "6.55", "3.28",
];
const MACRS_GDS_15_YEAR: &[&str] = &[
    "5.00", "9.50", "8.55", "7.70", "6.93", "6.23", "5.90", "5.90", "5.91", "5.90", "5.91", "5.90",
    "5.91", "5.90", "5.91", "2.95",
];
const MACRS_GDS_20_YEAR: &[&str] = &[
    "3.750", "7.219", "6.677", "6.177", "5.713", "5.285", "4.888", "4.522", "4.462", "4.461",
    "4.462", "4.461", "4.462", "4.461", "4.462", "4.461", "4.462", "4.461", "4.462", "4.461",
    "2.231",
];

/// Map useful life in years to the appropriate MACRS GDS depreciation table.
fn macrs_table_for_life(useful_life_years: u32) -> Option<&'static [&'static str]> {
    match useful_life_years {
        1..=3 => Some(MACRS_GDS_3_YEAR),
        4..=5 => Some(MACRS_GDS_5_YEAR),
        6..=7 => Some(MACRS_GDS_7_YEAR),
        8..=10 => Some(MACRS_GDS_10_YEAR),
        11..=15 => Some(MACRS_GDS_15_YEAR),
        16..=20 => Some(MACRS_GDS_20_YEAR),
        _ => None,
    }
}

/// Parse a MACRS percentage string into a `Decimal`.
fn macrs_pct(s: &str) -> Decimal {
    Decimal::from_str(s).unwrap_or(Decimal::ZERO)
}

/// Asset class for categorization and default depreciation rules.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum AssetClass {
    /// Buildings and structures
    Buildings,
    /// Building improvements
    BuildingImprovements,
    /// Land (typically non-depreciable)
    Land,
    /// Machinery and equipment
    #[default]
    MachineryEquipment,
    /// Machinery (alias for MachineryEquipment)
    Machinery,
    /// Computer hardware
    ComputerHardware,
    /// IT Equipment (alias for ComputerHardware)
    ItEquipment,
    /// Office furniture and fixtures
    FurnitureFixtures,
    /// Furniture (alias for FurnitureFixtures)
    Furniture,
    /// Vehicles
    Vehicles,
    /// Leasehold improvements
    LeaseholdImprovements,
    /// Intangible assets (software, patents)
    Intangibles,
    /// Software
    Software,
    /// Construction in progress (not yet depreciating)
    ConstructionInProgress,
    /// Low-value assets
    LowValueAssets,
}

impl AssetClass {
    /// Get default useful life in months for this asset class.
    pub fn default_useful_life_months(&self) -> u32 {
        match self {
            Self::Buildings | Self::BuildingImprovements => 480, // 40 years
            Self::Land => 0,                                     // Not depreciated
            Self::MachineryEquipment | Self::Machinery => 120,   // 10 years
            Self::ComputerHardware | Self::ItEquipment => 36,    // 3 years
            Self::FurnitureFixtures | Self::Furniture => 84,     // 7 years
            Self::Vehicles => 60,                                // 5 years
            Self::LeaseholdImprovements => 120,                  // 10 years (or lease term)
            Self::Intangibles | Self::Software => 60,            // 5 years
            Self::ConstructionInProgress => 0,                   // Not depreciated until complete
            Self::LowValueAssets => 12,                          // 1 year
        }
    }

    /// Check if this asset class is depreciable.
    pub fn is_depreciable(&self) -> bool {
        !matches!(self, Self::Land | Self::ConstructionInProgress)
    }

    /// Get default depreciation method for this asset class.
    pub fn default_depreciation_method(&self) -> DepreciationMethod {
        match self {
            Self::Buildings | Self::BuildingImprovements | Self::LeaseholdImprovements => {
                DepreciationMethod::StraightLine
            }
            Self::MachineryEquipment | Self::Machinery => DepreciationMethod::StraightLine,
            Self::ComputerHardware | Self::ItEquipment => {
                DepreciationMethod::DoubleDecliningBalance
            }
            Self::FurnitureFixtures | Self::Furniture => DepreciationMethod::StraightLine,
            Self::Vehicles => DepreciationMethod::DoubleDecliningBalance,
            Self::Intangibles | Self::Software => DepreciationMethod::StraightLine,
            Self::LowValueAssets => DepreciationMethod::ImmediateExpense,
            Self::Land | Self::ConstructionInProgress => DepreciationMethod::None,
        }
    }
}

/// Depreciation calculation method.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum DepreciationMethod {
    /// Straight-line depreciation
    #[default]
    StraightLine,
    /// Double declining balance
    DoubleDecliningBalance,
    /// Sum of years' digits
    SumOfYearsDigits,
    /// Units of production
    UnitsOfProduction,
    /// MACRS (Modified Accelerated Cost Recovery System)
    Macrs,
    /// Immediate expense (low-value assets)
    ImmediateExpense,
    /// German declining balance (Degressiv): min(3× straight-line rate, 30%) on NBV,
    /// with automatic switch to straight-line when SL exceeds Degressiv.
    /// Per EStG §7(2) / Wachstumschancengesetz (Jul 2025 – Dec 2027).
    Degressiv,
    /// No depreciation (land, CIP)
    None,
}

impl DepreciationMethod {
    /// Calculate monthly depreciation amount.
    pub fn calculate_monthly_depreciation(
        &self,
        acquisition_cost: Decimal,
        salvage_value: Decimal,
        useful_life_months: u32,
        months_elapsed: u32,
        accumulated_depreciation: Decimal,
    ) -> Decimal {
        if useful_life_months == 0 {
            return Decimal::ZERO;
        }

        let depreciable_base = acquisition_cost - salvage_value;
        let net_book_value = acquisition_cost - accumulated_depreciation;

        // Don't depreciate below salvage value
        if net_book_value <= salvage_value {
            return Decimal::ZERO;
        }

        match self {
            Self::StraightLine => {
                let monthly_amount = depreciable_base / Decimal::from(useful_life_months);
                // Cap at remaining book value above salvage
                monthly_amount.min(net_book_value - salvage_value)
            }

            Self::DoubleDecliningBalance => {
                // Double the straight-line rate applied to NBV
                let annual_rate = Decimal::from(2) / Decimal::from(useful_life_months) * dec!(12);
                let monthly_rate = annual_rate / dec!(12);
                let depreciation = net_book_value * monthly_rate;
                // Cap at remaining book value above salvage
                depreciation.min(net_book_value - salvage_value)
            }

            Self::SumOfYearsDigits => {
                let years_total = useful_life_months / 12;
                let sum_of_years: u32 = (1..=years_total).sum();
                let current_year = (months_elapsed / 12) + 1;
                let remaining_years = years_total.saturating_sub(current_year) + 1;

                if sum_of_years == 0 || remaining_years == 0 {
                    return Decimal::ZERO;
                }

                let year_fraction = Decimal::from(remaining_years) / Decimal::from(sum_of_years);
                let annual_depreciation = depreciable_base * year_fraction;
                let monthly_amount = annual_depreciation / dec!(12);
                monthly_amount.min(net_book_value - salvage_value)
            }

            Self::UnitsOfProduction => {
                // For units of production, caller should use specific production-based calculation
                // This is a fallback that uses straight-line
                let monthly_amount = depreciable_base / Decimal::from(useful_life_months);
                monthly_amount.min(net_book_value - salvage_value)
            }

            Self::Macrs => {
                // MACRS GDS half-year convention using IRS Publication 946 tables.
                // MACRS ignores salvage value — the full acquisition cost is the depreciable base.
                let useful_life_years = useful_life_months / 12;
                let current_year = (months_elapsed / 12) as usize;

                if let Some(table) = macrs_table_for_life(useful_life_years) {
                    if current_year < table.len() {
                        let pct = macrs_pct(table[current_year]);
                        let annual_depreciation = acquisition_cost * pct / dec!(100);
                        let monthly_amount = annual_depreciation / dec!(12);
                        // Cap so we don't go below zero NBV
                        monthly_amount.min(net_book_value)
                    } else {
                        Decimal::ZERO
                    }
                } else {
                    // Useful life outside MACRS table range — fall back to DDB
                    let annual_rate =
                        Decimal::from(2) / Decimal::from(useful_life_months) * dec!(12);
                    let monthly_rate = annual_rate / dec!(12);
                    let depreciation = net_book_value * monthly_rate;
                    depreciation.min(net_book_value - salvage_value)
                }
            }

            Self::ImmediateExpense => {
                // Full expense in first month
                if months_elapsed == 0 {
                    depreciable_base
                } else {
                    Decimal::ZERO
                }
            }

            Self::Degressiv => {
                // German declining balance: min(3 × SL rate, 30%) on NBV,
                // with automatic switch to straight-line when SL > Degressiv.
                let useful_life_years = useful_life_months / 12;
                if useful_life_years == 0 {
                    return Decimal::ZERO;
                }

                let sl_annual_rate = Decimal::ONE / Decimal::from(useful_life_years);
                // Degressiv rate: 3× straight-line, capped at 30%
                let degressiv_rate = (sl_annual_rate * Decimal::from(3)).min(dec!(0.30));
                let degressiv_monthly = net_book_value * degressiv_rate / dec!(12);

                // Straight-line on remaining: depreciable base spread over remaining months
                let remaining_months = useful_life_months.saturating_sub(months_elapsed);
                let sl_monthly = if remaining_months > 0 {
                    (net_book_value - salvage_value) / Decimal::from(remaining_months)
                } else {
                    Decimal::ZERO
                };

                // Switch to SL when it exceeds Degressiv
                let monthly_amount = degressiv_monthly.max(sl_monthly);
                monthly_amount
                    .min(net_book_value - salvage_value)
                    .max(Decimal::ZERO)
            }

            Self::None => Decimal::ZERO,
        }
    }
}

/// Account determination rules for fixed asset transactions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssetAccountDetermination {
    /// Asset balance sheet account
    pub asset_account: String,
    /// Accumulated depreciation account
    pub accumulated_depreciation_account: String,
    /// Depreciation expense account
    pub depreciation_expense_account: String,
    /// Gain on disposal account
    pub gain_on_disposal_account: String,
    /// Loss on disposal account
    pub loss_on_disposal_account: String,
    /// Clearing account for acquisitions
    pub acquisition_clearing_account: String,
    /// Gain/loss account (combined, for backward compatibility).
    pub gain_loss_account: String,
}

impl Default for AssetAccountDetermination {
    fn default() -> Self {
        Self {
            asset_account: "160000".to_string(),
            accumulated_depreciation_account: "169000".to_string(),
            depreciation_expense_account: "640000".to_string(),
            gain_on_disposal_account: "810000".to_string(),
            loss_on_disposal_account: "840000".to_string(),
            acquisition_clearing_account: "299000".to_string(),
            gain_loss_account: "810000".to_string(),
        }
    }
}

/// Asset acquisition type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum AcquisitionType {
    /// External purchase
    #[default]
    Purchase,
    /// Self-constructed
    SelfConstructed,
    /// Transfer from another entity
    Transfer,
    /// Acquired in business combination
    BusinessCombination,
    /// Leased asset (finance lease)
    FinanceLease,
    /// Donation received
    Donation,
}

/// Status of a fixed asset.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum AssetStatus {
    /// Under construction (CIP)
    UnderConstruction,
    /// Active and in use
    #[default]
    Active,
    /// Temporarily not in use
    Inactive,
    /// Fully depreciated but still in use
    FullyDepreciated,
    /// Scheduled for disposal
    PendingDisposal,
    /// Disposed/retired
    Disposed,
}

/// Fixed asset master data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FixedAsset {
    /// Asset ID (e.g., "FA-001234")
    pub asset_id: String,

    /// Asset sub-number (for component accounting)
    pub sub_number: u16,

    /// Asset description
    pub description: String,

    /// Asset class
    pub asset_class: AssetClass,

    /// Company code
    pub company_code: String,

    /// Cost center responsible for the asset
    pub cost_center: Option<String>,

    /// Location/plant
    pub location: Option<String>,

    /// Acquisition date
    pub acquisition_date: NaiveDate,

    /// Acquisition type
    pub acquisition_type: AcquisitionType,

    /// Original acquisition cost
    pub acquisition_cost: Decimal,

    /// Capitalized date (when depreciation starts)
    pub capitalized_date: Option<NaiveDate>,

    /// Depreciation method
    pub depreciation_method: DepreciationMethod,

    /// Useful life in months
    pub useful_life_months: u32,

    /// Salvage/residual value
    pub salvage_value: Decimal,

    /// Accumulated depreciation as of current period
    pub accumulated_depreciation: Decimal,

    /// Net book value (acquisition_cost - accumulated_depreciation)
    pub net_book_value: Decimal,

    /// Account determination rules
    pub account_determination: AssetAccountDetermination,

    /// Current status
    pub status: AssetStatus,

    /// Disposal date (if disposed)
    pub disposal_date: Option<NaiveDate>,

    /// Disposal proceeds (if disposed)
    pub disposal_proceeds: Option<Decimal>,

    /// Serial number (for tracking)
    pub serial_number: Option<String>,

    /// Manufacturer
    pub manufacturer: Option<String>,

    /// Model
    pub model: Option<String>,

    /// Warranty expiration date
    pub warranty_expiration: Option<NaiveDate>,

    /// Insurance policy number
    pub insurance_policy: Option<String>,

    /// Original PO number
    pub purchase_order: Option<String>,

    /// Vendor ID (who supplied the asset)
    pub vendor_id: Option<String>,

    /// Invoice reference
    pub invoice_reference: Option<String>,

    /// Whether this asset is a GWG (geringwertiges Wirtschaftsgut) under German tax law.
    /// Assets with acquisition cost ≤ 800 EUR are eligible for immediate full expensing
    /// per EStG §6(2).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub is_gwg: Option<bool>,
}

impl FixedAsset {
    /// Create a new fixed asset.
    pub fn new(
        asset_id: impl Into<String>,
        description: impl Into<String>,
        asset_class: AssetClass,
        company_code: impl Into<String>,
        acquisition_date: NaiveDate,
        acquisition_cost: Decimal,
    ) -> Self {
        let useful_life_months = asset_class.default_useful_life_months();
        let depreciation_method = asset_class.default_depreciation_method();

        Self {
            asset_id: asset_id.into(),
            sub_number: 0,
            description: description.into(),
            asset_class,
            company_code: company_code.into(),
            cost_center: None,
            location: None,
            acquisition_date,
            acquisition_type: AcquisitionType::Purchase,
            acquisition_cost,
            capitalized_date: Some(acquisition_date),
            depreciation_method,
            useful_life_months,
            salvage_value: Decimal::ZERO,
            accumulated_depreciation: Decimal::ZERO,
            net_book_value: acquisition_cost,
            account_determination: AssetAccountDetermination::default(),
            status: AssetStatus::Active,
            disposal_date: None,
            disposal_proceeds: None,
            serial_number: None,
            manufacturer: None,
            model: None,
            warranty_expiration: None,
            insurance_policy: None,
            purchase_order: None,
            vendor_id: None,
            invoice_reference: None,
            is_gwg: None,
        }
    }

    /// Set cost center.
    pub fn with_cost_center(mut self, cost_center: impl Into<String>) -> Self {
        self.cost_center = Some(cost_center.into());
        self
    }

    /// Set location.
    pub fn with_location(mut self, location: impl Into<String>) -> Self {
        self.location = Some(location.into());
        self
    }

    /// Set salvage value.
    pub fn with_salvage_value(mut self, salvage_value: Decimal) -> Self {
        self.salvage_value = salvage_value;
        self
    }

    /// Set depreciation method.
    pub fn with_depreciation_method(mut self, method: DepreciationMethod) -> Self {
        self.depreciation_method = method;
        self
    }

    /// Set useful life.
    pub fn with_useful_life_months(mut self, months: u32) -> Self {
        self.useful_life_months = months;
        self
    }

    /// Set vendor ID.
    pub fn with_vendor(mut self, vendor_id: impl Into<String>) -> Self {
        self.vendor_id = Some(vendor_id.into());
        self
    }

    /// Calculate months since capitalization.
    pub fn months_since_capitalization(&self, as_of_date: NaiveDate) -> u32 {
        let cap_date = self.capitalized_date.unwrap_or(self.acquisition_date);
        if as_of_date < cap_date {
            return 0;
        }

        let years = as_of_date.year() - cap_date.year();
        let months = as_of_date.month() as i32 - cap_date.month() as i32;
        ((years * 12) + months).max(0) as u32
    }

    /// Calculate depreciation for a specific month.
    pub fn calculate_monthly_depreciation(&self, as_of_date: NaiveDate) -> Decimal {
        if !self.asset_class.is_depreciable() {
            return Decimal::ZERO;
        }

        if self.status == AssetStatus::Disposed {
            return Decimal::ZERO;
        }

        let months_elapsed = self.months_since_capitalization(as_of_date);

        self.depreciation_method.calculate_monthly_depreciation(
            self.acquisition_cost,
            self.salvage_value,
            self.useful_life_months,
            months_elapsed,
            self.accumulated_depreciation,
        )
    }

    /// Apply depreciation and update balances.
    pub fn apply_depreciation(&mut self, depreciation_amount: Decimal) {
        self.accumulated_depreciation += depreciation_amount;
        self.net_book_value = self.acquisition_cost - self.accumulated_depreciation;

        // Update status if fully depreciated
        if self.net_book_value <= self.salvage_value && self.status == AssetStatus::Active {
            self.status = AssetStatus::FullyDepreciated;
        }
    }

    /// Calculate gain/loss on disposal.
    pub fn calculate_disposal_gain_loss(&self, proceeds: Decimal) -> Decimal {
        proceeds - self.net_book_value
    }

    /// Record disposal.
    pub fn dispose(&mut self, disposal_date: NaiveDate, proceeds: Decimal) {
        self.disposal_date = Some(disposal_date);
        self.disposal_proceeds = Some(proceeds);
        self.status = AssetStatus::Disposed;
    }

    /// Check if asset is fully depreciated.
    pub fn is_fully_depreciated(&self) -> bool {
        self.net_book_value <= self.salvage_value
    }

    /// Calculate remaining useful life in months.
    pub fn remaining_useful_life_months(&self, as_of_date: NaiveDate) -> u32 {
        let months_elapsed = self.months_since_capitalization(as_of_date);
        self.useful_life_months.saturating_sub(months_elapsed)
    }

    /// Calculate depreciation rate (annual percentage).
    pub fn annual_depreciation_rate(&self) -> Decimal {
        if self.useful_life_months == 0 {
            return Decimal::ZERO;
        }

        match self.depreciation_method {
            DepreciationMethod::StraightLine => {
                Decimal::from(12) / Decimal::from(self.useful_life_months) * dec!(100)
            }
            DepreciationMethod::DoubleDecliningBalance => {
                Decimal::from(24) / Decimal::from(self.useful_life_months) * dec!(100)
            }
            _ => Decimal::from(12) / Decimal::from(self.useful_life_months) * dec!(100),
        }
    }

    /// Return the annual MACRS depreciation for a given recovery year (1-indexed).
    ///
    /// Uses the IRS Publication 946 GDS half-year convention tables.
    /// MACRS depreciation is based on the full acquisition cost (salvage value is ignored).
    /// Returns `Decimal::ZERO` if the year is out of range or no table matches the useful life.
    pub fn macrs_depreciation(&self, year: u32) -> Decimal {
        if year == 0 {
            return Decimal::ZERO;
        }

        let useful_life_years = self.useful_life_months / 12;
        let table_index = (year - 1) as usize;

        match macrs_table_for_life(useful_life_years) {
            Some(table) if table_index < table.len() => {
                let pct = macrs_pct(table[table_index]);
                self.acquisition_cost * pct / dec!(100)
            }
            _ => Decimal::ZERO,
        }
    }

    /// Return the monthly double-declining balance depreciation amount.
    ///
    /// The DDB rate is `2 / useful_life_months * 12` applied monthly against the current
    /// net book value. The result is rounded to 2 decimal places and capped so the asset
    /// does not depreciate below the salvage value.
    pub fn ddb_depreciation(&self) -> Decimal {
        if self.useful_life_months == 0 {
            return Decimal::ZERO;
        }

        let net_book_value = self.acquisition_cost - self.accumulated_depreciation;
        if net_book_value <= self.salvage_value {
            return Decimal::ZERO;
        }

        let annual_rate = Decimal::from(2) / Decimal::from(self.useful_life_months) * dec!(12);
        let monthly_rate = annual_rate / dec!(12);
        let depreciation = (net_book_value * monthly_rate).round_dp(2);
        depreciation.min(net_book_value - self.salvage_value)
    }
}

/// Pool of fixed assets for transaction generation.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FixedAssetPool {
    /// All fixed assets
    pub assets: Vec<FixedAsset>,
    /// Index by asset class
    #[serde(skip)]
    class_index: std::collections::HashMap<AssetClass, Vec<usize>>,
    /// Index by company code
    #[serde(skip)]
    company_index: std::collections::HashMap<String, Vec<usize>>,
}

impl FixedAssetPool {
    /// Create a new empty asset pool.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an asset to the pool.
    pub fn add_asset(&mut self, asset: FixedAsset) {
        let idx = self.assets.len();
        let asset_class = asset.asset_class;
        let company_code = asset.company_code.clone();

        self.assets.push(asset);

        self.class_index.entry(asset_class).or_default().push(idx);
        self.company_index
            .entry(company_code)
            .or_default()
            .push(idx);
    }

    /// Get all assets requiring depreciation for a given month.
    pub fn get_depreciable_assets(&self) -> Vec<&FixedAsset> {
        self.assets
            .iter()
            .filter(|a| {
                a.asset_class.is_depreciable()
                    && a.status == AssetStatus::Active
                    && !a.is_fully_depreciated()
            })
            .collect()
    }

    /// Get mutable references to depreciable assets.
    pub fn get_depreciable_assets_mut(&mut self) -> Vec<&mut FixedAsset> {
        self.assets
            .iter_mut()
            .filter(|a| {
                a.asset_class.is_depreciable()
                    && a.status == AssetStatus::Active
                    && !a.is_fully_depreciated()
            })
            .collect()
    }

    /// Get assets by company code.
    pub fn get_by_company(&self, company_code: &str) -> Vec<&FixedAsset> {
        self.company_index
            .get(company_code)
            .map(|indices| indices.iter().map(|&i| &self.assets[i]).collect())
            .unwrap_or_default()
    }

    /// Get assets by class.
    pub fn get_by_class(&self, asset_class: AssetClass) -> Vec<&FixedAsset> {
        self.class_index
            .get(&asset_class)
            .map(|indices| indices.iter().map(|&i| &self.assets[i]).collect())
            .unwrap_or_default()
    }

    /// Get asset by ID.
    pub fn get_by_id(&self, asset_id: &str) -> Option<&FixedAsset> {
        self.assets.iter().find(|a| a.asset_id == asset_id)
    }

    /// Get mutable asset by ID.
    pub fn get_by_id_mut(&mut self, asset_id: &str) -> Option<&mut FixedAsset> {
        self.assets.iter_mut().find(|a| a.asset_id == asset_id)
    }

    /// Calculate total depreciation for all assets in a period.
    pub fn calculate_period_depreciation(&self, as_of_date: NaiveDate) -> Decimal {
        self.get_depreciable_assets()
            .iter()
            .map(|a| a.calculate_monthly_depreciation(as_of_date))
            .sum()
    }

    /// Get total net book value.
    pub fn total_net_book_value(&self) -> Decimal {
        self.assets
            .iter()
            .filter(|a| a.status != AssetStatus::Disposed)
            .map(|a| a.net_book_value)
            .sum()
    }

    /// Get count of assets.
    pub fn len(&self) -> usize {
        self.assets.len()
    }

    /// Check if pool is empty.
    pub fn is_empty(&self) -> bool {
        self.assets.is_empty()
    }

    /// Rebuild indices after deserialization.
    pub fn rebuild_indices(&mut self) {
        self.class_index.clear();
        self.company_index.clear();

        for (idx, asset) in self.assets.iter().enumerate() {
            self.class_index
                .entry(asset.asset_class)
                .or_default()
                .push(idx);
            self.company_index
                .entry(asset.company_code.clone())
                .or_default()
                .push(idx);
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    fn test_date(year: i32, month: u32, day: u32) -> NaiveDate {
        NaiveDate::from_ymd_opt(year, month, day).unwrap()
    }

    #[test]
    fn test_asset_creation() {
        let asset = FixedAsset::new(
            "FA-001",
            "Office Computer",
            AssetClass::ComputerHardware,
            "1000",
            test_date(2024, 1, 1),
            Decimal::from(2000),
        );

        assert_eq!(asset.asset_id, "FA-001");
        assert_eq!(asset.acquisition_cost, Decimal::from(2000));
        assert_eq!(asset.useful_life_months, 36); // 3 years for computers
    }

    #[test]
    fn test_straight_line_depreciation() {
        let asset = FixedAsset::new(
            "FA-001",
            "Office Equipment",
            AssetClass::FurnitureFixtures,
            "1000",
            test_date(2024, 1, 1),
            Decimal::from(8400),
        )
        .with_useful_life_months(84) // 7 years
        .with_depreciation_method(DepreciationMethod::StraightLine);

        let monthly_dep = asset.calculate_monthly_depreciation(test_date(2024, 2, 1));
        assert_eq!(monthly_dep, Decimal::from(100)); // 8400 / 84 months
    }

    #[test]
    fn test_salvage_value_limit() {
        let mut asset = FixedAsset::new(
            "FA-001",
            "Test Asset",
            AssetClass::MachineryEquipment,
            "1000",
            test_date(2024, 1, 1),
            Decimal::from(1200),
        )
        .with_useful_life_months(12)
        .with_salvage_value(Decimal::from(200));

        // Apply 11 months of depreciation (1000/12 ~= 83.33 each)
        for _ in 0..11 {
            let dep = Decimal::from(83);
            asset.apply_depreciation(dep);
        }

        // At this point, NBV should be around 287, which is above salvage (200)
        // Next depreciation should be limited to not go below salvage
        let final_dep = asset.calculate_monthly_depreciation(test_date(2024, 12, 1));

        // Verify we don't depreciate below salvage
        asset.apply_depreciation(final_dep);
        assert!(asset.net_book_value >= asset.salvage_value);
    }

    #[test]
    fn test_disposal() {
        let mut asset = FixedAsset::new(
            "FA-001",
            "Old Equipment",
            AssetClass::MachineryEquipment,
            "1000",
            test_date(2020, 1, 1),
            Decimal::from(10000),
        );

        // Simulate some depreciation
        asset.apply_depreciation(Decimal::from(5000));

        // Calculate gain/loss
        let gain_loss = asset.calculate_disposal_gain_loss(Decimal::from(6000));
        assert_eq!(gain_loss, Decimal::from(1000)); // Gain of 1000

        // Record disposal
        asset.dispose(test_date(2024, 1, 1), Decimal::from(6000));
        assert_eq!(asset.status, AssetStatus::Disposed);
    }

    #[test]
    fn test_land_not_depreciable() {
        let asset = FixedAsset::new(
            "FA-001",
            "Land Parcel",
            AssetClass::Land,
            "1000",
            test_date(2024, 1, 1),
            Decimal::from(500000),
        );

        let dep = asset.calculate_monthly_depreciation(test_date(2024, 6, 1));
        assert_eq!(dep, Decimal::ZERO);
    }

    #[test]
    fn test_asset_pool() {
        let mut pool = FixedAssetPool::new();

        pool.add_asset(FixedAsset::new(
            "FA-001",
            "Computer 1",
            AssetClass::ComputerHardware,
            "1000",
            test_date(2024, 1, 1),
            Decimal::from(2000),
        ));

        pool.add_asset(FixedAsset::new(
            "FA-002",
            "Desk",
            AssetClass::FurnitureFixtures,
            "1000",
            test_date(2024, 1, 1),
            Decimal::from(500),
        ));

        assert_eq!(pool.len(), 2);
        assert_eq!(pool.get_by_class(AssetClass::ComputerHardware).len(), 1);
        assert_eq!(pool.get_by_company("1000").len(), 2);
    }

    #[test]
    fn test_months_since_capitalization() {
        let asset = FixedAsset::new(
            "FA-001",
            "Test",
            AssetClass::MachineryEquipment,
            "1000",
            test_date(2024, 3, 15),
            Decimal::from(10000),
        );

        assert_eq!(asset.months_since_capitalization(test_date(2024, 3, 1)), 0);
        assert_eq!(asset.months_since_capitalization(test_date(2024, 6, 1)), 3);
        assert_eq!(asset.months_since_capitalization(test_date(2025, 3, 1)), 12);
    }

    // ---- MACRS GDS table tests ----

    #[test]
    fn test_macrs_tables_sum_to_100() {
        let tables: &[(&str, &[&str])] = &[
            ("3-year", MACRS_GDS_3_YEAR),
            ("5-year", MACRS_GDS_5_YEAR),
            ("7-year", MACRS_GDS_7_YEAR),
            ("10-year", MACRS_GDS_10_YEAR),
            ("15-year", MACRS_GDS_15_YEAR),
            ("20-year", MACRS_GDS_20_YEAR),
        ];

        let tolerance = dec!(0.02);
        let hundred = dec!(100);

        for (label, table) in tables {
            let sum: Decimal = table.iter().map(|s| macrs_pct(s)).sum();
            let diff = (sum - hundred).abs();
            assert!(
                diff < tolerance,
                "MACRS GDS {label} table sums to {sum}, expected ~100.0"
            );
        }
    }

    #[test]
    fn test_macrs_table_for_life_mapping() {
        // 1-3 years -> 3-year table (4 entries)
        assert_eq!(macrs_table_for_life(1).unwrap().len(), 4);
        assert_eq!(macrs_table_for_life(3).unwrap().len(), 4);

        // 4-5 years -> 5-year table (6 entries)
        assert_eq!(macrs_table_for_life(4).unwrap().len(), 6);
        assert_eq!(macrs_table_for_life(5).unwrap().len(), 6);

        // 6-7 years -> 7-year table (8 entries)
        assert_eq!(macrs_table_for_life(6).unwrap().len(), 8);
        assert_eq!(macrs_table_for_life(7).unwrap().len(), 8);

        // 8-10 years -> 10-year table (11 entries)
        assert_eq!(macrs_table_for_life(8).unwrap().len(), 11);
        assert_eq!(macrs_table_for_life(10).unwrap().len(), 11);

        // 11-15 years -> 15-year table (16 entries)
        assert_eq!(macrs_table_for_life(11).unwrap().len(), 16);
        assert_eq!(macrs_table_for_life(15).unwrap().len(), 16);

        // 16-20 years -> 20-year table (21 entries)
        assert_eq!(macrs_table_for_life(16).unwrap().len(), 21);
        assert_eq!(macrs_table_for_life(20).unwrap().len(), 21);

        // Out of range -> None
        assert!(macrs_table_for_life(0).is_none());
        assert!(macrs_table_for_life(21).is_none());
        assert!(macrs_table_for_life(100).is_none());
    }

    #[test]
    fn test_macrs_depreciation_5_year_asset() {
        let asset = FixedAsset::new(
            "FA-MACRS",
            "Vehicle",
            AssetClass::Vehicles,
            "1000",
            test_date(2024, 1, 1),
            Decimal::from(10000),
        )
        .with_useful_life_months(60) // 5 years
        .with_depreciation_method(DepreciationMethod::Macrs);

        // Year 1: 20.00% of 10,000 = 2,000
        assert_eq!(asset.macrs_depreciation(1), Decimal::from(2000));
        // Year 2: 32.00% of 10,000 = 3,200
        assert_eq!(asset.macrs_depreciation(2), Decimal::from(3200));
        // Year 3: 19.20% of 10,000 = 1,920
        assert_eq!(asset.macrs_depreciation(3), Decimal::from(1920));
        // Year 4: 11.52% of 10,000 = 1,152
        assert_eq!(asset.macrs_depreciation(4), Decimal::from(1152));
        // Year 5: 11.52% of 10,000 = 1,152
        assert_eq!(asset.macrs_depreciation(5), Decimal::from(1152));
        // Year 6: 5.76% of 10,000 = 576
        assert_eq!(asset.macrs_depreciation(6), Decimal::from(576));
        // Year 7: beyond table -> 0
        assert_eq!(asset.macrs_depreciation(7), Decimal::ZERO);
        // Year 0: invalid -> 0
        assert_eq!(asset.macrs_depreciation(0), Decimal::ZERO);
    }

    #[test]
    fn test_macrs_calculate_monthly_depreciation_uses_tables() {
        let asset = FixedAsset::new(
            "FA-MACRS-M",
            "Vehicle",
            AssetClass::Vehicles,
            "1000",
            test_date(2024, 1, 1),
            Decimal::from(12000),
        )
        .with_useful_life_months(60) // 5 years
        .with_depreciation_method(DepreciationMethod::Macrs);

        // Year 1 (months_elapsed 0..11): 20.00% of 12,000 = 2,400 annual -> 200/month
        let monthly_year1 = asset.calculate_monthly_depreciation(test_date(2024, 2, 1));
        assert_eq!(monthly_year1, Decimal::from(200));

        // Year 2 (months_elapsed 12..23): 32.00% of 12,000 = 3,840 annual -> 320/month
        let monthly_year2 = asset.calculate_monthly_depreciation(test_date(2025, 2, 1));
        assert_eq!(monthly_year2, Decimal::from(320));
    }

    #[test]
    fn test_ddb_depreciation() {
        let asset = FixedAsset::new(
            "FA-DDB",
            "Server",
            AssetClass::ComputerHardware,
            "1000",
            test_date(2024, 1, 1),
            Decimal::from(3600),
        )
        .with_useful_life_months(36) // 3 years
        .with_depreciation_method(DepreciationMethod::DoubleDecliningBalance);

        // DDB annual rate = 2 / 36 * 12 = 2/3
        // Monthly rate = (2/3) / 12 = 1/18
        // First month: 3600 * (1/18) = 200
        let monthly = asset.ddb_depreciation();
        assert_eq!(monthly, Decimal::from(200));
    }

    #[test]
    fn test_ddb_depreciation_with_accumulated() {
        let mut asset = FixedAsset::new(
            "FA-DDB2",
            "Laptop",
            AssetClass::ComputerHardware,
            "1000",
            test_date(2024, 1, 1),
            Decimal::from(1800),
        )
        .with_useful_life_months(36);

        // After accumulating 900 of depreciation, NBV = 900
        asset.apply_depreciation(Decimal::from(900));

        // Monthly rate = 1/18, on NBV 900 -> 50
        let monthly = asset.ddb_depreciation();
        assert_eq!(monthly, Decimal::from(50));
    }

    #[test]
    fn test_ddb_depreciation_respects_salvage() {
        let mut asset = FixedAsset::new(
            "FA-DDB3",
            "Printer",
            AssetClass::ComputerHardware,
            "1000",
            test_date(2024, 1, 1),
            Decimal::from(1800),
        )
        .with_useful_life_months(36)
        .with_salvage_value(Decimal::from(200));

        // Accumulate until NBV is barely above salvage
        // NBV = 1800 - 1590 = 210, salvage = 200
        asset.apply_depreciation(Decimal::from(1590));

        // DDB would compute 210 * (1/18) = 11.666..., but cap at NBV - salvage = 10
        let monthly = asset.ddb_depreciation();
        assert_eq!(monthly, Decimal::from(10));
    }

    // ---- Degressiv (German declining balance) tests ----

    #[test]
    fn test_degressiv_depreciation_initial() {
        // 10-year asset, 120000 EUR, no salvage
        // SL rate = 1/10 = 10%; Degressiv = min(3 * 10%, 30%) = 30%
        // Monthly: 120000 * 0.30 / 12 = 3000
        let asset = FixedAsset::new(
            "FA-DEG",
            "Maschine",
            AssetClass::MachineryEquipment,
            "DE01",
            test_date(2024, 1, 1),
            Decimal::from(120000),
        )
        .with_useful_life_months(120)
        .with_depreciation_method(DepreciationMethod::Degressiv);

        let monthly = asset.calculate_monthly_depreciation(test_date(2024, 2, 1));
        assert_eq!(monthly, Decimal::from(3000));
    }

    #[test]
    fn test_degressiv_rate_capped_at_30_percent() {
        // 5-year asset: SL rate = 1/5 = 20%; 3x = 60%. Capped at 30%.
        // Degressiv monthly: 6000 * 0.30 / 12 = 150
        // SL monthly on remaining: 6000 / 59 ≈ 101.69 (< 150)
        // → Degressiv wins at start
        let asset = FixedAsset::new(
            "FA-DEG2",
            "Fahrzeug",
            AssetClass::Vehicles,
            "DE01",
            test_date(2024, 1, 1),
            Decimal::from(6000),
        )
        .with_useful_life_months(60)
        .with_depreciation_method(DepreciationMethod::Degressiv);

        let monthly = asset.calculate_monthly_depreciation(test_date(2024, 2, 1));
        assert_eq!(monthly, Decimal::from(150));

        // For a 3-year asset, SL rate (33.3%) > Degressiv cap (30%),
        // so the switch to SL happens immediately.
        let short_asset = FixedAsset::new(
            "FA-DEG2S",
            "Server",
            AssetClass::ComputerHardware,
            "DE01",
            test_date(2024, 1, 1),
            Decimal::from(3600),
        )
        .with_useful_life_months(36)
        .with_depreciation_method(DepreciationMethod::Degressiv);

        // months_elapsed=1, remaining=35, SL = 3600/35 ≈ 102.86 > Degressiv 90
        let monthly_short = short_asset.calculate_monthly_depreciation(test_date(2024, 2, 1));
        assert!(
            monthly_short > Decimal::from(100),
            "SL should win for 3-year asset"
        );
    }

    #[test]
    fn test_degressiv_switches_to_straight_line() {
        // Degressiv should switch to SL when SL produces higher depreciation.
        // 10-year, 10000 EUR asset.
        // Degressiv rate = 30%, monthly = NBV * 0.30 / 12
        // SL on remaining = (NBV - salvage) / remaining_months
        let mut asset = FixedAsset::new(
            "FA-DEG3",
            "Fahrzeug",
            AssetClass::Vehicles,
            "DE01",
            test_date(2024, 1, 1),
            Decimal::from(10000),
        )
        .with_useful_life_months(120)
        .with_depreciation_method(DepreciationMethod::Degressiv);

        // Simulate significant depreciation: NBV = 1000, 24 months remaining
        asset.apply_depreciation(Decimal::from(9000));
        // NBV = 1000
        // Degressiv: 1000 * 0.30 / 12 = 25
        // SL on remaining: 1000 / 24 ≈ 41.67
        // Should use SL (41.67) since it's higher
        let dep = asset.calculate_monthly_depreciation(test_date(2032, 1, 1));
        // months_elapsed = 96, remaining = 120 - 96 = 24
        // SL = 1000/24 = 41.666...
        // Degressiv = 1000 * 0.30 / 12 = 25
        // max(25, 41.666) = 41.666...
        assert!(
            dep > Decimal::from(25),
            "Should switch to SL when it exceeds Degressiv"
        );
        assert!(dep < Decimal::from(42), "SL should be ~41.67");
    }

    #[test]
    fn test_gwg_field_default() {
        let asset = FixedAsset::new(
            "FA-GWG",
            "Keyboard",
            AssetClass::ComputerHardware,
            "DE01",
            test_date(2024, 1, 1),
            Decimal::from(200),
        );
        assert_eq!(asset.is_gwg, None, "is_gwg should default to None");
    }
}