Files
accounted/components/import/AccountMappingStep.tsx
T
Jakob Wennberg 91e2c1705a feat: per-line VAT, invoice document types, ledger-based VAT declaration, bank reconciliation, and pagination
Per-line VAT rates:
- Add generatePerRateLines() to group invoice items by vat_rate with separate
  revenue + VAT lines per rate group (invoice-entries.ts)
- Add getAvailableVatRates() and getVatTreatmentForRate() (vat-rules.ts)
- PDF template shows per-line VAT column and per-rate totals for mixed-rate invoices
- Invoice create/review UI supports per-line rate selection
- Types: add vat_rate/vat_amount to InvoiceItem, vat_rate to CreateInvoiceItemInput

Invoice document types (proforma, delivery note):
- Add InvoiceDocumentType, document_type and converted_from_id to Invoice type
- PDF hides prices for delivery notes, adds proforma notice
- Email templates support all document types
- mark-paid skips journal entries for non-invoice document types
- Migration 031: invoice_document_type

Accounting method support:
- Add AccountingMethod type (accrual/cash)
- Migration 032: add_accounting_method column to company_settings

VAT declaration rewrite:
- Rewrite to read directly from general ledger (26xx/3xxx account lines)
  instead of aggregating invoices/transactions/receipts
- ACCOUNT_RUTA mapping drives momsdeklaration boxes from GL balances

Bank reconciliation:
- Transaction ingest now pre-fetches unlinked GL lines and attempts
  auto-reconciliation during import
- Add transaction.reconciled event type
- Add ReconciliationMethod type and reconciliation_method on Transaction
- Migration 030: bank_reconciliation
- New reconciliation engine, API routes, and BankReconciliationView component

Pagination (fetchAllRows):
- New lib/supabase/fetch-all.ts overcomes PostgREST 1000-row limit
- Adopted in all report generators, SIE/SRU export, account list APIs

Fiscal period validation:
- New validate-period-duration.ts enforces max 18 months per BFL 3 kap.
- Applied in period-service.ts and fiscal-periods API

Account mapper simplification:
- Remove Levenshtein/fuzzy matching, use exact account number match only

Swedbank parser improvements:
- Support abbreviated headers (Clnr, Bokfdag, Radnr)
- Use Referens column as counterparty

Chart of accounts management:
- Add DELETE endpoint with system account and usage protection
- PUT uses partial updates
- New AccountCombobox, AddAccountDialog, EditAccountDialog, ChartOfAccountsManager

Tax deadline corrections:
- Rewrite inkomstdeklaration_ab using Skatteverket lookup table
- Rewrite arsredovisning deadline to 7 months after FY end per ÅRL 8:3

Onboarding first fiscal year:
- Add first fiscal year toggle with date pickers and 18-month validation

UI terminology:
- Change "okategoriserad/kategorisera" to "obokförd/bokföra" throughout

Report column fix:
- Fix start_date/end_date to period_start/period_end in report queries

Supplier invoice input:
- CreateSupplierInvoiceItemInput uses amount field (legacy quantity/unit_price kept)

Misc:
- SIE import uses upsert for idempotent account creation
- account-descriptions.ts falls back to BAS reference data
- Add invoice_default_notes to CompanySettings
- Update CLAUDE.md to reflect current project state

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 14:57:15 +01:00

356 lines
12 KiB
TypeScript

'use client'
import { useState, useMemo } from 'react'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Badge } from '@/components/ui/badge'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select'
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table'
import {
ArrowRight,
Search,
CheckCircle,
AlertCircle,
XCircle,
Filter,
} from 'lucide-react'
import type { AccountMapping } from '@/lib/import/types'
import type { BASAccount } from '@/types'
import { getAccountClassName } from '@/lib/bookkeeping/account-descriptions'
interface AccountMappingStepProps {
mappings: AccountMapping[]
basAccounts: BASAccount[]
onMappingChange: (sourceAccount: string, targetAccount: string, targetName: string) => void
onContinue: () => void
onBack: () => void
}
type FilterType = 'all' | 'unmapped' | 'low_confidence' | 'manual'
const PAGE_SIZE = 50
export default function AccountMappingStep({
mappings,
basAccounts,
onMappingChange,
onContinue,
onBack,
}: AccountMappingStepProps) {
const [searchTerm, setSearchTerm] = useState('')
// Default to showing unmapped accounts first (most actionable)
const [filter, setFilter] = useState<FilterType>(() => {
const hasUnmapped = mappings.some((m) => !m.targetAccount)
return hasUnmapped ? 'unmapped' : 'all'
})
const [currentPage, setCurrentPage] = useState(1)
// Filter and search mappings
const filteredMappings = useMemo(() => {
let result = mappings
// Apply filter
switch (filter) {
case 'unmapped':
result = result.filter((m) => !m.targetAccount)
break
case 'low_confidence':
result = result.filter((m) => m.targetAccount && m.confidence < 0.7)
break
case 'manual':
result = result.filter((m) => m.isOverride)
break
}
// Apply search
if (searchTerm) {
const term = searchTerm.toLowerCase()
result = result.filter(
(m) =>
m.sourceAccount.includes(term) ||
m.sourceName.toLowerCase().includes(term) ||
m.targetAccount?.includes(term) ||
m.targetName?.toLowerCase().includes(term)
)
}
return result
}, [mappings, filter, searchTerm])
// Pagination
const totalPages = Math.ceil(filteredMappings.length / PAGE_SIZE)
const paginatedMappings = useMemo(() => {
const start = (currentPage - 1) * PAGE_SIZE
return filteredMappings.slice(start, start + PAGE_SIZE)
}, [filteredMappings, currentPage])
// Reset page when filter or search changes
const handleFilterChange = (newFilter: FilterType) => {
setFilter(newFilter)
setCurrentPage(1)
}
const handleSearchChange = (term: string) => {
setSearchTerm(term)
setCurrentPage(1)
}
// Calculate stats
const stats = useMemo(() => {
const unmapped = mappings.filter((m) => !m.targetAccount).length
const lowConfidence = mappings.filter((m) => m.targetAccount && m.confidence < 0.7).length
const manual = mappings.filter((m) => m.isOverride).length
return { unmapped, lowConfidence, manual }
}, [mappings])
const canContinue = stats.unmapped === 0
// Group BAS accounts by class for the dropdown
const accountsByClass = useMemo(() => {
const groups: { [key: string]: BASAccount[] } = {}
for (const account of basAccounts) {
const className = getAccountClassName(account.account_class)
if (!groups[className]) {
groups[className] = []
}
groups[className].push(account)
}
return groups
}, [basAccounts])
return (
<div className="space-y-6">
<Card>
<CardHeader>
<CardTitle>Kontomappning</CardTitle>
<CardDescription>
Granska och justera mappningarna mellan importerade konton och din kontoplan.
Alla konton måste vara mappade innan importen kan genomföras.
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{/* Stats */}
<div className="flex gap-4 flex-wrap">
<Badge
variant={filter === 'unmapped' ? 'destructive' : stats.unmapped > 0 ? 'destructive' : 'secondary'}
className="cursor-pointer"
onClick={() => handleFilterChange('unmapped')}
>
<XCircle className="h-3 w-3 mr-1" />
{stats.unmapped} ej mappade
</Badge>
<Badge
variant={filter === 'low_confidence' ? 'default' : stats.lowConfidence > 0 ? 'secondary' : 'outline'}
className="cursor-pointer"
onClick={() => handleFilterChange('low_confidence')}
>
<AlertCircle className="h-3 w-3 mr-1" />
{stats.lowConfidence} osäkra
</Badge>
<Badge
variant={filter === 'manual' ? 'default' : 'outline'}
className="cursor-pointer"
onClick={() => handleFilterChange('manual')}
>
<CheckCircle className="h-3 w-3 mr-1" />
{stats.manual} manuellt satta
</Badge>
<Badge
variant={filter === 'all' ? 'default' : 'outline'}
className="cursor-pointer"
onClick={() => handleFilterChange('all')}
>
Visa alla ({mappings.length})
</Badge>
</div>
{/* Search and filter */}
<div className="flex gap-4">
<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 konto..."
value={searchTerm}
onChange={(e) => handleSearchChange(e.target.value)}
className="pl-9"
/>
</div>
<Select value={filter} onValueChange={(v) => handleFilterChange(v as FilterType)}>
<SelectTrigger className="w-48">
<Filter className="h-4 w-4 mr-2" />
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="all">Visa alla</SelectItem>
<SelectItem value="unmapped">Ej mappade</SelectItem>
<SelectItem value="low_confidence">Osäkra</SelectItem>
<SelectItem value="manual">Manuellt satta</SelectItem>
</SelectContent>
</Select>
</div>
{/* Mapping table */}
<div className="border rounded-lg overflow-hidden">
<Table>
<TableHeader>
<TableRow>
<TableHead className="w-36">Källkonto</TableHead>
<TableHead>Källnamn</TableHead>
<TableHead className="w-12"></TableHead>
<TableHead className="w-64">Målkonto</TableHead>
<TableHead className="w-24">Konfidens</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{paginatedMappings.map((mapping) => (
<TableRow
key={mapping.sourceAccount}
className={!mapping.targetAccount ? 'bg-destructive/5' : ''}
>
<TableCell className="font-mono">{mapping.sourceAccount}</TableCell>
<TableCell className="text-muted-foreground">{mapping.sourceName}</TableCell>
<TableCell>
<ArrowRight className="h-4 w-4 text-muted-foreground" />
</TableCell>
<TableCell>
<Select
value={mapping.targetAccount || 'none'}
onValueChange={(value) => {
const account = basAccounts.find((a) => a.account_number === value)
onMappingChange(
mapping.sourceAccount,
value === 'none' ? '' : value,
account?.account_name || ''
)
}}
>
<SelectTrigger className={!mapping.targetAccount ? 'border-destructive' : ''}>
<SelectValue placeholder="Välj konto..." />
</SelectTrigger>
<SelectContent className="max-h-80">
<SelectItem value="none">-- Välj konto --</SelectItem>
{Object.entries(accountsByClass).map(([className, accounts]) => (
<div key={className}>
<div className="px-2 py-1.5 text-xs font-semibold text-muted-foreground bg-muted">
{className}
</div>
{accounts.map((account) => (
<SelectItem
key={account.account_number}
value={account.account_number}
>
<span className="font-mono mr-2">{account.account_number}</span>
{account.account_name}
</SelectItem>
))}
</div>
))}
</SelectContent>
</Select>
</TableCell>
<TableCell>
{mapping.targetAccount && (
<ConfidenceBadge
confidence={mapping.confidence}
matchType={mapping.matchType}
isOverride={mapping.isOverride}
/>
)}
</TableCell>
</TableRow>
))}
{paginatedMappings.length === 0 && (
<TableRow>
<TableCell colSpan={5} className="text-center text-muted-foreground py-8">
Inga konton matchar filtret
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
{/* Pagination */}
{totalPages > 1 && (
<div className="flex items-center justify-between">
<p className="text-sm text-muted-foreground">
Visar {((currentPage - 1) * PAGE_SIZE) + 1}-{Math.min(currentPage * PAGE_SIZE, filteredMappings.length)} av {filteredMappings.length}
</p>
<div className="flex gap-2">
<Button
variant="outline"
size="sm"
onClick={() => setCurrentPage((p) => Math.max(1, p - 1))}
disabled={currentPage === 1}
>
Föregående
</Button>
<div className="flex items-center gap-1 px-2">
<span className="text-sm">Sida {currentPage} av {totalPages}</span>
</div>
<Button
variant="outline"
size="sm"
onClick={() => setCurrentPage((p) => Math.min(totalPages, p + 1))}
disabled={currentPage === totalPages}
>
Nästa
</Button>
</div>
</div>
)}
</CardContent>
</Card>
{/* Actions */}
<div className="flex justify-between">
<Button variant="outline" onClick={onBack}>
Tillbaka
</Button>
<Button onClick={onContinue} disabled={!canContinue}>
{canContinue ? 'Fortsätt till granskning' : `${stats.unmapped} konton saknar mappning`}
<ArrowRight className="ml-2 h-4 w-4" />
</Button>
</div>
</div>
)
}
function ConfidenceBadge({
confidence,
isOverride,
}: {
confidence: number
matchType: string // Keep for potential future use
isOverride: boolean
}) {
if (isOverride) {
return <Badge variant="default">Manuell</Badge>
}
if (confidence >= 0.9) {
return <Badge variant="default" className="bg-success">Exakt</Badge>
}
if (confidence >= 0.7) {
return <Badge variant="secondary">Trolig</Badge>
}
return <Badge variant="outline">Osäker</Badge>
}