'use client' import { AccountNumber } from '@/components/ui/account-number' import { buildCorrectionRows, formatSignedAmount, type AccountRow, type CorrectionLineInput, } from '@/components/bookkeeping/correction-preview-rows' import type { JournalEntryLine } from '@/types' interface Props { originalLines: JournalEntryLine[] correctedLines: CorrectionLineInput[] } function signClass(n: number): string { if (n > 0) return 'text-success' if (n < 0) return 'text-destructive' return 'text-muted-foreground' } export default function CorrectionPreview({ originalLines, correctedLines }: Props) { const rows = buildCorrectionRows(originalLines, correctedLines) const hasAnyCorrection = correctedLines.some((l) => { if (l.account_number.length !== 4) return false const d = typeof l.debit_amount === 'string' ? parseFloat(l.debit_amount) : l.debit_amount const c = typeof l.credit_amount === 'string' ? parseFloat(l.credit_amount) : l.credit_amount return (Number.isFinite(d) && d > 0) || (Number.isFinite(c) && c > 0) }) // An account that was on the original but the user dropped from the rättelse: // the storno still drains it to zero (delta = −original). Flag it so the cell // reads "tas bort" instead of a bare "–", which would imply "unchanged". const isRemoved = (row: AccountRow) => hasAnyCorrection && !row.correctionPresent && Math.abs(row.original) >= 0.005 // The per-account deltas sum to the corrected lines' debit − credit. A non-zero // sum means the proposed rättelse is not yet balanced — surface that here so the // förändring column is read as a work-in-progress, not a miscalculation. const netDelta = rows.reduce((sum, r) => sum + r.delta, 0) const unbalanced = hasAnyCorrection && Math.abs(netDelta) >= 0.005 if (rows.length === 0) return null return (

Effekt per konto

Debet − Kredit

{rows.map((row) => ( ))}
Konto Original Storno Rättelse Förändring
{formatSignedAmount(row.original)} {formatSignedAmount(row.storno)} {!hasAnyCorrection ? '–' : isRemoved(row) ? 'tas bort' : formatSignedAmount(row.correction)} {hasAnyCorrection ? formatSignedAmount(row.delta) : '–'}
{rows.map((row) => (
{hasAnyCorrection ? formatSignedAmount(row.delta) : '–'}
Original
{formatSignedAmount(row.original)}
Storno
{formatSignedAmount(row.storno)}
Rättelse
{!hasAnyCorrection ? '–' : isRemoved(row) ? 'tas bort' : formatSignedAmount(row.correction)}
))}

Förändring = storno + rättelse. Det är det netto som tillkommer ovanpå originalet när du bokför. Ett konto du tar bort nollställs av stornon.

{unbalanced && (

Förslaget balanserar inte ännu – debet och kredit i rättelsen måste vara lika.

)}
) }