diff --git a/crates/ledger-store/src/reconcilable.rs b/crates/ledger-store/src/reconcilable.rs index d67d1abf..7ebd8484 100644 --- a/crates/ledger-store/src/reconcilable.rs +++ b/crates/ledger-store/src/reconcilable.rs @@ -5,21 +5,25 @@ //! ```text //! GOAL: the monthly close's reconciliation step can't be forgotten, and //! the digest and the viewer never disagree on what is due. -//! SOURCE: docs/parties-and-attachments.md "Reconciliations" (Needs -//! attention: an account with activity not reconciled through last -//! month's end); CONVENTION (bank and card statements close -//! monthly); AGENTS.md's monthly close. +//! SOURCE: WB-ledger-17; docs/parties-and-attachments.md "Reconciliations" +//! (Needs attention: an account with activity not reconciled +//! through last month's end); CONVENTION (bank and card statements +//! close monthly); AGENTS.md's monthly close. //! DEFINITIONS: //! cash accounts: `[books] cash_accounts`, resolved; when none are set, //! the asset accounts (not placeholders) whose names say they hold -//! cash ("cash", "bank", "checking", "savings", "money market"). +//! cash ("cash", "bank", "checking", "savings", "money market"), in +//! ascending account id; the configured ones keep the order given. //! card accounts: `ledger_core::parties::card_accounts`: the //! liabilities of the business-card payment methods the books record, //! else (none recorded) the liabilities whose names say "card" but -//! not "gift"; never a member's card or a platform's account. +//! not "gift"; never a member's card or a platform's account; in +//! ascending account id. //! INVARIANTS: //! 1. reconcilable_accounts: the cash accounts, then the card accounts, -//! then every other liability with a recorded statement, each once. +//! then every other liability with a recorded statement (in the +//! order the statements are given: the store's are by period end, +//! then file), each once. //! A card is expected before its first statement, so its first //! reconciliation is asked for; a liability named otherwise (an Amex) //! joins once its first statement is recorded. @@ -45,7 +49,8 @@ pub fn cash_accounts(ledger: &Ledger, configured: &[String]) -> Vec { .filter_map(|c| ledger.chart().find_by_code(c).map(|a| a.id)) .collect(); } - // In chart order, as `card_accounts` is: the chart iterates in no order. + // In ascending account id, as `card_accounts` sorts: the chart's own + // iteration order is unspecified (`ChartOfAccounts::all`). let mut found: Vec = ledger .chart() .all() diff --git a/crates/ledger-store/tests/reconcilable.rs b/crates/ledger-store/tests/reconcilable.rs index 92690af6..19bc4e64 100644 --- a/crates/ledger-store/tests/reconcilable.rs +++ b/crates/ledger-store/tests/reconcilable.rs @@ -6,12 +6,10 @@ //! ```text //! GOAL: the monthly close's reconciliation step can't be forgotten, and //! the digest and the viewer never disagree on what is due. -//! SOURCE: docs/parties-and-attachments.md "Reconciliations" (Needs -//! attention: an account with activity not reconciled through last -//! month's end); CONVENTION (bank and card statements close -//! monthly), both as the module's own spec block states them. -//! No BS- or WB- id covers the module yet; the report of this -//! unit says so. +//! SOURCE: WB-ledger-17; docs/parties-and-attachments.md "Reconciliations" +//! (Needs attention: an account with activity not reconciled +//! through last month's end); CONVENTION (bank and card statements +//! close monthly). //! ``` //! //! Every test builds real books in a `Store`, records real statements and @@ -137,8 +135,8 @@ fn names(s: &Store, ids: &[AccountId]) -> Vec { // ---------------------------------------------------------------- the cash accounts -/// DEFINITIONS, cash accounts: with none configured, the asset accounts -/// (not placeholders) whose names say they hold cash, in chart order. A +/// WB-ledger-17 invariant 1, cash accounts: with none configured, the +/// asset accounts (not placeholders) whose names say they hold cash. A /// name is matched in any case; an expense called "Bank Fees", a /// placeholder group and an asset that holds no cash are not cash. #[test] @@ -174,7 +172,7 @@ fn cash_accounts_are_the_assets_whose_names_say_cash() { assert_eq!(CASH_WORDS.len(), 5); let mut found = cash_accounts(s.ledger(), &[]); - // Which accounts, not in which order: the order is the finding below. + // Which accounts, not in which order: the order is the next test's. found.sort_unstable(); assert_eq!(found, vec![checking, savings, petty, mm]); assert_eq!( @@ -188,40 +186,38 @@ fn cash_accounts_are_the_assets_whose_names_say_cash() { ); } -/// FINDING (V-D2; fixed by the commit that removed this test's ignore): the cash accounts found by name came out in the order -/// the chart's `HashMap` yields them (`ChartOfAccounts::all` says "order -/// unspecified"), not in chart order as `card_accounts` does (ascending -/// account id). `books.digest`'s `unreconciled_accounts` lists accounts in -/// this order, so with two or more cash accounts the digest's list can -/// differ between runs of the same books. The module's INVARIANT 1 names -/// the order of the three groups, not within one, so this is a gap in the -/// rule rather than a broken one; the expectation here is the order -/// `card_accounts` already keeps. +/// WB-ledger-17 invariant 1, the order within a group. FINDING (V-D2, +/// fixed in `cash_accounts`): the cash accounts found by name came out in +/// the order the chart's `HashMap` yields them (`ChartOfAccounts::all` +/// says "order unspecified"), so `books.digest`'s `unreconciled_accounts` +/// could list them differently on two runs of the same books. They now +/// come in ascending account id, as `card_accounts` sorts. The order is the +/// id's, not the chart code's: the codes below are deliberately out of +/// creation order, so a sort by code would fail this test. #[test] -fn cash_accounts_come_in_chart_order() { +fn cash_accounts_by_name_come_in_ascending_account_id() { let dir = test_dir("cash-order"); let mut s = Store::create(&dir, Currency::USD).unwrap(); let mut expected = Vec::new(); - for (i, name) in [ - "Checking", - "Savings", - "Petty cash", - "Money market", - "Bank two", - "Cash three", - ] - .iter() - .enumerate() - { - expected.push(account( - &mut s, - &format!("10{i}0"), - name, - AccountType::Asset, - false, - )); + // (code, name) in creation order; the codes do not ascend with it. + for (code, name) in [ + ("1400", "Checking"), + ("1100", "Savings"), + ("1300", "Petty cash"), + ("1000", "Money market"), + ("1200", "Bank two"), + ("1500", "Cash three"), + ] { + expected.push(account(&mut s, code, name, AccountType::Asset, false)); } - // Six accounts in a HashMap come out in any of 720 orders; ascending is one. + let mut by_code = expected.clone(); + by_code.sort_by_key(|&i| s.ledger().chart().get(i).unwrap().code.clone()); + assert_ne!( + by_code, expected, + "the codes must tell id order from code order" + ); + // Six accounts in a HashMap come out in any of 720 orders; ascending id + // is one of them, and the codes' order another. assert_eq!(cash_accounts(s.ledger(), &[]), expected); } diff --git a/docs/spec/claims/ledger.md b/docs/spec/claims/ledger.md index 14bea514..455edae3 100644 --- a/docs/spec/claims/ledger.md +++ b/docs/spec/claims/ledger.md @@ -539,3 +539,53 @@ journal feature `audits`. `workpaper_pdf_is_deterministic`; `ledger-cli` `tests/audit.rs` (`audit_is_deterministic`, `cpa_package_refuses_without_a_counted_audit_unless_a_reason_is_given`, `audits_wait_for_the_upgrade`). + +## Reconciliations: which accounts the monthly close reconciles, and when each is due + +**bookspec:** none + +**Claims:** yes (wholly a product rule: no bookspec rule states it) + +**Pick:** CONVENTION, `docs/parties-and-attachments.md` "Reconciliations" (Needs attention: an account with activity not reconciled through last month's end; bank and card statements close monthly). No authority states it, so it is the product's own. + +### Product rules + +**WB-ledger-17** The accounts the monthly close reconciles, and when each is due, are one rule (`ledger_store::reconcilable`), read by the digest's `unreconciled_accounts` and the viewer's Needs attention alike. + +```text +GOAL: the monthly close's reconciliation step can't be forgotten, and + the digest and the viewer never disagree on what is due. +SOURCE: CONVENTION (bank and card statements close monthly); + docs/parties-and-attachments.md "Reconciliations". +INVARIANTS: + 1. The accounts reconciled are the cash accounts, then the card + accounts, then every other liability with a recorded statement, each + once. The cash accounts are `books.cash_accounts` resolved by code, in + the order given (a code the chart lacks is dropped, and the names are + not then consulted); when none is set, the asset accounts that are + not placeholders and whose names say cash ("cash", "bank", + "checking", "savings", "money market"), in ascending account id. The + card accounts are `ledger_core::parties::card_accounts`, in ascending + account id. The liabilities with a statement follow in the order the + statements are given (the store's: period end, then file). A card is + expected before its first statement; a liability not named a card + joins once its first statement is recorded; an asset with a statement + and no cash name never does. + 2. An account is due through the last day of the month before today's + when it has a posted line dated on or before that day and no + reconciliation not voided reaches it (is dated on or after that + day); otherwise it is not due. A draft line, activity only this + month, and an account with no lines are not due; a voided + reconciliation reaches nothing. +SCENARIOS: + 1. Cash named "Checking" and a card named "Visa Card", an "Amex" with + no statement: the checking and the card are reconciled, the Amex is + not; once the Amex has a statement it joins, after the card. + 2. Today is March 10th, the checking has January activity and a + reconciliation through January 31st: it is due through February + 28th; one through February 28th clears it; voiding that one makes it + due again. +``` + +**Enforced:** `reconcilable_accounts`, `cash_accounts`, `card_accounts`, `last_month_end`, `reconciled_through`, `reconciliation_due` (`ledger_store::reconcilable`). +**Checked:** `ledger-store` `tests/reconcilable.rs` (`cash_accounts_are_the_assets_whose_names_say_cash`, `cash_accounts_by_name_come_in_ascending_account_id`, `configured_cash_accounts_win_by_code`, `card_accounts_follow_the_recorded_method_else_the_name`, `reconcilable_accounts_are_cash_then_cards_then_liabilities_with_statements`, `last_month_end_is_the_last_day_of_the_month_before`, `reconciled_through_is_the_newest_standing_reconciliation`, `an_account_is_due_until_a_reconciliation_reaches_last_months_end`, `only_posted_activity_through_last_months_end_makes_an_account_due`). diff --git a/docs/testing.md b/docs/testing.md index 186317eb..e7614c36 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -4235,7 +4235,7 @@ the name-matched cash accounts in `HashMap` order (`ChartOfAccounts::all` is "order unspecified"), so `books.digest`'s `unreconciled_accounts` could list the same books' accounts in a different order from run to run; `card_accounts` beside it sorts by account id. The test that found it -(`cash_accounts_come_in_chart_order`) was committed `#[ignore]`d with the +(`cash_accounts_by_name_come_in_ascending_account_id`) was committed `#[ignore]`d with the finding in its reason; the fix sorts the list and is the commit after the tests, so it can be dropped without touching them.