refactor(mcp): previews and totals refuse oversized sums instead of panicking
preview_lines_for (balance before + line), the disposal preview's debits, the depreciation run's catch-up difference, the digest's account movement and the payroll remittance total used raw + / - / .sum() on stored or replayed figures; they now go through ledger_core::money's checked helpers and return the tool's string error (TooLargeToCompute). Unchanged for ordinary input. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
afc7c545da
commit
e6104b49d6
4 files changed
+46
-27
No files matched your search
@@ -1348,7 +1348,9 @@ fn preview_lines_for(
|
||||
} else {
|
||||
-amount.amount
|
||||
};
|
||||
let after = before + delta;
|
||||
let after =
|
||||
ledger_core::money::add_exact(before, delta, "the account's balance after the line")
|
||||
.map_err(|e| e.to_string())?;
|
||||
out.push(PreviewLine {
|
||||
account_id: *account_id,
|
||||
code: account.code.clone(),
|
||||
|
||||
@@ -839,7 +839,15 @@ impl BooksServer {
|
||||
description,
|
||||
period: r.period,
|
||||
amount: money(r.amount),
|
||||
caught_up: money((r.amount - r.scheduled).max(Decimal::ZERO)),
|
||||
caught_up: money(
|
||||
ledger_core::money::sub_exact(
|
||||
r.amount,
|
||||
r.scheduled,
|
||||
"the catch-up depreciation",
|
||||
)
|
||||
.map_err(|e| e.to_string())?
|
||||
.max(Decimal::ZERO),
|
||||
),
|
||||
entry_id,
|
||||
already_posted: r.already_posted,
|
||||
});
|
||||
@@ -916,14 +924,20 @@ impl BooksServer {
|
||||
.map(code_of)
|
||||
.collect::<Result<_, _>>()?;
|
||||
let code_refs: Vec<&str> = codes.iter().map(|s| s.as_str()).collect();
|
||||
let debits = preview.accumulated
|
||||
+ proceeds.amount
|
||||
+ preview.catch_up_depreciation
|
||||
+ if preview.gain_loss < Decimal::ZERO {
|
||||
-preview.gain_loss
|
||||
} else {
|
||||
Decimal::ZERO
|
||||
};
|
||||
let debits = ledger_core::money::sum_exact(
|
||||
[
|
||||
preview.accumulated,
|
||||
proceeds.amount,
|
||||
preview.catch_up_depreciation,
|
||||
if preview.gain_loss < Decimal::ZERO {
|
||||
-preview.gain_loss
|
||||
} else {
|
||||
Decimal::ZERO
|
||||
},
|
||||
],
|
||||
"the disposal's debits",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let receipt_required = self
|
||||
.settings()
|
||||
.policy
|
||||
|
||||
@@ -114,12 +114,12 @@ impl BooksServer {
|
||||
return Err(party_required("pass agency: the tax agency paid"));
|
||||
}
|
||||
// Total before remitting, for the response.
|
||||
let total: Decimal = store
|
||||
.ledger()
|
||||
.payroll_tax_payables()
|
||||
.iter()
|
||||
.map(|(_, b)| *b)
|
||||
.sum();
|
||||
let total = ledger_core::money::sum_exact_of(
|
||||
store.ledger().payroll_tax_payables(),
|
||||
|(_, b)| b,
|
||||
"the payroll taxes payable",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let entry_id = store
|
||||
.remit_payroll_taxes(parse_date(&p.date)?, cash)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -31,21 +31,24 @@ fn movement_over(
|
||||
ledger: &Ledger,
|
||||
account: AccountId,
|
||||
entries: &[&ledger_core::JournalEntry],
|
||||
) -> Decimal {
|
||||
let movement: Decimal = entries
|
||||
.iter()
|
||||
.flat_map(|e| e.lines())
|
||||
.filter(|l| l.account == account)
|
||||
.map(|l| match l.side {
|
||||
) -> Result<Decimal, String> {
|
||||
let movement = ledger_core::money::sum_exact_of(
|
||||
entries
|
||||
.iter()
|
||||
.flat_map(|e| e.lines())
|
||||
.filter(|l| l.account == account),
|
||||
|l| match l.side {
|
||||
Side::Debit => l.amount.amount,
|
||||
Side::Credit => -l.amount.amount,
|
||||
})
|
||||
.sum();
|
||||
},
|
||||
"an account's movement",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let account = ledger.chart().get(account).expect("account exists");
|
||||
match account.account_type.normal_side() {
|
||||
Ok(match account.account_type.normal_side() {
|
||||
Side::Debit => movement,
|
||||
Side::Credit => -movement,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn lines_out(lines: &[ledger_core::StatementLine]) -> Vec<StatementLineOut> {
|
||||
@@ -835,7 +838,7 @@ impl BooksServer {
|
||||
if a.placeholder {
|
||||
continue;
|
||||
}
|
||||
let m = movement_over(ledger, a.id, &in_range);
|
||||
let m = movement_over(ledger, a.id, &in_range)?;
|
||||
if !m.is_zero() {
|
||||
totals.push(DigestTotal {
|
||||
code: a.code.clone(),
|
||||
|
||||
Reference in new issue
Block a user