refactor(mcp): Decimal arithmetic goes through the kernel (K-U12)
The raw-decimal-arith detector counted 19 places in ledger-mcp before and counts 0 after. No figure changes. Sides become signs through money::on_side and money::signed_amount (server delta, report movement, the P&L-by-class normal-side figure, which collapses two matches into one call); sums and differences of books figures through add_in_range and sub_in_range; the checked sums in helpers and documents through Amount::checked_add with their error handling untouched. The count of withdrawn attachments (a usize, not a Decimal) is the length of the flattened withdraw lists instead of an Iterator::sum of their lengths: the same number. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
5ef5f47ebf
commit
1d612d25b8
7 files changed
+34
-36
No files matched your search
@@ -4,6 +4,7 @@
|
||||
//! Split whole out of `server.rs` (plan/served.md 8.2 U0a).
|
||||
|
||||
use super::*;
|
||||
use ledger_core::money::{add_in_range, sub_in_range};
|
||||
|
||||
/// What the digest lists from the control check: each line on a control
|
||||
/// account that is no document's, and each document needing attention.
|
||||
@@ -37,18 +38,15 @@ fn movement_over(
|
||||
.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,
|
||||
},
|
||||
|l| ledger_core::money::signed_amount(l.side, l.amount.amount),
|
||||
"an account's movement",
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let account = ledger.chart().get(account).expect("account exists");
|
||||
Ok(match account.account_type.normal_side() {
|
||||
Side::Debit => movement,
|
||||
Side::Credit => -movement,
|
||||
})
|
||||
Ok(ledger_core::money::signed_amount(
|
||||
account.account_type.normal_side(),
|
||||
movement,
|
||||
))
|
||||
}
|
||||
|
||||
fn lines_out(lines: &[ledger_core::StatementLine]) -> Vec<StatementLineOut> {
|
||||
@@ -1057,19 +1055,16 @@ impl BooksServer {
|
||||
Some(a) => a,
|
||||
_ => continue,
|
||||
};
|
||||
let signed = match line.side {
|
||||
Side::Debit => line.amount.amount,
|
||||
Side::Credit => -line.amount.amount,
|
||||
};
|
||||
// Normal-side positive.
|
||||
let normal = match account.account_type.normal_side() {
|
||||
Side::Debit => signed,
|
||||
Side::Credit => -signed,
|
||||
};
|
||||
let normal = ledger_core::money::on_side(
|
||||
account.account_type.normal_side(),
|
||||
line.side,
|
||||
line.amount.amount,
|
||||
);
|
||||
let acc = by_class.entry(line.class_id).or_default();
|
||||
match account.account_type {
|
||||
AccountType::Revenue => acc.revenue += normal,
|
||||
AccountType::Expense => acc.expenses += normal,
|
||||
AccountType::Revenue => acc.revenue = add_in_range(acc.revenue, normal),
|
||||
AccountType::Expense => acc.expenses = add_in_range(acc.expenses, normal),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -1099,7 +1094,7 @@ impl BooksServer {
|
||||
class,
|
||||
revenue: money(acc.revenue),
|
||||
expenses: money(acc.expenses),
|
||||
net_income: money(acc.revenue - acc.expenses),
|
||||
net_income: money(sub_in_range(acc.revenue, acc.expenses)),
|
||||
})
|
||||
.collect();
|
||||
Ok(Json(ProfitAndLossByClassOut {
|
||||
|
||||
Reference in new issue
Block a user