Merge main into the K-U5 branch: X-H1b, K-U3, S-E2, S-E3, X-H1c

This commit is contained in:
swaits committed 2026-11-09 04:35:21 -07:00
commit 5e4950697e
100 files changed
+1793 -374

No files matched your search

+4 -4
View File
@@ -203,7 +203,7 @@ pub enum DocStatus {
/// Less than nothing owed: more paid against it than it is for, a
/// credit balance owed back (to the customer on an invoice, by the
/// vendor on a bill). Only in books an earlier version left out of
/// bounds (BS-arap-2 INVARIANT 2); never a receivable or a payable, so
/// bounds (BS-arap-2 invariant 6); never a receivable or a payable, so
/// never aged.
Overpaid,
/// Its own entry stands but carries no line tagged with it (an older
@@ -263,7 +263,7 @@ pub struct DocState {
impl DocState {
/// It owes less than nothing or more than its total: books an earlier
/// version left out of bounds (BS-arap-2 INVARIANT 2; plan P4b.14 item 3),
/// version left out of bounds (BS-arap-2 invariant 6; plan P4b.14 item 3),
/// which the control check lists for attention.
pub fn out_of_bounds(&self) -> bool {
self.outstanding < Decimal::ZERO || self.outstanding > self.total
@@ -1144,8 +1144,8 @@ pub type DocumentPartKey = (Option<crate::AccountId>, Option<crate::ClassId>);
/// GOAL: a document's lines post to their accounts and count toward their
/// projects, and a document whose lines name neither posts exactly
/// as before.
/// SOURCE: BS-statement-5 item 3 (job costing: the revenue and
/// direct costs identified with one job) and item 8 (an
/// SOURCE: BS-statement-5 statement 2 (job costing: the revenue and
/// direct costs identified with one job) and statement 4 (an
/// invoice's or bill's line names its project); BS-arap-3 and plan
/// P4b S4.1 A-10 (every bill line names its account).
/// INVARIANTS:
+2 -2
View File
@@ -11,7 +11,7 @@
//! — and postings go through the normal entry machinery, so every
//! accounting invariant holds.
//!
//! Two figures of accumulated depreciation, never confused (BS-asset-1 rule 8):
//! Two figures of accumulated depreciation, never confused (BS-asset-1 statement 28):
//! what the schedule says is due through a date
//! ([`accumulated_scheduled_through`]) and what the books carry, the runs
//! posted and not reversed ([`crate::Ledger::accumulated_posted`]). Book
@@ -743,7 +743,7 @@ fn schedule_months(asset: &Asset, through: NaiveDate) -> Result<Vec<MonthLine>,
/// (inclusive), exact to the minor unit ([`depreciation_schedule`]): the
/// tools' `accumulated_scheduled_through`. What the books carry is
/// [`crate::Ledger::accumulated_posted`]; the two agree once every month
/// due is posted (BS-asset-1 rule 8).
/// due is posted (BS-asset-1 statement 28).
pub fn accumulated_scheduled_through(
asset: &Asset,
as_of: NaiveDate,
+9 -9
View File
@@ -77,7 +77,7 @@ pub enum OperatingClass {
/// (e) Interest paid.
InterestPaid,
/// (f) Income taxes paid. No tax category marks an entity-level income
/// tax yet, so nothing is filed here (BS-statement-3 rule 5).
/// tax yet, so nothing is filed here (BS-statement-3 statement 7).
IncomeTaxesPaid,
/// (g) Other operating cash payments.
OtherPayments,
@@ -178,7 +178,7 @@ pub struct CashFlowLine {
/// The cash, + in.
pub amount: Decimal,
/// Of `amount`, what a member or a lender paid or received for the
/// company with no cash account moving (BS-statement-3 rule 11, D-42):
/// company with no cash account moving (BS-statement-3 statement 16, D-42):
/// a constructive receipt or payment. Zero for most lines.
pub on_behalf: Decimal,
/// Codes of this kind's accounts the entry moved, sorted.
@@ -295,7 +295,7 @@ pub struct OperatingReconciliation {
pub net_cash_from_operating: Decimal,
}
/// The footing check (BS-statement-3 rule 9).
/// The footing check (BS-statement-3 statement 14).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CashFlowFooting {
/// Operating + investing + financing.
@@ -440,8 +440,8 @@ pub enum RoleList {
Investing,
}
/// A contradiction among the cash flow account roles (BS-statement-3 rule
/// 2): the statement refuses rather than guess.
/// A contradiction among the cash flow account roles (BS-statement-3 statement
/// 3): the statement refuses rather than guess.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RoleProblem {
/// An account of the wrong type in a list.
@@ -547,14 +547,14 @@ impl RoleProblem {
}
/// Every contradiction among the cash flow account roles (BS-statement-3
/// rule 2), in a fixed order. Codes the chart lacks are the caller's to
/// statement 3), in a fixed order. Codes the chart lacks are the caller's to
/// report. The one check behind both the statement's refusal and the
/// settings' problems, so the two never drift apart.
///
/// ```text
/// GOAL: the statement's account roles partition the chart, so every
/// account is in exactly one section.
/// SOURCE: BS-statement-3 rule 2 (ASC 230-10-45-10: every cash receipt and
/// SOURCE: BS-statement-3 statement 3 (ASC 230-10-45-10: every cash receipt and
/// payment is investing, financing or operating).
/// INVARIANTS:
/// 1. Cash and investing accounts MUST be assets; financing accounts
@@ -639,7 +639,7 @@ pub fn role_problems(
out
}
/// BS-statement-3 rule 9, on its own so the check is tested directly (it
/// BS-statement-3 statement 14, on its own so the check is tested directly (it
/// cannot fail on posted entries): the sections sum to the change in
/// cash, or nothing is produced.
pub fn foot(sections_sum: Decimal, net_change: Decimal) -> Result<CashFlowFooting, LedgerError> {
@@ -978,7 +978,7 @@ impl<'a> Roles<'a> {
}
/// The liabilities that hold distributions declared by `end` (
/// BS-statement-3 rule 2), among the accounts `open` leaves to be placed: each
/// BS-statement-3 statement 3), among the accounts `open` leaves to be placed: each
/// credited, in the posted entries dated on or before `end` (closes and
/// reversals aside), only by entries whose debits are all to equity (Dr
/// Distributions / Cr Distributions Payable; a corporation's Dr Retained
+25 -10
View File
@@ -505,7 +505,7 @@ pub enum LedgerError {
end: String,
},
/// The statement of cash flows would not foot (BS-statement-3 rule 9):
/// The statement of cash flows would not foot (BS-statement-3 statement 14):
/// an engine error, never output. It cannot happen on balanced books
/// (Lean `cashflow_sections_sum_to_cash_change`); if it does, the
/// engine is wrong, and nothing is shown rather than a wrong figure.
@@ -523,7 +523,7 @@ pub enum LedgerError {
},
/// The cash flow statement's account roles (cash, financing, investing)
/// contradict each other or the chart (BS-statement-3 rule 2). The text
/// contradict each other or the chart (BS-statement-3 statement 3). The text
/// names the setting and says what to change.
#[error("cash flow account roles: {0}")]
CashFlowRoles(String),
@@ -695,13 +695,13 @@ pub enum LedgerError {
SettlementMixedKinds(String),
/// A settlement's documents don't all belong to the one party it
/// names (BS-arap-6 INVARIANT 1).
/// names (BS-arap-6 invariant 4).
#[error("{0} is not {1}'s: a settlement covers one party only")]
SettlementPartiesDiffer(String, u64),
/// A discount needs a reason: none is stated in the document's terms,
/// or it is larger than its terms allow, or it is taken after the
/// discount date (BS-arap-6 INVARIANT 2).
/// discount date (BS-arap-6 invariant 6).
#[error(
"{doc}: the discount of {discount} needs a reason ({because}); pass discount_reason with the payment"
)]
@@ -749,9 +749,24 @@ pub enum LedgerError {
#[error("{0}")]
InvalidFee(String),
/// A settlement's figures can't be exact: the cash and discounts
/// don't cover the applications, or the fee leaves a negative deposit
/// (BS-arap-6 INVARIANT 1).
/// A processor's fee is above the cash it was withheld from: a
/// processor cannot keep more than it was paid (BS-arap-6). A fee equal
/// to the cash is [`LedgerError::InvalidSettlement`]: the deposit would
/// be nothing.
#[error(
"the processor's fee of {fee} is above the cash of {cash}: a processor can't keep more than it was paid"
)]
FeeAboveCash {
/// The fee asked for.
fee: Decimal,
/// The settlement's cash, summed over its applications.
cash: Decimal,
},
/// A settlement's figures can't be exact (BS-arap-6 invariant 3): an
/// amount too large to add, a negative fee, or a fee that leaves
/// nothing to deposit; a fee above the cash is
/// [`LedgerError::FeeAboveCash`].
#[error("{0}")]
InvalidSettlement(String),
@@ -781,7 +796,7 @@ pub enum LedgerError {
},
/// Receivables or payables can't move to an account holding lines
/// that are no document's (BS-arap-2 INVARIANT 1): they would
/// that are no document's (BS-arap-2 statement 12): they would
/// become hand lines on a control account.
#[error(
"account {code} \"{name}\" holds lines no invoice or bill carries (entry {entry} first), \
@@ -828,7 +843,7 @@ pub enum LedgerError {
},
/// A reversal or payment would leave a document owing less than nothing
/// or more than its total at some date (BS-arap-2 INVARIANT 2): the
/// or more than its total at some date (BS-arap-2 invariant 6): the
/// message says which document, when, and what to reverse first.
#[error("{0}")]
DocumentBounds(String),
@@ -867,7 +882,7 @@ pub enum LedgerError {
#[error("{0}")]
InvalidSend(String),
/// A bad debt can't be written off on cash-basis books (
/// BS-arap-5 INVARIANT 3; IRC §166(a); Treas. Reg. §1.166-1(e)): the
/// BS-arap-5 statement 6; IRC §166(a); Treas. Reg. §1.166-1(e)): the
/// invoice was never counted as income, so there is no basis to
/// deduct.
#[error(
+2 -2
View File
@@ -43,7 +43,7 @@ pub enum Leftover {
/// SOURCE: BS-partner-1 (partners share by the agreement; capital accounts
/// carry the allocations), BS-partner-3 (a party's part of an
/// entry), BS-partner-4 (the K-1 worksheet), BS-1099-1 (a payment
/// split by the parties' shares), BS-statement-3 rule 5 (the
/// split by the parties' shares), BS-statement-3 statement 8 (the
/// government's part of a payment), BS-arap-3 (a document line's
/// exact amount).
/// INVARIANTS:
@@ -173,7 +173,7 @@ const MAX_CORRECTION_STEPS: usize = 4096;
/// SOURCE: BS-partner-1 (partners share by the agreement; the largest
/// remainder is the owner's choice, 2026-10-13), BS-partner-3 (a
/// party's part of an entry), BS-partner-4 (the K-1 worksheet's
/// shares), BS-1099-1 (a payment's buckets), BS-statement-3 rule 5
/// shares), BS-1099-1 (a payment's buckets), BS-statement-3 statement 8
/// (the government's part), BS-arap-3 (a document line's exact
/// amount), BS-ledger-1 (a whole number of smallest units).
/// INVARIANTS:
+20 -20
View File
@@ -391,7 +391,7 @@ pub fn net<T: Amount>(lines: impl IntoIterator<Item = (Side, T)>) -> Option<T> {
/// ```text
/// GOAL: an early-payment discount never takes more than its percentage
/// says, in exact money.
/// SOURCE: BS-arap-3 INVARIANT 2, BS-arap-6; plan/served.md
/// SOURCE: BS-arap-3 invariant 2, BS-arap-6; plan/served.md
/// 8.3.13; CHOICE: the gross method, A-8 (the discount is
/// booked when taken, never estimated).
/// INVARIANTS:
@@ -1047,8 +1047,8 @@ pub fn apportion_cumulative<T: Amount>(
Some(())
}
/// Where a line falls on the statement of cash flows (BS-statement-3 rule
/// 2): a cash account, or one of the three sections.
/// Where a line falls on the statement of cash flows (BS-statement-3 statement
/// 3): a cash account, or one of the three sections.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Flow {
/// A cash account: what the statement explains the change in.
@@ -1063,7 +1063,7 @@ pub enum Flow {
impl Flow {
/// The three sections, in the order cash is applied to them (
/// BS-statement-3 rule 3): operating, investing, financing.
/// BS-statement-3 statement 4): operating, investing, financing.
pub const SECTIONS: [Flow; 3] = [Flow::Operating, Flow::Investing, Flow::Financing];
}
@@ -1116,7 +1116,7 @@ pub fn take<T: Amount>(remaining: T, movement: T) -> Option<T> {
/// ```text
/// GOAL: split an entry's cash among the parts it settled, without
/// inventing or losing any.
/// SOURCE: BS-statement-3 rule 3 (ASC 230-10-50-5: only the cash portion
/// SOURCE: BS-statement-3 statement 4 (ASC 230-10-50-5: only the cash portion
/// of a part-cash transaction is a cash flow; the order is a
/// CONVENTION, the ASC is silent).
/// INVARIANTS:
@@ -1143,7 +1143,7 @@ pub fn apply_cash<T: Amount>(cash: T, movements: &[T], applied: &mut [T]) -> Opt
(remaining == T::ZERO).then_some(())
}
/// An entry's cash flows (BS-statement-3 rule 3): the net movement of its
/// An entry's cash flows (BS-statement-3 statement 4): the net movement of its
/// cash lines, applied to the sections moving against it in the order
/// operating, investing, financing, each up to its own movement; what is
/// left of a section's movement is the part no cash settled. `None` when
@@ -1244,7 +1244,7 @@ fn credits_and_debits<T: Amount>(movements: &[T]) -> Option<(T, T)> {
}
/// Which way money a member or a lender handled for the company went
/// (BS-statement-3 rule 11, D-42).
/// (BS-statement-3 statement 16, D-42).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum OnBehalf {
/// Paid for the company: sources credited (borrowed, contributed)
@@ -1258,7 +1258,7 @@ pub enum OnBehalf {
impl OnBehalf {
/// The other way: what an entry's reversal does.
///
/// SOURCE: BS-statement-3 rule 11 (what a member or lender paid or
/// SOURCE: BS-statement-3 statement 16 (what a member or lender paid or
/// received for the company is mirrored by its reversal).
///
/// INVARIANTS:
@@ -1273,7 +1273,7 @@ impl OnBehalf {
/// What a member or a lender paid, or received, for the company with no
/// cash account moving: a constructive receipt and disbursement (
/// BS-statement-3 rule 11, D-42). `sources` are what is left unsettled of the
/// BS-statement-3 statement 16, D-42). `sources` are what is left unsettled of the
/// entry's kinds that can pay or receive for the company (a financing
/// liability, a member's equity), `uses` of the kinds they can pay or
/// receive for, each debit-positive. [`OnBehalf::Paid`]: `amount =
@@ -1289,7 +1289,7 @@ impl OnBehalf {
/// GOAL: show money a member or lender handled for the company as the
/// company's own, borrowed (or contributed) and spent, without
/// inventing or losing a cent.
/// SOURCE: BS-statement-3 rule 11 (D-42; ASC 230 is silent, PwC Viewpoint
/// SOURCE: BS-statement-3 statement 16 (D-42; ASC 230 is silent, PwC Viewpoint
/// FSP 6.9.3.1, Deloitte DART Roadmap 7.2, BDO Blueprint 3.4).
/// INVARIANTS:
/// 1. The parts net to zero: Σ from + Σ to = 0, so the entry's cash
@@ -1322,7 +1322,7 @@ pub fn constructive<T: Amount>(
}
/// How much of an entry is an exchange inside investing and financing
/// that no cash settled (BS-statement-3 rule 7, ASC 230-10-50-4: debt
/// that no cash settled (BS-statement-3 statement 12, ASC 230-10-50-4: debt
/// converted to equity, one asset traded for another). `debits` and
/// `credits` are the net debits and net credits of the entry's investing
/// and financing balance-sheet accounts (each account netted first; both
@@ -1449,7 +1449,7 @@ fn least<T: Amount>(a: T, b: T) -> T {
}
/// What an entry's parties split and what of it counts (BS-partner-3
/// item 3, every case but a close and a reversal, which the caller
/// statement 3, every case but a close and a reversal, which the caller
/// handles: a close is its parties' own-account movement with direction
/// neither, a reversal is [`reverse_target`] of its original's).
///
@@ -1553,7 +1553,7 @@ pub fn party_target<T: Amount>(
})
}
/// A reversal's target from its original's (BS-partner-3 item 3; BS-ledger-5: a
/// A reversal's target from its original's (BS-partner-3 statement 3; BS-ledger-5: a
/// correction is the opposite entry): every amount negated (never to a
/// negative zero), paid and received swapped. An involution. `None` past
/// the type's range.
@@ -2043,12 +2043,12 @@ pub fn basis_figures<T: Amount>(
}
/// A project's unrecovered cost from its tagged revenue and expenses to
/// date: what it would take to be repaid in full (BS-statement-5 item 4).
/// date: what it would take to be repaid in full (BS-statement-5 statement 5).
///
/// ```text
/// GOAL: the "if repaid" figure of work done on spec, from one function
/// the tools, the viewer and bookspec share.
/// SOURCE: BS-statement-5 item 4 (ASC 450-30-25-1: a hoped-for
/// SOURCE: BS-statement-5 statement 5 (ASC 450-30-25-1: a hoped-for
/// payment is a gain contingency, never recorded before it is
/// received; so this is a figure, never a receivable).
/// INVARIANTS:
@@ -2087,7 +2087,7 @@ pub enum Bounds {
/// ```text
/// GOAL: one definition of "a document is never overpaid and never owes
/// more than it is for", at every date, not only at the end.
/// SOURCE: BS-arap-2 INVARIANT 2 (0 <= outstanding(d, D)
/// SOURCE: BS-arap-2 invariant 6 (0 <= outstanding(d, D)
/// <= total(d) at every D); plan/served.md P4b.3 (Bounds).
/// INVARIANTS:
/// 1. Within exactly when every prefix sum of `deltas` is within
@@ -2134,7 +2134,7 @@ fn out_of_bounds<T: Amount>(total: T, running: T) -> Option<(T, bool)> {
/// GOAL: a command never puts a document out of bounds, yet books an
/// earlier version left out of bounds (a void under a payment that
/// stands) take the step that mends them.
/// SOURCE: BS-arap-2 INVARIANT 2, BS-entry-4 (replay accepts what
/// SOURCE: BS-arap-2 invariant 6, BS-entry-4 (replay accepts what
/// was written; the bound is the commands'); plan/served.md
/// P4b.14 item 3 (the remedy: reverse the payment).
/// INVARIANTS:
@@ -2179,7 +2179,7 @@ pub fn bounds_kept<T: Amount>(total: T, before: &[T], after: &[T]) -> Bounds {
mod tests {
use super::*;
/// BS-statement-3 rule 3, by hand. Equipment 1,000 for 300 cash and a
/// BS-statement-3 statement 4, by hand. Equipment 1,000 for 300 cash and a
/// 700 note: investing takes the 300, the note's 700 and the rest of
/// the equipment's are the noncash part.
#[test]
@@ -2225,7 +2225,7 @@ mod tests {
assert_eq!(apply_cash(10, &[-3], &mut out), None);
}
/// The exchange measure (BS-statement-3 rule 7) on the review's
/// The exchange measure (BS-statement-3 statement 12) on the review's
/// hand-checked cases.
#[test]
fn exchanged_matches_the_hand_checked_cases() {
@@ -2247,7 +2247,7 @@ mod tests {
assert_eq!(exchanged(1000i64, 1000, 1000, 1000), Some(0));
}
/// BS-statement-3 rule 11 (D-42), by hand.
/// BS-statement-3 statement 16 (D-42), by hand.
#[test]
fn constructive_pays_and_receives_for_the_company() {
use OnBehalf::{Paid, Received};
+1 -1
View File
@@ -42,7 +42,7 @@
//! to zero, and a reversal's parts are the original's negated (BS-statement-3);
//! * what a member or lender paid or received for the company, with no
//! cash account moving, is split among the kinds it settled, netting to
//! zero, and mirrored by a reversal (BS-statement-3 rule 11);
//! zero, and mirrored by a reversal (BS-statement-3 statement 16);
//! * a party's part of an entry is signed as a line on the party's own
//! account would be, a reversal's is its original's negated, and what of
//! it counts as money or as property never exceeds it (BS-partner-3);
+36 -24
View File
@@ -1394,7 +1394,7 @@ impl Ledger {
/// - SHOULD refuse (RoleAccountHasHandLines), naming the entry
/// - NEVER make a hand line a control account's
/// BECAUSE no command adds an untagged control line (BS-arap-2
/// INVARIANT 1). Replay never refuses (BS-entry-4).
/// invariant 2). Replay never refuses (BS-entry-4).
/// An account the role moves off keeps its documents' lines, and
/// stays guarded while it holds them (Self::control_role_of).
/// ```
@@ -1658,7 +1658,7 @@ impl Ledger {
/// no other: a chart's own account at a role's default code
/// (1200 Equipment, a card at 2000) is anyone's until a document
/// posts there.
/// SOURCE: BS-ledger-8, BS-arap-2 INVARIANT 1.
/// SOURCE: BS-ledger-8, BS-arap-2 statement 9.
/// INVARIANTS:
/// 1. Some(role) when the role is assigned to the account, or the
/// account holds a line tagged with one of the role's documents
@@ -1761,7 +1761,7 @@ impl Ledger {
/// ```text
/// GOAL: no command adds a line on a control account that is no
/// document's; one only takes a legacy one away.
/// SOURCE: BS-arap-2 INVARIANT 1 and item 3 (the
/// SOURCE: BS-arap-2 invariant 2 and statement 11 (the
/// exemption).
/// INVARIANTS:
/// 1. A tagged line passes.
@@ -2310,7 +2310,7 @@ impl Ledger {
/// - NEVER refuse reversing a payment, which only reopens its
/// document
/// BECAUSE 0 <= outstanding <= total at every date (BS-arap-2
/// INVARIANT 2). Replay never refuses (BS-entry-4).
/// invariant 6). Replay never refuses (BS-entry-4).
/// ```
pub fn check_reversal(&self, entry: EntryId, date: NaiveDate) -> Result<(), LedgerError> {
let original = self
@@ -2515,7 +2515,7 @@ impl Ledger {
self.replay_add_line(id, line.account, kernel::flip(line.side), line.amount)?;
// The reversal offsets the original line for line: carry the
// class so divisional P&L nets correctly, and the document so
// it moves by exactly the opposite (BS-arap-2 INVARIANT 3).
// it moves by exactly the opposite (BS-arap-2 invariant 4).
if line.class_id.is_some() {
self.set_line_class(id, idx, line.class_id)?;
}
@@ -3240,7 +3240,7 @@ impl Ledger {
/// ```text
/// GOAL: a run the books record is the entry that posted it, no more
/// and no less, so accumulated_posted is what the books carry.
/// SOURCE: BS-asset-1 rules 6 and 8, BS-asset-3; BS-entry-4 (new commands are bounded; replay checks
/// SOURCE: BS-asset-1 statements 14 and 28, BS-asset-3; BS-entry-4 (new commands are bounded; replay checks
/// only what every journaled run passed:
/// record_depreciation_run).
/// ```
@@ -5797,7 +5797,7 @@ impl Ledger {
/// line per (account, project) part the lines name, the parts
/// naming neither as one (arap::document_parts), each revenue line
/// tagged with its project; the receivable line never (
/// BS-statement-5 item 8).
/// BS-statement-5 statement 6).
/// 2. The receivable line MUST carry the invoice (Origin), in the
/// step that posts it: the invoice starts Open, owing its total.
/// 3. A refusal MUST leave the books unchanged.
@@ -5932,7 +5932,7 @@ impl Ledger {
/// with the vendor (1099s): one line per (account, project) part
/// the lines name, the parts naming neither as one
/// (arap::document_parts), each tagged with its project; the
/// payable line never (BS-statement-5 item 8).
/// payable line never (BS-statement-5 statement 6).
/// 2. The payable line MUST carry the bill (Origin), in the step that
/// posts it.
/// 3. A refusal MUST leave the books unchanged.
@@ -6293,7 +6293,7 @@ impl Ledger {
/// GOAL: every figure about a document (paid, outstanding, status,
/// overdue) is read from its tagged lines, so a reversal, a void
/// or a bounced payment is right by construction.
/// SOURCE: BS-arap-2 (INVARIANTS 1-4), BS-ledger-5, BS-ledger-6, BS-arap-1;
/// SOURCE: BS-arap-2 (invariants 1, 4, 6 and 7), BS-ledger-5, BS-ledger-6, BS-arap-1;
/// ASC 310-10 (receivables), ASC 405-20-40-1 (payables).
/// INVARIANTS:
/// 1. outstanding = the signed sum of every posted line tagged with
@@ -6443,7 +6443,7 @@ impl Ledger {
/// Refuse a step that would move `doc` by `extra` (dated movements)
/// if, at some date, it would then owe less than nothing or more than
/// its total where it didn't before ([`kernel::bounds_kept`];
/// BS-arap-2 INVARIANT 2). Books an earlier version left out of bounds
/// BS-arap-2 invariant 6). Books an earlier version left out of bounds
/// (a void under a payment that stands) take any step that makes no
/// date worse, so the remedy (reversing the payment) is accepted.
fn check_item_bounds(
@@ -6504,7 +6504,7 @@ impl Ledger {
/// The least `doc` owes on any day from `date` on (from its own date,
/// for a date before it): what a payment may be, and what a write-off
/// may be, so no later day ends out of bounds (BS-arap-2
/// INVARIANT 2, BS-arap-5 INVARIANT 1).
/// invariant 6, BS-arap-5 invariant 3).
fn least_owed_from(&self, doc: arap::DocRef, date: NaiveDate) -> Result<Decimal, LedgerError> {
let (doc_date, ..) = self.doc_header(doc)?;
let from = date.max(doc_date);
@@ -6729,9 +6729,22 @@ impl Ledger {
discounts.push(app.discount);
}
let fee_amount = params.fee.as_ref().map_or(Decimal::ZERO, |f| f.amount);
// A fee above the cash is its own refusal; the kernel would also
// answer `None` for it, with no way to say which of its refusals it
// was. A sum that overflows is left to the kernel (too large).
if let Some(total_cash) = cash
.iter()
.try_fold(Decimal::ZERO, |sum, c| sum.checked_add(*c))
&& fee_amount > total_cash
{
return Err(LedgerError::FeeAboveCash {
fee: fee_amount,
cash: total_cash,
});
}
let settlement = kernel::settle(&cash, &discounts, fee_amount).ok_or_else(|| {
LedgerError::InvalidSettlement(
"the cash and discounts don't cover the applications exactly, or the fee is above the cash"
"the settlement's figures aren't exact: an amount is too large, or the fee is negative"
.into(),
)
})?;
@@ -7151,7 +7164,7 @@ impl Ledger {
/// 3. 0 < amount <= the least the invoice owes on any day from
/// `date` on (on the day, when nothing later lowers it): a
/// partial write-off is allowed (§1.166-3(a)), and no day may
/// owe less than nothing (BS-arap-2 INVARIANT 2).
/// owe less than nothing (BS-arap-2 invariant 6).
/// 4. The day MUST be in the calendar, in an open period, and not
/// before the invoice.
/// 5. A refusal MUST leave the books unchanged.
@@ -7165,7 +7178,7 @@ impl Ledger {
/// 4. IF it is dated before the invoice or in a closed period:
/// SHOULD refuse.
/// 5. Half paid, the rest written off: Written off, with 50 paid and
/// 50 written off (BS-arap-2 SCENARIO 7).
/// 50 written off (BS-arap-2 scenario 7).
/// ```
pub fn write_off(
&mut self,
@@ -7270,7 +7283,7 @@ impl Ledger {
/// ```text
/// GOAL: a document that should never have existed (a duplicate, the
/// wrong customer) is undone by an opposite entry, never erased.
/// SOURCE: BS-ledger-5, BS-ledger-6, BS-period-2, BS-arap-2 (scenarios 2, 3);
/// SOURCE: BS-ledger-5, BS-ledger-6, BS-period-2, BS-arap-2 (scenarios 2 and 3);
/// plan/served.md P4b.5, P4b.6; ASC 250 (an immaterial error is
/// corrected in the period found).
/// INVARIANTS:
@@ -7338,7 +7351,7 @@ impl Ledger {
/// ```text
/// GOAL: show that receivables and payables equal their documents, and
/// list every line that is no document's.
/// SOURCE: BS-ledger-8, BS-arap-2 INVARIANT 1:
/// SOURCE: BS-ledger-8, BS-arap-2 invariant 1:
/// GL(A, D) = Σ_docs outstanding(d, D) + unattached(A, D).
/// INVARIANTS:
/// 1. balance sums every posted line on the account dated on or
@@ -7574,8 +7587,7 @@ impl Ledger {
}
}
/// Tag the line a new payment event names (plan P4b S6; BS-arap-6
/// INVARIANT 3): line `index` of `entry`, when it is on the role's
/// Tag the line a new payment event names (plan P4b S6; WB-arap-15): line `index` of `entry`, when it is on the role's
/// account, on the side the movement posts, untagged, and for `amount`.
/// Otherwise it stays untagged and the control check lists it; replay
/// never refuses (BS-entry-4).
@@ -7784,7 +7796,7 @@ impl Ledger {
}
/// [`Self::replay_invoice_payment`] with the line a new event names and
/// the discount it took (plan P4b S6; BS-arap-6 INVARIANT 3). A
/// the discount it took (plan P4b S6; WB-arap-15). A
/// line the event names but that can't be matched (no such line, the
/// wrong account or side, or already tagged) stays untagged, and the
/// control check lists it: replay never refuses (BS-entry-4).
@@ -7935,7 +7947,7 @@ impl Ledger {
///
/// ```text
/// GOAL: what each customer owes as of a day, by how overdue.
/// SOURCE: BS-arap-1, BS-arap-2 INVARIANT 5.
/// SOURCE: BS-arap-1, BS-arap-2 invariant 8.
/// INVARIANTS:
/// 1. Each invoice issued by `as_of` that owes something then
/// (Self::doc_state's outstanding) is in exactly one bucket, at
@@ -9324,7 +9336,7 @@ impl Ledger {
///
/// ```text
/// GOAL: a new class under a name no other class has.
/// SOURCE: BS-statement-5 item 1.
/// SOURCE: WB-statement-5.
/// SCENARIOS:
/// 1. IF the name is blank: SHOULD refuse (EmptyClassName).
/// 2. IF it is longer than a line, holds a control character or a
@@ -9346,7 +9358,7 @@ impl Ledger {
///
/// ```text
/// GOAL: one rule for every name a command gives a class.
/// SOURCE: BS-statement-5 item 1; BS-partner-2 (no full TIN).
/// SOURCE: WB-statement-5; BS-partner-2 (no full TIN).
/// SCENARIOS:
/// 1. IF `except` is a class and the name is its own, exactly: SHOULD
/// keep it as stored, unchecked, so a fact-only change never
@@ -9495,7 +9507,7 @@ impl Ledger {
/// ```text
/// GOAL: refuse any project record the books can't take, before a
/// line is written.
/// SOURCE: BS-statement-5 item 1 (and BS-partner-2: no full TIN).
/// SOURCE: WB-statement-5 (and BS-partner-2: no full TIN).
/// SCENARIOS:
/// 1. IF the class doesn't exist: SHOULD refuse (UnknownClass).
/// 2. IF the name is blank, too long, or another class has it
@@ -9574,7 +9586,7 @@ impl Ledger {
}
}
/// A class name as names compare (BS-statement-5 item 1): trimmed, in
/// A class name as names compare (WB-statement-5): trimmed, in
/// lower case (Unicode's, so `Éclair` and `éCLAIR` are one name).
fn fold_class_name(name: &str) -> String {
name.trim().to_lowercase()
+3 -3
View File
@@ -850,7 +850,7 @@ impl PartyField {
}
}
/// Which of a party's totals to report (BS-partner-3 item 4). Not the
/// Which of a party's totals to report (BS-partner-3 statement 5). Not the
/// books' accounting method (`books.accounting_basis`): party totals count
/// what moved, whichever method the books keep.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
@@ -981,7 +981,7 @@ impl MoneyAccounts {
/// ```text
/// GOAL: a card is known from what the books record, and a promise (a
/// gift card sold) or a member's own card is never the company's.
/// SOURCE: BS-partner-3 item 5 (DECISION, owner, 2026-10-20: money is
/// SOURCE: BS-partner-3 statement 7 (DECISION, owner, 2026-10-20: money is
/// cash, cards, the members' financing and loans), BS-1099-1 (payment
/// methods, plan P3 defect 12).
/// INVARIANTS:
@@ -1376,7 +1376,7 @@ pub fn activity_with(
}
let shares = ledger.entry_parties(id);
// The target first: an entry that counts nothing is never
// unresolved for shares that don't fit it (BS-partner-3 item 4).
// unresolved for shares that don't fit it (BS-partner-3 statement 5).
let Ok(t) = crate::shares::target(ledger, id, shares, money) else {
a.unresolved.insert(id);
continue;
+1 -1
View File
@@ -17,7 +17,7 @@
//! is the callers' answer rather than a division by zero.
//! SOURCE: BS-partner-1 (partners share by the agreement; largest
//! remainder), BS-1099-1 (a payment's buckets), BS-statement-3
//! rule 5 (the government's part), BS-arap-3 (a document line's
//! statement 8 (the government's part), BS-arap-3 (a document line's
//! exact amount), BS-ledger-1 (whole smallest units).
//! INVARIANTS:
//! 1. The parts sum to the whole, for every policy.
+9 -9
View File
@@ -968,7 +968,7 @@ fn apply_cash_mirrors() {
/// An accepted entry always has cash flows, and they explain its cash
/// exactly: the sections' cash sums to the entry's cash, and what no cash
/// settled (each section's movement plus its cash) sums to zero
/// (BS-statement-3 rules 3 and 9: the statement foots, entry by entry).
/// (BS-statement-3 statements 4 and 14: the statement foots, entry by entry).
#[kani::proof]
#[kani::unwind(5)]
fn entry_flows_sum_to_cash() {
@@ -1043,7 +1043,7 @@ fn entry_flows_are_bounded() {
/// A reversal's cash flows are its original's, negated, part by part:
/// what lets an entry and its reversal in one period drop out of the
/// statement together (BS-statement-3 rule 6). End to end, on entries of
/// statement together (BS-statement-3 statement 11). End to end, on entries of
/// up to two lines: at three (`NF`) one run went past half an hour at
/// four jobs on a busy machine (2026-10-21), so three lines are proved in
/// two halves, `entry_movements_of_a_reversal_mirror` and
@@ -1228,7 +1228,7 @@ fn any_way() -> kernel::OnBehalf {
}
/// What a member or a lender paid or received for the company (
/// BS-statement-3 rule 11, D-42) invents and loses nothing: the parts net to zero,
/// BS-statement-3 statement 16, D-42) invents and loses nothing: the parts net to zero,
/// so an entry's flows still sum to its cash; each part settles its own
/// movement toward zero, never past it; and as much is settled as can be.
#[kani::proof]
@@ -1276,7 +1276,7 @@ fn constructive_nets_to_zero() {
/// The other way, minus the sources and minus the uses give minus the
/// parts: a reversal of a payment made on the company's behalf mirrors it
/// (BS-statement-3 rule 6), so the two drop out together.
/// (BS-statement-3 statement 11), so the two drop out together.
#[kani::proof]
#[kani::unwind(4)]
fn constructive_mirrors() {
@@ -1320,7 +1320,7 @@ fn any_party_inputs() -> (Option<(i64, i64)>, i64, i64, i64) {
(capital, any_amount(), any_amount(), debits)
}
/// BS-partner-3 items 1-4: the direction is the sign of what counts (paid above
/// BS-partner-3 statements 1-5: the direction is the sign of what counts (paid above
/// zero, received below), neither exactly when nothing counts; cash and
/// in kind each carry the whole's sign and together never exceed it; a
/// money target is all cash; a contribution counts all of itself; a
@@ -1393,7 +1393,7 @@ fn direction_of_is_the_sign() {
kani::cover!(amount == 0, "nothing moved");
}
/// BS-partner-3 item 3 and BS-ledger-5: the target is odd. Negating the lines negates the
/// BS-partner-3 statement 3 and BS-ledger-5: the target is odd. Negating the lines negates the
/// capital movement, the money net and the own-account movement, and the
/// property comes from the other side, so it keeps its size: the result
/// is the original's reverse_target, whenever it isn't the debits fallback
@@ -1837,7 +1837,7 @@ fn basis_figures_laws() {
);
}
/// `kernel::unrecovered_cost` (BS-statement-5 item 4), for every revenue
/// `kernel::unrecovered_cost` (BS-statement-5 statement 5), for every revenue
/// and expense a line could carry: never below zero; the shortfall when
/// expenses exceed revenue, else zero; never above max(expenses, 0) when
/// revenue isn't negative (expenses may be negative: a reimbursement
@@ -1859,7 +1859,7 @@ fn unrecovered_cost_laws() {
kani::cover!(u == 0 && revenue > expenses, "repaid, and more");
}
/// `kernel::running_bounds` (BS-arap-2 INVARIANT 2; the bounds every
/// `kernel::running_bounds` (BS-arap-2 invariant 6; the bounds every
/// document command and reversal checks), for every total and every
/// sequence of up to `N` dated movements a line could carry: it says
/// Within exactly when every running balance is in `0 ..= total`, and
@@ -1899,7 +1899,7 @@ fn bounds_check_matches_definition() {
}
/// `kernel::bounds_kept` (the check every document command and reversal
/// runs, BS-arap-2 INVARIANT 2): on books in bounds at every date it
/// runs, BS-arap-2 invariant 6): on books in bounds at every date it
/// is the definition (`running_bounds` of the books after the step); and
/// whenever it says Within, no running balance after the step is further
/// outside `0 ..= total` than before it, so a step that mends books an
+6 -6
View File
@@ -533,7 +533,7 @@ fn profit_and_loss_counted(
))
}
/// A project's figures (BS-statement-5 item 3): the period's statement and
/// A project's figures (BS-statement-5 statement 2): the period's statement and
/// months, and its figures to date.
#[derive(Clone, Debug)]
pub struct ProjectFigures {
@@ -592,7 +592,7 @@ pub struct ProjectToDate {
impl ProjectToDate {
/// Revenue less expenses: the project's contribution before overhead
/// (BS-statement-5 item 7), since only its own tagged lines count.
/// (WB-statement-7), since only its own tagged lines count.
pub fn net(&self) -> Decimal {
self.revenue - self.expenses
}
@@ -611,11 +611,11 @@ impl ProjectToDate {
}
/// Contribution as a percent of revenue, to one place (half to even), or
/// `None` without revenue to divide by (BS-statement-5 item 7).
/// `None` without revenue to divide by (WB-statement-7).
///
/// ```text
/// GOAL: one margin the tools and the viewer print alike.
/// SOURCE: BS-statement-5 item 7 (revenue less the costs traced
/// SOURCE: WB-statement-7 (revenue less the costs traced
/// to the project, fixed and variable alike: Garrison's segment
/// margin, as a ratio; not Horngren's variable-cost contribution
/// margin; a presentation choice, not GAAP).
@@ -650,7 +650,7 @@ pub const PROJECT_MONTHS: u32 = 240;
/// ```text
/// GOAL: what a project earned and cost, from one function the tools, the
/// viewer and bookspec share, so their figures never drift.
/// SOURCE: BS-statement-5 item 3 (job costing), BS-period-1, BS-statement-3.
/// SOURCE: BS-statement-5 statement 2 (job costing), BS-period-1, BS-statement-3.
/// INVARIANTS:
/// 1. MUST be profit_and_loss_for_period with the project's class: only
/// revenue and expense lines tagged to it, closing entries left out.
@@ -748,7 +748,7 @@ pub fn project_to_date(
/// Every entry with a line tagged to the project (drafts included),
/// newest first (by date, then id), with its project amount: its tagged
/// revenue lines less its tagged expense lines, each on its normal side.
/// A tagged balance-sheet line counts zero (BS-statement-5 item 3).
/// A tagged balance-sheet line counts zero (BS-statement-5 statement 2).
pub fn project_entries(ledger: &Ledger, id: ClassId) -> Vec<(crate::EntryId, Decimal)> {
let mut out: Vec<(NaiveDate, crate::EntryId, Decimal)> = Vec::new();
for eid in 1..ledger.next_entry_id() {
+2 -2
View File
@@ -22,7 +22,7 @@
//! part; DECISION, owner, 2026-10-20: a member's entry counts what
//! moved their capital).
//! DEFINITIONS:
//! target (target(), BS-partner-3 item 3): what the shares split,
//! target (target(), BS-partner-3 statement 3): what the shares split,
//! signed debit-positive as a line on the parties' own account would
//! be (positive: the company paid or gave; negative: it received),
//! with its direction (paid, received, neither) and what of it moved
@@ -317,7 +317,7 @@ pub struct Target {
pub in_kind: Decimal,
/// Which side the parts count on in a party's totals: the direction
/// of the entry's root original (itself, unless it is a reversal), so
/// a reversal nets what it reverses (BS-partner-3 item 4).
/// a reversal nets what it reverses (BS-partner-3 statement 5).
pub counts_as: Direction,
}
+2 -2
View File
@@ -89,9 +89,9 @@ use rust_decimal::Decimal;
/// `[start, end]`: its lines on the asset's depreciation expense account,
/// debit-positive (the depreciation still due through the disposal day,
/// or, negative, what the month's run posted past it, credited back:
/// BS-asset-1 rule 5). With the runs
/// BS-asset-1 statements 8 and 9). With the runs
/// (`Ledger::depreciation_posted_between`) it is the asset's depreciation
/// expense in the span, the P&L's and the cash flow's (BS-statement-3 rule 8).
/// expense in the span, the P&L's and the cash flow's (BS-statement-3 statement 13).
/// The disposal entry is the posted entry dated the disposal day that
/// credits the asset's cost account by its cost (`Store::dispose_asset`
/// posts it so).