32d9978f1b
* 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>
308 lines
8.8 KiB
TypeScript
308 lines
8.8 KiB
TypeScript
'use client'
|
|
|
|
import React, { useState, useCallback, useMemo } from 'react'
|
|
import Link from 'next/link'
|
|
import { ChevronDown, ChevronRight, AlertCircle } from 'lucide-react'
|
|
import { Skeleton } from '@/components/ui/skeleton'
|
|
import { formatDate } from '@/lib/utils'
|
|
import { formatVoucher } from '@/lib/bookkeeping/voucher-series-resolver'
|
|
import {
|
|
createSourceLoader,
|
|
type ReportSourceLine,
|
|
type ReportSourceFetcher,
|
|
} from '@/lib/reports/source-lines'
|
|
|
|
interface ReportRowExpansionProps {
|
|
/** Lazy fetcher invoked the first time the row is expanded. */
|
|
fetcher: ReportSourceFetcher
|
|
/** Column span used by the inline expansion `<tr>`. */
|
|
colSpan: number
|
|
/** Stable id used as the toggle's aria-controls reference. */
|
|
rowId: string
|
|
}
|
|
|
|
interface UseSourceLinesResult {
|
|
lines: ReportSourceLine[] | null
|
|
loading: boolean
|
|
error: string | null
|
|
load: () => Promise<void>
|
|
}
|
|
|
|
/**
|
|
* Hook owning the lazy fetch + caching of source lines. Thin wrapper on top
|
|
* of `createSourceLoader` from `lib/reports/source-lines` so the loading
|
|
* semantics can be unit-tested in node without DOM.
|
|
*
|
|
* The cache lives per-instance — reopening the same row never refetches.
|
|
*/
|
|
export function useSourceLines(fetcher: ReportSourceFetcher): UseSourceLinesResult {
|
|
const [lines, setLines] = useState<ReportSourceLine[] | null>(null)
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
// The loader is stable for the lifetime of the fetcher reference; callers
|
|
// are expected to memoise their fetcher with `useMemo` (every callsite in
|
|
// `reports/page.tsx` does).
|
|
const loader = useMemo(
|
|
() =>
|
|
createSourceLoader(fetcher, (s) => {
|
|
setLines(s.lines)
|
|
setLoading(s.loading)
|
|
setError(s.error)
|
|
}),
|
|
[fetcher]
|
|
)
|
|
|
|
const load = useCallback(() => loader.load(), [loader])
|
|
|
|
return { lines, loading, error, load }
|
|
}
|
|
|
|
/**
|
|
* Drilldown affordance for aggregated report rows.
|
|
*
|
|
* Renders two cells (the chevron-toggle is placed in the calling row; the
|
|
* expansion itself is rendered as a sibling `<tr>` only when expanded).
|
|
* The caller is responsible for placing `<ReportRowExpansion.Toggle>` inside
|
|
* the aggregated row and `<ReportRowExpansion.Panel>` immediately below.
|
|
*
|
|
* Usage:
|
|
* const expansion = useReportRowExpansion(fetcher)
|
|
* <tr><td><expansion.Toggle /></td>...</tr>
|
|
* {expansion.expanded && <expansion.Panel colSpan={6} />}
|
|
*
|
|
* Or use the all-in-one component below where the caller owns the
|
|
* surrounding `<tr>` and just plugs the expansion in.
|
|
*/
|
|
export function ReportRowExpansion({
|
|
fetcher,
|
|
colSpan,
|
|
rowId,
|
|
}: ReportRowExpansionProps) {
|
|
const [expanded, setExpanded] = useState(false)
|
|
const { lines, loading, error, load } = useSourceLines(fetcher)
|
|
|
|
const toggle = useCallback(() => {
|
|
const next = !expanded
|
|
setExpanded(next)
|
|
if (next) load()
|
|
}, [expanded, load])
|
|
|
|
return (
|
|
<>
|
|
<td className="py-2 w-8">
|
|
<button
|
|
type="button"
|
|
onClick={toggle}
|
|
aria-expanded={expanded}
|
|
aria-controls={`expansion-${rowId}`}
|
|
aria-label={expanded ? 'Dölj verifikat' : 'Visa verifikat'}
|
|
className="inline-flex h-6 w-6 items-center justify-center rounded text-muted-foreground hover:text-foreground hover:bg-muted/60 transition-colors"
|
|
>
|
|
{expanded ? (
|
|
<ChevronDown className="h-4 w-4" />
|
|
) : (
|
|
<ChevronRight className="h-4 w-4" />
|
|
)}
|
|
</button>
|
|
</td>
|
|
{expanded && (
|
|
<ExpansionPanel
|
|
colSpan={colSpan}
|
|
loading={loading}
|
|
error={error}
|
|
lines={lines}
|
|
rowId={rowId}
|
|
/>
|
|
)}
|
|
</>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Variant where the toggle and the expansion are placed by the caller. The
|
|
* caller renders `<Toggle />` inside the aggregated `<tr>`, then below
|
|
* conditionally renders `<Panel />` as a sibling `<tr>` so its `<td
|
|
* colSpan={n}>` lines up with the rest of the table.
|
|
*/
|
|
export function useReportRowExpansion(fetcher: ReportSourceFetcher, rowId: string) {
|
|
const [expanded, setExpanded] = useState(false)
|
|
const { lines, loading, error, load } = useSourceLines(fetcher)
|
|
|
|
const toggle = useCallback(() => {
|
|
const next = !expanded
|
|
setExpanded(next)
|
|
if (next) load()
|
|
}, [expanded, load])
|
|
|
|
const Toggle = () => (
|
|
<button
|
|
type="button"
|
|
onClick={toggle}
|
|
aria-expanded={expanded}
|
|
aria-controls={`expansion-${rowId}`}
|
|
aria-label={expanded ? 'Dölj verifikat' : 'Visa verifikat'}
|
|
className="inline-flex h-6 w-6 items-center justify-center rounded text-muted-foreground hover:text-foreground hover:bg-muted/60 transition-colors"
|
|
>
|
|
{expanded ? (
|
|
<ChevronDown className="h-4 w-4" />
|
|
) : (
|
|
<ChevronRight className="h-4 w-4" />
|
|
)}
|
|
</button>
|
|
)
|
|
|
|
const Panel = ({ colSpan }: { colSpan: number }) =>
|
|
expanded ? (
|
|
<ExpansionPanelRow
|
|
colSpan={colSpan}
|
|
loading={loading}
|
|
error={error}
|
|
lines={lines}
|
|
rowId={rowId}
|
|
/>
|
|
) : null
|
|
|
|
return { expanded, Toggle, Panel }
|
|
}
|
|
|
|
/**
|
|
* Body of the expansion when the wrapping `<td>` colSpan is provided —
|
|
* renders inline (used by ReportRowExpansion).
|
|
*/
|
|
function ExpansionPanel({
|
|
colSpan,
|
|
loading,
|
|
error,
|
|
lines,
|
|
rowId,
|
|
}: {
|
|
colSpan: number
|
|
loading: boolean
|
|
error: string | null
|
|
lines: ReportSourceLine[] | null
|
|
rowId: string
|
|
}) {
|
|
return (
|
|
<td colSpan={colSpan} id={`expansion-${rowId}`} className="bg-muted/20 p-0">
|
|
<ExpansionContent loading={loading} error={error} lines={lines} />
|
|
</td>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Body of the expansion when used in a sibling `<tr>` (used by
|
|
* `useReportRowExpansion`'s Panel).
|
|
*/
|
|
function ExpansionPanelRow({
|
|
colSpan,
|
|
loading,
|
|
error,
|
|
lines,
|
|
rowId,
|
|
}: {
|
|
colSpan: number
|
|
loading: boolean
|
|
error: string | null
|
|
lines: ReportSourceLine[] | null
|
|
rowId: string
|
|
}) {
|
|
return (
|
|
<tr className="bg-muted/20">
|
|
<td colSpan={colSpan} id={`expansion-${rowId}`} className="p-0">
|
|
<ExpansionContent loading={loading} error={error} lines={lines} />
|
|
</td>
|
|
</tr>
|
|
)
|
|
}
|
|
|
|
function ExpansionContent({
|
|
loading,
|
|
error,
|
|
lines,
|
|
}: {
|
|
loading: boolean
|
|
error: string | null
|
|
lines: ReportSourceLine[] | null
|
|
}) {
|
|
if (loading) {
|
|
return (
|
|
<div className="px-4 py-3 space-y-2">
|
|
<Skeleton className="h-4 w-full" />
|
|
<Skeleton className="h-4 w-5/6" />
|
|
<Skeleton className="h-4 w-2/3" />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
if (error) {
|
|
return (
|
|
<div className="px-4 py-3 flex items-center gap-2 text-sm text-destructive">
|
|
<AlertCircle className="h-4 w-4" />
|
|
{error}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
if (!lines || lines.length === 0) {
|
|
return (
|
|
<div className="px-4 py-3 text-sm text-muted-foreground">
|
|
Inga underliggande verifikat.
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div className="px-4 py-2">
|
|
<table className="w-full text-xs">
|
|
<thead className="[&_th]:font-medium [&_th]:text-[10px] [&_th]:uppercase [&_th]:tracking-wider [&_th]:text-muted-foreground">
|
|
<tr className="text-left">
|
|
<th className="py-1 w-24">Verifikat</th>
|
|
<th className="py-1 w-24">Datum</th>
|
|
<th className="py-1">Beskrivning</th>
|
|
<th className="py-1 w-24 text-right">Debet</th>
|
|
<th className="py-1 w-24 text-right">Kredit</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{lines.map((line) => (
|
|
<tr key={`${line.voucher_series}-${line.voucher_number}-${line.journal_entry_id}`} className="border-t border-border/40">
|
|
<td className="py-1.5">
|
|
{line.journal_entry_id ? (
|
|
<Link
|
|
href={`/bookkeeping/${line.journal_entry_id}`}
|
|
className="font-mono text-foreground hover:underline underline-offset-4"
|
|
>
|
|
{formatVoucher(line)}
|
|
</Link>
|
|
) : (
|
|
<span className="font-mono text-muted-foreground">—</span>
|
|
)}
|
|
</td>
|
|
<td className="py-1.5 tabular-nums text-muted-foreground">
|
|
{line.date ? formatDate(line.date) : ''}
|
|
</td>
|
|
<td className="py-1.5 truncate max-w-md" title={line.description}>
|
|
{line.description}
|
|
</td>
|
|
<td className="py-1.5 text-right tabular-nums">
|
|
{line.debit > 0 ? formatAmount(line.debit) : ''}
|
|
</td>
|
|
<td className="py-1.5 text-right tabular-nums">
|
|
{line.credit > 0 ? formatAmount(line.credit) : ''}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function formatAmount(amount: number): string {
|
|
return amount.toLocaleString('sv-SE', {
|
|
minimumFractionDigits: 2,
|
|
maximumFractionDigits: 2,
|
|
})
|
|
}
|