datasynth-standards 3.1.1

Accounting and audit standards framework for synthetic data generation (IFRS, US GAAP, ISA, SOX, PCAOB)
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! External Confirmations (ISA 505).
//!
//! Implements external confirmation procedures for obtaining audit evidence:
//! - Bank confirmations
//! - Accounts receivable confirmations
//! - Accounts payable confirmations
//! - Legal confirmations

use chrono::NaiveDate;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// External confirmation record.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExternalConfirmation {
    /// Unique confirmation identifier.
    pub confirmation_id: Uuid,

    /// Engagement ID.
    pub engagement_id: Uuid,

    /// Type of confirmation.
    pub confirmation_type: ConfirmationType,

    /// Form of confirmation.
    pub confirmation_form: ConfirmationForm,

    /// Name of confirming party.
    pub confirmee_name: String,

    /// Address of confirming party.
    pub confirmee_address: String,

    /// Contact information.
    pub confirmee_contact: String,

    /// Account or item being confirmed.
    pub item_description: String,

    /// Amount per client records.
    #[serde(with = "datasynth_core::serde_decimal")]
    pub client_amount: Decimal,

    /// Currency.
    pub currency: String,

    /// Date sent.
    pub date_sent: NaiveDate,

    /// Follow-up date.
    pub follow_up_date: Option<NaiveDate>,

    /// Response status.
    pub response_status: ConfirmationResponseStatus,

    /// Response details (if received).
    pub response: Option<ConfirmationResponse>,

    /// Reconciliation of differences.
    pub reconciliation: Option<ConfirmationReconciliation>,

    /// Alternative procedures (if no response).
    pub alternative_procedures: Option<AlternativeProcedures>,

    /// Conclusion.
    pub conclusion: ConfirmationConclusion,

    /// Workpaper reference.
    pub workpaper_reference: Option<String>,

    /// Prepared by.
    pub prepared_by: String,

    /// Reviewed by.
    pub reviewed_by: Option<String>,
}

impl ExternalConfirmation {
    /// Create a new external confirmation.
    pub fn new(
        engagement_id: Uuid,
        confirmation_type: ConfirmationType,
        confirmee_name: impl Into<String>,
        item_description: impl Into<String>,
        client_amount: Decimal,
        currency: impl Into<String>,
    ) -> Self {
        Self {
            confirmation_id: Uuid::now_v7(),
            engagement_id,
            confirmation_type,
            confirmation_form: ConfirmationForm::Positive,
            confirmee_name: confirmee_name.into(),
            confirmee_address: String::new(),
            confirmee_contact: String::new(),
            item_description: item_description.into(),
            client_amount,
            currency: currency.into(),
            date_sent: chrono::Utc::now().date_naive(),
            follow_up_date: None,
            response_status: ConfirmationResponseStatus::Pending,
            response: None,
            reconciliation: None,
            alternative_procedures: None,
            conclusion: ConfirmationConclusion::NotCompleted,
            workpaper_reference: None,
            prepared_by: String::new(),
            reviewed_by: None,
        }
    }

    /// Check if confirmation is complete.
    pub fn is_complete(&self) -> bool {
        !matches!(self.conclusion, ConfirmationConclusion::NotCompleted)
    }

    /// Check if alternative procedures are needed.
    pub fn needs_alternative_procedures(&self) -> bool {
        matches!(
            self.response_status,
            ConfirmationResponseStatus::NoResponse | ConfirmationResponseStatus::Returned
        )
    }

    /// Calculate difference between client and confirmed amounts.
    pub fn difference(&self) -> Option<Decimal> {
        self.response
            .as_ref()
            .map(|r| self.client_amount - r.confirmed_amount)
    }
}

/// Type of confirmation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConfirmationType {
    /// Bank account confirmation.
    Bank,
    /// Trade receivable confirmation.
    AccountsReceivable,
    /// Trade payable confirmation.
    AccountsPayable,
    /// Loan/debt confirmation.
    Loan,
    /// Legal confirmation (lawyers' letters).
    Legal,
    /// Investment confirmation.
    Investment,
    /// Insurance confirmation.
    Insurance,
    /// Related party confirmation.
    RelatedParty,
    /// Other confirmation.
    Other,
}

impl std::fmt::Display for ConfirmationType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Bank => write!(f, "Bank Confirmation"),
            Self::AccountsReceivable => write!(f, "AR Confirmation"),
            Self::AccountsPayable => write!(f, "AP Confirmation"),
            Self::Loan => write!(f, "Loan Confirmation"),
            Self::Legal => write!(f, "Legal Confirmation"),
            Self::Investment => write!(f, "Investment Confirmation"),
            Self::Insurance => write!(f, "Insurance Confirmation"),
            Self::RelatedParty => write!(f, "Related Party Confirmation"),
            Self::Other => write!(f, "Other Confirmation"),
        }
    }
}

/// Form of confirmation request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ConfirmationForm {
    /// Positive confirmation - requests response in all cases.
    #[default]
    Positive,
    /// Negative confirmation - requests response only if disagrees.
    Negative,
    /// Blank confirmation - confirmee fills in the amount.
    Blank,
}

impl std::fmt::Display for ConfirmationForm {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Positive => write!(f, "Positive"),
            Self::Negative => write!(f, "Negative"),
            Self::Blank => write!(f, "Blank"),
        }
    }
}

/// Confirmation response status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ConfirmationResponseStatus {
    /// Confirmation not yet sent.
    #[default]
    NotSent,
    /// Sent, awaiting response.
    Pending,
    /// Response received - agrees.
    ReceivedAgrees,
    /// Response received - disagrees.
    ReceivedDisagrees,
    /// Response received - partial information.
    ReceivedPartial,
    /// No response after follow-up.
    NoResponse,
    /// Returned undeliverable.
    Returned,
    /// Response received (for blank confirmations).
    ReceivedBlank,
}

impl std::fmt::Display for ConfirmationResponseStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotSent => write!(f, "Not Sent"),
            Self::Pending => write!(f, "Pending"),
            Self::ReceivedAgrees => write!(f, "Received - Agrees"),
            Self::ReceivedDisagrees => write!(f, "Received - Disagrees"),
            Self::ReceivedPartial => write!(f, "Received - Partial"),
            Self::NoResponse => write!(f, "No Response"),
            Self::Returned => write!(f, "Returned"),
            Self::ReceivedBlank => write!(f, "Received - Blank"),
        }
    }
}

/// Confirmation response details.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfirmationResponse {
    /// Date response received.
    pub date_received: NaiveDate,

    /// Confirmed amount.
    #[serde(with = "datasynth_core::serde_decimal")]
    pub confirmed_amount: Decimal,

    /// Response agrees with client records.
    pub agrees: bool,

    /// Comments from confirmee.
    pub comments: String,

    /// Differences noted by confirmee.
    pub differences_noted: Vec<ConfirmedDifference>,

    /// Respondent name/title.
    pub respondent_name: String,

    /// Whether response appears authentic.
    pub appears_authentic: bool,

    /// Reliability assessment.
    pub reliability_assessment: ResponseReliability,
}

impl ConfirmationResponse {
    /// Create a new confirmation response.
    pub fn new(date_received: NaiveDate, confirmed_amount: Decimal, agrees: bool) -> Self {
        Self {
            date_received,
            confirmed_amount,
            agrees,
            comments: String::new(),
            differences_noted: Vec::new(),
            respondent_name: String::new(),
            appears_authentic: true,
            reliability_assessment: ResponseReliability::Reliable,
        }
    }
}

/// Reliability assessment of confirmation response.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ResponseReliability {
    /// Response appears reliable.
    #[default]
    Reliable,
    /// Some concerns about reliability.
    QuestionableReliability,
    /// Response is unreliable.
    Unreliable,
}

/// Difference noted in confirmation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfirmedDifference {
    /// Description of difference.
    pub description: String,

    /// Amount of difference.
    #[serde(with = "datasynth_core::serde_decimal")]
    pub amount: Decimal,

    /// Type of difference.
    pub difference_type: DifferenceType,
}

/// Type of confirmation difference.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DifferenceType {
    /// Timing difference (e.g., payment in transit).
    Timing,
    /// Actual error in client records.
    Error,
    /// Disputed amount.
    Dispute,
    /// Cutoff difference.
    Cutoff,
    /// Classification difference.
    Classification,
    /// Unknown/unexplained.
    Unknown,
}

/// Reconciliation of confirmation differences.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfirmationReconciliation {
    /// Client balance.
    #[serde(with = "datasynth_core::serde_decimal")]
    pub client_balance: Decimal,

    /// Confirmed balance.
    #[serde(with = "datasynth_core::serde_decimal")]
    pub confirmed_balance: Decimal,

    /// Total difference.
    #[serde(with = "datasynth_core::serde_decimal")]
    pub total_difference: Decimal,

    /// Reconciling items.
    pub reconciling_items: Vec<ReconcilingItem>,

    /// Unreconciled difference.
    #[serde(with = "datasynth_core::serde_decimal")]
    pub unreconciled_difference: Decimal,

    /// Conclusion on reconciliation.
    pub conclusion: ReconciliationConclusion,
}

impl ConfirmationReconciliation {
    /// Create a new reconciliation.
    pub fn new(client_balance: Decimal, confirmed_balance: Decimal) -> Self {
        let total_difference = client_balance - confirmed_balance;
        Self {
            client_balance,
            confirmed_balance,
            total_difference,
            reconciling_items: Vec::new(),
            unreconciled_difference: total_difference,
            conclusion: ReconciliationConclusion::NotCompleted,
        }
    }

    /// Add a reconciling item and update unreconciled difference.
    pub fn add_reconciling_item(&mut self, item: ReconcilingItem) {
        self.unreconciled_difference -= item.amount;
        self.reconciling_items.push(item);
    }
}

/// Reconciling item.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReconcilingItem {
    /// Description.
    pub description: String,

    /// Amount.
    #[serde(with = "datasynth_core::serde_decimal")]
    pub amount: Decimal,

    /// Type of reconciling item.
    pub item_type: ReconcilingItemType,

    /// Supporting evidence obtained.
    pub evidence: String,
}

/// Type of reconciling item.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReconcilingItemType {
    /// Cash/payment in transit.
    CashInTransit,
    /// Deposit in transit.
    DepositInTransit,
    /// Outstanding check.
    OutstandingCheck,
    /// Bank charges not recorded.
    BankCharges,
    /// Interest not recorded.
    InterestNotRecorded,
    /// Cutoff adjustment.
    CutoffAdjustment,
    /// Error correction.
    ErrorCorrection,
    /// Other reconciling item.
    Other,
}

/// Reconciliation conclusion.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ReconciliationConclusion {
    /// Reconciliation not completed.
    #[default]
    NotCompleted,
    /// Fully reconciled, no issues.
    FullyReconciled,
    /// Reconciled with timing differences only.
    ReconciledTimingOnly,
    /// Potential misstatement identified.
    PotentialMisstatement,
    /// Misstatement identified.
    MisstatementIdentified,
}

/// Alternative procedures when confirmation not received.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlternativeProcedures {
    /// Reason alternative procedures were needed.
    pub reason: AlternativeProcedureReason,

    /// Procedures performed.
    pub procedures: Vec<AlternativeProcedure>,

    /// Evidence obtained.
    pub evidence_obtained: Vec<String>,

    /// Conclusion.
    pub conclusion: AlternativeProcedureConclusion,
}

impl AlternativeProcedures {
    /// Create new alternative procedures.
    pub fn new(reason: AlternativeProcedureReason) -> Self {
        Self {
            reason,
            procedures: Vec::new(),
            evidence_obtained: Vec::new(),
            conclusion: AlternativeProcedureConclusion::NotCompleted,
        }
    }
}

/// Reason for alternative procedures.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AlternativeProcedureReason {
    /// No response received.
    NoResponse,
    /// Response unreliable.
    UnreliableResponse,
    /// Confirmation returned undeliverable.
    Undeliverable,
    /// Management refused to allow.
    ManagementRefused,
}

/// Alternative audit procedure.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlternativeProcedure {
    /// Procedure description.
    pub description: String,

    /// Type of procedure.
    pub procedure_type: AlternativeProcedureType,

    /// Result of procedure.
    pub result: String,
}

/// Type of alternative procedure.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AlternativeProcedureType {
    /// Examine subsequent cash receipts.
    SubsequentCashReceipts,
    /// Examine subsequent cash disbursements.
    SubsequentCashDisbursements,
    /// Examine shipping documents.
    ShippingDocuments,
    /// Examine receiving reports.
    ReceivingReports,
    /// Examine customer purchase orders.
    PurchaseOrders,
    /// Examine sales contracts.
    SalesContracts,
    /// Examine bank statements.
    BankStatements,
    /// Other procedure.
    Other,
}

/// Conclusion from alternative procedures.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum AlternativeProcedureConclusion {
    /// Procedures not completed.
    #[default]
    NotCompleted,
    /// Sufficient evidence obtained.
    SufficientEvidence,
    /// Insufficient evidence obtained.
    InsufficientEvidence,
    /// Misstatement identified.
    MisstatementIdentified,
}

/// Confirmation conclusion.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ConfirmationConclusion {
    /// Confirmation not completed.
    #[default]
    NotCompleted,
    /// Satisfactory response received, balance confirmed.
    Confirmed,
    /// Exception noted and resolved.
    ExceptionResolved,
    /// Exception noted, potential misstatement.
    PotentialMisstatement,
    /// Misstatement identified.
    MisstatementIdentified,
    /// Alternative procedures satisfactory.
    AlternativesSatisfactory,
    /// Unable to obtain sufficient evidence.
    InsufficientEvidence,
}

impl std::fmt::Display for ConfirmationConclusion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotCompleted => write!(f, "Not Completed"),
            Self::Confirmed => write!(f, "Confirmed"),
            Self::ExceptionResolved => write!(f, "Exception Resolved"),
            Self::PotentialMisstatement => write!(f, "Potential Misstatement"),
            Self::MisstatementIdentified => write!(f, "Misstatement Identified"),
            Self::AlternativesSatisfactory => write!(f, "Alternative Procedures Satisfactory"),
            Self::InsufficientEvidence => write!(f, "Insufficient Evidence"),
        }
    }
}

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

    #[test]
    fn test_confirmation_creation() {
        let confirmation = ExternalConfirmation::new(
            Uuid::now_v7(),
            ConfirmationType::AccountsReceivable,
            "Customer Corp",
            "Trade receivable balance",
            dec!(50000),
            "USD",
        );

        assert_eq!(confirmation.confirmee_name, "Customer Corp");
        assert_eq!(confirmation.client_amount, dec!(50000));
        assert_eq!(
            confirmation.response_status,
            ConfirmationResponseStatus::Pending
        );
    }

    #[test]
    fn test_confirmation_difference() {
        let mut confirmation = ExternalConfirmation::new(
            Uuid::now_v7(),
            ConfirmationType::AccountsReceivable,
            "Customer Corp",
            "Trade receivable balance",
            dec!(50000),
            "USD",
        );

        confirmation.response = Some(ConfirmationResponse::new(
            NaiveDate::from_ymd_opt(2024, 2, 15).unwrap(),
            dec!(48000),
            false,
        ));

        assert_eq!(confirmation.difference(), Some(dec!(2000)));
    }

    #[test]
    fn test_reconciliation() {
        let mut recon = ConfirmationReconciliation::new(dec!(50000), dec!(48000));

        assert_eq!(recon.total_difference, dec!(2000));
        assert_eq!(recon.unreconciled_difference, dec!(2000));

        recon.add_reconciling_item(ReconcilingItem {
            description: "Payment in transit".to_string(),
            amount: dec!(2000),
            item_type: ReconcilingItemType::CashInTransit,
            evidence: "Examined subsequent receipt".to_string(),
        });

        assert_eq!(recon.unreconciled_difference, dec!(0));
    }

    #[test]
    fn test_alternative_procedures_needed() {
        let mut confirmation = ExternalConfirmation::new(
            Uuid::now_v7(),
            ConfirmationType::AccountsReceivable,
            "Customer Corp",
            "Trade receivable balance",
            dec!(50000),
            "USD",
        );

        confirmation.response_status = ConfirmationResponseStatus::NoResponse;
        assert!(confirmation.needs_alternative_procedures());

        confirmation.response_status = ConfirmationResponseStatus::ReceivedAgrees;
        assert!(!confirmation.needs_alternative_procedures());
    }
}