refactor(core): even monthly parts in the kernel

An equal-months schedule books exactly its total: every month but the last
is the quotient rounded to the currency's step, the last takes what the
others leave (BS-prepaid-1, BS-prepaid-2). That arithmetic moves from
amortization_plan into kernel::even_parts, built on round_step and
plug_last, with its spec block (what rounds: Decimal's division, a K-U10
item, is stated, not hidden).

amortization_plan keeps the cent rule exactly. The exhaustive test
(300,000+ plans, and a proptest to 10^25) compares it with a verbatim copy
of the old function, values and printed text: a posted month's replay
compares journaled lines as text, and round_step prints 0.250 and a zero
differently from round_places, so present_as_rounded restores the old
scale (it found 1 over 360 printing 1.00 against 1).

The rule a schedule was written under is now its own flag
(ScheduleRounding, AmortizationSchedule.rounding, unsaid for the cent
rule), with Ledger::register_schedule_under and the replay twin; the plain
register_schedule stays the cent rule, so every existing test and golden
is unchanged. amortization_plan_under rounds to the currency's minor unit:
100,000 yen over 3 months is 33,333, 33,333, 33,334 and 1.000 dinar is
0.333, 0.333, 0.334 (hand-checked in the tests).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-09 03:33:15 -07:00
1 parent 3d026b6473
commit 768ed7d92b
5 files changed
+640 -36

No files matched your search

+28 -2
View File
@@ -441,8 +441,22 @@ impl Ledger {
/// Register an amortization schedule. Returns its id.
pub fn register_schedule(&mut self, new: NewSchedule) -> Result<ScheduleId, LedgerError> {
schedules::validate_schedule(&new, &self.chart)?;
self.replay_register_schedule(new)
self.register_schedule_under(new, schedules::ScheduleRounding::Cent)
}
/// [`Self::register_schedule`] for a monthly_even schedule written
/// under `rounding` (D-72, BS-prepaid-4): the store registers new
/// schedules under the currency's minor unit once its books declare
/// the journal feature `schedule_minor_unit`. The plain
/// `register_schedule` is the cent rule, the rule of every schedule
/// before it.
pub fn register_schedule_under(
&mut self,
new: NewSchedule,
rounding: schedules::ScheduleRounding,
) -> Result<ScheduleId, LedgerError> {
schedules::validate_schedule_under(&new, rounding, &self.chart)?;
self.replay_register_schedule_under(new, rounding)
}
/// [`Self::register_schedule`] without the total's range and minor-unit
@@ -452,6 +466,17 @@ impl Ledger {
pub fn replay_register_schedule(
&mut self,
new: NewSchedule,
) -> Result<ScheduleId, LedgerError> {
self.replay_register_schedule_under(new, schedules::ScheduleRounding::Cent)
}
/// [`Self::replay_register_schedule`] for a schedule the journal says
/// was written under `rounding` (`schedule.registered`'s `rounding`,
/// absent for every schedule before D-72).
pub fn replay_register_schedule_under(
&mut self,
new: NewSchedule,
rounding: schedules::ScheduleRounding,
) -> Result<ScheduleId, LedgerError> {
schedules::validate_schedule_shape(&new, &self.chart)?;
let id = self.next_schedule_id;
@@ -469,6 +494,7 @@ impl Ledger {
pl_account: new.pl_account,
posted: Default::default(),
convention: new.convention,
rounding,
postings: Default::default(),
},
);