36e3f6ceb0
* style(design): normalize daily-operator flows to locked design system Sweep the dashboard, transactions, reconciliation, invoicing and supplier flows for design-system violations (.claude/rules/design.md): - font-medium removed from Hedvig display headings/numerals - font-mono -> tabular-nums on monetary values (voucher ids stay mono) - raw Tailwind status colors -> Badge variants / muted-alert pattern - semantic colors removed from chrome backgrounds (deadline widgets, icon halos) - hand-rolled skeletons/empty-states -> Skeleton / EmptyState primitives - opacity-suffixed borders, the invisible warning-foreground count color, and shadow-sm/rounded-xl on non-overlay surfaces normalized The four files that also received UX changes carry their token fixes in the following commit. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(ux): clearer dashboard CTA, match confidence, AI provenance, invoice actions Four high-impact UX fixes from the design critique (these files also carry their design-system token normalization): - Dashboard: render the next-best-action hero for every agent-built company, not only 'slim' nav density, so there is always one obvious next step instead of four equal-weight metric tiles. - Reconciliation: surface the match engine's 0-1 confidence as a graded strength badge (Stark / Trolig / Svag traff) in the shared verifikat picker rows and selected chip; drop the uninformative binary "Foreslagen traff" badge from the match dialog. - Supplier inbox: show AI-filled provenance per extracted field (a success dot that clears once the user verifies/edits the value) so misparsed amounts/dates get proofread before they post to an immutable verifikat. - Invoice detail: keep each status's primary action only in the header row; the sidebar "Status actions" card now holds secondary/reversible actions only (makulera, ta bort, skapa kreditnota, manual-send alternative), removing the duplicated CTAs and closing a viewer-permission gap on the old sidebar buttons. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * refactor(ux): Tier-B medium design-critique fixes across daily-operator flows - Reconciliation: standardise the match-confirm verb on "Matcha" (was "Koppla" in MatchVoucherDialog) and replace the hand-rolled date <input>s in the bank reconciliation view with the Input primitive. - Supplier inbox: fold the two alternative bookings (Skapa leverantorsfaktura / Bokfor som verifikat) behind a single "Andra satt att bokfora" dropdown so the default path (Matcha mot transaktion) stays the lone primary action. - Duplicate-payment guard: demote the "Skapa ny verifikation anda" escape hatch to a ghost button so the safe "Koppla till befintlig" path dominates. - Onboarding: raise the "start fresh" escape hatch from a muted text link to a visible secondary button; normalise the checklist's off-scale spacing. - Supplier flows: finish the font-mono -> tabular-nums sweep on monetary values in the supplier-invoice detail / create / review surfaces (ids stay mono). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(deadlines): keep overdue rows visually distinct (destructive chrome is allowed) The Tier-A normalization stripped all semantic-color row tints from the deadline widgets, but design.md exempts --destructive ("only --destructive survives in chrome"). Restore a subtle bg-destructive/5 on OVERDUE rows so missed tax/AGI deadlines (-> skattetillagg) stay noticeable in a list scan; action-needed (warning) rows stay clean since warning is data-only. Surfaced by the Swedish compliance review on #741. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(reconciliation): hide match-strength badge on already-matched verifikat Per the Swedish compliance review on #741: a green "Stark traff" confidence badge rendered alongside "Redan matchad" could visually nudge an accidental double-match of a posted verifikat (a BFL 5 kap audit-trail concern). Suppress the strength badge when linked_transaction_count > 0 so "Redan matchad" is the lone signal there; N:1 matching stays an explicit opt-in. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
301 lines
12 KiB
TypeScript
301 lines
12 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useEffect, useState } from 'react'
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
} from '@/components/ui/dialog'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Switch } from '@/components/ui/switch'
|
|
import {
|
|
MatchVerifikationPicker,
|
|
type UnlinkedGLLine,
|
|
} from '@/components/reconciliation/MatchVerifikationPicker'
|
|
import { formatCurrency, formatDate } from '@/lib/utils'
|
|
import { formatVoucher } from '@/lib/bookkeeping/voucher-series-resolver'
|
|
import { getErrorMessage } from '@/lib/errors/get-error-message'
|
|
import { useToast } from '@/components/ui/use-toast'
|
|
import { ArrowUpRight, ArrowDownRight, Loader2 } from 'lucide-react'
|
|
import type { TransactionWithInvoice } from './transaction-types'
|
|
import type { CashAccount } from '@/types'
|
|
|
|
interface MatchVoucherDialogProps {
|
|
open: boolean
|
|
onOpenChange: (open: boolean) => void
|
|
transaction: TransactionWithInvoice | null
|
|
/** Called after a successful link. voucherLabel is the picked verifikat's label (e.g. "A-42"). */
|
|
onLinked: (transactionId: string, journalEntryId: string, voucherLabel: string) => void
|
|
}
|
|
|
|
// ±30 days around the transaction date — wide enough to catch a salary or
|
|
// supplier voucher booked a few days off the bank value date, narrow enough to
|
|
// keep the candidate list short. "Visa alla" drops the window entirely.
|
|
const WINDOW_DAYS = 30
|
|
|
|
function shiftDate(isoDate: string, deltaDays: number): string {
|
|
const d = new Date(isoDate)
|
|
if (Number.isNaN(d.getTime())) return isoDate
|
|
d.setDate(d.getDate() + deltaDays)
|
|
return d.toISOString().slice(0, 10)
|
|
}
|
|
|
|
/** Resolve which cash account (BAS ledger number) this transaction reconciles against. */
|
|
function resolveAccount(
|
|
cashAccounts: CashAccount[],
|
|
tx: TransactionWithInvoice,
|
|
): { account: string; fallback: boolean } {
|
|
// 1. Bound row → its own account.
|
|
if (tx.cash_account_id) {
|
|
const bound = cashAccounts.find((a) => a.id === tx.cash_account_id)
|
|
if (bound) return { account: bound.ledger_account, fallback: false }
|
|
}
|
|
// 2. Legacy NULL → the sole enabled account of the transaction's currency.
|
|
const sameCurrency = cashAccounts.filter((a) => a.enabled && a.currency === tx.currency)
|
|
if (sameCurrency.length === 1) return { account: sameCurrency[0].ledger_account, fallback: false }
|
|
// 3. Give up gracefully on 1930 (the default SEK företagskonto).
|
|
return { account: '1930', fallback: true }
|
|
}
|
|
|
|
export function MatchVoucherDialog({
|
|
open,
|
|
onOpenChange,
|
|
transaction,
|
|
onLinked,
|
|
}: MatchVoucherDialogProps) {
|
|
const { toast } = useToast()
|
|
const [glLines, setGlLines] = useState<UnlinkedGLLine[]>([])
|
|
const [selected, setSelected] = useState('')
|
|
const [accountNumber, setAccountNumber] = useState('1930')
|
|
const [accountFallback, setAccountFallback] = useState(false)
|
|
const [loading, setLoading] = useState(false)
|
|
const [submitting, setSubmitting] = useState(false)
|
|
const [wideRange, setWideRange] = useState(false)
|
|
// Opt-in: also surface vouchers already matched to another bank transaction,
|
|
// so several transactions can settle one verifikat (N:1 — a salary run paid in
|
|
// multiple transfers, an invoice paid in instalments).
|
|
const [includeMatched, setIncludeMatched] = useState(false)
|
|
|
|
const loadCandidates = useCallback(
|
|
async (tx: TransactionWithInvoice, wide: boolean, matched: boolean) => {
|
|
setLoading(true)
|
|
try {
|
|
// Resolve the settlement account from the company's cash accounts.
|
|
let account = '1930'
|
|
let fallback = true
|
|
try {
|
|
const caRes = await fetch('/api/cash-accounts')
|
|
const caJson = await caRes.json()
|
|
const accounts = (caJson.data ?? []) as CashAccount[]
|
|
const resolved = resolveAccount(accounts, tx)
|
|
account = resolved.account
|
|
fallback = resolved.fallback
|
|
} catch {
|
|
// Network hiccup — fall back to 1930 and let the user see the note.
|
|
}
|
|
setAccountNumber(account)
|
|
setAccountFallback(fallback)
|
|
|
|
const params = new URLSearchParams()
|
|
params.set('account_number', account)
|
|
params.set('transaction_id', tx.id)
|
|
if (matched) params.set('include_matched', 'true')
|
|
if (!wide) {
|
|
params.set('date_from', shiftDate(tx.date, -WINDOW_DAYS))
|
|
params.set('date_to', shiftDate(tx.date, WINDOW_DAYS))
|
|
}
|
|
|
|
const res = await fetch(`/api/reconciliation/bank/unmatched-entries?${params}`)
|
|
const json = await res.json()
|
|
const lines = (json.data ?? []) as UnlinkedGLLine[]
|
|
setGlLines(lines)
|
|
// Pre-select a strong auto-match (exact/reference/date-range) so the
|
|
// common case is one click. Fuzzy (<0.85) is left for the user to confirm.
|
|
// Auto-select a strong match only when nothing is chosen yet. Toggling
|
|
// "Visa alla datum" reloads with a wider set — it must NOT discard a
|
|
// voucher the user already picked. (selected resets to '' on close.)
|
|
// Never auto-select an already-matched voucher — N:1 must be a
|
|
// deliberate choice, not the default when "visa matchade" is on.
|
|
const top = lines[0]
|
|
setSelected((prev) =>
|
|
prev
|
|
? prev
|
|
: top && (top.confidence ?? 0) >= 0.85 && !(top.linked_transaction_count ?? 0)
|
|
? top.journal_entry_id
|
|
: '',
|
|
)
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
},
|
|
[],
|
|
)
|
|
|
|
// (Re)load whenever the dialog opens for a transaction, the range widens, or
|
|
// the user toggles already-matched vouchers in/out.
|
|
useEffect(() => {
|
|
if (!open || !transaction) return
|
|
void loadCandidates(transaction, wideRange, includeMatched)
|
|
}, [open, transaction, wideRange, includeMatched, loadCandidates])
|
|
|
|
// Reset transient state when the dialog closes so the next open starts clean.
|
|
useEffect(() => {
|
|
if (open) return
|
|
setGlLines([])
|
|
setSelected('')
|
|
setWideRange(false)
|
|
setIncludeMatched(false)
|
|
setAccountFallback(false)
|
|
}, [open])
|
|
|
|
if (!transaction) return null
|
|
|
|
const isIncome = transaction.amount > 0
|
|
const selectedLine = glLines.find((l) => l.journal_entry_id === selected) ?? null
|
|
|
|
async function handleConfirm() {
|
|
if (!transaction || !selected) return
|
|
setSubmitting(true)
|
|
try {
|
|
const res = await fetch('/api/reconciliation/bank/link', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
transaction_id: transaction.id,
|
|
journal_entry_id: selected,
|
|
account_number: accountNumber,
|
|
}),
|
|
})
|
|
const result = await res.json()
|
|
if (!res.ok || result.error) {
|
|
toast({
|
|
title: 'Kunde inte matcha',
|
|
description: getErrorMessage(result, { context: 'transaction', statusCode: res.status }),
|
|
variant: 'destructive',
|
|
})
|
|
return
|
|
}
|
|
const label = selectedLine ? formatVoucher(selectedLine) : ''
|
|
onLinked(transaction.id, selected, label)
|
|
} catch {
|
|
toast({
|
|
title: 'Kunde inte koppla',
|
|
description: 'Ett fel uppstod. Försök igen.',
|
|
variant: 'destructive',
|
|
})
|
|
} finally {
|
|
setSubmitting(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-lg">
|
|
<DialogHeader>
|
|
<DialogTitle>Matcha mot befintlig verifikation</DialogTitle>
|
|
<DialogDescription>
|
|
Koppla bankhändelsen till en verifikation som redan är bokförd (t.ex. en
|
|
lön eller en post importerad från Fortnox). Ingen ny bokföring skapas.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
{/* Transaction summary */}
|
|
<div className="flex items-center gap-3 rounded-lg border border-border bg-secondary/30 px-3 py-2.5 text-sm">
|
|
<span
|
|
className={isIncome ? 'text-success' : 'text-foreground/60'}
|
|
aria-hidden
|
|
>
|
|
{isIncome ? <ArrowUpRight className="h-4 w-4" /> : <ArrowDownRight className="h-4 w-4" />}
|
|
</span>
|
|
<div className="min-w-0 flex-1">
|
|
<p className="truncate font-medium">{transaction.description}</p>
|
|
<p className="text-xs text-muted-foreground tabular-nums">{formatDate(transaction.date)}</p>
|
|
</div>
|
|
<span className={`font-medium tabular-nums ${isIncome ? 'text-success' : ''}`}>
|
|
{isIncome ? '+' : ''}
|
|
{formatCurrency(transaction.amount, transaction.currency)}
|
|
</span>
|
|
</div>
|
|
|
|
{accountFallback && (
|
|
<p className="text-xs text-muted-foreground">
|
|
Avstämning mot 1930. Hör transaktionen till ett annat bankkonto? Stäm av
|
|
det under Rapporter → Bankavstämning.
|
|
</p>
|
|
)}
|
|
|
|
{/* Candidate picker */}
|
|
<div className="space-y-2">
|
|
{loading ? (
|
|
<div className="flex items-center justify-center gap-2 rounded-lg border border-border py-6 text-sm text-muted-foreground">
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
Söker verifikationer…
|
|
</div>
|
|
) : glLines.length === 0 ? (
|
|
<div className="rounded-lg border border-border px-3 py-6 text-center text-sm text-muted-foreground">
|
|
<p>
|
|
{includeMatched
|
|
? `Inga verifikationer på ${accountNumber} i perioden.`
|
|
: `Inga omatchade verifikationer på ${accountNumber} i perioden.`}
|
|
</p>
|
|
</div>
|
|
) : (
|
|
<>
|
|
<MatchVerifikationPicker glLines={glLines} value={selected} onChange={setSelected} inline />
|
|
{(selectedLine?.linked_transaction_count ?? 0) > 0 && (
|
|
<p className="text-xs text-muted-foreground">
|
|
Verifikationen är redan matchad mot {selectedLine?.linked_transaction_count}{' '}
|
|
transaktion{(selectedLine?.linked_transaction_count ?? 0) === 1 ? '' : 'er'}.
|
|
Kopplingen lägger till den här transaktionen också — t.ex. en lön utbetald i
|
|
flera överföringar.
|
|
</p>
|
|
)}
|
|
</>
|
|
)}
|
|
|
|
{/* Discovery affordances — widen the date window, and surface vouchers
|
|
already matched so another transaction can be attached (N:1). */}
|
|
<div className="flex flex-wrap items-center justify-between gap-x-4 gap-y-2 pt-1">
|
|
<label className="flex cursor-pointer items-center gap-2 text-xs text-muted-foreground">
|
|
<Switch
|
|
checked={includeMatched}
|
|
onCheckedChange={setIncludeMatched}
|
|
aria-label="Visa även matchade verifikationer"
|
|
/>
|
|
Visa även matchade verifikationer
|
|
</label>
|
|
{!wideRange && (
|
|
<button
|
|
type="button"
|
|
className="text-xs text-muted-foreground underline underline-offset-2 hover:text-foreground"
|
|
onClick={() => setWideRange(true)}
|
|
>
|
|
Visa alla datum
|
|
</button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<DialogFooter>
|
|
<Button variant="outline" onClick={() => onOpenChange(false)} disabled={submitting}>
|
|
Avbryt
|
|
</Button>
|
|
<Button onClick={handleConfirm} disabled={!selected || submitting}>
|
|
{submitting ? (
|
|
<>
|
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
|
Matchar…
|
|
</>
|
|
) : (
|
|
'Matcha'
|
|
)}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|