* 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>
178 lines
5.8 KiB
TypeScript
178 lines
5.8 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
} from '@/components/ui/dialog'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Label } from '@/components/ui/label'
|
|
import { useToast } from '@/components/ui/use-toast'
|
|
import { Loader2 } from 'lucide-react'
|
|
import type { ReceiptRowWithPreview } from '@/app/(dashboard)/receipts/page'
|
|
|
|
// Fallback when AI can't read the image. The source document stays attached;
|
|
// we just let the user type the fields AI would have extracted so the row
|
|
// can move to 'ready' and be matched to a bank transaction.
|
|
export default function ManualExtractDialog({
|
|
row,
|
|
onClose,
|
|
onSaved,
|
|
}: {
|
|
row: ReceiptRowWithPreview
|
|
onClose: () => void
|
|
onSaved: () => void
|
|
}) {
|
|
const data = row.extracted_data as {
|
|
merchant?: { name?: string | null }
|
|
receipt?: { date?: string | null; currency?: string | null }
|
|
totals?: { total?: number | null; vatAmount?: number | null }
|
|
} | null
|
|
const [merchant, setMerchant] = useState(data?.merchant?.name ?? '')
|
|
const [date, setDate] = useState(data?.receipt?.date ?? new Date().toISOString().slice(0, 10))
|
|
const [total, setTotal] = useState<string>(data?.totals?.total != null ? String(data.totals.total) : '')
|
|
const [vatAmount, setVatAmount] = useState<string>(data?.totals?.vatAmount != null ? String(data.totals.vatAmount) : '')
|
|
const [currency, setCurrency] = useState(data?.receipt?.currency ?? 'SEK')
|
|
const [saving, setSaving] = useState(false)
|
|
const { toast } = useToast()
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault()
|
|
const totalNum = Number(total)
|
|
if (!merchant.trim() || !date || !Number.isFinite(totalNum) || totalNum <= 0) {
|
|
toast({
|
|
title: 'Kontrollera fälten',
|
|
description: 'Butiksnamn, datum och giltigt totalbelopp krävs.',
|
|
variant: 'destructive',
|
|
})
|
|
return
|
|
}
|
|
const vatNum = vatAmount.trim() === '' ? null : Number(vatAmount)
|
|
if (vatNum !== null && !Number.isFinite(vatNum)) {
|
|
toast({ title: 'Ogiltigt momsbelopp', variant: 'destructive' })
|
|
return
|
|
}
|
|
|
|
setSaving(true)
|
|
try {
|
|
const res = await fetch('/api/extensions/ext/invoice-inbox/manual-extract', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
inbox_item_id: row.id,
|
|
merchant: merchant.trim(),
|
|
date,
|
|
total: totalNum,
|
|
currency,
|
|
vat_amount: vatNum,
|
|
}),
|
|
})
|
|
const body = await res.json()
|
|
if (!res.ok) {
|
|
toast({ title: 'Kunde inte spara', description: body.error, variant: 'destructive' })
|
|
return
|
|
}
|
|
toast({ title: 'Kvitto sparat' })
|
|
onSaved()
|
|
} finally {
|
|
setSaving(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open onOpenChange={(open) => { if (!open) onClose() }}>
|
|
<DialogContent className="sm:max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>Skriv in kvittouppgifter</DialogTitle>
|
|
<DialogDescription>
|
|
Använd det här när AI inte kan läsa bilden. Bilden behålls som underlag —
|
|
du anger bara siffrorna så kan kvittot matchas mot en banktransaktion.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="merchant">Butik / leverantör</Label>
|
|
<Input
|
|
id="merchant"
|
|
value={merchant}
|
|
onChange={(e) => setMerchant(e.target.value)}
|
|
placeholder="t.ex. ICA Maxi"
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="date">Datum</Label>
|
|
<Input
|
|
id="date"
|
|
type="date"
|
|
value={date}
|
|
onChange={(e) => setDate(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="currency">Valuta</Label>
|
|
<Input
|
|
id="currency"
|
|
value={currency}
|
|
onChange={(e) => setCurrency(e.target.value.toUpperCase().slice(0, 3))}
|
|
maxLength={3}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="total">Totalbelopp</Label>
|
|
<Input
|
|
id="total"
|
|
type="number"
|
|
step="0.01"
|
|
inputMode="decimal"
|
|
value={total}
|
|
onChange={(e) => setTotal(e.target.value)}
|
|
placeholder="0.00"
|
|
/>
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="vat">Varav moms (valfritt)</Label>
|
|
<Input
|
|
id="vat"
|
|
type="number"
|
|
step="0.01"
|
|
inputMode="decimal"
|
|
value={vatAmount}
|
|
onChange={(e) => setVatAmount(e.target.value)}
|
|
placeholder="0.00"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<DialogFooter className="gap-2">
|
|
<Button type="button" variant="ghost" onClick={onClose} disabled={saving}>
|
|
Avbryt
|
|
</Button>
|
|
<Button type="submit" disabled={saving}>
|
|
{saving ? (
|
|
<>
|
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
|
Sparar…
|
|
</>
|
|
) : (
|
|
'Spara'
|
|
)}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|