datasynth-generators 2.2.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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! ISA 505 External Confirmation generator.
//!
//! Produces [`ExternalConfirmation`] instances for audit engagements, covering
//! accounts receivable, accounts payable, bank, and legal confirmations with
//! realistic response statuses, reconciliation details, and alternative
//! procedures for non-responses.

use chrono::NaiveDate;
use datasynth_core::utils::seeded_rng;
use rand::Rng;
use rand_chacha::ChaCha8Rng;
use rust_decimal::Decimal;
use uuid::Uuid;

use datasynth_standards::audit::confirmation::{
    AlternativeProcedureConclusion, AlternativeProcedureReason, AlternativeProcedures,
    ConfirmationConclusion, ConfirmationForm, ConfirmationReconciliation, ConfirmationResponse,
    ConfirmationResponseStatus, ConfirmationType, ExternalConfirmation, ReconcilingItem,
    ReconcilingItemType, ResponseReliability,
};

/// Configuration for the confirmation generator.
#[derive(Debug, Clone)]
pub struct ConfirmationGeneratorConfig {
    /// Total number of confirmations to generate.
    pub confirmation_count: usize,
    /// Positive response rate (0.0..1.0).
    pub positive_response_rate: f64,
    /// Exception rate (disagreements among responses).
    pub exception_rate: f64,
    /// Non-response rate.
    pub non_response_rate: f64,
    /// Type distribution: [AR, AP, Bank, Legal] (rest is Other).
    pub type_weights: [f64; 4],
}

impl Default for ConfirmationGeneratorConfig {
    fn default() -> Self {
        Self {
            confirmation_count: 50,
            positive_response_rate: 0.85,
            exception_rate: 0.10,
            non_response_rate: 0.10,
            type_weights: [0.40, 0.30, 0.20, 0.10],
        }
    }
}

/// Generates [`ExternalConfirmation`] instances for a given audit engagement.
pub struct ConfirmationGenerator {
    rng: ChaCha8Rng,
    config: ConfirmationGeneratorConfig,
    confirmation_counter: usize,
}

/// Discriminator added to the seed so this generator's RNG stream does not
/// overlap with other generators that may share the same base seed.
const SEED_DISCRIMINATOR: u64 = 0xAE_0E;

impl ConfirmationGenerator {
    /// Create a new generator with the given seed and default config.
    pub fn new(seed: u64) -> Self {
        Self {
            rng: seeded_rng(seed, SEED_DISCRIMINATOR),
            config: ConfirmationGeneratorConfig::default(),
            confirmation_counter: 0,
        }
    }

    /// Create a new generator with the given seed and custom config.
    pub fn with_config(seed: u64, config: ConfirmationGeneratorConfig) -> Self {
        Self {
            rng: seeded_rng(seed, SEED_DISCRIMINATOR),
            config,
            confirmation_counter: 0,
        }
    }

    /// Generate confirmations for an audit engagement.
    ///
    /// Returns `config.confirmation_count` confirmations with realistic
    /// response statuses, reconciliation details, and alternative procedures.
    pub fn generate_confirmations(
        &mut self,
        engagement_id: Uuid,
        base_date: NaiveDate,
    ) -> Vec<ExternalConfirmation> {
        let count = self.config.confirmation_count;
        let mut confirmations = Vec::with_capacity(count);

        for _ in 0..count {
            self.confirmation_counter += 1;
            let confirmation = self.build_confirmation(engagement_id, base_date);
            confirmations.push(confirmation);
        }

        confirmations
    }

    /// Pick a confirmation type from the configured weights.
    fn pick_confirmation_type(&mut self) -> ConfirmationType {
        let weights = &self.config.type_weights;
        let total: f64 = weights.iter().sum();
        let mut r: f64 = self.rng.random_range(0.0..total);

        for (i, &w) in weights.iter().enumerate() {
            r -= w;
            if r <= 0.0 {
                return match i {
                    0 => ConfirmationType::AccountsReceivable,
                    1 => ConfirmationType::AccountsPayable,
                    2 => ConfirmationType::Bank,
                    _ => ConfirmationType::Legal,
                };
            }
        }
        ConfirmationType::AccountsReceivable
    }

    /// Generate a confirmee name based on confirmation type.
    fn generate_confirmee_name(&mut self, conf_type: ConfirmationType) -> String {
        let n = self.confirmation_counter;
        match conf_type {
            ConfirmationType::AccountsReceivable => format!("Customer-{n}"),
            ConfirmationType::AccountsPayable => format!("Vendor-{n}"),
            ConfirmationType::Bank => {
                let cities = ["New York", "London", "Chicago", "Dallas", "Boston"];
                let idx = self.rng.random_range(0..cities.len());
                format!("Bank of {}", cities[idx])
            }
            ConfirmationType::Legal => format!("Law Office {n}"),
            _ => format!("Confirmee-{n}"),
        }
    }

    /// Generate client amount based on confirmation type.
    fn generate_client_amount(&mut self, conf_type: ConfirmationType) -> Decimal {
        match conf_type {
            ConfirmationType::AccountsReceivable | ConfirmationType::AccountsPayable => {
                Decimal::from(self.rng.random_range(1000..500_000_i64))
            }
            ConfirmationType::Bank => Decimal::from(self.rng.random_range(50_000..5_000_000_i64)),
            ConfirmationType::Legal => Decimal::from(self.rng.random_range(500..100_000_i64)),
            _ => Decimal::from(self.rng.random_range(1000..500_000_i64)),
        }
    }

    /// Generate an item description based on confirmation type.
    fn generate_item_description(&self, conf_type: ConfirmationType) -> String {
        match conf_type {
            ConfirmationType::AccountsReceivable => "Trade receivable balance".to_string(),
            ConfirmationType::AccountsPayable => "Trade payable balance".to_string(),
            ConfirmationType::Bank => "Bank account balance".to_string(),
            ConfirmationType::Legal => "Legal matters and contingencies".to_string(),
            _ => "Account balance".to_string(),
        }
    }

    /// Build a single confirmation with all its details.
    fn build_confirmation(
        &mut self,
        engagement_id: Uuid,
        base_date: NaiveDate,
    ) -> ExternalConfirmation {
        let conf_type = self.pick_confirmation_type();
        let confirmee_name = self.generate_confirmee_name(conf_type);
        let client_amount = self.generate_client_amount(conf_type);
        let item_description = self.generate_item_description(conf_type);

        let days_offset = self.rng.random_range(0..14_i64);
        let date_sent = base_date + chrono::Duration::days(days_offset);

        let mut confirmation = ExternalConfirmation::new(
            engagement_id,
            conf_type,
            &confirmee_name,
            &item_description,
            client_amount,
            "USD",
        );

        confirmation.confirmation_form = ConfirmationForm::Positive;
        confirmation.date_sent = date_sent;
        confirmation.prepared_by = format!("Audit Staff {}", self.confirmation_counter);
        confirmation.workpaper_reference =
            Some(format!("WP-CONF-{:04}", self.confirmation_counter));

        // Determine response status
        let roll: f64 = self.rng.random_range(0.0..1.0);

        if roll < self.config.non_response_rate {
            // No response
            self.apply_no_response(&mut confirmation, date_sent);
        } else {
            // Got some response; distribute among agrees/disagrees/partial
            let remaining_roll: f64 = self.rng.random_range(0.0..1.0);

            if remaining_roll < self.config.positive_response_rate {
                self.apply_received_agrees(&mut confirmation, date_sent, client_amount);
            } else if remaining_roll
                < self.config.positive_response_rate + self.config.exception_rate
            {
                self.apply_received_disagrees(&mut confirmation, date_sent, client_amount);
            } else {
                self.apply_received_partial(&mut confirmation, date_sent, client_amount);
            }
        }

        // Set follow-up date for pending/no-response
        if matches!(
            confirmation.response_status,
            ConfirmationResponseStatus::Pending | ConfirmationResponseStatus::NoResponse
        ) {
            confirmation.follow_up_date = Some(date_sent + chrono::Duration::days(14));
        }

        confirmation
    }

    /// Apply ReceivedAgrees status to a confirmation.
    fn apply_received_agrees(
        &mut self,
        confirmation: &mut ExternalConfirmation,
        date_sent: NaiveDate,
        client_amount: Decimal,
    ) {
        let response_days = self.rng.random_range(7..30_i64);
        let date_received = date_sent + chrono::Duration::days(response_days);

        let mut response = ConfirmationResponse::new(date_received, client_amount, true);
        response.respondent_name = format!("{} - Authorized Signer", confirmation.confirmee_name);
        response.appears_authentic = true;
        response.reliability_assessment = ResponseReliability::Reliable;

        confirmation.response_status = ConfirmationResponseStatus::ReceivedAgrees;
        confirmation.response = Some(response);
        confirmation.conclusion = ConfirmationConclusion::Confirmed;
    }

    /// Apply ReceivedDisagrees status to a confirmation.
    fn apply_received_disagrees(
        &mut self,
        confirmation: &mut ExternalConfirmation,
        date_sent: NaiveDate,
        client_amount: Decimal,
    ) {
        let response_days = self.rng.random_range(7..30_i64);
        let date_received = date_sent + chrono::Duration::days(response_days);

        // Confirmed amount differs by 0.90..1.10 factor
        let factor: f64 = self.rng.random_range(0.90..1.10);
        let factor_decimal = Decimal::from_f64_retain(factor).unwrap_or(Decimal::ONE);
        let confirmed_amount = client_amount * factor_decimal;

        let mut response = ConfirmationResponse::new(date_received, confirmed_amount, false);
        response.respondent_name = format!("{} - Authorized Signer", confirmation.confirmee_name);
        response.appears_authentic = true;
        response.reliability_assessment = ResponseReliability::Reliable;

        confirmation.response_status = ConfirmationResponseStatus::ReceivedDisagrees;
        confirmation.response = Some(response);

        // Create reconciliation
        let mut reconciliation = ConfirmationReconciliation::new(client_amount, confirmed_amount);

        // Add a reconciling item
        let item_type = if self.rng.random_bool(0.5) {
            ReconcilingItemType::CashInTransit
        } else {
            ReconcilingItemType::CutoffAdjustment
        };

        let difference = client_amount - confirmed_amount;
        let item = ReconcilingItem {
            description: match item_type {
                ReconcilingItemType::CashInTransit => "Payment in transit".to_string(),
                ReconcilingItemType::CutoffAdjustment => "Cutoff timing difference".to_string(),
                _ => "Other reconciling item".to_string(),
            },
            amount: difference,
            item_type,
            evidence: "Examined supporting documentation".to_string(),
        };
        reconciliation.add_reconciling_item(item);

        confirmation.reconciliation = Some(reconciliation);

        // Conclusion: 80% ExceptionResolved, 20% PotentialMisstatement
        if self.rng.random_bool(0.80) {
            confirmation.conclusion = ConfirmationConclusion::ExceptionResolved;
        } else {
            confirmation.conclusion = ConfirmationConclusion::PotentialMisstatement;
        }
    }

    /// Apply NoResponse status with alternative procedures.
    fn apply_no_response(&mut self, confirmation: &mut ExternalConfirmation, date_sent: NaiveDate) {
        confirmation.response_status = ConfirmationResponseStatus::NoResponse;
        confirmation.follow_up_date = Some(date_sent + chrono::Duration::days(14));

        let mut alt_procedures = AlternativeProcedures::new(AlternativeProcedureReason::NoResponse);
        alt_procedures
            .evidence_obtained
            .push("Reviewed subsequent transactions".to_string());
        alt_procedures
            .evidence_obtained
            .push("Examined supporting documentation".to_string());

        // Conclusion: 90% satisfactory, 10% insufficient
        if self.rng.random_bool(0.90) {
            alt_procedures.conclusion = AlternativeProcedureConclusion::SufficientEvidence;
            confirmation.conclusion = ConfirmationConclusion::AlternativesSatisfactory;
        } else {
            alt_procedures.conclusion = AlternativeProcedureConclusion::InsufficientEvidence;
            confirmation.conclusion = ConfirmationConclusion::InsufficientEvidence;
        }

        confirmation.alternative_procedures = Some(alt_procedures);
    }

    /// Apply ReceivedPartial status to a confirmation.
    fn apply_received_partial(
        &mut self,
        confirmation: &mut ExternalConfirmation,
        date_sent: NaiveDate,
        client_amount: Decimal,
    ) {
        let response_days = self.rng.random_range(7..30_i64);
        let date_received = date_sent + chrono::Duration::days(response_days);

        // Partial amount: 50-90% of client amount
        let partial_factor: f64 = self.rng.random_range(0.50..0.90);
        let partial_decimal =
            Decimal::from_f64_retain(partial_factor).unwrap_or(Decimal::new(70, 2));
        let partial_amount = client_amount * partial_decimal;

        let mut response = ConfirmationResponse::new(date_received, partial_amount, false);
        response.respondent_name = format!("{} - Authorized Signer", confirmation.confirmee_name);
        response.appears_authentic = true;
        response.reliability_assessment = ResponseReliability::Reliable;
        response.comments = "Partial information provided".to_string();

        confirmation.response_status = ConfirmationResponseStatus::ReceivedPartial;
        confirmation.response = Some(response);
        confirmation.conclusion = ConfirmationConclusion::ExceptionResolved;
    }
}

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

    #[test]
    fn test_deterministic_generation() {
        let engagement_id = Uuid::nil();
        let base_date = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();

        let mut gen1 = ConfirmationGenerator::new(42);
        let mut gen2 = ConfirmationGenerator::new(42);

        let results1 = gen1.generate_confirmations(engagement_id, base_date);
        let results2 = gen2.generate_confirmations(engagement_id, base_date);

        assert_eq!(results1.len(), results2.len());
        for (c1, c2) in results1.iter().zip(results2.iter()) {
            assert_eq!(c1.confirmee_name, c2.confirmee_name);
            assert_eq!(c1.client_amount, c2.client_amount);
            assert_eq!(c1.response_status, c2.response_status);
            assert_eq!(c1.date_sent, c2.date_sent);
            assert_eq!(c1.prepared_by, c2.prepared_by);
        }
    }

    #[test]
    fn test_confirmation_count() {
        let engagement_id = Uuid::nil();
        let base_date = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();

        let config = ConfirmationGeneratorConfig {
            confirmation_count: 25,
            ..Default::default()
        };
        let mut gen = ConfirmationGenerator::with_config(42, config);
        let results = gen.generate_confirmations(engagement_id, base_date);

        assert_eq!(results.len(), 25);
    }

    #[test]
    fn test_type_distribution() {
        let engagement_id = Uuid::nil();
        let base_date = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();

        let config = ConfirmationGeneratorConfig {
            confirmation_count: 200,
            ..Default::default()
        };
        let mut gen = ConfirmationGenerator::with_config(42, config);
        let results = gen.generate_confirmations(engagement_id, base_date);

        let ar_count = results
            .iter()
            .filter(|c| c.confirmation_type == ConfirmationType::AccountsReceivable)
            .count();
        let ap_count = results
            .iter()
            .filter(|c| c.confirmation_type == ConfirmationType::AccountsPayable)
            .count();
        let bank_count = results
            .iter()
            .filter(|c| c.confirmation_type == ConfirmationType::Bank)
            .count();
        let legal_count = results
            .iter()
            .filter(|c| c.confirmation_type == ConfirmationType::Legal)
            .count();

        // With weights [0.40, 0.30, 0.20, 0.10], AR > AP > Bank > Legal
        assert!(
            ar_count > ap_count,
            "AR ({}) should exceed AP ({})",
            ar_count,
            ap_count
        );
        assert!(
            ap_count > bank_count,
            "AP ({}) should exceed Bank ({})",
            ap_count,
            bank_count
        );
        assert!(
            bank_count > legal_count,
            "Bank ({}) should exceed Legal ({})",
            bank_count,
            legal_count
        );
    }

    #[test]
    fn test_positive_response_rate() {
        let engagement_id = Uuid::nil();
        let base_date = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();

        let config = ConfirmationGeneratorConfig {
            confirmation_count: 100,
            positive_response_rate: 1.0,
            exception_rate: 0.0,
            non_response_rate: 0.0,
            type_weights: [1.0, 0.0, 0.0, 0.0],
        };
        let mut gen = ConfirmationGenerator::with_config(42, config);
        let results = gen.generate_confirmations(engagement_id, base_date);

        for c in &results {
            assert_eq!(
                c.response_status,
                ConfirmationResponseStatus::ReceivedAgrees,
                "All should be ReceivedAgrees when positive_response_rate=1.0"
            );
        }
    }

    #[test]
    fn test_non_response_generates_alternatives() {
        let engagement_id = Uuid::nil();
        let base_date = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();

        let config = ConfirmationGeneratorConfig {
            confirmation_count: 100,
            positive_response_rate: 0.0,
            exception_rate: 0.0,
            non_response_rate: 1.0,
            type_weights: [1.0, 0.0, 0.0, 0.0],
        };
        let mut gen = ConfirmationGenerator::with_config(42, config);
        let results = gen.generate_confirmations(engagement_id, base_date);

        for c in &results {
            assert_eq!(c.response_status, ConfirmationResponseStatus::NoResponse);
            assert!(
                c.alternative_procedures.is_some(),
                "NoResponse confirmations must have alternative_procedures"
            );
        }
    }

    #[test]
    fn test_disagreements_have_reconciliation() {
        let engagement_id = Uuid::nil();
        let base_date = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();

        // Force all to be disagrees: non_response_rate=0, positive_response_rate=0,
        // exception_rate=1.0 so the "remaining" roll always lands in disagree range.
        let config = ConfirmationGeneratorConfig {
            confirmation_count: 50,
            positive_response_rate: 0.0,
            exception_rate: 1.0,
            non_response_rate: 0.0,
            type_weights: [1.0, 0.0, 0.0, 0.0],
        };
        let mut gen = ConfirmationGenerator::with_config(42, config);
        let results = gen.generate_confirmations(engagement_id, base_date);

        let disagrees: Vec<_> = results
            .iter()
            .filter(|c| c.response_status == ConfirmationResponseStatus::ReceivedDisagrees)
            .collect();

        assert!(!disagrees.is_empty(), "Should have some disagreements");

        for c in &disagrees {
            assert!(
                c.reconciliation.is_some(),
                "ReceivedDisagrees confirmations must have reconciliation"
            );
        }
    }

    #[test]
    fn test_all_confirmations_have_prepared_by() {
        let engagement_id = Uuid::nil();
        let base_date = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();

        let mut gen = ConfirmationGenerator::new(42);
        let results = gen.generate_confirmations(engagement_id, base_date);

        for c in &results {
            assert!(
                !c.prepared_by.is_empty(),
                "All confirmations must have non-empty prepared_by"
            );
        }
    }

    #[test]
    fn test_zero_non_response_rate() {
        let engagement_id = Uuid::nil();
        let base_date = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();

        let config = ConfirmationGeneratorConfig {
            confirmation_count: 100,
            positive_response_rate: 0.85,
            exception_rate: 0.10,
            non_response_rate: 0.0,
            type_weights: [0.40, 0.30, 0.20, 0.10],
        };
        let mut gen = ConfirmationGenerator::with_config(42, config);
        let results = gen.generate_confirmations(engagement_id, base_date);

        let no_responses = results
            .iter()
            .filter(|c| c.response_status == ConfirmationResponseStatus::NoResponse)
            .count();

        assert_eq!(
            no_responses, 0,
            "With non_response_rate=0.0, there should be no NoResponse statuses"
        );
    }
}