fix(core): every reversal is dated on or after its original (D-73)
A correction comes after what it corrects (Pacioli ch. 31; B-9). Until now only document reversals and depreciation/schedule runs were held to it; a plain posted entry could be reversed with an earlier date, leaving the books carrying the opposite of an entry that doesn't exist yet at the reversal's date. The owner's decision D-73 (2026-11-08) makes it one rule for every kind of entry; bookspec BS-ledger-5 states it. - Ledger::check_reversal refuses date < original.date() with one error, ReversalBeforeOriginal (it replaces DocumentReversalBeforeOriginal, which was the document-only wording). Same-day reversals are allowed. - Not on replay (B-28): the check sits behind is_replaying(), so a journal an earlier version wrote, holding a reversal dated before its original, reopens with the same figures. - The store's own date check for depreciation runs and schedule months (InvalidSchedule / AssetEntryIrreversible wording) was the same rule for a narrower case; it is removed so there is one variant for all. Store::check_reverse_entry, books.reverse_entry (preview and post), and the document voids all reach the refusal through check_reversal; the only construction path for a reversal is reverse_entry_steps, which calls it first. Tests: store reversal_dates.rs (plain entry refused before it, nothing journaled, same day and later taken; a journal with an earlier-dated reversal written the old way opens with the same figures); ledger-cli spec_mcp (preview, unconfirmed and confirmed post all refuse alike). Existing tests changed: store assets.rs (three expectations move to the new variant); core spec_asset_basis.rs (it backdated a reversal on purpose to check the trial balance carries -100.00 between the dates; it now replays one, as an old journal would); model.rs (the model oracle's `reverse` requires the date, as the Lean/bookspec rule does); bookspec common.rs (the pinned depreciation example words this refusal "entry_irreversible"); spec_documents.rs (variant rename). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c5fc16cad6
commit
bb5008edb5
14 files changed
+278
-69
No files matched your search
@@ -833,17 +833,13 @@ pub enum LedgerError {
|
||||
#[error("{0}")]
|
||||
DocumentBounds(String),
|
||||
|
||||
/// A reversal of an entry an invoice or bill owns is dated before that
|
||||
/// entry (rules.md B-9, B-38): a correction comes after what it
|
||||
/// corrects.
|
||||
#[error(
|
||||
"entry {entry} belongs to {document} and is dated {original}: its reversal can't be dated before it, on {date}"
|
||||
)]
|
||||
DocumentReversalBeforeOriginal {
|
||||
/// A reversal is dated before the entry it reverses (rules.md B-9,
|
||||
/// D-73): a correction comes after what it corrects, for every kind of
|
||||
/// entry. New commands only; replay never refuses (B-28).
|
||||
#[error("entry {entry} is dated {original}: its reversal can't be dated before it, on {date}")]
|
||||
ReversalBeforeOriginal {
|
||||
/// The entry reversed.
|
||||
entry: u64,
|
||||
/// The document, e.g. "invoice 7".
|
||||
document: String,
|
||||
/// The entry's date.
|
||||
original: chrono::NaiveDate,
|
||||
/// The reversal's date.
|
||||
|
||||
@@ -2235,12 +2235,15 @@ impl Ledger {
|
||||
/// - NEVER recognize one month of a schedule twice
|
||||
/// BECAUSE a reversed month is due again and may have been run
|
||||
/// again (P3 depreciation review R3).
|
||||
/// 6. IF the entry carries a document's lines (an invoice's or a
|
||||
/// bill's own entry, a payment, or a reversal of one) and `date`
|
||||
/// is before the entry's own date:
|
||||
/// - SHOULD refuse (DocumentReversalBeforeOriginal)
|
||||
/// BECAUSE a correction comes after what it corrects (B-9; plan
|
||||
/// P4b.3, "Dating rules").
|
||||
/// 6. IF `date` is before the entry's own date (any kind of entry:
|
||||
/// plain, document, payment, schedule run):
|
||||
/// - SHOULD refuse (ReversalBeforeOriginal)
|
||||
/// - NEVER refuse the same day
|
||||
/// - NEVER refuse on replay
|
||||
/// BECAUSE a correction comes after what it corrects (B-9; owner
|
||||
/// decision D-73, 2026-11-08; plan P4b.3, "Dating rules"). Books
|
||||
/// written before D-73 may hold an earlier-dated reversal and
|
||||
/// reopen as they were (B-28).
|
||||
/// 7. IF reversing it would leave a document it carries owing less
|
||||
/// than nothing or more than its total at some date (a void
|
||||
/// under a payment that stands; re-instating a payment beside
|
||||
@@ -2284,13 +2287,21 @@ impl Ledger {
|
||||
)));
|
||||
}
|
||||
if !crate::parties::is_replaying() {
|
||||
if date < original.date() {
|
||||
return Err(LedgerError::ReversalBeforeOriginal {
|
||||
entry,
|
||||
original: original.date(),
|
||||
date,
|
||||
});
|
||||
}
|
||||
self.check_document_reversal(original, date)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// [`Self::check_reversal`] SCENARIOS 6 and 7: a reversal of an entry
|
||||
/// carrying documents' lines.
|
||||
/// [`Self::check_reversal`] SCENARIO 7: a reversal of an entry
|
||||
/// carrying documents' lines (the date floor, SCENARIO 6, is checked
|
||||
/// before this runs).
|
||||
fn check_document_reversal(
|
||||
&self,
|
||||
original: &JournalEntry,
|
||||
@@ -2307,16 +2318,8 @@ impl Ledger {
|
||||
}
|
||||
}
|
||||
}
|
||||
let Some(&(first, _)) = moves.first() else {
|
||||
if moves.is_empty() {
|
||||
return Ok(());
|
||||
};
|
||||
if date < original.date() {
|
||||
return Err(LedgerError::DocumentReversalBeforeOriginal {
|
||||
entry: original.id(),
|
||||
document: first.to_string(),
|
||||
original: original.date(),
|
||||
date,
|
||||
});
|
||||
}
|
||||
for (doc, by) in moves {
|
||||
self.check_item_bounds(doc, &[(date, by)])?;
|
||||
|
||||
@@ -133,7 +133,12 @@ pub(crate) fn refusal(e: &LedgerError) -> String {
|
||||
LedgerError::DuplicateAccountCode(_) => "code_taken",
|
||||
LedgerError::TaxCategoryRequired { .. } => "tax_category_required",
|
||||
LedgerError::DepreciationPastDisposal(_) => "depreciation_past_disposal",
|
||||
LedgerError::AssetEntryIrreversible(..) => "entry_irreversible",
|
||||
// A reversal dated before its original (D-73) is the examples'
|
||||
// "entry_irreversible" too: bookspec's depreciation example words
|
||||
// the refusal that way, whichever engine check makes it.
|
||||
LedgerError::AssetEntryIrreversible(..) | LedgerError::ReversalBeforeOriginal { .. } => {
|
||||
"entry_irreversible"
|
||||
}
|
||||
LedgerError::AssetAlreadyDisposed(_) => "already_disposed",
|
||||
LedgerError::InvalidDepreciationPeriod(_) => "invalid_depreciation_period",
|
||||
LedgerError::InvalidSchedule(_) => "invalid_schedule",
|
||||
|
||||
@@ -218,7 +218,9 @@ impl Model {
|
||||
return false;
|
||||
};
|
||||
let reversed_already = self.posted.iter().any(|e| e.reversal_of == Some(entry));
|
||||
if reversed_already || orig.closes || self.closed_on(date) {
|
||||
// D-73 (bookspec BS-ledger-5): a correction is dated on or after
|
||||
// what it corrects.
|
||||
if reversed_already || orig.closes || self.closed_on(date) || date < orig.date {
|
||||
return false;
|
||||
}
|
||||
let lines = orig
|
||||
|
||||
@@ -960,11 +960,19 @@ fn a_reinstated_run_counts_and_is_live() {
|
||||
}
|
||||
|
||||
/// A reversal dated before its run: between the two dates the books carry
|
||||
/// −100.00, as the trial balance does (review R2).
|
||||
/// −100.00, as the trial balance does (review R2). No command can write one
|
||||
/// any more (D-73: a reversal is dated on or after its original, so
|
||||
/// `reverse_entry` refuses it), but a journal an earlier version wrote holds
|
||||
/// them and replays as it was (B-28): this test replays one.
|
||||
#[test]
|
||||
fn a_reversal_dated_before_its_run_matches_the_trial_balance() {
|
||||
let (mut l, a, id, e) = one_run();
|
||||
l.reverse_entry(e, date(2026, 1, 15), "early").unwrap();
|
||||
assert!(matches!(
|
||||
l.reverse_entry(e, date(2026, 1, 15), "early"),
|
||||
Err(LedgerError::ReversalBeforeOriginal { .. })
|
||||
));
|
||||
l.replay_reverse_entry(e, date(2026, 1, 15), "early")
|
||||
.unwrap();
|
||||
let on = date(2026, 1, 20);
|
||||
assert_eq!(l.accumulated_posted(id, on).unwrap(), d("-100.00"));
|
||||
let tb = ledger_core::trial_balance_as_of(&l, on);
|
||||
|
||||
@@ -510,11 +510,11 @@ fn reversal_before_original_refused_for_documents() {
|
||||
.unwrap();
|
||||
assert!(matches!(
|
||||
b.l.reverse_entry(pay, d(2026, 1, 19), "early"),
|
||||
Err(LedgerError::DocumentReversalBeforeOriginal { .. })
|
||||
Err(LedgerError::ReversalBeforeOriginal { .. })
|
||||
));
|
||||
assert!(matches!(
|
||||
b.l.void_document(DocRef::Invoice(inv), d(2026, 1, 9), "early"),
|
||||
Err(LedgerError::DocumentReversalBeforeOriginal { .. })
|
||||
Err(LedgerError::ReversalBeforeOriginal { .. })
|
||||
));
|
||||
// The same day is fine.
|
||||
b.l.reverse_entry(pay, d(2026, 1, 20), "same day").unwrap();
|
||||
|
||||
Reference in new issue
Block a user