diff --git a/crates/ledger-core/src/arap.rs b/crates/ledger-core/src/arap.rs index e054aac0..a47ec7c8 100644 --- a/crates/ledger-core/src/arap.rs +++ b/crates/ledger-core/src/arap.rs @@ -1355,10 +1355,14 @@ pub struct AgingRow { impl AgingRow { /// Total outstanding; refused past exact arithmetic. pub fn total(&self) -> Result { - [self.days_30, self.days_60, self.days_90, self.days_90_plus] - .into_iter() - .try_fold(self.current, Decimal::checked_add) - .ok_or_else(|| LedgerError::TooLargeToCompute(format!("{}'s aging total", self.name))) + crate::kernel::aging_total( + self.current, + self.days_30, + self.days_60, + self.days_90, + self.days_90_plus, + ) + .ok_or_else(|| LedgerError::TooLargeToCompute(format!("{}'s aging total", self.name))) } /// Add `other`'s buckets to this row's; refused past exact arithmetic. diff --git a/crates/ledger-core/src/kernel/control.rs b/crates/ledger-core/src/kernel/control.rs index 6e6df45d..c7422047 100644 --- a/crates/ledger-core/src/kernel/control.rs +++ b/crates/ledger-core/src/kernel/control.rs @@ -1,5 +1,116 @@ -//! U8: control and aging, what is due or overdue (kernel sweep). +//! U8: control, aging and reconciliation totals (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`. +//! Three identities the books state by name, each a short chain of exact +//! additions and subtractions: an aging row's five buckets sum to what is +//! owed (BS-arap-1), a control account's balance less its documents and its +//! unattached lines is its difference, which is always zero (BS-arap-2), and +//! a member's book capital less tax capital is their reconciliation total +//! (BS-partner-4). Naming them here puts them under proof rather than +//! leaving each a hand-written sum at its call site. The sums reuse +//! [`super::total`]; the chains of differences are checked one step at a +//! time, so none of them can panic or round. + +use super::{Amount, total}; + +/// What an aging row owes: its five buckets, current, 1-30, 31-60, 61-90 +/// and over 90 days, added exactly. +/// +/// ```text +/// GOAL: an aging row's total is exactly the sum of its five buckets, so +/// the buckets partition what is owed and no bucket is lost or +/// counted twice in the total. +/// SOURCE: BS-arap-1 (an aging puts each open balance in exactly one +/// bucket, so the buckets sum to the open balances); BS-entry-3, +/// BS-entry-4 (exact, no panic). +/// INVARIANTS: +/// 1. Some(t): t = current + b30 + b60 + b90 + b90p, exactly. +/// 2. The order of the buckets does not change a total that exists +/// for in-range figures; aging_total of five zeros is Some(0). +/// 3. None when a running sum cannot be represented; NEVER a rounded or +/// wrapped figure, never a panic. +/// SCENARIOS: +/// 1. IF a customer owes 100.00 current and 40.00 over 90 days past due: +/// - SHOULD total 140.00 +/// BECAUSE every bucket counts once. +/// 2. IF the buckets are Decimal::MAX and 1 in any two places: +/// - SHOULD be None +/// BECAUSE the sum is not representable. +/// ``` +pub fn aging_total(current: T, b30: T, b60: T, b90: T, b90p: T) -> Option { + total([current, b30, b60, b90, b90p]) +} + +/// A control account's difference: its balance less what its documents +/// still owe on it, less its unattached lines. +/// +/// ```text +/// GOAL: the control check's difference is the balance less the documents +/// and the unattached lines, exactly; it is zero whenever the +/// balance is those two sums, which it always is, as both sides sum +/// the same lines. +/// SOURCE: BS-arap-2 (a control account's balance equals the sum of its +/// documents plus its unattached lines); BS-ledger-8; BS-entry-4. +/// INVARIANTS: +/// 1. Some(d): d = balance - documents - unattached, exactly. +/// 2. control_difference(documents + unattached, documents, unattached) +/// = Some(0), and conversely Some(0) means balance = documents + +/// unattached: the difference vanishes exactly when the account +/// agrees with its subledger. +/// 3. None when a step cannot be represented; NEVER a rounded or +/// wrapped figure, never a panic. +/// SCENARIOS: +/// 1. IF the balance is 150.00, the documents 100.00 and one unattached +/// line 50.00: +/// - SHOULD be 0 +/// BECAUSE 150.00 = 100.00 + 50.00. +/// 2. IF the balance is 100.00 and the documents 90.00 with nothing +/// unattached: +/// - SHOULD be 10.00 +/// BECAUSE the account holds 10.00 no document explains. +/// ``` +pub fn control_difference(balance: T, documents: T, unattached: T) -> Option { + balance + .checked_sub(documents) + .and_then(|d| d.checked_sub(unattached)) +} + +/// A member's book capital less their tax capital, by cause: the +/// beginning difference, plus the built-in gain, plus property without a +/// basis, less property distributed (all at book). +/// +/// ```text +/// GOAL: the reconciliation total is exactly book ending less tax ending +/// as far as the books know it: what contributing and distributing +/// property does to the two capitals is the only way they part. +/// SOURCE: BS-partner-4 (the book-to-tax capital reconciliation: book +/// ending less tax ending = beginning (book less tax) + built-in +/// gain share + property without a basis (at book) - property +/// distributed (at book)); BS-asset-4 (the section 704(c) +/// built-in gain); BS-entry-4. +/// INVARIANTS: +/// 1. Some(t): t = beginning + built_in_gain + without_basis - +/// distributed, exactly. +/// 2. For any book and tax capital rows that differ only by those +/// causes (tax contributions = book contributions less the gain and +/// the property without a basis; tax distributions = book +/// distributions less what was distributed), book ending less tax +/// ending = reconciliation_total (Lean: Bookspec.Reconcile). +/// 3. None when a step cannot be represented; NEVER a rounded or +/// wrapped figure, never a panic. +/// SCENARIOS: +/// 1. IF two members jointly contribute a laptop worth 1,440.00 whose +/// basis is 1,688.40 (no other activity): +/// - SHOULD give each a total of -124.20 +/// BECAUSE book property is 720.00 and tax 844.20. +/// ``` +pub fn reconciliation_total( + beginning: T, + built_in_gain: T, + without_basis: T, + distributed: T, +) -> Option { + beginning + .checked_add(built_in_gain)? + .checked_add(without_basis)? + .checked_sub(distributed) +} diff --git a/crates/ledger-core/src/ledger.rs b/crates/ledger-core/src/ledger.rs index f7dcd0bc..a7a09a71 100644 --- a/crates/ledger-core/src/ledger.rs +++ b/crates/ledger-core/src/ledger.rs @@ -7494,10 +7494,9 @@ impl Ledger { } } let documents_total = doc_totals.get(&account).copied().unwrap_or_default(); - let difference = balance - .checked_sub(documents_total) - .and_then(|d| d.checked_sub(unattached_sum)) - .ok_or_else(too_large)?; + let difference = + crate::kernel::control_difference(balance, documents_total, unattached_sum) + .ok_or_else(too_large)?; out.push(arap::ControlCheck { role: role.key.to_string(), account, diff --git a/crates/ledger-core/src/tax_capital.rs b/crates/ledger-core/src/tax_capital.rs index 8cb33d22..a3c29a45 100644 --- a/crates/ledger-core/src/tax_capital.rs +++ b/crates/ledger-core/src/tax_capital.rs @@ -274,10 +274,12 @@ pub struct Reconciliation { impl Reconciliation { /// Book ending less tax ending (as far as known). pub fn total(&self) -> Option { - self.beginning - .checked_add(self.built_in_gain_704c)? - .checked_add(self.property_without_basis)? - .checked_sub(self.property_distributed) + crate::kernel::reconciliation_total( + self.beginning, + self.built_in_gain_704c, + self.property_without_basis, + self.property_distributed, + ) } }