feat(core): new schedules round to the currency's minor unit (D-72)
The owner's decision D-72: a new monthly-even schedule rounds each month
to its currency's minor unit (yen whole, dinar to the thousandth), where
the cent rule gave two places to every currency; a schedule written
earlier replays under the cent rule, so nothing journaled moves
(BS-prepaid-4, BS-entry-8).
The rule is the schedule's own flag, journaled on its schedule.registered
line ("rounding": "minor_unit", absent for the cent rule), not the
sequence position of the journal feature: a flag survives a snapshot, a
copy and a replay that starts mid-journal. The new feature
schedule_minor_unit gates it (KNOWN_FEATURES, ensure_feature in
Store::register_schedule): books that haven't declared it keep the cent
rule and write nothing new, and an older reader stops at the declaration
as newer books (BS-format-1); an unknown rounding is newer, never
corrupt. A dollar schedule plans the same under both rules.
Tests: a dinar schedule written before the feature replays to the same
plan and the same posted line after the owner's upgrade, while one written
after plans 0.333, 0.333, 0.334. WB-prepaid-2 becomes yes.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
768ed7d92b
commit
09308d5528
5 files changed
+449
-35
No files matched your search
@@ -55,6 +55,7 @@ pub const KNOWN_FEATURES: &[&str] = &[
|
||||
VENDOR_1099_DECISIONS,
|
||||
MONTH_REPOSTING,
|
||||
SCHEDULE_CONVENTION,
|
||||
SCHEDULE_MINOR_UNIT,
|
||||
ASSET_BASIS,
|
||||
TAX_CATEGORIES_GAIN_EXEMPT,
|
||||
DISPOSAL_BEFORE_SERVICE,
|
||||
@@ -167,6 +168,18 @@ pub const MONTH_REPOSTING: &str = "month_reposting";
|
||||
/// months (P3 defect 9, plan/served.md D-11).
|
||||
pub const SCHEDULE_CONVENTION: &str = "schedule_convention";
|
||||
|
||||
/// Feature: `schedule.registered` carries `rounding` (`minor_unit`): a
|
||||
/// monthly_even schedule written after this feature rounds each month to
|
||||
/// its currency's minor unit (a yen schedule in whole yen, a dinar one in
|
||||
/// thousandths); one with no `rounding` is the cent rule, two places for
|
||||
/// every currency, as every schedule before it is (BS-prepaid-4, D-72).
|
||||
/// An older version would read the schedule under the cent rule and
|
||||
/// refuse its first month as a posting that doesn't match its plan, or,
|
||||
/// replaying a month it can derive, post ¥33,333.33 where the books say
|
||||
/// ¥33,333. The flag rides on the schedule, never on where the feature
|
||||
/// was declared: a schedule written before it replays as it was posted.
|
||||
pub const SCHEDULE_MINOR_UNIT: &str = "schedule_minor_unit";
|
||||
|
||||
/// Feature: `asset.basis_set`, a new event type: an asset's basis facts
|
||||
/// with the value at contribution and the §704(c) method (P3, defect 13).
|
||||
pub const ASSET_BASIS: &str = "asset_basis";
|
||||
@@ -1088,6 +1101,43 @@ mod tests {
|
||||
assert_eq!(up.adds(), vec!["the journal feature audits"]);
|
||||
}
|
||||
|
||||
/// The version before K-U5 (every feature but `schedule_minor_unit`)
|
||||
/// stops at the declaration as newer, so it never reads a ¥33,333
|
||||
/// month under the cent rule (and refuses, or posts ¥33,333.33 where
|
||||
/// the books say ¥33,333); and a `schedule.registered` naming a
|
||||
/// rounding this version doesn't know is newer, never corrupt
|
||||
/// (BS-prepaid-4, D-72, BS-format-1). Books that declared every
|
||||
/// earlier feature still wait for it.
|
||||
#[test]
|
||||
fn schedule_minor_unit_is_its_own_feature() {
|
||||
assert!(KNOWN_FEATURES.contains(&SCHEDULE_MINOR_UNIT));
|
||||
let before: Vec<&str> = KNOWN_FEATURES
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|f| *f != SCHEDULE_MINOR_UNIT)
|
||||
.collect();
|
||||
let l = line(
|
||||
FORMAT_SET,
|
||||
json!({"format": 2, "features": [SCHEDULE_CONVENTION, SCHEDULE_MINOR_UNIT]}),
|
||||
);
|
||||
match declared_knowing(&l, &before) {
|
||||
Err(StoreError::NewerJournal { need, .. }) => {
|
||||
assert_eq!(need, "journal features schedule_minor_unit")
|
||||
}
|
||||
other => panic!("expected NewerJournal, got {other:?}"),
|
||||
}
|
||||
let older = JournalFormat {
|
||||
format: 2,
|
||||
features: before.iter().map(|f| f.to_string()).collect(),
|
||||
};
|
||||
assert!(matches!(
|
||||
older.require_feature(SCHEDULE_MINOR_UNIT),
|
||||
Err(StoreError::FormatUpgradePending { .. })
|
||||
));
|
||||
let up = older.upgrade().expect("waits for it");
|
||||
assert_eq!(up.adds(), vec!["the journal feature schedule_minor_unit"]);
|
||||
}
|
||||
|
||||
/// This version's format: the newest it reads, every feature it
|
||||
/// knows, each once, sorted (what new books and an upgrade declare).
|
||||
#[test]
|
||||
|
||||
@@ -4961,31 +4961,68 @@ impl Store {
|
||||
/// nothing, in books that haven't declared it): an older version reads
|
||||
/// every schedule as equal months, and would refuse a daily one's
|
||||
/// postings as wrong (rather than as newer).
|
||||
///
|
||||
/// A monthly_even schedule's months round to its currency's minor
|
||||
/// unit in books that declared `schedule_minor_unit` (D-72,
|
||||
/// BS-prepaid-4), which the journal records as `"rounding":
|
||||
/// "minor_unit"` on the schedule itself; in books that haven't it is
|
||||
/// the cent rule, as before, and nothing new is written.
|
||||
///
|
||||
/// ```text
|
||||
/// GOAL: a new monthly-even schedule's months are whole units of its
|
||||
/// currency, and what an earlier version journaled never moves.
|
||||
/// SOURCE: BS-prepaid-4, BS-format-1, BS-entry-8; D-72 (owner,
|
||||
/// 2026-11-08).
|
||||
/// INVARIANTS:
|
||||
/// 1. The rule is a fact of the books when the schedule is written:
|
||||
/// minor unit exactly when the books declared the feature and the
|
||||
/// convention is monthly_even, and the flag is journaled with the
|
||||
/// schedule, so replay never consults the feature's position.
|
||||
/// 2. A schedule journaling the flag requires the feature
|
||||
/// (`ensure_feature`): an older version stops at the declaration,
|
||||
/// never at the schedule.
|
||||
/// 3. Books without the feature write exactly the line they always
|
||||
/// did: no `rounding` key.
|
||||
/// SCENARIOS:
|
||||
/// 1. IF the books declared the feature and ¥100,000 is spread over
|
||||
/// 3 equal months: SHOULD plan ¥33,333, ¥33,333, ¥33,334.
|
||||
/// 2. IF they haven't: SHOULD refuse it as a month finer than a yen
|
||||
/// (¥33,333.33), as before, until the owner's upgrade.
|
||||
/// ```
|
||||
pub fn register_schedule(
|
||||
&mut self,
|
||||
new: ledger_core::NewSchedule,
|
||||
) -> Result<ledger_core::ScheduleId, StoreError> {
|
||||
self.rehearse(|l| l.register_schedule(new.clone()))?;
|
||||
ledger_core::schedules::validate_schedule(&new, self.ledger.chart())?;
|
||||
let rounding = if new.convention == ledger_core::ScheduleConvention::MonthlyEven
|
||||
&& self.format.has_feature(crate::format::SCHEDULE_MINOR_UNIT)
|
||||
{
|
||||
ledger_core::ScheduleRounding::MinorUnit
|
||||
} else {
|
||||
ledger_core::ScheduleRounding::Cent
|
||||
};
|
||||
self.rehearse(|l| l.register_schedule_under(new.clone(), rounding))?;
|
||||
ledger_core::schedules::validate_schedule_under(&new, rounding, self.ledger.chart())?;
|
||||
let id = self.ledger.next_schedule_id();
|
||||
if new.convention == ledger_core::ScheduleConvention::Daily {
|
||||
self.ensure_feature(crate::format::SCHEDULE_CONVENTION)?;
|
||||
}
|
||||
let line = self.append_journal(
|
||||
"schedule.registered",
|
||||
json!({
|
||||
"id": id,
|
||||
"description": new.description,
|
||||
"kind": serde_json::to_value(new.kind)?,
|
||||
"total": {"amount": new.total.amount.to_string(), "currency": new.total.currency.code()},
|
||||
"start": new.start.to_string(),
|
||||
"months": new.months,
|
||||
"balance_account": new.balance_account,
|
||||
"pl_account": new.pl_account,
|
||||
"convention": new.convention.key(),
|
||||
}),
|
||||
)?;
|
||||
let got = self.ledger.register_schedule(new)?;
|
||||
let mut payload = json!({
|
||||
"id": id,
|
||||
"description": new.description,
|
||||
"kind": serde_json::to_value(new.kind)?,
|
||||
"total": {"amount": new.total.amount.to_string(), "currency": new.total.currency.code()},
|
||||
"start": new.start.to_string(),
|
||||
"months": new.months,
|
||||
"balance_account": new.balance_account,
|
||||
"pl_account": new.pl_account,
|
||||
"convention": new.convention.key(),
|
||||
});
|
||||
if !rounding.is_cent() {
|
||||
self.ensure_feature(crate::format::SCHEDULE_MINOR_UNIT)?;
|
||||
payload["rounding"] = json!(rounding.key());
|
||||
}
|
||||
let line = self.append_journal("schedule.registered", payload)?;
|
||||
let got = self.ledger.register_schedule_under(new, rounding)?;
|
||||
debug_assert_eq!(got, id);
|
||||
apply_line_to_sqlite(&self.conn, &line)?;
|
||||
Ok(id)
|
||||
@@ -7414,16 +7451,36 @@ fn replay_line(
|
||||
));
|
||||
}
|
||||
};
|
||||
let got = ledger.replay_register_schedule(ledger_core::NewSchedule {
|
||||
description: get_str(p, "description")?.to_string(),
|
||||
kind: serde_json::from_value(p["kind"].clone())?,
|
||||
total: parse_money(p, "total")?,
|
||||
start: parse_date(get_str(p, "start")?)?,
|
||||
months: get_u64(p, "months")? as u32,
|
||||
balance_account: get_u64(p, "balance_account")?,
|
||||
pl_account: get_u64(p, "pl_account")?,
|
||||
convention,
|
||||
})?;
|
||||
// No rounding (every schedule before D-72): the cent rule, as
|
||||
// it was posted. The rule is the schedule's own, never the
|
||||
// books' later format. A rule this version doesn't know is
|
||||
// newer, never corrupt (BS-format-1).
|
||||
let rounding = match p.get("rounding") {
|
||||
None | Some(Value::Null) => ledger_core::ScheduleRounding::Cent,
|
||||
Some(Value::String(r)) => {
|
||||
ledger_core::ScheduleRounding::parse(r).ok_or_else(|| {
|
||||
crate::format::newer_content(line, format!("the schedule rounding {r}"))
|
||||
})?
|
||||
}
|
||||
Some(_) => {
|
||||
return Err(StoreError::JournalCorrupt(
|
||||
"schedule.registered rounding".into(),
|
||||
));
|
||||
}
|
||||
};
|
||||
let got = ledger.replay_register_schedule_under(
|
||||
ledger_core::NewSchedule {
|
||||
description: get_str(p, "description")?.to_string(),
|
||||
kind: serde_json::from_value(p["kind"].clone())?,
|
||||
total: parse_money(p, "total")?,
|
||||
start: parse_date(get_str(p, "start")?)?,
|
||||
months: get_u64(p, "months")? as u32,
|
||||
balance_account: get_u64(p, "balance_account")?,
|
||||
pl_account: get_u64(p, "pl_account")?,
|
||||
convention,
|
||||
},
|
||||
rounding,
|
||||
)?;
|
||||
if got != want {
|
||||
return Err(StoreError::ReplayMismatch(format!(
|
||||
"schedule id: journal says {want}, replay got {got}"
|
||||
|
||||
Reference in new issue
Block a user