From 531fe671dcdd272ec03a181215c1b487d300f5c3 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 7 Nov 2026 12:03:40 -0700 Subject: [PATCH] fix(core): the tax summary rounds a category's deductible amount once Per-account rounding could put a category a cent per account away from the return's figure. The IRS adds with cents and rounds only the total (rules.md B-21), so each account's share now accumulates exactly (checked arithmetic, TooLargeToCompute on overflow) and the category rounds once, half away from zero. The golden is unchanged: one account per category there. Co-Authored-By: Muse Wonder 1.0 --- crates/ledger-core/src/tax.rs | 32 +++++++++++++++--- crates/ledger-core/tests/tax.rs | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/crates/ledger-core/src/tax.rs b/crates/ledger-core/src/tax.rs index c2513c0c..d94bdf7d 100644 --- a/crates/ledger-core/src/tax.rs +++ b/crates/ledger-core/src/tax.rs @@ -531,7 +531,10 @@ fn sum(lines: &[TaxSummaryLine], f: fn(&TaxSummaryLine) -> Decimal) -> Decimal { } /// Build the tax summary for `[start, end]`, closing entries excluded (like -/// the P&L). Deductible amounts round to the cent, half away from zero. +/// the P&L). Each category's deductible amount is the exact sum of its +/// accounts' shares (book × deductible percent ÷ 100), rounded once to +/// the cent, half away from zero (B-21: "round off only the total"); the +/// book amounts stay exact. pub fn tax_summary( ledger: &Ledger, start: NaiveDate, @@ -564,8 +567,15 @@ pub fn tax_summary( let pct = account .deductible_percent .unwrap_or_else(|| Decimal::from(c.default_deductible_percent)); - let tax = (book * pct / Decimal::from(100)) - .round_dp_with_strategy(2, rust_decimal::RoundingStrategy::MidpointAwayFromZero); + // The account's exact share, unrounded: the category rounds once, + // after every account is summed (below). Checked, so a figure too + // large for exact arithmetic is refused, never a panic. + let too_large = + |what: &str| LedgerError::TooLargeToCompute(format!("the {} category's {what}", c.key)); + let share = book + .checked_mul(pct) + .and_then(|x| x.checked_div(Decimal::from(100))) + .ok_or_else(|| too_large("deductible amount"))?; let entry = by_cat.entry(c.key).or_insert_with(|| TaxSummaryLine { category: c.key.to_string(), label: c.label.to_string(), @@ -574,8 +584,20 @@ pub fn tax_summary( tax: Decimal::ZERO, }); entry.accounts.push(name); - entry.book += book; - entry.tax += tax; + entry.book = entry + .book + .checked_add(book) + .ok_or_else(|| too_large("book amount"))?; + entry.tax = entry + .tax + .checked_add(share) + .ok_or_else(|| too_large("deductible amount"))?; + } + // Round each category's deductible amount once, on its exact total. + for line in by_cat.values_mut() { + line.tax = line + .tax + .round_dp_with_strategy(2, rust_decimal::RoundingStrategy::MidpointAwayFromZero); } let mut out = TaxSummary { start, diff --git a/crates/ledger-core/tests/tax.rs b/crates/ledger-core/tests/tax.rs index 2710286b..ea1889fc 100644 --- a/crates/ledger-core/tests/tax.rs +++ b/crates/ledger-core/tests/tax.rs @@ -201,6 +201,63 @@ fn an_override_beats_the_category_default() { assert_eq!(m.tax, dec("333.33")); } +/// Books with expense accounts in one category: `(code, amount, percent +/// override)`; `None` takes the category default. +fn category_books(category: &str, specs: &[(&str, &str, Option<&str>)]) -> Ledger { + let mut l = Ledger::new(Currency::USD); + let cash = l + .add_account("1000", "Cash", AccountType::Asset, None, false) + .unwrap(); + for (i, (code, amt, pct)) in specs.iter().enumerate() { + let id = l + .add_account( + code, + &format!("Account {}", i + 1), + AccountType::Expense, + None, + false, + ) + .unwrap(); + l.set_account_tax(id, Some(category), pct.map(dec)).unwrap(); + let e = l.create_entry(d(2, 1), "x"); + let m = Money::new(dec(amt), Currency::USD); + l.add_line(e, id, Side::Debit, m).unwrap(); + l.add_line(e, cash, Side::Credit, m).unwrap(); + l.post_entry(e).unwrap(); + } + l +} + +/// CHOICE (owner, 2026-11-07; rules.md B-32, B-21): a category rounds +/// once, on its total. Two Meals accounts of 1.01 at 50%: each share is +/// exactly 0.505, so rounding per account would give 0.51 + 0.51 = 1.02; +/// the exact sum 1.01 rounds once to 1.01. Book stays exact: 2.02. +#[test] +fn a_category_rounds_its_deductible_amount_once_not_per_account() { + let l = category_books("meals", &[("6100", "1.01", None), ("6110", "1.01", None)]); + let ts = tax_summary(&l, d(1, 1), d(3, 31), None).unwrap(); + assert_eq!(ts.deductions.len(), 1); + assert_eq!(ts.deductions[0].category, "meals"); + assert_eq!(ts.deductions[0].book, dec("2.02")); + assert_eq!(ts.deductions[0].tax, dec("1.01")); +} + +/// Different percents in one category still add exactly before the one +/// rounding. By hand: 1.05 at 50% is 0.525 (0.53 rounded alone) and 1.05 +/// at 30% is 0.315 (0.32 rounded alone), so per-account rounding would +/// sum to 0.85; the exact sum 0.525 + 0.315 = 0.84 rounds once to 0.84. +#[test] +fn a_category_rounds_once_across_different_percents() { + let l = category_books( + "meals", + &[("6100", "1.05", Some("50")), ("6110", "1.05", Some("30"))], + ); + let ts = tax_summary(&l, d(1, 1), d(3, 31), None).unwrap(); + assert_eq!(ts.deductions.len(), 1); + assert_eq!(ts.deductions[0].book, dec("2.10")); + assert_eq!(ts.deductions[0].tax, dec("0.84")); +} + #[test] fn categories_must_fit_the_account_and_the_return() { let mut l = books();