Add/bokslut (#718)

* feat(arcim-migration): Briox provider with SIE-over-API import

- Briox auth via account ID + application token (no app-level
  credentials); both tokens rotate on refresh and are persisted
- New sie-fetcher pulls the general ledger as SIE through the
  provider API for Fortnox, Briox and Bjorn Lunden
- Wizard stops on a failed SIE import and surfaces the real errors
  instead of proceeding to the misleading migrate-guard message
- PROVIDER_SIE_ONLY_FORTNOX renamed to PROVIDER_SIE_NOT_SUPPORTED;
  new PROVIDER_TOKEN_INVALID for rejected provider credentials

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(bookkeeping): per-line accruals (periodisering) on invoices and supplier invoices

Defer revenue/costs per invoice line to 29xx/17xx interim accounts with
automatic monthly dissolution (nightly cron + catch-up at registration),
schedule cancellation on credit, year-end auto-detect exclusion for
already-scheduled invoices, invoice-inbox service-period extraction for
prefill, and an MCP tool to list schedules.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(bokslut): iXBRL arsredovisning generation and Bolagsverket digital filing

Generate the annual report as iXBRL from a generated taxonomy registry
(K2 element lists, taxonomy:generate/check scripts + CI guard), expose it
via the fiscal-period API, and add the bolagsverket extension for digital
submission to eget utrymme with webhook-driven status tracking
(submissions table + pg tests, lifecycle events, year-end wizard UI).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(mcp): raise origin-guard test timeout to 20s

The dynamic import pulls in the full server module; the parse alone
flirts with the 5s default under full-suite parallel load.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Add new scripts and documentation for K2 AB taxonomy generation and validation

- Introduced `generate-taxonomy-registry.ts` to automate the generation of the iXBRL taxonomy concept registry from official element lists and tuple models.
- Added `validate-ixbrl.mjs` for validating generated iXBRL reports against the official taxonomy package using Arelle.
- Included new documentation files:
  - `k2-ab-arsredovisning-elementlista-2024-09-12_rev20250312_sv.xlsx`
  - `tuple-innehallsmodell-arsredovisning-k2-2024-09-12.xlsx`
  - `taxonomi-paket-2024-09-12_rev20250312.zip`

* Add tests for bookkeeping accruals dissolution and supplier invoices

- Implement tests for the POST /api/bookkeeping/accruals/[id]/dissolve route, covering success and error scenarios.
- Add tests for the DELETE /api/supplier-invoices/[id] route, including authentication checks and validation of invoice deletion conditions.
- Introduce tests for the Arcim migration provider client, ensuring token handling and error classification.
- Create tests for the Bolagsverket extension, validating submission role enforcement and environment settings.
- Add Zod schemas for Bolagsverket response payloads to ensure proper validation.
- Implement tests for MCP server's list accrual schedules, confirming registration and scope mapping.
- Add consistency tests for IXBRL document generation, ensuring duplicate facts and XML escaping are handled correctly.
- Introduce typed domain errors for accrual schedules to improve error handling in the service.
- Add tests for resolving consent with Briox token refresh concurrency, ensuring proper token management and error handling.

* fix(tests): update payload size guard comments to reflect recent changes in tool descriptions and ceiling adjustments

* fix(gitattributes): mark generated JSON files in bokslut taxonomy as linguist-generated

* feat(migrations): add backfill for invoices.journal_entry_id and fallback for next_voucher_number user_id

* feat(bokslut): enhance compliance and financial processing features with new submission details and security measures

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Mattsson
2026-06-12 16:35:30 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 8e8b63a200
commit db8983ba9e
131 changed files with 33796 additions and 340 deletions
+111 -5
View File
@@ -597,6 +597,31 @@ const INVOICE: Record<string, StructuredErrorEntry> = {
message_sv: 'ROT/RUT-avdraget kunde inte valideras. Kontrollera personnummer och fastighetsbeteckning.',
message_en: 'ROT/RUT deduction failed validation. Check personnummer and housing designation.',
},
INVOICE_CREATE_ACCRUAL_INVALID: {
httpStatus: 400,
message_sv: 'Periodisering kan inte användas här. Den kräver faktureringsmetoden och stöds inte för omvänd skattskyldighet, export eller proforma.',
message_en: 'Periodisering cannot be used here. It requires the accrual method and is not supported for reverse charge, export, or proforma documents.',
},
ACCRUAL_NOT_FOUND: {
httpStatus: 404,
message_sv: 'Periodiseringen kunde inte hittas.',
message_en: 'Accrual schedule not found.',
},
ACCRUAL_DISSOLVE_FAILED: {
httpStatus: 400,
message_sv: 'Periodiseringen kunde inte lösas upp.',
message_en: 'The accrual schedule could not be dissolved.',
},
ACCRUAL_NOT_ACTIVE: {
httpStatus: 400,
message_sv: 'Periodiseringen är inte aktiv.',
message_en: 'The accrual schedule is not active.',
},
ACCRUAL_NOTHING_TO_DISSOLVE: {
httpStatus: 400,
message_sv: 'Det finns inget kvar att lösa upp.',
message_en: 'There is nothing left to dissolve on this accrual schedule.',
},
INVOICE_CREATE_ROT_RUT_PERSONNUMMER_INVALID: {
httpStatus: 400,
message_sv: 'Personnumret för ROT/RUT-avdraget är ogiltigt.',
@@ -1298,6 +1323,17 @@ const PROVIDER_MIGRATION: Record<string, StructuredErrorEntry> = {
message_sv: 'Tokensubmissionen misslyckades.',
message_en: 'Failed to submit provider token.',
},
PROVIDER_TOKEN_INVALID: {
// 422 (not 401): the UPSTREAM provider rejected the pasted credentials.
// The caller's own session is fine — a 401 here can trip client-side auth
// interceptors into logging the user out. Clients must dispatch on the
// error code, never on the HTTP status.
httpStatus: 422,
message_sv:
'Leverantören avvisade uppgifterna. Kontrollera att konto-ID och applikationstoken stämmer och försök igen.',
message_en:
'The provider rejected the credentials. Check that the account ID and application token are correct and try again.',
},
PROVIDER_PREVIEW_FAILED: {
httpStatus: 500,
message_sv: 'Förhandsgranskningen från leverantören misslyckades.',
@@ -1309,14 +1345,19 @@ const PROVIDER_MIGRATION: Record<string, StructuredErrorEntry> = {
message_en: 'Failed to fetch SIE data from the provider.',
},
PROVIDER_SIE_NO_YEARS: {
// The supported window is rolling (current year and the two before it) —
// the route interpolates the actual range via the messageSv/messageEn
// overrides on errorResponseFromCode(); this entry is the static fallback.
httpStatus: 404,
message_sv: 'Inga räkenskapsår 2024–2026 hittades hos leverantören.',
message_en: 'No fiscal years available for 2024–2026.',
message_sv: 'Inga räkenskapsår inom det stödda intervallet hittades hos leverantören.',
message_en: 'No fiscal years available within the supported range.',
},
PROVIDER_SIE_ONLY_FORTNOX: {
PROVIDER_SIE_NOT_SUPPORTED: {
httpStatus: 400,
message_sv: 'SIE-export stöds för närvarande endast för Fortnox.',
message_en: 'SIE export is currently only supported for Fortnox.',
message_sv:
'Den här leverantören stöder inte SIE-hämtning via API. Ladda upp en SIE-fil manuellt istället.',
message_en:
'This provider does not support fetching SIE via API. Upload a SIE file manually instead.',
},
PROVIDER_SIE_IMPORT_REQUIRED: {
httpStatus: 409,
@@ -1538,6 +1579,20 @@ const SUPPLIER_INVOICE_WAVE4: Record<string, StructuredErrorEntry> = {
message_en:
'No fiscal year covers the invoice date. Create the fiscal year first, or change the invoice date.',
},
SI_CREATE_ACCRUAL_REVERSE_CHARGE: {
httpStatus: 400,
message_sv:
'Periodisering kan inte kombineras med omvänd skattskyldighet. Kostnadsraden utgör momsunderlaget i momsdeklarationen (ruta 20–32), så nettobeloppet kan inte skjutas upp till ett interimskonto.',
message_en:
'Periodisering cannot be combined with reverse charge. The expense line carries the VAT base for the VAT declaration (boxes 20–32), so the net amount cannot be deferred to an interim account.',
},
SI_DELETE_HAS_BOOKING: {
httpStatus: 400,
message_sv:
'Leverantörsfakturan är bokförd eller har en periodisering och kan inte tas bort. Skapa en kreditfaktura i stället för att återställa bokföringen.',
message_en:
'The supplier invoice has a posted journal entry or an accrual schedule and cannot be deleted. Create a credit note instead to reverse the bookkeeping.',
},
SI_PAID_ALREADY: {
httpStatus: 409,
message_sv: 'Leverantörsfakturan är redan betald eller krediterad.',
@@ -2279,6 +2334,56 @@ const SKATTEVERKET: Record<string, StructuredErrorEntry> = {
},
}
// ─────────────────────────────────────────────────────────────────
// Bolagsverket filing codes (digital inlämning av årsredovisning)
// ─────────────────────────────────────────────────────────────────
const BOLAGSVERKET: Record<string, StructuredErrorEntry> = {
BOLAGSVERKET_API_ERROR: {
httpStatus: 502,
message_sv: 'Bolagsverkets tjänst svarade med ett fel. Se detaljerna och försök igen.',
message_en: 'The Bolagsverket API returned an error. See details for the upstream message.',
},
BOLAGSVERKET_SUBMISSION_EXISTS: {
httpStatus: 409,
message_sv:
'Det finns redan en aktiv inlämning av årsredovisningen för räkenskapsåret. Invänta Bolagsverkets besked innan du lämnar in på nytt.',
message_en:
'An active årsredovisning submission already exists for this fiscal period. Wait for Bolagsverket to resolve it before submitting again.',
},
BOLAGSVERKET_FORBIDDEN: {
httpStatus: 403,
message_sv: 'Otillräcklig behörighet för att lämna in årsredovisning för det här företaget.',
message_en:
'Insufficient role to file an årsredovisning for this company (viewer members cannot submit).',
},
BOLAGSVERKET_INVALID_ENVIRONMENT: {
httpStatus: 400,
message_sv: "Ogiltig Bolagsverket-miljö. Tillåtna värden: 'test', 'accept', 'prod'.",
message_en: "Invalid Bolagsverket environment. Allowed values: 'test', 'accept', 'prod'.",
},
BOLAGSVERKET_ENV_NOT_ALLOWED: {
httpStatus: 403,
message_sv:
'Den valda Bolagsverket-miljön är inte tillåten i den här installationen. Plattformens BOLAGSVERKET_ENV sätter taket.',
message_en:
'The selected Bolagsverket environment exceeds the platform ceiling set by BOLAGSVERKET_ENV (order: test < accept < prod; unset means test).',
},
BOLAGSVERKET_CONFIG_MISSING: {
httpStatus: 503,
message_sv:
'Serverkonfiguration saknas för Bolagsverket-integrationen. Kontakta administratören.',
message_en:
'Server configuration required by the Bolagsverket integration is missing (see details).',
},
BOLAGSVERKET_NO_SUBSCRIPTION: {
httpStatus: 404,
message_sv: 'Ingen händelseprenumeration finns för företaget ännu.',
message_en:
'No Bolagsverket event subscription exists for this company yet. One is created on the first submission.',
},
}
// ─────────────────────────────────────────────────────────────────
// Combined registry
// ─────────────────────────────────────────────────────────────────
@@ -2320,6 +2425,7 @@ const REGISTRY: Record<string, StructuredErrorEntry> = {
...API_KEY,
...PROVIDER,
...SKATTEVERKET,
...BOLAGSVERKET,
}
export function getErrorEntry(code: string): StructuredErrorEntry | undefined {