datasynth-standards 2.2.0

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
//! Audit Opinion Models (ISA 700/705/706/701).
//!
//! Implements audit opinion formation and reporting:
//! - ISA 700: Forming an Opinion
//! - ISA 701: Key Audit Matters
//! - ISA 705: Modifications to the Opinion
//! - ISA 706: Emphasis of Matter and Other Matter Paragraphs

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

/// Audit opinion record.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditOpinion {
    /// Unique opinion identifier.
    pub opinion_id: Uuid,

    /// Engagement ID.
    pub engagement_id: Uuid,

    /// Opinion date.
    pub opinion_date: NaiveDate,

    /// Type of opinion.
    pub opinion_type: OpinionType,

    /// Key Audit Matters (ISA 701).
    pub key_audit_matters: Vec<KeyAuditMatter>,

    /// Modification details (if modified opinion).
    pub modification: Option<OpinionModification>,

    /// Emphasis of Matter paragraphs (ISA 706).
    pub emphasis_of_matter: Vec<EmphasisOfMatter>,

    /// Other Matter paragraphs (ISA 706).
    pub other_matter: Vec<OtherMatter>,

    /// Going concern conclusion.
    pub going_concern_conclusion: GoingConcernConclusion,

    /// Material uncertainty related to going concern.
    pub material_uncertainty_going_concern: bool,

    /// PCAOB compliance elements (for US issuers).
    pub pcaob_compliance: Option<PcaobOpinionElements>,

    /// Financial statement period end.
    pub period_end_date: NaiveDate,

    /// Entity name.
    pub entity_name: String,

    /// Auditor name/firm.
    pub auditor_name: String,

    /// Engagement partner.
    pub engagement_partner: String,

    /// Whether EQCR was performed.
    pub eqcr_performed: bool,
}

impl AuditOpinion {
    /// Create a new audit opinion.
    pub fn new(
        engagement_id: Uuid,
        opinion_date: NaiveDate,
        opinion_type: OpinionType,
        entity_name: impl Into<String>,
        period_end_date: NaiveDate,
    ) -> Self {
        Self {
            opinion_id: Uuid::now_v7(),
            engagement_id,
            opinion_date,
            opinion_type,
            key_audit_matters: Vec::new(),
            modification: None,
            emphasis_of_matter: Vec::new(),
            other_matter: Vec::new(),
            going_concern_conclusion: GoingConcernConclusion::default(),
            material_uncertainty_going_concern: false,
            pcaob_compliance: None,
            period_end_date,
            entity_name: entity_name.into(),
            auditor_name: String::new(),
            engagement_partner: String::new(),
            eqcr_performed: false,
        }
    }

    /// Check if opinion is unmodified.
    pub fn is_unmodified(&self) -> bool {
        matches!(self.opinion_type, OpinionType::Unmodified)
    }

    /// Check if opinion is modified.
    pub fn is_modified(&self) -> bool {
        !self.is_unmodified()
    }

    /// Add a Key Audit Matter.
    pub fn add_kam(&mut self, kam: KeyAuditMatter) {
        self.key_audit_matters.push(kam);
    }

    /// Add Emphasis of Matter.
    pub fn add_eom(&mut self, eom: EmphasisOfMatter) {
        self.emphasis_of_matter.push(eom);
    }
}

/// Type of audit opinion.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum OpinionType {
    /// Clean opinion - financial statements are fairly presented.
    #[default]
    Unmodified,
    /// Material misstatement but not pervasive.
    Qualified,
    /// Material and pervasive misstatement.
    Adverse,
    /// Unable to obtain sufficient appropriate audit evidence.
    Disclaimer,
}

impl std::fmt::Display for OpinionType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Unmodified => write!(f, "Unmodified"),
            Self::Qualified => write!(f, "Qualified"),
            Self::Adverse => write!(f, "Adverse"),
            Self::Disclaimer => write!(f, "Disclaimer"),
        }
    }
}

/// Key Audit Matter (ISA 701).
///
/// Matters that were of most significance in the audit and required
/// significant auditor attention.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyAuditMatter {
    /// KAM identifier.
    pub kam_id: Uuid,

    /// Title/heading of the KAM.
    pub title: String,

    /// Description of why this matter was significant.
    pub significance_explanation: String,

    /// How the matter was addressed in the audit.
    pub audit_response: String,

    /// Related financial statement area.
    pub financial_statement_area: String,

    /// Risk of material misstatement level.
    pub romm_level: RiskLevel,

    /// Related findings (if any).
    pub related_finding_ids: Vec<Uuid>,

    /// Workpaper references.
    pub workpaper_references: Vec<String>,
}

impl KeyAuditMatter {
    /// Create a new Key Audit Matter.
    pub fn new(
        title: impl Into<String>,
        significance_explanation: impl Into<String>,
        audit_response: impl Into<String>,
        financial_statement_area: impl Into<String>,
    ) -> Self {
        Self {
            kam_id: Uuid::now_v7(),
            title: title.into(),
            significance_explanation: significance_explanation.into(),
            audit_response: audit_response.into(),
            financial_statement_area: financial_statement_area.into(),
            romm_level: RiskLevel::High,
            related_finding_ids: Vec::new(),
            workpaper_references: Vec::new(),
        }
    }
}

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

/// Opinion modification details (ISA 705).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpinionModification {
    /// Basis for modification.
    pub basis: ModificationBasis,

    /// Description of the matter.
    pub matter_description: String,

    /// Financial statement effects.
    pub financial_effects: FinancialEffects,

    /// Whether matter is pervasive.
    pub is_pervasive: bool,

    /// Affected financial statement areas.
    pub affected_areas: Vec<String>,

    /// Misstatement amount (if quantifiable).
    pub misstatement_amount: Option<rust_decimal::Decimal>,

    /// Related to prior period.
    pub relates_to_prior_period: bool,

    /// Related to going concern.
    pub relates_to_going_concern: bool,
}

impl OpinionModification {
    /// Create a new opinion modification.
    pub fn new(basis: ModificationBasis, matter_description: impl Into<String>) -> Self {
        Self {
            basis,
            matter_description: matter_description.into(),
            financial_effects: FinancialEffects::default(),
            is_pervasive: false,
            affected_areas: Vec::new(),
            misstatement_amount: None,
            relates_to_prior_period: false,
            relates_to_going_concern: false,
        }
    }
}

/// Basis for opinion modification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ModificationBasis {
    /// Material misstatement in the financial statements.
    MaterialMisstatement,
    /// Inability to obtain sufficient appropriate audit evidence.
    InabilityToObtainEvidence,
    /// Both misstatement and scope limitation.
    Both,
}

impl std::fmt::Display for ModificationBasis {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MaterialMisstatement => write!(f, "Material Misstatement"),
            Self::InabilityToObtainEvidence => write!(f, "Inability to Obtain Evidence"),
            Self::Both => write!(f, "Material Misstatement and Scope Limitation"),
        }
    }
}

/// Financial effects of modification matter.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FinancialEffects {
    /// Effect on assets.
    pub assets_effect: String,
    /// Effect on liabilities.
    pub liabilities_effect: String,
    /// Effect on equity.
    pub equity_effect: String,
    /// Effect on revenue.
    pub revenue_effect: String,
    /// Effect on expenses.
    pub expenses_effect: String,
    /// Effect on cash flows.
    pub cash_flows_effect: String,
    /// Effect on disclosures.
    pub disclosures_effect: String,
}

/// Emphasis of Matter paragraph (ISA 706).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmphasisOfMatter {
    /// EOM identifier.
    pub eom_id: Uuid,

    /// Matter being emphasized.
    pub matter: EomMatter,

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

    /// Reference to relevant notes.
    pub note_reference: String,

    /// Whether matter is appropriately presented and disclosed.
    pub appropriately_presented: bool,
}

impl EmphasisOfMatter {
    /// Create a new Emphasis of Matter.
    pub fn new(matter: EomMatter, description: impl Into<String>) -> Self {
        Self {
            eom_id: Uuid::now_v7(),
            matter,
            description: description.into(),
            note_reference: String::new(),
            appropriately_presented: true,
        }
    }
}

/// Common Emphasis of Matter topics.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EomMatter {
    /// Significant uncertainty about entity's ability to continue.
    GoingConcern,
    /// Major catastrophe affecting operations.
    MajorCatastrophe,
    /// Significant related party transactions.
    RelatedPartyTransactions,
    /// Significant subsequent event.
    SubsequentEvent,
    /// Change in accounting policy.
    AccountingPolicyChange,
    /// New accounting standard adoption.
    NewStandardAdoption,
    /// Unusually important litigation.
    Litigation,
    /// Regulatory action.
    RegulatoryAction,
    /// Other significant matter.
    Other,
}

impl std::fmt::Display for EomMatter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::GoingConcern => write!(f, "Going Concern"),
            Self::MajorCatastrophe => write!(f, "Major Catastrophe"),
            Self::RelatedPartyTransactions => write!(f, "Related Party Transactions"),
            Self::SubsequentEvent => write!(f, "Subsequent Event"),
            Self::AccountingPolicyChange => write!(f, "Accounting Policy Change"),
            Self::NewStandardAdoption => write!(f, "New Standard Adoption"),
            Self::Litigation => write!(f, "Litigation"),
            Self::RegulatoryAction => write!(f, "Regulatory Action"),
            Self::Other => write!(f, "Other"),
        }
    }
}

/// Other Matter paragraph (ISA 706).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OtherMatter {
    /// OM identifier.
    pub om_id: Uuid,

    /// Type of other matter.
    pub matter_type: OtherMatterType,

    /// Description.
    pub description: String,
}

impl OtherMatter {
    /// Create a new Other Matter.
    pub fn new(matter_type: OtherMatterType, description: impl Into<String>) -> Self {
        Self {
            om_id: Uuid::now_v7(),
            matter_type,
            description: description.into(),
        }
    }
}

/// Types of Other Matter paragraphs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OtherMatterType {
    /// Prior period financial statements audited by another auditor.
    PredecessorAuditor,
    /// Prior period not audited.
    PriorPeriodNotAudited,
    /// Supplementary information.
    SupplementaryInformation,
    /// Other matter relevant to users.
    Other,
}

/// Going concern conclusion (ISA 570).
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GoingConcernConclusion {
    /// Conclusion on going concern.
    pub conclusion: GoingConcernAssessment,

    /// Material uncertainty exists.
    pub material_uncertainty_exists: bool,

    /// If uncertainty exists, is it adequately disclosed.
    pub adequately_disclosed: bool,

    /// Events/conditions identified.
    pub events_conditions: Vec<String>,

    /// Management's plans to address.
    pub management_plans: String,

    /// Auditor's evaluation of management's plans.
    pub auditor_evaluation: String,

    /// Impact on opinion.
    pub opinion_impact: Option<OpinionType>,
}

/// Going concern assessment outcome.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum GoingConcernAssessment {
    /// No material uncertainty identified.
    #[default]
    NoMaterialUncertainty,
    /// Material uncertainty exists, adequately disclosed.
    MaterialUncertaintyAdequatelyDisclosed,
    /// Material uncertainty exists, not adequately disclosed.
    MaterialUncertaintyNotAdequatelyDisclosed,
    /// Going concern basis inappropriate.
    GoingConcernInappropriate,
}

impl std::fmt::Display for GoingConcernAssessment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NoMaterialUncertainty => write!(f, "No Material Uncertainty"),
            Self::MaterialUncertaintyAdequatelyDisclosed => {
                write!(f, "Material Uncertainty - Adequately Disclosed")
            }
            Self::MaterialUncertaintyNotAdequatelyDisclosed => {
                write!(f, "Material Uncertainty - Not Adequately Disclosed")
            }
            Self::GoingConcernInappropriate => write!(f, "Going Concern Basis Inappropriate"),
        }
    }
}

/// PCAOB-specific opinion elements for US issuers.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PcaobOpinionElements {
    /// Whether this is an integrated audit (SOX 404).
    pub is_integrated_audit: bool,

    /// ICFR opinion (for integrated audits).
    pub icfr_opinion: Option<IcfrOpinion>,

    /// Critical Audit Matters (PCAOB equivalent of KAMs).
    pub critical_audit_matters: Vec<KeyAuditMatter>,

    /// Auditor tenure disclosure.
    pub auditor_tenure_years: Option<u32>,

    /// PCAOB registration number.
    pub pcaob_registration_number: Option<String>,
}

impl PcaobOpinionElements {
    /// Create new PCAOB elements.
    pub fn new(is_integrated_audit: bool) -> Self {
        Self {
            is_integrated_audit,
            icfr_opinion: None,
            critical_audit_matters: Vec::new(),
            auditor_tenure_years: None,
            pcaob_registration_number: None,
        }
    }
}

/// ICFR (Internal Control over Financial Reporting) opinion.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IcfrOpinion {
    /// ICFR opinion type.
    pub opinion_type: IcfrOpinionType,

    /// Material weaknesses identified.
    pub material_weaknesses: Vec<MaterialWeakness>,

    /// Significant deficiencies identified.
    pub significant_deficiencies: Vec<String>,

    /// Scope limitations (if any).
    pub scope_limitations: Vec<String>,
}

/// ICFR opinion type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum IcfrOpinionType {
    /// ICFR is effective.
    #[default]
    Effective,
    /// Adverse opinion due to material weakness.
    Adverse,
    /// Disclaimer due to scope limitation.
    Disclaimer,
}

/// Material weakness in ICFR.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MaterialWeakness {
    /// Weakness identifier.
    pub weakness_id: Uuid,

    /// Description.
    pub description: String,

    /// Affected controls.
    pub affected_controls: Vec<String>,

    /// Affected accounts.
    pub affected_accounts: Vec<String>,

    /// Potential misstatement.
    pub potential_misstatement: String,
}

impl MaterialWeakness {
    /// Create a new material weakness.
    pub fn new(description: impl Into<String>) -> Self {
        Self {
            weakness_id: Uuid::now_v7(),
            description: description.into(),
            affected_controls: Vec::new(),
            affected_accounts: Vec::new(),
            potential_misstatement: String::new(),
        }
    }
}

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

    #[test]
    fn test_audit_opinion_creation() {
        let opinion = AuditOpinion::new(
            Uuid::now_v7(),
            NaiveDate::from_ymd_opt(2024, 3, 15).unwrap(),
            OpinionType::Unmodified,
            "Test Company Inc.",
            NaiveDate::from_ymd_opt(2023, 12, 31).unwrap(),
        );

        assert!(opinion.is_unmodified());
        assert!(!opinion.is_modified());
        assert!(opinion.key_audit_matters.is_empty());
    }

    #[test]
    fn test_modified_opinion() {
        let mut opinion = AuditOpinion::new(
            Uuid::now_v7(),
            NaiveDate::from_ymd_opt(2024, 3, 15).unwrap(),
            OpinionType::Qualified,
            "Test Company Inc.",
            NaiveDate::from_ymd_opt(2023, 12, 31).unwrap(),
        );

        opinion.modification = Some(OpinionModification::new(
            ModificationBasis::MaterialMisstatement,
            "Inventory was not properly valued",
        ));

        assert!(opinion.is_modified());
        assert!(opinion.modification.is_some());
    }

    #[test]
    fn test_key_audit_matter() {
        let kam = KeyAuditMatter::new(
            "Revenue Recognition",
            "Complex multi-element arrangements require significant judgment",
            "We tested controls over revenue recognition and performed substantive testing",
            "Revenue",
        );

        assert_eq!(kam.title, "Revenue Recognition");
        assert_eq!(kam.romm_level, RiskLevel::High);
    }

    #[test]
    fn test_going_concern() {
        let gc = GoingConcernConclusion {
            conclusion: GoingConcernAssessment::MaterialUncertaintyAdequatelyDisclosed,
            material_uncertainty_exists: true,
            adequately_disclosed: true,
            ..Default::default()
        };

        assert!(gc.material_uncertainty_exists);
        assert!(gc.adequately_disclosed);
    }

    #[test]
    fn test_pcaob_elements() {
        let mut pcaob = PcaobOpinionElements::new(true);
        pcaob.auditor_tenure_years = Some(5);
        pcaob.icfr_opinion = Some(IcfrOpinion {
            opinion_type: IcfrOpinionType::Effective,
            material_weaknesses: Vec::new(),
            significant_deficiencies: Vec::new(),
            scope_limitations: Vec::new(),
        });

        assert!(pcaob.is_integrated_audit);
        assert!(pcaob.icfr_opinion.is_some());
    }
}