refactor(core): payroll's net pay in the kernel
Net pay was a hand fold of Decimal::checked_sub in payroll.rs, and payroll_entry_plan totalled its seven columns with local checked folds. Both now go through the kernel: kernel::net_pay (gross less every deduction, folded left to right, None past exact arithmetic) and kernel::total_of/total_try for the plan's columns, the net over each line's kernel net pay. The fold order preserves the old behaviour exactly, including at the edge: each running difference must be representable, so [MAX, MAX, -MAX] from a zero gross refuses where summing first would cancel. Figures, scale, zero-skipping and the TooLargeToCompute refusals are unchanged; every golden and test passes as before. The rules are BS-entry-3 (exact amounts), BS-entry-4 (every payroll figure computed with checked arithmetic) and BS-entry-1 (the plan balances). payroll_remit_plan's bare total and name_payroll_parties' grouped checked_add stay: both callers refuse through sum_exact_of first, in the same order, and a grouped accumulation has no kernel shape. See docs/proofs/k-u7.md. Co-Authored-By: Muse Wonder 1.0
This commit is contained in:
1 parent
01acb159f0
commit
e8f60532b1
2 files changed
+86
-26
No files matched your search
@@ -1,5 +1,58 @@
|
||||
//! U7: payroll arithmetic, withholding and employer taxes (kernel sweep).
|
||||
//!
|
||||
//! Empty in U0: the unit adds its arithmetic here, in this file alone, so
|
||||
//! no two sweep units ever edit the same file. The module root re-exports
|
||||
//! it, keeping every path `crate::kernel::foo`.
|
||||
//! Net pay is gross less every employee withholding and deduction, exact
|
||||
//! to the last unit. `payroll::payroll_entry_plan` keeps its account
|
||||
//! mapping in `payroll.rs` and builds its totals from [`net_pay`] and
|
||||
//! [`super::total`]/[`super::total_try`]; the plan it builds is checked
|
||||
//! by [`super::check_lines`] before it posts, as every entry is. There
|
||||
//! is no tax law here: every withholding number is supplied by the
|
||||
//! caller, and this file only subtracts what it is given.
|
||||
|
||||
use super::Amount;
|
||||
|
||||
/// Gross pay less every deduction, in the order given (`gross` less
|
||||
/// `d1`, less `d2`, and so on), each subtraction exact. `None` the
|
||||
/// moment a running difference cannot be represented, never a wrapped,
|
||||
/// rounded or panicking one.
|
||||
///
|
||||
/// ```text
|
||||
/// GOAL: one definition of net pay: gross less the employee's
|
||||
/// withholdings and deductions, exactly, or refused.
|
||||
/// SOURCE: BS-entry-3 (amounts are exact; a sum of amounts is their
|
||||
/// exact sum, so a difference of amounts is their exact
|
||||
/// difference), BS-entry-4 (every payroll figure a command
|
||||
/// takes, and every figure an engine derives, is computed with
|
||||
/// checked arithmetic and refused past exact arithmetic),
|
||||
/// BS-entry-1 (the payroll plan this feeds balances: debits
|
||||
/// equal credits); IRS Pub. 15 (docs/spec/sources/irs-payroll.md:
|
||||
/// the withholdings are the caller's numbers, never a table here).
|
||||
/// INVARIANTS:
|
||||
/// 1. Some(n) is the exact mathematical `gross` less the sum of the
|
||||
/// deductions (every running difference was representable, so no
|
||||
/// step lost anything). No rounding, ever.
|
||||
/// 2. net_pay(gross, []) = Some(gross).
|
||||
/// 3. When the deductions sum to at most `gross`, the net is the
|
||||
/// gross less that sum and is not negative; the engine refuses a
|
||||
/// payroll line whose net is negative, so a posted run's net pay
|
||||
/// is never below zero. A net below zero is still a value here:
|
||||
/// the refusal is the caller's rule, not arithmetic's.
|
||||
/// 4. None on overflow; NEVER a rounded or wrapped result, never a
|
||||
/// panic. The fold is left to right, so [MAX, MAX, -MAX] from a
|
||||
/// zero gross is None while the same figures summed first would
|
||||
/// cancel: each step must be exact, as `total`'s fold is.
|
||||
/// SCENARIOS:
|
||||
/// 1. IF gross 5,000.00 and withholdings 750.00, 250.00, 310.00,
|
||||
/// 72.50 and 100.00: SHOULD be 3,517.50.
|
||||
/// 2. IF the withholdings together exceed gross: SHOULD be the exact
|
||||
/// negative difference, for the caller to refuse as negative net
|
||||
/// pay, never a clamped zero.
|
||||
/// 3. IF a running difference is past the amount's range: SHOULD be
|
||||
/// None, never a panic.
|
||||
/// ```
|
||||
pub fn net_pay<T: Amount>(gross: T, deductions: &[T]) -> Option<T> {
|
||||
let mut net = gross;
|
||||
for &deduction in deductions {
|
||||
net = net.checked_sub(deduction)?;
|
||||
}
|
||||
Some(net)
|
||||
}
|
||||
Reference in new issue
Block a user