* 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
79 lines
3.0 KiB
TypeScript
79 lines
3.0 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { renderToBuffer } from '@react-pdf/renderer'
|
|
import { generateSupplierLedger } from '@/lib/reports/supplier-ledger'
|
|
import { ReskontraPDF } from '@/lib/reports/reskontra-pdf-template'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { slugifyCompanyName } from '@/lib/reports/xlsx-export'
|
|
import type { CompanySettings } from '@/types'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
export const GET = withRouteContext('report.supplier_ledger.pdf', async (request, { supabase, companyId }) => {
|
|
const { searchParams } = new URL(request.url)
|
|
const asOfParam = searchParams.get('as_of_date')
|
|
|
|
if (asOfParam && !/^\d{4}-\d{2}-\d{2}$/.test(asOfParam)) {
|
|
return NextResponse.json({ error: 'as_of_date måste vara på formatet ÅÅÅÅ-MM-DD' }, { status: 400 })
|
|
}
|
|
const asOfDate = asOfParam ?? new Date().toISOString().slice(0, 10)
|
|
|
|
const { data: companyRow } = await supabase
|
|
.from('company_settings')
|
|
.select('*')
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!companyRow) {
|
|
return NextResponse.json({ error: 'Företagsinställningar saknas' }, { status: 404 })
|
|
}
|
|
|
|
try {
|
|
const ledger = await generateSupplierLedger(supabase, companyId, asOfDate)
|
|
|
|
const pdfBuffer = await renderToBuffer(
|
|
ReskontraPDF({
|
|
title: 'Leverantörsreskontra',
|
|
counterpartyLabel: 'Leverantör',
|
|
asOfDate,
|
|
aging: ledger.entries.map((e) => ({
|
|
name: e.supplier_name,
|
|
current: e.current,
|
|
days_1_30: e.days_1_30,
|
|
days_31_60: e.days_31_60,
|
|
days_61_90: e.days_61_90,
|
|
days_90_plus: e.days_90_plus,
|
|
total_outstanding: e.total_outstanding,
|
|
})),
|
|
totals: {
|
|
name: 'Summa',
|
|
current: ledger.total_current,
|
|
days_1_30: ledger.entries.reduce((s, e) => s + e.days_1_30, 0),
|
|
days_31_60: ledger.entries.reduce((s, e) => s + e.days_31_60, 0),
|
|
days_61_90: ledger.entries.reduce((s, e) => s + e.days_61_90, 0),
|
|
days_90_plus: ledger.entries.reduce((s, e) => s + e.days_90_plus, 0),
|
|
total_outstanding: ledger.total_outstanding,
|
|
},
|
|
unpaidCount: ledger.unpaid_count,
|
|
unconvertedFxCount: ledger.unconverted_fx_count,
|
|
company: companyRow as CompanySettings,
|
|
generatedAt: new Date().toISOString(),
|
|
})
|
|
)
|
|
|
|
const companySlug = slugifyCompanyName(companyRow.company_name ?? '')
|
|
const parts = ['leverantorsreskontra', companySlug, asOfDate.replace(/-/g, '')].filter(Boolean)
|
|
const filename = `${parts.join('-')}.pdf`
|
|
|
|
return new Response(new Uint8Array(pdfBuffer), {
|
|
headers: {
|
|
'Content-Type': 'application/pdf',
|
|
'Content-Disposition': `attachment; filename="${filename}"`,
|
|
},
|
|
})
|
|
} catch (err) {
|
|
return NextResponse.json(
|
|
{ error: err instanceof Error ? getUserErrorMessage(err) : 'Kunde inte generera leverantörsreskontra' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
})
|