980f29dae8
* 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>
287 lines
12 KiB
TypeScript
287 lines
12 KiB
TypeScript
'use client'
|
|
|
|
import { motion } from 'framer-motion'
|
|
import { Card, CardContent } from '@/components/ui/card'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Badge } from '@/components/ui/badge'
|
|
import { Checkbox } from '@/components/ui/checkbox'
|
|
import { cn, formatCurrency, formatDate } from '@/lib/utils'
|
|
import { AlertCircle, ArrowUpRight, ArrowDownRight, FileText, Loader2, Trash2 } from 'lucide-react'
|
|
import { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider } from '@/components/ui/info-tooltip'
|
|
import { getAccountName, formatAccountWithName } from '@/lib/bookkeeping/client-account-names'
|
|
import { getTemplateById } from '@/lib/bookkeeping/booking-templates'
|
|
import { isCounterpartyTemplateId } from '@/lib/bookkeeping/counterparty-templates'
|
|
import { TransactionAttachmentIndicator } from './TransactionAttachmentIndicator'
|
|
import type { TransactionWithInvoice, CategorizeHandler } from './transaction-types'
|
|
import type { SuggestedCategory, SuggestedTemplate } from '@/lib/transactions/category-suggestions'
|
|
|
|
interface TransactionInboxCardProps {
|
|
transaction: TransactionWithInvoice
|
|
suggestions?: SuggestedCategory[]
|
|
templateSuggestions?: SuggestedTemplate[]
|
|
/** When set, this bank tx looks like the bank side of a 1930↔1630
|
|
* transfer that the user will later see on /skattekonto. Renders a
|
|
* hint warning so the user doesn't book both sides separately. */
|
|
skvCounterpartDate?: string
|
|
processingId: string | null
|
|
isBatchMode: boolean
|
|
isSelected: boolean
|
|
entityType?: string
|
|
onCategorize: CategorizeHandler
|
|
onMarkPrivate: (id: string) => void
|
|
onOpenMatchDialog: (transaction: TransactionWithInvoice) => void
|
|
onOpenCategoryDialog: (transaction: TransactionWithInvoice) => void
|
|
onDelete?: (id: string) => void
|
|
onOpenQuickReview?: (transaction: TransactionWithInvoice, suggestion: SuggestedCategory) => void
|
|
onOpenTemplateReview?: (transaction: TransactionWithInvoice, templateId: string) => void
|
|
onToggleSelect: (id: string) => void
|
|
onAnimationComplete?: (id: string) => void
|
|
}
|
|
|
|
export default function TransactionInboxCard({
|
|
transaction,
|
|
suggestions,
|
|
templateSuggestions,
|
|
skvCounterpartDate,
|
|
processingId,
|
|
isBatchMode,
|
|
isSelected,
|
|
entityType = 'enskild_firma',
|
|
onCategorize,
|
|
onMarkPrivate,
|
|
onOpenMatchDialog,
|
|
onOpenCategoryDialog,
|
|
onDelete,
|
|
onOpenQuickReview,
|
|
onOpenTemplateReview,
|
|
onToggleSelect,
|
|
onAnimationComplete,
|
|
}: TransactionInboxCardProps) {
|
|
const isProcessing = processingId === transaction.id
|
|
const isDisabled = processingId !== null && processingId !== transaction.id
|
|
const isIncome = transaction.amount > 0
|
|
const hasInvoiceMatch = !!transaction.potential_invoice && !transaction.invoice_id
|
|
const hasSupplierInvoiceMatch = !!transaction.potential_supplier_invoice && !transaction.supplier_invoice_id
|
|
const topSuggestion = suggestions?.[0]
|
|
const isUncategorized = transaction.is_business === null && !transaction.journal_entry_id
|
|
const showCheckbox = isBatchMode && isUncategorized
|
|
const isDeletable = !transaction.journal_entry_id
|
|
|
|
function handleSuggestionClick(suggestion: SuggestedCategory) {
|
|
if (onOpenQuickReview) {
|
|
onOpenQuickReview(transaction, suggestion)
|
|
} else {
|
|
onCategorize(transaction.id, true, suggestion.category)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<motion.div
|
|
layout
|
|
initial={{ opacity: 1, scale: 1 }}
|
|
exit={{ opacity: 0, scale: 0.95, x: -16 }}
|
|
transition={{ duration: 0.25, ease: [0.25, 0.46, 0.45, 0.94] }}
|
|
onAnimationComplete={(definition) => {
|
|
// Only call on exit animation
|
|
if (typeof definition === 'object' && 'opacity' in definition && definition.opacity === 0) {
|
|
onAnimationComplete?.(transaction.id)
|
|
}
|
|
}}
|
|
>
|
|
<Card
|
|
data-tx-id={transaction.id}
|
|
className={cn(
|
|
'transition-colors',
|
|
hasInvoiceMatch || hasSupplierInvoiceMatch ? 'border-primary/50' : 'border-warning/50',
|
|
isSelected && 'border-primary bg-primary/[0.02]',
|
|
isDisabled && 'opacity-50'
|
|
)}
|
|
onClick={showCheckbox ? () => onToggleSelect(transaction.id) : undefined}
|
|
>
|
|
<CardContent className="py-4">
|
|
{skvCounterpartDate && (
|
|
<div className="mb-3 flex items-start gap-2 rounded-md border border-warning/40 bg-warning/5 p-2 text-xs">
|
|
<AlertCircle className="h-3.5 w-3.5 mt-0.5 flex-shrink-0 text-warning" />
|
|
<p className="min-w-0">
|
|
<span className="font-medium">Möjlig 1930↔1630-överföring.</span>{' '}
|
|
Det finns en skattekonto-händelse den{' '}
|
|
<span className="tabular-nums">{skvCounterpartDate}</span> som
|
|
matchar — bokför detta verifikat först, koppla sedan
|
|
skattekonto-raden mot samma verifikat istället för att bokföra
|
|
två gånger.
|
|
</p>
|
|
</div>
|
|
)}
|
|
<div className="flex items-start justify-between gap-4">
|
|
{/* Left: checkbox + icon + info */}
|
|
<div className="flex items-start gap-3 min-w-0 flex-1">
|
|
{showCheckbox && (
|
|
<Checkbox
|
|
checked={isSelected}
|
|
onCheckedChange={() => onToggleSelect(transaction.id)}
|
|
onClick={(e) => e.stopPropagation()}
|
|
className="mt-1"
|
|
/>
|
|
)}
|
|
<div
|
|
className={`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'}`}
|
|
aria-hidden="true"
|
|
>
|
|
{isIncome ? (
|
|
<ArrowUpRight className="h-5 w-5" />
|
|
) : (
|
|
<ArrowDownRight className="h-5 w-5" />
|
|
)}
|
|
</div>
|
|
<div className="min-w-0">
|
|
<div className="flex items-center gap-1.5 min-w-0">
|
|
<p className="font-medium truncate">{transaction.description}</p>
|
|
<TransactionAttachmentIndicator documentId={transaction.document_id} />
|
|
</div>
|
|
<p className="text-sm text-muted-foreground">{formatDate(transaction.date)}</p>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Right: amount */}
|
|
<div className="text-right flex-shrink-0">
|
|
<p className={cn('font-medium tabular-nums', isIncome && 'text-success')}>
|
|
{isIncome ? '+' : ''}
|
|
{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>
|
|
|
|
{/* Inline action buttons - only shown when not in batch mode */}
|
|
{!isBatchMode && (
|
|
<div className="flex flex-wrap items-center gap-2 mt-3 pt-3 border-t">
|
|
{/* Primary action: invoice match or top suggestion */}
|
|
{hasInvoiceMatch ? (
|
|
<Button
|
|
size="sm"
|
|
variant="default"
|
|
className="h-9 text-xs max-w-full truncate"
|
|
onClick={() => onOpenMatchDialog(transaction)}
|
|
disabled={isProcessing || isDisabled}
|
|
>
|
|
{isProcessing ? (
|
|
<Loader2 className="mr-1.5 h-3 w-3 animate-spin flex-shrink-0" />
|
|
) : (
|
|
<FileText className="mr-1.5 h-3 w-3 flex-shrink-0" />
|
|
)}
|
|
Matcha Faktura {transaction.potential_invoice!.invoice_number}
|
|
</Button>
|
|
) : hasSupplierInvoiceMatch ? (
|
|
<Button
|
|
size="sm"
|
|
variant="default"
|
|
className="h-9 text-xs max-w-full truncate"
|
|
onClick={() => onOpenMatchDialog(transaction)}
|
|
disabled={isProcessing || isDisabled}
|
|
>
|
|
{isProcessing ? (
|
|
<Loader2 className="mr-1.5 h-3 w-3 animate-spin flex-shrink-0" />
|
|
) : (
|
|
<FileText className="mr-1.5 h-3 w-3 flex-shrink-0" />
|
|
)}
|
|
Matcha Leverantörsfaktura {transaction.potential_supplier_invoice!.supplier_invoice_number}
|
|
</Button>
|
|
) : templateSuggestions && templateSuggestions.length > 0 ? (
|
|
<>
|
|
{templateSuggestions.slice(0, 2).map((ts, idx) => {
|
|
const isCounterparty = isCounterpartyTemplateId(ts.template_id)
|
|
const tmpl = isCounterparty ? null : getTemplateById(ts.template_id)
|
|
return (
|
|
<Button
|
|
key={ts.template_id}
|
|
size="sm"
|
|
variant={idx === 0 ? 'default' : 'outline'}
|
|
className="h-auto py-1.5 text-xs"
|
|
onClick={() => {
|
|
if (onOpenTemplateReview && (isCounterparty || tmpl)) {
|
|
onOpenTemplateReview(transaction, ts.template_id)
|
|
} else if (topSuggestion) {
|
|
handleSuggestionClick(topSuggestion)
|
|
}
|
|
}}
|
|
disabled={isProcessing || isDisabled}
|
|
>
|
|
<div className="flex flex-col items-start">
|
|
<div className="flex items-center">
|
|
{isProcessing && idx === 0 ? (
|
|
<Loader2 className="mr-1.5 h-3 w-3 animate-spin" />
|
|
) : null}
|
|
{ts.name_sv}
|
|
</div>
|
|
<span className="opacity-70 font-normal text-[10px]">
|
|
{isCounterparty
|
|
? `${ts.description_sv}`
|
|
: getAccountName(tmpl?.debit_account || ts.debit_account)
|
|
}
|
|
</span>
|
|
</div>
|
|
</Button>
|
|
)
|
|
})}
|
|
</>
|
|
) : topSuggestion ? (
|
|
<Button
|
|
size="sm"
|
|
variant="default"
|
|
className="h-9 text-xs"
|
|
onClick={() => handleSuggestionClick(topSuggestion)}
|
|
disabled={isProcessing || isDisabled}
|
|
>
|
|
{isProcessing ? (
|
|
<Loader2 className="mr-1.5 h-3 w-3 animate-spin" />
|
|
) : null}
|
|
{topSuggestion.label}
|
|
{topSuggestion.account && (
|
|
<span className="ml-1 opacity-70 font-normal">
|
|
({formatAccountWithName(topSuggestion.account)})
|
|
</span>
|
|
)}
|
|
{topSuggestion.confidence >= 0.8 && (
|
|
<Badge variant="secondary" className="ml-1.5 text-[10px] px-1 py-0">
|
|
{Math.round(topSuggestion.confidence * 100)}%
|
|
</Badge>
|
|
)}
|
|
</Button>
|
|
) : null}
|
|
|
|
{/* Open category dialog / template picker */}
|
|
<Button
|
|
size="sm"
|
|
variant={!hasInvoiceMatch && !hasSupplierInvoiceMatch && !topSuggestion && (!templateSuggestions || templateSuggestions.length === 0) ? 'default' : 'outline'}
|
|
className="h-9 text-xs"
|
|
onClick={() => onOpenCategoryDialog(transaction)}
|
|
disabled={isProcessing || isDisabled}
|
|
>
|
|
Välj mall...
|
|
</Button>
|
|
|
|
{/* Delete button — available for all unbooked transactions */}
|
|
{isDeletable && onDelete && (
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
className="ml-auto text-muted-foreground hover:text-destructive"
|
|
onClick={() => onDelete(transaction.id)}
|
|
disabled={isProcessing || isDisabled}
|
|
aria-label="Ta bort transaktion"
|
|
>
|
|
<Trash2 className="h-4 w-4" />
|
|
</Button>
|
|
)}
|
|
</div>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
</motion.div>
|
|
)
|
|
}
|