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
@@ -496,6 +496,68 @@ fn terms_fall_back_in_order() {
|
||||
assert_eq!(bill["terms"], "Net 10", "{bill}");
|
||||
}
|
||||
|
||||
/// BS-arap-3 statement 1 through the tool: terms "Due 2026-11-15", from
|
||||
/// the caller or from a party's defaults, make an invoice or a bill due on
|
||||
/// the 15th, not on the document's own date; a document dated after the
|
||||
/// agreed day is refused and writes nothing.
|
||||
#[test]
|
||||
fn custom_terms_are_due_on_the_date_they_give() {
|
||||
let books = Books::new("documents-custom-terms");
|
||||
let mut m = Mcp::spawn(&books);
|
||||
let harbor = m.customer("Harbor Coffee");
|
||||
let line = json!([{"description": "Work", "amount": "100.00"}]);
|
||||
let invoice = |date: &str, extra: Value| {
|
||||
let mut args = json!({"customer": harbor.to_string(), "date": date,
|
||||
"lines": line, "confirmed": true});
|
||||
for (k, v) in extra.as_object().unwrap() {
|
||||
args[k] = v.clone();
|
||||
}
|
||||
args
|
||||
};
|
||||
|
||||
// Terms given.
|
||||
let given = m.call(
|
||||
"books.create_invoice",
|
||||
invoice("2026-11-01", json!({"terms": "Due 2026-11-15"})),
|
||||
);
|
||||
assert_eq!(given["due_date"], "2026-11-15", "{given}");
|
||||
assert_eq!(given["terms"], "Due 2026-11-15", "{given}");
|
||||
|
||||
// The customer's default terms.
|
||||
let party = m.party_of("Harbor Coffee");
|
||||
m.call(
|
||||
"books.update_party",
|
||||
json!({"party": party.to_string(), "default_terms_as_customer": "Due 2026-12-01"}),
|
||||
);
|
||||
let by_party = m.call("books.create_invoice", invoice("2026-11-02", json!({})));
|
||||
assert_eq!(by_party["due_date"], "2026-12-01", "{by_party}");
|
||||
assert_eq!(by_party["terms"], "Due 2026-12-01", "{by_party}");
|
||||
|
||||
// A bill, terms given.
|
||||
let coast = m.vendor("Coastline Hosting");
|
||||
m.expense("6500", "Hosting");
|
||||
let file = m.upload(b"coastline", "coastline.pdf", "receipt");
|
||||
let bill = m.call(
|
||||
"books.create_bill",
|
||||
json!({"vendor": coast.to_string(), "date": "2026-11-01", "vendor_ref": "CH-1",
|
||||
"terms": "Due 2026-11-20",
|
||||
"lines": [{"description": "Hosting", "amount": "100.00", "account": "6500"}],
|
||||
"upload_id": file, "confirmed": true}),
|
||||
);
|
||||
assert_eq!(bill["due_date"], "2026-11-20", "{bill}");
|
||||
|
||||
// Dated after the agreed day: refused.
|
||||
let before = books.journal();
|
||||
let late = m
|
||||
.try_call(
|
||||
"books.create_invoice",
|
||||
invoice("2026-11-16", json!({"terms": "Due 2026-11-15"})),
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(late.contains("due"), "{late}");
|
||||
assert_eq!(books.journal(), before, "a refusal writes nothing");
|
||||
}
|
||||
|
||||
/// A customer given by id and by name is the same record (the old
|
||||
/// `party_id` spelling is gone, P5); a number that is one record's id and
|
||||
/// another's name is refused, never guessed.
|
||||
|
||||
Reference in new issue
Block a user