fix(core): payroll_remit_plan refuses past exact arithmetic instead of panicking
The remittance plan totalled the payables with a raw `total +=`, which panics on Decimal overflow, on a pub fn returning a bare plan. Its callers refused first through sum_exact_of, but that was an unenforced precondition (BS-entry-4: every derived figure is checked arithmetic). It now returns Result<_, LedgerError>, totalling through kernel::total_of and refusing TooLargeToCompute; Ledger and the store propagate it and keep their pre-checks. A test shows payables past the range, up and down, are refused, not a panic. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
90d34585fd
commit
46dccd5d76
4 files changed
+36
-8
No files matched your search
@@ -8533,7 +8533,7 @@ impl Ledger {
|
||||
}
|
||||
let entry = self.try_create_entry(date, &format!("Payroll tax remittance {date}"))?;
|
||||
for (account, side, amount) in
|
||||
payroll_remit_plan(&payables, cash_account, self.base_currency)
|
||||
payroll_remit_plan(&payables, cash_account, self.base_currency)?
|
||||
{
|
||||
self.add_line(entry, account, side, amount)?;
|
||||
}
|
||||
|
||||
@@ -213,24 +213,28 @@ pub fn payroll_entry_plan(
|
||||
/// The remittance entry's lines: Dr each payable with a non-zero balance,
|
||||
/// Cr cash for the total. Balances are normal-side (positive = owed), so
|
||||
/// the plan balances by construction.
|
||||
///
|
||||
/// Refused (TooLargeToCompute) when the payables' total is past exact
|
||||
/// arithmetic, never a panic: the total is the kernel's checked fold, so
|
||||
/// this function does not depend on its callers having refused first.
|
||||
pub fn payroll_remit_plan(
|
||||
payables: &[(AccountId, Decimal)],
|
||||
cash_account: AccountId,
|
||||
base: Currency,
|
||||
) -> Vec<(AccountId, Side, Money)> {
|
||||
) -> Result<Vec<(AccountId, Side, Money)>, LedgerError> {
|
||||
let total = crate::kernel::total_of(payables.iter(), |(_, b)| *b)
|
||||
.ok_or_else(|| LedgerError::TooLargeToCompute("the payroll taxes payable".into()))?;
|
||||
let mut plan = Vec::new();
|
||||
let mut total = Decimal::ZERO;
|
||||
for (account, balance) in payables {
|
||||
if balance.is_zero() {
|
||||
continue;
|
||||
}
|
||||
plan.push((*account, Side::Debit, Money::new(*balance, base)));
|
||||
total += *balance;
|
||||
}
|
||||
if !total.is_zero() {
|
||||
plan.push((cash_account, Side::Credit, Money::new(total, base)));
|
||||
}
|
||||
plan
|
||||
Ok(plan)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in new issue
Block a user