fix(core): a fee above the cash is its own refusal
A processor cannot keep more than it was paid. The engine already refused
a settlement whose fee is above its cash, but as InvalidSettlement, one
message for two causes ("the cash and discounts don't cover the
applications exactly, or the fee is above the cash"), so the bookspec
adapter had to read the refusal out of the message's prose. Settlement
now checks fee > Σ cash itself, in the same place as before (after the
per-application checks, before kernel::settle), and returns the typed
LedgerError::FeeAboveCash { fee, cash }. InvalidSettlement keeps what
remains (a sum too large, a negative fee, a fee that leaves no deposit),
and its message no longer claims the fee case. The adapter matches the
variant like every sibling.
The refusal's precedence is unchanged, and nothing was journaled by a
refused settlement before or now (the store journals only a settlement
that succeeded); no stored data changes. The only output change is the
error text of a fee above the cash and of the leftover InvalidSettlement
cases; no golden or test asserted either text, and no caller matched the
variant (ledger-mcp, ledger-store, ledger-cli pass them on as text).
a_fee_above_the_cash_is_refused_and_changes_nothing encodes the rule
(150.00 of cash, a fee of 150.01 refused, books untouched; a fee equal to
the cash is the other refusal; a fee of 3.00 posts, deposit 147.00).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d35775922b
commit
0ffb0935fe
5 files changed
+105
-11
No files matched your search
@@ -749,9 +749,24 @@ pub enum LedgerError {
|
||||
#[error("{0}")]
|
||||
InvalidFee(String),
|
||||
|
||||
/// A settlement's figures can't be exact: the cash and discounts
|
||||
/// don't cover the applications, or the fee leaves a negative deposit
|
||||
/// (BS-arap-6 INVARIANT 1).
|
||||
/// A processor's fee is above the cash it was withheld from: a
|
||||
/// processor cannot keep more than it was paid (BS-arap-6). A fee equal
|
||||
/// to the cash is [`LedgerError::InvalidSettlement`]: the deposit would
|
||||
/// be nothing.
|
||||
#[error(
|
||||
"the processor's fee of {fee} is above the cash of {cash}: a processor can't keep more than it was paid"
|
||||
)]
|
||||
FeeAboveCash {
|
||||
/// The fee asked for.
|
||||
fee: Decimal,
|
||||
/// The settlement's cash, summed over its applications.
|
||||
cash: Decimal,
|
||||
},
|
||||
|
||||
/// A settlement's figures can't be exact (BS-arap-6 INVARIANT 1): an
|
||||
/// amount too large to add, a negative fee, or a fee that leaves
|
||||
/// nothing to deposit; a fee above the cash is
|
||||
/// [`LedgerError::FeeAboveCash`].
|
||||
#[error("{0}")]
|
||||
InvalidSettlement(String),
|
||||
|
||||
|
||||
@@ -6701,9 +6701,22 @@ impl Ledger {
|
||||
discounts.push(app.discount);
|
||||
}
|
||||
let fee_amount = params.fee.as_ref().map_or(Decimal::ZERO, |f| f.amount);
|
||||
// A fee above the cash is its own refusal; the kernel would also
|
||||
// answer `None` for it, with no way to say which of its refusals it
|
||||
// was. A sum that overflows is left to the kernel (too large).
|
||||
if let Some(total_cash) = cash
|
||||
.iter()
|
||||
.try_fold(Decimal::ZERO, |sum, c| sum.checked_add(*c))
|
||||
&& fee_amount > total_cash
|
||||
{
|
||||
return Err(LedgerError::FeeAboveCash {
|
||||
fee: fee_amount,
|
||||
cash: total_cash,
|
||||
});
|
||||
}
|
||||
let settlement = kernel::settle(&cash, &discounts, fee_amount).ok_or_else(|| {
|
||||
LedgerError::InvalidSettlement(
|
||||
"the cash and discounts don't cover the applications exactly, or the fee is above the cash"
|
||||
"the settlement's figures aren't exact: an amount is too large, or the fee is negative"
|
||||
.into(),
|
||||
)
|
||||
})?;
|
||||
|
||||
@@ -154,12 +154,7 @@ pub(crate) fn refusal(e: &LedgerError) -> String {
|
||||
LedgerError::Overpayment(..) => "application_above_owed",
|
||||
LedgerError::DiscountNeedsReason { .. } => "discount_needs_reason",
|
||||
LedgerError::DiscountOnAssetBill { .. } => "discount_on_asset_bill",
|
||||
// The cash and the discounts the adapter hands over always cover the
|
||||
// applications exactly, so the one way the engine's exactness check
|
||||
// fails is a fee above the cash (BS-arap-6).
|
||||
LedgerError::InvalidSettlement(why) if why.contains("the fee is above the cash") => {
|
||||
"fee_above_cash"
|
||||
}
|
||||
LedgerError::FeeAboveCash { .. } => "fee_above_cash",
|
||||
LedgerError::UnbalancedEntry { .. } => "unbalanced",
|
||||
LedgerError::TooFewLines => "too_few_lines",
|
||||
LedgerError::NegativeAmountLine => "negative_amount",
|
||||
|
||||
@@ -2553,3 +2553,72 @@ fn discount_on_an_asset_bill_is_refused_with_the_remedy() {
|
||||
);
|
||||
b.ties(d(2026, 4, 5));
|
||||
}
|
||||
|
||||
/// A processor cannot keep more than it was paid: a fee above the cash is
|
||||
/// refused as its own refusal (`FeeAboveCash`, BS-arap-6) and changes
|
||||
/// nothing. A fee equal to the cash is a different refusal (the deposit
|
||||
/// would be nothing), and a fee within the cash still posts.
|
||||
#[test]
|
||||
fn a_fee_above_the_cash_is_refused_and_changes_nothing() {
|
||||
use ledger_core::{Application, DocRef, Fee, SettleParams};
|
||||
let mut b = books();
|
||||
let first = b.invoice(d(2026, 4, 1), 10_000);
|
||||
let second = b.invoice(d(2026, 4, 2), 5_000);
|
||||
let params = |fee: i64| SettleParams {
|
||||
date: d(2026, 4, 5),
|
||||
description: "Card payment".into(),
|
||||
cash_account: b.bank,
|
||||
party: b.customer,
|
||||
applications: vec![
|
||||
Application {
|
||||
doc: DocRef::Invoice(first),
|
||||
cash: cents(10_000),
|
||||
discount: Decimal::ZERO,
|
||||
discount_reason: None,
|
||||
},
|
||||
Application {
|
||||
doc: DocRef::Invoice(second),
|
||||
cash: cents(5_000),
|
||||
discount: Decimal::ZERO,
|
||||
discount_reason: None,
|
||||
},
|
||||
],
|
||||
fee: Some(Fee {
|
||||
amount: cents(fee),
|
||||
account: None,
|
||||
vendor: None,
|
||||
}),
|
||||
instrument: None,
|
||||
reference: None,
|
||||
};
|
||||
let posted_before = b.l.posted_entries().count();
|
||||
let first_before = b.state(DocRef::Invoice(first), d(2026, 4, 5));
|
||||
let second_before = b.state(DocRef::Invoice(second), d(2026, 4, 5));
|
||||
// The cash is 150.00 across two applications; a fee of 150.01 is above it.
|
||||
let refused = b.l.settle(params(15_001));
|
||||
assert!(
|
||||
matches!(
|
||||
refused,
|
||||
Err(LedgerError::FeeAboveCash { fee, cash })
|
||||
if fee == cents(15_001) && cash == cents(15_000)
|
||||
),
|
||||
"{refused:?}"
|
||||
);
|
||||
assert_eq!(b.l.posted_entries().count(), posted_before);
|
||||
assert_eq!(b.state(DocRef::Invoice(first), d(2026, 4, 5)), first_before);
|
||||
assert_eq!(
|
||||
b.state(DocRef::Invoice(second), d(2026, 4, 5)),
|
||||
second_before
|
||||
);
|
||||
assert_eq!(b.l.account_balance(b.bank).unwrap(), Decimal::ZERO);
|
||||
// The fee equal to the cash is the other refusal, not this one.
|
||||
assert!(matches!(
|
||||
b.l.settle(params(15_000)),
|
||||
Err(LedgerError::InvalidSettlement(_))
|
||||
));
|
||||
assert_eq!(b.l.posted_entries().count(), posted_before);
|
||||
// A fee within the cash posts: 150.00 gross, 3.00 fee, 147.00 deposit.
|
||||
b.l.settle(params(300)).unwrap();
|
||||
assert_eq!(b.l.account_balance(b.bank).unwrap(), cents(14_700));
|
||||
b.ties(d(2026, 4, 5));
|
||||
}
|
||||
@@ -338,7 +338,9 @@ check_discount}`, `arap::{Application, Fee, SettleParams, SettlementRow}`,
|
||||
(`bill_on_terms_with_discount`, `late_discount_needs_a_reason`,
|
||||
`discount_on_an_asset_bill_is_refused_with_the_remedy`,
|
||||
`a_fee_posts_gross_to_receivables_and_net_to_the_bank`,
|
||||
`a_settlement_names_its_lines_and_refuses_a_document_twice`),
|
||||
`a_settlement_names_its_lines_and_refuses_a_document_twice`,
|
||||
`a_fee_above_the_cash_is_refused_and_changes_nothing`: the refusal is
|
||||
`LedgerError::FeeAboveCash`, its own variant),
|
||||
`tests/kernel_bridge/main.rs::settle_agrees`; Kani `settle_is_exact`;
|
||||
`ledger-store` `tests/arap_standing.rs`
|
||||
(`a_settlement_names_its_lines_and_replays`, `settlement_waits_for_documents_v2`,
|
||||
|
||||
Reference in new issue
Block a user