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
//! Common types shared across all subledgers.

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

/// Status of a subledger document.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum SubledgerDocumentStatus {
    /// Document is open/outstanding.
    #[default]
    Open,
    /// Document is partially cleared.
    PartiallyCleared,
    /// Document is fully cleared.
    Cleared,
    /// Document is reversed/cancelled.
    Reversed,
    /// Document is on hold.
    OnHold,
    /// Document is in dispute.
    InDispute,
}

/// Clearing information for a subledger document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClearingInfo {
    /// Document that cleared this item.
    pub clearing_document: String,
    /// Date of clearing.
    pub clearing_date: NaiveDate,
    /// Amount cleared.
    pub clearing_amount: Decimal,
    /// Clearing type.
    pub clearing_type: ClearingType,
}

/// Type of clearing transaction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ClearingType {
    /// Payment received/made.
    Payment,
    /// Credit/debit memo applied.
    Memo,
    /// Write-off.
    WriteOff,
    /// Netting between AR and AP.
    Netting,
    /// Manual clearing.
    Manual,
    /// Reversal.
    Reversal,
}

/// Reference to GL posting.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GLReference {
    /// Journal entry ID.
    pub journal_entry_id: String,
    /// Posting date in GL.
    pub posting_date: NaiveDate,
    /// GL account code.
    pub gl_account: String,
    /// Amount posted to GL.
    pub amount: Decimal,
    /// Debit or credit indicator.
    pub debit_credit: DebitCredit,
}

/// Debit or credit indicator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DebitCredit {
    Debit,
    Credit,
}

/// Tax information for a document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaxInfo {
    /// Tax code.
    pub tax_code: String,
    /// Tax rate percentage.
    pub tax_rate: Decimal,
    /// Tax base amount.
    pub tax_base: Decimal,
    /// Tax amount.
    pub tax_amount: Decimal,
    /// Tax jurisdiction.
    pub jurisdiction: Option<String>,
}

impl TaxInfo {
    /// Creates new tax info with calculated tax amount.
    pub fn new(tax_code: String, tax_rate: Decimal, tax_base: Decimal) -> Self {
        let tax_amount = (tax_base * tax_rate / dec!(100)).round_dp(2);
        Self {
            tax_code,
            tax_rate,
            tax_base,
            tax_amount,
            jurisdiction: None,
        }
    }

    /// Creates tax info with explicit tax amount.
    pub fn with_amount(
        tax_code: String,
        tax_rate: Decimal,
        tax_base: Decimal,
        tax_amount: Decimal,
    ) -> Self {
        Self {
            tax_code,
            tax_rate,
            tax_base,
            tax_amount,
            jurisdiction: None,
        }
    }

    /// Sets the tax jurisdiction.
    pub fn with_jurisdiction(mut self, jurisdiction: String) -> Self {
        self.jurisdiction = Some(jurisdiction);
        self
    }
}

/// Payment terms for AR/AP documents.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaymentTerms {
    /// Terms code (e.g., "NET30", "2/10NET30").
    pub terms_code: String,
    /// Description.
    pub description: String,
    /// Net due days from baseline date.
    pub net_due_days: u32,
    /// Discount percentage if paid early.
    pub discount_percent: Option<Decimal>,
    /// Days to qualify for discount.
    pub discount_days: Option<u32>,
    /// Second discount tier percentage.
    pub discount_percent_2: Option<Decimal>,
    /// Days for second discount tier.
    pub discount_days_2: Option<u32>,
}

impl PaymentTerms {
    /// Creates standard net terms.
    pub fn net(days: u32) -> Self {
        Self {
            terms_code: format!("NET{days}"),
            description: format!("Net {days} days"),
            net_due_days: days,
            discount_percent: None,
            discount_days: None,
            discount_percent_2: None,
            discount_days_2: None,
        }
    }

    /// Creates terms with early payment discount.
    pub fn with_discount(net_days: u32, discount_percent: Decimal, discount_days: u32) -> Self {
        Self {
            terms_code: format!("{discount_percent}/{discount_days}NET{net_days}"),
            description: format!(
                "{discount_percent}% discount if paid within {discount_days} days, net {net_days} days"
            ),
            net_due_days: net_days,
            discount_percent: Some(discount_percent),
            discount_days: Some(discount_days),
            discount_percent_2: None,
            discount_days_2: None,
        }
    }

    /// Common payment terms.
    pub fn net_30() -> Self {
        Self::net(30)
    }

    pub fn net_60() -> Self {
        Self::net(60)
    }

    pub fn net_90() -> Self {
        Self::net(90)
    }

    pub fn two_ten_net_30() -> Self {
        Self::with_discount(30, dec!(2), 10)
    }

    pub fn one_ten_net_30() -> Self {
        Self::with_discount(30, dec!(1), 10)
    }

    /// Calculates due date from baseline date.
    pub fn calculate_due_date(&self, baseline_date: NaiveDate) -> NaiveDate {
        baseline_date + chrono::Duration::days(self.net_due_days as i64)
    }

    /// Calculates discount due date.
    pub fn calculate_discount_date(&self, baseline_date: NaiveDate) -> Option<NaiveDate> {
        self.discount_days
            .map(|days| baseline_date + chrono::Duration::days(days as i64))
    }

    /// Calculates discount amount for a given base amount.
    pub fn calculate_discount(
        &self,
        base_amount: Decimal,
        payment_date: NaiveDate,
        baseline_date: NaiveDate,
    ) -> Decimal {
        if let (Some(discount_percent), Some(discount_days)) =
            (self.discount_percent, self.discount_days)
        {
            let discount_deadline = baseline_date + chrono::Duration::days(discount_days as i64);
            if payment_date <= discount_deadline {
                return (base_amount * discount_percent / dec!(100)).round_dp(2);
            }
        }
        Decimal::ZERO
    }
}

/// Reconciliation status between subledger and GL.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReconciliationStatus {
    /// Company code.
    pub company_code: String,
    /// GL control account.
    pub gl_account: String,
    /// Subledger type.
    pub subledger_type: SubledgerType,
    /// As-of date.
    pub as_of_date: NaiveDate,
    /// GL balance.
    pub gl_balance: Decimal,
    /// Subledger balance.
    pub subledger_balance: Decimal,
    /// Difference.
    pub difference: Decimal,
    /// Is reconciled (within tolerance).
    pub is_reconciled: bool,
    /// Reconciliation timestamp.
    #[serde(with = "crate::serde_timestamp::utc")]
    pub reconciled_at: DateTime<Utc>,
    /// Unreconciled items.
    pub unreconciled_items: Vec<UnreconciledItem>,
}

impl ReconciliationStatus {
    /// Creates new reconciliation status.
    pub fn new(
        company_code: String,
        gl_account: String,
        subledger_type: SubledgerType,
        as_of_date: NaiveDate,
        gl_balance: Decimal,
        subledger_balance: Decimal,
        tolerance: Decimal,
    ) -> Self {
        let difference = gl_balance - subledger_balance;
        let is_reconciled = difference.abs() <= tolerance;

        Self {
            company_code,
            gl_account,
            subledger_type,
            as_of_date,
            gl_balance,
            subledger_balance,
            difference,
            is_reconciled,
            reconciled_at: Utc::now(),
            unreconciled_items: Vec::new(),
        }
    }

    /// Adds an unreconciled item.
    pub fn add_unreconciled_item(&mut self, item: UnreconciledItem) {
        self.unreconciled_items.push(item);
    }
}

/// Type of subledger.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SubledgerType {
    /// Accounts Receivable.
    AR,
    /// Accounts Payable.
    AP,
    /// Fixed Assets.
    FA,
    /// Inventory.
    Inventory,
}

/// An item that doesn't reconcile.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnreconciledItem {
    /// Document number.
    pub document_number: String,
    /// Document type.
    pub document_type: String,
    /// Amount in subledger.
    pub subledger_amount: Decimal,
    /// Amount in GL.
    pub gl_amount: Decimal,
    /// Difference.
    pub difference: Decimal,
    /// Reason for discrepancy.
    pub reason: Option<String>,
}

/// Currency amount with original and local currency.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CurrencyAmount {
    /// Amount in document currency.
    pub document_amount: Decimal,
    /// Document currency code.
    pub document_currency: String,
    /// Amount in local currency.
    pub local_amount: Decimal,
    /// Local currency code.
    pub local_currency: String,
    /// Exchange rate used.
    pub exchange_rate: Decimal,
}

impl CurrencyAmount {
    /// Creates amount in single currency.
    pub fn single_currency(amount: Decimal, currency: String) -> Self {
        Self {
            document_amount: amount,
            document_currency: currency.clone(),
            local_amount: amount,
            local_currency: currency,
            exchange_rate: Decimal::ONE,
        }
    }

    /// Creates amount with currency conversion.
    pub fn with_conversion(
        document_amount: Decimal,
        document_currency: String,
        local_currency: String,
        exchange_rate: Decimal,
    ) -> Self {
        let local_amount = (document_amount * exchange_rate).round_dp(2);
        Self {
            document_amount,
            document_currency,
            local_amount,
            local_currency,
            exchange_rate,
        }
    }
}

/// Baseline date type for payment terms.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum BaselineDateType {
    /// Document date.
    #[default]
    DocumentDate,
    /// Posting date.
    PostingDate,
    /// Entry date.
    EntryDate,
    /// Goods receipt date.
    GoodsReceiptDate,
    /// Custom date.
    CustomDate,
}

/// Dunning information for AR items.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DunningInfo {
    /// Current dunning level (0 = not dunned).
    pub dunning_level: u8,
    /// Maximum dunning level reached.
    pub max_dunning_level: u8,
    /// Last dunning date.
    pub last_dunning_date: Option<NaiveDate>,
    /// Last dunning run ID.
    pub last_dunning_run: Option<String>,
    /// Is blocked for dunning.
    pub dunning_blocked: bool,
    /// Block reason.
    pub block_reason: Option<String>,
}

impl DunningInfo {
    /// Advances to next dunning level.
    pub fn advance_level(&mut self, dunning_date: NaiveDate, run_id: String) {
        if !self.dunning_blocked {
            self.dunning_level += 1;
            if self.dunning_level > self.max_dunning_level {
                self.max_dunning_level = self.dunning_level;
            }
            self.last_dunning_date = Some(dunning_date);
            self.last_dunning_run = Some(run_id);
        }
    }

    /// Blocks dunning.
    pub fn block(&mut self, reason: String) {
        self.dunning_blocked = true;
        self.block_reason = Some(reason);
    }

    /// Unblocks dunning.
    pub fn unblock(&mut self) {
        self.dunning_blocked = false;
        self.block_reason = None;
    }
}

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

    #[test]
    fn test_payment_terms_due_date() {
        let terms = PaymentTerms::net_30();
        let baseline = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();
        let due_date = terms.calculate_due_date(baseline);
        assert_eq!(due_date, NaiveDate::from_ymd_opt(2024, 2, 14).unwrap());
    }

    #[test]
    fn test_payment_terms_discount() {
        let terms = PaymentTerms::two_ten_net_30();
        let baseline = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();
        let amount = dec!(1000);

        // Payment within discount period
        let early_payment = NaiveDate::from_ymd_opt(2024, 1, 20).unwrap();
        let discount = terms.calculate_discount(amount, early_payment, baseline);
        assert_eq!(discount, dec!(20)); // 2% of 1000

        // Payment after discount period
        let late_payment = NaiveDate::from_ymd_opt(2024, 2, 1).unwrap();
        let no_discount = terms.calculate_discount(amount, late_payment, baseline);
        assert_eq!(no_discount, Decimal::ZERO);
    }

    #[test]
    fn test_tax_info() {
        let tax = TaxInfo::new("VAT".to_string(), dec!(20), dec!(1000));
        assert_eq!(tax.tax_amount, dec!(200));
    }

    #[test]
    fn test_currency_conversion() {
        let amount = CurrencyAmount::with_conversion(
            dec!(1000),
            "EUR".to_string(),
            "USD".to_string(),
            dec!(1.10),
        );
        assert_eq!(amount.local_amount, dec!(1100));
    }
}