datasynth-core 2.3.1

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
//! AR Invoice model.

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

use crate::models::subledger::{
    ClearingInfo, CurrencyAmount, DunningInfo, GLReference, PaymentTerms, SubledgerDocumentStatus,
    TaxInfo,
};

/// AR Invoice (customer invoice).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ARInvoice {
    /// Unique invoice number.
    pub invoice_number: String,
    /// Company code.
    pub company_code: String,
    /// Customer ID.
    pub customer_id: String,
    /// Customer name.
    pub customer_name: String,
    /// Invoice date.
    pub invoice_date: NaiveDate,
    /// Posting date.
    pub posting_date: NaiveDate,
    /// Due date.
    pub due_date: NaiveDate,
    /// Baseline date for payment terms.
    pub baseline_date: NaiveDate,
    /// Invoice type.
    pub invoice_type: ARInvoiceType,
    /// Invoice status.
    pub status: SubledgerDocumentStatus,
    /// Invoice lines.
    pub lines: Vec<ARInvoiceLine>,
    /// Net amount (before tax).
    pub net_amount: CurrencyAmount,
    /// Tax amount.
    pub tax_amount: CurrencyAmount,
    /// Gross amount (after tax).
    pub gross_amount: CurrencyAmount,
    /// Amount paid.
    pub amount_paid: Decimal,
    /// Amount remaining.
    pub amount_remaining: Decimal,
    /// Payment terms.
    pub payment_terms: PaymentTerms,
    /// Tax details.
    pub tax_details: Vec<TaxInfo>,
    /// GL reference.
    pub gl_reference: Option<GLReference>,
    /// Clearing information.
    pub clearing_info: Vec<ClearingInfo>,
    /// Dunning information.
    pub dunning_info: DunningInfo,
    /// Reference documents (sales order, delivery).
    pub reference_documents: Vec<ARDocumentReference>,
    /// Cost center.
    pub cost_center: Option<String>,
    /// Profit center.
    pub profit_center: Option<String>,
    /// Sales organization.
    pub sales_org: Option<String>,
    /// Distribution channel.
    pub distribution_channel: Option<String>,
    /// Division.
    pub division: Option<String>,
    /// Created timestamp.
    #[serde(with = "crate::serde_timestamp::utc")]
    pub created_at: DateTime<Utc>,
    /// Created by user.
    pub created_by: Option<String>,
    /// Last modified timestamp.
    #[serde(default, with = "crate::serde_timestamp::utc::option")]
    pub modified_at: Option<DateTime<Utc>>,
    /// Notes.
    pub notes: Option<String>,
}

impl ARInvoice {
    /// Creates a new AR invoice.
    pub fn new(
        invoice_number: String,
        company_code: String,
        customer_id: String,
        customer_name: String,
        invoice_date: NaiveDate,
        payment_terms: PaymentTerms,
        currency: String,
    ) -> Self {
        let due_date = payment_terms.calculate_due_date(invoice_date);

        Self {
            invoice_number,
            company_code,
            customer_id,
            customer_name,
            invoice_date,
            posting_date: invoice_date,
            due_date,
            baseline_date: invoice_date,
            invoice_type: ARInvoiceType::Standard,
            status: SubledgerDocumentStatus::Open,
            lines: Vec::new(),
            net_amount: CurrencyAmount::single_currency(Decimal::ZERO, currency.clone()),
            tax_amount: CurrencyAmount::single_currency(Decimal::ZERO, currency.clone()),
            gross_amount: CurrencyAmount::single_currency(Decimal::ZERO, currency),
            amount_paid: Decimal::ZERO,
            amount_remaining: Decimal::ZERO,
            payment_terms,
            tax_details: Vec::new(),
            gl_reference: None,
            clearing_info: Vec::new(),
            dunning_info: DunningInfo::default(),
            reference_documents: Vec::new(),
            cost_center: None,
            profit_center: None,
            sales_org: None,
            distribution_channel: None,
            division: None,
            created_at: Utc::now(),
            created_by: None,
            modified_at: None,
            notes: None,
        }
    }

    /// Adds an invoice line.
    pub fn add_line(&mut self, line: ARInvoiceLine) {
        self.lines.push(line);
        self.recalculate_totals();
    }

    /// Recalculates totals from lines.
    pub fn recalculate_totals(&mut self) {
        let net_total: Decimal = self.lines.iter().map(|l| l.net_amount).sum();
        let tax_total: Decimal = self.lines.iter().map(|l| l.tax_amount).sum();
        let gross_total = net_total + tax_total;

        self.net_amount.document_amount = net_total;
        self.net_amount.local_amount = net_total * self.net_amount.exchange_rate;
        self.tax_amount.document_amount = tax_total;
        self.tax_amount.local_amount = tax_total * self.tax_amount.exchange_rate;
        self.gross_amount.document_amount = gross_total;
        self.gross_amount.local_amount = gross_total * self.gross_amount.exchange_rate;
        self.amount_remaining = gross_total - self.amount_paid;
    }

    /// Applies a payment to the invoice.
    pub fn apply_payment(&mut self, amount: Decimal, clearing: ClearingInfo) {
        self.amount_paid += amount;
        self.amount_remaining = self.gross_amount.document_amount - self.amount_paid;
        self.clearing_info.push(clearing);

        self.status = if self.amount_remaining <= Decimal::ZERO {
            SubledgerDocumentStatus::Cleared
        } else {
            SubledgerDocumentStatus::PartiallyCleared
        };

        self.modified_at = Some(Utc::now());
    }

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

    /// Calculates days overdue.
    pub fn days_overdue(&self, as_of_date: NaiveDate) -> i64 {
        if self.is_overdue(as_of_date) {
            (as_of_date - self.due_date).num_days()
        } else {
            0
        }
    }

    /// Gets discount amount if paid by discount date.
    pub fn available_discount(&self, payment_date: NaiveDate) -> Decimal {
        self.payment_terms.calculate_discount(
            self.gross_amount.document_amount,
            payment_date,
            self.baseline_date,
        )
    }

    /// Sets the GL reference.
    pub fn set_gl_reference(&mut self, reference: GLReference) {
        self.gl_reference = Some(reference);
    }

    /// Adds a reference document.
    pub fn add_reference(&mut self, reference: ARDocumentReference) {
        self.reference_documents.push(reference);
    }

    /// Reverses the invoice.
    pub fn reverse(&mut self, reversal_date: NaiveDate, reason: String) {
        self.status = SubledgerDocumentStatus::Reversed;
        self.notes = Some(format!(
            "{}Reversed on {}: {}",
            self.notes
                .as_ref()
                .map(|n| format!("{n}. "))
                .unwrap_or_default(),
            reversal_date,
            reason
        ));
        self.modified_at = Some(Utc::now());
    }
}

/// Type of AR invoice.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum ARInvoiceType {
    /// Standard invoice.
    #[default]
    Standard,
    /// Down payment request.
    DownPaymentRequest,
    /// Recurring invoice.
    Recurring,
    /// Credit invoice (negative).
    CreditInvoice,
    /// Debit invoice (adjustment).
    DebitInvoice,
    /// Pro forma invoice.
    ProForma,
    /// Intercompany invoice.
    Intercompany,
}

/// AR invoice line item.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ARInvoiceLine {
    /// Line number.
    pub line_number: u32,
    /// Material/product ID.
    pub material_id: Option<String>,
    /// Description.
    pub description: String,
    /// Quantity.
    pub quantity: Decimal,
    /// Unit of measure.
    pub unit: String,
    /// Unit price.
    pub unit_price: Decimal,
    /// Net amount (quantity * unit_price).
    pub net_amount: Decimal,
    /// Tax code.
    pub tax_code: Option<String>,
    /// Tax rate.
    pub tax_rate: Decimal,
    /// Tax amount.
    pub tax_amount: Decimal,
    /// Gross amount.
    pub gross_amount: Decimal,
    /// Revenue account.
    pub revenue_account: String,
    /// Cost center.
    pub cost_center: Option<String>,
    /// Profit center.
    pub profit_center: Option<String>,
    /// Reference (sales order line).
    pub reference: Option<String>,
}

impl ARInvoiceLine {
    /// Creates a new invoice line.
    pub fn new(
        line_number: u32,
        description: String,
        quantity: Decimal,
        unit: String,
        unit_price: Decimal,
        revenue_account: String,
    ) -> Self {
        let net_amount = (quantity * unit_price).round_dp(2);
        Self {
            line_number,
            material_id: None,
            description,
            quantity,
            unit,
            unit_price,
            net_amount,
            tax_code: None,
            tax_rate: Decimal::ZERO,
            tax_amount: Decimal::ZERO,
            gross_amount: net_amount,
            revenue_account,
            cost_center: None,
            profit_center: None,
            reference: None,
        }
    }

    /// Sets tax information.
    pub fn with_tax(mut self, tax_code: String, tax_rate: Decimal) -> Self {
        self.tax_code = Some(tax_code);
        self.tax_rate = tax_rate;
        self.tax_amount = (self.net_amount * tax_rate / dec!(100)).round_dp(2);
        self.gross_amount = self.net_amount + self.tax_amount;
        self
    }

    /// Sets material ID.
    pub fn with_material(mut self, material_id: String) -> Self {
        self.material_id = Some(material_id);
        self
    }

    /// Sets cost/profit center.
    pub fn with_cost_center(mut self, cost_center: String, profit_center: Option<String>) -> Self {
        self.cost_center = Some(cost_center);
        self.profit_center = profit_center;
        self
    }
}

/// Reference to related documents.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ARDocumentReference {
    /// Reference document type.
    pub document_type: ARReferenceDocType,
    /// Document number.
    pub document_number: String,
    /// Document date.
    pub document_date: NaiveDate,
}

/// Type of reference document.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ARReferenceDocType {
    /// Sales order.
    SalesOrder,
    /// Delivery.
    Delivery,
    /// Contract.
    Contract,
    /// Quotation.
    Quotation,
    /// Return order.
    ReturnOrder,
}

/// Summary of open AR items for a customer.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomerARSummary {
    /// Customer ID.
    pub customer_id: String,
    /// Customer name.
    pub customer_name: String,
    /// Total open amount.
    pub total_open: Decimal,
    /// Total overdue amount.
    pub total_overdue: Decimal,
    /// Number of open invoices.
    pub open_invoice_count: u32,
    /// Number of overdue invoices.
    pub overdue_invoice_count: u32,
    /// Oldest open invoice date.
    pub oldest_open_date: Option<NaiveDate>,
    /// Credit limit.
    pub credit_limit: Option<Decimal>,
    /// Credit utilization percentage.
    pub credit_utilization: Option<Decimal>,
    /// Payment behavior score.
    pub payment_score: Option<Decimal>,
}

impl CustomerARSummary {
    /// Creates from a list of invoices.
    pub fn from_invoices(
        customer_id: String,
        customer_name: String,
        invoices: &[ARInvoice],
        as_of_date: NaiveDate,
        credit_limit: Option<Decimal>,
    ) -> Self {
        let open_invoices: Vec<_> = invoices
            .iter()
            .filter(|i| {
                i.customer_id == customer_id
                    && matches!(
                        i.status,
                        SubledgerDocumentStatus::Open | SubledgerDocumentStatus::PartiallyCleared
                    )
            })
            .collect();

        let total_open: Decimal = open_invoices.iter().map(|i| i.amount_remaining).sum();
        let overdue_invoices: Vec<_> = open_invoices
            .iter()
            .filter(|i| i.is_overdue(as_of_date))
            .collect();
        let total_overdue: Decimal = overdue_invoices.iter().map(|i| i.amount_remaining).sum();

        let oldest_open_date = open_invoices.iter().map(|i| i.invoice_date).min();

        let credit_utilization = credit_limit.map(|limit| {
            if limit > Decimal::ZERO {
                (total_open / limit * dec!(100)).round_dp(2)
            } else {
                Decimal::ZERO
            }
        });

        Self {
            customer_id,
            customer_name,
            total_open,
            total_overdue,
            open_invoice_count: open_invoices.len() as u32,
            overdue_invoice_count: overdue_invoices.len() as u32,
            oldest_open_date,
            credit_limit,
            credit_utilization,
            payment_score: None,
        }
    }
}

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

    fn create_test_invoice() -> ARInvoice {
        let mut invoice = ARInvoice::new(
            "INV001".to_string(),
            "1000".to_string(),
            "CUST001".to_string(),
            "Test Customer".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            PaymentTerms::net_30(),
            "USD".to_string(),
        );

        let line = ARInvoiceLine::new(
            1,
            "Product A".to_string(),
            dec!(10),
            "EA".to_string(),
            dec!(100),
            "4000".to_string(),
        )
        .with_tax("VAT".to_string(), dec!(20));

        invoice.add_line(line);
        invoice
    }

    #[test]
    fn test_invoice_totals() {
        let invoice = create_test_invoice();
        assert_eq!(invoice.net_amount.document_amount, dec!(1000));
        assert_eq!(invoice.tax_amount.document_amount, dec!(200));
        assert_eq!(invoice.gross_amount.document_amount, dec!(1200));
    }

    #[test]
    fn test_invoice_due_date() {
        let invoice = create_test_invoice();
        assert_eq!(
            invoice.due_date,
            NaiveDate::from_ymd_opt(2024, 2, 14).unwrap()
        );
    }

    #[test]
    fn test_invoice_overdue() {
        let invoice = create_test_invoice();
        let before_due = NaiveDate::from_ymd_opt(2024, 2, 10).unwrap();
        let after_due = NaiveDate::from_ymd_opt(2024, 2, 20).unwrap();

        assert!(!invoice.is_overdue(before_due));
        assert!(invoice.is_overdue(after_due));
        assert_eq!(invoice.days_overdue(after_due), 6);
    }

    #[test]
    fn test_apply_payment() {
        let mut invoice = create_test_invoice();
        let clearing = ClearingInfo {
            clearing_document: "PAY001".to_string(),
            clearing_date: NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
            clearing_amount: dec!(600),
            clearing_type: crate::models::subledger::ClearingType::Payment,
        };

        invoice.apply_payment(dec!(600), clearing);
        assert_eq!(invoice.amount_paid, dec!(600));
        assert_eq!(invoice.amount_remaining, dec!(600));
        assert_eq!(invoice.status, SubledgerDocumentStatus::PartiallyCleared);
    }
}