refactor(core): every report and book total goes through the kernel's checked sum
A raw Decimal + / += / .sum() panics at Decimal::MAX, and a replayed journal line is accepted of any size (rules.md B-28), so a total of restored figures must be exact or refused (B-21), never a crash. The reports (P&L, balance sheet, general ledger, members' equity, asset register), K-1 lines, tax-basis wholes, payouts, the 1099 split, the audit's articulation check, the members close split and arap's document_total now sum with kernel::total / total_of / total_try and add or subtract with checked arithmetic, refusing with TooLargeToCompute. money::sum_exact* and add_exact/sub_exact are the engine's thin wrappers over the kernel (no second copy of the sum). form1099::sum3 was a copy of the kernel's sum and is now kernel::total. periods::allocate and allocate_last_absorbs (frozen replay rules) keep their signatures and results to the cent; each gains a try_ twin that returns TooLargeToCompute where the raw amount * share used to panic, and the ledger's close split calls the twins. Behaviour for every ordinary input is unchanged: no figure moves, no test or golden edited. Infallible public signatures (trial_balance, ProfitAndLossStatement::net_income, tax book_net, project_entries, cash-flow kinds, the Regime) keep their raw sums; making them fallible would change ~100 callers and is a decision, not a refactor. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b3d504067f
commit
3376c3d549
10 files changed
+368
-162
No files matched your search
@@ -17,6 +17,7 @@
|
||||
//! no platform: callers (or a format-specific parser) supply the numbers
|
||||
//! from the payout statement, which is the entry's receipt.
|
||||
|
||||
use crate::money::{add_exact, sub_exact, sum_exact_of};
|
||||
use crate::{AccountId, AccountType, ChartOfAccounts, ClassId, LedgerError, Side};
|
||||
use rust_decimal::Decimal;
|
||||
|
||||
@@ -165,23 +166,27 @@ pub fn payout_plan(
|
||||
for a in &payout.adjustments {
|
||||
check(a.account, a.amount, "adjustment", &[])?;
|
||||
}
|
||||
let gross: Decimal = payout.revenue.iter().map(|l| l.amount).sum();
|
||||
let fees: Decimal = payout.fees.iter().map(|l| l.amount).sum();
|
||||
let adjustments: Decimal = payout
|
||||
.adjustments
|
||||
.iter()
|
||||
.map(|a| match a.side {
|
||||
let gross = sum_exact_of(&payout.revenue, |l| l.amount, "the payout's revenue")?;
|
||||
let fees = sum_exact_of(&payout.fees, |l| l.amount, "the payout's fees")?;
|
||||
let adjustments = sum_exact_of(
|
||||
&payout.adjustments,
|
||||
|a| match a.side {
|
||||
Side::Credit => a.amount,
|
||||
Side::Debit => -a.amount,
|
||||
})
|
||||
.sum();
|
||||
let expected = gross - fees + adjustments;
|
||||
},
|
||||
"the payout's adjustments",
|
||||
)?;
|
||||
let expected = add_exact(
|
||||
sub_exact(gross, fees, "the payout's total")?,
|
||||
adjustments,
|
||||
"the payout's total",
|
||||
)?;
|
||||
if expected != payout.deposit {
|
||||
return bad(format!(
|
||||
"payout doesn't reconcile: gross {gross} − fees {fees} + adjustments \
|
||||
{adjustments} = {expected}, but the deposit is {} (off by {})",
|
||||
payout.deposit,
|
||||
payout.deposit - expected
|
||||
sub_exact(payout.deposit, expected, "the payout's difference")?
|
||||
));
|
||||
}
|
||||
let mut plan = Vec::new();
|
||||
|
||||
Reference in new issue
Block a user