merge: the tax summary rounds a category's deductible amount once (D-67; branch t3/tax-summary-round-once, 5b1f9d02; reviewed)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Opus 5.5 committed 2026-11-07 12:10:39 -07:00
commit 107571fd5e
4 files changed
+90 -5

No files matched your search

+27 -5
View File
@@ -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,
+57
View File
@@ -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();
+5
View File
@@ -2243,6 +2243,11 @@ included, and an account's header flag never changes).
chart tools", not "never any".
- Turning the policy to `refuse` changes no existing account: it applies
to new commands (B-28). Needs attention keeps listing the gaps.
- CHOICE (owner, 2026-11-07): a category's deductible amount in the tax
summary is the exact sum of each of its accounts' shares (book ×
deductible percent ÷ 100), rounded once to the cent, half away from
zero, following B-21's "round off only the total"; the books stay
exact (R-2).
- Every account a tool can require has a category that fits it on every
return: the gain or loss account of a disposal (`form_4797_gain_loss`,
Form 4797), tax-exempt income (`tax_exempt_income`, Schedule K), and
+1
View File
@@ -2188,6 +2188,7 @@ The owner decisions of 2026-10-16 (at the top of this document) replace the form
| D-64 | Q-4 answered (owner, 2026-10-29, the question tool: "Defer: ship unsigned builds to yourself first") | **P11a step 5 (the signed release list, the key ceremony, Apple and Microsoft code signing) is deferred until the first public release is near.** CI builds unsigned artifacts for the owner's own machines; `wanderbooks update` works from a local feed meanwhile (no compiled-in key). The two-key design of D-25 is unchanged, only its date. | P11a step 5, `release.yml` |
| D-65 | P13 step 6 (owner, 2026-10-29, the question tool: "Split it out now"; then, when this session's token could not create a GitHub repository: "Gitea only, skip GitHub") | **The bookspec specification lives in a public repository of its own, `https://git.swaits.com/swaits/bookspec`** (history preserved; CC BY 4.0 for the text and examples, MIT OR Apache-2.0 for the code), and wanderbooks depends on it pinned by commit, so `just check` still runs every example. No GitHub copy. The ten stated rules without examples and the reserved numbers are published as such. | P13 step 6; `crates/ledger-core/Cargo.toml` |
| D-66 | P12 step 2's shape (owner, 2026-11-06, the question tool: "Shorter fuzzing (1 hour each)") | **The run is one resumable nightly command, `just p12-night`, on the owner's machine: it resumes where the last night stopped and stops at 07:00, so the laptop is free by day. Every fuzz target runs 1 hour, all thirteen side by side (the owner's number, which 8.2 P12 step 2 allows in place of 4 hours).** Then `just prove-all`, `just oracle`, the property maxima, then mutation crate by crate over later nights. What a night finds is fixed by day. | P12 step 2 |
| D-67 | Tax summary rounding (owner, 2026-11-07, the question tool: "Round once per category") | **A category's deductible amount is the exact sum of its accounts' shares, rounded once to the cent; was per account then summed, up to a cent per account apart. Follows B-21's 'round off only the total'.** | `ledger_core::tax_summary`; rules.md B-32, B-21 |
### 10.2 Deferred