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>
75 lines
2.7 KiB
TypeScript
75 lines
2.7 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { planSupplierPayment } from '@/lib/invoices/apply-supplier-payment'
|
|
|
|
describe('planSupplierPayment', () => {
|
|
const invoice = { total: 11231.25, paid_amount: 0, remaining_amount: 11231.25 }
|
|
|
|
it('settles in full and flags öre when a whole-krona payment is a sub-krona short (absorbOreRounding)', () => {
|
|
const r = planSupplierPayment(invoice, 11231, { absorbOreRounding: true })
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.plan.newStatus).toBe('paid')
|
|
expect(r.plan.newRemaining).toBe(0)
|
|
expect(r.plan.newPaidAmount).toBe(11231.25) // the AP, not the cash, is fully cleared
|
|
expect(r.plan.oreSettled).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('accepts a sub-krona OVERpayment as öresavrundning instead of rejecting it', () => {
|
|
const inv = { total: 11231, paid_amount: 0, remaining_amount: 11231 }
|
|
const r = planSupplierPayment(inv, 11231.25, { absorbOreRounding: true })
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.plan.newStatus).toBe('paid')
|
|
expect(r.plan.oreSettled).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('leaves a ≥1 kr shortfall as a genuine partial', () => {
|
|
const r = planSupplierPayment(invoice, 5000, { absorbOreRounding: true })
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.plan.newStatus).toBe('partially_paid')
|
|
expect(r.plan.newRemaining).toBe(6231.25)
|
|
expect(r.plan.oreSettled).toBe(false)
|
|
}
|
|
})
|
|
|
|
it('rejects an overpayment beyond the 1 kr öre band', () => {
|
|
const r = planSupplierPayment(invoice, 12000, { absorbOreRounding: true })
|
|
expect(r.ok).toBe(false)
|
|
if (!r.ok) {
|
|
expect(r.code).toBe('MATCH_SI_AMOUNT_EXCEEDS_REMAINING')
|
|
expect(r.details.remaining_amount).toBe(11231.25)
|
|
}
|
|
})
|
|
|
|
it('exact payment settles fully without flagging öre', () => {
|
|
const inv = { total: 1000, paid_amount: 0, remaining_amount: 1000 }
|
|
const r = planSupplierPayment(inv, 1000, { absorbOreRounding: true })
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.plan.newStatus).toBe('paid')
|
|
expect(r.plan.oreSettled).toBe(false)
|
|
}
|
|
})
|
|
|
|
describe('without öre absorption (default — preserves legacy behaviour)', () => {
|
|
it('strands the sub-krona remainder as a partial', () => {
|
|
const r = planSupplierPayment(invoice, 11231)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.plan.newStatus).toBe('partially_paid')
|
|
expect(r.plan.newRemaining).toBe(0.25)
|
|
expect(r.plan.oreSettled).toBe(false)
|
|
}
|
|
})
|
|
|
|
it('rejects even a sub-krona overpayment (strict half-öre tolerance)', () => {
|
|
const inv = { total: 11231, paid_amount: 0, remaining_amount: 11231 }
|
|
const r = planSupplierPayment(inv, 11231.25)
|
|
expect(r.ok).toBe(false)
|
|
})
|
|
})
|
|
})
|