* feat(bookkeeping): guarded fiscal-year reset + findable Angra import (#1883) Two deliverables from the community report where a bad SIE test import left no way out short of deleting the company: A) Discoverability: the voucher list shows one attn line linking to /import?history=sie whenever the page contains import-sourced vouchers, and /import?history=sie deep-links straight into the fold-open SIE import history where per-import Angra already lives. B) Reset of an UNLOCKED fiscal year regardless of how the entries arrived: new reset_fiscal_year RPC (same gnubok.allow_delete escape hatch as undo_sie_import; no enforcement trigger touched) behind GET/POST /api/bookkeeping/fiscal-periods/[id]/reset and a typed type-the-year-name confirmation dialog on the fiscal years settings list. Refuses on: locked/closed year, company lock date over any part of the year, executed year-end, arsredovisning state, later year depending on this year's UB, VAT-declared evidence (vat_settlement verifikat, SKV lock/submit audit rows, extension workflow keys, fail closed) and AGI-declared months. Entries referenced by RESTRICT/NO ACTION FKs abort the whole reset (all-or-nothing). Documents are detached, never deleted (BFL 7 kap); every delete is audit-logged plus one behandlingshistorik summary row. Fixes #1883 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(bookkeeping): harden fiscal-year reset after skeptic review (#1883) Blocking skeptic findings on PR #1897, one consolidated pass: - New snapshot blocker cross_year_reference: an entry outside the year whose correction_of_id / reverses_id / reversed_by_id points into the year made the delete crash with an uncaught P0001 (immutability trigger refusing the ON DELETE SET NULL referential UPDATE) after an eligible:true preview, and silently severed draft chains. 12 such chains exist in prod today. - New snapshot blocker rot_rut_state: a begaran om utbetalning that reached Skatteverket (submitted/paid/partially_paid/rejected) was silently unlinked via SET NULL, erasing the bokforing behind a filed and possibly decided myndighetsarende. - Rakenskapsinformation preservation (BFL 7 kap): line-level trigger audit rows carry no company_id and header rows no amounts, so a reset destroyed konton/belopp with no company-readable trace. The RPC now archives the full content of every verifikat in company-scoped RESET_SNAPSHOT audit rows before deleting (action added to audit_log_action_check, NOT VALID), and behandlingshistorik renders them. - Dimension registry lockstep on reset (mirrors undo_sie_import): flipped imports can never be undone again, so their dimensions/values would have been orphaned forever. - EXCEPTION WHEN raise_exception now returns a typed FISCAL_YEAR_RESET_LINKED_ENTRIES envelope instead of a bare 500; gnubok.allow_delete is cleared before leaving the guarded block. - Voucher-list attn line fires only for source_type 'import': opening_balance is also written by year-end closing and the manual IB flows, which mislabelled every year-2+ company as SIE-imported. - /import?history=sie now scrolls the SIE history into view. - Reset dialog copy (sv+en) discloses that linked invoices, payments and bank transactions become unbooked; new blocker strings in both locales. - pg fixture fix: document_attachments seeded without company_id (23502); new pg tests for both blockers, RESET_SNAPSHOT rows and the lockstep. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
77cacdcf34
commit
79013cf092
@@ -117,6 +117,53 @@ export interface CompanyMigrationResetRpcResult {
|
||||
counts?: CompanyMigrationResetEligibility['counts']
|
||||
}
|
||||
|
||||
// Fiscal-year reset (issue #1883): guarded hard-delete of one OPEN fiscal
|
||||
// year's vouchers. Mirrors the migration-reset envelope shapes above.
|
||||
export type FiscalYearResetBlockerCode =
|
||||
| 'period_closed'
|
||||
| 'period_locked'
|
||||
| 'company_lock_date'
|
||||
| 'year_end_state'
|
||||
| 'arsredovisning_state'
|
||||
| 'next_year_dependency'
|
||||
| 'vat_declared'
|
||||
| 'agi_declared'
|
||||
| 'rot_rut_state'
|
||||
| 'cross_year_reference'
|
||||
|
||||
export interface FiscalYearResetBlocker {
|
||||
code: FiscalYearResetBlockerCode
|
||||
count?: number
|
||||
date?: string
|
||||
}
|
||||
|
||||
export interface FiscalYearResetEligibility {
|
||||
eligible: boolean
|
||||
blockers: FiscalYearResetBlocker[]
|
||||
period: {
|
||||
id: string
|
||||
name: string
|
||||
period_start: string
|
||||
period_end: string
|
||||
}
|
||||
counts: {
|
||||
vouchers: number
|
||||
documents_to_detach: number
|
||||
}
|
||||
}
|
||||
|
||||
export interface FiscalYearResetRpcResult {
|
||||
ok: boolean
|
||||
code?: string
|
||||
eligible?: boolean
|
||||
blockers?: FiscalYearResetBlocker[]
|
||||
period?: FiscalYearResetEligibility['period']
|
||||
counts?: FiscalYearResetEligibility['counts']
|
||||
deleted?: number
|
||||
detached_documents?: number
|
||||
period_name?: string
|
||||
}
|
||||
|
||||
// User preferences (cross-company)
|
||||
export interface UserPreferences {
|
||||
id: string
|
||||
@@ -3658,6 +3705,7 @@ export type AuditAction =
|
||||
| 'SECURITY_EVENT'
|
||||
| 'INTEGRITY_FAILURE'
|
||||
| 'COMMITTED_AT_OVERRIDE'
|
||||
| 'RESET_SNAPSHOT'
|
||||
|
||||
export interface AuditLogEntry {
|
||||
id: string
|
||||
|
||||
Reference in New Issue
Block a user