* 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
143 lines
4.2 KiB
TypeScript
143 lines
4.2 KiB
TypeScript
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { NextResponse } from 'next/server'
|
|
import { generateResultatrapport } from '@/lib/reports/resultatrapport'
|
|
import { parseReportDateRange } from '@/lib/reports/date-range'
|
|
import { parseDimensionFilterParams, dimensionFilterDisclosure, dimensionFilterFileSuffix } from '@/lib/reports/dimension-filter'
|
|
import {
|
|
reportToWorkbook,
|
|
textColumn,
|
|
currencyColumn,
|
|
xlsxFilename,
|
|
} from '@/lib/reports/xlsx-export'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
interface FlatRow {
|
|
group: string
|
|
account_number: string
|
|
account_name: string
|
|
current_period: number
|
|
prior_period: number
|
|
}
|
|
|
|
export const GET = withRouteContext('report.resultatrapport.xlsx', async (request, { supabase, companyId }) => {
|
|
const { searchParams } = new URL(request.url)
|
|
const periodId = searchParams.get('period_id')
|
|
|
|
if (!periodId) {
|
|
return NextResponse.json({ error: 'period_id is required' }, { status: 400 })
|
|
}
|
|
|
|
const [{ data: companyRow }, { data: period }] = await Promise.all([
|
|
supabase
|
|
.from('company_settings')
|
|
.select('company_name')
|
|
.eq('company_id', companyId)
|
|
.single(),
|
|
supabase
|
|
.from('fiscal_periods')
|
|
.select('period_start, period_end')
|
|
.eq('id', periodId)
|
|
.eq('company_id', companyId)
|
|
.single(),
|
|
])
|
|
|
|
let range: { fromDate?: string; toDate?: string } = {}
|
|
if (period) {
|
|
const parsed = parseReportDateRange(searchParams, period)
|
|
if (!parsed.ok) {
|
|
return NextResponse.json({ error: parsed.error }, { status: 400 })
|
|
}
|
|
range = parsed.range
|
|
}
|
|
|
|
const dimFilter = parseDimensionFilterParams(searchParams)
|
|
if (!dimFilter.ok) {
|
|
return NextResponse.json({ error: dimFilter.error }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
const report = await generateResultatrapport(supabase, companyId, periodId, {
|
|
...range,
|
|
dimensions: dimFilter.dimensions,
|
|
})
|
|
|
|
const rows: FlatRow[] = []
|
|
for (const g of report.groups) {
|
|
for (const r of g.rows) {
|
|
rows.push({
|
|
group: g.class_label,
|
|
account_number: r.account_number,
|
|
account_name: r.account_name,
|
|
current_period: r.current_period,
|
|
prior_period: r.prior_period,
|
|
})
|
|
}
|
|
rows.push({
|
|
group: g.class_label,
|
|
account_number: '',
|
|
account_name: `Summa ${g.class_label}`,
|
|
current_period: g.subtotal_current,
|
|
prior_period: g.subtotal_prior,
|
|
})
|
|
}
|
|
rows.push({
|
|
group: 'Resultat',
|
|
account_number: '',
|
|
account_name: 'Årets resultat',
|
|
current_period: report.net_result_current,
|
|
prior_period: report.net_result_prior,
|
|
})
|
|
|
|
// Partial-view disclosure survives the file boundary: a filtered export
|
|
// must never be mistakable for the authoritative report (BFNAR 2013:2).
|
|
const disclosure = dimensionFilterDisclosure(dimFilter.dimensions)
|
|
if (disclosure) {
|
|
rows.unshift({
|
|
group: disclosure,
|
|
account_number: '',
|
|
account_name: '',
|
|
current_period: null as unknown as number,
|
|
prior_period: null as unknown as number,
|
|
})
|
|
}
|
|
|
|
const buffer = reportToWorkbook<FlatRow>([
|
|
{
|
|
name: 'Resultatrapport',
|
|
columns: [
|
|
textColumn('Grupp'),
|
|
textColumn('Konto'),
|
|
textColumn('Kontonamn'),
|
|
currencyColumn('Aktuell period'),
|
|
currencyColumn('Föregående period'),
|
|
],
|
|
rows,
|
|
mapRow: (r) => [
|
|
r.group,
|
|
r.account_number,
|
|
r.account_name,
|
|
r.current_period,
|
|
r.prior_period,
|
|
],
|
|
},
|
|
])
|
|
|
|
const filename = xlsxFilename(
|
|
`resultatrapport${dimensionFilterFileSuffix(dimFilter.dimensions)}`,
|
|
companyRow?.company_name ?? '',
|
|
report.period.end,
|
|
)
|
|
return new NextResponse(new Uint8Array(buffer), {
|
|
headers: {
|
|
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
'Content-Disposition': `attachment; filename="${filename}"`,
|
|
},
|
|
})
|
|
} catch (err) {
|
|
return NextResponse.json(
|
|
{ error: err instanceof Error ? getUserErrorMessage(err) : 'Kunde inte generera resultatrapport' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
})
|