* feat: add option to exclude year-end closing entries in SIE export and related reports * delete docs * fix: allow Chrome's PDF viewer in verifikat document preview The /api/documents/:id/inline route shipped with `object-src 'none'` in its CSP, which blocked Chrome's built-in PDF viewer (it renders inline PDFs via an internal <embed>). Users on Chrome saw "Det här innehållet har blockerats" when expanding a PDF attachment in the bookkeeping view; Firefox (PDF.js) and Edge (own viewer) were unaffected, and JPGs worked because <img> isn't subject to object-src. Drops the CSP for this route to the minimum needed for embeddability: `frame-ancestors 'self'`. X-Content-Type-Options: nosniff plus the fixed Content-Type from the handler already block MIME confusion; X-Frame-Options: SAMEORIGIN + frame-ancestors still block clickjacking. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(auth): add webmail deep link to email confirmation screens Mirrors Stripe's signup UX: after asking the user to verify their email, detect their webmail provider from the domain and show a button that opens the inbox in a new tab. Gmail gets a from:<sender> search pre-populated; Outlook/Yahoo/iCloud/Proton open the inbox directly. Unknown / custom domains fall back to the existing copy. Sender address is configurable via NEXT_PUBLIC_BRANDING_AUTH_EMAIL_FROM (default noreply@gnubok.se) so white-label installs can match their Supabase Auth SMTP config. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(auth): unblock first-time password set for BankID users with MFA Supabase rejects updateUser({password}) and mfa.unenroll with "AAL2 session is required" whenever a TOTP factor is enrolled. BankID magic-link logins produce AAL1, and middleware skips MFA enforcement for bankid_linked users, so they had no path to AAL2 — leaving them unable to set a backup password or disable MFA without going through the email-recovery escape hatch. - /api/account/password: branch on app_metadata.has_password. First-time set writes via service.auth.admin.updateUserById (no existing credential to protect, AAL2 guard does not apply). Change-password keeps the user-session updateUser so AAL2 still fires for credential rotation. - /mfa/verify: accept a safeReturnTo query param and route there after successful verify, so step-up flows can land back where they came from. - SecuritySettings: detect the AAL2 error from both change-password and mfa.unenroll and redirect through /mfa/verify?returnTo=/settings/account instead of toasting a dead-end error. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Add tests and rounding utility for öre precision in bokslut calculations - Implemented `roundOre` function for rounding SEK amounts to two decimal places, ensuring consistent monetary calculations. - Introduced `ORE_TOLERANCE` constant for comparing rounded amounts, facilitating invariant checks in financial entries. - Created comprehensive tests for `roundOre`, covering typical cases, edge cases, and idempotency. - Added year-end invariants tests to verify database-level guarantees for closing entries, ensuring they balance to the öre and reject discrepancies. - Developed end-to-end tests for the dispositions chain, validating the correctness of calculations across various scenarios. * fix: update PDF rendering to remove Swish QR code generation and set default to disable Swish visibility * fix: enhance security by rejecting data URIs in safeReturnTo function tests * fix: improve rounding logic in roundOre function and add customer_type migration * fix: add customer_type column to customers and enforce CHECK constraint --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
257 lines
9.0 KiB
TypeScript
257 lines
9.0 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { NextResponse } from 'next/server'
|
|
import { generateIncomeStatement } from '@/lib/reports/income-statement'
|
|
import { generateTrialBalance } from '@/lib/reports/trial-balance'
|
|
import { generateARLedger } from '@/lib/reports/ar-ledger'
|
|
import { generateMonthlyBreakdown } from '@/lib/reports/monthly-breakdown'
|
|
import {
|
|
calculateCashPosition,
|
|
calculateGrossMargin,
|
|
calculateExpenseRatio,
|
|
calculateAvgPaymentDays,
|
|
} from '@/lib/reports/kpi'
|
|
import { requireCompanyId } from '@/lib/company/context'
|
|
import {
|
|
reportToWorkbook,
|
|
textColumn,
|
|
currencyColumn,
|
|
percentColumn,
|
|
integerColumn,
|
|
xlsxFilename,
|
|
} from '@/lib/reports/xlsx-export'
|
|
|
|
interface KpiKv {
|
|
label: string
|
|
value: number | null
|
|
}
|
|
|
|
interface MonthRow {
|
|
label: string
|
|
income: number
|
|
expenses: number
|
|
net: number
|
|
}
|
|
|
|
interface CompositionRow {
|
|
klass: string
|
|
amount: number
|
|
}
|
|
|
|
interface SupplierRow {
|
|
supplier_name: string
|
|
total: number
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
|
|
const companyId = await requireCompanyId(supabase, user.id)
|
|
|
|
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, is_closed')
|
|
.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: 'Fiscal period not found' }, { status: 404 })
|
|
}
|
|
|
|
try {
|
|
const [
|
|
incomeStatement,
|
|
trialBalanceResult,
|
|
arLedger,
|
|
monthlyBreakdown,
|
|
paidInvoicesResult,
|
|
topSuppliersResult,
|
|
] = await Promise.all([
|
|
generateIncomeStatement(supabase, companyId, periodId),
|
|
generateTrialBalance(supabase, companyId, periodId),
|
|
generateARLedger(supabase, companyId),
|
|
generateMonthlyBreakdown(supabase, companyId, periodId),
|
|
supabase
|
|
.from('invoices')
|
|
.select('invoice_date, paid_at')
|
|
.eq('company_id', companyId)
|
|
.eq('status', 'paid')
|
|
.not('paid_at', 'is', null),
|
|
supabase
|
|
.from('supplier_invoices')
|
|
.select('supplier_id, total_sek, total, supplier:suppliers(id, name)')
|
|
.eq('company_id', companyId)
|
|
.gte('invoice_date', period.period_start)
|
|
.lte('invoice_date', period.period_end)
|
|
.neq('status', 'credited'),
|
|
])
|
|
|
|
const cashPosition = calculateCashPosition(trialBalanceResult.rows)
|
|
const vatOutputAccounts = ['2611', '2621', '2631']
|
|
const vatInputAccounts = ['2641', '2645']
|
|
const outputVat = trialBalanceResult.rows
|
|
.filter((r) => vatOutputAccounts.includes(r.account_number))
|
|
.reduce((sum, r) => sum + (r.closing_credit - r.closing_debit), 0)
|
|
const inputVat = trialBalanceResult.rows
|
|
.filter((r) => vatInputAccounts.includes(r.account_number))
|
|
.reduce((sum, r) => sum + (r.closing_debit - r.closing_credit), 0)
|
|
const vatLiability = Math.round((outputVat - inputVat) * 100) / 100
|
|
|
|
const paidInvoices = (paidInvoicesResult.data ?? []).map((inv) => ({
|
|
invoice_date: inv.invoice_date as string,
|
|
paid_at: inv.paid_at as string,
|
|
}))
|
|
|
|
// Expense composition by BAS class (mirrors KPI JSON route logic).
|
|
const expenseComposition = trialBalanceResult.rows.reduce(
|
|
(acc, r) => {
|
|
if (r.account_class < 4 || r.account_class > 7) return acc
|
|
const amount = r.closing_debit - r.closing_credit
|
|
if (amount <= 0) return acc
|
|
if (r.account_class === 4) acc.class4 += amount
|
|
else if (r.account_class === 5) acc.class5 += amount
|
|
else if (r.account_class === 6) acc.class6 += amount
|
|
else if (r.account_class === 7) acc.class7 += amount
|
|
return acc
|
|
},
|
|
{ class4: 0, class5: 0, class6: 0, class7: 0 },
|
|
)
|
|
|
|
type SupplierInvoiceRow = {
|
|
supplier_id: string | null
|
|
total_sek: number | null
|
|
total: number | null
|
|
supplier: { id: string; name: string } | { id: string; name: string }[] | null
|
|
}
|
|
const supplierTotals = new Map<string, { name: string; total: number }>()
|
|
for (const row of (topSuppliersResult.data ?? []) as SupplierInvoiceRow[]) {
|
|
if (!row.supplier_id) continue
|
|
const supplier = Array.isArray(row.supplier) ? row.supplier[0] : row.supplier
|
|
if (!supplier?.name) continue
|
|
const amount = row.total_sek ?? null
|
|
if (amount == null) continue
|
|
const existing = supplierTotals.get(row.supplier_id)
|
|
if (existing) existing.total += amount
|
|
else supplierTotals.set(row.supplier_id, { name: supplier.name, total: amount })
|
|
}
|
|
const topSuppliers = Array.from(supplierTotals.values())
|
|
.map((v) => ({
|
|
supplier_name: v.name,
|
|
total: Math.round(v.total * 100) / 100,
|
|
}))
|
|
.sort((a, b) => b.total - a.total)
|
|
.slice(0, 7)
|
|
|
|
// Sheet 1: scalar KPIs, label + value. Currency by default; percent rows
|
|
// are split into a separate sheet so the formatting is unambiguous.
|
|
const currencyKpis: KpiKv[] = [
|
|
{ label: 'Årets resultat', value: incomeStatement.net_result },
|
|
{ label: 'Likvida medel', value: cashPosition },
|
|
{ label: 'Utestående kundfordringar', value: arLedger.total_outstanding },
|
|
{ label: 'Förfallna kundfordringar', value: arLedger.total_overdue },
|
|
{ label: 'Momsskuld (ruta 49)', value: vatLiability },
|
|
{ label: 'Totala intäkter', value: incomeStatement.total_revenue },
|
|
{ label: 'Totala kostnader', value: incomeStatement.total_expenses },
|
|
]
|
|
|
|
const percentKpis: KpiKv[] = [
|
|
// calculateGrossMargin returns percentage as `25.5` (i.e. percent units).
|
|
// The xlsx percent format expects fractional values (0.255 → 25.50%).
|
|
// Divide by 100 so the displayed value matches the in-app KPI tile.
|
|
{ label: 'Bruttomarginal', value: scaleToFraction(calculateGrossMargin(incomeStatement)) },
|
|
{ label: 'Kostnadsandel', value: scaleToFraction(calculateExpenseRatio(incomeStatement)) },
|
|
]
|
|
|
|
const integerKpis: KpiKv[] = [
|
|
{ label: 'Genomsnittliga betaldagar', value: calculateAvgPaymentDays(paidInvoices) },
|
|
]
|
|
|
|
const monthRows: MonthRow[] = monthlyBreakdown.months
|
|
|
|
const compositionRows: CompositionRow[] = [
|
|
{ klass: '4 — Material/varor', amount: Math.round(expenseComposition.class4 * 100) / 100 },
|
|
{ klass: '5 — Externa kostnader', amount: Math.round(expenseComposition.class5 * 100) / 100 },
|
|
{ klass: '6 — Externa kostnader', amount: Math.round(expenseComposition.class6 * 100) / 100 },
|
|
{ klass: '7 — Personalkostnader', amount: Math.round(expenseComposition.class7 * 100) / 100 },
|
|
]
|
|
|
|
const supplierRows: SupplierRow[] = topSuppliers
|
|
|
|
const buffer = reportToWorkbook([
|
|
{
|
|
name: 'Nyckeltal (kr)',
|
|
columns: [textColumn('Nyckeltal'), currencyColumn('Värde')],
|
|
rows: currencyKpis,
|
|
mapRow: (r) => [r.label, r.value],
|
|
},
|
|
{
|
|
name: 'Nyckeltal (%)',
|
|
columns: [textColumn('Nyckeltal'), percentColumn('Värde')],
|
|
rows: percentKpis,
|
|
mapRow: (r) => [r.label, r.value],
|
|
},
|
|
{
|
|
name: 'Nyckeltal (övrigt)',
|
|
columns: [textColumn('Nyckeltal'), integerColumn('Värde')],
|
|
rows: integerKpis,
|
|
mapRow: (r) => [r.label, r.value],
|
|
},
|
|
{
|
|
name: 'Månadsbrytning',
|
|
columns: [
|
|
textColumn('Månad'),
|
|
currencyColumn('Intäkter'),
|
|
currencyColumn('Kostnader'),
|
|
currencyColumn('Netto'),
|
|
],
|
|
rows: monthRows,
|
|
mapRow: (m) => [m.label, m.income, m.expenses, m.net],
|
|
},
|
|
{
|
|
name: 'Kostnadssammansättning',
|
|
columns: [textColumn('Kontoklass'), currencyColumn('Belopp')],
|
|
rows: compositionRows,
|
|
mapRow: (r) => [r.klass, r.amount],
|
|
},
|
|
{
|
|
name: 'Topp leverantörer',
|
|
columns: [textColumn('Leverantör'), currencyColumn('Totalt')],
|
|
rows: supplierRows,
|
|
mapRow: (r) => [r.supplier_name, r.total],
|
|
},
|
|
])
|
|
|
|
const filename = xlsxFilename('nyckeltal', 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 ? err.message : 'Kunde inte generera nyckeltalsrapport' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
function scaleToFraction(value: number | null): number | null {
|
|
return value === null ? null : Math.round(value) / 100
|
|
}
|