Correcting a single voucher cleared the momsdeklaration's RC_BASIS_MISSING error and the whole per-voucher worklist with it: the check tested mere presence of ruta 20-24 basis, the stepper re-derived its landing step and yanked the user to Granska mid-work, and the remounted checks card never refetched gaps once the aggregate check stopped firing. The declaration then claimed "klart" while the remaining vouchers still under-reported rutor 20-24 (FK004). - Make RC_BASIS_MISSING/RC_OUTPUT_MISSING proportional: compare reported basis against the basis the per-rate output boxes imply (moms/sats), with a 0.5% + 1 kr tolerance for per-voucher ore rounding. - Fetch the rc-basis-gaps worklist once per period, ungated from the aggregate check, so remaining rows survive remounts. - Latch the automatic stepper landing once per period so a refetch after a korrigering cannot navigate the user off Kontrollera. - Resolve rc-basis-gaps against the rakenskapsar (fiscal_period_id) for helarsmoms, matching the declaration totals; a calendar span hid gap vouchers in the tail of an extended first year. Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
54 lines
2.1 KiB
TypeScript
54 lines
2.1 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
import { findRcBasisGaps } from '@/lib/reports/rc-basis-gaps'
|
|
import type { VatPeriodType } from '@/types'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
export const GET = withRouteContext(
|
|
'report.vat_declaration.rc_basis_gaps',
|
|
async (request, ctx) => {
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const periodType = searchParams.get('periodType') as VatPeriodType | null
|
|
const yearStr = searchParams.get('year')
|
|
const periodStr = searchParams.get('period')
|
|
// Yearly (helårsmoms) resolves against the räkenskapsår, mirroring the
|
|
// declaration route; monthly/quarterly ignore it.
|
|
const fiscalPeriodId = searchParams.get('fiscal_period_id') ?? undefined
|
|
|
|
if (!periodType || !yearStr || !periodStr) {
|
|
return errorResponseFromCode('VAT_REPORT_MISSING_PARAMS', log, { requestId })
|
|
}
|
|
if (!['monthly', 'quarterly', 'yearly'].includes(periodType)) {
|
|
return errorResponseFromCode('VAT_REPORT_INVALID_PERIOD_TYPE', log, {
|
|
requestId,
|
|
details: { received: periodType },
|
|
})
|
|
}
|
|
|
|
const year = parseInt(yearStr, 10)
|
|
const period = parseInt(periodStr, 10)
|
|
if (isNaN(year) || isNaN(period)) {
|
|
return errorResponseFromCode('VAT_REPORT_INVALID_PERIOD', log, {
|
|
requestId,
|
|
details: { year: yearStr, period: periodStr },
|
|
})
|
|
}
|
|
|
|
try {
|
|
const gaps = await findRcBasisGaps(supabase, companyId, periodType, year, period, {
|
|
fiscalPeriodId,
|
|
})
|
|
return NextResponse.json({ data: { gaps } })
|
|
} catch (err) {
|
|
log.error('rc-basis-gaps detection failed', err as Error, { periodType, year, period })
|
|
return errorResponseFromCode('VAT_REPORT_GENERATION_FAILED', log, {
|
|
requestId,
|
|
details: { reason: err instanceof Error ? getUserErrorMessage(err) : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
)
|