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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
//! Audit Trail for Complete Traceability.
//!
//! Provides structures for maintaining a complete audit trail from
//! risk assessment through to conclusions, enabling traceability
//! across the entire audit process.

use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::isa_reference::IsaRequirement;

/// Complete audit trail for an assertion or audit area.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditTrail {
    /// Unique trail identifier.
    pub trail_id: Uuid,

    /// Engagement ID.
    pub engagement_id: Uuid,

    /// Account or audit area.
    pub account_or_area: String,

    /// Assertion being addressed.
    pub assertion: Assertion,

    /// Risk assessment phase.
    pub risk_assessment: RiskTrailNode,

    /// Planned audit responses.
    pub planned_responses: Vec<ResponseTrailNode>,

    /// Procedures actually performed.
    pub procedures_performed: Vec<ProcedureTrailNode>,

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

    /// Conclusion reached.
    pub conclusion: ConclusionTrailNode,

    /// Gaps identified in the audit trail.
    pub gaps_identified: Vec<TrailGap>,

    /// ISA requirements addressed.
    pub isa_coverage: Vec<IsaRequirement>,
}

impl AuditTrail {
    /// Create a new audit trail.
    pub fn new(
        engagement_id: Uuid,
        account_or_area: impl Into<String>,
        assertion: Assertion,
    ) -> Self {
        Self {
            trail_id: Uuid::now_v7(),
            engagement_id,
            account_or_area: account_or_area.into(),
            assertion,
            risk_assessment: RiskTrailNode::default(),
            planned_responses: Vec::new(),
            procedures_performed: Vec::new(),
            evidence_obtained: Vec::new(),
            conclusion: ConclusionTrailNode::default(),
            gaps_identified: Vec::new(),
            isa_coverage: Vec::new(),
        }
    }

    /// Check if trail is complete (no gaps).
    pub fn is_complete(&self) -> bool {
        self.gaps_identified.is_empty()
            && self.conclusion.conclusion_reached
            && !self.evidence_obtained.is_empty()
    }

    /// Identify gaps in the audit trail.
    pub fn identify_gaps(&mut self) {
        self.gaps_identified.clear();

        // Check for risk assessment gaps
        if !self.risk_assessment.risk_identified {
            self.gaps_identified.push(TrailGap {
                gap_type: GapType::RiskAssessment,
                description: "Risk of material misstatement not documented".to_string(),
                severity: GapSeverity::High,
                remediation_required: true,
            });
        }

        // Check for response gaps
        if self.planned_responses.is_empty() {
            self.gaps_identified.push(TrailGap {
                gap_type: GapType::PlannedResponse,
                description: "No audit responses planned".to_string(),
                severity: GapSeverity::High,
                remediation_required: true,
            });
        }

        // Check for procedures gap
        if self.procedures_performed.is_empty() {
            self.gaps_identified.push(TrailGap {
                gap_type: GapType::ProceduresPerformed,
                description: "No audit procedures performed".to_string(),
                severity: GapSeverity::High,
                remediation_required: true,
            });
        }

        // Check for evidence gap
        if self.evidence_obtained.is_empty() {
            self.gaps_identified.push(TrailGap {
                gap_type: GapType::Evidence,
                description: "No audit evidence documented".to_string(),
                severity: GapSeverity::High,
                remediation_required: true,
            });
        }

        // Check for conclusion gap
        if !self.conclusion.conclusion_reached {
            self.gaps_identified.push(TrailGap {
                gap_type: GapType::Conclusion,
                description: "No conclusion documented".to_string(),
                severity: GapSeverity::High,
                remediation_required: true,
            });
        }

        // Check response-to-risk linkage
        for response in &self.planned_responses {
            if !self
                .procedures_performed
                .iter()
                .any(|p| p.response_id == Some(response.response_id))
            {
                self.gaps_identified.push(TrailGap {
                    gap_type: GapType::Linkage,
                    description: format!(
                        "Planned response '{}' not linked to performed procedure",
                        response.response_description
                    ),
                    severity: GapSeverity::Medium,
                    remediation_required: true,
                });
            }
        }
    }
}

/// Financial statement assertion.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum Assertion {
    // Transaction assertions
    /// Transactions and events occurred.
    #[default]
    Occurrence,
    /// All transactions are recorded (none omitted).
    Completeness,
    /// Transactions are recorded in the correct period.
    Cutoff,
    /// Transactions are recorded at correct amounts.
    Accuracy,
    /// Transactions are recorded in proper accounts.
    Classification,

    // Balance assertions
    /// Assets and liabilities exist.
    Existence,
    /// Entity has rights to assets and obligations for liabilities.
    RightsAndObligations,
    /// Assets and liabilities are recorded at appropriate amounts.
    Valuation,

    // Disclosure assertions
    /// Disclosures are understandable.
    Understandability,
    /// Information is appropriately classified and described.
    ClassificationAndUnderstandability,
    /// Amounts are accurate and appropriately measured.
    AccuracyAndValuation,
}

impl std::fmt::Display for Assertion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Occurrence => write!(f, "Occurrence"),
            Self::Completeness => write!(f, "Completeness"),
            Self::Cutoff => write!(f, "Cutoff"),
            Self::Accuracy => write!(f, "Accuracy"),
            Self::Classification => write!(f, "Classification"),
            Self::Existence => write!(f, "Existence"),
            Self::RightsAndObligations => write!(f, "Rights and Obligations"),
            Self::Valuation => write!(f, "Valuation"),
            Self::Understandability => write!(f, "Understandability"),
            Self::ClassificationAndUnderstandability => {
                write!(f, "Classification and Understandability")
            }
            Self::AccuracyAndValuation => write!(f, "Accuracy and Valuation"),
        }
    }
}

/// Risk assessment trail node.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RiskTrailNode {
    /// Risk identified.
    pub risk_identified: bool,

    /// Risk description.
    pub risk_description: String,

    /// Risk level (inherent risk).
    pub inherent_risk_level: AuditRiskLevel,

    /// Control risk level.
    pub control_risk_level: AuditRiskLevel,

    /// Combined assessment (RoMM).
    pub romm_level: AuditRiskLevel,

    /// Significant risk designation.
    pub is_significant_risk: bool,

    /// Fraud risk identified.
    pub fraud_risk_identified: bool,

    /// Understanding of entity obtained.
    pub understanding_documented: bool,

    /// Internal controls evaluated.
    pub controls_evaluated: bool,

    /// Risk assessment workpaper reference.
    pub workpaper_reference: Option<String>,
}

/// Risk level.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum AuditRiskLevel {
    Low,
    #[default]
    Medium,
    High,
    Maximum,
}

impl std::fmt::Display for AuditRiskLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Low => write!(f, "Low"),
            Self::Medium => write!(f, "Medium"),
            Self::High => write!(f, "High"),
            Self::Maximum => write!(f, "Maximum"),
        }
    }
}

/// Planned response trail node.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseTrailNode {
    /// Response ID.
    pub response_id: Uuid,

    /// Response description.
    pub response_description: String,

    /// Type of response.
    pub response_type: ResponseType,

    /// Risk being addressed.
    pub risk_addressed: String,

    /// Nature of procedure.
    pub procedure_nature: ProcedureNature,

    /// Timing of procedure.
    pub procedure_timing: ProcedureTiming,

    /// Extent of procedure.
    pub procedure_extent: String,

    /// Staff assigned.
    pub staff_assigned: Vec<String>,

    /// Budgeted hours.
    pub budgeted_hours: Option<f64>,
}

impl ResponseTrailNode {
    /// Create a new response trail node.
    pub fn new(response_description: impl Into<String>, response_type: ResponseType) -> Self {
        Self {
            response_id: Uuid::now_v7(),
            response_description: response_description.into(),
            response_type,
            risk_addressed: String::new(),
            procedure_nature: ProcedureNature::Substantive,
            procedure_timing: ProcedureTiming::YearEnd,
            procedure_extent: String::new(),
            staff_assigned: Vec::new(),
            budgeted_hours: None,
        }
    }
}

/// Type of audit response.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ResponseType {
    /// Test of controls.
    TestOfControls,
    /// Substantive procedures.
    #[default]
    Substantive,
    /// Combined approach.
    Combined,
    /// Overall response.
    Overall,
}

/// Nature of audit procedure.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ProcedureNature {
    /// Inspection of records/documents.
    Inspection,
    /// Physical observation.
    Observation,
    /// External confirmation.
    Confirmation,
    /// Recalculation.
    Recalculation,
    /// Reperformance.
    Reperformance,
    /// Analytical procedures.
    Analytical,
    /// Inquiry.
    Inquiry,
    #[default]
    /// Substantive testing.
    Substantive,
}

/// Timing of audit procedure.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ProcedureTiming {
    /// At interim date.
    Interim,
    /// At year-end.
    #[default]
    YearEnd,
    /// Roll-forward from interim to year-end.
    RollForward,
    /// Throughout the period.
    Continuous,
}

/// Procedure performed trail node.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcedureTrailNode {
    /// Procedure ID.
    pub procedure_id: Uuid,

    /// Linked response ID.
    pub response_id: Option<Uuid>,

    /// Procedure description.
    pub procedure_description: String,

    /// Date performed.
    pub date_performed: chrono::NaiveDate,

    /// Performed by.
    pub performed_by: String,

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

    /// Hours spent.
    pub hours_spent: Option<f64>,

    /// Population tested.
    pub population_size: Option<u64>,

    /// Sample size.
    pub sample_size: Option<u64>,

    /// Exceptions found.
    pub exceptions_found: u32,

    /// Results summary.
    pub results_summary: String,

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

impl ProcedureTrailNode {
    /// Create a new procedure trail node.
    pub fn new(
        procedure_description: impl Into<String>,
        date_performed: chrono::NaiveDate,
        performed_by: impl Into<String>,
    ) -> Self {
        Self {
            procedure_id: Uuid::now_v7(),
            response_id: None,
            procedure_description: procedure_description.into(),
            date_performed,
            performed_by: performed_by.into(),
            reviewed_by: None,
            hours_spent: None,
            population_size: None,
            sample_size: None,
            exceptions_found: 0,
            results_summary: String::new(),
            workpaper_reference: None,
        }
    }
}

/// Evidence trail node.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvidenceTrailNode {
    /// Evidence ID.
    pub evidence_id: Uuid,

    /// Linked procedure ID.
    pub procedure_id: Option<Uuid>,

    /// Evidence type.
    pub evidence_type: EvidenceType,

    /// Evidence description.
    pub evidence_description: String,

    /// Source of evidence.
    pub source: EvidenceSource,

    /// Reliability assessment.
    pub reliability: EvidenceReliability,

    /// Relevance to assertion.
    pub relevance: EvidenceRelevance,

    /// Document reference.
    pub document_reference: Option<String>,

    /// Date obtained.
    pub date_obtained: chrono::NaiveDate,
}

impl EvidenceTrailNode {
    /// Create a new evidence trail node.
    pub fn new(
        evidence_type: EvidenceType,
        evidence_description: impl Into<String>,
        source: EvidenceSource,
    ) -> Self {
        Self {
            evidence_id: Uuid::now_v7(),
            procedure_id: None,
            evidence_type,
            evidence_description: evidence_description.into(),
            source,
            reliability: EvidenceReliability::Moderate,
            relevance: EvidenceRelevance::Relevant,
            document_reference: None,
            date_obtained: chrono::Utc::now().date_naive(),
        }
    }
}

/// Type of audit evidence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceType {
    /// Physical examination.
    Physical,
    /// External confirmation.
    Confirmation,
    /// Documentary - external source.
    DocumentaryExternal,
    /// Documentary - internal source.
    DocumentaryInternal,
    /// Mathematical recalculation.
    Recalculation,
    /// Analytical evidence.
    Analytical,
    /// Management representation.
    Representation,
    /// Observation.
    Observation,
    /// Inquiry response.
    Inquiry,
}

/// Source of audit evidence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceSource {
    /// External third party.
    ExternalThirdParty,
    /// External - client's records of external transactions.
    ExternalClientRecords,
    /// Internal to the entity.
    #[default]
    Internal,
    /// Auditor-generated.
    AuditorGenerated,
}

/// Reliability of audit evidence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceReliability {
    /// Low reliability.
    Low,
    /// Moderate reliability.
    #[default]
    Moderate,
    /// High reliability.
    High,
}

/// Relevance of audit evidence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceRelevance {
    /// Not relevant.
    NotRelevant,
    /// Partially relevant.
    PartiallyRelevant,
    /// Relevant.
    #[default]
    Relevant,
    /// Directly relevant.
    DirectlyRelevant,
}

/// Conclusion trail node.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConclusionTrailNode {
    /// Conclusion reached.
    pub conclusion_reached: bool,

    /// Conclusion text.
    pub conclusion_text: String,

    /// Conclusion type.
    pub conclusion_type: ConclusionType,

    /// Misstatements identified.
    pub misstatements_identified: Vec<MisstatementReference>,

    /// Sufficient appropriate evidence obtained.
    pub sufficient_evidence: bool,

    /// Further procedures required.
    pub further_procedures_required: bool,

    /// Reference to summary memo.
    pub summary_memo_reference: Option<String>,

    /// Preparer.
    pub prepared_by: String,

    /// Reviewer.
    pub reviewed_by: Option<String>,

    /// Date concluded.
    pub conclusion_date: Option<chrono::NaiveDate>,
}

/// Type of conclusion.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ConclusionType {
    /// No exceptions noted.
    #[default]
    Satisfactory,
    /// Minor issues, not material.
    SatisfactoryWithMinorIssues,
    /// Potential misstatement identified.
    PotentialMisstatement,
    /// Misstatement identified.
    MisstatementIdentified,
    /// Unable to conclude.
    UnableToConclude,
}

/// Misstatement reference in conclusion.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MisstatementReference {
    /// Misstatement ID.
    pub misstatement_id: Uuid,

    /// Description.
    pub description: String,

    /// Amount (if quantified).
    pub amount: Option<rust_decimal::Decimal>,

    /// Is it factual, judgmental, or projected.
    pub misstatement_type: MisstatementType,
}

/// Type of misstatement.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MisstatementType {
    /// Factual misstatement - no doubt.
    Factual,
    /// Judgmental misstatement - differences in estimates.
    Judgmental,
    /// Projected misstatement - extrapolated from sample.
    Projected,
}

/// Gap in the audit trail.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrailGap {
    /// Type of gap.
    pub gap_type: GapType,

    /// Description of the gap.
    pub description: String,

    /// Severity of the gap.
    pub severity: GapSeverity,

    /// Whether remediation is required.
    pub remediation_required: bool,
}

/// Type of audit trail gap.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GapType {
    /// Gap in risk assessment.
    RiskAssessment,
    /// Gap in planned responses.
    PlannedResponse,
    /// Gap in procedures performed.
    ProceduresPerformed,
    /// Gap in evidence.
    Evidence,
    /// Gap in conclusion.
    Conclusion,
    /// Gap in linkage between elements.
    Linkage,
    /// Gap in documentation.
    Documentation,
}

/// Severity of audit trail gap.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GapSeverity {
    /// Low severity - documentation issue only.
    Low,
    /// Medium severity - could affect conclusions.
    Medium,
    /// High severity - significant audit quality concern.
    High,
}

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

    #[test]
    fn test_audit_trail_creation() {
        let trail = AuditTrail::new(Uuid::now_v7(), "Revenue", Assertion::Occurrence);

        assert_eq!(trail.account_or_area, "Revenue");
        assert_eq!(trail.assertion, Assertion::Occurrence);
        assert!(!trail.is_complete());
    }

    #[test]
    fn test_gap_identification() {
        let mut trail = AuditTrail::new(Uuid::now_v7(), "Inventory", Assertion::Existence);

        trail.identify_gaps();

        // Should have gaps for all elements
        assert!(!trail.gaps_identified.is_empty());
        assert!(trail
            .gaps_identified
            .iter()
            .any(|g| matches!(g.gap_type, GapType::RiskAssessment)));
        assert!(trail
            .gaps_identified
            .iter()
            .any(|g| matches!(g.gap_type, GapType::Evidence)));
    }

    #[test]
    fn test_complete_trail() {
        let mut trail = AuditTrail::new(Uuid::now_v7(), "Cash", Assertion::Existence);

        // Populate all elements
        trail.risk_assessment.risk_identified = true;
        trail.risk_assessment.risk_description = "Risk of misappropriation".to_string();

        let response =
            ResponseTrailNode::new("Perform bank reconciliation", ResponseType::Substantive);
        let response_id = response.response_id;
        trail.planned_responses.push(response);

        let mut procedure = ProcedureTrailNode::new(
            "Reconciled bank to GL",
            chrono::NaiveDate::from_ymd_opt(2024, 1, 31).unwrap(),
            "Auditor A",
        );
        procedure.response_id = Some(response_id);
        trail.procedures_performed.push(procedure);

        trail.evidence_obtained.push(EvidenceTrailNode::new(
            EvidenceType::DocumentaryExternal,
            "Bank statement obtained",
            EvidenceSource::ExternalThirdParty,
        ));

        trail.conclusion.conclusion_reached = true;
        trail.conclusion.conclusion_type = ConclusionType::Satisfactory;
        trail.conclusion.sufficient_evidence = true;

        trail.identify_gaps();

        assert!(trail.is_complete());
        assert!(trail.gaps_identified.is_empty());
    }
}