diff --git a/crates/ledger-core/src/error.rs b/crates/ledger-core/src/error.rs index bb437d0d..72d434a5 100644 --- a/crates/ledger-core/src/error.rs +++ b/crates/ledger-core/src/error.rs @@ -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), diff --git a/crates/ledger-core/src/ledger.rs b/crates/ledger-core/src/ledger.rs index e5a0c005..77b0877b 100644 --- a/crates/ledger-core/src/ledger.rs +++ b/crates/ledger-core/src/ledger.rs @@ -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(), ) })?; diff --git a/crates/ledger-core/tests/bookspec/common.rs b/crates/ledger-core/tests/bookspec/common.rs index eaa2b261..7b0f42f4 100644 --- a/crates/ledger-core/tests/bookspec/common.rs +++ b/crates/ledger-core/tests/bookspec/common.rs @@ -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", diff --git a/crates/ledger-core/tests/spec_documents.rs b/crates/ledger-core/tests/spec_documents.rs index 1986e901..66d5bf46 100644 --- a/crates/ledger-core/tests/spec_documents.rs +++ b/crates/ledger-core/tests/spec_documents.rs @@ -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)); +} diff --git a/docs/spec/claims/arap.md b/docs/spec/claims/arap.md index 4c30327b..0bf59ffa 100644 --- a/docs/spec/claims/arap.md +++ b/docs/spec/claims/arap.md @@ -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`,