* feat(invoices): implement öresavrundning logic and next invoice number preview - Added `getDisplayTotal` utility to handle rounding for SEK invoices based on company settings. - Updated `InvoicesPage` to utilize the new rounding logic when displaying totals. - Introduced `peek_next_invoice_number` function to allow previewing the next invoice number without consuming the sequence. - Modified invoice number generation to remove the year prefix and prevent truncation of numbers exceeding three digits. - Enhanced tests for invoice number generation and rounding functionality to ensure correctness. - Updated PDF template to reflect new rounding logic for totals and display appropriate values. - Adjusted company switcher to hide options in sandbox mode. - Improved error handling and logging in sandbox seeding process. * fix(skatteverket): remove unused scope labels from SCOPE_LABELS and DEFAULT_SCOPES
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponse } from '@/lib/errors/get-structured-error'
|
|
|
|
export const GET = withRouteContext(
|
|
'invoice.peek_next_number',
|
|
async (request, ctx) => {
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
const url = new URL(request.url)
|
|
const documentType = url.searchParams.get('document_type') ?? 'invoice'
|
|
if (!['invoice', 'proforma', 'delivery_note'].includes(documentType)) {
|
|
return NextResponse.json(
|
|
{ error: 'invalid document_type', requestId },
|
|
{ status: 400 },
|
|
)
|
|
}
|
|
|
|
// delivery_note has its own sequence (generate_delivery_note_number); the
|
|
// peek RPC only covers the invoice/proforma F-series counter, so for
|
|
// delivery notes we return null and let the form skip the preview.
|
|
if (documentType === 'delivery_note') {
|
|
return NextResponse.json({ data: { preview: null } })
|
|
}
|
|
|
|
const { data, error } = await supabase.rpc('peek_next_invoice_number', {
|
|
p_company_id: companyId,
|
|
p_document_type: documentType,
|
|
})
|
|
|
|
if (error) {
|
|
log.error('peek_next_invoice_number failed', error)
|
|
return errorResponse(error, log, { requestId })
|
|
}
|
|
|
|
return NextResponse.json({ data: { preview: data ?? null } })
|
|
},
|
|
)
|