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
//! Customer Invoice document model.
//!
//! Represents customer invoices (billing documents) in the O2C (Order-to-Cash) process flow.
//! Customer invoices create accounting entries: DR Accounts Receivable, CR Revenue.

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

use super::{
    DocumentHeader, DocumentLineItem, DocumentReference, DocumentStatus, DocumentType,
    ReferenceType,
};

/// Customer Invoice type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum CustomerInvoiceType {
    /// Standard invoice
    #[default]
    Standard,
    /// Credit memo
    CreditMemo,
    /// Debit memo
    DebitMemo,
    /// Pro forma invoice
    ProForma,
    /// Down payment request
    DownPaymentRequest,
    /// Final invoice (settling down payment)
    FinalInvoice,
    /// Intercompany invoice
    Intercompany,
}

impl CustomerInvoiceType {
    /// Check if this type increases AR (debit).
    pub fn is_debit(&self) -> bool {
        matches!(
            self,
            Self::Standard
                | Self::DebitMemo
                | Self::DownPaymentRequest
                | Self::FinalInvoice
                | Self::Intercompany
        )
    }

    /// Check if this type decreases AR (credit).
    pub fn is_credit(&self) -> bool {
        matches!(self, Self::CreditMemo)
    }

    /// Check if this creates revenue.
    pub fn creates_revenue(&self) -> bool {
        matches!(
            self,
            Self::Standard | Self::FinalInvoice | Self::Intercompany
        )
    }
}

/// Customer Invoice payment status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum InvoicePaymentStatus {
    /// Open - not paid
    #[default]
    Open,
    /// Partially paid
    PartiallyPaid,
    /// Fully paid
    Paid,
    /// Cleared (matched and closed)
    Cleared,
    /// Written off
    WrittenOff,
    /// In dispute
    InDispute,
    /// Sent to collection
    InCollection,
}

/// Customer Invoice line item.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomerInvoiceItem {
    /// Base line item fields
    #[serde(flatten)]
    pub base: DocumentLineItem,

    /// Reference sales order number
    pub sales_order_id: Option<String>,

    /// Reference SO item
    pub so_item: Option<u16>,

    /// Reference delivery number
    pub delivery_id: Option<String>,

    /// Reference delivery item
    pub delivery_item: Option<u16>,

    /// Revenue account (override from material)
    pub revenue_account: Option<String>,

    /// COGS account (for statistical tracking)
    pub cogs_account: Option<String>,

    /// COGS amount (for margin calculation)
    pub cogs_amount: Decimal,

    /// Discount amount
    pub discount_amount: Decimal,

    /// Is this a service item?
    pub is_service: bool,

    /// Returns reference (if credit memo for returns)
    pub returns_reference: Option<String>,
}

impl CustomerInvoiceItem {
    /// Create a new customer invoice item.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        line_number: u16,
        description: impl Into<String>,
        quantity: Decimal,
        unit_price: Decimal,
    ) -> Self {
        Self {
            base: DocumentLineItem::new(line_number, description, quantity, unit_price),
            sales_order_id: None,
            so_item: None,
            delivery_id: None,
            delivery_item: None,
            revenue_account: None,
            cogs_account: None,
            cogs_amount: Decimal::ZERO,
            discount_amount: Decimal::ZERO,
            is_service: false,
            returns_reference: None,
        }
    }

    /// Create from delivery reference.
    #[allow(clippy::too_many_arguments)]
    pub fn from_delivery(
        line_number: u16,
        description: impl Into<String>,
        quantity: Decimal,
        unit_price: Decimal,
        delivery_id: impl Into<String>,
        delivery_item: u16,
    ) -> Self {
        let mut item = Self::new(line_number, description, quantity, unit_price);
        item.delivery_id = Some(delivery_id.into());
        item.delivery_item = Some(delivery_item);
        item
    }

    /// Set material.
    pub fn with_material(mut self, material_id: impl Into<String>) -> Self {
        self.base = self.base.with_material(material_id);
        self
    }

    /// Set sales order reference.
    pub fn with_sales_order(mut self, so_id: impl Into<String>, so_item: u16) -> Self {
        self.sales_order_id = Some(so_id.into());
        self.so_item = Some(so_item);
        self
    }

    /// Set COGS amount.
    pub fn with_cogs(mut self, cogs: Decimal) -> Self {
        self.cogs_amount = cogs;
        self
    }

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

    /// Set as service item.
    pub fn as_service(mut self) -> Self {
        self.is_service = true;
        self
    }

    /// Set discount.
    pub fn with_discount(mut self, discount: Decimal) -> Self {
        self.discount_amount = discount;
        self
    }

    /// Calculate gross margin.
    pub fn gross_margin(&self) -> Decimal {
        if self.base.net_amount == Decimal::ZERO {
            return Decimal::ZERO;
        }
        ((self.base.net_amount - self.cogs_amount) / self.base.net_amount * Decimal::from(100))
            .round_dp(2)
    }
}

/// Customer Invoice document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomerInvoice {
    /// Document header
    pub header: DocumentHeader,

    /// Invoice type
    pub invoice_type: CustomerInvoiceType,

    /// Line items
    pub items: Vec<CustomerInvoiceItem>,

    /// Customer ID
    pub customer_id: String,

    /// Bill-to party (if different)
    pub bill_to: Option<String>,

    /// Payer (if different)
    pub payer: Option<String>,

    /// Sales organization
    pub sales_org: String,

    /// Distribution channel
    pub distribution_channel: String,

    /// Division
    pub division: String,

    /// Total net amount
    pub total_net_amount: Decimal,

    /// Total tax amount
    pub total_tax_amount: Decimal,

    /// Total gross amount
    pub total_gross_amount: Decimal,

    /// Total discount amount
    pub total_discount: Decimal,

    /// Total COGS
    pub total_cogs: Decimal,

    /// Payment terms
    pub payment_terms: String,

    /// Due date
    pub due_date: NaiveDate,

    /// Cash discount date 1
    pub discount_date_1: Option<NaiveDate>,

    /// Cash discount percent 1
    pub discount_percent_1: Option<Decimal>,

    /// Cash discount date 2
    pub discount_date_2: Option<NaiveDate>,

    /// Cash discount percent 2
    pub discount_percent_2: Option<Decimal>,

    /// Amount paid
    pub amount_paid: Decimal,

    /// Amount open (remaining)
    pub amount_open: Decimal,

    /// Payment status
    pub payment_status: InvoicePaymentStatus,

    /// Reference sales order (primary)
    pub sales_order_id: Option<String>,

    /// Reference delivery (primary)
    pub delivery_id: Option<String>,

    /// External invoice number (for customer)
    pub external_reference: Option<String>,

    /// Customer PO number
    pub customer_po_number: Option<String>,

    /// Is invoice posted?
    pub is_posted: bool,

    /// Is invoice printed/sent?
    pub is_output_complete: bool,

    /// Is this an intercompany invoice?
    pub is_intercompany: bool,

    /// Intercompany partner (company code)
    pub ic_partner: Option<String>,

    /// Dispute reason (if in dispute)
    pub dispute_reason: Option<String>,

    /// Write-off amount
    pub write_off_amount: Decimal,

    /// Write-off reason
    pub write_off_reason: Option<String>,

    /// Dunning level (0 = not dunned)
    pub dunning_level: u8,

    /// Last dunning date
    pub last_dunning_date: Option<NaiveDate>,

    /// Is invoice cancelled/reversed?
    pub is_cancelled: bool,

    /// Cancellation invoice reference
    pub cancellation_invoice: Option<String>,

    /// Customer display name (denormalized, DS-011)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub customer_name: Option<String>,
}

impl CustomerInvoice {
    /// Create a new customer invoice.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        invoice_id: impl Into<String>,
        company_code: impl Into<String>,
        customer_id: impl Into<String>,
        fiscal_year: u16,
        fiscal_period: u8,
        document_date: NaiveDate,
        due_date: NaiveDate,
        created_by: impl Into<String>,
    ) -> Self {
        let header = DocumentHeader::new(
            invoice_id,
            DocumentType::CustomerInvoice,
            company_code,
            fiscal_year,
            fiscal_period,
            document_date,
            created_by,
        )
        .with_currency("USD");

        Self {
            header,
            invoice_type: CustomerInvoiceType::Standard,
            items: Vec::new(),
            customer_id: customer_id.into(),
            bill_to: None,
            payer: None,
            sales_org: "1000".to_string(),
            distribution_channel: "10".to_string(),
            division: "00".to_string(),
            total_net_amount: Decimal::ZERO,
            total_tax_amount: Decimal::ZERO,
            total_gross_amount: Decimal::ZERO,
            total_discount: Decimal::ZERO,
            total_cogs: Decimal::ZERO,
            payment_terms: "NET30".to_string(),
            due_date,
            discount_date_1: None,
            discount_percent_1: None,
            discount_date_2: None,
            discount_percent_2: None,
            amount_paid: Decimal::ZERO,
            amount_open: Decimal::ZERO,
            payment_status: InvoicePaymentStatus::Open,
            sales_order_id: None,
            delivery_id: None,
            external_reference: None,
            customer_po_number: None,
            is_posted: false,
            is_output_complete: false,
            is_intercompany: false,
            ic_partner: None,
            dispute_reason: None,
            write_off_amount: Decimal::ZERO,
            write_off_reason: None,
            dunning_level: 0,
            last_dunning_date: None,
            is_cancelled: false,
            cancellation_invoice: None,
            customer_name: None,
        }
    }

    /// Create from delivery reference.
    #[allow(clippy::too_many_arguments)]
    pub fn from_delivery(
        invoice_id: impl Into<String>,
        company_code: impl Into<String>,
        delivery_id: impl Into<String>,
        customer_id: impl Into<String>,
        fiscal_year: u16,
        fiscal_period: u8,
        document_date: NaiveDate,
        due_date: NaiveDate,
        created_by: impl Into<String>,
    ) -> Self {
        let dlv_id = delivery_id.into();
        let mut invoice = Self::new(
            invoice_id,
            company_code,
            customer_id,
            fiscal_year,
            fiscal_period,
            document_date,
            due_date,
            created_by,
        );
        invoice.delivery_id = Some(dlv_id.clone());

        // Add reference to delivery
        invoice.header.add_reference(DocumentReference::new(
            DocumentType::Delivery,
            dlv_id,
            DocumentType::CustomerInvoice,
            invoice.header.document_id.clone(),
            ReferenceType::FollowOn,
            invoice.header.company_code.clone(),
            document_date,
        ));

        invoice
    }

    /// Create a credit memo.
    pub fn credit_memo(
        invoice_id: impl Into<String>,
        company_code: impl Into<String>,
        customer_id: impl Into<String>,
        fiscal_year: u16,
        fiscal_period: u8,
        document_date: NaiveDate,
        created_by: impl Into<String>,
    ) -> Self {
        let mut invoice = Self::new(
            invoice_id,
            company_code,
            customer_id,
            fiscal_year,
            fiscal_period,
            document_date,
            document_date, // Due immediately
            created_by,
        );
        invoice.invoice_type = CustomerInvoiceType::CreditMemo;
        invoice.header.document_type = DocumentType::CreditMemo;
        invoice
    }

    /// Set invoice type.
    pub fn with_invoice_type(mut self, invoice_type: CustomerInvoiceType) -> Self {
        self.invoice_type = invoice_type;
        self
    }

    /// Set sales organization.
    pub fn with_sales_org(
        mut self,
        sales_org: impl Into<String>,
        dist_channel: impl Into<String>,
        division: impl Into<String>,
    ) -> Self {
        self.sales_org = sales_org.into();
        self.distribution_channel = dist_channel.into();
        self.division = division.into();
        self
    }

    /// Set partner functions.
    pub fn with_partners(mut self, bill_to: impl Into<String>, payer: impl Into<String>) -> Self {
        self.bill_to = Some(bill_to.into());
        self.payer = Some(payer.into());
        self
    }

    /// Set payment terms with cash discount.
    pub fn with_payment_terms(
        mut self,
        terms: impl Into<String>,
        discount_days_1: Option<u16>,
        discount_percent_1: Option<Decimal>,
    ) -> Self {
        self.payment_terms = terms.into();
        if let (Some(days), Some(pct)) = (discount_days_1, discount_percent_1) {
            self.discount_date_1 =
                Some(self.header.document_date + chrono::Duration::days(days as i64));
            self.discount_percent_1 = Some(pct);
        }
        self
    }

    /// Set customer PO reference.
    pub fn with_customer_po(mut self, po_number: impl Into<String>) -> Self {
        self.customer_po_number = Some(po_number.into());
        self
    }

    /// Set as intercompany.
    pub fn as_intercompany(mut self, partner_company: impl Into<String>) -> Self {
        self.is_intercompany = true;
        self.ic_partner = Some(partner_company.into());
        self.invoice_type = CustomerInvoiceType::Intercompany;
        self
    }

    /// Add a line item.
    pub fn add_item(&mut self, item: CustomerInvoiceItem) {
        self.items.push(item);
        self.recalculate_totals();
    }

    /// Recalculate totals.
    pub fn recalculate_totals(&mut self) {
        self.total_net_amount = self.items.iter().map(|i| i.base.net_amount).sum();
        self.total_tax_amount = self.items.iter().map(|i| i.base.tax_amount).sum();
        self.total_gross_amount = self.total_net_amount + self.total_tax_amount;
        self.total_discount = self.items.iter().map(|i| i.discount_amount).sum();
        self.total_cogs = self.items.iter().map(|i| i.cogs_amount).sum();
        self.amount_open = self.total_gross_amount - self.amount_paid - self.write_off_amount;
    }

    /// Post the invoice.
    pub fn post(&mut self, user: impl Into<String>, posting_date: NaiveDate) {
        self.is_posted = true;
        self.header.posting_date = Some(posting_date);
        self.header.update_status(DocumentStatus::Posted, user);
        self.recalculate_totals();
    }

    /// Record a payment.
    pub fn record_payment(&mut self, amount: Decimal, discount_taken: Decimal) {
        self.amount_paid += amount + discount_taken;
        self.amount_open = self.total_gross_amount - self.amount_paid - self.write_off_amount;

        if self.amount_open <= Decimal::ZERO {
            self.payment_status = InvoicePaymentStatus::Paid;
        } else if self.amount_paid > Decimal::ZERO {
            self.payment_status = InvoicePaymentStatus::PartiallyPaid;
        }
    }

    /// Clear the invoice.
    pub fn clear(&mut self) {
        self.payment_status = InvoicePaymentStatus::Cleared;
        self.amount_open = Decimal::ZERO;
        self.header.update_status(DocumentStatus::Cleared, "SYSTEM");
    }

    /// Put invoice in dispute.
    pub fn dispute(&mut self, reason: impl Into<String>) {
        self.payment_status = InvoicePaymentStatus::InDispute;
        self.dispute_reason = Some(reason.into());
    }

    /// Resolve dispute.
    pub fn resolve_dispute(&mut self) {
        self.dispute_reason = None;
        if self.amount_open > Decimal::ZERO {
            self.payment_status = if self.amount_paid > Decimal::ZERO {
                InvoicePaymentStatus::PartiallyPaid
            } else {
                InvoicePaymentStatus::Open
            };
        } else {
            self.payment_status = InvoicePaymentStatus::Paid;
        }
    }

    /// Write off remaining amount.
    pub fn write_off(&mut self, amount: Decimal, reason: impl Into<String>) {
        self.write_off_amount = amount;
        self.write_off_reason = Some(reason.into());
        self.amount_open = self.total_gross_amount - self.amount_paid - self.write_off_amount;

        if self.amount_open <= Decimal::ZERO {
            self.payment_status = InvoicePaymentStatus::WrittenOff;
        }
    }

    /// Record dunning.
    pub fn record_dunning(&mut self, dunning_date: NaiveDate) {
        self.dunning_level += 1;
        self.last_dunning_date = Some(dunning_date);

        if self.dunning_level >= 4 {
            self.payment_status = InvoicePaymentStatus::InCollection;
        }
    }

    /// Cancel the invoice.
    pub fn cancel(&mut self, user: impl Into<String>, cancellation_invoice: impl Into<String>) {
        self.is_cancelled = true;
        self.cancellation_invoice = Some(cancellation_invoice.into());
        self.header.update_status(DocumentStatus::Cancelled, user);
    }

    /// Check if invoice is overdue.
    pub fn is_overdue(&self, as_of_date: NaiveDate) -> bool {
        self.payment_status == InvoicePaymentStatus::Open && as_of_date > self.due_date
    }

    /// Days past due.
    pub fn days_past_due(&self, as_of_date: NaiveDate) -> i64 {
        if as_of_date <= self.due_date {
            0
        } else {
            (as_of_date - self.due_date).num_days()
        }
    }

    /// Get aging bucket.
    pub fn aging_bucket(&self, as_of_date: NaiveDate) -> AgingBucket {
        let days = self.days_past_due(as_of_date);
        match days {
            d if d <= 0 => AgingBucket::Current,
            1..=30 => AgingBucket::Days1To30,
            31..=60 => AgingBucket::Days31To60,
            61..=90 => AgingBucket::Days61To90,
            _ => AgingBucket::Over90,
        }
    }

    /// Cash discount available.
    pub fn cash_discount_available(&self, as_of_date: NaiveDate) -> Decimal {
        if let (Some(date1), Some(pct1)) = (self.discount_date_1, self.discount_percent_1) {
            if as_of_date <= date1 {
                return self.amount_open * pct1 / Decimal::from(100);
            }
        }
        if let (Some(date2), Some(pct2)) = (self.discount_date_2, self.discount_percent_2) {
            if as_of_date <= date2 {
                return self.amount_open * pct2 / Decimal::from(100);
            }
        }
        Decimal::ZERO
    }

    /// Calculate gross margin.
    pub fn gross_margin(&self) -> Decimal {
        if self.total_net_amount == Decimal::ZERO {
            return Decimal::ZERO;
        }
        ((self.total_net_amount - self.total_cogs) / self.total_net_amount * Decimal::from(100))
            .round_dp(2)
    }

    /// Generate GL entries.
    /// DR Accounts Receivable, CR Revenue, CR Tax Payable
    pub fn generate_gl_entries(&self) -> Vec<(String, Decimal, Decimal)> {
        let mut entries = Vec::new();

        let sign = if self.invoice_type.is_debit() { 1 } else { -1 };

        // DR AR (or CR for credit memo)
        let ar_account = "120000".to_string();
        if sign > 0 {
            entries.push((ar_account, self.total_gross_amount, Decimal::ZERO));
        } else {
            entries.push((ar_account, Decimal::ZERO, self.total_gross_amount));
        }

        // CR Revenue per item (or DR for credit memo)
        for item in &self.items {
            let revenue_account = item
                .revenue_account
                .clone()
                .or_else(|| item.base.gl_account.clone())
                .unwrap_or_else(|| "400000".to_string());

            if sign > 0 {
                entries.push((revenue_account, Decimal::ZERO, item.base.net_amount));
            } else {
                entries.push((revenue_account, item.base.net_amount, Decimal::ZERO));
            }
        }

        // CR Tax (or DR for credit memo)
        if self.total_tax_amount > Decimal::ZERO {
            let tax_account = "220000".to_string();
            if sign > 0 {
                entries.push((tax_account, Decimal::ZERO, self.total_tax_amount));
            } else {
                entries.push((tax_account, self.total_tax_amount, Decimal::ZERO));
            }
        }

        entries
    }
}

/// AR aging bucket.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AgingBucket {
    /// Not yet due
    Current,
    /// 1-30 days past due
    Days1To30,
    /// 31-60 days past due
    Days31To60,
    /// 61-90 days past due
    Days61To90,
    /// Over 90 days past due
    Over90,
}

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

    #[test]
    fn test_customer_invoice_creation() {
        let invoice = CustomerInvoice::new(
            "CI-1000-0000000001",
            "1000",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            "JSMITH",
        );

        assert_eq!(invoice.customer_id, "C-000001");
        assert_eq!(invoice.payment_status, InvoicePaymentStatus::Open);
    }

    #[test]
    fn test_customer_invoice_from_delivery() {
        let invoice = CustomerInvoice::from_delivery(
            "CI-1000-0000000001",
            "1000",
            "DLV-1000-0000000001",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            "JSMITH",
        );

        assert_eq!(invoice.delivery_id, Some("DLV-1000-0000000001".to_string()));
        assert_eq!(invoice.header.document_references.len(), 1);
    }

    #[test]
    fn test_invoice_items() {
        let mut invoice = CustomerInvoice::new(
            "CI-1000-0000000001",
            "1000",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            "JSMITH",
        );

        let item = CustomerInvoiceItem::from_delivery(
            1,
            "Product A",
            Decimal::from(100),
            Decimal::from(50),
            "DLV-1000-0000000001",
            1,
        )
        .with_material("MAT-001")
        .with_cogs(Decimal::from(3000));

        invoice.add_item(item);

        assert_eq!(invoice.total_net_amount, Decimal::from(5000));
        assert_eq!(invoice.total_cogs, Decimal::from(3000));
        assert_eq!(invoice.gross_margin(), Decimal::from(40)); // (5000-3000)/5000 * 100 = 40%
    }

    #[test]
    fn test_payment_recording() {
        let mut invoice = CustomerInvoice::new(
            "CI-1000-0000000001",
            "1000",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            "JSMITH",
        );

        let item = CustomerInvoiceItem::new(1, "Product", Decimal::from(10), Decimal::from(100));
        invoice.add_item(item);
        invoice.post("BILLING", NaiveDate::from_ymd_opt(2024, 1, 15).unwrap());

        assert_eq!(invoice.amount_open, Decimal::from(1000));

        // Partial payment
        invoice.record_payment(Decimal::from(500), Decimal::ZERO);
        assert_eq!(invoice.amount_paid, Decimal::from(500));
        assert_eq!(invoice.amount_open, Decimal::from(500));
        assert_eq!(invoice.payment_status, InvoicePaymentStatus::PartiallyPaid);

        // Final payment
        invoice.record_payment(Decimal::from(500), Decimal::ZERO);
        assert_eq!(invoice.payment_status, InvoicePaymentStatus::Paid);
    }

    #[test]
    fn test_cash_discount() {
        let mut invoice = CustomerInvoice::new(
            "CI-1000-0000000001",
            "1000",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            "JSMITH",
        )
        .with_payment_terms("2/10 NET 30", Some(10), Some(Decimal::from(2)));

        let item = CustomerInvoiceItem::new(1, "Product", Decimal::from(10), Decimal::from(100));
        invoice.add_item(item);
        invoice.post("BILLING", NaiveDate::from_ymd_opt(2024, 1, 15).unwrap());

        // Within discount period
        let discount =
            invoice.cash_discount_available(NaiveDate::from_ymd_opt(2024, 1, 20).unwrap());
        assert_eq!(discount, Decimal::from(20)); // 2% of 1000

        // After discount period
        let discount =
            invoice.cash_discount_available(NaiveDate::from_ymd_opt(2024, 1, 30).unwrap());
        assert_eq!(discount, Decimal::ZERO);
    }

    #[test]
    fn test_aging() {
        let invoice = CustomerInvoice::new(
            "CI-1000-0000000001",
            "1000",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            "JSMITH",
        );

        // Not overdue
        assert!(!invoice.is_overdue(NaiveDate::from_ymd_opt(2024, 2, 14).unwrap()));
        assert_eq!(
            invoice.aging_bucket(NaiveDate::from_ymd_opt(2024, 2, 14).unwrap()),
            AgingBucket::Current
        );

        // 15 days overdue
        assert!(invoice.is_overdue(NaiveDate::from_ymd_opt(2024, 3, 1).unwrap()));
        assert_eq!(
            invoice.aging_bucket(NaiveDate::from_ymd_opt(2024, 3, 1).unwrap()),
            AgingBucket::Days1To30
        );

        // 45 days overdue
        assert_eq!(
            invoice.aging_bucket(NaiveDate::from_ymd_opt(2024, 4, 1).unwrap()),
            AgingBucket::Days31To60
        );

        // 100 days overdue
        assert_eq!(
            invoice.aging_bucket(NaiveDate::from_ymd_opt(2024, 5, 25).unwrap()),
            AgingBucket::Over90
        );
    }

    #[test]
    fn test_gl_entry_generation() {
        let mut invoice = CustomerInvoice::new(
            "CI-1000-0000000001",
            "1000",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            "JSMITH",
        );

        let mut item =
            CustomerInvoiceItem::new(1, "Product", Decimal::from(10), Decimal::from(100));
        item.base.tax_amount = Decimal::from(100);
        invoice.add_item(item);
        invoice.recalculate_totals();

        let entries = invoice.generate_gl_entries();
        assert_eq!(entries.len(), 3);

        // DR AR
        assert_eq!(entries[0].0, "120000");
        assert_eq!(entries[0].1, Decimal::from(1100)); // 1000 net + 100 tax

        // CR Revenue
        assert_eq!(entries[1].0, "400000");
        assert_eq!(entries[1].2, Decimal::from(1000));

        // CR Tax
        assert_eq!(entries[2].0, "220000");
        assert_eq!(entries[2].2, Decimal::from(100));
    }

    #[test]
    fn test_credit_memo_gl_entries() {
        let mut invoice = CustomerInvoice::credit_memo(
            "CM-1000-0000000001",
            "1000",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            "JSMITH",
        );

        let item = CustomerInvoiceItem::new(1, "Return", Decimal::from(5), Decimal::from(100));
        invoice.add_item(item);

        let entries = invoice.generate_gl_entries();

        // CR AR (credit reduces AR)
        assert_eq!(entries[0].0, "120000");
        assert_eq!(entries[0].2, Decimal::from(500)); // Credit side

        // DR Revenue (credit reduces revenue)
        assert_eq!(entries[1].0, "400000");
        assert_eq!(entries[1].1, Decimal::from(500)); // Debit side
    }

    #[test]
    fn test_write_off() {
        let mut invoice = CustomerInvoice::new(
            "CI-1000-0000000001",
            "1000",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            "JSMITH",
        );

        let item = CustomerInvoiceItem::new(1, "Product", Decimal::from(10), Decimal::from(100));
        invoice.add_item(item);
        invoice.post("BILLING", NaiveDate::from_ymd_opt(2024, 1, 15).unwrap());

        invoice.record_payment(Decimal::from(900), Decimal::ZERO);
        invoice.write_off(Decimal::from(100), "Small balance write-off");

        assert_eq!(invoice.write_off_amount, Decimal::from(100));
        assert_eq!(invoice.amount_open, Decimal::ZERO);
        assert_eq!(invoice.payment_status, InvoicePaymentStatus::WrittenOff);
    }

    #[test]
    fn test_dunning() {
        let mut invoice = CustomerInvoice::new(
            "CI-1000-0000000001",
            "1000",
            "C-000001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            "JSMITH",
        );

        invoice.record_dunning(NaiveDate::from_ymd_opt(2024, 2, 20).unwrap());
        assert_eq!(invoice.dunning_level, 1);

        invoice.record_dunning(NaiveDate::from_ymd_opt(2024, 3, 5).unwrap());
        invoice.record_dunning(NaiveDate::from_ymd_opt(2024, 3, 20).unwrap());
        invoice.record_dunning(NaiveDate::from_ymd_opt(2024, 4, 5).unwrap());

        assert_eq!(invoice.dunning_level, 4);
        assert_eq!(invoice.payment_status, InvoicePaymentStatus::InCollection);
    }
}