fix(core): custom terms are due on the date they give
BS-arap-3 statement 1: custom terms ("Due 2026-11-15") are due on the date
they give. Terms::due_date returned the document's own date for them, so
the MCP path (create_invoice / create_bill, from the terms given or a
party's default) stored an invoice dated 2026-11-01 on "Due 2026-11-15" as
due 2026-11-01: a document that read past due two weeks early in the aging.
Terms now keeps the agreed date (a new `agreed` field, Some exactly for
custom terms, with Terms::agreed_date), and due_date returns it, refusing
an agreed date before the document's like any due date before its document.
TermsKind keeps its shape, so stored kinds read as before. Terms read back
from storage fill `agreed` from their canonical text when it is missing, and
a custom terms with no readable date is refused rather than invented.
Stored books: the journal records each document's due_date itself
(invoice.created / bill.created payloads) and replay reads it, never
recomputing it from the terms, so a document already written keeps the due
date it recorded (posted is forever). New journal entries with custom terms
carry the extra "agreed" key; older ones read as before. No golden under
crates/*/tests/golden holds custom terms, so none changes; the live books
have no invoices or bills.
Through the tool, an invoice dated 2026-11-01 on "Due 2026-11-15": due
2026-11-01 before, 2026-11-15 after. Tests: ledger-core
custom_terms_are_due_on_the_date_they_give (Terms, the stored document, the
storage round trip incl. the older form, and the refusals) and the same
through books.create_invoice / create_bill / a party's default terms and the
refusal of a document dated after the agreed day (ledger-cli documents.rs).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
23e52de969
commit
71a4e7e59d
3 files changed
+192
-14
No files matched your search
@@ -1637,6 +1637,60 @@ fn doc_line(
|
||||
}
|
||||
}
|
||||
|
||||
/// BS-arap-3 statement 1: custom terms are due on the date they give, not
|
||||
/// on the document's own date. A document dated 2026-11-01 on "Due
|
||||
/// 2026-11-15" is due on the 15th (through Terms::due_date and through the
|
||||
/// stored document); an agreed date before the document's is refused like
|
||||
/// any due date before its document; terms read back from storage (even
|
||||
/// written before they carried the date) keep the date.
|
||||
#[test]
|
||||
fn custom_terms_are_due_on_the_date_they_give() {
|
||||
let on = d(2026, 11, 1);
|
||||
let agreed = d(2026, 11, 15);
|
||||
let terms = Terms::parse("Due 2026-11-15").unwrap();
|
||||
assert_eq!(terms.agreed_date(), Some(agreed));
|
||||
assert_eq!(terms.due_date(on).unwrap(), agreed);
|
||||
// The same, whichever day the document is dated (up to the 15th).
|
||||
assert_eq!(terms.due_date(d(2026, 11, 15)).unwrap(), agreed);
|
||||
assert_eq!(terms.due_date(d(2026, 1, 3)).unwrap(), agreed);
|
||||
// Dated after the agreed day, the terms contradict the document.
|
||||
assert!(terms.due_date(d(2026, 11, 16)).is_err());
|
||||
// Other kinds give no agreed date.
|
||||
assert_eq!(Terms::parse("Net 30").unwrap().agreed_date(), None);
|
||||
|
||||
// Through the engine: an invoice and a bill on those terms.
|
||||
let mut b = books();
|
||||
let invoice =
|
||||
b.l.create_invoice_with(
|
||||
b.customer,
|
||||
on,
|
||||
terms.due_date(on).unwrap(),
|
||||
line("Work", 10_000),
|
||||
InvoiceFields {
|
||||
terms: Some(terms.clone()),
|
||||
..InvoiceFields::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let stored = b.l.get_invoice(invoice).unwrap();
|
||||
assert_eq!(stored.due_date, agreed);
|
||||
assert_eq!(stored.terms.as_ref().unwrap().describe(), "Due 2026-11-15");
|
||||
|
||||
// Storage round-trips the date; an older stored form, which had the
|
||||
// text only, reads back with the date from its text.
|
||||
let json = serde_json::to_value(&terms).unwrap();
|
||||
assert_eq!(json["agreed"], "2026-11-15");
|
||||
assert_eq!(serde_json::from_value::<Terms>(json).unwrap(), terms);
|
||||
let old = serde_json::json!({"kind": "custom", "text": "Due 2026-11-15"});
|
||||
assert_eq!(serde_json::from_value::<Terms>(old).unwrap(), terms);
|
||||
// Custom terms with no readable date are refused, not invented.
|
||||
let none = serde_json::json!({"kind": "custom", "text": "Due"});
|
||||
assert!(serde_json::from_value::<Terms>(none).is_err());
|
||||
// A date on any other kind is refused.
|
||||
let stray = serde_json::json!({"kind": {"net": 30}, "text": "Net 30", "agreed": "2026-11-15"});
|
||||
assert!(serde_json::from_value::<Terms>(stray).is_err());
|
||||
}
|
||||
|
||||
/// BS-arap-3 SCENARIOS: Net 30, Net 15 EOM, a leap day, due on receipt, a
|
||||
/// custom date, and the 9999-12 bound refused instead of panicking.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user