Files
accounted/extensions/receipt-ocr/lib/receipt-categorizer.ts
T
Jakob WennbergandClaude Opus 4.6 91e2c1705a feat: per-line VAT, invoice document types, ledger-based VAT declaration, bank reconciliation, and pagination
Per-line VAT rates:
- Add generatePerRateLines() to group invoice items by vat_rate with separate
  revenue + VAT lines per rate group (invoice-entries.ts)
- Add getAvailableVatRates() and getVatTreatmentForRate() (vat-rules.ts)
- PDF template shows per-line VAT column and per-rate totals for mixed-rate invoices
- Invoice create/review UI supports per-line rate selection
- Types: add vat_rate/vat_amount to InvoiceItem, vat_rate to CreateInvoiceItemInput

Invoice document types (proforma, delivery note):
- Add InvoiceDocumentType, document_type and converted_from_id to Invoice type
- PDF hides prices for delivery notes, adds proforma notice
- Email templates support all document types
- mark-paid skips journal entries for non-invoice document types
- Migration 031: invoice_document_type

Accounting method support:
- Add AccountingMethod type (accrual/cash)
- Migration 032: add_accounting_method column to company_settings

VAT declaration rewrite:
- Rewrite to read directly from general ledger (26xx/3xxx account lines)
  instead of aggregating invoices/transactions/receipts
- ACCOUNT_RUTA mapping drives momsdeklaration boxes from GL balances

Bank reconciliation:
- Transaction ingest now pre-fetches unlinked GL lines and attempts
  auto-reconciliation during import
- Add transaction.reconciled event type
- Add ReconciliationMethod type and reconciliation_method on Transaction
- Migration 030: bank_reconciliation
- New reconciliation engine, API routes, and BankReconciliationView component

Pagination (fetchAllRows):
- New lib/supabase/fetch-all.ts overcomes PostgREST 1000-row limit
- Adopted in all report generators, SIE/SRU export, account list APIs

Fiscal period validation:
- New validate-period-duration.ts enforces max 18 months per BFL 3 kap.
- Applied in period-service.ts and fiscal-periods API

Account mapper simplification:
- Remove Levenshtein/fuzzy matching, use exact account number match only

Swedbank parser improvements:
- Support abbreviated headers (Clnr, Bokfdag, Radnr)
- Use Referens column as counterparty

Chart of accounts management:
- Add DELETE endpoint with system account and usage protection
- PUT uses partial updates
- New AccountCombobox, AddAccountDialog, EditAccountDialog, ChartOfAccountsManager

Tax deadline corrections:
- Rewrite inkomstdeklaration_ab using Skatteverket lookup table
- Rewrite arsredovisning deadline to 7 months after FY end per ÅRL 8:3

Onboarding first fiscal year:
- Add first fiscal year toggle with date pickers and 18-month validation

UI terminology:
- Change "okategoriserad/kategorisera" to "obokförd/bokföra" throughout

Report column fix:
- Fix start_date/end_date to period_start/period_end in report queries

Supplier invoice input:
- CreateSupplierInvoiceItemInput uses amount field (legacy quantity/unit_price kept)

Misc:
- SIE import uses upsert for idempotent account creation
- account-descriptions.ts falls back to BAS reference data
- Add invoice_default_notes to CompanySettings
- Update CLAUDE.md to reflect current project state

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 14:57:15 +01:00

298 lines
9.1 KiB
TypeScript

/**
* Receipt Categorizer - Auto-categorization of receipt line items
*
* Maps line item descriptions to BAS accounts and expense categories
* using pattern matching and AI-suggested categories.
*/
import type { TransactionCategory, ReceiptLineItem, ExtractedLineItem } from '@/types'
// Category mappings from suggested category to TransactionCategory
const CATEGORY_MAPPING: Record<string, TransactionCategory> = {
equipment: 'expense_equipment',
software: 'expense_software',
travel: 'expense_travel',
office: 'expense_office',
marketing: 'expense_marketing',
professional_services: 'expense_professional_services',
education: 'expense_education',
other: 'expense_other',
}
// BAS account mappings for expense categories
const BAS_ACCOUNT_MAPPING: Record<TransactionCategory, string> = {
expense_equipment: '5410', // Förbrukningsinventarier
expense_software: '5420', // Programvaror
expense_travel: '5800', // Resekostnader
expense_office: '5010', // Lokalhyra / Kontorsmaterial
expense_marketing: '5910', // Annonsering
expense_professional_services: '6530', // Redovisningstjänster
expense_education: '6991', // Övriga avdragsgilla kostnader
expense_bank_fees: '6570', // Bankavgifter
expense_card_fees: '6570', // Kortavgifter
expense_currency_exchange: '7960', // Valutakursförluster
expense_other: '6991', // Övriga avdragsgilla kostnader
// Non-expense categories (for completeness)
income_services: '3001',
income_products: '3001',
income_other: '3900',
private: '2013',
uncategorized: '6991',
}
// Keyword patterns for auto-categorization
const CATEGORY_PATTERNS: Array<{
category: TransactionCategory
patterns: RegExp[]
}> = [
{
category: 'expense_equipment',
patterns: [
/\b(dator|laptop|macbook|ipad|tablet|telefon|iphone|samsung|kamera|camera|mikrofon|microphone|belysning|lighting|stativ|tripod|usb|kabel|adapter|hörlurar|headphones|airpods)\b/i,
/\b(apple|logitech|sony|canon|nikon|rode|elgato|razer)\b/i,
],
},
{
category: 'expense_software',
patterns: [
/\b(adobe|microsoft|spotify|netflix|youtube premium|dropbox|google|icloud|canva|notion|slack|zoom|teams|subscription|prenumeration|licens|license)\b/i,
/\b(app|program|software|saas|cloud)\b/i,
],
},
{
category: 'expense_travel',
patterns: [
/\b(flyg|flight|hotell|hotel|taxi|uber|bolt|tåg|train|sj|arlanda|bromma|landvetter|bensin|fuel|parkering|parking|biljett|ticket)\b/i,
/\b(resa|travel|transport|resekostnad)\b/i,
],
},
{
category: 'expense_office',
patterns: [
/\b(hyra|rent|el|electricity|internet|bredband|vatten|försäkring|insurance|städning|cleaning|kontorsmaterial|papper|toner|skrivare|printer)\b/i,
/\b(ikea|clas ohlson|biltema|staples|lyreco)\b/i,
],
},
{
category: 'expense_marketing',
patterns: [
/\b(reklam|advertising|annons|ad|meta ads|facebook ads|instagram ads|google ads|tiktok ads|influencer|sponsor|kampanj|campaign|pr|press)\b/i,
/\b(marknadsföring|marketing|promotion)\b/i,
],
},
{
category: 'expense_professional_services',
patterns: [
/\b(konsult|consultant|redovisning|accounting|bokföring|bookkeeping|juridik|legal|advokat|lawyer|revisor|auditor)\b/i,
/\b(tjänst|service|arvode|fee)\b/i,
],
},
{
category: 'expense_education',
patterns: [
/\b(kurs|course|utbildning|training|bok|book|seminar|konferens|conference|workshop|webinar|certifiering|certification)\b/i,
/\b(lärande|learning|studie|study)\b/i,
],
},
]
/**
* Get the TransactionCategory from AI-suggested category
*/
export function mapSuggestedCategory(suggestedCategory: string | null): TransactionCategory | null {
if (!suggestedCategory) return null
return CATEGORY_MAPPING[suggestedCategory] || null
}
/**
* Get BAS account for a category
*/
export function getBASAccount(category: TransactionCategory): string {
return BAS_ACCOUNT_MAPPING[category] || '6991'
}
/**
* Auto-categorize a line item based on description
*/
export function categorizeLineItem(description: string): {
category: TransactionCategory | null
confidence: number
} {
const normalizedDescription = description.toLowerCase()
for (const { category, patterns } of CATEGORY_PATTERNS) {
for (const pattern of patterns) {
if (pattern.test(normalizedDescription)) {
return {
category,
confidence: 0.7, // Pattern-based confidence
}
}
}
}
return {
category: null,
confidence: 0,
}
}
/**
* Process extracted line items and add category suggestions
*/
export function processLineItems(
lineItems: ExtractedLineItem[]
): Array<ExtractedLineItem & { category: TransactionCategory | null; basAccount: string | null }> {
return lineItems.map((item) => {
// First try the AI-suggested category
let category = mapSuggestedCategory(item.suggestedCategory)
let confidence = item.confidence || 0.8
// If no AI suggestion, try pattern matching
if (!category) {
const patternResult = categorizeLineItem(item.description)
category = patternResult.category
confidence = patternResult.confidence
}
return {
...item,
category,
basAccount: category ? getBASAccount(category) : null,
confidence,
}
})
}
/**
* Calculate split amounts for mixed business/private receipt
*/
export function calculateReceiptSplit(
lineItems: Array<{ lineTotal: number; is_business: boolean | null }>
): {
businessTotal: number
privateTotal: number
unclassifiedTotal: number
businessPercentage: number
} {
let businessTotal = 0
let privateTotal = 0
let unclassifiedTotal = 0
for (const item of lineItems) {
if (item.is_business === true) {
businessTotal += item.lineTotal
} else if (item.is_business === false) {
privateTotal += item.lineTotal
} else {
unclassifiedTotal += item.lineTotal
}
}
const total = businessTotal + privateTotal + unclassifiedTotal
const businessPercentage = total > 0 ? (businessTotal / total) * 100 : 0
return {
businessTotal: Math.round(businessTotal * 100) / 100,
privateTotal: Math.round(privateTotal * 100) / 100,
unclassifiedTotal: Math.round(unclassifiedTotal * 100) / 100,
businessPercentage: Math.round(businessPercentage * 10) / 10,
}
}
/**
* Aggregate line items by category for summary display
*/
export function aggregateByCategory(
lineItems: Array<{ lineTotal: number; category: TransactionCategory | null; is_business: boolean | null }>
): Array<{ category: TransactionCategory | null; total: number; count: number }> {
const aggregation = new Map<TransactionCategory | null, { total: number; count: number }>()
for (const item of lineItems) {
// Only aggregate business items
if (item.is_business !== true) continue
const existing = aggregation.get(item.category) || { total: 0, count: 0 }
aggregation.set(item.category, {
total: existing.total + item.lineTotal,
count: existing.count + 1,
})
}
return Array.from(aggregation.entries())
.map(([category, data]) => ({
category,
total: Math.round(data.total * 100) / 100,
count: data.count,
}))
.sort((a, b) => b.total - a.total)
}
/**
* Generate default line item classifications based on receipt flags
*/
export function getDefaultClassification(
isRestaurant: boolean,
isSystembolaget: boolean
): {
defaultIsBusiness: boolean | null
requiresReview: boolean
warningMessage: string | null
} {
if (isSystembolaget) {
return {
defaultIsBusiness: false, // Default to private for Systembolaget
requiresReview: true,
warningMessage: 'Alkohol från Systembolaget är normalt en privat utgift. Om det är representation, ange syfte och antal personer.',
}
}
if (isRestaurant) {
return {
defaultIsBusiness: null, // Need user input
requiresReview: true,
warningMessage: 'Restaurangbesök kräver uppgift om antal personer och syfte för att räknas som representation.',
}
}
return {
defaultIsBusiness: null, // User decides per item
requiresReview: false,
warningMessage: null,
}
}
/**
* Category labels in Swedish
*/
export const CATEGORY_LABELS: Record<TransactionCategory, string> = {
expense_equipment: 'Utrustning',
expense_software: 'Programvara',
expense_travel: 'Resa',
expense_office: 'Kontor',
expense_marketing: 'Marknadsföring',
expense_professional_services: 'Konsulttjänster',
expense_education: 'Utbildning',
expense_bank_fees: 'Bankavgift',
expense_card_fees: 'Kortavgift',
expense_currency_exchange: 'Valutaväxling',
expense_other: 'Övrigt',
income_services: 'Tjänsteintäkt',
income_products: 'Varuintäkt',
income_other: 'Övrig intäkt',
private: 'Privat',
uncategorized: 'Ej bokförd',
}
/**
* Get expense categories only (for dropdown)
*/
export function getExpenseCategories(): Array<{ value: TransactionCategory; label: string }> {
return Object.entries(CATEGORY_LABELS)
.filter(([key]) => key.startsWith('expense_'))
.map(([value, label]) => ({
value: value as TransactionCategory,
label,
}))
}