* 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
86 lines
2.6 KiB
TypeScript
86 lines
2.6 KiB
TypeScript
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { NextResponse } from 'next/server'
|
|
import { generateTrialBalance } from '@/lib/reports/trial-balance'
|
|
import {
|
|
reportToWorkbook,
|
|
textColumn,
|
|
currencyColumn,
|
|
integerColumn,
|
|
xlsxFilename,
|
|
} from '@/lib/reports/xlsx-export'
|
|
import type { TrialBalanceRow } from '@/types'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
export const GET = withRouteContext('report.trial_balance.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: period }, { data: companyRow }] = await Promise.all([
|
|
supabase
|
|
.from('fiscal_periods')
|
|
.select('period_start, period_end')
|
|
.eq('id', periodId)
|
|
.eq('company_id', companyId)
|
|
.single(),
|
|
supabase
|
|
.from('company_settings')
|
|
.select('company_name')
|
|
.eq('company_id', companyId)
|
|
.single(),
|
|
])
|
|
|
|
if (!period) {
|
|
return NextResponse.json({ error: 'Räkenskapsperioden kunde inte läsas.' }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
const report = await generateTrialBalance(supabase, companyId, periodId)
|
|
|
|
const buffer = reportToWorkbook<TrialBalanceRow>([
|
|
{
|
|
name: 'Saldobalans',
|
|
columns: [
|
|
textColumn('Konto'),
|
|
textColumn('Kontonamn'),
|
|
integerColumn('Klass'),
|
|
currencyColumn('IB Debet'),
|
|
currencyColumn('IB Kredit'),
|
|
currencyColumn('Period Debet'),
|
|
currencyColumn('Period Kredit'),
|
|
currencyColumn('UB Debet'),
|
|
currencyColumn('UB Kredit'),
|
|
],
|
|
rows: report.rows,
|
|
mapRow: (r) => [
|
|
r.account_number,
|
|
r.account_name,
|
|
r.account_class,
|
|
r.opening_debit,
|
|
r.opening_credit,
|
|
r.period_debit,
|
|
r.period_credit,
|
|
r.closing_debit,
|
|
r.closing_credit,
|
|
],
|
|
},
|
|
])
|
|
|
|
const filename = xlsxFilename('saldobalans', companyRow?.company_name ?? '', period.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 saldobalans' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
})
|