Files
accounted/components/transactions/TransactionHistoryList.tsx
T
MattssonandClaude Opus 4.7 980f29dae8 Bug/momsdeklaration skv (#449)
* fix(salary): show birthdate in masked personnummer, hide the 4-digit suffix

Flip the personnummer display format from XXXXXXXX-NNNN to YYYYMMDD-XXXX so
the sensitive 4-digit suffix is hidden while the (public) birthdate stays
visible. Affects the employees list/detail, salary run, payslip PDF, payslip
email, and the MCP server tools (list_employees, get_salary_run). Each call
site now decrypts the stored personnummer before masking.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(transactions): allow deleting unbooked transactions from "Alla transaktioner"

The history list only let users delete via the inbox card; once a category or
mall was picked but the verifikation hadn't been created, the row showed
"Ej bokförd" with no way to remove it. The API already permits delete while
journal_entry_id is null, so the gap was purely a missing UI affordance.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(vat): populate ruta 20-24 for reverse charge + dishonest "Validera OK"

Three connected issues caused Skatteverket to reject momsdeklarationer
with FK004 even after our local "Validera"-knapp returned OK.

1. supplier-invoice-entries booked fiktiv moms (2614/2624/2634 + 2645/2647)
   on reverse-charge invoices but never the underlying basbelopp on 44xx/45xx.
   Ruta 30-32 filled up at SKV while ruta 20-24 stayed at 0 — SKV's FK004
   ("silent netting prohibited", ML 13 kap kräver båda sidor).

   Fix: generateReverseChargeBasisLines in vat-entries.ts emits parallel
   45xx/44xx debit + 4598 motkonto credit per rate group. Engine calls it
   from registration, cash, and credit-note paths. Skipped when the user
   booked the expense directly on a basis account to avoid double-counting.
   4598 added to BAS reference (no migration needed; account_number is
   plain text on journal_entry_lines).

2. rutorToMomsuppgift rounded each ruta independently but computed
   summaMoms from the unrounded ruta49. SKV recomputes the sum from
   integer rutor on their side, so fractional öres caused ±1 SEK drift
   and SKV rejected with FK009.

   Fix: derive summaMoms from the already-rounded VAT-amount rutor.

3. "Validera"-knappen only confirmed SKV's internal arithmetic — a
   declaration with ruta 30-32 populated and ruta 20-24 empty validated
   fine until /utkast hit FK004. Users got a false green light.

   Fix: vat-declaration-checks.ts runs locally before the SKV call,
   blocks Validera/Spara when ERROR-level findings exist, and surfaces
   them in a separate "Lokala kontroller"-section. Success message
   reworded so SKV's OK is no longer presented as filing-ready.

Tests: 4535/4536/4531/4425 lines + 4598 motkonto on EU/non-EU/byggtjänster
RC, credit-note reversal, fractional-öres summaMoms, all four pre-flight
codes (RC_BASIS_MISSING, RC_OUTPUT_MISSING, RC_INPUT_VAT_MISMATCH,
SUMMA_MOMS_DRIFT).

Backfill for already-posted entries follows in the next commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat: add skattekonto matching functionality

- Enhance TransactionInboxCard to display a warning for potential 1930↔1630 transfers.
- Implement match suggestions for skattekonto transactions in the backend.
- Create SkattekontoMatchDialog component for linking skattekonto rows to existing journal entries.
- Develop SkattekontoInboxCard component to handle skattekonto transactions in the inbox.
- Introduce skattekonto-match utility functions for candidate matching and linking.
- Update types to include match suggestions and enriched transaction responses.

* refactor: reorganize skattekonto types and implement bank counterpart matching logic

* docs: update CLAUDE.md to streamline integrations and clarify architecture details

* refactor: enhance reverse charge logic to handle non-basis accounts and prevent double-counting

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 18:04:48 +02:00

450 lines
16 KiB
TypeScript

'use client'
import { useState } from 'react'
import Link from 'next/link'
import { Card, CardContent } from '@/components/ui/card'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { cn, formatCurrency, formatDate } from '@/lib/utils'
import { getCategoryDisplayName } from '@/lib/tax/expense-warnings'
import {
Search,
ArrowUpRight,
ArrowDownRight,
ArrowLeftRight,
Check,
Landmark,
Link2,
FileText,
Loader2,
Trash2,
} from 'lucide-react'
import { TransactionAttachmentIndicator } from './TransactionAttachmentIndicator'
import type { TransactionWithInvoice, HistoryFilter } from './transaction-types'
import type {
SkattekontoTransactionWithSuggestion,
StoredSkattekontoTransaction,
} from '@/types/skatteverket'
type SourceFilter = 'all' | 'bank' | 'skatteverket'
type HistoryRow =
| { source: 'bank'; date: string; data: TransactionWithInvoice }
| { source: 'skatteverket'; date: string; data: SkattekontoTransactionWithSuggestion }
interface TransactionHistoryListProps {
transactions: TransactionWithInvoice[]
skvRows?: SkattekontoTransactionWithSuggestion[]
onOpenMatchDialog: (transaction: TransactionWithInvoice) => void
onOpenCategoryDialog: (transaction: TransactionWithInvoice) => void
onDelete?: (id: string) => void
onSkvBokfor?: (row: StoredSkattekontoTransaction) => void
onSkvMatch?: (row: StoredSkattekontoTransaction) => void
hasMore?: boolean
isLoadingMore?: boolean
onLoadMore?: () => void
}
export default function TransactionHistoryList({
transactions,
skvRows = [],
onOpenMatchDialog,
onOpenCategoryDialog,
onDelete,
onSkvBokfor,
onSkvMatch,
hasMore,
isLoadingMore,
onLoadMore,
}: TransactionHistoryListProps) {
const [searchTerm, setSearchTerm] = useState('')
const [filter, setFilter] = useState<HistoryFilter>('all')
const [sourceFilter, setSourceFilter] = useState<SourceFilter>('all')
// The bank/private filter doesn't apply to SKV rows — they have no
// is_business flag. So when the filter is 'business' or 'private' we
// implicitly hide SKV (it doesn't match either). Source filter narrows
// further if the user picks one explicitly.
const bankFiltered = transactions.filter((t) => {
const matchesSearch = t.description.toLowerCase().includes(searchTerm.toLowerCase())
const matchesFilter =
filter === 'all' ||
(filter === 'business' && t.is_business === true) ||
(filter === 'private' && t.is_business === false)
return matchesSearch && matchesFilter
})
const skvFiltered = skvRows.filter((r) => {
if (filter !== 'all') return false
return r.transaktionstext.toLowerCase().includes(searchTerm.toLowerCase())
})
const merged: HistoryRow[] = []
if (sourceFilter !== 'skatteverket') {
for (const t of bankFiltered) {
merged.push({ source: 'bank', date: t.date, data: t })
}
}
if (sourceFilter !== 'bank') {
for (const r of skvFiltered) {
merged.push({ source: 'skatteverket', date: r.transaktionsdatum, data: r })
}
}
merged.sort((a, b) => {
if (a.date !== b.date) return b.date.localeCompare(a.date)
return a.source === 'bank' ? -1 : 1
})
const showSourceFilter = skvRows.length > 0 && transactions.length > 0
const filtered = merged
return (
<div className="space-y-4">
{/* Search + filter pills */}
<div className="flex flex-col sm:flex-row gap-3">
<div className="relative flex-1">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
placeholder="Sök transaktioner..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="pl-10"
/>
</div>
<div className="flex gap-1.5">
{(['all', 'business', 'private'] as const).map((f) => (
<Button
key={f}
size="sm"
variant={filter === f ? 'default' : 'outline'}
className="h-9"
onClick={() => setFilter(f)}
>
{f === 'all' ? 'Alla' : f === 'business' ? 'Företag' : 'Privat'}
</Button>
))}
</div>
</div>
{showSourceFilter && (
<div className="flex items-center gap-2 text-xs">
<span className="text-muted-foreground">Källa:</span>
{(['all', 'bank', 'skatteverket'] as const).map((s) => (
<button
key={s}
onClick={() => setSourceFilter(s)}
className={cn(
'flex items-center gap-1 rounded-full border px-3 py-1 transition-colors',
sourceFilter === s
? 'border-foreground bg-foreground text-background'
: 'border-border text-muted-foreground hover:text-foreground',
)}
>
{s === 'skatteverket' && <Landmark className="h-3 w-3" />}
{s === 'all' ? 'Alla' : s === 'bank' ? 'Bank' : 'Skatteverket'}
</button>
))}
</div>
)}
{/* Transaction list */}
{filtered.length === 0 ? (
<Card>
<CardContent className="flex flex-col items-center justify-center py-12">
<ArrowLeftRight className="h-12 w-12 text-muted-foreground mb-4" />
<h3 className="text-lg font-medium">Inga transaktioner</h3>
<p className="text-muted-foreground text-center mt-1">
{searchTerm
? 'Inga transaktioner matchar din sökning'
: 'Inga transaktioner att visa med valt filter'}
</p>
</CardContent>
</Card>
) : (
<div className="space-y-2">
{filtered.map((item) =>
item.source === 'bank' ? (
<BankHistoryRow
key={`bank-${item.data.id}`}
transaction={item.data}
onOpenMatchDialog={onOpenMatchDialog}
onOpenCategoryDialog={onOpenCategoryDialog}
onDelete={onDelete}
/>
) : (
<SkattekontoHistoryRow
key={`skv-${item.data.id}`}
row={item.data}
onBokfor={onSkvBokfor}
onMatch={onSkvMatch}
/>
),
)}
{hasMore && onLoadMore && !searchTerm && (
<div className="flex justify-center pt-4">
<Button
variant="outline"
onClick={onLoadMore}
disabled={isLoadingMore}
>
{isLoadingMore ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Laddar...
</>
) : (
'Ladda fler'
)}
</Button>
</div>
)}
</div>
)}
</div>
)
}
function BankHistoryRow({
transaction,
onOpenMatchDialog,
onOpenCategoryDialog,
onDelete,
}: {
transaction: TransactionWithInvoice
onOpenMatchDialog: (transaction: TransactionWithInvoice) => void
onOpenCategoryDialog: (transaction: TransactionWithInvoice) => void
onDelete?: (id: string) => void
}) {
return (
<Card data-tx-id={transaction.id} className="hover:border-primary/50 transition-colors">
<CardContent className="py-4">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div
className={`h-9 w-9 rounded-full flex items-center justify-center flex-shrink-0 ${
transaction.amount > 0
? 'bg-success/10 text-success'
: 'bg-destructive/10 text-destructive'
}`}
>
{transaction.amount > 0 ? (
<ArrowUpRight className="h-5 w-5" />
) : (
<ArrowDownRight className="h-5 w-5" />
)}
</div>
<div>
<div className="flex items-center gap-1.5">
<p className="font-medium">{transaction.description}</p>
<TransactionAttachmentIndicator documentId={transaction.document_id} />
</div>
<div className="flex flex-wrap items-center gap-2 text-sm text-muted-foreground">
<span>{formatDate(transaction.date)}</span>
{transaction.is_business !== null &&
!(
transaction.is_business &&
transaction.category === 'uncategorized' &&
transaction.journal_entry_id
) && (
<>
<span>·</span>
<Badge variant={transaction.is_business ? 'default' : 'secondary'}>
{transaction.is_business
? getCategoryDisplayName(transaction.category)
: 'Privat'}
</Badge>
</>
)}
{transaction.invoice_id && (
<>
<span>·</span>
<Badge variant="outline" className="text-primary border-primary">
<Link2 className="h-3 w-3 mr-1" />
Kopplad till faktura
</Badge>
</>
)}
{transaction.journal_entry_id ? (
<>
<span>·</span>
<Badge variant="outline" className="text-success border-success">
<Check className="h-3 w-3 mr-1" />
Bokförd
</Badge>
</>
) : (
<>
<span>·</span>
<button
type="button"
className="inline-flex items-center rounded-md border border-warning px-2.5 py-0.5 text-xs font-semibold text-warning-foreground hover:bg-warning/10 transition-colors"
onClick={() => onOpenCategoryDialog(transaction)}
>
Ej bokförd
</button>
</>
)}
{transaction.potential_invoice && !transaction.invoice_id && (
<>
<span>·</span>
<button
type="button"
className="inline-flex items-center rounded-md border border-primary px-2.5 py-0.5 text-xs font-semibold text-primary hover:bg-primary/10 transition-colors"
onClick={() => onOpenMatchDialog(transaction)}
>
<FileText className="h-3 w-3 mr-1" />
Möjlig match: Faktura {transaction.potential_invoice.invoice_number}
</button>
</>
)}
</div>
</div>
</div>
<div className="flex items-center gap-3">
{!transaction.journal_entry_id && (
<Button
size="sm"
variant="default"
className="h-10 text-xs"
onClick={() => onOpenCategoryDialog(transaction)}
>
Bokför
</Button>
)}
{!transaction.journal_entry_id && onDelete && (
<Button
size="icon"
variant="ghost"
className="text-muted-foreground hover:text-destructive"
onClick={() => onDelete(transaction.id)}
aria-label="Ta bort transaktion"
>
<Trash2 className="h-4 w-4" />
</Button>
)}
<div className="text-right">
<p className="font-medium tabular-nums">
{transaction.amount > 0 ? '+' : ''}
{formatCurrency(transaction.amount, transaction.currency)}
</p>
{transaction.currency !== 'SEK' && transaction.amount_sek && (
<p className="text-sm text-muted-foreground">
{formatCurrency(transaction.amount_sek)}
</p>
)}
</div>
</div>
</div>
</CardContent>
</Card>
)
}
function SkattekontoHistoryRow({
row,
onBokfor,
onMatch,
}: {
row: SkattekontoTransactionWithSuggestion
onBokfor?: (row: StoredSkattekontoTransaction) => void
onMatch?: (row: StoredSkattekontoTransaction) => void
}) {
const amount = Number(row.belopp_skatteverket)
const isIncome = amount > 0
const isBooked = !!row.journal_entry_id
return (
<Card className="hover:border-primary/50 transition-colors">
<CardContent className="py-4">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div
className={cn(
'h-9 w-9 rounded-full flex items-center justify-center flex-shrink-0',
isIncome ? 'bg-success/10 text-success' : 'bg-destructive/10 text-destructive',
)}
>
{isIncome ? (
<ArrowUpRight className="h-5 w-5" />
) : (
<ArrowDownRight className="h-5 w-5" />
)}
</div>
<div>
<p className="font-medium">{row.transaktionstext}</p>
<div className="flex flex-wrap items-center gap-2 text-sm text-muted-foreground">
<span>{formatDate(row.transaktionsdatum)}</span>
<span>·</span>
<Badge variant="outline" className="gap-1">
<Landmark className="h-3 w-3" />
Skatteverket
</Badge>
{isBooked ? (
<>
<span>·</span>
<Badge variant="outline" className="text-success border-success">
<Check className="h-3 w-3 mr-1" />
Bokförd
</Badge>
</>
) : row.match_suggestion ? (
<>
<span>·</span>
<Badge variant="outline" className="border-warning text-warning">
Möjlig dublett
</Badge>
</>
) : (
<>
<span>·</span>
<Badge variant="outline">Ej bokförd</Badge>
</>
)}
</div>
</div>
</div>
<div className="flex items-center gap-3">
{!isBooked && onMatch && (
<Button
size="sm"
variant={row.match_suggestion ? 'default' : 'outline'}
className="h-10 text-xs"
onClick={() => onMatch(row)}
>
<Link2 className="mr-1 h-3 w-3" />
{row.match_suggestion ? 'Koppla' : 'Matcha'}
</Button>
)}
{!isBooked && !row.match_suggestion && onBokfor && (
<Button
size="sm"
variant="default"
className="h-10 text-xs"
onClick={() => onBokfor(row)}
>
Bokför
</Button>
)}
{isBooked && (
<Button asChild size="sm" variant="ghost" className="h-10 text-xs">
<Link href={`/bookkeeping/${row.journal_entry_id}`}>Visa verifikat</Link>
</Button>
)}
<div className="text-right">
<p
className={cn(
'font-medium tabular-nums',
isIncome && 'text-success',
)}
>
{isIncome ? '+' : ''}
{formatCurrency(amount)}
</p>
</div>
</div>
</div>
</CardContent>
</Card>
)
}