Files
accounted/types/skatteverket.ts
T
dea5e31756 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-12 21:00:57 +02:00

99 lines
3.4 KiB
TypeScript

/**
* Skatteverket data shapes used by core UI (the /transactions page lives
* in core, but renders skattekonto rows alongside bank tx). The DB table
* `skattekonto_transactions` lives in core migrations even when the
* skatteverket extension is disabled: the extension only owns the API
* that populates it. Keeping these types in core means components can
* render the table's shape without depending on the extension module.
*
* If skatteverket is disabled, the API returns 503 and the UI just sees
* an empty list: the types remain valid descriptors of the schema.
*/
/** Row shape for the `skattekonto_transactions` table (DB → app). */
export interface StoredSkattekontoTransaction {
id: string
company_id: string
transaktionsidentitet: number | null
dedup_key: string
transaktionsdatum: string
forfallodatum: string | null
ranteberakningsdatum: string | null
transaktionstext: string
belopp_skatteverket: number
belopp_kronofogden: number | null
status: 'booked' | 'upcoming'
journal_entry_id: string | null
imported_at: string
updated_at: string
}
/**
* Single best candidate verifikat for an unmatched SKV row. Attached by
* the `/skattekonto/transaktioner` endpoint when exactly one strong match
* exists, so the UI can offer a one-click "koppla till A12" hint instead
* of forcing the user to open the full Matcha-dialog.
*/
export interface SkattekontoMatchSuggestion {
journal_entry_id: string
voucher_number: number | null
voucher_series: string | null
entry_date: string
description: string
status: 'draft' | 'posted' | 'reversed'
}
/**
* The deterministic counter-account a "Bokför" on this row would use,
* resolved from `skattekonto_rules` server-side. Lets the list show what a
* booking will do ("Bokförs mot 8314 Skattefria ränteintäkter") and drives
* bulk-booking eligibility. `account_name` comes from the BAS reference and
* may be null for custom accounts; `label` is the matched rule's label.
*/
export interface SkattekontoBookingSuggestion {
account: string
account_name?: string | null
label?: string | null
}
/**
* API response variant: stored row plus optional auto-match suggestion.
* `match_suggestion` is optional because kommande/upcoming rows skip the
* enrichment step entirely (no journal entry can match a future event).
* `booking_suggestion` is likewise only computed for unbooked genomförda
* rows: undefined means "not computed", null means "no rule matched".
*/
export interface SkattekontoTransactionWithSuggestion extends StoredSkattekontoTransaction {
match_suggestion?: SkattekontoMatchSuggestion | null
booking_suggestion?: SkattekontoBookingSuggestion | null
}
/**
* Per-row outcome from POST /skattekonto/transaktioner/bokfor-batch.
* `journal_entry_id` is present on success AND on COMMIT_FAILED (the draft
* was created and stays linked; only the commit step failed).
*/
export interface SkattekontoBatchRowResult {
id: string
ok: boolean
journal_entry_id?: string
voucher_number?: number | null
voucher_series?: string | null
error_code?:
| 'NO_COUNTER_ACCOUNT'
| 'NO_FISCAL_PERIOD'
| 'PERIOD_LOCKED'
| 'ALREADY_BOOKED'
| 'NOT_SETTLED'
| 'TRANSACTION_NOT_FOUND'
| 'COMMIT_FAILED'
| 'UNKNOWN'
error_message?: string
}
/** Response envelope body for the bokfor-batch endpoint. */
export interface SkattekontoBatchResult {
results: SkattekontoBatchRowResult[]
summary: { total: number; succeeded: number; failed: number }
}