From 05c4b1638552230ee40efd243903b0e0fd04f1ea Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 7 Nov 2026 09:59:01 -0700 Subject: [PATCH 1/4] test(core): golden file for date-range profit and loss Add a golden for the P&L over [2026-01-01, 2026-01-31] on an extended copy of the hand-verified January fixture: the original five entries, plus a multi-leg equipment purchase, a January close, and a February entry. (The original fixture has no closing entry and no entry after an as-of date, so a copy was extended; the first five entries are unchanged.) Hand recomputation (plan/golden-worksheets/profit-and-loss-period.md): revenue account 4000 is credited 4,000 by the Jan 10 invoice; expense account 6000 is debited 800 by the Jan 15 rent. The Jan 31 closing entry (Dr revenue 4,000, Cr rent 800, Cr retained earnings 3,200) lies inside the range but is excluded because a close moves earnings rather than earning or spending, and the Feb 5 entry after the range is excluded too. So revenue 4,000.00, expenses 800.00, net income 3,200.00 -- exactly the 3,200 the close moved. The golden pins those numbers. Co-Authored-By: DeepSeek V4.1 Flash --- .../tests/golden/profit_and_loss_period.json | 20 +++ crates/ledger-core/tests/golden_reports.rs | 155 ++++++++++++++++++ .../profit-and-loss-period.md | 66 ++++++++ 3 files changed, 241 insertions(+) create mode 100644 crates/ledger-core/tests/golden/profit_and_loss_period.json create mode 100644 crates/ledger-core/tests/golden_reports.rs create mode 100644 plan/golden-worksheets/profit-and-loss-period.md diff --git a/crates/ledger-core/tests/golden/profit_and_loss_period.json b/crates/ledger-core/tests/golden/profit_and_loss_period.json new file mode 100644 index 00000000..f3ec70fc --- /dev/null +++ b/crates/ledger-core/tests/golden/profit_and_loss_period.json @@ -0,0 +1,20 @@ +{ + "start": "2026-01-01", + "end": "2026-01-31", + "revenue": [ + { + "code": "4000", + "name": "Service Revenue", + "amount": "4000" + } + ], + "expenses": [ + { + "code": "6000", + "name": "Rent Expense", + "amount": "800" + } + ], + "total_revenue": "4000", + "total_expenses": "800" +} \ No newline at end of file diff --git a/crates/ledger-core/tests/golden_reports.rs b/crates/ledger-core/tests/golden_reports.rs new file mode 100644 index 00000000..4dc3e1ea --- /dev/null +++ b/crates/ledger-core/tests/golden_reports.rs @@ -0,0 +1,155 @@ +//! Golden files for the report rows in `docs/parity.md` that had none: +//! profit and loss for a date range, balance sheet as of a date, trial +//! balance as of a date, and the general ledger by account. +//! +//! The fixture is an **extended copy** of the hand-verified +//! `fixture_company()` in `invariants.rs`. The original has no closing +//! entry, no entry dated after an as-of date, and no multi-leg entry, so it +//! cannot show the exclusions and the `-Split-` case these reports are +//! about. Entries 1–5 are the original fixture unchanged; entries 6–8 are +//! the extension. Every expected number is computed by hand in +//! `plan/golden-worksheets/`; see the worksheet named in each test. + +use chrono::NaiveDate; +use ledger_core::reports::profit_and_loss_for_period; +use ledger_core::{AccountId, AccountType, Currency, Ledger, Money, Side}; +use rust_decimal::Decimal; + +fn date(day: u32) -> NaiveDate { + NaiveDate::from_ymd_opt(2026, 1, day).unwrap() +} + +fn february(day: u32) -> NaiveDate { + NaiveDate::from_ymd_opt(2026, 2, day).unwrap() +} + +fn usd(amount: u64) -> Money { + Money::new(Decimal::from(amount), Currency::USD) +} + +/// Post a balanced entry in one call. Fixtures must be valid by construction. +fn post(ledger: &mut Ledger, date: NaiveDate, description: &str, lines: &[(AccountId, Side, u64)]) { + // As replay posts it: this fixture holds hand lines on the receivable + // and payable control accounts (1200, 2000), which books an earlier + // version wrote still hold and every report must read; the control + // guard (rules.md B-38) refuses them as new work. + ledger_core::parties::replaying(|| { + let id = ledger.create_entry(date, description); + for (account, side, amount) in lines { + ledger.add_line(id, *account, *side, usd(*amount)).unwrap(); + } + ledger.post_entry(id).unwrap(); + }) +} + +/// The extended January 2026 fixture: the original five entries, then a +/// multi-leg equipment purchase (cash and payable), a January close, and a +/// February entry after every as-of date used here. +fn golden_company() -> Ledger { + let mut ledger = Ledger::new(Currency::USD); + let cash = ledger + .add_account("1000", "Cash", AccountType::Asset, None, false) + .unwrap(); + let ar = ledger + .add_account( + "1200", + "Accounts Receivable", + AccountType::Asset, + None, + false, + ) + .unwrap(); + let equipment = ledger + .add_account("1500", "Equipment", AccountType::Asset, None, false) + .unwrap(); + let ap = ledger + .add_account( + "2000", + "Accounts Payable", + AccountType::Liability, + None, + false, + ) + .unwrap(); + let equity = ledger + .add_account("3000", "Owner's Equity", AccountType::Equity, None, false) + .unwrap(); + let revenue = ledger + .add_account("4000", "Service Revenue", AccountType::Revenue, None, false) + .unwrap(); + let rent = ledger + .add_account("6000", "Rent Expense", AccountType::Expense, None, false) + .unwrap(); + + // 1. Owner invests $10,000 cash. + post( + &mut ledger, + date(2), + "Owner investment", + &[(cash, Side::Debit, 10_000), (equity, Side::Credit, 10_000)], + ); + // 2. Buy equipment for $2,500 cash. + post( + &mut ledger, + date(5), + "Buy equipment", + &[(equipment, Side::Debit, 2_500), (cash, Side::Credit, 2_500)], + ); + // 3. Bill a client $4,000. + post( + &mut ledger, + date(10), + "Invoice client", + &[(ar, Side::Debit, 4_000), (revenue, Side::Credit, 4_000)], + ); + // 4. Pay January rent, $800. + post( + &mut ledger, + date(15), + "January rent", + &[(rent, Side::Debit, 800), (cash, Side::Credit, 800)], + ); + // 5. Collect $1,500 of receivables. + post( + &mut ledger, + date(20), + "Client payment", + &[(cash, Side::Debit, 1_500), (ar, Side::Credit, 1_500)], + ); + // 6. Buy more equipment: $1,000 cash and $500 on account (multi-leg, so + // its Equipment line's split is "-Split-"). + post( + &mut ledger, + date(25), + "Buy more equipment", + &[ + (equipment, Side::Debit, 1_500), + (cash, Side::Credit, 1_000), + (ap, Side::Credit, 500), + ], + ); + // 7. Close January: revenue 4,000 less rent 800 moves 3,200 to retained + // earnings, dated at the period end (2026-01-31). + let period = ledger.create_period("2026-01", date(1), date(31)).unwrap(); + ledger.close_period(period).unwrap(); + // 8. February revenue after every as-of date in these tests. + post( + &mut ledger, + february(5), + "February service", + &[(ar, Side::Debit, 600), (revenue, Side::Credit, 600)], + ); + + ledger +} + +/// By-hand worksheet: `plan/golden-worksheets/profit-and-loss-period.md`. +#[test] +fn golden_profit_and_loss_period() { + let ledger = golden_company(); + let report = profit_and_loss_for_period(&ledger, date(1), date(31), None).unwrap(); + let got = serde_json::to_value(&report).unwrap(); + let want: serde_json::Value = + serde_json::from_str(include_str!("golden/profit_and_loss_period.json")).unwrap(); + assert_eq!(got, want); +} diff --git a/plan/golden-worksheets/profit-and-loss-period.md b/plan/golden-worksheets/profit-and-loss-period.md new file mode 100644 index 00000000..736fa02b --- /dev/null +++ b/plan/golden-worksheets/profit-and-loss-period.md @@ -0,0 +1,66 @@ +# Golden worksheet — profit and loss for a date range + +Report: `profit_and_loss_for_period(&ledger, 2026-01-01, 2026-01-31, None)`. +Golden: `crates/ledger-core/tests/golden/profit_and_loss_period.json`. +Test: `golden_profit_and_loss_period` in +`crates/ledger-core/tests/golden_reports.rs`. + +## Fixture + +The fixture is an **extended copy** of `fixture_company()` in +`tests/invariants.rs` (the hand-verified January 2026 fixture), because the +original has neither a closing entry nor an entry dated after an as-of date +and no multi-leg entry (needed for the general-ledger split). The six +original entries are unchanged; entries 6–8 are the extension: + +| # | Date | Description | Lines | +|---|------|-------------|-------| +| 1 | 2026-01-02 | Owner investment | Dr 1000 Cash 10,000.00; Cr 3000 Owner's Equity 10,000.00 | +| 2 | 2026-01-05 | Buy equipment | Dr 1500 Equipment 2,500.00; Cr 1000 Cash 2,500.00 | +| 3 | 2026-01-10 | Invoice client | Dr 1200 Accounts Receivable 4,000.00; Cr 4000 Service Revenue 4,000.00 | +| 4 | 2026-01-15 | January rent | Dr 6000 Rent Expense 800.00; Cr 1000 Cash 800.00 | +| 5 | 2026-01-20 | Client payment | Dr 1000 Cash 1,500.00; Cr 1200 Accounts Receivable 1,500.00 | +| 6 | 2026-01-25 | Buy more equipment | Dr 1500 Equipment 1,500.00; Cr 1000 Cash 1,000.00; Cr 2000 Accounts Payable 500.00 | +| 7 | 2026-01-31 | Close period 2026-01 (generated by `close_period`) | Dr 4000 Service Revenue 4,000.00; Cr 6000 Rent Expense 800.00; Cr 3999 Retained Earnings 3,200.00 | +| 8 | 2026-02-05 | February service | Dr 1200 Accounts Receivable 600.00; Cr 4000 Service Revenue 600.00 | + +Every entry balances: 1–5 are two-line pairs; 6 is 1,500 = 1,000 + 500; 7 +is 4,000 = 800 + 3,200; 8 is 600 = 600. + +Accounts: 1000 Cash (Asset), 1200 Accounts Receivable (Asset), 1500 +Equipment (Asset), 2000 Accounts Payable (Liability), 3000 Owner's Equity +(Equity), 4000 Service Revenue (Revenue), 6000 Rent Expense (Expense), and +3999 Retained Earnings (Equity, created by the close). + +## Hand computation + +The range is 2026-01-01 through 2026-01-31, both inclusive. + +- **Closing entry 7 is inside the range but excluded.** A close moves + earnings into retained earnings; it neither earns revenue nor spends + money (rules.md B-14/B-11). Counting it would make the month break even. +- **Entry 8 (2026-02-05) is outside the range and excluded.** + +Revenue accounts, normal side credit, amount = credits − debits: + +- 4000 Service Revenue: entry 3 credits 4,000; entry 7 is the close + (excluded); entry 8 is February (excluded). 4,000 − 0 = **4,000.00**. + +Expense accounts, normal side debit, amount = debits − credits: + +- 6000 Rent Expense: entry 4 debits 800; entry 7 is the close (excluded). + 800 − 0 = **800.00**. + +Totals: + +- `total_revenue` = **4,000.00** +- `total_expenses` = **800.00** +- net income = 4,000 − 800 = **3,200.00** + +Cross-check against the close: entry 7 credits Retained Earnings exactly +3,200, which is January's revenue minus expenses. The P&L for the closed +month therefore reports the same earnings the close moved. + +The fixture therefore demonstrates both exclusions the parity row asks +for: a closing entry inside the range is not counted, and a later-period +entry is not counted. From be9bb02e1b37a94de90d2a7040050daf0ffa1862 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 7 Nov 2026 09:59:36 -0700 Subject: [PATCH 2/4] test(core): golden file for the balance sheet as of a date Add a golden for balance_sheet_as_of on 2026-01-31 over the shared extended January fixture. Closing entries count on a balance sheet, so the January close's 3,200 to retained earnings is in; the Feb 5 entry is after the as-of date and is out. Hand recomputation (plan/golden-worksheets/balance-sheet-as-of.md): asset balances are debits minus credits -- cash 10,000 + 1,500 - 2,500 - 800 - 1,000 = 7,200, receivables 4,000 - 1,500 = 2,500, equipment 2,500 + 1,500 = 4,000, total assets 13,700. Liabilities: payable 500. Equity: owner's equity 10,000 plus retained earnings 3,200. Current earnings is zero because the close zeroed revenue (4,000 credit and 4,000 debit) and rent (800 debit and 800 credit). Total equity 13,200, and 13,700 = 500 + 13,200. The Feb 5 entry would have shown AR 3,100 and current earnings 600; the golden pins the January figures instead. Co-Authored-By: DeepSeek V4.1 Flash --- .../tests/golden/balance_sheet_as_of.json | 43 +++++++++++ crates/ledger-core/tests/golden_reports.rs | 13 +++- plan/golden-worksheets/balance-sheet-as-of.md | 74 +++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 crates/ledger-core/tests/golden/balance_sheet_as_of.json create mode 100644 plan/golden-worksheets/balance-sheet-as-of.md diff --git a/crates/ledger-core/tests/golden/balance_sheet_as_of.json b/crates/ledger-core/tests/golden/balance_sheet_as_of.json new file mode 100644 index 00000000..51f3db8e --- /dev/null +++ b/crates/ledger-core/tests/golden/balance_sheet_as_of.json @@ -0,0 +1,43 @@ +{ + "as_of": "2026-01-31", + "assets": [ + { + "code": "1000", + "name": "Cash", + "amount": "7200" + }, + { + "code": "1200", + "name": "Accounts Receivable", + "amount": "2500" + }, + { + "code": "1500", + "name": "Equipment", + "amount": "4000" + } + ], + "liabilities": [ + { + "code": "2000", + "name": "Accounts Payable", + "amount": "500" + } + ], + "equity": [ + { + "code": "3000", + "name": "Owner's Equity", + "amount": "10000" + }, + { + "code": "3999", + "name": "Retained Earnings", + "amount": "3200" + } + ], + "current_earnings": "0", + "total_assets": "13700", + "total_liabilities": "500", + "total_equity": "13200" +} \ No newline at end of file diff --git a/crates/ledger-core/tests/golden_reports.rs b/crates/ledger-core/tests/golden_reports.rs index 4dc3e1ea..c144efc4 100644 --- a/crates/ledger-core/tests/golden_reports.rs +++ b/crates/ledger-core/tests/golden_reports.rs @@ -11,7 +11,7 @@ //! `plan/golden-worksheets/`; see the worksheet named in each test. use chrono::NaiveDate; -use ledger_core::reports::profit_and_loss_for_period; +use ledger_core::reports::{balance_sheet_as_of, profit_and_loss_for_period}; use ledger_core::{AccountId, AccountType, Currency, Ledger, Money, Side}; use rust_decimal::Decimal; @@ -153,3 +153,14 @@ fn golden_profit_and_loss_period() { serde_json::from_str(include_str!("golden/profit_and_loss_period.json")).unwrap(); assert_eq!(got, want); } + +/// By-hand worksheet: `plan/golden-worksheets/balance-sheet-as-of.md`. +#[test] +fn golden_balance_sheet_as_of() { + let ledger = golden_company(); + let report = balance_sheet_as_of(&ledger, date(31)).unwrap(); + let got = serde_json::to_value(&report).unwrap(); + let want: serde_json::Value = + serde_json::from_str(include_str!("golden/balance_sheet_as_of.json")).unwrap(); + assert_eq!(got, want); +} diff --git a/plan/golden-worksheets/balance-sheet-as-of.md b/plan/golden-worksheets/balance-sheet-as-of.md new file mode 100644 index 00000000..4a18aff7 --- /dev/null +++ b/plan/golden-worksheets/balance-sheet-as-of.md @@ -0,0 +1,74 @@ +# Golden worksheet — balance sheet as of a date + +Report: `balance_sheet_as_of(&ledger, 2026-01-31)`. +Golden: `crates/ledger-core/tests/golden/balance_sheet_as_of.json`. +Test: `golden_balance_sheet_as_of` in +`crates/ledger-core/tests/golden_reports.rs`. + +## Fixture + +The shared extended copy of `fixture_company()`; see +`profit-and-loss-period.md` for the full table and why it was extended. +Entries 1–5 are the original fixture, 6 is a multi-leg purchase, 7 is the +January close, 8 is a February entry after the as-of date: + +| # | Date | Description | Lines | +|---|------|-------------|-------| +| 1 | 2026-01-02 | Owner investment | Dr 1000 Cash 10,000.00; Cr 3000 Owner's Equity 10,000.00 | +| 2 | 2026-01-05 | Buy equipment | Dr 1500 Equipment 2,500.00; Cr 1000 Cash 2,500.00 | +| 3 | 2026-01-10 | Invoice client | Dr 1200 Accounts Receivable 4,000.00; Cr 4000 Service Revenue 4,000.00 | +| 4 | 2026-01-15 | January rent | Dr 6000 Rent Expense 800.00; Cr 1000 Cash 800.00 | +| 5 | 2026-01-20 | Client payment | Dr 1000 Cash 1,500.00; Cr 1200 Accounts Receivable 1,500.00 | +| 6 | 2026-01-25 | Buy more equipment | Dr 1500 Equipment 1,500.00; Cr 1000 Cash 1,000.00; Cr 2000 Accounts Payable 500.00 | +| 7 | 2026-01-31 | Close period 2026-01 (generated) | Dr 4000 Service Revenue 4,000.00; Cr 6000 Rent Expense 800.00; Cr 3999 Retained Earnings 3,200.00 | +| 8 | 2026-02-05 | February service | Dr 1200 Accounts Receivable 600.00; Cr 4000 Service Revenue 600.00 | + +Accounts: 1000 Cash (Asset), 1200 Accounts Receivable (Asset), 1500 +Equipment (Asset), 2000 Accounts Payable (Liability), 3000 Owner's Equity +(Equity), 4000 Service Revenue (Revenue), 6000 Rent Expense (Expense), 3999 +Retained Earnings (Equity, created by the close). + +## Hand computation + +As of 2026-01-31, inclusive: entries 1–7 count, entry 8 (2026-02-05) does +not. Closing entries count on a balance sheet: after a close the earnings +sit in retained earnings. + +Asset accounts, normal side debit, amount = debits − credits: + +- 1000 Cash: debits 10,000 + 1,500 = 11,500; credits 2,500 + 800 + 1,000 = + 4,300; 11,500 − 4,300 = **7,200.00**. +- 1200 Accounts Receivable: debits 4,000; credits 1,500; 4,000 − 1,500 = + **2,500.00**. +- 1500 Equipment: debits 2,500 + 1,500 = 4,000; credits 0; **4,000.00**. +- `total_assets` = 7,200 + 2,500 + 4,000 = **13,700.00**. + +Liability accounts, normal side credit, amount = credits − debits: + +- 2000 Accounts Payable: credits 500; debits 0; **500.00**. +- `total_liabilities` = **500.00**. + +Equity accounts, normal side credit, amount = credits − debits: + +- 3000 Owner's Equity: credits 10,000; debits 0; **10,000.00**. +- 3999 Retained Earnings: credits 3,200; debits 0; **3,200.00**. + +Current earnings (revenue minus expenses through the as-of date, not yet +closed): + +- 4000 Service Revenue: credit 4,000 (entry 3) and the close's debit 4,000 + (entry 7) → movement 0, no line. +- 6000 Rent Expense: debit 800 (entry 4) and the close's credit 800 (entry + 7) → movement 0, no line. +- `current_earnings` = **0.00**. + +Totals: + +- `total_equity` = 10,000 + 3,200 + 0 = **13,200.00**. +- Accounting equation: assets 13,700 = liabilities 500 + equity 13,200. + Balanced exactly. + +**Entry 8 is left out.** If it were included (wrong for this as-of date), +AR would be 3,100, revenue/current earnings 600, total assets 14,300 and +total equity 13,800 — balanced but dated 2026-02-05. The golden pins the +January-only figures. From 73e1210248ffa9b65206e82eb30108f45ba6560b Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 7 Nov 2026 10:00:05 -0700 Subject: [PATCH 3/4] test(core): golden file for the trial balance as of a date Add a golden for trial_balance_as_of on 2026-01-31 over the shared extended January fixture. The as-of applies before the close is read, so this is the post-closing trial balance: revenue and rent net to zero and the close's 3,200 sits in retained earnings; the Feb 5 entry is after the as-of date and is out. Hand recomputation (plan/golden-worksheets/trial-balance-as-of.md): cash net debit 10,000 + 1,500 - 2,500 - 800 - 1,000 = 7,200; receivables 4,000 - 1,500 = 2,500; equipment 2,500 + 1,500 = 4,000; payable credit 500; owner's equity credit 10,000; retained earnings credit 3,200. Service revenue and rent expense each have equal debits and credits after the close, so neither is listed. Totals: debits 13,700, credits 500 + 10,000 + 3,200 = 13,700. The Feb 5 entry would have added 600 to each column; the golden pins the January figures. Co-Authored-By: DeepSeek V4.1 Flash --- .../tests/golden/trial_balance_as_of.json | 42 ++++++++++++ crates/ledger-core/tests/golden_reports.rs | 13 +++- plan/golden-worksheets/trial-balance-as-of.md | 67 +++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 crates/ledger-core/tests/golden/trial_balance_as_of.json create mode 100644 plan/golden-worksheets/trial-balance-as-of.md diff --git a/crates/ledger-core/tests/golden/trial_balance_as_of.json b/crates/ledger-core/tests/golden/trial_balance_as_of.json new file mode 100644 index 00000000..f8ff88ce --- /dev/null +++ b/crates/ledger-core/tests/golden/trial_balance_as_of.json @@ -0,0 +1,42 @@ +{ + "lines": [ + { + "code": "1000", + "name": "Cash", + "debit": "7200", + "credit": "0" + }, + { + "code": "1200", + "name": "Accounts Receivable", + "debit": "2500", + "credit": "0" + }, + { + "code": "1500", + "name": "Equipment", + "debit": "4000", + "credit": "0" + }, + { + "code": "2000", + "name": "Accounts Payable", + "debit": "0", + "credit": "500" + }, + { + "code": "3000", + "name": "Owner's Equity", + "debit": "0", + "credit": "10000" + }, + { + "code": "3999", + "name": "Retained Earnings", + "debit": "0", + "credit": "3200" + } + ], + "total_debits": "13700", + "total_credits": "13700" +} \ No newline at end of file diff --git a/crates/ledger-core/tests/golden_reports.rs b/crates/ledger-core/tests/golden_reports.rs index c144efc4..9104677e 100644 --- a/crates/ledger-core/tests/golden_reports.rs +++ b/crates/ledger-core/tests/golden_reports.rs @@ -11,7 +11,7 @@ //! `plan/golden-worksheets/`; see the worksheet named in each test. use chrono::NaiveDate; -use ledger_core::reports::{balance_sheet_as_of, profit_and_loss_for_period}; +use ledger_core::reports::{balance_sheet_as_of, profit_and_loss_for_period, trial_balance_as_of}; use ledger_core::{AccountId, AccountType, Currency, Ledger, Money, Side}; use rust_decimal::Decimal; @@ -164,3 +164,14 @@ fn golden_balance_sheet_as_of() { serde_json::from_str(include_str!("golden/balance_sheet_as_of.json")).unwrap(); assert_eq!(got, want); } + +/// By-hand worksheet: `plan/golden-worksheets/trial-balance-as-of.md`. +#[test] +fn golden_trial_balance_as_of() { + let ledger = golden_company(); + let report = trial_balance_as_of(&ledger, date(31)); + let got = serde_json::to_value(&report).unwrap(); + let want: serde_json::Value = + serde_json::from_str(include_str!("golden/trial_balance_as_of.json")).unwrap(); + assert_eq!(got, want); +} diff --git a/plan/golden-worksheets/trial-balance-as-of.md b/plan/golden-worksheets/trial-balance-as-of.md new file mode 100644 index 00000000..359a2d8f --- /dev/null +++ b/plan/golden-worksheets/trial-balance-as-of.md @@ -0,0 +1,67 @@ +# Golden worksheet — trial balance as of a date + +Report: `trial_balance_as_of(&ledger, 2026-01-31)`. +Golden: `crates/ledger-core/tests/golden/trial_balance_as_of.json`. +Test: `golden_trial_balance_as_of` in +`crates/ledger-core/tests/golden_reports.rs`. + +## Fixture + +The shared extended copy of `fixture_company()`; see +`profit-and-loss-period.md` for the full table and why it was extended. +Entries 1–5 are the original fixture, 6 is a multi-leg purchase, 7 is the +January close, 8 is a February entry after the as-of date: + +| # | Date | Description | Lines | +|---|------|-------------|-------| +| 1 | 2026-01-02 | Owner investment | Dr 1000 Cash 10,000.00; Cr 3000 Owner's Equity 10,000.00 | +| 2 | 2026-01-05 | Buy equipment | Dr 1500 Equipment 2,500.00; Cr 1000 Cash 2,500.00 | +| 3 | 2026-01-10 | Invoice client | Dr 1200 Accounts Receivable 4,000.00; Cr 4000 Service Revenue 4,000.00 | +| 4 | 2026-01-15 | January rent | Dr 6000 Rent Expense 800.00; Cr 1000 Cash 800.00 | +| 5 | 2026-01-20 | Client payment | Dr 1000 Cash 1,500.00; Cr 1200 Accounts Receivable 1,500.00 | +| 6 | 2026-01-25 | Buy more equipment | Dr 1500 Equipment 1,500.00; Cr 1000 Cash 1,000.00; Cr 2000 Accounts Payable 500.00 | +| 7 | 2026-01-31 | Close period 2026-01 (generated) | Dr 4000 Service Revenue 4,000.00; Cr 6000 Rent Expense 800.00; Cr 3999 Retained Earnings 3,200.00 | +| 8 | 2026-02-05 | February service | Dr 1200 Accounts Receivable 600.00; Cr 4000 Service Revenue 600.00 | + +Accounts: 1000 Cash (Asset), 1200 Accounts Receivable (Asset), 1500 +Equipment (Asset), 2000 Accounts Payable (Liability), 3000 Owner's Equity +(Equity), 4000 Service Revenue (Revenue), 6000 Rent Expense (Expense), 3999 +Retained Earnings (Equity, created by the close). + +## Hand computation + +As of 2026-01-31, inclusive: entries 1–7 count, entry 8 (2026-02-05) does +not. A trial balance shows each account's signed movement: debit positive, +credit negative, then in one column on the side it falls (never negative). + +Per account over entries 1–7: + +- **1000 Cash:** debits 10,000 (e1) + 1,500 (e5) = 11,500; credits 2,500 + (e2) + 800 (e4) + 1,000 (e6) = 4,300; net debit 11,500 − 4,300 = + **debit 7,200.00**. +- **1200 Accounts Receivable:** debits 4,000 (e3); credits 1,500 (e5); net + **debit 2,500.00**. +- **1500 Equipment:** debits 2,500 (e2) + 1,500 (e6) = 4,000; credits 0; + **debit 4,000.00**. +- **2000 Accounts Payable:** credits 500 (e6); debits 0; **credit 500.00**. +- **3000 Owner's Equity:** credits 10,000 (e1); debits 0; **credit + 10,000.00**. +- **3999 Retained Earnings:** credits 3,200 (e7); debits 0; **credit + 3,200.00**. +- **4000 Service Revenue:** credits 4,000 (e3), debits 4,000 (close e7); + net 0 — the invariant says a zero balance is not listed. +- **6000 Rent Expense:** debits 800 (e4), credits 800 (close e7); net 0 — + not listed. + +Lines are in code order: 1000, 1200, 1500, 2000, 3000, 3999. + +Totals: + +- `total_debits` = 7,200 + 2,500 + 4,000 = **13,700.00**. +- `total_credits` = 500 + 10,000 + 3,200 = **13,700.00**. +- Balanced exactly. + +**Entry 8 is left out.** If included it would add 600 to AR (3,100) and +600 to revenue (credit 600), shifting both columns by 600; the golden pins +the post-closing January trial balance after the close, with the February +entry absent. From c5824c4fa2f48b49007e2adf9c616b6ba1b3f4bd Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 7 Nov 2026 10:00:51 -0700 Subject: [PATCH 4/4] test(core): golden file for the general ledger by account Add a golden for general_ledger over [2026-01-01, 2026-01-31] on the shared extended January fixture, with running balances, each posting's split, per-account totals, and entry ids. The close is a posting like any other here; the Feb 5 entry is after the period end and is out. Hand recomputation (plan/golden-worksheets/general-ledger.md): cash has five postings (begin 0, debits 11,500, credits 4,300, ending 7,200); receivables 4,000 - 1,500 = 2,500; equipment 2,500 + 1,500 = 4,000; payable 500; owner's equity 10,000; retained earnings 3,200; service revenue 4,000 credit then the close's 4,000 debit, ending 0; rent 800 debit then the close's 800 credit, ending 0. Entry 6's equipment debit has two accounts on the credit side (cash and payable), so its split is "-Split-"; the same for entry 7's revenue debit (rent and retained earnings) and for the close's two other lines the split is the single account 4000 Service Revenue. Every ending balance ties to the as-of trial balance. Co-Authored-By: DeepSeek V4.1 Flash --- .../tests/golden/general_ledger.json | 238 ++++++++++++++++++ crates/ledger-core/tests/golden_reports.rs | 15 +- plan/golden-worksheets/general-ledger.md | 133 ++++++++++ 3 files changed, 385 insertions(+), 1 deletion(-) create mode 100644 crates/ledger-core/tests/golden/general_ledger.json create mode 100644 plan/golden-worksheets/general-ledger.md diff --git a/crates/ledger-core/tests/golden/general_ledger.json b/crates/ledger-core/tests/golden/general_ledger.json new file mode 100644 index 00000000..3cb358f2 --- /dev/null +++ b/crates/ledger-core/tests/golden/general_ledger.json @@ -0,0 +1,238 @@ +{ + "start": "2026-01-01", + "end": "2026-01-31", + "accounts": [ + { + "code": "1000", + "name": "Cash", + "account_type": "Asset", + "beginning_balance": "0", + "postings": [ + { + "date": "2026-01-02", + "entry_id": 1, + "description": "Owner investment", + "split": "3000 Owner's Equity", + "debit": "10000", + "credit": "0", + "balance": "10000" + }, + { + "date": "2026-01-05", + "entry_id": 2, + "description": "Buy equipment", + "split": "1500 Equipment", + "debit": "0", + "credit": "2500", + "balance": "7500" + }, + { + "date": "2026-01-15", + "entry_id": 4, + "description": "January rent", + "split": "6000 Rent Expense", + "debit": "0", + "credit": "800", + "balance": "6700" + }, + { + "date": "2026-01-20", + "entry_id": 5, + "description": "Client payment", + "split": "1200 Accounts Receivable", + "debit": "1500", + "credit": "0", + "balance": "8200" + }, + { + "date": "2026-01-25", + "entry_id": 6, + "description": "Buy more equipment", + "split": "1500 Equipment", + "debit": "0", + "credit": "1000", + "balance": "7200" + } + ], + "total_debits": "11500", + "total_credits": "4300", + "ending_balance": "7200" + }, + { + "code": "1200", + "name": "Accounts Receivable", + "account_type": "Asset", + "beginning_balance": "0", + "postings": [ + { + "date": "2026-01-10", + "entry_id": 3, + "description": "Invoice client", + "split": "4000 Service Revenue", + "debit": "4000", + "credit": "0", + "balance": "4000" + }, + { + "date": "2026-01-20", + "entry_id": 5, + "description": "Client payment", + "split": "1000 Cash", + "debit": "0", + "credit": "1500", + "balance": "2500" + } + ], + "total_debits": "4000", + "total_credits": "1500", + "ending_balance": "2500" + }, + { + "code": "1500", + "name": "Equipment", + "account_type": "Asset", + "beginning_balance": "0", + "postings": [ + { + "date": "2026-01-05", + "entry_id": 2, + "description": "Buy equipment", + "split": "1000 Cash", + "debit": "2500", + "credit": "0", + "balance": "2500" + }, + { + "date": "2026-01-25", + "entry_id": 6, + "description": "Buy more equipment", + "split": "-Split-", + "debit": "1500", + "credit": "0", + "balance": "4000" + } + ], + "total_debits": "4000", + "total_credits": "0", + "ending_balance": "4000" + }, + { + "code": "2000", + "name": "Accounts Payable", + "account_type": "Liability", + "beginning_balance": "0", + "postings": [ + { + "date": "2026-01-25", + "entry_id": 6, + "description": "Buy more equipment", + "split": "1500 Equipment", + "debit": "0", + "credit": "500", + "balance": "500" + } + ], + "total_debits": "0", + "total_credits": "500", + "ending_balance": "500" + }, + { + "code": "3000", + "name": "Owner's Equity", + "account_type": "Equity", + "beginning_balance": "0", + "postings": [ + { + "date": "2026-01-02", + "entry_id": 1, + "description": "Owner investment", + "split": "1000 Cash", + "debit": "0", + "credit": "10000", + "balance": "10000" + } + ], + "total_debits": "0", + "total_credits": "10000", + "ending_balance": "10000" + }, + { + "code": "3999", + "name": "Retained Earnings", + "account_type": "Equity", + "beginning_balance": "0", + "postings": [ + { + "date": "2026-01-31", + "entry_id": 7, + "description": "Close period 2026-01", + "split": "4000 Service Revenue", + "debit": "0", + "credit": "3200", + "balance": "3200" + } + ], + "total_debits": "0", + "total_credits": "3200", + "ending_balance": "3200" + }, + { + "code": "4000", + "name": "Service Revenue", + "account_type": "Revenue", + "beginning_balance": "0", + "postings": [ + { + "date": "2026-01-10", + "entry_id": 3, + "description": "Invoice client", + "split": "1200 Accounts Receivable", + "debit": "0", + "credit": "4000", + "balance": "4000" + }, + { + "date": "2026-01-31", + "entry_id": 7, + "description": "Close period 2026-01", + "split": "-Split-", + "debit": "4000", + "credit": "0", + "balance": "0" + } + ], + "total_debits": "4000", + "total_credits": "4000", + "ending_balance": "0" + }, + { + "code": "6000", + "name": "Rent Expense", + "account_type": "Expense", + "beginning_balance": "0", + "postings": [ + { + "date": "2026-01-15", + "entry_id": 4, + "description": "January rent", + "split": "1000 Cash", + "debit": "800", + "credit": "0", + "balance": "800" + }, + { + "date": "2026-01-31", + "entry_id": 7, + "description": "Close period 2026-01", + "split": "4000 Service Revenue", + "debit": "0", + "credit": "800", + "balance": "0" + } + ], + "total_debits": "800", + "total_credits": "800", + "ending_balance": "0" + } + ] +} \ No newline at end of file diff --git a/crates/ledger-core/tests/golden_reports.rs b/crates/ledger-core/tests/golden_reports.rs index 9104677e..475235b3 100644 --- a/crates/ledger-core/tests/golden_reports.rs +++ b/crates/ledger-core/tests/golden_reports.rs @@ -11,7 +11,9 @@ //! `plan/golden-worksheets/`; see the worksheet named in each test. use chrono::NaiveDate; -use ledger_core::reports::{balance_sheet_as_of, profit_and_loss_for_period, trial_balance_as_of}; +use ledger_core::reports::{ + balance_sheet_as_of, general_ledger, profit_and_loss_for_period, trial_balance_as_of, +}; use ledger_core::{AccountId, AccountType, Currency, Ledger, Money, Side}; use rust_decimal::Decimal; @@ -175,3 +177,14 @@ fn golden_trial_balance_as_of() { serde_json::from_str(include_str!("golden/trial_balance_as_of.json")).unwrap(); assert_eq!(got, want); } + +/// By-hand worksheet: `plan/golden-worksheets/general-ledger.md`. +#[test] +fn golden_general_ledger() { + let ledger = golden_company(); + let report = general_ledger(&ledger, date(1), date(31)).unwrap(); + let got = serde_json::to_value(&report).unwrap(); + let want: serde_json::Value = + serde_json::from_str(include_str!("golden/general_ledger.json")).unwrap(); + assert_eq!(got, want); +} diff --git a/plan/golden-worksheets/general-ledger.md b/plan/golden-worksheets/general-ledger.md new file mode 100644 index 00000000..82b2d89b --- /dev/null +++ b/plan/golden-worksheets/general-ledger.md @@ -0,0 +1,133 @@ +# Golden worksheet — general ledger by account + +Report: `general_ledger(&ledger, 2026-01-01, 2026-01-31)`. +Golden: `crates/ledger-core/tests/golden/general_ledger.json`. +Test: `golden_general_ledger` in +`crates/ledger-core/tests/golden_reports.rs`. + +## Fixture + +The shared extended copy of `fixture_company()`; see +`profit-and-loss-period.md` for the full table and why it was extended. +Entries 1–5 are the original fixture, 6 is a multi-leg purchase, 7 is the +January close, 8 is a February entry after the period end: + +| # | Date | Description | Lines | +|---|------|-------------|-------| +| 1 | 2026-01-02 | Owner investment | Dr 1000 Cash 10,000.00; Cr 3000 Owner's Equity 10,000.00 | +| 2 | 2026-01-05 | Buy equipment | Dr 1500 Equipment 2,500.00; Cr 1000 Cash 2,500.00 | +| 3 | 2026-01-10 | Invoice client | Dr 1200 Accounts Receivable 4,000.00; Cr 4000 Service Revenue 4,000.00 | +| 4 | 2026-01-15 | January rent | Dr 6000 Rent Expense 800.00; Cr 1000 Cash 800.00 | +| 5 | 2026-01-20 | Client payment | Dr 1000 Cash 1,500.00; Cr 1200 Accounts Receivable 1,500.00 | +| 6 | 2026-01-25 | Buy more equipment | Dr 1500 Equipment 1,500.00; Cr 1000 Cash 1,000.00; Cr 2000 Accounts Payable 500.00 | +| 7 | 2026-01-31 | Close period 2026-01 (generated) | Dr 4000 Service Revenue 4,000.00; Cr 6000 Rent Expense 800.00; Cr 3999 Retained Earnings 3,200.00 | +| 8 | 2026-02-05 | February service | Dr 1200 Accounts Receivable 600.00; Cr 4000 Service Revenue 600.00 | + +Accounts: 1000 Cash (Asset), 1200 Accounts Receivable (Asset), 1500 +Equipment (Asset), 2000 Accounts Payable (Liability), 3000 Owner's Equity +(Equity), 4000 Service Revenue (Revenue), 6000 Rent Expense (Expense), 3999 +Retained Earnings (Equity, created by the close). Entry ids are assigned in +posting order, 1–8; the close creates account 3999 without consuming an +entry id, and entry 8 is the February entry. + +## Hand computation + +The period is 2026-01-01 through 2026-01-31, both inclusive. Nothing is +dated before the period, so every beginning balance is 0. Entries 1–7 are +in the period and **entry 8 is after it and excluded**. The closing entry +7 is a posting like any other (the general ledger is not a P&L). + +Splits (QuickBooks convention): the one account on the entry's opposite +side, else `-Split-` when that side has several accounts. Running balances +are on each account's normal side (assets and expenses debit, the rest +credit). Accounts are listed in code order. + +### 1000 Cash — Asset, beginning 0 + +| Date | Entry | Description | Split | Debit | Credit | Balance | +|---|---|---|---|---|---|---| +| 2026-01-02 | 1 | Owner investment | 3000 Owner's Equity | 10,000.00 | 0 | 10,000.00 | +| 2026-01-05 | 2 | Buy equipment | 1500 Equipment | 0 | 2,500.00 | 7,500.00 | +| 2026-01-15 | 4 | January rent | 6000 Rent Expense | 0 | 800.00 | 6,700.00 | +| 2026-01-20 | 5 | Client payment | 1200 Accounts Receivable | 1,500.00 | 0 | 8,200.00 | +| 2026-01-25 | 6 | Buy more equipment | 1500 Equipment | 0 | 1,000.00 | 7,200.00 | + +Debits 10,000 + 1,500 = 11,500; credits 2,500 + 800 + 1,000 = 4,300; +ending 11,500 − 4,300 = **7,200.00**. + +### 1200 Accounts Receivable — Asset, beginning 0 + +| Date | Entry | Description | Split | Debit | Credit | Balance | +|---|---|---|---|---|---|---| +| 2026-01-10 | 3 | Invoice client | 4000 Service Revenue | 4,000.00 | 0 | 4,000.00 | +| 2026-01-20 | 5 | Client payment | 1000 Cash | 0 | 1,500.00 | 2,500.00 | + +Debits 4,000; credits 1,500; ending **2,500.00**. (Entry 8's 600 debit is +dated 2026-02-05 and is excluded; including it would add a third posting +and an ending of 3,100.) + +### 1500 Equipment — Asset, beginning 0 + +| Date | Entry | Description | Split | Debit | Credit | Balance | +|---|---|---|---|---|---|---| +| 2026-01-05 | 2 | Buy equipment | 1000 Cash | 2,500.00 | 0 | 2,500.00 | +| 2026-01-25 | 6 | Buy more equipment | -Split- | 1,500.00 | 0 | 4,000.00 | + +Entry 6's Debit has **two** accounts on the opposite (credit) side, 1000 +Cash and 2000 Accounts Payable, so its split is `-Split-`. Debits 4,000; +credits 0; ending **4,000.00**. + +### 2000 Accounts Payable — Liability, beginning 0 + +| Date | Entry | Description | Split | Debit | Credit | Balance | +|---|---|---|---|---|---|---| +| 2026-01-25 | 6 | Buy more equipment | 1500 Equipment | 0 | 500.00 | 500.00 | + +Debits 0; credits 500; ending **500.00**. + +### 3000 Owner's Equity — Equity, beginning 0 + +| Date | Entry | Description | Split | Debit | Credit | Balance | +|---|---|---|---|---|---|---| +| 2026-01-02 | 1 | Owner investment | 1000 Cash | 0 | 10,000.00 | 10,000.00 | + +Debits 0; credits 10,000; ending **10,000.00**. + +### 3999 Retained Earnings — Equity, beginning 0 + +| Date | Entry | Description | Split | Debit | Credit | Balance | +|---|---|---|---|---|---|---| +| 2026-01-31 | 7 | Close period 2026-01 | 4000 Service Revenue | 0 | 3,200.00 | 3,200.00 | + +The close's Credit has only 4000 Service Revenue on the opposite (debit) +side, so its split is that one account. Debits 0; credits 3,200; ending +**3,200.00**. + +### 4000 Service Revenue — Revenue, beginning 0 + +| Date | Entry | Description | Split | Debit | Credit | Balance | +|---|---|---|---|---|---|---| +| 2026-01-10 | 3 | Invoice client | 1200 Accounts Receivable | 0 | 4,000.00 | 4,000.00 | +| 2026-01-31 | 7 | Close period 2026-01 | -Split- | 4,000.00 | 0 | 0.00 | + +The close's Debit has two accounts on the opposite (credit) side, 6000 Rent +Expense and 3999 Retained Earnings, so its split is `-Split-`. Debits +4,000; credits 4,000; ending **0.00** (the account is still listed because +it has postings in the period). + +### 6000 Rent Expense — Expense, beginning 0 + +| Date | Entry | Description | Split | Debit | Credit | Balance | +|---|---|---|---|---|---|---| +| 2026-01-15 | 4 | January rent | 1000 Cash | 800.00 | 0 | 800.00 | +| 2026-01-31 | 7 | Close period 2026-01 | 4000 Service Revenue | 0 | 800.00 | 0.00 | + +Debits 800; credits 800; ending **0.00** (listed for its postings). + +## Cross-check + +Each ending balance equals the 2026-01-31 trial balance in +`trial-balance-as-of.md`: cash 7,200, receivables 2,500, equipment 4,000, +payable 500, owner's equity 10,000, retained earnings 3,200, revenue 0, +rent 0. The running balances are consistent at every step, and entry 8 +(2026-02-05) never appears.