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
58 lines
2.2 KiB
TypeScript
58 lines
2.2 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { generateDimensionPnl } from '@/lib/reports/dimension-pnl'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
import { parseReportDateRange } from '@/lib/reports/date-range'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
// Resultat per projekt/kostnadsställe: value-as-column P&L matrix over one
|
|
// SIE dimension. ?dim_no picks the dimension (default 6, projekt).
|
|
export const GET = withRouteContext(
|
|
'report.dimension_pnl',
|
|
async (request, ctx) => {
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const periodId = searchParams.get('period_id')
|
|
const dimNo = searchParams.get('dim_no') ?? '6'
|
|
|
|
if (!periodId) {
|
|
return errorResponseFromCode('REPORT_PERIOD_REQUIRED', log, { requestId })
|
|
}
|
|
if (!/^[1-9]\d{0,3}$/.test(dimNo)) {
|
|
return NextResponse.json({ error: 'dim_no must be an SIE dimension number' }, { status: 400 })
|
|
}
|
|
|
|
const { data: period } = await supabase
|
|
.from('fiscal_periods')
|
|
.select('period_start, period_end')
|
|
.eq('id', periodId)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!period) {
|
|
return NextResponse.json({ error: 'Fiscal period not found' }, { status: 404 })
|
|
}
|
|
|
|
const parsed = parseReportDateRange(searchParams, period)
|
|
if (!parsed.ok) {
|
|
return NextResponse.json({ error: parsed.error }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
// Only toDate: the matrix is cumulative from period_start by design
|
|
// (closing-balance semantics; see lib/reports/dimension-pnl.ts).
|
|
const data = await generateDimensionPnl(supabase, companyId!, periodId, dimNo, {
|
|
toDate: parsed.range.toDate,
|
|
})
|
|
return NextResponse.json({ data })
|
|
} catch (err) {
|
|
log.error('dimension pnl generation failed', err as Error, { periodId, dimNo })
|
|
return errorResponseFromCode('REPORT_GENERATION_FAILED', log, {
|
|
requestId,
|
|
details: { reason: err instanceof Error ? getUserErrorMessage(err) : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
)
|