e11f70b347
* 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
72 lines
2.4 KiB
TypeScript
72 lines
2.4 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { generateINK2Declaration } from '@/lib/reports/ink2/ink2-engine'
|
|
import {
|
|
generateSRUSubmission,
|
|
getZipFilename,
|
|
} from '@/lib/reports/ink2/sru-generator'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
import { encodeISO88591 } from '@/lib/reports/sru-encoding'
|
|
import JSZip from 'jszip'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
/**
|
|
* GET /api/reports/ink2
|
|
*
|
|
* Query parameters:
|
|
* period_id: fiscal period id (required)
|
|
* format: 'json' (default) or 'sru' for SRU file download (ZIP with INFO.SRU + BLANKETTER.SRU)
|
|
*/
|
|
export const GET = withRouteContext(
|
|
'report.ink2',
|
|
async (request, ctx) => {
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const periodId = searchParams.get('period_id')
|
|
const format = searchParams.get('format') || 'json'
|
|
|
|
if (!periodId) {
|
|
return errorResponseFromCode('REPORT_PERIOD_REQUIRED', log, { requestId })
|
|
}
|
|
|
|
const opLog = log.child({ periodId, format })
|
|
|
|
try {
|
|
const declaration = await generateINK2Declaration(supabase, companyId!, periodId)
|
|
|
|
if (format === 'sru') {
|
|
const submission = generateSRUSubmission(declaration)
|
|
|
|
// Skatteverket requires ISO 8859-1 (Latin-1)
|
|
const infoBytes = encodeISO88591(submission.infoSru)
|
|
const blanketterBytes = encodeISO88591(submission.blanketterSru)
|
|
|
|
const zip = new JSZip()
|
|
zip.file('INFO.SRU', infoBytes)
|
|
zip.file('BLANKETTER.SRU', blanketterBytes)
|
|
|
|
const zipArrayBuffer = await zip.generateAsync({ type: 'arraybuffer' })
|
|
const filename = getZipFilename(declaration)
|
|
|
|
return new NextResponse(zipArrayBuffer, {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': 'application/zip',
|
|
'Content-Disposition': `attachment; filename="${filename}"`,
|
|
'X-Request-Id': requestId,
|
|
},
|
|
})
|
|
}
|
|
|
|
return NextResponse.json({ data: declaration })
|
|
} catch (err) {
|
|
opLog.error('ink2 declaration generation failed', err as Error)
|
|
return errorResponseFromCode('TAX_DECL_GENERATION_FAILED', opLog, {
|
|
requestId,
|
|
details: { reason: err instanceof Error ? getUserErrorMessage(err) : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
)
|