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
//! AR Receipt (customer payment) model.

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

use crate::models::subledger::{CurrencyAmount, GLReference, SubledgerDocumentStatus};

/// AR Receipt (payment from customer).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ARReceipt {
    /// Unique receipt number.
    pub receipt_number: String,
    /// Company code.
    pub company_code: String,
    /// Customer ID.
    pub customer_id: String,
    /// Customer name.
    pub customer_name: String,
    /// Receipt date.
    pub receipt_date: NaiveDate,
    /// Posting date.
    pub posting_date: NaiveDate,
    /// Value date (bank value date).
    pub value_date: NaiveDate,
    /// Receipt type.
    pub receipt_type: ARReceiptType,
    /// Receipt status.
    pub status: SubledgerDocumentStatus,
    /// Receipt amount.
    pub amount: CurrencyAmount,
    /// Bank charges deducted.
    pub bank_charges: Decimal,
    /// Discount taken.
    pub discount_taken: Decimal,
    /// Write-off amount.
    pub write_off_amount: Decimal,
    /// Net amount applied to invoices.
    pub net_applied: Decimal,
    /// Unapplied amount.
    pub unapplied_amount: Decimal,
    /// Payment method.
    pub payment_method: PaymentMethod,
    /// Bank account.
    pub bank_account: String,
    /// Bank reference.
    pub bank_reference: Option<String>,
    /// Check number (if check payment).
    pub check_number: Option<String>,
    /// Applied invoices.
    pub applied_invoices: Vec<ReceiptApplication>,
    /// GL references.
    pub gl_references: Vec<GLReference>,
    /// Created timestamp.
    #[serde(with = "crate::serde_timestamp::utc")]
    pub created_at: DateTime<Utc>,
    /// Created by user.
    pub created_by: Option<String>,
    /// Notes.
    pub notes: Option<String>,
}

impl ARReceipt {
    /// Creates a new AR receipt.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        receipt_number: String,
        company_code: String,
        customer_id: String,
        customer_name: String,
        receipt_date: NaiveDate,
        amount: Decimal,
        currency: String,
        payment_method: PaymentMethod,
        bank_account: String,
    ) -> Self {
        Self {
            receipt_number,
            company_code,
            customer_id,
            customer_name,
            receipt_date,
            posting_date: receipt_date,
            value_date: receipt_date,
            receipt_type: ARReceiptType::Standard,
            status: SubledgerDocumentStatus::Open,
            amount: CurrencyAmount::single_currency(amount, currency),
            bank_charges: Decimal::ZERO,
            discount_taken: Decimal::ZERO,
            write_off_amount: Decimal::ZERO,
            net_applied: Decimal::ZERO,
            unapplied_amount: amount,
            payment_method,
            bank_account,
            bank_reference: None,
            check_number: None,
            applied_invoices: Vec::new(),
            gl_references: Vec::new(),
            created_at: Utc::now(),
            created_by: None,
            notes: None,
        }
    }

    /// Applies receipt to an invoice.
    pub fn apply_to_invoice(
        &mut self,
        invoice_number: String,
        amount_applied: Decimal,
        discount: Decimal,
    ) {
        let application = ReceiptApplication {
            invoice_number,
            amount_applied,
            discount_taken: discount,
            write_off: Decimal::ZERO,
            application_date: self.receipt_date,
        };

        self.applied_invoices.push(application);
        self.net_applied += amount_applied;
        self.discount_taken += discount;
        self.unapplied_amount = self.amount.document_amount - self.net_applied;

        if self.unapplied_amount <= Decimal::ZERO {
            self.status = SubledgerDocumentStatus::Cleared;
        }
    }

    /// Sets bank charges.
    pub fn with_bank_charges(mut self, charges: Decimal) -> Self {
        self.bank_charges = charges;
        self.unapplied_amount -= charges;
        self
    }

    /// Sets check number.
    pub fn with_check(mut self, check_number: String) -> Self {
        self.check_number = Some(check_number);
        self.payment_method = PaymentMethod::Check;
        self
    }

    /// Sets bank reference.
    pub fn with_bank_reference(mut self, reference: String) -> Self {
        self.bank_reference = Some(reference);
        self
    }

    /// Adds a GL reference.
    pub fn add_gl_reference(&mut self, reference: GLReference) {
        self.gl_references.push(reference);
    }

    /// Gets total amount including discount.
    pub fn total_settlement(&self) -> Decimal {
        self.net_applied + self.discount_taken + self.write_off_amount
    }

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

    /// Creates on-account receipt (not applied to specific invoices).
    #[allow(clippy::too_many_arguments)]
    pub fn on_account(
        receipt_number: String,
        company_code: String,
        customer_id: String,
        customer_name: String,
        receipt_date: NaiveDate,
        amount: Decimal,
        currency: String,
        payment_method: PaymentMethod,
        bank_account: String,
    ) -> Self {
        let mut receipt = Self::new(
            receipt_number,
            company_code,
            customer_id,
            customer_name,
            receipt_date,
            amount,
            currency,
            payment_method,
            bank_account,
        );
        receipt.receipt_type = ARReceiptType::OnAccount;
        receipt
    }
}

/// Type of AR receipt.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum ARReceiptType {
    /// Standard receipt applied to invoices.
    #[default]
    Standard,
    /// On-account receipt (unapplied).
    OnAccount,
    /// Down payment receipt.
    DownPayment,
    /// Refund (negative receipt).
    Refund,
    /// Write-off receipt.
    WriteOff,
    /// Netting (AR against AP).
    Netting,
}

/// Payment method for receipts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum PaymentMethod {
    /// Wire transfer.
    #[default]
    WireTransfer,
    /// Check.
    Check,
    /// ACH/Direct debit.
    ACH,
    /// Credit card.
    CreditCard,
    /// Cash.
    Cash,
    /// Letter of credit.
    LetterOfCredit,
    /// Netting.
    Netting,
    /// Other.
    Other,
}

/// Application of receipt to an invoice.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReceiptApplication {
    /// Invoice number.
    pub invoice_number: String,
    /// Amount applied.
    pub amount_applied: Decimal,
    /// Discount taken.
    pub discount_taken: Decimal,
    /// Write-off amount.
    pub write_off: Decimal,
    /// Application date.
    pub application_date: NaiveDate,
}

impl ReceiptApplication {
    /// Total settlement for this application.
    pub fn total_settlement(&self) -> Decimal {
        self.amount_applied + self.discount_taken + self.write_off
    }
}

/// Batch of receipts for processing.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ARReceiptBatch {
    /// Batch ID.
    pub batch_id: String,
    /// Company code.
    pub company_code: String,
    /// Batch date.
    pub batch_date: NaiveDate,
    /// Receipts in batch.
    pub receipts: Vec<ARReceipt>,
    /// Total batch amount.
    pub total_amount: Decimal,
    /// Batch status.
    pub status: BatchStatus,
    /// Created by.
    pub created_by: String,
    /// Created at.
    #[serde(with = "crate::serde_timestamp::utc")]
    pub created_at: DateTime<Utc>,
}

impl ARReceiptBatch {
    /// Creates a new receipt batch.
    pub fn new(
        batch_id: String,
        company_code: String,
        batch_date: NaiveDate,
        created_by: String,
    ) -> Self {
        Self {
            batch_id,
            company_code,
            batch_date,
            receipts: Vec::new(),
            total_amount: Decimal::ZERO,
            status: BatchStatus::Open,
            created_by,
            created_at: Utc::now(),
        }
    }

    /// Adds a receipt to the batch.
    pub fn add_receipt(&mut self, receipt: ARReceipt) {
        self.total_amount += receipt.amount.document_amount;
        self.receipts.push(receipt);
    }

    /// Posts the batch.
    pub fn post(&mut self) {
        self.status = BatchStatus::Posted;
    }

    /// Gets receipt count.
    pub fn count(&self) -> usize {
        self.receipts.len()
    }
}

/// Status of a batch.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BatchStatus {
    /// Batch is open for additions.
    Open,
    /// Batch is submitted for approval.
    Submitted,
    /// Batch is approved.
    Approved,
    /// Batch is posted.
    Posted,
    /// Batch is cancelled.
    Cancelled,
}

/// Bank statement line for automatic matching.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BankStatementLine {
    /// Statement line ID.
    pub line_id: String,
    /// Bank account.
    pub bank_account: String,
    /// Statement date.
    pub statement_date: NaiveDate,
    /// Value date.
    pub value_date: NaiveDate,
    /// Amount (positive = receipt, negative = payment).
    pub amount: Decimal,
    /// Currency.
    pub currency: String,
    /// Bank reference.
    pub bank_reference: String,
    /// Payer/payee name.
    pub counterparty_name: Option<String>,
    /// Payer/payee account.
    pub counterparty_account: Option<String>,
    /// Payment reference/remittance info.
    pub payment_reference: Option<String>,
    /// Is matched.
    pub is_matched: bool,
    /// Matched receipt number.
    pub matched_receipt: Option<String>,
}

impl BankStatementLine {
    /// Checks if this is an incoming payment.
    pub fn is_receipt(&self) -> bool {
        self.amount > Decimal::ZERO
    }

    /// Marks as matched.
    pub fn match_to_receipt(&mut self, receipt_number: String) {
        self.is_matched = true;
        self.matched_receipt = Some(receipt_number);
    }
}

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

    #[test]
    fn test_receipt_creation() {
        let receipt = ARReceipt::new(
            "REC001".to_string(),
            "1000".to_string(),
            "CUST001".to_string(),
            "Test Customer".to_string(),
            NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
            dec!(1000),
            "USD".to_string(),
            PaymentMethod::WireTransfer,
            "1000".to_string(),
        );

        assert_eq!(receipt.amount.document_amount, dec!(1000));
        assert_eq!(receipt.unapplied_amount, dec!(1000));
        assert_eq!(receipt.status, SubledgerDocumentStatus::Open);
    }

    #[test]
    fn test_apply_to_invoice() {
        let mut receipt = ARReceipt::new(
            "REC001".to_string(),
            "1000".to_string(),
            "CUST001".to_string(),
            "Test Customer".to_string(),
            NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
            dec!(1000),
            "USD".to_string(),
            PaymentMethod::WireTransfer,
            "1000".to_string(),
        );

        receipt.apply_to_invoice("INV001".to_string(), dec!(800), dec!(20));

        assert_eq!(receipt.net_applied, dec!(800));
        assert_eq!(receipt.discount_taken, dec!(20));
        assert_eq!(receipt.unapplied_amount, dec!(200));
        assert_eq!(receipt.applied_invoices.len(), 1);
    }

    #[test]
    fn test_receipt_fully_applied() {
        let mut receipt = ARReceipt::new(
            "REC001".to_string(),
            "1000".to_string(),
            "CUST001".to_string(),
            "Test Customer".to_string(),
            NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
            dec!(1000),
            "USD".to_string(),
            PaymentMethod::WireTransfer,
            "1000".to_string(),
        );

        receipt.apply_to_invoice("INV001".to_string(), dec!(1000), Decimal::ZERO);

        assert_eq!(receipt.status, SubledgerDocumentStatus::Cleared);
        assert_eq!(receipt.unapplied_amount, Decimal::ZERO);
    }

    #[test]
    fn test_batch_totals() {
        let mut batch = ARReceiptBatch::new(
            "BATCH001".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
            "USER1".to_string(),
        );

        let receipt1 = ARReceipt::new(
            "REC001".to_string(),
            "1000".to_string(),
            "CUST001".to_string(),
            "Customer 1".to_string(),
            NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
            dec!(500),
            "USD".to_string(),
            PaymentMethod::WireTransfer,
            "1000".to_string(),
        );

        let receipt2 = ARReceipt::new(
            "REC002".to_string(),
            "1000".to_string(),
            "CUST002".to_string(),
            "Customer 2".to_string(),
            NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
            dec!(750),
            "USD".to_string(),
            PaymentMethod::Check,
            "1000".to_string(),
        );

        batch.add_receipt(receipt1);
        batch.add_receipt(receipt2);

        assert_eq!(batch.count(), 2);
        assert_eq!(batch.total_amount, dec!(1250));
    }
}