Files
wanderbooks/crates/ledger-core/src/analysis.rs
T
swaitsandClaude 32725fb669 fix(core): Decimal arithmetic in ledger-core goes through the kernel (K-U11a)
Bar item 1: money arithmetic lives in kernel/ and nowhere else.
scripts/raw-decimal-arith.sh listed 210 places in ledger-core that did
not: raw +, -, * and unary minus (which panic past Decimal's range), and
checked_add / checked_sub / checked_mul / checked_div calls that
resolved to rust_decimal's inherent methods (which round silently past
28 digits) and not to the kernel's Amount. 199 of them now call the
kernel: Amount's methods by their full path, total / total_of for the
hand-rolled sums, normal_balance, and money.rs's sign and in-range
helpers.

The rewrite was done by a script from the compiler's own line and
column for each site, so no site was copied by hand; the few that
needed a decision are explicit:
- a balance sheet that cannot add liabilities and equity is not
  balanced (it panicked);
- a template's shares, a party's percents, a reconciliation's book and
  cleared balances and a reversal's document movements refuse with
  TooLargeToCompute or their own message where they panicked;
- tax_capital's split refuses where a product by the sign could not be
  taken, and collects its remainders as Options.
No figure moves: every site computes what it computed, by the same
operations in the same order.

Left, each for a named reason (the detector prints these 11):
money.rs 139 and 150 and periods.rs 149 and 202 (K-U10a rewrites them),
audit.rs 355 and 511 (V-D3 is in them), ledger.rs 4048 (a share times
the closed total: checked_mul_rounded, with K-U10a), and four rescale
calls (money.rs 302, reports.rs 637, schedules.rs 370 and 377) that
need a kernel function of their own.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-11-09 17:22:17 -07:00

300 lines
9.9 KiB
Rust

//! Time series over the books, for dashboards and interval-column reports.
//!
//! Statements answer "what was true for this range"; these answer "how did
//! it change": a range split into months, quarters, or years; balances of a
//! set of accounts on a series of dates; profit and loss per interval. They
//! are built from the same posted entries and statements as everything
//! else, so a number on a chart always ties to a number on a statement.
use crate::money::{add_in_range, on_side, sub_in_range};
use crate::reports::profit_and_loss_for_period;
use crate::{AccountId, AccountType, Ledger, LedgerError, Side};
use chrono::{Datelike, Days, Months, NaiveDate};
use rust_decimal::Decimal;
use std::collections::HashSet;
/// How to split a date range into columns.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Interval {
/// Calendar months.
Month,
/// Calendar quarters.
Quarter,
/// Calendar years.
Year,
}
impl Interval {
fn months(self) -> u32 {
match self {
Interval::Month => 1,
Interval::Quarter => 3,
Interval::Year => 12,
}
}
}
/// The first day of the calendar interval containing `d`.
pub fn interval_start(d: NaiveDate, interval: Interval) -> NaiveDate {
let month0 = (d.month0() / interval.months()) * interval.months();
// The first of a month of `d`'s own year: every year chrono holds has
// all twelve.
NaiveDate::from_ymd_opt(d.year(), month0 + 1, 1).expect("valid")
}
/// `[start, end]` split on calendar interval boundaries, first and last
/// pieces clipped to the range. Empty when `end < start`. Never panics:
/// an interval that would end past chrono's last day ends at `end`
/// (BS-entry-4: books an earlier version wrote may hold such dates).
pub fn intervals(
start: NaiveDate,
end: NaiveDate,
interval: Interval,
) -> Vec<(NaiveDate, NaiveDate)> {
let mut out = Vec::new();
let mut from = interval_start(start, interval);
while from <= end {
let Some(next) = from.checked_add_months(Months::new(interval.months())) else {
// The interval holding chrono's last day: nothing after it.
out.push((from.max(start), end));
break;
};
let to = (next - Days::new(1)).min(end);
out.push((from.max(start), to));
from = next;
}
out
}
/// The combined normal-side balance of `accounts` at the end of each date
/// in `dates` (which must be ascending). Assets and expenses count debits
/// as positive; liabilities, equity, and revenue count credits. Mixing
/// types is allowed but rarely meaningful.
pub fn balance_series(
ledger: &Ledger,
accounts: &[AccountId],
dates: &[NaiveDate],
) -> Vec<Decimal> {
let set: HashSet<AccountId> = accounts.iter().copied().collect();
let mut postings: Vec<(NaiveDate, Decimal)> = Vec::new();
for entry in ledger.posted_entries() {
for line in entry.lines() {
if !set.contains(&line.account) {
continue;
}
let Some(account) = ledger.chart().get(line.account) else {
continue;
};
let signed = on_side(
account.account_type.normal_side(),
line.side,
line.amount.amount,
);
postings.push((entry.date(), signed));
}
}
postings.sort_by_key(|(d, _)| *d);
let mut out = Vec::with_capacity(dates.len());
let mut running = Decimal::ZERO;
let mut i = 0;
for date in dates {
while i < postings.len() && postings[i].0 <= *date {
running = add_in_range(running, postings[i].1);
i += 1;
}
out.push(running);
}
out
}
/// Every date `accounts` moved, with the combined balance at the end of
/// that day: the exact shape of a balance-over-time chart.
pub fn balance_history(ledger: &Ledger, accounts: &[AccountId]) -> Vec<(NaiveDate, Decimal)> {
let set: HashSet<AccountId> = accounts.iter().copied().collect();
let mut dates: Vec<NaiveDate> = ledger
.posted_entries()
.filter(|e| e.lines().iter().any(|l| set.contains(&l.account)))
.map(|e| e.date())
.collect();
dates.sort();
dates.dedup();
let balances = balance_series(ledger, accounts, &dates);
dates.into_iter().zip(balances).collect()
}
/// One interval's profit and loss.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IntervalPnl {
/// First day.
pub start: NaiveDate,
/// Last day.
pub end: NaiveDate,
/// Total revenue.
pub revenue: Decimal,
/// Total expenses.
pub expenses: Decimal,
}
impl IntervalPnl {
/// Revenue minus expenses.
pub fn net(&self) -> Decimal {
sub_in_range(self.revenue, self.expenses)
}
}
/// Profit and loss per interval across `[start, end]` (closing entries
/// excluded, as on every period statement).
pub fn pnl_by_interval(
ledger: &Ledger,
start: NaiveDate,
end: NaiveDate,
interval: Interval,
) -> Result<Vec<IntervalPnl>, LedgerError> {
pnl_by_interval_for(ledger, start, end, interval, None)
}
/// [`pnl_by_interval`] for one class (project): only the lines tagged
/// with it count (`None`: every line).
pub fn pnl_by_interval_for(
ledger: &Ledger,
start: NaiveDate,
end: NaiveDate,
interval: Interval,
class: Option<crate::ClassId>,
) -> Result<Vec<IntervalPnl>, LedgerError> {
intervals(start, end, interval)
.into_iter()
.map(|(s, e)| {
let pl = profit_and_loss_for_period(ledger, s, e, class)?;
Ok(IntervalPnl {
start: s,
end: e,
revenue: pl.total_revenue,
expenses: pl.total_expenses,
})
})
.collect()
}
/// Money in and out of `accounts` per interval: (inflows, outflows), each
/// positive. Each entry counts by its net debit (in) or credit (out) to the
/// accounts together: for cash, deposits and payments; a transfer between
/// two of them (checking to savings) is neither; with a credit card in the
/// set, a charge is money out and paying the card moves nothing.
pub fn flows_by_interval(
ledger: &Ledger,
accounts: &[AccountId],
start: NaiveDate,
end: NaiveDate,
interval: Interval,
) -> Vec<(NaiveDate, NaiveDate, Decimal, Decimal)> {
let set: HashSet<AccountId> = accounts.iter().copied().collect();
let mut out: Vec<(NaiveDate, NaiveDate, Decimal, Decimal)> = intervals(start, end, interval)
.into_iter()
.map(|(s, e)| (s, e, Decimal::ZERO, Decimal::ZERO))
.collect();
for entry in ledger.posted_entries() {
let d = entry.date();
let Some(slot) = out.iter_mut().find(|(s, e, _, _)| *s <= d && d <= *e) else {
continue;
};
// Debits in, credits out, for every account in the set: cash
// received is a debit to cash, and a charge on a card in the set is
// a credit to the card.
let mut net = Decimal::ZERO;
for line in entry.lines().iter().filter(|l| set.contains(&l.account)) {
match line.side {
Side::Debit => net = add_in_range(net, line.amount.amount),
Side::Credit => net = sub_in_range(net, line.amount.amount),
}
}
if net > Decimal::ZERO {
slot.2 = add_in_range(slot.2, net);
} else {
slot.3 = sub_in_range(slot.3, net);
}
}
out
}
/// Accounts of the given types, in chart order.
pub fn accounts_of_type(ledger: &Ledger, types: &[AccountType]) -> Vec<AccountId> {
ledger
.chart()
.all()
.filter(|a| types.contains(&a.account_type))
.map(|a| a.id)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn d(y: i32, m: u32, day: u32) -> NaiveDate {
NaiveDate::from_ymd_opt(y, m, day).unwrap()
}
#[test]
fn intervals_clip_to_the_range() {
assert_eq!(
intervals(d(2026, 1, 15), d(2026, 3, 10), Interval::Month),
[
(d(2026, 1, 15), d(2026, 1, 31)),
(d(2026, 2, 1), d(2026, 2, 28)),
(d(2026, 3, 1), d(2026, 3, 10)),
]
);
assert_eq!(
intervals(d(2026, 2, 1), d(2026, 12, 31), Interval::Quarter),
[
(d(2026, 2, 1), d(2026, 3, 31)),
(d(2026, 4, 1), d(2026, 6, 30)),
(d(2026, 7, 1), d(2026, 9, 30)),
(d(2026, 10, 1), d(2026, 12, 31)),
]
);
assert_eq!(
intervals(d(2025, 6, 1), d(2026, 2, 1), Interval::Year),
[
(d(2025, 6, 1), d(2025, 12, 31)),
(d(2026, 1, 1), d(2026, 2, 1)),
]
);
assert!(intervals(d(2026, 2, 1), d(2026, 1, 1), Interval::Month).is_empty());
}
/// At chrono's last day (books an earlier version wrote may hold it):
/// the last interval ends there, never a panic (BS-entry-4).
#[test]
fn intervals_reach_chronos_last_day() {
let y = NaiveDate::MAX.year();
assert_eq!(
intervals(d(y, 11, 20), NaiveDate::MAX, Interval::Month),
[(d(y, 11, 20), d(y, 11, 30)), (d(y, 12, 1), NaiveDate::MAX)]
);
assert_eq!(
intervals(d(y, 2, 2), NaiveDate::MAX, Interval::Year),
[(d(y, 2, 2), NaiveDate::MAX)]
);
assert_eq!(
intervals(d(y, 12, 31), NaiveDate::MAX, Interval::Quarter),
[(NaiveDate::MAX, NaiveDate::MAX)]
);
assert_eq!(
intervals(
NaiveDate::MIN,
d(NaiveDate::MIN.year(), 2, 3),
Interval::Month
),
[
(NaiveDate::MIN, d(NaiveDate::MIN.year(), 1, 31)),
(
d(NaiveDate::MIN.year(), 2, 1),
d(NaiveDate::MIN.year(), 2, 3)
),
]
);
}
}