Files
accounted/app/api/bookkeeping/fiscal-periods/[id]/arsredovisning/ixbrl/route.ts
T
Mattsson e11f70b347 Bug/gh issues fiz (#1103)
* refactor: optimize page loading and data fetching

* fix: resolve recurring production runtime errors

* feat: add MCP company and customer updates

* fix: handle year-end tax adjustments

* feat: harden annual report compliance

* fix: expand invoice logo and font support

* fix: sanitize API route error responses

* fix: sanitize user-facing error messages

* feat: persist onboarding and tax assessment notices

* fix: reduce cloud backup audit churn

* feat: refine invoice editor layout

* fix: show saved tax adjustments in INK2

* fix: complete annual report API mappings

* docs: record operational safeguards and decisions

* fix: harden annual report review findings

* fix: adjust column span for description based on VAT registration

* New css class name
2026-07-21 23:00:15 +02:00

68 lines
3.1 KiB
TypeScript

import { withRouteContext } from '@/lib/api/with-route-context'
import { errorResponse, errorResponseFromCode } from '@/lib/errors/get-structured-error'
import { buildIxbrlInput } from '@/lib/bokslut/ixbrl/build-input'
import { generateK2IxbrlDocument } from '@/lib/bokslut/ixbrl/document/k2-document'
import { getVersionIxbrlInput } from '@/lib/bokslut/arsredovisning/version-ixbrl'
/**
* GET /api/bookkeeping/fiscal-periods/:id/arsredovisning/ixbrl
*
* Generates the iXBRL (XHTML) årsredovisning for the period. The document IS
* the presentation (per TILLAMPNINGSANVISNING): the wizard renders it in an
* iframe as the authoritative preview, and `?download=1` exports the same
* bytes for validation and the company's archive. The XHTML file is not a
* standalone manual filing path: digital filing goes through connected
* software and the paper fallback is a certified copy sent by post.
*
* Query params:
* - download=1 → Content-Disposition: attachment
* - utdelning=N → proposed dividend in whole SEK for the resultatdisposition
*/
export const GET = withRouteContext(
'period.arsredovisning_ixbrl',
async (request, ctx, { params }: { params: Promise<{ id: string }> }) => {
const { id } = await params
const { supabase, companyId, log, requestId } = ctx
try {
const url = new URL(request.url)
const download = url.searchParams.get('download') === '1'
const versionId = url.searchParams.get('version')
const utdelningRaw = url.searchParams.get('utdelning')
const proposedDividend = utdelningRaw ? Number(utdelningRaw) : undefined
const input = versionId
? await getVersionIxbrlInput(supabase, companyId, id, versionId)
: await buildIxbrlInput(supabase, companyId, id, {
proposedDividend:
proposedDividend !== undefined && Number.isFinite(proposedDividend)
? proposedDividend
: undefined,
})
if (!input) return errorResponseFromCode('NOT_FOUND', log, { requestId })
const { xhtml, warnings } = generateK2IxbrlDocument(input)
const safePeriodEnd = input.period.end.replace(/[^\w.-]/g, '_')
const filename = `arsredovisning-${safePeriodEnd}.xhtml`
return new Response(xhtml, {
headers: {
// Served as XHTML so iframe preview renders the inline XBRL
// document exactly as Bolagsverket will present it.
'Content-Type': 'application/xhtml+xml; charset=utf-8',
'Content-Disposition': `${download ? 'attachment' : 'inline'}; filename="${filename}"`,
'Cache-Control': 'private, no-store, no-cache, must-revalidate',
Pragma: 'no-cache',
// Generation warnings surfaced without disturbing the body.
'X-Ixbrl-Warning-Count': String(warnings.length),
...(versionId ? { 'X-Annual-Report-Version': versionId } : {}),
},
})
} catch (err) {
const message = err instanceof Error ? err.message : ''
if (/not found/i.test(message)) {
return errorResponseFromCode('PERIOD_NOT_FOUND', log, { requestId })
}
return errorResponse(err, log, { requestId })
}
},
)