refactor(package): exported totals are exact or say so

The K-1 members' total row, the receipts index amount and the 1099
worksheet's payments text summed with a raw .sum(); they use
kernel::total_of and print 'too large to compute' if a sum is past exact
arithmetic (text::total_text), so an export of restored books never
crashes. Unchanged text for every ordinary input.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-08 12:23:11 -07:00
1 parent 7aa0cc68f3
commit afc7c545da
3 files changed
+20 -15

No files matched your search

+10 -12
View File
@@ -64,7 +64,7 @@ use crate::reports::{
Settings, bs_statement, cash_flow_for, member_capitals, pnl_statement, report_members,
report_meta, year_earnings,
};
use crate::text::{csv_escape, money, share_text};
use crate::text::{csv_escape, money, share_text, total_text};
use crate::tins::{TinSource, tin_type_key};
use crate::vendor_1099::{vendor_1099_csv, vendor_1099_csv_name, vendor_1099_worksheet};
@@ -292,13 +292,13 @@ pub fn build_package<B: PackageBooks + ?Sized>(
// a K-1 (each member gets their own).
if rows.len() > 1 {
let sum = |f: fn(&ledger_core::k1::K1Row) -> Decimal| {
rows.iter().map(f).sum::<Decimal>()
ledger_core::kernel::total_of(&rows, f)
};
let share: Decimal = company.members.iter().map(|m| m.share).sum();
let share = ledger_core::kernel::total_of(&company.members, |m| m.share);
let mut cells = vec![
csv_escape("Total, all members (not a K-1)"),
String::new(),
share_text(share),
share.map_or_else(|| total_text(None), share_text),
String::new(),
String::new(),
String::new(),
@@ -315,7 +315,7 @@ pub fn build_package<B: PackageBooks + ?Sized>(
sum(|r| r.other),
sum(|r| r.ending),
] {
cells.push(money(v));
cells.push(total_text(v));
}
w.push_str(&cells.join(","));
w.push('\n');
@@ -530,18 +530,16 @@ pub fn build_package<B: PackageBooks + ?Sized>(
e.id(),
clean(&a.file_name)
));
let amount: Decimal = e
.lines()
.iter()
.filter(|l| l.side == Side::Debit)
.map(|l| l.amount.amount)
.sum();
let amount = ledger_core::kernel::total_of(
e.lines().iter().filter(|l| l.side == Side::Debit),
|l| l.amount.amount,
);
index.push_str(&format!(
"{},{},\"{}\",{},{},{}\n",
e.id(),
e.date(),
e.description().replace('"', "\"\""),
money(amount),
total_text(amount),
name,
a.blob_sha
));
+7
View File
@@ -29,6 +29,13 @@ pub fn money(n: Decimal) -> String {
n.to_string()
}
/// A total as [`money`] prints it, or "too large to compute" when the sum
/// of its figures was past exact arithmetic (`kernel::total` gave `None`):
/// a printed total is exact or says so, and never crashes the export.
pub fn total_text(total: Option<Decimal>) -> String {
total.map_or_else(|| "too large to compute".to_string(), money)
}
/// A member's share as the K-1 worksheet prints it: at least two places,
/// however it was written ("50%", "0.5" and "0.500" all print "0.50"; a
/// share of a third keeps its places). The printed text depends on the
+3 -3
View File
@@ -34,7 +34,7 @@ use ledger_core::{Decision1099, EntryId, Ledger, PartyId, Payment1099, Status109
use rust_decimal::Decimal;
use crate::reports::Settings;
use crate::text::{csv_escape, money};
use crate::text::{csv_escape, money, total_text};
use crate::tins::{TinSource, tin_type_key};
/// Where payments to vendors come from, for the 1099 worksheet: the
@@ -193,12 +193,12 @@ fn entries_text(ids: &[EntryId]) -> String {
/// "1 payment" or "3 payments", with their total.
fn payments_text(list: &[Payment1099]) -> String {
let total: Decimal = list.iter().map(|p| p.amount).sum();
let total = ledger_core::kernel::total_of(list, |p| p.amount);
let n = list.len();
format!(
"{n} payment{} totaling {}",
if n == 1 { "" } else { "s" },
money(total)
total_text(total)
)
}