* 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>
156 lines
5.9 KiB
TypeScript
156 lines
5.9 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Label } from '@/components/ui/label'
|
|
import type { AIProposal, BookingProposalLine, BookingProposalPayload } from '@/types'
|
|
|
|
interface EditBookingDialogProps {
|
|
proposal: AIProposal
|
|
onClose: () => void
|
|
onSubmit: (edits: BookingProposalPayload) => Promise<void>
|
|
}
|
|
|
|
/**
|
|
* Minimal in-place editor for a booking proposal.
|
|
*
|
|
* v1 lets the user change account numbers + amounts per line; the full
|
|
* AccountCombobox + VatTreatmentSelect UX comes in a polish pass. The
|
|
* point of this dialog is proving the edit-accept-learning-prompt path
|
|
* works end-to-end before we invest in the richer form.
|
|
*/
|
|
export default function EditBookingDialog({ proposal, onClose, onSubmit }: EditBookingDialogProps) {
|
|
const original = proposal.proposal_json as BookingProposalPayload
|
|
const [lines, setLines] = useState<BookingProposalLine[]>(original.lines)
|
|
const [description, setDescription] = useState(original.description)
|
|
const [submitting, setSubmitting] = useState(false)
|
|
|
|
const totalDebit = lines.reduce((s, l) => s + (Number(l.debit_amount) || 0), 0)
|
|
const totalCredit = lines.reduce((s, l) => s + (Number(l.credit_amount) || 0), 0)
|
|
const balanced = Math.abs(totalDebit - totalCredit) < 0.005 && totalDebit > 0
|
|
|
|
const updateLine = (index: number, patch: Partial<BookingProposalLine>) => {
|
|
setLines((prev) => prev.map((l, i) => (i === index ? { ...l, ...patch } : l)))
|
|
}
|
|
|
|
const handleSubmit = async () => {
|
|
if (!balanced) return
|
|
setSubmitting(true)
|
|
try {
|
|
await onSubmit({
|
|
...original,
|
|
lines,
|
|
description,
|
|
})
|
|
} finally {
|
|
setSubmitting(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open onOpenChange={(open) => !open && onClose()}>
|
|
<DialogContent className="max-w-2xl">
|
|
<DialogHeader>
|
|
<DialogTitle>Redigera bokföringsförslag</DialogTitle>
|
|
</DialogHeader>
|
|
|
|
<div className="space-y-4">
|
|
<div>
|
|
<Label htmlFor="description">Beskrivning</Label>
|
|
<Input
|
|
id="description"
|
|
value={description}
|
|
onChange={(e) => setDescription(e.target.value)}
|
|
className="mt-1"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<Label>Rader</Label>
|
|
<div className="mt-1 border rounded">
|
|
<table className="w-full text-sm">
|
|
<thead className="text-xs text-muted-foreground bg-muted/40">
|
|
<tr>
|
|
<th className="text-left p-2 font-normal">Konto</th>
|
|
<th className="text-left p-2 font-normal">Beskrivning</th>
|
|
<th className="text-right p-2 font-normal">Debet</th>
|
|
<th className="text-right p-2 font-normal">Kredit</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{lines.map((line, i) => (
|
|
<tr key={i} className="border-t">
|
|
<td className="p-2">
|
|
<Input
|
|
value={line.account_number}
|
|
onChange={(e) => updateLine(i, { account_number: e.target.value })}
|
|
className="h-8 font-mono w-20"
|
|
maxLength={4}
|
|
/>
|
|
</td>
|
|
<td className="p-2">
|
|
<Input
|
|
value={line.description}
|
|
onChange={(e) => updateLine(i, { description: e.target.value })}
|
|
className="h-8"
|
|
/>
|
|
</td>
|
|
<td className="p-2">
|
|
<Input
|
|
type="number"
|
|
step="0.01"
|
|
value={line.debit_amount || ''}
|
|
onChange={(e) =>
|
|
updateLine(i, { debit_amount: parseFloat(e.target.value) || 0 })
|
|
}
|
|
className="h-8 text-right tabular-nums w-24 ml-auto"
|
|
/>
|
|
</td>
|
|
<td className="p-2">
|
|
<Input
|
|
type="number"
|
|
step="0.01"
|
|
value={line.credit_amount || ''}
|
|
onChange={(e) =>
|
|
updateLine(i, { credit_amount: parseFloat(e.target.value) || 0 })
|
|
}
|
|
className="h-8 text-right tabular-nums w-24 ml-auto"
|
|
/>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
<tfoot>
|
|
<tr className="border-t text-xs">
|
|
<td colSpan={2} className="p-2 text-muted-foreground">
|
|
Summa
|
|
</td>
|
|
<td className="p-2 text-right tabular-nums">{totalDebit.toFixed(2)}</td>
|
|
<td className="p-2 text-right tabular-nums">{totalCredit.toFixed(2)}</td>
|
|
</tr>
|
|
</tfoot>
|
|
</table>
|
|
</div>
|
|
{!balanced && (
|
|
<p className="text-xs text-destructive mt-1">
|
|
Debet och kredit måste summera till samma belopp.
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
<div className="flex justify-end gap-2">
|
|
<Button variant="ghost" onClick={onClose} disabled={submitting}>
|
|
Avbryt
|
|
</Button>
|
|
<Button onClick={handleSubmit} disabled={!balanced || submitting}>
|
|
{submitting ? 'Bokför…' : 'Godkänn med ändringar'}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|