Copy voucher, MRU booking templates, and PDF export for reports (#303)
* feat: copy voucher, MRU booking templates, and PDF export for reports - Add "Kopiera verifikat" action on the journal-entry detail page that prefills a new draft with the source entry's lines, description, and notes. Date defaults to today so locked-period posts can't happen by accident; source_type resets to manual. - Track per-company MRU for booking_template_library rows via a new booking_template_usage table (fire-and-forget touch endpoint hooked into both pickers) and sort the list most-recently-used first for the active company. - Generate downloadable PDFs for balansräkning and resultaträkning using the existing @react-pdf/renderer toolchain. Adds a reusable parameterized template and two API routes, with download buttons on the matching report views. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: address PR review feedback on copy-voucher + report PDFs Compliance review (Swedish accounting): - Balance-sheet PDF now refuses to render when tillgångar ≠ eget kapital och skulder; the stale "Differens" summary row is gone. The on-screen view still surfaces the existing "Balanserar ej" warning so users can diagnose the imbalance before downloading. ÅRL 3 kap / K2 / K3 require exact balance. - Both PDF routes now 400 when the requested fiscal period cannot be resolved — identifiable period is part of räkenskapsinformation under BFL 7 kap. - Income-statement PDF adds the mandatory "Resultat efter finansiella poster" subtotal when financial items are present, per K2/K3 uppställningsform (ÅRL bilaga 2). - Copy-voucher flow now shows a clear banner ("Kopia av verifikat X — nytt, fristående verifikat skapas") so users cannot mistake the copy for a rättelse/storno. Code review (Greptile): - New migration adds updated_at column + trigger to booking_template_usage (project convention; applied to the Supabase project). - Replace localeCompare on ISO timestamps with plain relational comparison to avoid any locale-dependent ordering. - UUID-format validation on the copy_from query param before it goes into the fetch URL. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: second round of Swedish compliance fixes on report PDFs - Balance-sheet PDF imbalance check now compares rounded-to-whole-kronor totals (SFL 22:1 convention). The previous 0.5-öre tolerance could reject a legitimate balance sheet when accumulated floating-point noise across hundreds of ledger lines exceeded the threshold. The on-screen view still surfaces the öre-precise "Balanserar ej" badge for diagnostic visibility. - Both PDFs now carry a prominent "Arbetsutkast — ej undertecknat" notice per ÅRL 2 kap 7 §. Prevents a downloaded PDF from being mistaken for or filed as an approved årsredovisning. - Income-statement PDF now follows K2/K3 uppställningsform (ÅRL bilaga 2) by splitting class 8 into three blocks with named subtotals: Finansiella poster (80–84), Bokslutsdispositioner (88), Skatter (89). The summary now always shows a "Skatt på årets resultat" row so the reader can verify the tax calculation, and adds "Resultat efter finansiella poster" / "Bokslutsdispositioner" subtotals when each block is present. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: harden report PDFs against out-of-band filing + future BAS growth - Append "-utkast" to downloaded PDF filenames. The filename survives the PDF's disclaimer context — a file named balansrakning-2026-01-01.pdf in a Downloads folder or forwarded attachment is ambiguous, whereas balansrakning-2026-01-01-utkast.pdf makes the draft status legible even without opening the document. - Add a catch-all "Övriga finansiella poster" bucket in the income-statement PDF for any class-8 section whose account prefix isn't one of the known K2/K3 blocks (80–84 / 88 / 89). Counted in the "Resultat efter finansiella poster" subtotal so arithmetic stays consistent. Future-proofs the PDF against a generator change that starts emitting 85–87 sections. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -7,7 +7,7 @@ import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { AccountNumber } from '@/components/ui/account-number'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Loader2, ArrowLeft, Paperclip, AlertTriangle, Lock, MessageSquare, Pencil, Check, X } from 'lucide-react'
|
||||
import { Loader2, ArrowLeft, Paperclip, AlertTriangle, Lock, MessageSquare, Pencil, Check, X, Copy } from 'lucide-react'
|
||||
import { useCanWrite } from '@/lib/hooks/use-can-write'
|
||||
import JournalEntryAttachments from '@/components/bookkeeping/JournalEntryAttachments'
|
||||
import JournalEntryStatusBadge, { sourceTypeLabels } from '@/components/bookkeeping/JournalEntryStatusBadge'
|
||||
@@ -205,6 +205,17 @@ export default function JournalEntryDetailPage({ params }: { params: Promise<{ i
|
||||
Skapa ändringsverifikation
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="w-full sm:w-auto"
|
||||
onClick={() => router.push(`/bookkeeping?copy_from=${entry.id}`)}
|
||||
disabled={!canWrite}
|
||||
title={!canWrite ? 'Du har endast läsbehörighet i detta företag' : undefined}
|
||||
>
|
||||
{!canWrite ? <Lock className="mr-2 h-4 w-4" /> : <Copy className="mr-2 h-4 w-4" />}
|
||||
Kopiera verifikat
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -1,19 +1,94 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useState, useEffect } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import JournalEntryList from '@/components/bookkeeping/JournalEntryList'
|
||||
import JournalEntryForm from '@/components/bookkeeping/JournalEntryForm'
|
||||
import JournalEntryForm, { type FormLine } from '@/components/bookkeeping/JournalEntryForm'
|
||||
import ChartOfAccountsManager from '@/components/bookkeeping/ChartOfAccountsManager'
|
||||
import { FiscalYearSelector } from '@/components/common/FiscalYearSelector'
|
||||
import { Lock } from 'lucide-react'
|
||||
import { useToast } from '@/components/ui/use-toast'
|
||||
import { Lock, Loader2, Copy } from 'lucide-react'
|
||||
import type { JournalEntry, JournalEntryLine } from '@/types'
|
||||
|
||||
interface CopyPrefill {
|
||||
sourceId: string
|
||||
sourceVoucherLabel: string
|
||||
lines: FormLine[]
|
||||
description: string
|
||||
notes: string
|
||||
}
|
||||
|
||||
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
||||
|
||||
function readCopyFromParam(): string | null {
|
||||
if (typeof window === 'undefined') return null
|
||||
const raw = new URLSearchParams(window.location.search).get('copy_from')
|
||||
if (!raw) return null
|
||||
// Guard against path-traversal or other malformed input in the fetch URL.
|
||||
return UUID_RE.test(raw) ? raw : null
|
||||
}
|
||||
|
||||
export default function BookkeepingPage() {
|
||||
const { toast } = useToast()
|
||||
const [refreshKey, setRefreshKey] = useState(0)
|
||||
const [activeTab, setActiveTab] = useState('journal')
|
||||
const [copyFromId] = useState<string | null>(readCopyFromParam)
|
||||
const [activeTab, setActiveTab] = useState(() =>
|
||||
copyFromId ? 'new-entry' : 'journal',
|
||||
)
|
||||
const [periodId, setPeriodId] = useState<string | null>(null)
|
||||
const [copyPrefill, setCopyPrefill] = useState<CopyPrefill | null>(null)
|
||||
const [isLoadingCopy, setIsLoadingCopy] = useState<boolean>(() => copyFromId !== null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!copyFromId) return
|
||||
|
||||
fetch(`/api/bookkeeping/journal-entries/${copyFromId}`)
|
||||
.then((res) => res.json())
|
||||
.then(({ data, error }: { data?: JournalEntry; error?: string }) => {
|
||||
if (error || !data) {
|
||||
toast({
|
||||
title: 'Kunde inte kopiera verifikat',
|
||||
description: error || 'Källverifikatet hittades inte.',
|
||||
variant: 'destructive',
|
||||
})
|
||||
return
|
||||
}
|
||||
const sourceLines = ((data.lines || []) as JournalEntryLine[])
|
||||
.slice()
|
||||
.sort((a, b) => a.sort_order - b.sort_order)
|
||||
const lines: FormLine[] = sourceLines.map((l) => {
|
||||
const debit = Number(l.debit_amount) || 0
|
||||
const credit = Number(l.credit_amount) || 0
|
||||
return {
|
||||
account_number: l.account_number,
|
||||
debit_amount: debit > 0 ? debit.toFixed(2) : '',
|
||||
credit_amount: credit > 0 ? credit.toFixed(2) : '',
|
||||
line_description: l.line_description || '',
|
||||
}
|
||||
})
|
||||
setCopyPrefill({
|
||||
sourceId: copyFromId,
|
||||
sourceVoucherLabel: `${data.voucher_series ?? ''}${data.voucher_number ?? ''}`,
|
||||
lines,
|
||||
description: data.description || '',
|
||||
notes: data.notes || '',
|
||||
})
|
||||
})
|
||||
.catch(() => {
|
||||
toast({
|
||||
title: 'Kunde inte kopiera verifikat',
|
||||
description: 'Källverifikatet kunde inte hämtas.',
|
||||
variant: 'destructive',
|
||||
})
|
||||
})
|
||||
.finally(() => {
|
||||
setIsLoadingCopy(false)
|
||||
// Clean the URL so a page refresh doesn't re-trigger the copy prefill.
|
||||
window.history.replaceState({}, '', '/bookkeeping')
|
||||
})
|
||||
}, [copyFromId, toast])
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
@@ -48,7 +123,40 @@ export default function BookkeepingPage() {
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="new-entry">
|
||||
<JournalEntryForm onCreated={() => setRefreshKey((k) => k + 1)} />
|
||||
{isLoadingCopy ? (
|
||||
<div className="flex items-center gap-2 py-12 justify-center text-muted-foreground">
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
<span className="text-sm">Laddar källverifikat...</span>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{copyPrefill && (
|
||||
<div className="mb-4 flex items-start gap-3 rounded-lg border border-border bg-muted/30 p-3 text-sm">
|
||||
<Copy className="h-4 w-4 mt-0.5 shrink-0 text-muted-foreground" />
|
||||
<div className="flex-1">
|
||||
<p className="font-medium">
|
||||
Kopia av verifikat {copyPrefill.sourceVoucherLabel || '(okänt nummer)'}
|
||||
</p>
|
||||
<p className="text-muted-foreground mt-0.5">
|
||||
Ett nytt, fristående verifikat skapas med egen verifikationsserie och nummer.
|
||||
Detta är <strong>inte</strong> en rättelse eller storno av originalet — använd
|
||||
"Skapa ändringsverifikation" om du vill korrigera källverifikatet.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<JournalEntryForm
|
||||
key={copyPrefill?.sourceId ?? 'fresh'}
|
||||
onCreated={() => {
|
||||
setRefreshKey((k) => k + 1)
|
||||
setCopyPrefill(null)
|
||||
}}
|
||||
initialLines={copyPrefill?.lines}
|
||||
initialDescription={copyPrefill?.description}
|
||||
initialNotes={copyPrefill?.notes}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="accounts">
|
||||
|
||||
@@ -617,6 +617,17 @@ function IncomeStatementView({ periodId, onNavigateToAccount }: { periodId: stri
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex justify-end">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => window.open(`/api/reports/income-statement/pdf?period_id=${periodId}`, '_blank')}
|
||||
>
|
||||
<Download className="h-4 w-4 mr-2" />
|
||||
Ladda ner PDF
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{!monthlyLoading && monthlyData.length > 0 && (
|
||||
<IncomeExpenseChart months={monthlyData} />
|
||||
)}
|
||||
@@ -751,6 +762,17 @@ function BalanceSheetView({ periodId, onNavigateToAccount }: { periodId: string;
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex justify-end">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => window.open(`/api/reports/balance-sheet/pdf?period_id=${periodId}`, '_blank')}
|
||||
>
|
||||
<Download className="h-4 w-4 mr-2" />
|
||||
Ladda ner PDF
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Assets */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { renderToBuffer } from '@react-pdf/renderer'
|
||||
import { generateBalanceSheet } from '@/lib/reports/balance-sheet'
|
||||
import { FinancialStatementPDF } from '@/lib/reports/financial-statement-pdf-template'
|
||||
import { requireCompanyId } from '@/lib/company/context'
|
||||
import type { CompanySettings } from '@/types'
|
||||
|
||||
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')
|
||||
.eq('id', periodId)
|
||||
.eq('company_id', companyId)
|
||||
.single(),
|
||||
supabase
|
||||
.from('company_settings')
|
||||
.select('*')
|
||||
.eq('company_id', companyId)
|
||||
.single(),
|
||||
])
|
||||
|
||||
if (!companyRow) {
|
||||
return NextResponse.json({ error: 'Företagsinställningar saknas' }, { status: 404 })
|
||||
}
|
||||
// An identifiable period is part of räkenskapsinformation (BFL 7 kap). Refuse
|
||||
// to render a PDF that can't be archived with the period it refers to.
|
||||
if (!period) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Räkenskapsperioden kunde inte läsas. Välj en befintlig period innan du genererar PDF.' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
const report = await generateBalanceSheet(supabase, companyId, periodId)
|
||||
report.period = { start: period.period_start, end: period.period_end }
|
||||
|
||||
const totalAssets = report.total_assets
|
||||
const totalEquityLiab = report.total_equity_liabilities
|
||||
|
||||
// ÅRL 3 kap / K2 / K3 require balansräkningen to balance. Compare rounded
|
||||
// to whole kronor — matches SFL 22:1's truncation convention for statutory
|
||||
// reports and is immune to floating-point accumulation across hundreds of
|
||||
// ledger lines (öresavrundning noise under half a krona is never a real
|
||||
// accounting error). The on-screen view still surfaces a "Balanserar ej"
|
||||
// warning at öre precision so users can diagnose smaller discrepancies.
|
||||
const diffInKronor = Math.abs(Math.round(totalAssets) - Math.round(totalEquityLiab))
|
||||
if (diffInKronor >= 1) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
'Balansräkningen balanserar inte (tillgångar ≠ eget kapital och skulder). Åtgärda differensen innan du genererar PDF.',
|
||||
},
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const pdfBuffer = await renderToBuffer(
|
||||
FinancialStatementPDF({
|
||||
title: 'Balansräkning',
|
||||
groups: [
|
||||
{
|
||||
heading: 'Tillgångar',
|
||||
sections: report.asset_sections,
|
||||
totalLabel: 'Summa tillgångar',
|
||||
total: totalAssets,
|
||||
},
|
||||
{
|
||||
heading: 'Eget kapital och skulder',
|
||||
sections: report.equity_liability_sections,
|
||||
totalLabel: 'Summa eget kapital och skulder',
|
||||
total: totalEquityLiab,
|
||||
},
|
||||
],
|
||||
period: report.period,
|
||||
company: companyRow as CompanySettings,
|
||||
generatedAt: new Date().toISOString(),
|
||||
})
|
||||
)
|
||||
|
||||
// "-utkast" suffix keeps the draft status visible even after the file
|
||||
// leaves the browser — complements the in-document ÅRL 2:7 disclaimer.
|
||||
const filename = `balansrakning-${report.period.start}-utkast.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 ? err.message : 'Kunde inte generera balansräkning' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { renderToBuffer } from '@react-pdf/renderer'
|
||||
import { generateIncomeStatement } from '@/lib/reports/income-statement'
|
||||
import { FinancialStatementPDF, type FinancialStatementGroup, type FinancialStatementSection, type FinancialStatementSummaryRow } from '@/lib/reports/financial-statement-pdf-template'
|
||||
import { requireCompanyId } from '@/lib/company/context'
|
||||
import type { CompanySettings } from '@/types'
|
||||
|
||||
// K2/K3 uppställningsform (ÅRL bilaga 2, kostnadsslagsindelad) splits class 8
|
||||
// into three named blocks with subtotals:
|
||||
// 80–84 → Finansiella poster (followed by "Resultat efter finansiella poster")
|
||||
// 88 → Bokslutsdispositioner
|
||||
// 89 → Skatt på årets resultat
|
||||
// The generator lumps these together under financial_sections, so we split
|
||||
// here by the first row's account prefix.
|
||||
const FINANSIELLA_POSTER_PREFIXES = ['80', '81', '82', '83', '84']
|
||||
const BOKSLUTSDISPOSITIONER_PREFIXES = ['88']
|
||||
const SKATT_PREFIXES = ['89']
|
||||
const KNOWN_CLASS_8_PREFIXES = [
|
||||
...FINANSIELLA_POSTER_PREFIXES,
|
||||
...BOKSLUTSDISPOSITIONER_PREFIXES,
|
||||
...SKATT_PREFIXES,
|
||||
]
|
||||
|
||||
function sectionPrefix(section: FinancialStatementSection, prefixes: string[]): boolean {
|
||||
if (section.rows.length === 0) return false
|
||||
const acc = section.rows[0].account_number
|
||||
return prefixes.some((p) => acc.startsWith(p))
|
||||
}
|
||||
|
||||
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')
|
||||
.eq('id', periodId)
|
||||
.eq('company_id', companyId)
|
||||
.single(),
|
||||
supabase
|
||||
.from('company_settings')
|
||||
.select('*')
|
||||
.eq('company_id', companyId)
|
||||
.single(),
|
||||
])
|
||||
|
||||
if (!companyRow) {
|
||||
return NextResponse.json({ error: 'Företagsinställningar saknas' }, { status: 404 })
|
||||
}
|
||||
// An identifiable period is part of räkenskapsinformation (BFL 7 kap). Refuse
|
||||
// to render a PDF that can't be archived with the period it refers to.
|
||||
if (!period) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Räkenskapsperioden kunde inte läsas. Välj en befintlig period innan du genererar PDF.' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
const report = await generateIncomeStatement(supabase, companyId, periodId)
|
||||
report.period = { start: period.period_start, end: period.period_end }
|
||||
|
||||
const operatingResult = Math.round((report.total_revenue - report.total_expenses) * 100) / 100
|
||||
|
||||
// Split class 8 into its three K2/K3 blocks plus a catch-all for any
|
||||
// prefix the generator emits but we haven't explicitly mapped. If a future
|
||||
// generator change adds sections for 85/86/87 or similar, this keeps them
|
||||
// visible and arithmetically accounted for rather than silently dropped.
|
||||
const finansiellaPosterSections = report.financial_sections.filter((s) =>
|
||||
sectionPrefix(s, FINANSIELLA_POSTER_PREFIXES),
|
||||
)
|
||||
const bokslutsdispositionerSections = report.financial_sections.filter((s) =>
|
||||
sectionPrefix(s, BOKSLUTSDISPOSITIONER_PREFIXES),
|
||||
)
|
||||
const skattSections = report.financial_sections.filter((s) =>
|
||||
sectionPrefix(s, SKATT_PREFIXES),
|
||||
)
|
||||
const ovrigaFinansiellaPosterSections = report.financial_sections.filter(
|
||||
(s) => !sectionPrefix(s, KNOWN_CLASS_8_PREFIXES),
|
||||
)
|
||||
|
||||
const totalFinansiellaPoster = Math.round(
|
||||
finansiellaPosterSections.reduce((sum, s) => sum + s.subtotal, 0) * 100,
|
||||
) / 100
|
||||
const totalBokslutsdispositioner = Math.round(
|
||||
bokslutsdispositionerSections.reduce((sum, s) => sum + s.subtotal, 0) * 100,
|
||||
) / 100
|
||||
const totalSkatt = Math.round(
|
||||
skattSections.reduce((sum, s) => sum + s.subtotal, 0) * 100,
|
||||
) / 100
|
||||
const totalOvrigaFinansiellaPoster = Math.round(
|
||||
ovrigaFinansiellaPosterSections.reduce((sum, s) => sum + s.subtotal, 0) * 100,
|
||||
) / 100
|
||||
// Catch-all is treated as part of "finansiella poster" for the subtotal —
|
||||
// 85-87 accounts in BAS are financial-adjacent (not tax, not bokslut).
|
||||
const resultatEfterFinansiellaPoster = Math.round(
|
||||
(operatingResult + totalFinansiellaPoster + totalOvrigaFinansiellaPoster) * 100,
|
||||
) / 100
|
||||
|
||||
const groups: FinancialStatementGroup[] = [
|
||||
{
|
||||
heading: 'Rörelseintäkter',
|
||||
sections: report.revenue_sections,
|
||||
totalLabel: 'Summa rörelseintäkter',
|
||||
total: report.total_revenue,
|
||||
},
|
||||
{
|
||||
heading: 'Rörelsekostnader',
|
||||
sections: report.expense_sections,
|
||||
totalLabel: 'Summa rörelsekostnader',
|
||||
total: report.total_expenses,
|
||||
negate: true,
|
||||
},
|
||||
]
|
||||
|
||||
if (finansiellaPosterSections.length > 0) {
|
||||
groups.push({
|
||||
heading: 'Finansiella poster',
|
||||
sections: finansiellaPosterSections,
|
||||
totalLabel: 'Summa finansiella poster',
|
||||
total: totalFinansiellaPoster,
|
||||
})
|
||||
}
|
||||
if (ovrigaFinansiellaPosterSections.length > 0) {
|
||||
groups.push({
|
||||
heading: 'Övriga finansiella poster',
|
||||
sections: ovrigaFinansiellaPosterSections,
|
||||
totalLabel: 'Summa övriga finansiella poster',
|
||||
total: totalOvrigaFinansiellaPoster,
|
||||
})
|
||||
}
|
||||
if (bokslutsdispositionerSections.length > 0) {
|
||||
groups.push({
|
||||
heading: 'Bokslutsdispositioner',
|
||||
sections: bokslutsdispositionerSections,
|
||||
totalLabel: 'Summa bokslutsdispositioner',
|
||||
total: totalBokslutsdispositioner,
|
||||
})
|
||||
}
|
||||
if (skattSections.length > 0) {
|
||||
groups.push({
|
||||
heading: 'Skatter',
|
||||
sections: skattSections,
|
||||
totalLabel: 'Summa skatter',
|
||||
total: totalSkatt,
|
||||
})
|
||||
}
|
||||
|
||||
// K2/K3 uppställningsform (ÅRL bilaga 2) summary structure:
|
||||
// Rörelseresultat
|
||||
// Resultat efter finansiella poster (only if finansiella poster present)
|
||||
// Bokslutsdispositioner (only if present)
|
||||
// Skatt på årets resultat (always, so the reader can verify the tax calc)
|
||||
// Årets resultat
|
||||
const summary: FinancialStatementSummaryRow[] = [
|
||||
{ label: 'Rörelseresultat', amount: operatingResult },
|
||||
]
|
||||
if (
|
||||
finansiellaPosterSections.length > 0 ||
|
||||
ovrigaFinansiellaPosterSections.length > 0
|
||||
) {
|
||||
summary.push({
|
||||
label: 'Resultat efter finansiella poster',
|
||||
amount: resultatEfterFinansiellaPoster,
|
||||
})
|
||||
}
|
||||
if (bokslutsdispositionerSections.length > 0) {
|
||||
summary.push({ label: 'Bokslutsdispositioner', amount: totalBokslutsdispositioner })
|
||||
}
|
||||
summary.push({ label: 'Skatt på årets resultat', amount: totalSkatt })
|
||||
summary.push({ label: 'Årets resultat', amount: report.net_result, emphasis: true })
|
||||
|
||||
const pdfBuffer = await renderToBuffer(
|
||||
FinancialStatementPDF({
|
||||
title: 'Resultaträkning',
|
||||
groups,
|
||||
summary,
|
||||
period: report.period,
|
||||
company: companyRow as CompanySettings,
|
||||
generatedAt: new Date().toISOString(),
|
||||
})
|
||||
)
|
||||
|
||||
// "-utkast" suffix keeps the draft status visible even after the file
|
||||
// leaves the browser — complements the in-document ÅRL 2:7 disclaimer.
|
||||
const filename = `resultatrakning-${report.period.start}-utkast.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 ? err.message : 'Kunde inte generera resultaträkning' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { requireCompanyId } from '@/lib/company/context'
|
||||
|
||||
/**
|
||||
* POST /api/settings/booking-templates/[id]/touch
|
||||
*
|
||||
* Record that this template was applied by the current company. Upserts the
|
||||
* (template_id, company_id) row in booking_template_usage, refreshing
|
||||
* last_used_at. Used by the template pickers to drive MRU ordering.
|
||||
*
|
||||
* Fire-and-forget from the client — errors are non-fatal.
|
||||
*/
|
||||
export async function POST(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
const { id } = await params
|
||||
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 { error } = await supabase
|
||||
.from('booking_template_usage')
|
||||
.upsert(
|
||||
{
|
||||
template_id: id,
|
||||
company_id: companyId,
|
||||
last_used_at: new Date().toISOString(),
|
||||
},
|
||||
{ onConflict: 'template_id,company_id' },
|
||||
)
|
||||
|
||||
if (error) {
|
||||
return NextResponse.json({ error: error.message }, { status: 500 })
|
||||
}
|
||||
|
||||
return NextResponse.json({ data: { success: true } })
|
||||
}
|
||||
@@ -31,24 +31,67 @@ const CreateBookingTemplateSchema = z.object({
|
||||
* GET /api/settings/booking-templates
|
||||
* Returns all templates visible to the current user:
|
||||
* system + company + team templates.
|
||||
*
|
||||
* Ordering: most recently used (per current company) first, then by category
|
||||
* and name for never-used templates. Usage is tracked in
|
||||
* booking_template_usage via POST /[id]/touch.
|
||||
*/
|
||||
export async function GET() {
|
||||
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)
|
||||
|
||||
// RLS handles scoping (system OR company OR team)
|
||||
const { data, error } = await supabase
|
||||
.from('booking_template_library')
|
||||
.select('*')
|
||||
.eq('is_active', true)
|
||||
.order('is_system', { ascending: false })
|
||||
.order('category')
|
||||
.order('name')
|
||||
const [templatesRes, usageRes] = await Promise.all([
|
||||
supabase
|
||||
.from('booking_template_library')
|
||||
.select('*')
|
||||
.eq('is_active', true)
|
||||
.order('category')
|
||||
.order('name'),
|
||||
supabase
|
||||
.from('booking_template_usage')
|
||||
.select('template_id, last_used_at')
|
||||
.eq('company_id', companyId),
|
||||
])
|
||||
|
||||
if (error) return NextResponse.json({ error: error.message }, { status: 500 })
|
||||
if (templatesRes.error) {
|
||||
return NextResponse.json({ error: templatesRes.error.message }, { status: 500 })
|
||||
}
|
||||
// usage lookup failing is non-fatal — we just fall back to default ordering
|
||||
const usageByTemplate = new Map<string, string>()
|
||||
if (!usageRes.error && usageRes.data) {
|
||||
for (const row of usageRes.data) {
|
||||
usageByTemplate.set(row.template_id, row.last_used_at)
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ data })
|
||||
const templates = templatesRes.data ?? []
|
||||
const decorated = templates.map((t) => ({
|
||||
...t,
|
||||
last_used_at: usageByTemplate.get(t.id) ?? null,
|
||||
}))
|
||||
|
||||
// Stable-sort: templates with last_used_at come first (most-recent first).
|
||||
// Templates without usage keep their category/name order from the query.
|
||||
// ISO 8601 timestamps are fixed-width ASCII — plain relational comparison
|
||||
// is correct and avoids any locale-dependent behaviour from localeCompare.
|
||||
decorated.sort((a, b) => {
|
||||
const aUsed = a.last_used_at
|
||||
const bUsed = b.last_used_at
|
||||
if (aUsed && bUsed) {
|
||||
if (bUsed > aUsed) return -1
|
||||
if (bUsed < aUsed) return 1
|
||||
return 0
|
||||
}
|
||||
if (aUsed) return -1
|
||||
if (bUsed) return 1
|
||||
return 0
|
||||
})
|
||||
|
||||
return NextResponse.json({ data: decorated })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -101,6 +101,8 @@ export default function BookingTemplatePicker({ onApply, entityType }: Props) {
|
||||
return
|
||||
}
|
||||
const lines = applyTemplate(selected.lines, totalAmount)
|
||||
// Fire-and-forget MRU bump so this template surfaces at the top next time.
|
||||
fetch(`/api/settings/booking-templates/${selected.id}/touch`, { method: 'POST' }).catch(() => {})
|
||||
onApply(lines, selected.name)
|
||||
setOpen(false)
|
||||
setSelectedId(null)
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
} from '@/lib/bookkeeping/booking-templates'
|
||||
import { formatAccountWithName } from '@/lib/bookkeeping/client-account-names'
|
||||
import { isCounterpartyTemplateId } from '@/lib/bookkeeping/counterparty-templates'
|
||||
import { convertLibraryToBookingTemplate } from '@/lib/bookkeeping/template-library'
|
||||
import { convertLibraryToBookingTemplate, LIBRARY_TEMPLATE_PREFIX, isLibraryTemplateId } from '@/lib/bookkeeping/template-library'
|
||||
import { getAccountName } from '@/lib/bookkeeping/client-account-names'
|
||||
import type { BookingTemplateLibrary, EntityType } from '@/types'
|
||||
import type { SuggestedTemplate } from '@/lib/transactions/category-suggestions'
|
||||
@@ -237,6 +237,11 @@ export default function TemplatePicker({
|
||||
const advancedGrouped = useMemo(() => groupTemplates(allAdvanced), [allAdvanced])
|
||||
|
||||
const handleSelect = (template: BookingTemplate) => {
|
||||
// For library-backed templates, bump MRU so they surface at the top next time.
|
||||
if (isLibraryTemplateId(template.id)) {
|
||||
const libraryId = template.id.slice(LIBRARY_TEMPLATE_PREFIX.length)
|
||||
fetch(`/api/settings/booking-templates/${libraryId}/touch`, { method: 'POST' }).catch(() => {})
|
||||
}
|
||||
onSelect(template)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { renderToBuffer } from '@react-pdf/renderer'
|
||||
import { FinancialStatementPDF } from '../financial-statement-pdf-template'
|
||||
import type { CompanySettings } from '@/types'
|
||||
|
||||
function fakeCompany(): CompanySettings {
|
||||
return {
|
||||
company_name: 'Gnubok AB',
|
||||
trade_name: 'Gnubok',
|
||||
org_number: '5566778899',
|
||||
vat_number: 'SE556677889901',
|
||||
address_line1: 'Kungsgatan 1',
|
||||
postal_code: '11143',
|
||||
city: 'Stockholm',
|
||||
country: 'SE',
|
||||
entity_type: 'aktiebolag',
|
||||
} as unknown as CompanySettings
|
||||
}
|
||||
|
||||
describe('FinancialStatementPDF', () => {
|
||||
it('renders a balance-sheet-shaped document to a PDF buffer', async () => {
|
||||
const doc = FinancialStatementPDF({
|
||||
title: 'Balansräkning',
|
||||
groups: [
|
||||
{
|
||||
heading: 'Tillgångar',
|
||||
sections: [
|
||||
{
|
||||
title: 'Kassa och bank',
|
||||
rows: [
|
||||
{ account_number: '1930', account_name: 'Företagskonto', amount: 125_432.5 },
|
||||
],
|
||||
subtotal: 125_432.5,
|
||||
},
|
||||
],
|
||||
totalLabel: 'Summa tillgångar',
|
||||
total: 125_432.5,
|
||||
},
|
||||
{
|
||||
heading: 'Eget kapital och skulder',
|
||||
sections: [
|
||||
{
|
||||
title: 'Eget kapital',
|
||||
rows: [
|
||||
{ account_number: '2010', account_name: 'Eget kapital', amount: 100_000 },
|
||||
{ account_number: '2091', account_name: 'Balanserat resultat', amount: 25_432.5 },
|
||||
],
|
||||
subtotal: 125_432.5,
|
||||
},
|
||||
],
|
||||
totalLabel: 'Summa eget kapital och skulder',
|
||||
total: 125_432.5,
|
||||
},
|
||||
],
|
||||
period: { start: '2026-01-01', end: '2026-12-31' },
|
||||
company: fakeCompany(),
|
||||
generatedAt: '2026-04-21T10:00:00Z',
|
||||
})
|
||||
|
||||
const buffer = await renderToBuffer(doc)
|
||||
expect(buffer).toBeInstanceOf(Buffer)
|
||||
expect(buffer.length).toBeGreaterThan(1000)
|
||||
// PDF files always start with "%PDF-"
|
||||
expect(buffer.slice(0, 5).toString()).toBe('%PDF-')
|
||||
})
|
||||
|
||||
it('renders an income-statement-shaped document with a summary block', async () => {
|
||||
const doc = FinancialStatementPDF({
|
||||
title: 'Resultaträkning',
|
||||
groups: [
|
||||
{
|
||||
heading: 'Rörelseintäkter',
|
||||
sections: [
|
||||
{
|
||||
title: 'Huvudintäkter',
|
||||
rows: [
|
||||
{ account_number: '3001', account_name: 'Försäljning 25%', amount: 500_000 },
|
||||
],
|
||||
subtotal: 500_000,
|
||||
},
|
||||
],
|
||||
totalLabel: 'Summa rörelseintäkter',
|
||||
total: 500_000,
|
||||
},
|
||||
{
|
||||
heading: 'Rörelsekostnader',
|
||||
sections: [
|
||||
{
|
||||
title: 'Lokalkostnader',
|
||||
rows: [
|
||||
{ account_number: '5010', account_name: 'Lokalhyra', amount: 120_000 },
|
||||
],
|
||||
subtotal: 120_000,
|
||||
},
|
||||
],
|
||||
totalLabel: 'Summa rörelsekostnader',
|
||||
total: 120_000,
|
||||
negate: true,
|
||||
},
|
||||
],
|
||||
summary: [
|
||||
{ label: 'Rörelseresultat', amount: 380_000 },
|
||||
{ label: 'Årets resultat', amount: 380_000, emphasis: true },
|
||||
],
|
||||
period: { start: '2026-01-01', end: '2026-12-31' },
|
||||
company: fakeCompany(),
|
||||
generatedAt: '2026-04-21T10:00:00Z',
|
||||
})
|
||||
|
||||
const buffer = await renderToBuffer(doc)
|
||||
expect(buffer).toBeInstanceOf(Buffer)
|
||||
expect(buffer.slice(0, 5).toString()).toBe('%PDF-')
|
||||
})
|
||||
|
||||
it('handles empty section groups gracefully', async () => {
|
||||
const doc = FinancialStatementPDF({
|
||||
title: 'Balansräkning',
|
||||
groups: [
|
||||
{
|
||||
heading: 'Tillgångar',
|
||||
sections: [],
|
||||
totalLabel: 'Summa tillgångar',
|
||||
total: 0,
|
||||
},
|
||||
{
|
||||
heading: 'Eget kapital och skulder',
|
||||
sections: [],
|
||||
totalLabel: 'Summa eget kapital och skulder',
|
||||
total: 0,
|
||||
},
|
||||
],
|
||||
period: { start: '', end: '' },
|
||||
company: fakeCompany(),
|
||||
generatedAt: '2026-04-21T10:00:00Z',
|
||||
})
|
||||
|
||||
const buffer = await renderToBuffer(doc)
|
||||
expect(buffer.slice(0, 5).toString()).toBe('%PDF-')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,382 @@
|
||||
import {
|
||||
Document,
|
||||
Page,
|
||||
Text,
|
||||
View,
|
||||
StyleSheet,
|
||||
} from '@react-pdf/renderer'
|
||||
import type { CompanySettings } from '@/types'
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
page: {
|
||||
paddingTop: 40,
|
||||
paddingHorizontal: 40,
|
||||
// Leave room for the fixed disclaimer + footer at the bottom of every page.
|
||||
paddingBottom: 120,
|
||||
fontSize: 10,
|
||||
fontFamily: 'Helvetica',
|
||||
},
|
||||
header: {
|
||||
flexDirection: 'row',
|
||||
justifyContent: 'space-between',
|
||||
alignItems: 'flex-start',
|
||||
marginBottom: 24,
|
||||
paddingBottom: 14,
|
||||
borderBottomWidth: 1,
|
||||
borderBottomColor: '#d4d4d4',
|
||||
},
|
||||
titleBlock: {
|
||||
flex: 1,
|
||||
},
|
||||
title: {
|
||||
fontSize: 20,
|
||||
fontWeight: 'bold',
|
||||
color: '#1a1a1a',
|
||||
marginBottom: 4,
|
||||
},
|
||||
subtitle: {
|
||||
fontSize: 11,
|
||||
color: '#333',
|
||||
marginBottom: 2,
|
||||
},
|
||||
period: {
|
||||
fontSize: 10,
|
||||
color: '#666',
|
||||
},
|
||||
companyInfo: {
|
||||
textAlign: 'right',
|
||||
},
|
||||
companyName: {
|
||||
fontSize: 11,
|
||||
fontWeight: 'bold',
|
||||
marginBottom: 2,
|
||||
},
|
||||
companyMeta: {
|
||||
fontSize: 9,
|
||||
color: '#666',
|
||||
},
|
||||
group: {
|
||||
marginBottom: 18,
|
||||
},
|
||||
groupHeading: {
|
||||
fontSize: 12,
|
||||
fontWeight: 'bold',
|
||||
color: '#1a1a1a',
|
||||
marginBottom: 8,
|
||||
paddingBottom: 4,
|
||||
borderBottomWidth: 1,
|
||||
borderBottomColor: '#1a1a1a',
|
||||
},
|
||||
section: {
|
||||
marginBottom: 8,
|
||||
},
|
||||
sectionTitle: {
|
||||
fontSize: 10,
|
||||
fontWeight: 'bold',
|
||||
color: '#444',
|
||||
marginBottom: 4,
|
||||
marginTop: 6,
|
||||
},
|
||||
row: {
|
||||
flexDirection: 'row',
|
||||
paddingVertical: 2,
|
||||
},
|
||||
colAccount: {
|
||||
width: 48,
|
||||
color: '#666',
|
||||
fontFamily: 'Courier',
|
||||
},
|
||||
colName: {
|
||||
flex: 1,
|
||||
color: '#1a1a1a',
|
||||
paddingRight: 12,
|
||||
},
|
||||
colAmount: {
|
||||
width: 110,
|
||||
textAlign: 'right',
|
||||
fontFamily: 'Courier',
|
||||
color: '#1a1a1a',
|
||||
},
|
||||
sectionSubtotalRow: {
|
||||
flexDirection: 'row',
|
||||
paddingVertical: 3,
|
||||
marginTop: 2,
|
||||
borderTopWidth: 0.5,
|
||||
borderTopColor: '#d4d4d4',
|
||||
},
|
||||
sectionSubtotalLabel: {
|
||||
flex: 1,
|
||||
fontStyle: 'italic',
|
||||
color: '#444',
|
||||
paddingLeft: 48,
|
||||
},
|
||||
sectionSubtotalAmount: {
|
||||
width: 110,
|
||||
textAlign: 'right',
|
||||
fontFamily: 'Courier',
|
||||
fontStyle: 'italic',
|
||||
color: '#444',
|
||||
},
|
||||
groupTotalRow: {
|
||||
flexDirection: 'row',
|
||||
paddingVertical: 6,
|
||||
marginTop: 6,
|
||||
borderTopWidth: 1,
|
||||
borderTopColor: '#1a1a1a',
|
||||
},
|
||||
groupTotalLabel: {
|
||||
flex: 1,
|
||||
fontWeight: 'bold',
|
||||
fontSize: 11,
|
||||
},
|
||||
groupTotalAmount: {
|
||||
width: 110,
|
||||
textAlign: 'right',
|
||||
fontFamily: 'Courier',
|
||||
fontWeight: 'bold',
|
||||
fontSize: 11,
|
||||
},
|
||||
summaryBlock: {
|
||||
marginTop: 20,
|
||||
paddingTop: 10,
|
||||
borderTopWidth: 2,
|
||||
borderTopColor: '#1a1a1a',
|
||||
},
|
||||
summaryRow: {
|
||||
flexDirection: 'row',
|
||||
paddingVertical: 4,
|
||||
},
|
||||
summaryLabel: {
|
||||
flex: 1,
|
||||
color: '#1a1a1a',
|
||||
},
|
||||
summaryAmount: {
|
||||
width: 110,
|
||||
textAlign: 'right',
|
||||
fontFamily: 'Courier',
|
||||
},
|
||||
summaryEmphasisLabel: {
|
||||
flex: 1,
|
||||
fontWeight: 'bold',
|
||||
fontSize: 12,
|
||||
},
|
||||
summaryEmphasisAmount: {
|
||||
width: 110,
|
||||
textAlign: 'right',
|
||||
fontFamily: 'Courier',
|
||||
fontWeight: 'bold',
|
||||
fontSize: 12,
|
||||
},
|
||||
disclaimer: {
|
||||
position: 'absolute',
|
||||
bottom: 52,
|
||||
left: 40,
|
||||
right: 40,
|
||||
paddingTop: 6,
|
||||
paddingBottom: 6,
|
||||
paddingHorizontal: 10,
|
||||
borderWidth: 0.8,
|
||||
borderColor: '#b45309',
|
||||
backgroundColor: '#fef3c7',
|
||||
borderRadius: 3,
|
||||
},
|
||||
disclaimerTitle: {
|
||||
fontSize: 8,
|
||||
fontWeight: 'bold',
|
||||
color: '#78350f',
|
||||
marginBottom: 2,
|
||||
},
|
||||
disclaimerText: {
|
||||
fontSize: 7.5,
|
||||
color: '#78350f',
|
||||
lineHeight: 1.3,
|
||||
},
|
||||
footer: {
|
||||
position: 'absolute',
|
||||
bottom: 24,
|
||||
left: 40,
|
||||
right: 40,
|
||||
borderTopWidth: 0.5,
|
||||
borderTopColor: '#d4d4d4',
|
||||
paddingTop: 6,
|
||||
flexDirection: 'row',
|
||||
justifyContent: 'space-between',
|
||||
},
|
||||
footerText: {
|
||||
fontSize: 8,
|
||||
color: '#888',
|
||||
},
|
||||
})
|
||||
|
||||
function formatAmount(amount: number): string {
|
||||
return new Intl.NumberFormat('sv-SE', {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
}).format(amount)
|
||||
}
|
||||
|
||||
function formatOrgNumber(orgNumber: string): string {
|
||||
const cleaned = orgNumber.replace(/\D/g, '')
|
||||
if (cleaned.length === 10) {
|
||||
return `${cleaned.slice(0, 6)}-${cleaned.slice(6)}`
|
||||
}
|
||||
return orgNumber
|
||||
}
|
||||
|
||||
function formatDateSv(iso: string): string {
|
||||
if (!iso) return ''
|
||||
return new Date(iso).toLocaleDateString('sv-SE')
|
||||
}
|
||||
|
||||
export interface FinancialStatementSection {
|
||||
title: string
|
||||
rows: { account_number: string; account_name: string; amount: number }[]
|
||||
subtotal: number
|
||||
}
|
||||
|
||||
export interface FinancialStatementGroup {
|
||||
heading: string
|
||||
sections: FinancialStatementSection[]
|
||||
totalLabel: string
|
||||
total: number
|
||||
negate?: boolean
|
||||
}
|
||||
|
||||
export interface FinancialStatementSummaryRow {
|
||||
label: string
|
||||
amount: number
|
||||
emphasis?: boolean
|
||||
}
|
||||
|
||||
interface FinancialStatementPDFProps {
|
||||
title: string
|
||||
groups: FinancialStatementGroup[]
|
||||
summary?: FinancialStatementSummaryRow[]
|
||||
period: { start: string; end: string }
|
||||
company: CompanySettings
|
||||
generatedAt: string
|
||||
}
|
||||
|
||||
export function FinancialStatementPDF({
|
||||
title,
|
||||
groups,
|
||||
summary,
|
||||
period,
|
||||
company,
|
||||
generatedAt,
|
||||
}: FinancialStatementPDFProps) {
|
||||
const companyDisplayName = company.trade_name || company.company_name || ''
|
||||
const periodLabel = period.start && period.end
|
||||
? `${formatDateSv(period.start)} – ${formatDateSv(period.end)}`
|
||||
: ''
|
||||
|
||||
return (
|
||||
<Document>
|
||||
<Page size="A4" style={styles.page}>
|
||||
<View style={styles.header} fixed>
|
||||
<View style={styles.titleBlock}>
|
||||
<Text style={styles.title}>{title}</Text>
|
||||
{companyDisplayName && (
|
||||
<Text style={styles.subtitle}>{companyDisplayName}</Text>
|
||||
)}
|
||||
{periodLabel && (
|
||||
<Text style={styles.period}>Period: {periodLabel}</Text>
|
||||
)}
|
||||
</View>
|
||||
<View style={styles.companyInfo}>
|
||||
{company.company_name && (
|
||||
<Text style={styles.companyName}>{company.company_name}</Text>
|
||||
)}
|
||||
{company.org_number && (
|
||||
<Text style={styles.companyMeta}>
|
||||
Org.nr: {formatOrgNumber(company.org_number)}
|
||||
</Text>
|
||||
)}
|
||||
{company.vat_number && (
|
||||
<Text style={styles.companyMeta}>VAT: {company.vat_number}</Text>
|
||||
)}
|
||||
</View>
|
||||
</View>
|
||||
|
||||
{groups.map((group, gi) => (
|
||||
<View key={gi} style={styles.group} wrap>
|
||||
<Text style={styles.groupHeading}>{group.heading}</Text>
|
||||
|
||||
{group.sections.length === 0 ? (
|
||||
<Text style={{ fontSize: 9, color: '#888', fontStyle: 'italic' }}>
|
||||
Inga poster i perioden.
|
||||
</Text>
|
||||
) : (
|
||||
group.sections.map((section, si) => (
|
||||
<View key={si} style={styles.section} wrap={false}>
|
||||
<Text style={styles.sectionTitle}>{section.title}</Text>
|
||||
{section.rows.map((row, ri) => {
|
||||
const displayAmount = group.negate ? -row.amount : row.amount
|
||||
return (
|
||||
<View key={ri} style={styles.row}>
|
||||
<Text style={styles.colAccount}>{row.account_number}</Text>
|
||||
<Text style={styles.colName}>{row.account_name}</Text>
|
||||
<Text style={styles.colAmount}>{formatAmount(displayAmount)}</Text>
|
||||
</View>
|
||||
)
|
||||
})}
|
||||
{section.rows.length > 1 && (
|
||||
<View style={styles.sectionSubtotalRow}>
|
||||
<Text style={styles.sectionSubtotalLabel}>Summa {section.title.toLowerCase()}</Text>
|
||||
<Text style={styles.sectionSubtotalAmount}>
|
||||
{formatAmount(group.negate ? -section.subtotal : section.subtotal)}
|
||||
</Text>
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
))
|
||||
)}
|
||||
|
||||
<View style={styles.groupTotalRow}>
|
||||
<Text style={styles.groupTotalLabel}>{group.totalLabel}</Text>
|
||||
<Text style={styles.groupTotalAmount}>
|
||||
{formatAmount(group.negate ? -group.total : group.total)}
|
||||
</Text>
|
||||
</View>
|
||||
</View>
|
||||
))}
|
||||
|
||||
{summary && summary.length > 0 && (
|
||||
<View style={styles.summaryBlock} wrap={false}>
|
||||
{summary.map((row, i) => (
|
||||
<View key={i} style={styles.summaryRow}>
|
||||
<Text style={row.emphasis ? styles.summaryEmphasisLabel : styles.summaryLabel}>
|
||||
{row.label}
|
||||
</Text>
|
||||
<Text style={row.emphasis ? styles.summaryEmphasisAmount : styles.summaryAmount}>
|
||||
{formatAmount(row.amount)}
|
||||
</Text>
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
)}
|
||||
|
||||
<View style={styles.disclaimer} fixed>
|
||||
<Text style={styles.disclaimerTitle}>Arbetsutkast – ej undertecknat</Text>
|
||||
<Text style={styles.disclaimerText}>
|
||||
Detta dokument är ett internt arbetsutkast och utgör inte en godkänd
|
||||
årsredovisning enligt ÅRL 2 kap 7 §. Den formella årsredovisningen ska
|
||||
undertecknas av samtliga styrelseledamöter och, i förekommande fall, VD
|
||||
innan den lämnas in till Bolagsverket.
|
||||
</Text>
|
||||
</View>
|
||||
|
||||
<View style={styles.footer} fixed>
|
||||
<Text style={styles.footerText}>
|
||||
{companyDisplayName}
|
||||
{company.org_number ? ` · ${formatOrgNumber(company.org_number)}` : ''}
|
||||
</Text>
|
||||
<Text
|
||||
style={styles.footerText}
|
||||
render={({ pageNumber, totalPages }) => `Genererad ${formatDateSv(generatedAt)} · Sida ${pageNumber} av ${totalPages}`}
|
||||
/>
|
||||
</View>
|
||||
</Page>
|
||||
</Document>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
-- =============================================================================
|
||||
-- Booking Template Usage (per-company MRU tracking)
|
||||
-- =============================================================================
|
||||
--
|
||||
-- Tracks when a booking template was last used *within a specific company*.
|
||||
-- Stored separately from booking_template_library because:
|
||||
-- 1. System templates are shared globally (is_system = TRUE, company_id NULL)
|
||||
-- so a per-row last_used_at would be useless — company A using a template
|
||||
-- would surface it for company B too.
|
||||
-- 2. Team templates are shared across a team's companies; each company should
|
||||
-- track its own usage independently.
|
||||
--
|
||||
-- One row per (template_id, company_id). Upsert on use.
|
||||
|
||||
CREATE TABLE public.booking_template_usage (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
template_id UUID NOT NULL REFERENCES public.booking_template_library(id) ON DELETE CASCADE,
|
||||
company_id UUID NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE,
|
||||
last_used_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
UNIQUE (template_id, company_id)
|
||||
);
|
||||
|
||||
-- RLS
|
||||
ALTER TABLE public.booking_template_usage ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
CREATE POLICY "btu_select" ON public.booking_template_usage
|
||||
FOR SELECT USING (
|
||||
company_id IN (SELECT public.user_company_ids())
|
||||
);
|
||||
|
||||
CREATE POLICY "btu_insert" ON public.booking_template_usage
|
||||
FOR INSERT WITH CHECK (
|
||||
company_id IN (SELECT public.user_company_ids())
|
||||
);
|
||||
|
||||
CREATE POLICY "btu_update" ON public.booking_template_usage
|
||||
FOR UPDATE USING (
|
||||
company_id IN (SELECT public.user_company_ids())
|
||||
);
|
||||
|
||||
CREATE POLICY "btu_delete" ON public.booking_template_usage
|
||||
FOR DELETE USING (
|
||||
company_id IN (SELECT public.user_company_ids())
|
||||
);
|
||||
|
||||
-- Index for the sort query: fetch last_used_at for a given company.
|
||||
CREATE INDEX idx_btu_company_last_used
|
||||
ON public.booking_template_usage (company_id, last_used_at DESC);
|
||||
|
||||
-- Schema reload for PostgREST
|
||||
NOTIFY pgrst, 'reload schema';
|
||||
@@ -0,0 +1,24 @@
|
||||
-- =============================================================================
|
||||
-- Booking Template Usage: add updated_at column + trigger
|
||||
-- =============================================================================
|
||||
--
|
||||
-- Follow-up to 20260421160000_booking_template_usage.sql. The project
|
||||
-- migration rules (CLAUDE.md) require every table to carry an updated_at
|
||||
-- column maintained by the shared update_updated_at_column() trigger. The
|
||||
-- initial migration omitted it because the row is touched via upsert
|
||||
-- (which bumps last_used_at) — but the audit convention applies regardless.
|
||||
|
||||
ALTER TABLE public.booking_template_usage
|
||||
ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT now();
|
||||
|
||||
-- Seed updated_at for existing rows to match last_used_at so history is
|
||||
-- coherent from day one.
|
||||
UPDATE public.booking_template_usage
|
||||
SET updated_at = last_used_at
|
||||
WHERE updated_at < last_used_at;
|
||||
|
||||
CREATE TRIGGER btu_updated_at
|
||||
BEFORE UPDATE ON public.booking_template_usage
|
||||
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
|
||||
|
||||
NOTIFY pgrst, 'reload schema';
|
||||
Reference in New Issue
Block a user