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
//! Reference number generation for journal entries.
//!
//! Generates realistic document reference numbers like invoice numbers,
//! purchase orders, sales orders, etc.

use crate::models::BusinessProcess;
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};

/// Types of reference numbers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReferenceType {
    /// Invoice number (vendor or customer)
    Invoice,
    /// Purchase order number
    PurchaseOrder,
    /// Sales order number
    SalesOrder,
    /// Goods receipt number
    GoodsReceipt,
    /// Payment reference
    PaymentReference,
    /// Asset tag number
    AssetTag,
    /// Project number
    ProjectNumber,
    /// Expense report number
    ExpenseReport,
    /// Contract number
    ContractNumber,
    /// Batch number
    BatchNumber,
    /// Internal document number
    InternalDocument,
}

impl ReferenceType {
    /// Get the default prefix for this reference type.
    pub fn default_prefix(&self) -> &'static str {
        match self {
            Self::Invoice => "INV",
            Self::PurchaseOrder => "PO",
            Self::SalesOrder => "SO",
            Self::GoodsReceipt => "GR",
            Self::PaymentReference => "PAY",
            Self::AssetTag => "FA",
            Self::ProjectNumber => "PRJ",
            Self::ExpenseReport => "EXP",
            Self::ContractNumber => "CTR",
            Self::BatchNumber => "BATCH",
            Self::InternalDocument => "DOC",
        }
    }

    /// Get the typical reference type for a business process.
    pub fn for_business_process(process: BusinessProcess) -> Self {
        match process {
            BusinessProcess::O2C => Self::SalesOrder,
            BusinessProcess::P2P => Self::PurchaseOrder,
            BusinessProcess::R2R => Self::InternalDocument,
            BusinessProcess::H2R => Self::ExpenseReport,
            BusinessProcess::A2R => Self::AssetTag,
            BusinessProcess::S2C => Self::PurchaseOrder,
            BusinessProcess::Mfg => Self::InternalDocument,
            BusinessProcess::Bank => Self::PaymentReference,
            BusinessProcess::Audit => Self::InternalDocument,
            BusinessProcess::Treasury => Self::PaymentReference,
            BusinessProcess::Tax => Self::InternalDocument,
            BusinessProcess::Intercompany => Self::InternalDocument,
            BusinessProcess::ProjectAccounting => Self::ProjectNumber,
            BusinessProcess::Esg => Self::InternalDocument,
        }
    }
}

/// Format for reference numbers.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub enum ReferenceFormat {
    /// Simple sequential: PREFIX-000001
    Sequential,
    /// Year-prefixed: PREFIX-YYYY-000001
    #[default]
    YearPrefixed,
    /// Year-month: PREFIX-YYYYMM-00001
    YearMonthPrefixed,
    /// Random alphanumeric: PREFIX-XXXXXX
    Random,
    /// Company-year: PREFIX-COMP-YYYY-00001
    CompanyYearPrefixed,
}

/// Configuration for a reference type.
#[derive(Debug, Clone)]
pub struct ReferenceConfig {
    /// Prefix for the reference
    pub prefix: String,
    /// Format to use
    pub format: ReferenceFormat,
    /// Number of digits in the sequence
    pub sequence_digits: usize,
    /// Starting sequence number
    pub start_sequence: u64,
}

impl Default for ReferenceConfig {
    fn default() -> Self {
        Self {
            prefix: "REF".to_string(),
            format: ReferenceFormat::YearPrefixed,
            sequence_digits: 6,
            start_sequence: 1,
        }
    }
}

/// Generator for reference numbers.
#[derive(Debug)]
pub struct ReferenceGenerator {
    /// Configuration by reference type
    configs: HashMap<ReferenceType, ReferenceConfig>,
    /// Counters by reference type and year
    counters: HashMap<(ReferenceType, Option<i32>), AtomicU64>,
    /// Default year for generation
    default_year: i32,
    /// Company code for company-prefixed formats
    company_code: String,
}

impl Default for ReferenceGenerator {
    fn default() -> Self {
        Self::new(2024, "1000")
    }
}

impl ReferenceGenerator {
    /// Create a new reference generator.
    pub fn new(year: i32, company_code: &str) -> Self {
        let mut configs = HashMap::new();

        // Set up default configurations for each type
        for ref_type in [
            ReferenceType::Invoice,
            ReferenceType::PurchaseOrder,
            ReferenceType::SalesOrder,
            ReferenceType::GoodsReceipt,
            ReferenceType::PaymentReference,
            ReferenceType::AssetTag,
            ReferenceType::ProjectNumber,
            ReferenceType::ExpenseReport,
            ReferenceType::ContractNumber,
            ReferenceType::BatchNumber,
            ReferenceType::InternalDocument,
        ] {
            configs.insert(
                ref_type,
                ReferenceConfig {
                    prefix: ref_type.default_prefix().to_string(),
                    format: ReferenceFormat::YearPrefixed,
                    sequence_digits: 6,
                    start_sequence: 1,
                },
            );
        }

        Self {
            configs,
            counters: HashMap::new(),
            default_year: year,
            company_code: company_code.to_string(),
        }
    }

    /// Set the company code.
    pub fn with_company_code(mut self, code: &str) -> Self {
        self.company_code = code.to_string();
        self
    }

    /// Set the default year.
    pub fn with_year(mut self, year: i32) -> Self {
        self.default_year = year;
        self
    }

    /// Set configuration for a reference type.
    pub fn set_config(&mut self, ref_type: ReferenceType, config: ReferenceConfig) {
        self.configs.insert(ref_type, config);
    }

    /// Set a custom prefix for a reference type.
    pub fn set_prefix(&mut self, ref_type: ReferenceType, prefix: &str) {
        if let Some(config) = self.configs.get_mut(&ref_type) {
            config.prefix = prefix.to_string();
        }
    }

    /// Get the next sequence number for a reference type and optional year.
    fn next_sequence(&mut self, ref_type: ReferenceType, year: Option<i32>) -> u64 {
        let key = (ref_type, year);
        let config = self.configs.get(&ref_type).cloned().unwrap_or_default();

        let counter = self
            .counters
            .entry(key)
            .or_insert_with(|| AtomicU64::new(config.start_sequence));

        counter.fetch_add(1, Ordering::SeqCst)
    }

    /// Generate a reference number.
    pub fn generate(&mut self, ref_type: ReferenceType) -> String {
        self.generate_for_year(ref_type, self.default_year)
    }

    /// Generate a reference number for a specific year.
    pub fn generate_for_year(&mut self, ref_type: ReferenceType, year: i32) -> String {
        let config = self.configs.get(&ref_type).cloned().unwrap_or_default();
        let seq = self.next_sequence(ref_type, Some(year));

        match config.format {
            ReferenceFormat::Sequential => {
                format!(
                    "{}-{:0width$}",
                    config.prefix,
                    seq,
                    width = config.sequence_digits
                )
            }
            ReferenceFormat::YearPrefixed => {
                format!(
                    "{}-{}-{:0width$}",
                    config.prefix,
                    year,
                    seq,
                    width = config.sequence_digits
                )
            }
            ReferenceFormat::YearMonthPrefixed => {
                // Use a default month; in practice, pass month as parameter
                format!(
                    "{}-{}01-{:0width$}",
                    config.prefix,
                    year,
                    seq,
                    width = config.sequence_digits - 1
                )
            }
            ReferenceFormat::Random => {
                // Generate deterministic alphanumeric suffix based on sequence number
                let suffix: String = (0..config.sequence_digits)
                    .map(|i| {
                        let idx = ((seq as usize)
                            .wrapping_mul(7)
                            .wrapping_add(i)
                            .wrapping_mul(13)
                            .wrapping_add(17))
                            % 36;
                        if idx < 10 {
                            (b'0' + idx as u8) as char
                        } else {
                            (b'A' + idx as u8 - 10) as char
                        }
                    })
                    .collect();
                format!("{}-{}", config.prefix, suffix)
            }
            ReferenceFormat::CompanyYearPrefixed => {
                format!(
                    "{}-{}-{}-{:0width$}",
                    config.prefix,
                    self.company_code,
                    year,
                    seq,
                    width = config.sequence_digits
                )
            }
        }
    }

    /// Generate a reference for a business process.
    pub fn generate_for_process(&mut self, process: BusinessProcess) -> String {
        let ref_type = ReferenceType::for_business_process(process);
        self.generate(ref_type)
    }

    /// Generate a reference for a business process and year.
    pub fn generate_for_process_year(&mut self, process: BusinessProcess, year: i32) -> String {
        let ref_type = ReferenceType::for_business_process(process);
        self.generate_for_year(ref_type, year)
    }

    /// Generate an external reference (vendor invoice, etc.) with random elements.
    pub fn generate_external_reference(&self, rng: &mut impl Rng) -> String {
        // External references often have different formats
        let formats = [
            // Vendor invoice formats
            |rng: &mut dyn rand::RngCore| {
                format!("INV{:08}", rng.random_range(10000000u64..99999999))
            },
            |rng: &mut dyn rand::RngCore| {
                format!("{:010}", rng.random_range(1000000000u64..9999999999))
            },
            |rng: &mut dyn rand::RngCore| {
                format!(
                    "V{}-{:06}",
                    rng.random_range(100..999),
                    rng.random_range(1..999999)
                )
            },
            |rng: &mut dyn rand::RngCore| {
                format!(
                    "{}{:07}",
                    (b'A' + rng.random_range(0..26)) as char,
                    rng.random_range(1000000..9999999)
                )
            },
        ];

        let idx = rng.random_range(0..formats.len());
        formats[idx](rng)
    }
}

/// Builder for configuring reference generation.
#[derive(Debug, Clone, Default)]
pub struct ReferenceGeneratorBuilder {
    year: Option<i32>,
    company_code: Option<String>,
    invoice_prefix: Option<String>,
    po_prefix: Option<String>,
    so_prefix: Option<String>,
}

impl ReferenceGeneratorBuilder {
    /// Create a new builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the year.
    pub fn year(mut self, year: i32) -> Self {
        self.year = Some(year);
        self
    }

    /// Set the company code.
    pub fn company_code(mut self, code: &str) -> Self {
        self.company_code = Some(code.to_string());
        self
    }

    /// Set invoice prefix.
    pub fn invoice_prefix(mut self, prefix: &str) -> Self {
        self.invoice_prefix = Some(prefix.to_string());
        self
    }

    /// Set PO prefix.
    pub fn po_prefix(mut self, prefix: &str) -> Self {
        self.po_prefix = Some(prefix.to_string());
        self
    }

    /// Set SO prefix.
    pub fn so_prefix(mut self, prefix: &str) -> Self {
        self.so_prefix = Some(prefix.to_string());
        self
    }

    /// Build the generator.
    pub fn build(self) -> ReferenceGenerator {
        let year = self.year.unwrap_or(2024);
        let company = self.company_code.as_deref().unwrap_or("1000");

        let mut gen = ReferenceGenerator::new(year, company);

        if let Some(prefix) = self.invoice_prefix {
            gen.set_prefix(ReferenceType::Invoice, &prefix);
        }
        if let Some(prefix) = self.po_prefix {
            gen.set_prefix(ReferenceType::PurchaseOrder, &prefix);
        }
        if let Some(prefix) = self.so_prefix {
            gen.set_prefix(ReferenceType::SalesOrder, &prefix);
        }

        gen
    }
}

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

    #[test]
    fn test_sequential_generation() {
        let mut gen = ReferenceGenerator::new(2024, "1000");

        let ref1 = gen.generate(ReferenceType::Invoice);
        let ref2 = gen.generate(ReferenceType::Invoice);
        let ref3 = gen.generate(ReferenceType::Invoice);

        assert!(ref1.starts_with("INV-2024-"));
        assert!(ref2.starts_with("INV-2024-"));
        assert!(ref3.starts_with("INV-2024-"));

        // Should be sequential
        assert_ne!(ref1, ref2);
        assert_ne!(ref2, ref3);
    }

    #[test]
    fn test_different_types() {
        let mut gen = ReferenceGenerator::new(2024, "1000");

        let inv = gen.generate(ReferenceType::Invoice);
        let po = gen.generate(ReferenceType::PurchaseOrder);
        let so = gen.generate(ReferenceType::SalesOrder);

        assert!(inv.starts_with("INV-"));
        assert!(po.starts_with("PO-"));
        assert!(so.starts_with("SO-"));
    }

    #[test]
    fn test_year_based_counters() {
        let mut gen = ReferenceGenerator::new(2024, "1000");

        let ref_2024 = gen.generate_for_year(ReferenceType::Invoice, 2024);
        let ref_2025 = gen.generate_for_year(ReferenceType::Invoice, 2025);

        assert!(ref_2024.contains("2024"));
        assert!(ref_2025.contains("2025"));

        // Different years should have independent counters
        assert!(ref_2024.ends_with("000001"));
        assert!(ref_2025.ends_with("000001"));
    }

    #[test]
    fn test_business_process_mapping() {
        let mut gen = ReferenceGenerator::new(2024, "1000");

        let o2c_ref = gen.generate_for_process(BusinessProcess::O2C);
        let p2p_ref = gen.generate_for_process(BusinessProcess::P2P);

        assert!(o2c_ref.starts_with("SO-")); // Sales Order
        assert!(p2p_ref.starts_with("PO-")); // Purchase Order
    }

    #[test]
    fn test_custom_prefix() {
        let mut gen = ReferenceGenerator::new(2024, "ACME");
        gen.set_prefix(ReferenceType::Invoice, "ACME-INV");

        let inv = gen.generate(ReferenceType::Invoice);
        assert!(inv.starts_with("ACME-INV-"));
    }

    #[test]
    fn test_builder() {
        let mut gen = ReferenceGeneratorBuilder::new()
            .year(2025)
            .company_code("CORP")
            .invoice_prefix("CORP-INV")
            .build();

        let inv = gen.generate(ReferenceType::Invoice);
        assert!(inv.starts_with("CORP-INV-2025-"));
    }

    #[test]
    fn test_external_reference() {
        use rand::SeedableRng;
        use rand_chacha::ChaCha8Rng;

        let gen = ReferenceGenerator::new(2024, "1000");
        let mut rng = ChaCha8Rng::seed_from_u64(42);

        let ext_ref = gen.generate_external_reference(&mut rng);
        assert!(!ext_ref.is_empty());
    }
}