- Expand BAS reference from ~180 to ~1,276 accounts (full BAS Kontoplan 2026) with K2 exclusion flags, per-class data files, and computed SRU codes - Evolve invoice inbox into unified document inbox handling invoices, receipts, and government letters with AI-powered classification (Claude Haiku Vision) - Add multi-pass document-to-transaction matching engine with greedy assignment for both supplier invoices (reference/amount/date/name) and receipts (weighted amount/merchant/date scoring) - Add supplier invoice matching in transaction ingest pipeline - Inject booking template suggestions into AI extraction prompts - Surface matched documents in swipe categorization UI with one-tap booking - Auto-activate missing BAS accounts during SIE import against full reference - Add K2 filter toggle in Chart of Accounts manager - Add receipt confirmation route with BFNAR representation fields - Add database migrations for K2 support and document matching columns - Remove obsolete extension migration scripts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
132 lines
4.4 KiB
TypeScript
132 lines
4.4 KiB
TypeScript
/**
|
||
* Supplier Invoice Matching — auto-match expense transactions to unpaid supplier invoices.
|
||
*
|
||
* 4-pass matching algorithm (ordered by confidence):
|
||
* 1. Payment reference/OCR exact match → 0.98
|
||
* 2. Exact amount + bankgiro/plusgiro match → 0.92
|
||
* 3. Exact amount + date ±5 days → 0.85
|
||
* 4. Fuzzy amount (±0.01) + supplier name in description → 0.70
|
||
*
|
||
* Auto-match threshold: ≥0.85 → applied automatically
|
||
* Suggestion threshold: 0.70–0.85 → stored as potential_supplier_invoice_id
|
||
*/
|
||
|
||
import type { Transaction, SupplierInvoice } from '@/types'
|
||
|
||
export interface SupplierInvoiceMatch {
|
||
supplierInvoice: SupplierInvoice
|
||
confidence: number
|
||
matchMethod: 'payment_reference' | 'amount_bankgiro' | 'amount_date' | 'fuzzy_name'
|
||
}
|
||
|
||
/**
|
||
* Normalize payment reference for comparison (strip whitespace and non-digits).
|
||
*/
|
||
function normalizeReference(ref: string): string {
|
||
return ref.replace(/\D/g, '')
|
||
}
|
||
|
||
/**
|
||
* Find the best matching supplier invoice for an expense transaction.
|
||
* Expects invoices to have the `supplier` relation populated (for name/bankgiro matching).
|
||
* Only matches against invoices with status 'registered' or 'approved'
|
||
* and with remaining_amount > 0.
|
||
*/
|
||
export function findSupplierInvoiceMatch(
|
||
transaction: Transaction,
|
||
unpaidInvoices: SupplierInvoice[]
|
||
): SupplierInvoiceMatch | null {
|
||
if (unpaidInvoices.length === 0) return null
|
||
|
||
// Only match expense transactions
|
||
const txAmount = Math.abs(transaction.amount)
|
||
if (txAmount === 0) return null
|
||
|
||
let bestMatch: SupplierInvoiceMatch | null = null
|
||
|
||
for (const invoice of unpaidInvoices) {
|
||
// Only match against registered/approved invoices with remaining amount
|
||
if (!['registered', 'approved'].includes(invoice.status)) continue
|
||
const remaining = invoice.remaining_amount ?? invoice.total
|
||
if (remaining <= 0) continue
|
||
|
||
// Pass 1: Payment reference/OCR exact match → 0.98
|
||
if (transaction.reference && invoice.payment_reference) {
|
||
const txRef = normalizeReference(transaction.reference)
|
||
const invRef = normalizeReference(invoice.payment_reference)
|
||
if (txRef && invRef && txRef === invRef) {
|
||
return {
|
||
supplierInvoice: invoice,
|
||
confidence: 0.98,
|
||
matchMethod: 'payment_reference',
|
||
}
|
||
}
|
||
}
|
||
|
||
// Pass 2: Exact amount + bankgiro/plusgiro match → 0.92
|
||
const amountMatch = Math.abs(txAmount - remaining) < 0.005
|
||
if (amountMatch) {
|
||
const txDesc = (transaction.description || '').toLowerCase()
|
||
const supplierBg = invoice.supplier?.bankgiro
|
||
const supplierPg = invoice.supplier?.plusgiro
|
||
const bgMatch = supplierBg && txDesc.includes(normalizeReference(supplierBg))
|
||
const pgMatch = supplierPg && txDesc.includes(normalizeReference(supplierPg))
|
||
|
||
if (bgMatch || pgMatch) {
|
||
return {
|
||
supplierInvoice: invoice,
|
||
confidence: 0.92,
|
||
matchMethod: 'amount_bankgiro',
|
||
}
|
||
}
|
||
}
|
||
|
||
// Pass 3: Exact amount + date ±5 days → 0.85
|
||
if (amountMatch && invoice.due_date) {
|
||
const txDate = new Date(transaction.date)
|
||
const dueDate = new Date(invoice.due_date)
|
||
const diffDays = Math.abs((txDate.getTime() - dueDate.getTime()) / (1000 * 60 * 60 * 24))
|
||
|
||
if (diffDays <= 5) {
|
||
const confidence = 0.85
|
||
if (!bestMatch || confidence > bestMatch.confidence) {
|
||
bestMatch = {
|
||
supplierInvoice: invoice,
|
||
confidence,
|
||
matchMethod: 'amount_date',
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// Pass 4: Fuzzy amount (±0.01) + supplier name in description → 0.70
|
||
const fuzzyAmountMatch = Math.abs(txAmount - remaining) <= 0.01
|
||
const supplierName = invoice.supplier?.name
|
||
if (fuzzyAmountMatch && supplierName) {
|
||
const txDesc = (transaction.description || '').toLowerCase()
|
||
const normalizedName = supplierName.toLowerCase()
|
||
|
||
// Check if any significant word from the supplier name appears in the description
|
||
const nameWords = normalizedName
|
||
.replace(/[^\w\såäöé]/g, '')
|
||
.split(/\s+/)
|
||
.filter((w) => w.length >= 3)
|
||
|
||
const nameInDesc = nameWords.some((word) => txDesc.includes(word))
|
||
|
||
if (nameInDesc) {
|
||
const confidence = 0.70
|
||
if (!bestMatch || confidence > bestMatch.confidence) {
|
||
bestMatch = {
|
||
supplierInvoice: invoice,
|
||
confidence,
|
||
matchMethod: 'fuzzy_name',
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return bestMatch
|
||
}
|