feat(skattekonto): bulk Bokför + inline single-row booking (fewer clicks) (#1535)
* feat(skattekonto): bulk Bokför and inline single-row booking
Booking a year of skattekonto events took 6 clicks per row (list, Bokför,
review page, Bokför, confirm, navigate back), even for a +1 kr
intäktsränta row.
- GET /skattekonto/transaktioner now attaches a deterministic
booking_suggestion per unbooked row (one hoisted skattekonto_rules +
entity_type fetch via the new attachBookingSuggestions), shown as muted
text on the inbox row ('Bokförs mot 8314 ...').
- New POST /skattekonto/transaktioner/bokfor-batch (Zod, max 200 ids):
sequential draft+commit per row server-side (no orphan drafts), per-row
results, never aborts on a row failure. Commit attribution: bulk_accept
for real batches, user_accept for the one-row inline flow. A failed
commit keeps the linked draft (degrades to the old review flow).
- Inbox: hover-reveal checkboxes on eligible SKV rows (suggestion
present, no duplicate hint, unbooked, genomförd), separate skvSelectedIds
set, bulkbar 'Bokför valda (N)' with ONE summary ConfirmationDialog
grouped by suggestion with per-group sums, chunked batchProgress, ONE
aggregate toast, local state patch with exit animation.
- Single-row: new SkattekontoBookDialog (dynamic import) replaces the
draft-then-window.location detour on both /transactions and /skattekonto;
'Öppna som utkast' keeps the old draft path via router.push.
Co-Authored-By: Claude Fable 5 <[email protected]>
* fix(skattekonto): batch booking guards for unsettled rows, double-post race and locked periods
- reject status != booked rows in the batch flow with NOT_SETTLED before
any draft exists (server no longer trusts client eligibility); the
single-row draft endpoint keeps its behaviour
- make the journal_entry_id backlink a conditional claim (update where
journal_entry_id is null, select affected rows): zero affected rows maps
to ALREADY_BOOKED and commitEntry only runs after a won claim, so two
concurrent submissions can no longer double-post the same row
- detect the period-lock trigger signature in the batch catch and map it
to PERIOD_LOCKED with Swedish text instead of UNKNOWN with raw DB output
- SkattekontoBookDialog: rows with no matched rule no longer get the
guaranteed-422 draft CTA; they route to the existing match flow and to
manual verifikat creation in /bookkeeping
- pass an explicit skv_book_dialog.commit_warning key for the
direct-commit warning instead of ConfirmationDialog's hardcoded default
Co-Authored-By: Claude Fable 5 <[email protected]>
---------
Co-authored-by: Jakob Wennberg <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5
Jakob Wennberg
parent
7cf0e34434
commit
dea5e31756
@@ -1,6 +1,7 @@
|
||||
'use client'
|
||||
|
||||
import { Fragment, useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import dynamic from 'next/dynamic'
|
||||
import Link from 'next/link'
|
||||
import { useTranslations } from 'next-intl'
|
||||
import { Button } from '@/components/ui/button'
|
||||
@@ -27,6 +28,8 @@ import {
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import { useToast } from '@/components/ui/use-toast'
|
||||
import { ToastAction } from '@/components/ui/toast'
|
||||
import { DialogLoadingSkeleton } from '@/components/ui/dialog-loading-skeleton'
|
||||
import { cn } from '@/lib/utils'
|
||||
import {
|
||||
formatCurrency,
|
||||
@@ -50,8 +53,14 @@ import type {
|
||||
SkattekontoTransactionWithSuggestion,
|
||||
StoredSkattekontoTransaction,
|
||||
} from '@/extensions/general/skatteverket/types'
|
||||
import type { SkattekontoBatchRowResult } from '@/types/skatteverket'
|
||||
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
||||
|
||||
const SkattekontoBookDialog = dynamic(
|
||||
() => import('@/components/skattekonto/SkattekontoBookDialog'),
|
||||
{ loading: DialogLoadingSkeleton },
|
||||
)
|
||||
|
||||
interface SaldoEnvelope {
|
||||
data: SkatteverketSaldoResponse | null
|
||||
fetchedAt: string | null
|
||||
@@ -86,7 +95,10 @@ export default function SkattekontoPage() {
|
||||
const [tx, setTx] = useState<TransaktionerEnvelope['data'] | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [syncing, setSyncing] = useState(false)
|
||||
const [bookingId, setBookingId] = useState<string | null>(null)
|
||||
// Row whose inline booking dialog is open (null = closed).
|
||||
const [bookTarget, setBookTarget] = useState<SkattekontoTransactionWithSuggestion | null>(
|
||||
null,
|
||||
)
|
||||
const [notConnected, setNotConnected] = useState(false)
|
||||
const [loadError, setLoadError] = useState(false)
|
||||
// Set when a sync fails with an auth error while a connection exists
|
||||
@@ -222,37 +234,41 @@ export default function SkattekontoPage() {
|
||||
}
|
||||
}
|
||||
|
||||
async function bokfor(id: string) {
|
||||
setBookingId(id)
|
||||
try {
|
||||
const res = await fetch(
|
||||
`/api/extensions/ext/skatteverket/skattekonto/transaktioner/${id}/bokfor`,
|
||||
{ method: 'POST' },
|
||||
)
|
||||
const json = await res.json()
|
||||
if (!res.ok) {
|
||||
toast({
|
||||
title: 'Kunde inte bokföra',
|
||||
description: getUserErrorMessage(json, { statusCode: res.status }),
|
||||
variant: 'destructive',
|
||||
})
|
||||
return
|
||||
}
|
||||
toast({
|
||||
title: 'Utkast skapat',
|
||||
description: 'Granska och bokför verifikatet i Bokföring.',
|
||||
})
|
||||
// Take the user to the draft so they can review.
|
||||
window.location.href = `/bookkeeping/${json.data.entry.id}`
|
||||
} catch (err) {
|
||||
toast({
|
||||
title: 'Kunde inte bokföra',
|
||||
description: err instanceof Error ? getUserErrorMessage(err) : undefined,
|
||||
variant: 'destructive',
|
||||
})
|
||||
} finally {
|
||||
setBookingId(null)
|
||||
}
|
||||
function bokfor(id: string) {
|
||||
// Open the inline booking dialog instead of the old draft-then-navigate
|
||||
// detour. The row may live in any bucket: genomförda rows carry the
|
||||
// booking suggestion, kommande/förfallna open in plain draft mode.
|
||||
const row =
|
||||
tx?.booked.find((r) => r.id === id) ??
|
||||
tx?.overdue.find((r) => r.id === id) ??
|
||||
tx?.upcoming.find((r) => r.id === id) ??
|
||||
null
|
||||
if (row) setBookTarget(row)
|
||||
}
|
||||
|
||||
function handleBooked(_rowId: string, result: SkattekontoBatchRowResult) {
|
||||
setBookTarget(null)
|
||||
const voucherLabel =
|
||||
result.voucher_series && result.voucher_number != null
|
||||
? formatVoucher({
|
||||
voucher_series: result.voucher_series,
|
||||
voucher_number: result.voucher_number,
|
||||
})
|
||||
: null
|
||||
toast({
|
||||
title: t('booked_toast_title'),
|
||||
description: voucherLabel
|
||||
? t('booked_toast_description', { voucher: voucherLabel })
|
||||
: undefined,
|
||||
action: result.journal_entry_id ? (
|
||||
<ToastAction altText={t('booked_toast_show')} asChild>
|
||||
<Link href={`/bookkeeping/${result.journal_entry_id}`}>
|
||||
{t('booked_toast_show')}
|
||||
</Link>
|
||||
</ToastAction>
|
||||
) : undefined,
|
||||
})
|
||||
void reload()
|
||||
}
|
||||
|
||||
async function openMatch(row: StoredSkattekontoTransaction) {
|
||||
@@ -538,13 +554,28 @@ export default function SkattekontoPage() {
|
||||
tx={tx}
|
||||
onBokfor={bokfor}
|
||||
onMatch={openMatch}
|
||||
bookingId={bookingId}
|
||||
/>
|
||||
|
||||
<p className="px-1 text-xs leading-5 text-muted-foreground">
|
||||
{t('pgnote', { amount: formatCurrency(data?.saldoKronofogden ?? 0) })}
|
||||
</p>
|
||||
|
||||
{bookTarget && (
|
||||
<SkattekontoBookDialog
|
||||
row={bookTarget}
|
||||
open
|
||||
onOpenChange={(o) => {
|
||||
if (!o) setBookTarget(null)
|
||||
}}
|
||||
onBooked={handleBooked}
|
||||
onMatch={() => {
|
||||
const target = bookTarget
|
||||
setBookTarget(null)
|
||||
void openMatch(target)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
<MatchDialog
|
||||
row={matchOpenFor}
|
||||
candidates={matchCandidates}
|
||||
@@ -629,12 +660,10 @@ function SkattekontoTable({
|
||||
tx,
|
||||
onBokfor,
|
||||
onMatch,
|
||||
bookingId,
|
||||
}: {
|
||||
tx: TransaktionerEnvelope['data'] | null
|
||||
onBokfor: (id: string) => void
|
||||
onMatch: (row: StoredSkattekontoTransaction) => void
|
||||
bookingId: string | null
|
||||
}) {
|
||||
const t = useTranslations('skattekonto')
|
||||
|
||||
@@ -698,7 +727,6 @@ function SkattekontoTable({
|
||||
section={section.key}
|
||||
onBokfor={onBokfor}
|
||||
onMatch={onMatch}
|
||||
bookingId={bookingId}
|
||||
showInterestDate={section.interestDateRowIds.has(row.id)}
|
||||
/>
|
||||
))}
|
||||
@@ -715,14 +743,12 @@ function SkattekontoRow({
|
||||
section,
|
||||
onBokfor,
|
||||
onMatch,
|
||||
bookingId,
|
||||
showInterestDate,
|
||||
}: {
|
||||
row: SkattekontoTransactionWithSuggestion
|
||||
section: TableSection['key']
|
||||
onBokfor: (id: string) => void
|
||||
onMatch: (row: StoredSkattekontoTransaction) => void
|
||||
bookingId: string | null
|
||||
showInterestDate: boolean
|
||||
}) {
|
||||
const t = useTranslations('skattekonto')
|
||||
@@ -802,10 +828,9 @@ function SkattekontoRow({
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onBokfor(row.id)}
|
||||
disabled={bookingId === row.id}
|
||||
className={QUIET_LINK_CLASS}
|
||||
>
|
||||
{bookingId === row.id ? t('action_booking') : t('action_book')}
|
||||
{t('action_book')}
|
||||
</button>
|
||||
</span>
|
||||
)}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user