* fix(invoices): reject overpayment on all invoice-match paths (audit C3) The paid/remaining math was copy-pasted across three sites; the dashboard match-invoice route guarded against overpayment but the v1 public API route and the agent/MCP commitMatchTransactionInvoice had drifted WITHOUT it — silently accepting payment > remaining (recording paid_amount > total, over-crediting AR; cleanup needs storno, not edit). - New lib/invoices/apply-invoice-payment.ts planInvoicePayment(): single source of the paid/remaining/status math + overpayment guard, via canonical roundOre (@/lib/money, guard rail #9). FX-agnostic — caller passes the invoice-currency amount. - All three sites delegate; the guard runs BEFORE journal-entry creation so a rejected match never burns a voucher number. Dashboard behaviour unchanged (faithful extraction — its existing overpayment test still passes, the equivalence anchor). v1 returns MATCH_AMOUNT_EXCEEDS_REMAINING; commit returns the same registry message at 400. - Removes 7 hand-rolled Math.round(x*100)/100 sites; antipattern guard ratchets 668 -> 661. - Unit tests for the helper (overpayment rejection, half-öre tolerance, remaining_amount fallback). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * review: run overpayment guard before the storno (PR #647) greptile: in commit.ts and the v1 route the conflicting-JE storno ran BEFORE the new guard, so a rejected overpayment would still reverse the transaction's prior JE and null its journal_entry_id — a side effect on a rejected match. Move planInvoicePayment above the storno so a rejection leaves the transaction fully untouched. (The dashboard route's pre-existing storno-before-guard ordering is FX-entangled and unchanged here; noted as a follow-up.) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
68 lines
2.3 KiB
TypeScript
68 lines
2.3 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import {
|
|
planInvoicePayment,
|
|
PAYMENT_OVERSHOOT_TOLERANCE,
|
|
} from '@/lib/invoices/apply-invoice-payment'
|
|
|
|
describe('planInvoicePayment', () => {
|
|
it('marks fully paid on an exact payment', () => {
|
|
const r = planInvoicePayment({ total: 1000, paid_amount: 0, remaining_amount: 1000 }, 1000)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.plan).toEqual({
|
|
newPaidAmount: 1000,
|
|
newRemaining: 0,
|
|
isFullyPaid: true,
|
|
newStatus: 'paid',
|
|
})
|
|
}
|
|
})
|
|
|
|
it('marks partially paid on a partial payment', () => {
|
|
const r = planInvoicePayment({ total: 1000, paid_amount: 0, remaining_amount: 1000 }, 400)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.plan.newStatus).toBe('partially_paid')
|
|
expect(r.plan.newPaidAmount).toBe(400)
|
|
expect(r.plan.newRemaining).toBe(600)
|
|
expect(r.plan.isFullyPaid).toBe(false)
|
|
}
|
|
})
|
|
|
|
it('accumulates onto an existing paid_amount', () => {
|
|
const r = planInvoicePayment({ total: 1000, paid_amount: 600, remaining_amount: 400 }, 400)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.plan.newPaidAmount).toBe(1000)
|
|
expect(r.plan.isFullyPaid).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('REJECTS overpayment (the bug: agent/v1 paths used to swallow it)', () => {
|
|
const r = planInvoicePayment({ total: 1000, paid_amount: 0, remaining_amount: 1000 }, 1500)
|
|
expect(r.ok).toBe(false)
|
|
if (!r.ok) {
|
|
expect(r.code).toBe('MATCH_AMOUNT_EXCEEDS_REMAINING')
|
|
expect(r.details).toEqual({
|
|
transaction_amount: 1500,
|
|
remaining_amount: 1000,
|
|
excess: 500,
|
|
})
|
|
}
|
|
})
|
|
|
|
it('accepts a sub-öre overshoot (float drift) but rejects a real one-öre over', () => {
|
|
expect(planInvoicePayment({ total: 1000, remaining_amount: 1000 }, 1000.004).ok).toBe(true)
|
|
expect(planInvoicePayment({ total: 1000, remaining_amount: 1000 }, 1000.01).ok).toBe(false)
|
|
})
|
|
|
|
it('falls back to total - paid_amount when remaining_amount is absent', () => {
|
|
expect(planInvoicePayment({ total: 1000, paid_amount: 300 }, 700).ok).toBe(true)
|
|
expect(planInvoicePayment({ total: 1000, paid_amount: 300 }, 701).ok).toBe(false)
|
|
})
|
|
|
|
it('overshoot tolerance is half an öre', () => {
|
|
expect(PAYMENT_OVERSHOOT_TOLERANCE).toBe(0.005)
|
|
})
|
|
})
|