datasynth-generators 2.4.0

50+ data generators covering GL, P2P, O2C, S2C, HR, manufacturing, audit, tax, treasury, and ESG
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
//! Links document flows to subledger records.
//!
//! This module provides conversion functions to create subledger records
//! from document flow documents, ensuring data coherence between:
//! - P2P document flow (VendorInvoice) -> AP subledger (APInvoice)
//! - O2C document flow (CustomerInvoice) -> AR subledger (ARInvoice)

use std::collections::HashMap;

use rust_decimal::Decimal;

use datasynth_core::models::documents::{CustomerInvoice, Payment, PaymentType, VendorInvoice};
use datasynth_core::models::subledger::ap::{APInvoice, APInvoiceLine, MatchStatus};
use datasynth_core::models::subledger::ar::{ARInvoice, ARInvoiceLine};
use datasynth_core::models::subledger::{PaymentTerms, SubledgerDocumentStatus};

/// Links document flow invoices to subledger records.
#[derive(Default)]
pub struct DocumentFlowLinker {
    ap_counter: u64,
    ar_counter: u64,
    /// Vendor ID → vendor name lookup for realistic AP invoice names.
    vendor_names: HashMap<String, String>,
    /// Customer ID → customer name lookup for realistic AR invoice names.
    customer_names: HashMap<String, String>,
}

impl DocumentFlowLinker {
    /// Create a new document flow linker.
    pub fn new() -> Self {
        Self {
            ap_counter: 0,
            ar_counter: 0,
            vendor_names: HashMap::new(),
            customer_names: HashMap::new(),
        }
    }

    /// Set vendor name lookup map for realistic AP invoice vendor names.
    pub fn with_vendor_names(mut self, names: HashMap<String, String>) -> Self {
        self.vendor_names = names;
        self
    }

    /// Set customer name lookup map for realistic AR invoice customer names.
    pub fn with_customer_names(mut self, names: HashMap<String, String>) -> Self {
        self.customer_names = names;
        self
    }

    /// Convert a document flow VendorInvoice to an AP subledger APInvoice.
    ///
    /// This ensures that vendor invoices from the P2P flow create corresponding
    /// AP subledger records for complete data coherence.
    pub fn create_ap_invoice_from_vendor_invoice(
        &mut self,
        vendor_invoice: &VendorInvoice,
    ) -> APInvoice {
        self.ap_counter += 1;

        // Generate AP invoice number based on vendor invoice
        let invoice_number = format!("APINV{:08}", self.ap_counter);

        // Create the AP invoice.
        // Use the document-flow document ID as vendor_invoice_number so that
        // PaymentAllocation.invoice_id (which references the same document ID)
        // can be matched during settlement.
        let mut ap_invoice = APInvoice::new(
            invoice_number,
            vendor_invoice.header.document_id.clone(),
            vendor_invoice.header.company_code.clone(),
            vendor_invoice.vendor_id.clone(),
            self.vendor_names
                .get(&vendor_invoice.vendor_id)
                .cloned()
                .unwrap_or_else(|| format!("Vendor {}", vendor_invoice.vendor_id)),
            vendor_invoice.invoice_date,
            parse_payment_terms(&vendor_invoice.payment_terms),
            vendor_invoice.header.currency.clone(),
        );

        // Set PO reference if available
        if let Some(po_id) = &vendor_invoice.purchase_order_id {
            ap_invoice.reference_po = Some(po_id.clone());
            ap_invoice.match_status = match vendor_invoice.verification_status {
                datasynth_core::models::documents::InvoiceVerificationStatus::ThreeWayMatchPassed => {
                    MatchStatus::Matched
                }
                datasynth_core::models::documents::InvoiceVerificationStatus::ThreeWayMatchFailed => {
                    // Compute non-zero variance from invoice line items.
                    // When three-way match fails, there is a meaningful price/quantity difference.
                    let total_line_amount: Decimal = vendor_invoice
                        .items
                        .iter()
                        .map(|item| item.base.unit_price * item.base.quantity)
                        .sum();
                    // Price variance: ~2-5% of invoice total
                    let price_var = (total_line_amount * Decimal::new(3, 2)).round_dp(2);
                    // Quantity variance: ~1-3% of invoice total
                    let qty_var = (total_line_amount * Decimal::new(15, 3)).round_dp(2);
                    MatchStatus::MatchedWithVariance {
                        price_variance: price_var,
                        quantity_variance: qty_var,
                    }
                }
                _ => MatchStatus::NotRequired,
            };
        }

        // Add line items
        for (idx, item) in vendor_invoice.items.iter().enumerate() {
            let line = APInvoiceLine::new(
                (idx + 1) as u32,
                item.base.description.clone(),
                item.base.quantity,
                item.base.uom.clone(),
                item.base.unit_price,
                item.base
                    .gl_account
                    .clone()
                    .unwrap_or_else(|| "5000".to_string()),
            )
            .with_tax(
                item.tax_code.clone().unwrap_or_else(|| "VAT".to_string()),
                item.base.tax_amount,
            );

            ap_invoice.add_line(line);
        }

        ap_invoice
    }

    /// Convert a document flow CustomerInvoice to an AR subledger ARInvoice.
    ///
    /// This ensures that customer invoices from the O2C flow create corresponding
    /// AR subledger records for complete data coherence.
    pub fn create_ar_invoice_from_customer_invoice(
        &mut self,
        customer_invoice: &CustomerInvoice,
    ) -> ARInvoice {
        self.ar_counter += 1;

        // Use the document-flow document ID as the AR invoice number so that
        // PaymentAllocation.invoice_id (which references the same document ID)
        // can be matched during settlement.
        let invoice_number = customer_invoice.header.document_id.clone();

        // Create the AR invoice
        let mut ar_invoice = ARInvoice::new(
            invoice_number,
            customer_invoice.header.company_code.clone(),
            customer_invoice.customer_id.clone(),
            self.customer_names
                .get(&customer_invoice.customer_id)
                .cloned()
                .unwrap_or_else(|| format!("Customer {}", customer_invoice.customer_id)),
            customer_invoice.header.document_date,
            parse_payment_terms(&customer_invoice.payment_terms),
            customer_invoice.header.currency.clone(),
        );

        // Add line items
        for (idx, item) in customer_invoice.items.iter().enumerate() {
            let line = ARInvoiceLine::new(
                (idx + 1) as u32,
                item.base.description.clone(),
                item.base.quantity,
                item.base.uom.clone(),
                item.base.unit_price,
                item.revenue_account
                    .clone()
                    .unwrap_or_else(|| "4000".to_string()),
            )
            .with_tax("VAT".to_string(), item.base.tax_amount);

            ar_invoice.add_line(line);
        }

        ar_invoice
    }

    /// Batch convert multiple vendor invoices to AP invoices.
    pub fn batch_create_ap_invoices(
        &mut self,
        vendor_invoices: &[VendorInvoice],
    ) -> Vec<APInvoice> {
        vendor_invoices
            .iter()
            .map(|vi| self.create_ap_invoice_from_vendor_invoice(vi))
            .collect()
    }

    /// Batch convert multiple customer invoices to AR invoices.
    pub fn batch_create_ar_invoices(
        &mut self,
        customer_invoices: &[CustomerInvoice],
    ) -> Vec<ARInvoice> {
        customer_invoices
            .iter()
            .map(|ci| self.create_ar_invoice_from_customer_invoice(ci))
            .collect()
    }
}

/// Reduces `amount_remaining` on AP invoices by the amounts applied in each payment.
///
/// For each `Payment` whose `payment_type` is `ApPayment`, iterates over its
/// `allocations` and matches them to AP invoices by `allocation.invoice_id` ==
/// `ap_invoice.vendor_invoice_number`. `amount_remaining` is clamped to zero so
/// over-payments do not produce negative balances.
pub fn apply_ap_settlements(ap_invoices: &mut [APInvoice], payments: &[Payment]) {
    // Build a lookup: vendor_invoice_number → list of indices in ap_invoices.
    // Uses owned String keys so the map does not hold borrows into the slice,
    // allowing mutable access to elements later.
    let mut index_map: HashMap<String, Vec<usize>> = HashMap::new();
    for (idx, inv) in ap_invoices.iter().enumerate() {
        index_map
            .entry(inv.vendor_invoice_number.clone())
            .or_default()
            .push(idx);
    }

    for payment in payments {
        if payment.payment_type != PaymentType::ApPayment {
            continue;
        }
        for allocation in &payment.allocations {
            if let Some(indices) = index_map.get(&allocation.invoice_id) {
                for &idx in indices {
                    let inv = &mut ap_invoices[idx];
                    inv.amount_remaining =
                        (inv.amount_remaining - allocation.amount).max(Decimal::ZERO);
                    // Update status to reflect settlement state.
                    inv.status = if inv.amount_remaining == Decimal::ZERO {
                        SubledgerDocumentStatus::Cleared
                    } else {
                        SubledgerDocumentStatus::PartiallyCleared
                    };
                }
            }
        }
    }
}

/// Reduces `amount_remaining` on AR invoices by the amounts applied in each receipt.
///
/// For each `Payment` whose `payment_type` is `ArReceipt`, iterates over its
/// `allocations` and matches them to AR invoices by `allocation.invoice_id` ==
/// `ar_invoice.invoice_number`. `amount_remaining` is clamped to zero so
/// over-payments do not produce negative balances.
pub fn apply_ar_settlements(ar_invoices: &mut [ARInvoice], payments: &[Payment]) {
    // Build a lookup: invoice_number → list of indices in ar_invoices.
    // Uses owned String keys so the map does not hold borrows into the slice,
    // allowing mutable access to elements later.
    let mut index_map: HashMap<String, Vec<usize>> = HashMap::new();
    for (idx, inv) in ar_invoices.iter().enumerate() {
        index_map
            .entry(inv.invoice_number.clone())
            .or_default()
            .push(idx);
    }

    for payment in payments {
        if payment.payment_type != PaymentType::ArReceipt {
            continue;
        }
        for allocation in &payment.allocations {
            if let Some(indices) = index_map.get(&allocation.invoice_id) {
                for &idx in indices {
                    let inv = &mut ar_invoices[idx];
                    inv.amount_remaining =
                        (inv.amount_remaining - allocation.amount).max(Decimal::ZERO);
                    // Update status to reflect settlement state.
                    inv.status = if inv.amount_remaining == Decimal::ZERO {
                        SubledgerDocumentStatus::Cleared
                    } else {
                        SubledgerDocumentStatus::PartiallyCleared
                    };
                }
            }
        }
    }
}

/// Parse payment terms string into PaymentTerms struct.
fn parse_payment_terms(terms_str: &str) -> PaymentTerms {
    // Try to parse common payment terms formats
    match terms_str.to_uppercase().as_str() {
        "NET30" | "N30" => PaymentTerms::net_30(),
        "NET60" | "N60" => PaymentTerms::net_60(),
        "NET90" | "N90" => PaymentTerms::net_90(),
        "DUE ON RECEIPT" | "COD" => PaymentTerms::net(0), // Due immediately
        _ => {
            // Default to NET30 if parsing fails
            PaymentTerms::net_30()
        }
    }
}

/// Result of linking document flows to subledgers.
#[derive(Debug, Clone, Default)]
pub struct SubledgerLinkResult {
    /// AP invoices created from vendor invoices.
    pub ap_invoices: Vec<APInvoice>,
    /// AR invoices created from customer invoices.
    pub ar_invoices: Vec<ARInvoice>,
    /// Number of vendor invoices processed.
    pub vendor_invoices_processed: usize,
    /// Number of customer invoices processed.
    pub customer_invoices_processed: usize,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use chrono::NaiveDate;
    use datasynth_core::models::documents::VendorInvoiceItem;
    use rust_decimal_macros::dec;

    #[test]
    fn test_create_ap_invoice_from_vendor_invoice() {
        let mut vendor_invoice = VendorInvoice::new(
            "VI-001",
            "1000",
            "VEND001",
            "V-INV-001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            "SYSTEM",
        );

        vendor_invoice.add_item(VendorInvoiceItem::new(1, "Test Item", dec!(10), dec!(100)));

        let mut linker = DocumentFlowLinker::new();
        let ap_invoice = linker.create_ap_invoice_from_vendor_invoice(&vendor_invoice);

        assert_eq!(ap_invoice.vendor_id, "VEND001");
        // vendor_invoice_number now stores the document-flow document ID so that
        // PaymentAllocation.invoice_id can be matched during settlement.
        assert_eq!(ap_invoice.vendor_invoice_number, "VI-001");
        assert_eq!(ap_invoice.lines.len(), 1);
        assert!(ap_invoice.gross_amount.document_amount > Decimal::ZERO);
    }

    #[test]
    fn test_create_ar_invoice_from_customer_invoice() {
        use datasynth_core::models::documents::CustomerInvoiceItem;

        let mut customer_invoice = CustomerInvoice::new(
            "CI-001",
            "1000",
            "CUST001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            NaiveDate::from_ymd_opt(2024, 2, 14).unwrap(),
            "SYSTEM",
        );

        customer_invoice.add_item(CustomerInvoiceItem::new(1, "Product A", dec!(5), dec!(200)));

        let mut linker = DocumentFlowLinker::new();
        let ar_invoice = linker.create_ar_invoice_from_customer_invoice(&customer_invoice);

        assert_eq!(ar_invoice.customer_id, "CUST001");
        assert_eq!(ar_invoice.lines.len(), 1);
        assert!(ar_invoice.gross_amount.document_amount > Decimal::ZERO);
    }

    #[test]
    fn test_batch_conversion() {
        let vendor_invoice = VendorInvoice::new(
            "VI-001",
            "1000",
            "VEND001",
            "V-INV-001",
            2024,
            1,
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            "SYSTEM",
        );

        let mut linker = DocumentFlowLinker::new();
        let ap_invoices =
            linker.batch_create_ap_invoices(&[vendor_invoice.clone(), vendor_invoice]);

        assert_eq!(ap_invoices.len(), 2);
        assert_eq!(ap_invoices[0].invoice_number, "APINV00000001");
        assert_eq!(ap_invoices[1].invoice_number, "APINV00000002");
    }

    #[test]
    fn test_parse_payment_terms() {
        let terms = parse_payment_terms("NET30");
        assert_eq!(terms.net_due_days, 30);

        let terms = parse_payment_terms("NET60");
        assert_eq!(terms.net_due_days, 60);

        let terms = parse_payment_terms("DUE ON RECEIPT");
        assert_eq!(terms.net_due_days, 0);

        // Unknown terms default to NET30
        let terms = parse_payment_terms("CUSTOM");
        assert_eq!(terms.net_due_days, 30);
    }
}