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 {
|
} else {
|
||||||
-amount.amount
|
-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 {
|
out.push(PreviewLine {
|
||||||
account_id: *account_id,
|
account_id: *account_id,
|
||||||
code: account.code.clone(),
|
code: account.code.clone(),
|
||||||
|
|||||||
@@ -839,7 +839,15 @@ impl BooksServer {
|
|||||||
description,
|
description,
|
||||||
period: r.period,
|
period: r.period,
|
||||||
amount: money(r.amount),
|
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,
|
entry_id,
|
||||||
already_posted: r.already_posted,
|
already_posted: r.already_posted,
|
||||||
});
|
});
|
||||||
@@ -916,14 +924,20 @@ impl BooksServer {
|
|||||||
.map(code_of)
|
.map(code_of)
|
||||||
.collect::<Result<_, _>>()?;
|
.collect::<Result<_, _>>()?;
|
||||||
let code_refs: Vec<&str> = codes.iter().map(|s| s.as_str()).collect();
|
let code_refs: Vec<&str> = codes.iter().map(|s| s.as_str()).collect();
|
||||||
let debits = preview.accumulated
|
let debits = ledger_core::money::sum_exact(
|
||||||
+ proceeds.amount
|
[
|
||||||
+ preview.catch_up_depreciation
|
preview.accumulated,
|
||||||
+ if preview.gain_loss < Decimal::ZERO {
|
proceeds.amount,
|
||||||
-preview.gain_loss
|
preview.catch_up_depreciation,
|
||||||
} else {
|
if preview.gain_loss < Decimal::ZERO {
|
||||||
Decimal::ZERO
|
-preview.gain_loss
|
||||||
};
|
} else {
|
||||||
|
Decimal::ZERO
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"the disposal's debits",
|
||||||
|
)
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
let receipt_required = self
|
let receipt_required = self
|
||||||
.settings()
|
.settings()
|
||||||
.policy
|
.policy
|
||||||
|
|||||||
@@ -114,12 +114,12 @@ impl BooksServer {
|
|||||||
return Err(party_required("pass agency: the tax agency paid"));
|
return Err(party_required("pass agency: the tax agency paid"));
|
||||||
}
|
}
|
||||||
// Total before remitting, for the response.
|
// Total before remitting, for the response.
|
||||||
let total: Decimal = store
|
let total = ledger_core::money::sum_exact_of(
|
||||||
.ledger()
|
store.ledger().payroll_tax_payables(),
|
||||||
.payroll_tax_payables()
|
|(_, b)| b,
|
||||||
.iter()
|
"the payroll taxes payable",
|
||||||
.map(|(_, b)| *b)
|
)
|
||||||
.sum();
|
.map_err(|e| e.to_string())?;
|
||||||
let entry_id = store
|
let entry_id = store
|
||||||
.remit_payroll_taxes(parse_date(&p.date)?, cash)
|
.remit_payroll_taxes(parse_date(&p.date)?, cash)
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
|
|||||||
@@ -31,21 +31,24 @@ fn movement_over(
|
|||||||
ledger: &Ledger,
|
ledger: &Ledger,
|
||||||
account: AccountId,
|
account: AccountId,
|
||||||
entries: &[&ledger_core::JournalEntry],
|
entries: &[&ledger_core::JournalEntry],
|
||||||
) -> Decimal {
|
) -> Result<Decimal, String> {
|
||||||
let movement: Decimal = entries
|
let movement = ledger_core::money::sum_exact_of(
|
||||||
.iter()
|
entries
|
||||||
.flat_map(|e| e.lines())
|
.iter()
|
||||||
.filter(|l| l.account == account)
|
.flat_map(|e| e.lines())
|
||||||
.map(|l| match l.side {
|
.filter(|l| l.account == account),
|
||||||
|
|l| match l.side {
|
||||||
Side::Debit => l.amount.amount,
|
Side::Debit => l.amount.amount,
|
||||||
Side::Credit => -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");
|
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::Debit => movement,
|
||||||
Side::Credit => -movement,
|
Side::Credit => -movement,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lines_out(lines: &[ledger_core::StatementLine]) -> Vec<StatementLineOut> {
|
fn lines_out(lines: &[ledger_core::StatementLine]) -> Vec<StatementLineOut> {
|
||||||
@@ -835,7 +838,7 @@ impl BooksServer {
|
|||||||
if a.placeholder {
|
if a.placeholder {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let m = movement_over(ledger, a.id, &in_range);
|
let m = movement_over(ledger, a.id, &in_range)?;
|
||||||
if !m.is_zero() {
|
if !m.is_zero() {
|
||||||
totals.push(DigestTotal {
|
totals.push(DigestTotal {
|
||||||
code: a.code.clone(),
|
code: a.code.clone(),
|
||||||
|
|||||||
Reference in new issue
Block a user