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).
|
//! U7: payroll arithmetic, withholding and employer taxes (kernel sweep).
|
||||||
//!
|
//!
|
||||||
//! Empty in U0: the unit adds its arithmetic here, in this file alone, so
|
//! Net pay is gross less every employee withholding and deduction, exact
|
||||||
//! no two sweep units ever edit the same file. The module root re-exports
|
//! to the last unit. `payroll::payroll_entry_plan` keeps its account
|
||||||
//! it, keeping every path `crate::kernel::foo`.
|
//! 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)
|
||||||
|
}
|
||||||
@@ -52,15 +52,16 @@ impl PayrollLine {
|
|||||||
/// (TooLargeToCompute), never a panic, past exact arithmetic: a run is
|
/// (TooLargeToCompute), never a panic, past exact arithmetic: a run is
|
||||||
/// restored as journaled (BS-entry-4).
|
/// restored as journaled (BS-entry-4).
|
||||||
pub fn net_pay(&self) -> Result<Decimal, LedgerError> {
|
pub fn net_pay(&self) -> Result<Decimal, LedgerError> {
|
||||||
[
|
crate::kernel::net_pay(
|
||||||
self.federal_withholding,
|
self.gross_pay,
|
||||||
self.state_withholding,
|
&[
|
||||||
self.ss_employee,
|
self.federal_withholding,
|
||||||
self.medicare_employee,
|
self.state_withholding,
|
||||||
self.other_deductions,
|
self.ss_employee,
|
||||||
]
|
self.medicare_employee,
|
||||||
.into_iter()
|
self.other_deductions,
|
||||||
.try_fold(self.gross_pay, Decimal::checked_sub)
|
],
|
||||||
|
)
|
||||||
.ok_or_else(|| LedgerError::TooLargeToCompute(format!("{}'s net pay", self.employee_name)))
|
.ok_or_else(|| LedgerError::TooLargeToCompute(format!("{}'s net pay", self.employee_name)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -172,20 +173,26 @@ pub fn payroll_entry_plan(
|
|||||||
base: Currency,
|
base: Currency,
|
||||||
) -> Result<Vec<(AccountId, Side, Money)>, LedgerError> {
|
) -> Result<Vec<(AccountId, Side, Money)>, LedgerError> {
|
||||||
let too_large = || LedgerError::TooLargeToCompute("the payroll run's totals".into());
|
let too_large = || LedgerError::TooLargeToCompute("the payroll run's totals".into());
|
||||||
let sum = |f: &dyn Fn(&PayrollLine) -> Option<Decimal>| {
|
// Every total is a kernel total (net pay itself is kernel::net_pay):
|
||||||
lines
|
// exact, or refused here as TooLargeToCompute, never a panic.
|
||||||
.iter()
|
let gross = crate::kernel::total_of(lines.iter(), |l| l.gross_pay).ok_or_else(too_large)?;
|
||||||
.try_fold(Decimal::ZERO, |t, l| t.checked_add(f(l)?))
|
let employer = crate::kernel::total_try(lines.iter(), |l| {
|
||||||
.ok_or_else(too_large)
|
l.ss_employer.checked_add(l.medicare_employer)
|
||||||
};
|
})
|
||||||
let gross = sum(&|l| Some(l.gross_pay))?;
|
.ok_or_else(too_large)?;
|
||||||
let employer = sum(&|l| l.ss_employer.checked_add(l.medicare_employer))?;
|
let federal =
|
||||||
let federal = sum(&|l| Some(l.federal_withholding))?;
|
crate::kernel::total_of(lines.iter(), |l| l.federal_withholding).ok_or_else(too_large)?;
|
||||||
let state = sum(&|l| Some(l.state_withholding))?;
|
let state =
|
||||||
let ss = sum(&|l| l.ss_employee.checked_add(l.ss_employer))?;
|
crate::kernel::total_of(lines.iter(), |l| l.state_withholding).ok_or_else(too_large)?;
|
||||||
let medicare = sum(&|l| l.medicare_employee.checked_add(l.medicare_employer))?;
|
let ss = crate::kernel::total_try(lines.iter(), |l| l.ss_employee.checked_add(l.ss_employer))
|
||||||
let other = sum(&|l| Some(l.other_deductions))?;
|
.ok_or_else(too_large)?;
|
||||||
let net = sum(&|l| l.net_pay().ok())?;
|
let medicare = crate::kernel::total_try(lines.iter(), |l| {
|
||||||
|
l.medicare_employee.checked_add(l.medicare_employer)
|
||||||
|
})
|
||||||
|
.ok_or_else(too_large)?;
|
||||||
|
let other =
|
||||||
|
crate::kernel::total_of(lines.iter(), |l| l.other_deductions).ok_or_else(too_large)?;
|
||||||
|
let net = crate::kernel::total_try(lines.iter(), |l| l.net_pay().ok()).ok_or_else(too_large)?;
|
||||||
let mut plan = Vec::new();
|
let mut plan = Vec::new();
|
||||||
let mut push = |account: AccountId, side: Side, amount: Decimal| {
|
let mut push = |account: AccountId, side: Side, amount: Decimal| {
|
||||||
if !amount.is_zero() {
|
if !amount.is_zero() {
|
||||||
|
|||||||
Reference in new issue
Block a user