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:
1 parent
7aa0cc68f3
commit
afc7c545da
3 files changed
+20
-15
No files matched your search
@@ -64,7 +64,7 @@ use crate::reports::{
|
|||||||
Settings, bs_statement, cash_flow_for, member_capitals, pnl_statement, report_members,
|
Settings, bs_statement, cash_flow_for, member_capitals, pnl_statement, report_members,
|
||||||
report_meta, year_earnings,
|
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::tins::{TinSource, tin_type_key};
|
||||||
use crate::vendor_1099::{vendor_1099_csv, vendor_1099_csv_name, vendor_1099_worksheet};
|
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).
|
// a K-1 (each member gets their own).
|
||||||
if rows.len() > 1 {
|
if rows.len() > 1 {
|
||||||
let sum = |f: fn(&ledger_core::k1::K1Row) -> Decimal| {
|
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![
|
let mut cells = vec![
|
||||||
csv_escape("Total, all members (not a K-1)"),
|
csv_escape("Total, all members (not a K-1)"),
|
||||||
String::new(),
|
String::new(),
|
||||||
share_text(share),
|
share.map_or_else(|| total_text(None), share_text),
|
||||||
String::new(),
|
String::new(),
|
||||||
String::new(),
|
String::new(),
|
||||||
String::new(),
|
String::new(),
|
||||||
@@ -315,7 +315,7 @@ pub fn build_package<B: PackageBooks + ?Sized>(
|
|||||||
sum(|r| r.other),
|
sum(|r| r.other),
|
||||||
sum(|r| r.ending),
|
sum(|r| r.ending),
|
||||||
] {
|
] {
|
||||||
cells.push(money(v));
|
cells.push(total_text(v));
|
||||||
}
|
}
|
||||||
w.push_str(&cells.join(","));
|
w.push_str(&cells.join(","));
|
||||||
w.push('\n');
|
w.push('\n');
|
||||||
@@ -530,18 +530,16 @@ pub fn build_package<B: PackageBooks + ?Sized>(
|
|||||||
e.id(),
|
e.id(),
|
||||||
clean(&a.file_name)
|
clean(&a.file_name)
|
||||||
));
|
));
|
||||||
let amount: Decimal = e
|
let amount = ledger_core::kernel::total_of(
|
||||||
.lines()
|
e.lines().iter().filter(|l| l.side == Side::Debit),
|
||||||
.iter()
|
|l| l.amount.amount,
|
||||||
.filter(|l| l.side == Side::Debit)
|
);
|
||||||
.map(|l| l.amount.amount)
|
|
||||||
.sum();
|
|
||||||
index.push_str(&format!(
|
index.push_str(&format!(
|
||||||
"{},{},\"{}\",{},{},{}\n",
|
"{},{},\"{}\",{},{},{}\n",
|
||||||
e.id(),
|
e.id(),
|
||||||
e.date(),
|
e.date(),
|
||||||
e.description().replace('"', "\"\""),
|
e.description().replace('"', "\"\""),
|
||||||
money(amount),
|
total_text(amount),
|
||||||
name,
|
name,
|
||||||
a.blob_sha
|
a.blob_sha
|
||||||
));
|
));
|
||||||
|
|||||||
@@ -29,6 +29,13 @@ pub fn money(n: Decimal) -> String {
|
|||||||
n.to_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,
|
/// 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
|
/// 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
|
/// share of a third keeps its places). The printed text depends on the
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ use ledger_core::{Decision1099, EntryId, Ledger, PartyId, Payment1099, Status109
|
|||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
|
|
||||||
use crate::reports::Settings;
|
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};
|
use crate::tins::{TinSource, tin_type_key};
|
||||||
|
|
||||||
/// Where payments to vendors come from, for the 1099 worksheet: the
|
/// 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.
|
/// "1 payment" or "3 payments", with their total.
|
||||||
fn payments_text(list: &[Payment1099]) -> String {
|
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();
|
let n = list.len();
|
||||||
format!(
|
format!(
|
||||||
"{n} payment{} totaling {}",
|
"{n} payment{} totaling {}",
|
||||||
if n == 1 { "" } else { "s" },
|
if n == 1 { "" } else { "s" },
|
||||||
money(total)
|
total_text(total)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user