refactor(core): percentages and ratios in the kernel

A project's contribution margin is its net as a percent of revenue: the
exact ratio times 100, rounded once to the place the page prints (one
place, half to even), and none when there is no revenue to divide by. The
same computation was hand-written in reports; it is now
kernel::percent_of, division first as the call site always did, so the
figures, the scale and the None on a non-positive revenue do not move.

The government's part of an operating payment is an amount split a : b:
the government side rounded once to the currency's places, half to even,
the rest exact, the two summing to the payment (BS-statement-3 statement
8). U3 had already routed the share through kernel::share_of; U6 names the
whole split as kernel::split_ratio, which builds on share_of and
round_step, and cashflow::split_part calls it. The old party-shares
refusal on the shares' own sum is kept.

The percentage's rules come from BS-statement-5 statement 2 (a project's
net is revenue less expenses) and wanderbooks' WB-statement-7 (the margin
as a percent of revenue), and BS-entry-3 (round only where a rule says
to); the split's from BS-statement-3 statement 8, BS-partner-1 and
BS-entry-3.

Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
This commit is contained in:
swaitsandDeepSeek V4.1 Flash committed 2026-11-09 09:14:19 -07:00
1 parent 7a5cb6873a
commit a70099daae
3 files changed
+136 -21

No files matched your search

+14 -9
View File
@@ -1716,15 +1716,20 @@ fn split_part(
(ours, theirs): (Decimal, Decimal),
places: u32,
) -> Result<(Decimal, Decimal), LedgerError> {
let whole = add(ours, theirs, "an entry's party shares")?;
let part = crate::kernel::share_of(amount, ours, whole)
.ok_or_else(|| too_large("a payment's government part"))?;
let part = crate::kernel::round_places(part, places, crate::kernel::Rounding::HalfToEven)
.ok_or_else(|| too_large("a payment's government part"))?;
let rest = amount
.checked_sub(part)
.ok_or_else(|| too_large("a payment's government part"))?;
Ok((part, rest))
// The shares' own sum fails with the party-shares message, as it did
// before the split moved into the kernel; the rest of the split's
// arithmetic keeps the government-part message.
add(ours, theirs, "an entry's party shares")?;
let step = Decimal::try_from_i128_with_scale(1, places)
.map_err(|_| too_large("a payment's government part"))?;
crate::kernel::split_ratio(
amount,
ours,
theirs,
step,
crate::kernel::Rounding::HalfToEven,
)
.ok_or_else(|| too_large("a payment's government part"))
}
/// The codes of the accounts in some parts, sorted, once each.