Files
accounted/lib/invoices/__tests__/apply-invoice-payment.test.ts
T
Jakob WennbergandClaude Opus 4.8 b38b3d0230 fix(bookkeeping): settle öre differences to 3740 and improve supplier-invoice matching (#699)
Whole-krona Bankgiro/Swish payments of öre-bearing invoices were stranded
as partially_paid forever (e.g. 11 231 paid on an 11 231,25 invoice left
0,25 kr open). Book the sub-krona residual to BAS 3740 (Öres- och
kronutjämning) and settle the invoice in full, on both the supplier- and
customer-invoice match flows.

New shared pure helpers buildSupplierPaymentClearingLines +
planSupplierPayment mirror the customer-side primitives; routing preview
and commit through the same builder also fixes two pre-existing
preview↔commit drifts (payment account + line descriptions). Öre
absorption is accrual-only — cash entries book the full invoice, so
absorbing there would hide a 1930 discrepancy.

Also improves supplier-invoice ↔ bank matching:
- Pass-3 date window now spans [invoice_date-5, due_date+5] instead of
  due_date ±5, so early payments auto-match; an ambiguity guard demotes
  non-unique amount matches to suggestions.
- New retroactive matcher (on supplier_invoice.registered/.approved)
  surfaces the settling bank payment when the invoice is registered after
  the payment was imported. Matches are written as suggestions for
  one-click confirm-to-book, never silently auto-booked.

Tests: new unit tests for both pure helpers; extended matching, handler,
customer öre, and route suites. Full suite green (407 files / 5364 tests).

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 19:09:39 +02:00

69 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',
oreSettled: false,
})
}
})
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)
})
})