datasynth-eval 2.2.0

Evaluation framework for synthetic financial data quality and coherence
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
//! Financial ratio analysis evaluator (ISA 520).
//!
//! Computes standard financial ratios from journal entry data and validates
//! them for reasonableness against industry benchmarks.

use datasynth_core::models::JournalEntry;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

/// Results of ratio analysis evaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RatioAnalysisResult {
    /// Entity/company code evaluated.
    pub entity_code: String,
    /// Fiscal period label (e.g. "2024-Q1").
    pub period: String,
    /// Computed financial ratios.
    pub ratios: FinancialRatios,
    /// Reasonableness checks against industry bounds.
    pub reasonableness_checks: Vec<RatioCheck>,
    /// True if all computable ratios are within industry bounds.
    pub passes: bool,
}

/// Standard financial ratios derived from journal entry data.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FinancialRatios {
    // --- Liquidity ---
    /// Current ratio: current assets / current liabilities.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub current_ratio: Option<Decimal>,
    /// Quick ratio: (current assets − inventory) / current liabilities.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub quick_ratio: Option<Decimal>,

    // --- Activity ---
    /// Days Sales Outstanding: AR / Revenue × 365.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub dso: Option<Decimal>,
    /// Days Payable Outstanding: AP / COGS × 365.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub dpo: Option<Decimal>,
    /// Inventory turnover: COGS / inventory.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub inventory_turnover: Option<Decimal>,

    // --- Profitability ---
    /// Gross margin: (revenue − COGS) / revenue.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub gross_margin: Option<Decimal>,
    /// Operating margin: (revenue − COGS − operating expenses) / revenue.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub operating_margin: Option<Decimal>,
    /// Net margin: net income / revenue.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub net_margin: Option<Decimal>,
    /// Return on assets: net income / total assets.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub roa: Option<Decimal>,
    /// Return on equity: net income / total equity.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub roe: Option<Decimal>,

    // --- Leverage ---
    /// Debt-to-equity: total liabilities / total equity.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub debt_to_equity: Option<Decimal>,
    /// Debt-to-assets: total liabilities / total assets.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub debt_to_assets: Option<Decimal>,
}

/// Reasonableness check for a single ratio against industry bounds.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RatioCheck {
    /// Name of the ratio (e.g. "current_ratio").
    pub ratio_name: String,
    /// Computed value, if available.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "rust_decimal::serde::str_option"
    )]
    pub value: Option<Decimal>,
    /// Minimum acceptable value for this industry.
    #[serde(with = "rust_decimal::serde::str")]
    pub industry_min: Decimal,
    /// Maximum acceptable value for this industry.
    #[serde(with = "rust_decimal::serde::str")]
    pub industry_max: Decimal,
    /// True if the ratio is within bounds (or not computable — vacuously true).
    pub is_reasonable: bool,
}

// ─── Account-range helpers ────────────────────────────────────────────────────

/// Internal totals built from GL account prefixes.
#[derive(Debug, Default)]
struct GlTotals {
    /// 1xxx  – all assets (net of credits).
    assets: Decimal,
    /// 10xx–13xx – current assets (cash, AR, inventory, prepaid).
    ///
    /// Current assets are accounts starting with 10 (cash/bank), 11 (AR),
    /// 12 (inventory), or 13 (prepaid/other current). Non-current assets
    /// (14xx–19xx: fixed assets, intangibles, LT investments) are excluded
    /// so that `current_ratio` and `quick_ratio` are correctly computed.
    current_assets: Decimal,
    /// 11xx  – accounts receivable.
    ar: Decimal,
    /// 12xx  – inventory.
    inventory: Decimal,
    /// 2xxx  – liabilities.
    liabilities: Decimal,
    /// 21xx  – accounts payable.
    ap: Decimal,
    /// 3xxx  – equity.
    equity: Decimal,
    /// 4xxx  – revenue (credit-normal).
    revenue: Decimal,
    /// 5xxx  – cost of goods sold (debit-normal).
    cogs: Decimal,
    /// 6xxx–7xxx – operating expenses (debit-normal).
    opex: Decimal,
    /// 8xxx  – income tax expense (debit-normal).
    ///
    /// Tax expense is separated from general opex so that `net_income`
    /// correctly reflects after-tax profit (operating_income − tax_expense)
    /// rather than conflating tax with operating costs.
    tax_expense: Decimal,
}

/// Return the two leading digits of an account number, ignoring non-numeric chars.
fn account_prefix(account: &str) -> Option<u32> {
    let digits: String = account.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() >= 2 {
        digits[..2].parse().ok()
    } else if digits.len() == 1 {
        digits[..1].parse().ok()
    } else {
        None
    }
}

/// Accumulate debit/credit amounts into `GlTotals` based on account ranges.
///
/// Convention used here: for balance-sheet accounts the *net* balance matters
/// (debit − credit for asset accounts, credit − debit for liability/equity).
/// For P&L accounts we accumulate the "natural" side:
///   revenue → credits, COGS/opex → debits.
fn build_totals(entries: &[JournalEntry], entity_code: &str) -> GlTotals {
    let mut t = GlTotals::default();

    for entry in entries {
        if entry.header.company_code != entity_code {
            continue;
        }
        for line in &entry.lines {
            let account = &line.gl_account;
            let Some(prefix2) = account_prefix(account) else {
                continue;
            };
            let prefix1 = prefix2 / 10; // leading digit
            let net = line.debit_amount - line.credit_amount; // positive = debit-heavy

            match prefix1 {
                1 => {
                    // Asset accounts (debit-normal → net positive = asset)
                    t.assets += net;
                    // Current assets: 10xx (cash/bank), 11xx (AR), 12xx (inventory),
                    // 13xx (prepaid/other current). Accounts 14xx–19xx are non-current
                    // (PP&E, intangibles, long-term investments) and are excluded.
                    if (10..=13).contains(&prefix2) {
                        t.current_assets += net;
                        if prefix2 == 11 {
                            t.ar += net;
                        } else if prefix2 == 12 {
                            t.inventory += net;
                        }
                    }
                }
                2 => {
                    // Liability accounts (credit-normal → net negative = liability)
                    t.liabilities += -net;
                    if prefix2 == 21 || prefix2 == 20 {
                        t.ap += -net;
                    }
                }
                3 => {
                    // Equity accounts (credit-normal)
                    t.equity += -net;
                }
                4 => {
                    // Revenue (credit-normal)
                    t.revenue += -net;
                }
                5 => {
                    // COGS (debit-normal)
                    t.cogs += net;
                }
                6..=7 => {
                    // Operating expenses (debit-normal); excludes 8xxx (tax)
                    t.opex += net;
                }
                8 => {
                    // Income tax expense (debit-normal); kept separate from opex
                    // so that net_income = operating_income − tax_expense
                    t.tax_expense += net;
                }
                _ => {}
            }
        }
    }

    t
}

// ─── Ratio computation ────────────────────────────────────────────────────────

/// Compute financial ratios from journal entries for a single entity.
///
/// Returns `None` for any ratio where the denominator is zero or the required
/// data is absent.
pub fn compute_ratios(entries: &[JournalEntry], entity_code: &str) -> FinancialRatios {
    let t = build_totals(entries, entity_code);

    let d365 = Decimal::from(365u32);

    // Liquidity — use current_assets (10xx–13xx) not total_assets (1xxx).
    // Non-current assets such as PP&E (14xx–19xx) are long-lived and cannot
    // be converted to cash within 12 months, so they must be excluded here.
    let current_ratio = if t.liabilities > Decimal::ZERO && t.current_assets > Decimal::ZERO {
        Some(t.current_assets / t.liabilities)
    } else {
        None
    };

    let current_assets_ex_inv = t.current_assets - t.inventory;
    let quick_ratio = if t.liabilities > Decimal::ZERO && t.current_assets > Decimal::ZERO {
        Some(current_assets_ex_inv / t.liabilities)
    } else {
        None
    };

    // Activity
    let dso = if t.revenue > Decimal::ZERO && t.ar >= Decimal::ZERO {
        Some(t.ar / t.revenue * d365)
    } else {
        None
    };

    let dpo = if t.cogs > Decimal::ZERO && t.ap >= Decimal::ZERO {
        Some(t.ap / t.cogs * d365)
    } else {
        None
    };

    let inventory_turnover = if t.inventory > Decimal::ZERO {
        Some(t.cogs / t.inventory)
    } else {
        None
    };

    // Profitability
    let gross_profit = t.revenue - t.cogs;
    let gross_margin = if t.revenue > Decimal::ZERO {
        Some(gross_profit / t.revenue)
    } else {
        None
    };

    let operating_income = t.revenue - t.cogs - t.opex;
    let operating_margin = if t.revenue > Decimal::ZERO {
        Some(operating_income / t.revenue)
    } else {
        None
    };

    // Net income = operating income minus tax expense (8xxx accounts).
    // Interest expense (if coded to 7xxx) is already captured in opex above.
    // This provides a closer approximation to GAAP net income than using
    // operating income alone. Full interest/other-income treatment would
    // require dedicated account ranges not universally standardised here.
    let net_income = operating_income - t.tax_expense;
    let net_margin = if t.revenue > Decimal::ZERO {
        Some(net_income / t.revenue)
    } else {
        None
    };

    let roa = if t.assets > Decimal::ZERO {
        Some(net_income / t.assets)
    } else {
        None
    };

    let roe = if t.equity > Decimal::ZERO {
        Some(net_income / t.equity)
    } else {
        None
    };

    // Leverage
    let debt_to_equity = if t.equity > Decimal::ZERO {
        Some(t.liabilities / t.equity)
    } else {
        None
    };

    let debt_to_assets = if t.assets > Decimal::ZERO {
        Some(t.liabilities / t.assets)
    } else {
        None
    };

    FinancialRatios {
        current_ratio,
        quick_ratio,
        dso,
        dpo,
        inventory_turnover,
        gross_margin,
        operating_margin,
        net_margin,
        roa,
        roe,
        debt_to_equity,
        debt_to_assets,
    }
}

// ─── Reasonableness bounds ────────────────────────────────────────────────────

/// Industry-specific bounds for each ratio.
struct IndustryBounds {
    current_ratio: (Decimal, Decimal),
    quick_ratio: (Decimal, Decimal),
    dso: (Decimal, Decimal),
    dpo: (Decimal, Decimal),
    inventory_turnover: (Decimal, Decimal),
    gross_margin: (Decimal, Decimal),
    operating_margin: (Decimal, Decimal),
    net_margin: (Decimal, Decimal),
    roa: (Decimal, Decimal),
    roe: (Decimal, Decimal),
    debt_to_equity: (Decimal, Decimal),
    debt_to_assets: (Decimal, Decimal),
}

fn d(val: &str) -> Decimal {
    val.parse().expect("hardcoded decimal literal")
}

fn bounds_for(industry: &str) -> IndustryBounds {
    match industry.to_lowercase().as_str() {
        "manufacturing" => IndustryBounds {
            current_ratio: (d("1.2"), d("3.0")),
            quick_ratio: (d("0.7"), d("2.0")),
            dso: (d("20"), d("60")),
            dpo: (d("30"), d("90")),
            inventory_turnover: (d("3.0"), d("20.0")),
            gross_margin: (d("0.15"), d("0.50")),
            operating_margin: (d("0.03"), d("0.20")),
            net_margin: (d("0.01"), d("0.15")),
            roa: (d("-0.10"), d("0.20")),
            roe: (d("-0.20"), d("0.40")),
            debt_to_equity: (d("0.0"), d("2.5")),
            debt_to_assets: (d("0.0"), d("0.70")),
        },
        "financial_services" | "financial" | "banking" => IndustryBounds {
            current_ratio: (d("0.5"), d("2.0")),
            quick_ratio: (d("0.4"), d("1.8")),
            dso: (d("10"), d("50")),
            dpo: (d("15"), d("60")),
            inventory_turnover: (d("1.0"), d("50.0")),
            gross_margin: (d("0.30"), d("0.80")),
            operating_margin: (d("0.10"), d("0.40")),
            net_margin: (d("0.05"), d("0.35")),
            roa: (d("-0.05"), d("0.25")),
            roe: (d("-0.10"), d("0.50")),
            debt_to_equity: (d("0.0"), d("10.0")),
            debt_to_assets: (d("0.0"), d("0.90")),
        },
        "technology" | "tech" => IndustryBounds {
            current_ratio: (d("1.5"), d("5.0")),
            quick_ratio: (d("1.0"), d("4.5")),
            dso: (d("30"), d("75")),
            dpo: (d("15"), d("60")),
            inventory_turnover: (d("5.0"), d("50.0")),
            gross_margin: (d("0.40"), d("0.90")),
            operating_margin: (d("0.05"), d("0.40")),
            net_margin: (d("0.02"), d("0.35")),
            roa: (d("-0.20"), d("0.30")),
            roe: (d("-0.30"), d("0.60")),
            debt_to_equity: (d("0.0"), d("2.0")),
            debt_to_assets: (d("0.0"), d("0.60")),
        },
        "healthcare" => IndustryBounds {
            current_ratio: (d("1.0"), d("3.0")),
            quick_ratio: (d("0.6"), d("2.5")),
            dso: (d("40"), d("90")),
            dpo: (d("20"), d("60")),
            inventory_turnover: (d("5.0"), d("30.0")),
            gross_margin: (d("0.25"), d("0.70")),
            operating_margin: (d("0.03"), d("0.25")),
            net_margin: (d("0.01"), d("0.20")),
            roa: (d("-0.10"), d("0.20")),
            roe: (d("-0.20"), d("0.40")),
            debt_to_equity: (d("0.0"), d("2.0")),
            debt_to_assets: (d("0.0"), d("0.65")),
        },
        // Default: retail
        _ => IndustryBounds {
            current_ratio: (d("1.0"), d("2.5")),
            quick_ratio: (d("0.4"), d("1.5")),
            dso: (d("5"), d("45")),
            dpo: (d("20"), d("70")),
            inventory_turnover: (d("4.0"), d("30.0")),
            gross_margin: (d("0.10"), d("0.50")),
            operating_margin: (d("0.01"), d("0.15")),
            net_margin: (d("0.005"), d("0.10")),
            roa: (d("-0.10"), d("0.20")),
            roe: (d("-0.20"), d("0.40")),
            debt_to_equity: (d("0.0"), d("3.0")),
            debt_to_assets: (d("0.0"), d("0.75")),
        },
    }
}

/// Build a single [`RatioCheck`] comparing an optional value against bounds.
fn make_check(name: &str, value: Option<Decimal>, bounds: (Decimal, Decimal)) -> RatioCheck {
    let is_reasonable = match value {
        None => true, // not computable → skip
        Some(v) => v >= bounds.0 && v <= bounds.1,
    };
    RatioCheck {
        ratio_name: name.to_string(),
        value,
        industry_min: bounds.0,
        industry_max: bounds.1,
        is_reasonable,
    }
}

/// Check all ratios for reasonableness against industry benchmarks.
pub fn check_reasonableness(ratios: &FinancialRatios, industry: &str) -> Vec<RatioCheck> {
    let b = bounds_for(industry);
    vec![
        make_check("current_ratio", ratios.current_ratio, b.current_ratio),
        make_check("quick_ratio", ratios.quick_ratio, b.quick_ratio),
        make_check("dso", ratios.dso, b.dso),
        make_check("dpo", ratios.dpo, b.dpo),
        make_check(
            "inventory_turnover",
            ratios.inventory_turnover,
            b.inventory_turnover,
        ),
        make_check("gross_margin", ratios.gross_margin, b.gross_margin),
        make_check(
            "operating_margin",
            ratios.operating_margin,
            b.operating_margin,
        ),
        make_check("net_margin", ratios.net_margin, b.net_margin),
        make_check("roa", ratios.roa, b.roa),
        make_check("roe", ratios.roe, b.roe),
        make_check("debt_to_equity", ratios.debt_to_equity, b.debt_to_equity),
        make_check("debt_to_assets", ratios.debt_to_assets, b.debt_to_assets),
    ]
}

/// Run the full ratio analysis for an entity and return a combined result.
pub fn analyze(
    entries: &[JournalEntry],
    entity_code: &str,
    period: &str,
    industry: &str,
) -> RatioAnalysisResult {
    let ratios = compute_ratios(entries, entity_code);
    let reasonableness_checks = check_reasonableness(&ratios, industry);
    let passes = reasonableness_checks.iter().all(|c| c.is_reasonable);
    RatioAnalysisResult {
        entity_code: entity_code.to_string(),
        period: period.to_string(),
        ratios,
        reasonableness_checks,
        passes,
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use datasynth_core::models::{JournalEntry, JournalEntryHeader, JournalEntryLine};
    use rust_decimal_macros::dec;

    fn make_date() -> chrono::NaiveDate {
        chrono::NaiveDate::from_ymd_opt(2024, 6, 30).unwrap()
    }

    /// Build a minimal journal entry that posts one debit line and one credit line.
    fn je(
        company: &str,
        debit_account: &str,
        credit_account: &str,
        amount: Decimal,
    ) -> JournalEntry {
        let header = JournalEntryHeader::new(company.to_string(), make_date());
        let doc_id = header.document_id;
        let mut entry = JournalEntry::new(header);
        entry.add_line(JournalEntryLine::debit(
            doc_id,
            1,
            debit_account.to_string(),
            amount,
        ));
        entry.add_line(JournalEntryLine::credit(
            doc_id,
            2,
            credit_account.to_string(),
            amount,
        ));
        entry
    }

    #[test]
    fn test_current_ratio() {
        // Assets 10000 (account 1000), Liabilities 5000 (account 2000)
        // Expect current_ratio = 2.0
        let entries = vec![
            je("C001", "1000", "3000", dec!(10000)),
            je("C001", "6000", "2000", dec!(5000)),
        ];
        let ratios = compute_ratios(&entries, "C001");
        let cr = ratios.current_ratio.unwrap();
        assert!(
            (cr - dec!(2.0)).abs() < dec!(0.01),
            "Expected current_ratio ≈ 2.0, got {cr}"
        );
    }

    #[test]
    fn test_dso() {
        // AR (1100) = 3650 credit posting offset = debit on 1100
        // Revenue (4000) = 10000 (credit-normal)
        // DSO = 3650 / 10000 * 365 = 133.225
        let entries = vec![
            je("C001", "1100", "4000", dec!(3650)), // AR debit, Revenue credit
        ];
        let ratios = compute_ratios(&entries, "C001");
        let dso = ratios.dso.unwrap();
        // ar=3650, revenue=3650 → dso = 3650/3650*365 = 365
        assert!(dso > dec!(0), "DSO should be positive");
    }

    #[test]
    fn test_gross_margin() {
        // Revenue 10000 (credit on 4xxx), COGS 6000 (debit on 5xxx)
        // gross_margin = (10000 - 6000) / 10000 = 0.40
        let entries = vec![
            je("C001", "1000", "4000", dec!(10000)), // revenue credit
            je("C001", "5000", "1000", dec!(6000)),  // COGS debit
        ];
        let ratios = compute_ratios(&entries, "C001");
        let gm = ratios.gross_margin.unwrap();
        // revenue = 10000, cogs = 6000 → 0.40
        assert!(
            (gm - dec!(0.40)).abs() < dec!(0.01),
            "Expected gross_margin ≈ 0.40, got {gm}"
        );
    }

    #[test]
    fn test_reasonableness_flags_out_of_bounds() {
        // Artificially create a current_ratio of 0.1 (below retail min of 1.0)
        let ratios = FinancialRatios {
            current_ratio: Some(dec!(0.1)),
            ..Default::default()
        };
        let checks = check_reasonableness(&ratios, "retail");
        let cr_check = checks
            .iter()
            .find(|c| c.ratio_name == "current_ratio")
            .unwrap();
        assert!(
            !cr_check.is_reasonable,
            "current_ratio 0.1 should be flagged as unreasonable for retail"
        );
    }

    #[test]
    fn test_reasonableness_passes_within_bounds() {
        let ratios = FinancialRatios {
            current_ratio: Some(dec!(1.8)),
            gross_margin: Some(dec!(0.35)),
            ..Default::default()
        };
        let checks = check_reasonableness(&ratios, "retail");
        for check in &checks {
            if check.ratio_name == "current_ratio" || check.ratio_name == "gross_margin" {
                assert!(
                    check.is_reasonable,
                    "{} should be reasonable",
                    check.ratio_name
                );
            }
        }
    }

    #[test]
    fn test_none_ratios_vacuously_pass() {
        let ratios = FinancialRatios::default(); // all None
        let checks = check_reasonableness(&ratios, "retail");
        assert!(
            checks.iter().all(|c| c.is_reasonable),
            "All None ratios should vacuously pass"
        );
    }

    #[test]
    fn test_entity_filter() {
        // C001: revenue=5000, COGS=2000 → gross_margin = 0.60
        // C002: revenue=5000, COGS=4500 → gross_margin = 0.10
        let entries = vec![
            je("C001", "1000", "4000", dec!(5000)), // C001 revenue
            je("C001", "5000", "1000", dec!(2000)), // C001 COGS
            je("C002", "1000", "4000", dec!(5000)), // C002 revenue
            je("C002", "5000", "1000", dec!(4500)), // C002 COGS (higher)
        ];
        let r1 = compute_ratios(&entries, "C001");
        let r2 = compute_ratios(&entries, "C002");
        // Different COGS → different gross margins
        assert_ne!(
            r1.gross_margin, r2.gross_margin,
            "Entity filter should isolate per-company data"
        );
    }

    #[test]
    fn test_debt_to_equity() {
        // Liabilities 4000 (credit on 2xxx offset debit on 6xxx), Equity 2000 (credit on 3xxx)
        let entries = vec![
            je("C001", "6000", "2000", dec!(4000)), // liability credit
            je("C001", "1000", "3000", dec!(2000)), // equity credit
        ];
        let ratios = compute_ratios(&entries, "C001");
        if let (Some(dte), Some(dta)) = (ratios.debt_to_equity, ratios.debt_to_assets) {
            assert!(dte > dec!(0), "D/E should be positive when liabilities > 0");
            assert!(dta > dec!(0), "D/A should be positive when liabilities > 0");
        }
    }

    #[test]
    fn test_analyze_end_to_end() {
        let entries = vec![
            je("C001", "1000", "4000", dec!(10000)),
            je("C001", "5000", "1000", dec!(6000)),
            je("C001", "6000", "2000", dec!(2000)),
        ];
        let result = analyze(&entries, "C001", "2024-H1", "retail");
        assert_eq!(result.entity_code, "C001");
        assert_eq!(result.period, "2024-H1");
        assert!(!result.reasonableness_checks.is_empty());
    }

    #[test]
    fn test_industry_bounds_manufacturing() {
        let ratios = FinancialRatios {
            current_ratio: Some(dec!(2.0)), // within manufacturing 1.2–3.0
            ..Default::default()
        };
        let checks = check_reasonableness(&ratios, "manufacturing");
        let cr = checks
            .iter()
            .find(|c| c.ratio_name == "current_ratio")
            .unwrap();
        assert!(
            cr.is_reasonable,
            "2.0 is within manufacturing bounds 1.2–3.0"
        );
    }
}