* Refactor bookkeeping error handling and introduce new error classes - Introduced new error classes for better error categorization: - JournalEntryNotBalancedError - FiscalPeriodNotFoundError - EntryDateOutsideFiscalPeriodError - JournalEntryNotFoundError - CannotReverseNonPostedError - CannotCorrectNonPostedError - EntryAlreadyReversedError - CurrencyRevaluationAlreadyExistsError - InvalidMappingResultError - BookkeepingDatabaseError - Updated existing functions in engine.ts and transaction-entries.ts to throw specific errors instead of generic ones. - Enhanced error response handling in get-error-message.ts to provide localized messages for new error types. - Added unit tests for new error classes and error handling functions to ensure correctness and coverage. * feat(ai): implement AI proposal application and persistence - Add apply.ts to handle the application of AI proposals, including match and booking steps. - Introduce persist.ts for inserting and managing AI requests and proposals, ensuring unique constraints. - Create re-validate.ts for validating proposals before acceptance, checking for stale conditions. - Define database migrations for ai_requests and ai_proposals tables, including constraints and indexes. - Enhance journal_entries with AI provenance tracking, linking entries to AI proposals. - Update categorization_templates to distinguish AI-corrected templates. - Add company settings for toggling AI flow and managing backfill processes. - Extend processing_history to include AI-related events for better tracking. * feat: add uncategorized transactions API and UI for transaction selection - Implemented a new API endpoint for fetching uncategorized transactions with pagination and filtering options. - Created ChangeTransactionDialog component for selecting alternative transactions based on AI proposals. - Developed ReceiptDetailDialog to display detailed information about receipts, including upload functionality. - Added TransactionDetailDialog for viewing transaction details with links to the transaction list. - Introduced receipt quality assessment logic to evaluate extracted receipt data. - Implemented feature flagging for the AI bookkeeping agent to control availability in different environments. * feat: add manual receipt extraction dialog and integrate AWS Textract for expense analysis - Added ManualExtractDialog component for user input when AI fails to extract receipt data. - Implemented ReceiptsList component to manage and display uploaded receipts, including upload and rescan functionalities. - Introduced Textract integration for analyzing expenses, extracting fields like total, vendor, and date. - Updated package.json to include @aws-sdk/client-textract dependency. * fix(ai): handle livsmedel VAT transition (12% → 6%) in booking prompt and re-validate guard Add date-aware guidance to BOOKING_SYSTEM_PROMPT for the temporary livsmedel VAT cut (Prop. 2025/26:55, 2026-04-01 to 2027-12-31), with restaurang/servering carve-out at 12%. Add a re-validate safety net that rejects clearly-stale rate labels for grocery-chain merchants relative to the entry date. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
379 lines
12 KiB
TypeScript
379 lines
12 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from 'react'
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from '@/components/ui/dialog'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Badge } from '@/components/ui/badge'
|
|
import { Loader2, Search, Sparkles, Check } from 'lucide-react'
|
|
import { formatCurrency, formatDate, cn } from '@/lib/utils'
|
|
import type { AIProposal, MatchProposalPayload } from '@/types'
|
|
|
|
type ChangeSource = 'user_alternative' | 'user_manual' | 'ai_regenerated'
|
|
|
|
interface ChangeTransactionDialogProps {
|
|
open: boolean
|
|
onOpenChange: (open: boolean) => void
|
|
proposal: AIProposal
|
|
receiptTotal: number | null
|
|
receiptDate: string | null
|
|
onChanged: () => void
|
|
}
|
|
|
|
interface PickerTx {
|
|
id: string
|
|
date: string
|
|
description: string | null
|
|
amount: number
|
|
currency: string | null
|
|
merchant_name: string | null
|
|
}
|
|
|
|
export default function ChangeTransactionDialog({
|
|
open,
|
|
onOpenChange,
|
|
proposal,
|
|
receiptTotal,
|
|
receiptDate,
|
|
onChanged,
|
|
}: ChangeTransactionDialogProps) {
|
|
const payload = proposal.proposal_json as MatchProposalPayload
|
|
const alternatives = payload.alternatives ?? []
|
|
|
|
const [selected, setSelected] = useState<{ id: string; source: ChangeSource } | null>(null)
|
|
const [saving, setSaving] = useState(false)
|
|
const [showAll, setShowAll] = useState(alternatives.length === 0)
|
|
const [search, setSearch] = useState('')
|
|
const [allTx, setAllTx] = useState<PickerTx[]>([])
|
|
const [alternativeTx, setAlternativeTx] = useState<Record<string, PickerTx>>({})
|
|
const [loadingList, setLoadingList] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
// Reset when dialog opens/closes or proposal changes.
|
|
useEffect(() => {
|
|
if (!open) {
|
|
setSelected(null)
|
|
setSearch('')
|
|
setError(null)
|
|
}
|
|
}, [open, proposal.id])
|
|
|
|
// Fetch human-readable context for the AI's alternatives so the user
|
|
// can compare description/amount/date, not bare UUIDs.
|
|
useEffect(() => {
|
|
if (!open || alternatives.length === 0) return
|
|
const ids = alternatives.map((a) => a.transaction_id)
|
|
fetch(`/api/transactions/uncategorized?limit=50&offset=0`)
|
|
.then((r) => r.json())
|
|
.then((body) => {
|
|
const list: PickerTx[] = body?.data?.transactions ?? []
|
|
const map: Record<string, PickerTx> = {}
|
|
for (const tx of list) if (ids.includes(tx.id)) map[tx.id] = tx
|
|
setAlternativeTx(map)
|
|
})
|
|
.catch(() => { /* alternatives still show with reasoning text */ })
|
|
}, [open, alternatives])
|
|
|
|
// Fetch the full picker list when the user opens "Visa alla".
|
|
const loadAllTransactions = useCallback(async () => {
|
|
setLoadingList(true)
|
|
setError(null)
|
|
try {
|
|
const params = new URLSearchParams()
|
|
params.set('limit', '30')
|
|
if (search) params.set('search', search)
|
|
if (receiptTotal) {
|
|
params.set('amount_center', String(-Math.abs(receiptTotal)))
|
|
params.set('amount_window', String(Math.max(5, Math.abs(receiptTotal) * 0.1)))
|
|
}
|
|
if (receiptDate) {
|
|
params.set('date_center', receiptDate)
|
|
params.set('date_window', '60')
|
|
}
|
|
const res = await fetch(`/api/transactions/uncategorized?${params.toString()}`)
|
|
const body = await res.json()
|
|
if (!res.ok) {
|
|
setError(body?.error ?? 'Kunde inte hämta transaktioner')
|
|
return
|
|
}
|
|
setAllTx(body?.data?.transactions ?? [])
|
|
} catch {
|
|
setError('Nätverksfel')
|
|
} finally {
|
|
setLoadingList(false)
|
|
}
|
|
}, [search, receiptTotal, receiptDate])
|
|
|
|
// Refetch whenever the "show all" pane is open and the search changes.
|
|
useEffect(() => {
|
|
if (!open || !showAll) return
|
|
loadAllTransactions()
|
|
}, [open, showAll, loadAllTransactions])
|
|
|
|
const handleConfirm = async () => {
|
|
if (!selected) return
|
|
setSaving(true)
|
|
setError(null)
|
|
try {
|
|
const res = await fetch(`/api/ai/proposals/${proposal.id}/change-match`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
version: proposal.version,
|
|
matched_transaction_id: selected.id,
|
|
source: selected.source,
|
|
}),
|
|
})
|
|
const body = await res.json()
|
|
if (!res.ok) {
|
|
setError(body?.error ?? 'Kunde inte uppdatera förslaget')
|
|
setSaving(false)
|
|
return
|
|
}
|
|
onChanged()
|
|
onOpenChange(false)
|
|
} catch {
|
|
setError('Nätverksfel')
|
|
} finally {
|
|
setSaving(false)
|
|
}
|
|
}
|
|
|
|
const currentMatchId = payload.matched_transaction_id
|
|
|
|
const alternativesWithContext = useMemo(
|
|
() =>
|
|
alternatives.map((alt) => ({
|
|
...alt,
|
|
tx: alternativeTx[alt.transaction_id] ?? null,
|
|
})),
|
|
[alternatives, alternativeTx]
|
|
)
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="max-w-2xl max-h-[85vh] overflow-hidden flex flex-col">
|
|
<DialogHeader>
|
|
<DialogTitle>Byt transaktion</DialogTitle>
|
|
<DialogDescription>
|
|
Välj en annan transaktion att koppla kvittot till. Du kan välja bland AI:ns
|
|
alternativ eller söka i alla okategoriserade transaktioner.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<div className="flex-1 overflow-y-auto space-y-6 py-2">
|
|
{/* AI alternatives */}
|
|
{alternatives.length > 0 && (
|
|
<section>
|
|
<h3 className="text-xs font-medium uppercase tracking-wider text-muted-foreground mb-2 flex items-center gap-2">
|
|
<Sparkles className="h-3.5 w-3.5" />
|
|
AI:ns alternativ ({alternatives.length})
|
|
</h3>
|
|
<div className="space-y-2">
|
|
{alternativesWithContext.map((alt) => (
|
|
<AlternativeRow
|
|
key={alt.transaction_id}
|
|
tx={alt.tx}
|
|
transactionId={alt.transaction_id}
|
|
confidence={alt.confidence}
|
|
reasoning={alt.reasoning}
|
|
isCurrent={alt.transaction_id === currentMatchId}
|
|
isSelected={selected?.id === alt.transaction_id}
|
|
onSelect={() =>
|
|
setSelected({ id: alt.transaction_id, source: 'user_alternative' })
|
|
}
|
|
/>
|
|
))}
|
|
</div>
|
|
</section>
|
|
)}
|
|
|
|
{/* Toggle manual picker */}
|
|
{alternatives.length > 0 && !showAll && (
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => setShowAll(true)}
|
|
className="w-full"
|
|
>
|
|
<Search className="h-3.5 w-3.5 mr-2" />
|
|
Visa alla transaktioner
|
|
</Button>
|
|
)}
|
|
|
|
{/* Manual picker */}
|
|
{showAll && (
|
|
<section>
|
|
<h3 className="text-xs font-medium uppercase tracking-wider text-muted-foreground mb-2">
|
|
Alla okategoriserade transaktioner
|
|
</h3>
|
|
<div className="relative mb-2">
|
|
<Search className="absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground" />
|
|
<Input
|
|
placeholder="Sök beskrivning eller handlare…"
|
|
value={search}
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
className="pl-8"
|
|
/>
|
|
</div>
|
|
{receiptTotal && (
|
|
<p className="text-xs text-muted-foreground mb-2">
|
|
Filtrerat på belopp runt {formatCurrency(-Math.abs(receiptTotal), 'SEK')} och datum runt{' '}
|
|
{receiptDate ? formatDate(receiptDate) : '—'}. Rensa sökrutan för att se fler.
|
|
</p>
|
|
)}
|
|
{loadingList ? (
|
|
<div className="flex items-center justify-center py-8 text-muted-foreground">
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
</div>
|
|
) : allTx.length === 0 ? (
|
|
<p className="text-sm text-muted-foreground text-center py-6">
|
|
Inga matchande transaktioner
|
|
</p>
|
|
) : (
|
|
<div className="space-y-1">
|
|
{allTx.map((tx) => (
|
|
<PickerRow
|
|
key={tx.id}
|
|
tx={tx}
|
|
isCurrent={tx.id === currentMatchId}
|
|
isSelected={selected?.id === tx.id}
|
|
onSelect={() => setSelected({ id: tx.id, source: 'user_manual' })}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
</section>
|
|
)}
|
|
</div>
|
|
|
|
{error && (
|
|
<p className="text-sm text-destructive">{error}</p>
|
|
)}
|
|
|
|
<DialogFooter>
|
|
<Button variant="outline" onClick={() => onOpenChange(false)} disabled={saving}>
|
|
Avbryt
|
|
</Button>
|
|
<Button onClick={handleConfirm} disabled={!selected || saving}>
|
|
{saving ? (
|
|
<>
|
|
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
|
Sparar…
|
|
</>
|
|
) : (
|
|
'Använd denna transaktion'
|
|
)}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|
|
|
|
function AlternativeRow({
|
|
tx,
|
|
transactionId,
|
|
confidence,
|
|
reasoning,
|
|
isCurrent,
|
|
isSelected,
|
|
onSelect,
|
|
}: {
|
|
tx: PickerTx | null
|
|
transactionId: string
|
|
confidence: number
|
|
reasoning: string
|
|
isCurrent: boolean
|
|
isSelected: boolean
|
|
onSelect: () => void
|
|
}) {
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={onSelect}
|
|
disabled={isCurrent}
|
|
className={cn(
|
|
'w-full text-left rounded border p-3 transition-colors',
|
|
isSelected && 'border-primary bg-primary/5',
|
|
!isSelected && 'hover:bg-muted/40',
|
|
isCurrent && 'opacity-50 cursor-not-allowed'
|
|
)}
|
|
>
|
|
<div className="flex items-start justify-between gap-3 mb-1">
|
|
<div className="flex-1 min-w-0">
|
|
{tx ? (
|
|
<>
|
|
<div className="flex items-baseline justify-between gap-2">
|
|
<span className="text-sm truncate">{tx.description ?? 'Okänd'}</span>
|
|
<span className="text-sm tabular-nums font-medium">
|
|
{formatCurrency(tx.amount, tx.currency ?? 'SEK')}
|
|
</span>
|
|
</div>
|
|
<div className="text-xs text-muted-foreground">{formatDate(tx.date)}</div>
|
|
</>
|
|
) : (
|
|
<div className="text-xs text-muted-foreground font-mono">
|
|
{transactionId.slice(0, 8)}…
|
|
</div>
|
|
)}
|
|
</div>
|
|
<div className="flex items-center gap-1 flex-shrink-0">
|
|
{isCurrent && <Badge variant="outline" className="text-xs">Nuvarande</Badge>}
|
|
<Badge className="text-xs">{Math.round(confidence * 100)}%</Badge>
|
|
{isSelected && <Check className="h-4 w-4 text-primary" />}
|
|
</div>
|
|
</div>
|
|
<p className="text-xs text-muted-foreground italic">“{reasoning}”</p>
|
|
</button>
|
|
)
|
|
}
|
|
|
|
function PickerRow({
|
|
tx,
|
|
isCurrent,
|
|
isSelected,
|
|
onSelect,
|
|
}: {
|
|
tx: PickerTx
|
|
isCurrent: boolean
|
|
isSelected: boolean
|
|
onSelect: () => void
|
|
}) {
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={onSelect}
|
|
disabled={isCurrent}
|
|
className={cn(
|
|
'w-full text-left rounded border p-2.5 transition-colors',
|
|
isSelected && 'border-primary bg-primary/5',
|
|
!isSelected && 'hover:bg-muted/40',
|
|
isCurrent && 'opacity-50 cursor-not-allowed'
|
|
)}
|
|
>
|
|
<div className="flex items-baseline justify-between gap-2">
|
|
<span className="text-sm truncate">{tx.description ?? 'Okänd'}</span>
|
|
<span className="text-sm tabular-nums font-medium">
|
|
{formatCurrency(tx.amount, tx.currency ?? 'SEK')}
|
|
</span>
|
|
</div>
|
|
<div className="flex items-center justify-between text-xs text-muted-foreground mt-0.5">
|
|
<span>{formatDate(tx.date)}</span>
|
|
<div className="flex items-center gap-1">
|
|
{isCurrent && <Badge variant="outline" className="text-xs">Nuvarande</Badge>}
|
|
{isSelected && <Check className="h-4 w-4 text-primary" />}
|
|
</div>
|
|
</div>
|
|
</button>
|
|
)
|
|
}
|
|
|