merge: every reversal is dated on or after its original (D-73; branch t3/reversal-dating-d73, bb5008ed; reviewed)
claims/ledger.md conflicted with the reduced B-9 section: the D-73 pick, Enforced and Checked lines sit above its product rules; the check's place corrected (after the schedule double-post check, not ahead of it) and the citation completed (BS-ledger-16, the Lean model since L-G1). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
e080f761a0
14 files changed
+279
-69
No files matched your search
@@ -1770,17 +1770,11 @@ impl Store {
|
||||
/// - SHOULD refuse, naming the room left
|
||||
/// - NEVER let accumulated depreciation pass the schedule, or cost
|
||||
/// less salvage.
|
||||
/// 5. IF it is a depreciation run or a schedule month's posting, or a
|
||||
/// reversal in the chain of one, and `date` is before the entry's
|
||||
/// own date:
|
||||
/// - SHOULD refuse
|
||||
/// - NEVER carry a correction at month ends the entry it corrects
|
||||
/// never reached (a run of March reversed on Jan 31 would leave
|
||||
/// January and February with negative depreciation, and a
|
||||
/// disposal in February clearing what they don't carry)
|
||||
/// BECAUSE a correction comes after what it corrects (B-9, Pacioli
|
||||
/// ch. 31: "after you have made this correction"). Commands only:
|
||||
/// a journal an earlier version wrote replays as it was (B-28).
|
||||
/// 5. (the date floor: a reversal dated before the entry it
|
||||
/// reverses is refused for every entry, by `Ledger::check_reversal`
|
||||
/// with `ReversalBeforeOriginal`, D-73; it was this store's check
|
||||
/// for runs and schedule months alone. A journal an earlier
|
||||
/// version wrote replays as it was, B-28.)
|
||||
/// 6. Otherwise: a run of an active asset is un-posted (its month is
|
||||
/// due again, caught up by the next run).
|
||||
/// ```
|
||||
@@ -1797,18 +1791,6 @@ impl Store {
|
||||
.copied()
|
||||
.unwrap_or(entry);
|
||||
let refuse = |why: String| Err(LedgerError::AssetEntryIrreversible(entry, why).into());
|
||||
// SCENARIO 5: the date of the entry reversed (it ends its chain).
|
||||
let dated = self
|
||||
.ledger
|
||||
.get_entry(entry)
|
||||
.map(ledger_core::JournalEntry::date);
|
||||
let before_it = dated.filter(|&d| date < d);
|
||||
if let (Some(on), Some((sid, month))) = (before_it, self.ledger.schedule_posting(top)) {
|
||||
return Err(LedgerError::InvalidSchedule(format!(
|
||||
"entry {entry} (schedule {sid}'s {month}) is dated {on}: a reversal of it can't be dated before it, on {date}"
|
||||
))
|
||||
.into());
|
||||
}
|
||||
let payloads = |t: &str| -> Result<Vec<Value>, StoreError> {
|
||||
let mut stmt = self
|
||||
.conn
|
||||
@@ -1835,11 +1817,6 @@ impl Store {
|
||||
}
|
||||
let asset_id = get_u64(&p, "asset_id")?;
|
||||
let period = get_str(&p, "period")?;
|
||||
if let Some(on) = before_it {
|
||||
return refuse(format!(
|
||||
"it is dated {on}, and a reversal of asset {asset_id}'s depreciation for {period} can't be dated before it, on {date}"
|
||||
));
|
||||
}
|
||||
if self
|
||||
.ledger
|
||||
.get_asset(asset_id)
|
||||
|
||||
@@ -884,6 +884,7 @@ fn a_run_reinstated_after_its_rerun_was_reversed_still_counts() {
|
||||
/// 1,535.71). So is a re-instatement dated before the reversal it reverses,
|
||||
/// and a schedule month's reversal dated before its posting. A reversal on
|
||||
/// the entry's own day is taken. Each refusal journals nothing.
|
||||
/// Since D-73 the refusal is the ledger's for every entry, `ReversalBeforeOriginal`.
|
||||
#[test]
|
||||
fn a_reversal_dated_before_the_entry_it_reverses_is_refused() {
|
||||
let (_dir, mut store) = open_store();
|
||||
@@ -901,10 +902,12 @@ fn a_reversal_dated_before_the_entry_it_reverses_is_refused() {
|
||||
for on in [date(2026, 2, 10), date(2026, 3, 30)] {
|
||||
match store.reverse_entry(mar, on, "backdated") {
|
||||
Err(ledger_store::StoreError::Ledger(
|
||||
ledger_core::LedgerError::AssetEntryIrreversible(e, why),
|
||||
ledger_core::LedgerError::ReversalBeforeOriginal {
|
||||
entry, original, ..
|
||||
},
|
||||
)) => {
|
||||
assert_eq!(e, mar);
|
||||
assert!(why.contains("can't be dated before it"), "{why}");
|
||||
assert_eq!(entry, mar);
|
||||
assert_eq!(original, date(2026, 3, 31));
|
||||
}
|
||||
other => panic!("{on}: {other:?}"),
|
||||
}
|
||||
@@ -917,7 +920,7 @@ fn a_reversal_dated_before_the_entry_it_reverses_is_refused() {
|
||||
assert!(matches!(
|
||||
store.reverse_entry(rev, date(2026, 3, 30), "it was right"),
|
||||
Err(ledger_store::StoreError::Ledger(
|
||||
ledger_core::LedgerError::AssetEntryIrreversible(..)
|
||||
ledger_core::LedgerError::ReversalBeforeOriginal { .. }
|
||||
))
|
||||
));
|
||||
assert_eq!(store.head(), head);
|
||||
@@ -951,7 +954,7 @@ fn a_reversal_dated_before_the_entry_it_reverses_is_refused() {
|
||||
assert!(matches!(
|
||||
store.reverse_entry(feb, date(2026, 1, 31), "backdated"),
|
||||
Err(ledger_store::StoreError::Ledger(
|
||||
ledger_core::LedgerError::InvalidSchedule(_)
|
||||
ledger_core::LedgerError::ReversalBeforeOriginal { .. }
|
||||
))
|
||||
));
|
||||
assert_eq!(store.head(), head);
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
//! A reversal is dated on or after the entry it reverses, for every kind of
|
||||
//! entry (D-73, 2026-11-08; rules.md B-9; bookspec BS-ledger-5): a
|
||||
//! correction comes after what it corrects. A journal an earlier version
|
||||
//! wrote may hold one dated before its original, and it opens as it was
|
||||
//! (B-28: bounds bind new commands, never replay).
|
||||
//!
|
||||
//! ```text
|
||||
//! GOAL: no new reversal precedes its original; no old one stops a journal
|
||||
//! from opening, or moves a figure.
|
||||
//! SOURCE: docs/spec/rules.md B-9, B-28; plan/served.md D-73.
|
||||
//! ```
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use ledger_core::reports::trial_balance;
|
||||
use ledger_core::{AccountType, Currency, LedgerError, Money, Side};
|
||||
use ledger_store::{JournalWriter, Store, StoreError};
|
||||
use rust_decimal::Decimal;
|
||||
use serde_json::json;
|
||||
use std::path::PathBuf;
|
||||
|
||||
struct TestDir(PathBuf);
|
||||
|
||||
impl std::ops::Deref for TestDir {
|
||||
type Target = std::path::Path;
|
||||
fn deref(&self) -> &std::path::Path {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TestDir {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn test_dir(name: &str) -> TestDir {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"wanderbooks-store-revdates-{name}-{}",
|
||||
std::process::id()
|
||||
));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
TestDir(dir)
|
||||
}
|
||||
|
||||
fn d(y: i32, m: u32, day: u32) -> NaiveDate {
|
||||
NaiveDate::from_ymd_opt(y, m, day).unwrap()
|
||||
}
|
||||
|
||||
fn usd(n: i64) -> Money {
|
||||
Money::new(Decimal::new(n, 2), Currency::USD)
|
||||
}
|
||||
|
||||
/// Books with one plain posted entry: rent paid in cash, 100.00, on
|
||||
/// 2026-03-10. Returns (dir, store, rent, cash, entry).
|
||||
fn books(name: &str) -> (TestDir, Store, u64, u64, u64) {
|
||||
let dir = test_dir(name);
|
||||
let mut s = Store::create(&dir, Currency::USD).unwrap();
|
||||
let rent = s
|
||||
.add_account("6000", "Rent", AccountType::Expense, None, false)
|
||||
.unwrap();
|
||||
let cash = s
|
||||
.add_account("1000", "Cash", AccountType::Asset, None, false)
|
||||
.unwrap();
|
||||
let e = s.create_entry(d(2026, 3, 10), "March rent").unwrap();
|
||||
s.add_line(e, rent, Side::Debit, usd(10_000)).unwrap();
|
||||
s.add_line(e, cash, Side::Credit, usd(10_000)).unwrap();
|
||||
s.post_entry(e).unwrap();
|
||||
(dir, s, rent, cash, e)
|
||||
}
|
||||
|
||||
/// A plain entry's reversal dated before it is refused, by the check the
|
||||
/// preview uses and by the command alike, and nothing is journaled; the
|
||||
/// same day and later are taken.
|
||||
#[test]
|
||||
fn a_plain_entrys_reversal_is_not_dated_before_it() {
|
||||
let (_dir, mut s, rent, cash, e) = books("plain");
|
||||
let head = s.head();
|
||||
let tb = trial_balance(s.ledger());
|
||||
let before = (tb.total_debits, tb.total_credits);
|
||||
for early in [d(2026, 3, 9), d(2026, 1, 1)] {
|
||||
assert!(matches!(
|
||||
s.check_reverse_entry(e, early),
|
||||
Err(StoreError::Ledger(LedgerError::ReversalBeforeOriginal { entry, original, date }))
|
||||
if entry == e && original == d(2026, 3, 10) && date == early
|
||||
));
|
||||
assert!(matches!(
|
||||
s.reverse_entry(e, early, "early"),
|
||||
Err(StoreError::Ledger(
|
||||
LedgerError::ReversalBeforeOriginal { .. }
|
||||
))
|
||||
));
|
||||
}
|
||||
assert_eq!(s.head(), head, "a refused reversal journals nothing");
|
||||
let tb = trial_balance(s.ledger());
|
||||
assert_eq!((tb.total_debits, tb.total_credits), before);
|
||||
assert_eq!(s.ledger().account_movement(rent), Decimal::new(10_000, 2));
|
||||
// The same day is fine, and the books net to nothing.
|
||||
s.check_reverse_entry(e, d(2026, 3, 10)).unwrap();
|
||||
let r = s.reverse_entry(e, d(2026, 3, 10), "same day").unwrap();
|
||||
assert_eq!(s.ledger().account_movement(rent), Decimal::ZERO);
|
||||
assert_eq!(s.ledger().account_movement(cash), Decimal::ZERO);
|
||||
// Reversing the reversal (re-instating) is a correction of it too: it
|
||||
// can't precede the reversal, and may follow it.
|
||||
assert!(matches!(
|
||||
s.reverse_entry(r, d(2026, 3, 9), "early"),
|
||||
Err(StoreError::Ledger(
|
||||
LedgerError::ReversalBeforeOriginal { .. }
|
||||
))
|
||||
));
|
||||
s.reverse_entry(r, d(2026, 3, 11), "later").unwrap();
|
||||
assert_eq!(s.ledger().account_movement(rent), Decimal::new(10_000, 2));
|
||||
}
|
||||
|
||||
/// A journal an earlier version wrote, holding a reversal dated before its
|
||||
/// original, opens exactly as it was: same figures, chain verified, in the
|
||||
/// writer and again after a snapshot-less reopen (B-28).
|
||||
#[test]
|
||||
fn a_journal_with_an_earlier_dated_reversal_opens_as_it_was() {
|
||||
let (dir, s, rent, cash, e) = books("replay");
|
||||
let next = s.ledger().next_entry_id();
|
||||
drop(s);
|
||||
// What the old behaviour journaled: the mirror of the entry, dated
|
||||
// 2026-03-01, nine days before it.
|
||||
let mut w = JournalWriter::open(&dir.join(ledger_store::JOURNAL_FILE)).unwrap();
|
||||
let line = |account: u64, side: &str| json!({"account": account, "side": side, "amount": "100.00", "currency": "USD"});
|
||||
w.append(
|
||||
"entry.reversed",
|
||||
json!({"id": next, "reverses": e, "date": "2026-03-01",
|
||||
"description": "backdated by an earlier version",
|
||||
"lines": [line(rent, "credit"), line(cash, "debit")]}),
|
||||
)
|
||||
.unwrap();
|
||||
drop(w);
|
||||
|
||||
let store = Store::open(&dir).unwrap();
|
||||
store.verify().unwrap();
|
||||
let l = store.ledger();
|
||||
let r = l.get_entry(next).unwrap();
|
||||
assert_eq!(r.date(), d(2026, 3, 1));
|
||||
assert_eq!(r.reversal_of(), Some(e));
|
||||
assert_eq!(l.account_movement(rent), Decimal::ZERO);
|
||||
assert_eq!(l.account_movement(cash), Decimal::ZERO);
|
||||
let tb = trial_balance(l);
|
||||
assert_eq!(tb.total_debits, tb.total_credits);
|
||||
// The old entry stands reversed, and the books still refuse a new
|
||||
// backdated reversal of anything.
|
||||
assert!(!l.in_effect(e));
|
||||
assert!(matches!(
|
||||
store.check_reverse_entry(next, d(2026, 2, 1)),
|
||||
Err(StoreError::Ledger(
|
||||
LedgerError::ReversalBeforeOriginal { .. }
|
||||
))
|
||||
));
|
||||
}
|
||||
Reference in new issue
Block a user