datasynth-generators 2.4.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
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
//! Deferred Tax Engine (IAS 12 / ASC 740).
//!
//! Generates temporary differences between book and tax bases, computes
//! DTA and DTL balances, produces ETR reconciliation schedules, and
//! emits balanced GL journal entries for the deferred tax movements.
//!
//! # Journal entry logic
//!
//! For each company the engine generates two JEs:
//!
//! | Scenario | Debit | Credit |
//! |----------|-------|--------|
//! | Net DTL  | Tax Expense (8000) | Deferred Tax Liability (2500) |
//! | Net DTA  | Deferred Tax Asset (1600) | Tax Benefit (8000) |

use chrono::{Datelike, NaiveDate};
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;

use datasynth_core::accounts::{expense_accounts, tax_accounts};
use datasynth_core::models::deferred_tax::{
    DeferredTaxRollforward, DeferredTaxType, PermanentDifference, TaxRateReconciliation,
    TemporaryDifference,
};
use datasynth_core::models::journal_entry::{
    BusinessProcess, JournalEntry, JournalEntryHeader, JournalEntryLine, TransactionSource,
};
use datasynth_core::utils::seeded_rng;

// ---------------------------------------------------------------------------
// Public snapshot
// ---------------------------------------------------------------------------

/// All deferred tax data generated for a single generation run.
#[derive(Debug, Clone, Default)]
pub struct DeferredTaxSnapshot {
    /// Temporary differences (book vs. tax basis) per company.
    pub temporary_differences: Vec<TemporaryDifference>,
    /// ETR reconciliation schedules per company.
    pub etr_reconciliations: Vec<TaxRateReconciliation>,
    /// Deferred tax rollforward schedules per company.
    pub rollforwards: Vec<DeferredTaxRollforward>,
    /// Balanced GL journal entries recording deferred tax movements.
    pub journal_entries: Vec<JournalEntry>,
}

// ---------------------------------------------------------------------------
// Generator
// ---------------------------------------------------------------------------

/// Generates deferred tax data under IAS 12 / ASC 740.
///
/// The generator is deterministic: given the same seed and inputs it always
/// produces the same output.
pub struct DeferredTaxGenerator {
    rng: ChaCha8Rng,
    counter: u64,
}

impl DeferredTaxGenerator {
    /// Create a new generator with the given seed.
    pub fn new(seed: u64) -> Self {
        Self {
            rng: seeded_rng(seed, 88),
            counter: 0,
        }
    }

    /// Generate a full deferred tax snapshot for the provided companies.
    ///
    /// # Arguments
    ///
    /// * `companies` – slice of `(company_code, country_code)` tuples.
    /// * `posting_date` – balance sheet / period-end date for JE headers.
    /// * `journal_entries` – existing JEs used to estimate pre-tax income,
    ///   total assets and total revenue.
    pub fn generate(
        &mut self,
        companies: &[(&str, &str)],
        posting_date: NaiveDate,
        journal_entries: &[JournalEntry],
    ) -> DeferredTaxSnapshot {
        let mut snapshot = DeferredTaxSnapshot::default();

        for &(company_code, country_code) in companies {
            let statutory_rate = Self::statutory_rate(country_code);
            let pre_tax_income = self.estimate_pre_tax_income(company_code, journal_entries);

            // Compute total assets and total revenue directly from journal entries.
            // Fall back to income-based heuristics only when no JE data is available.
            let total_assets =
                Self::compute_total_assets(company_code, journal_entries).max(dec!(1_000_000));
            let total_revenue =
                Self::compute_total_revenue(company_code, journal_entries).max(dec!(500_000));

            let period_label = format!("FY{}", posting_date.year());

            // Determine framework from country: US entities use US GAAP (ASC);
            // all others default to IFRS (IAS/IFRS standards).
            let is_ifrs = !matches!(country_code.to_uppercase().as_str(), "US");

            // 1. Temporary differences
            let diffs = self.generate_temp_diffs_with_framework(
                company_code,
                pre_tax_income,
                total_assets,
                total_revenue,
                is_ifrs,
            );
            let (dta, dtl) = compute_dta_dtl(&diffs, statutory_rate);

            // 2. ETR reconciliation
            let etr = self.build_etr_reconciliation(
                company_code,
                &period_label,
                pre_tax_income,
                statutory_rate,
            );

            // 3. Rollforward
            let rollforward = self.build_rollforward(company_code, &period_label, dta, dtl);

            // 4. Journal entries
            let jes = self.build_journal_entries(company_code, posting_date, dta, dtl);

            snapshot.temporary_differences.extend(diffs);
            snapshot.etr_reconciliations.push(etr);
            snapshot.rollforwards.push(rollforward);
            snapshot.journal_entries.extend(jes);
        }

        snapshot
    }

    // -----------------------------------------------------------------------
    // Temporary differences
    // -----------------------------------------------------------------------

    /// Generate temporary differences using framework-appropriate standard references.
    ///
    /// `is_ifrs` – when `true`, references use IAS/IFRS standards; when `false`,
    /// references use US GAAP ASC standards.
    fn generate_temp_diffs_with_framework(
        &mut self,
        entity_code: &str,
        _pre_tax_income: Decimal,
        total_assets: Decimal,
        revenue_proxy: Decimal,
        is_ifrs: bool,
    ) -> Vec<TemporaryDifference> {
        // Select standard references based on framework.
        let std_depreciation = if is_ifrs { "IAS 16" } else { "ASC 360" };
        let std_accruals = if is_ifrs { "IAS 37" } else { "ASC 450" };
        let std_receivables = if is_ifrs { "IFRS 9" } else { "ASC 310" };
        let std_inventory = if is_ifrs { "IAS 2" } else { "ASC 330" };
        let std_leases = if is_ifrs { "IFRS 16" } else { "ASC 842" };
        let std_warranty = if is_ifrs { "IAS 37" } else { "ASC 460" };
        let std_sbc = if is_ifrs { "IFRS 2" } else { "ASC 718" };
        let std_rd = if is_ifrs { "IAS 38" } else { "ASC 730" };

        let templates: Vec<(&str, &str, DeferredTaxType, Option<&str>, Decimal, Decimal)> = vec![
            // (description, account, type, standard, book_basis, tax_basis)
            // Accelerated depreciation – MACRS/capital allowances
            (
                "Accelerated depreciation (MACRS / capital allowances)",
                datasynth_core::accounts::control_accounts::FIXED_ASSETS,
                DeferredTaxType::Liability,
                Some(std_depreciation),
                // book NBV: 8-12% of total assets
                total_assets * self.rand_decimal(dec!(0.08), dec!(0.12)),
                // tax NBV: lower because accelerated → 1.4-1.6x book depreciation taken
                total_assets * self.rand_decimal(dec!(0.05), dec!(0.09)),
            ),
            // Accrued expenses (deductible when paid)
            (
                "Accrued expenses (deductible when paid)",
                datasynth_core::accounts::liability_accounts::ACCRUED_EXPENSES,
                DeferredTaxType::Asset,
                Some(std_accruals),
                revenue_proxy * self.rand_decimal(dec!(0.01), dec!(0.03)),
                Decimal::ZERO,
            ),
            // Allowance for doubtful accounts
            (
                "Allowance for doubtful accounts",
                datasynth_core::accounts::control_accounts::AR_CONTROL,
                DeferredTaxType::Asset,
                Some(std_receivables),
                revenue_proxy * self.rand_decimal(dec!(0.005), dec!(0.015)),
                Decimal::ZERO,
            ),
            // Inventory write-down (lower of cost or NRV)
            (
                "Inventory write-down (LCM / NRV)",
                datasynth_core::accounts::control_accounts::INVENTORY,
                DeferredTaxType::Asset,
                Some(std_inventory),
                total_assets * self.rand_decimal(dec!(0.005), dec!(0.015)),
                Decimal::ZERO,
            ),
            // Lease ROU asset (IFRS 16 / ASC 842 – operating for tax)
            (
                "Right-of-use asset – operating lease (tax: rental deduction)",
                expense_accounts::RENT,
                DeferredTaxType::Liability,
                Some(std_leases),
                total_assets * self.rand_decimal(dec!(0.02), dec!(0.06)),
                Decimal::ZERO,
            ),
            // Warranty provision
            (
                "Warranty provision (deductible when paid)",
                datasynth_core::accounts::liability_accounts::ACCRUED_EXPENSES,
                DeferredTaxType::Asset,
                Some(std_warranty),
                revenue_proxy * self.rand_decimal(dec!(0.003), dec!(0.010)),
                Decimal::ZERO,
            ),
            // Share-based compensation (IFRS 2 / ASC 718)
            (
                "Share-based compensation (book > tax until exercise)",
                datasynth_core::accounts::expense_accounts::BENEFITS,
                DeferredTaxType::Asset,
                Some(std_sbc),
                revenue_proxy * self.rand_decimal(dec!(0.005), dec!(0.012)),
                Decimal::ZERO,
            ),
            // Capitalised development costs (book; immediately expensed for tax)
            (
                "Capitalised development costs (expensed for tax)",
                datasynth_core::accounts::control_accounts::FIXED_ASSETS,
                DeferredTaxType::Liability,
                Some(std_rd),
                total_assets * self.rand_decimal(dec!(0.01), dec!(0.04)),
                Decimal::ZERO,
            ),
        ];

        // Pick 5-8 templates
        let n = self.rng.random_range(5usize..=8);
        let mut indices: Vec<usize> = (0..templates.len()).collect();
        indices.shuffle(&mut self.rng);
        indices.truncate(n);
        indices.sort();

        indices
            .iter()
            .map(|&i| {
                let (desc, account, dtype, standard, book, tax) = &templates[i];
                let book = book.round_dp(2);
                let tax = tax.round_dp(2);
                let difference = (book - tax).round_dp(2);
                self.counter += 1;
                TemporaryDifference {
                    id: format!("TDIFF-{entity_code}-{:05}", self.counter),
                    entity_code: entity_code.to_string(),
                    account: account.to_string(),
                    description: desc.to_string(),
                    book_basis: book,
                    tax_basis: tax,
                    difference,
                    deferred_type: *dtype,
                    originating_standard: standard.map(|s| s.to_string()),
                }
            })
            .collect()
    }

    // -----------------------------------------------------------------------
    // ETR reconciliation
    // -----------------------------------------------------------------------

    fn build_etr_reconciliation(
        &mut self,
        entity_code: &str,
        period: &str,
        pre_tax_income: Decimal,
        statutory_rate: Decimal,
    ) -> TaxRateReconciliation {
        let expected_tax = (pre_tax_income * statutory_rate).round_dp(2);

        // Generate 3-5 permanent differences
        let perm_diff_templates: Vec<(&str, Decimal, Decimal)> = vec![
            // (description, book_amount_as_pct_of_pti, tax_effect_multiplier)
            // Meals & entertainment (50% disallowed)
            (
                "Meals & entertainment (50% non-deductible)",
                pre_tax_income * self.rand_decimal(dec!(0.002), dec!(0.006)),
                // Only 50% is disallowed → tax effect = 50% × amount × rate
                pre_tax_income * self.rand_decimal(dec!(0.001), dec!(0.003)) * statutory_rate,
            ),
            // Municipal bond interest (tax-exempt)
            (
                "Tax-exempt municipal bond interest",
                -(pre_tax_income * self.rand_decimal(dec!(0.005), dec!(0.015))),
                -(pre_tax_income * self.rand_decimal(dec!(0.005), dec!(0.015)) * statutory_rate),
            ),
            // Non-deductible fines & penalties
            (
                "Non-deductible fines & penalties",
                pre_tax_income * self.rand_decimal(dec!(0.001), dec!(0.003)),
                pre_tax_income * self.rand_decimal(dec!(0.001), dec!(0.003)) * statutory_rate,
            ),
            // R&D tax credit (reduces tax below statutory)
            (
                "Research & development tax credits",
                -(pre_tax_income * self.rand_decimal(dec!(0.005), dec!(0.020))),
                -(pre_tax_income * self.rand_decimal(dec!(0.005), dec!(0.020))),
            ),
            // Stock-based compensation (ASC 718 / IFRS 2 excess benefit)
            (
                "Stock-based compensation – excess tax benefit",
                -(pre_tax_income * self.rand_decimal(dec!(0.002), dec!(0.008))),
                -(pre_tax_income * self.rand_decimal(dec!(0.002), dec!(0.008)) * statutory_rate),
            ),
            // Foreign-derived intangible income (US FDII deduction)
            (
                "Foreign-derived intangible income (FDII) deduction",
                -(pre_tax_income * self.rand_decimal(dec!(0.003), dec!(0.010))),
                -(pre_tax_income * self.rand_decimal(dec!(0.003), dec!(0.010)) * statutory_rate),
            ),
            // Officer compensation disallowance (§162(m) US)
            (
                "Officer compensation in excess of §162(m) limit",
                pre_tax_income * self.rand_decimal(dec!(0.001), dec!(0.004)),
                pre_tax_income * self.rand_decimal(dec!(0.001), dec!(0.004)) * statutory_rate,
            ),
        ];

        let n = self.rng.random_range(3usize..=5);
        let mut indices: Vec<usize> = (0..perm_diff_templates.len()).collect();
        indices.shuffle(&mut self.rng);
        indices.truncate(n);
        indices.sort();

        let permanent_differences: Vec<PermanentDifference> = indices
            .iter()
            .map(|&i| {
                let (desc, amount, tax_effect) = &perm_diff_templates[i];
                PermanentDifference {
                    description: desc.to_string(),
                    amount: amount.round_dp(2),
                    tax_effect: tax_effect.round_dp(2),
                }
            })
            .collect();

        let total_perm_effect: Decimal = permanent_differences.iter().map(|p| p.tax_effect).sum();
        let actual_tax = (expected_tax + total_perm_effect).round_dp(2);
        let effective_rate = if pre_tax_income != Decimal::ZERO {
            (actual_tax / pre_tax_income).round_dp(6)
        } else {
            statutory_rate
        };

        TaxRateReconciliation {
            entity_code: entity_code.to_string(),
            period: period.to_string(),
            pre_tax_income: pre_tax_income.round_dp(2),
            statutory_rate,
            expected_tax,
            permanent_differences,
            effective_rate,
            actual_tax,
        }
    }

    // -----------------------------------------------------------------------
    // Rollforward
    // -----------------------------------------------------------------------

    fn build_rollforward(
        &mut self,
        entity_code: &str,
        period: &str,
        closing_dta: Decimal,
        closing_dtl: Decimal,
    ) -> DeferredTaxRollforward {
        self.build_rollforward_with_prior(entity_code, period, closing_dta, closing_dtl, None, None)
    }

    /// Build a deferred-tax rollforward, optionally accepting prior-period closing balances.
    ///
    /// Pass `prior_closing_dta` / `prior_closing_dtl` for subsequent periods in a
    /// multi-period series.  Both default to zero (first period) when `None`.
    fn build_rollforward_with_prior(
        &mut self,
        entity_code: &str,
        period: &str,
        closing_dta: Decimal,
        closing_dtl: Decimal,
        prior_closing_dta: Option<Decimal>,
        prior_closing_dtl: Option<Decimal>,
    ) -> DeferredTaxRollforward {
        // Opening balances: use prior period values when provided, else zero (first period).
        let opening_dta = prior_closing_dta.unwrap_or(Decimal::ZERO);
        let opening_dtl = prior_closing_dtl.unwrap_or(Decimal::ZERO);
        let current_year_movement = (closing_dta - opening_dta) - (closing_dtl - opening_dtl);

        DeferredTaxRollforward {
            entity_code: entity_code.to_string(),
            period: period.to_string(),
            opening_dta,
            opening_dtl,
            current_year_movement: current_year_movement.round_dp(2),
            closing_dta: closing_dta.round_dp(2),
            closing_dtl: closing_dtl.round_dp(2),
        }
    }

    // -----------------------------------------------------------------------
    // Journal entries
    // -----------------------------------------------------------------------

    fn build_journal_entries(
        &mut self,
        company_code: &str,
        posting_date: NaiveDate,
        dta: Decimal,
        dtl: Decimal,
    ) -> Vec<JournalEntry> {
        let mut jes = Vec::new();

        // DTL entry: DR Tax Expense / CR Deferred Tax Liability
        if dtl > Decimal::ZERO {
            self.counter += 1;
            let mut header = JournalEntryHeader::new(company_code.to_string(), posting_date);
            header.document_type = "TAX_DEFERRED".to_string();
            header.created_by = "DEFERRED_TAX_ENGINE".to_string();
            header.source = TransactionSource::Automated;
            header.business_process = Some(BusinessProcess::R2R);
            header.header_text = Some(format!(
                "Deferred tax liability – period {}",
                posting_date.format("%Y-%m")
            ));

            let doc_id = header.document_id;
            let mut je = JournalEntry::new(header);
            je.add_line(JournalEntryLine::debit(
                doc_id,
                1,
                tax_accounts::TAX_EXPENSE.to_string(),
                dtl,
            ));
            je.add_line(JournalEntryLine::credit(
                doc_id,
                2,
                tax_accounts::DEFERRED_TAX_LIABILITY.to_string(),
                dtl,
            ));
            jes.push(je);
        }

        // DTA entry: DR Deferred Tax Asset / CR Tax Benefit (credit to Tax Expense)
        if dta > Decimal::ZERO {
            self.counter += 1;
            let mut header = JournalEntryHeader::new(company_code.to_string(), posting_date);
            header.document_type = "TAX_DEFERRED".to_string();
            header.created_by = "DEFERRED_TAX_ENGINE".to_string();
            header.source = TransactionSource::Automated;
            header.business_process = Some(BusinessProcess::R2R);
            header.header_text = Some(format!(
                "Deferred tax asset – period {}",
                posting_date.format("%Y-%m")
            ));

            let doc_id = header.document_id;
            let mut je = JournalEntry::new(header);
            je.add_line(JournalEntryLine::debit(
                doc_id,
                1,
                tax_accounts::DEFERRED_TAX_ASSET.to_string(),
                dta,
            ));
            je.add_line(JournalEntryLine::credit(
                doc_id,
                2,
                tax_accounts::TAX_EXPENSE.to_string(),
                dta,
            ));
            jes.push(je);
        }

        jes
    }

    // -----------------------------------------------------------------------
    // Helpers
    // -----------------------------------------------------------------------

    /// Compute total assets for a company from journal entries.
    ///
    /// Asset accounts start with "1". Net assets = Σ(debit − credit) on asset lines.
    fn compute_total_assets(company_code: &str, journal_entries: &[JournalEntry]) -> Decimal {
        use datasynth_core::accounts::AccountCategory;
        let mut net = Decimal::ZERO;
        for je in journal_entries {
            if je.header.company_code != company_code {
                continue;
            }
            for line in &je.lines {
                if matches!(
                    AccountCategory::from_account(&line.gl_account),
                    AccountCategory::Asset
                ) {
                    net += line.debit_amount - line.credit_amount;
                }
            }
        }
        net.abs()
    }

    /// Compute total revenue for a company from journal entries.
    ///
    /// Revenue accounts start with "4". Revenue is credit-normal.
    fn compute_total_revenue(company_code: &str, journal_entries: &[JournalEntry]) -> Decimal {
        use datasynth_core::accounts::AccountCategory;
        let mut revenue = Decimal::ZERO;
        for je in journal_entries {
            if je.header.company_code != company_code {
                continue;
            }
            for line in &je.lines {
                if matches!(
                    AccountCategory::from_account(&line.gl_account),
                    AccountCategory::Revenue
                ) {
                    revenue += line.credit_amount - line.debit_amount;
                }
            }
        }
        revenue.max(Decimal::ZERO)
    }

    /// Look up the statutory corporate income tax rate for a country.
    fn statutory_rate(country_code: &str) -> Decimal {
        match country_code.to_uppercase().as_str() {
            "US" => dec!(0.21),
            "DE" => dec!(0.30),
            "GB" | "UK" => dec!(0.25),
            "FR" => dec!(0.25),
            "NL" => dec!(0.258),
            "IE" => dec!(0.125),
            "CH" => dec!(0.15),
            "CA" => dec!(0.265),
            "AU" => dec!(0.30),
            "JP" => dec!(0.2928),
            "SG" => dec!(0.17),
            "CN" => dec!(0.25),
            "IN" => dec!(0.2517),
            "BR" => dec!(0.34),
            _ => dec!(0.21), // default to US rate
        }
    }

    /// Estimate pre-tax income for a company from existing journal entries.
    ///
    /// Pre-tax income = Σ revenue account credits − Σ expense account debits.
    /// Falls back to a heuristic if no entries match.
    fn estimate_pre_tax_income(
        &self,
        company_code: &str,
        journal_entries: &[JournalEntry],
    ) -> Decimal {
        use datasynth_core::accounts::AccountCategory;

        let mut revenue = Decimal::ZERO;
        let mut expenses = Decimal::ZERO;

        for je in journal_entries {
            if je.header.company_code != company_code {
                continue;
            }
            for line in &je.lines {
                let cat = AccountCategory::from_account(&line.gl_account);
                match cat {
                    AccountCategory::Revenue => {
                        // Revenue accounts are credit-normal; credits increase revenue
                        revenue += line.credit_amount;
                        revenue -= line.debit_amount;
                    }
                    AccountCategory::Cogs
                    | AccountCategory::OperatingExpense
                    | AccountCategory::OtherIncomeExpense => {
                        // Expense accounts are debit-normal
                        expenses += line.debit_amount;
                        expenses -= line.credit_amount;
                    }
                    _ => {}
                }
            }
        }

        let pti = (revenue - expenses).round_dp(2);
        if pti == Decimal::ZERO {
            // Fallback: synthetic income so we can still generate meaningful ratios
            dec!(1_000_000)
        } else {
            pti
        }
    }

    /// Random decimal in `[min, max]`.
    fn rand_decimal(&mut self, min: Decimal, max: Decimal) -> Decimal {
        let range: f64 = (max - min).to_string().parse().unwrap_or(0.0);
        let min_f: f64 = min.to_string().parse().unwrap_or(0.0);
        let v = min_f + self.rng.random::<f64>() * range;
        Decimal::try_from(v).unwrap_or(min).round_dp(6)
    }
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Compute total DTA and DTL from a slice of temporary differences.
///
/// `dta = Σ |difference| × statutory_rate` for DTA-type diffs.
/// `dtl = Σ |difference| × statutory_rate` for DTL-type diffs.
pub fn compute_dta_dtl(
    diffs: &[TemporaryDifference],
    statutory_rate: Decimal,
) -> (Decimal, Decimal) {
    let mut dta = Decimal::ZERO;
    let mut dtl = Decimal::ZERO;
    for d in diffs {
        let effect = (d.difference.abs() * statutory_rate).round_dp(2);
        match d.deferred_type {
            DeferredTaxType::Asset => dta += effect,
            DeferredTaxType::Liability => dtl += effect,
        }
    }
    (dta.round_dp(2), dtl.round_dp(2))
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn sample_date() -> NaiveDate {
        NaiveDate::from_ymd_opt(2024, 12, 31).unwrap()
    }

    #[test]
    fn test_generate_returns_data_for_each_company() {
        let mut gen = DeferredTaxGenerator::new(42);
        let companies = vec![("C001", "US"), ("C002", "DE")];
        let snapshot = gen.generate(&companies, sample_date(), &[]);

        // At least 5 temp diffs per company (5–8 each)
        assert!(
            snapshot.temporary_differences.len() >= 2 * 5,
            "Expected ≥10 temp diffs, got {}",
            snapshot.temporary_differences.len()
        );
        assert_eq!(snapshot.etr_reconciliations.len(), 2);
        assert_eq!(snapshot.rollforwards.len(), 2);
    }

    #[test]
    fn test_dta_dtl_computation() {
        let diffs = vec![
            TemporaryDifference {
                id: "T1".into(),
                entity_code: "C001".into(),
                account: "1500".into(),
                description: "Depreciation".into(),
                book_basis: dec!(100_000),
                tax_basis: dec!(80_000),
                difference: dec!(20_000),
                deferred_type: DeferredTaxType::Liability,
                originating_standard: None,
            },
            TemporaryDifference {
                id: "T2".into(),
                entity_code: "C001".into(),
                account: "2200".into(),
                description: "Accruals".into(),
                book_basis: dec!(50_000),
                tax_basis: Decimal::ZERO,
                difference: dec!(50_000),
                deferred_type: DeferredTaxType::Asset,
                originating_standard: None,
            },
        ];
        let (dta, dtl) = compute_dta_dtl(&diffs, dec!(0.21));
        assert_eq!(dtl, (dec!(20_000) * dec!(0.21)).round_dp(2));
        assert_eq!(dta, (dec!(50_000) * dec!(0.21)).round_dp(2));
    }

    #[test]
    fn test_etr_reconciliation_math() {
        let mut gen = DeferredTaxGenerator::new(7);
        let companies = vec![("C001", "US")];
        let snap = gen.generate(&companies, sample_date(), &[]);

        let etr = &snap.etr_reconciliations[0];
        let expected_tax = (etr.pre_tax_income * etr.statutory_rate).round_dp(2);
        assert_eq!(etr.expected_tax, expected_tax, "expected_tax mismatch");

        // actual_tax = expected_tax + Σ permanent_diff.tax_effect
        let total_perm: Decimal = etr.permanent_differences.iter().map(|p| p.tax_effect).sum();
        let expected_actual = (expected_tax + total_perm).round_dp(2);
        assert_eq!(etr.actual_tax, expected_actual, "actual_tax mismatch");

        // effective_rate = actual_tax / pre_tax_income
        if etr.pre_tax_income != Decimal::ZERO {
            let expected_etr = (etr.actual_tax / etr.pre_tax_income).round_dp(6);
            assert_eq!(etr.effective_rate, expected_etr, "effective_rate mismatch");
        }
    }

    #[test]
    fn test_rollforward_opening_plus_movement_equals_closing() {
        let mut gen = DeferredTaxGenerator::new(13);
        let snap = gen.generate(&[("C001", "GB")], sample_date(), &[]);

        let rf = &snap.rollforwards[0];
        // closing_dta = opening_dta + dta_movement
        // closing_dtl = opening_dtl + dtl_movement
        // current_year_movement = (closing_dta - opening_dta) - (closing_dtl - opening_dtl)
        let implied_movement =
            (rf.closing_dta - rf.opening_dta) - (rf.closing_dtl - rf.opening_dtl);
        assert_eq!(
            rf.current_year_movement, implied_movement,
            "Rollforward movement check failed"
        );
    }

    #[test]
    fn test_journal_entries_are_balanced() {
        let mut gen = DeferredTaxGenerator::new(42);
        let snap = gen.generate(&[("C001", "US")], sample_date(), &[]);

        for je in &snap.journal_entries {
            let total_debit: Decimal = je.lines.iter().map(|l| l.debit_amount).sum();
            let total_credit: Decimal = je.lines.iter().map(|l| l.credit_amount).sum();
            assert_eq!(
                total_debit, total_credit,
                "JE {} is not balanced: debits={}, credits={}",
                je.header.document_id, total_debit, total_credit
            );
        }
    }

    #[test]
    fn test_journal_entries_have_tax_document_type() {
        let mut gen = DeferredTaxGenerator::new(42);
        let snap = gen.generate(&[("C001", "US"), ("C002", "DE")], sample_date(), &[]);

        for je in &snap.journal_entries {
            assert!(
                je.header.document_type.contains("TAX"),
                "Expected document_type to contain 'TAX', got '{}'",
                je.header.document_type
            );
        }
    }

    #[test]
    fn test_deterministic() {
        let companies = vec![("C001", "US")];
        let mut gen1 = DeferredTaxGenerator::new(99);
        let snap1 = gen1.generate(&companies, sample_date(), &[]);

        let mut gen2 = DeferredTaxGenerator::new(99);
        let snap2 = gen2.generate(&companies, sample_date(), &[]);

        assert_eq!(
            snap1.temporary_differences.len(),
            snap2.temporary_differences.len()
        );
        assert_eq!(
            snap1.etr_reconciliations[0].actual_tax,
            snap2.etr_reconciliations[0].actual_tax
        );
        assert_eq!(
            snap1.rollforwards[0].closing_dta,
            snap2.rollforwards[0].closing_dta
        );
    }
}