db8983ba9e
* feat(arcim-migration): Briox provider with SIE-over-API import - Briox auth via account ID + application token (no app-level credentials); both tokens rotate on refresh and are persisted - New sie-fetcher pulls the general ledger as SIE through the provider API for Fortnox, Briox and Bjorn Lunden - Wizard stops on a failed SIE import and surfaces the real errors instead of proceeding to the misleading migrate-guard message - PROVIDER_SIE_ONLY_FORTNOX renamed to PROVIDER_SIE_NOT_SUPPORTED; new PROVIDER_TOKEN_INVALID for rejected provider credentials Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(bookkeeping): per-line accruals (periodisering) on invoices and supplier invoices Defer revenue/costs per invoice line to 29xx/17xx interim accounts with automatic monthly dissolution (nightly cron + catch-up at registration), schedule cancellation on credit, year-end auto-detect exclusion for already-scheduled invoices, invoice-inbox service-period extraction for prefill, and an MCP tool to list schedules. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(bokslut): iXBRL arsredovisning generation and Bolagsverket digital filing Generate the annual report as iXBRL from a generated taxonomy registry (K2 element lists, taxonomy:generate/check scripts + CI guard), expose it via the fiscal-period API, and add the bolagsverket extension for digital submission to eget utrymme with webhook-driven status tracking (submissions table + pg tests, lifecycle events, year-end wizard UI). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(mcp): raise origin-guard test timeout to 20s The dynamic import pulls in the full server module; the parse alone flirts with the 5s default under full-suite parallel load. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Add new scripts and documentation for K2 AB taxonomy generation and validation - Introduced `generate-taxonomy-registry.ts` to automate the generation of the iXBRL taxonomy concept registry from official element lists and tuple models. - Added `validate-ixbrl.mjs` for validating generated iXBRL reports against the official taxonomy package using Arelle. - Included new documentation files: - `k2-ab-arsredovisning-elementlista-2024-09-12_rev20250312_sv.xlsx` - `tuple-innehallsmodell-arsredovisning-k2-2024-09-12.xlsx` - `taxonomi-paket-2024-09-12_rev20250312.zip` * Add tests for bookkeeping accruals dissolution and supplier invoices - Implement tests for the POST /api/bookkeeping/accruals/[id]/dissolve route, covering success and error scenarios. - Add tests for the DELETE /api/supplier-invoices/[id] route, including authentication checks and validation of invoice deletion conditions. - Introduce tests for the Arcim migration provider client, ensuring token handling and error classification. - Create tests for the Bolagsverket extension, validating submission role enforcement and environment settings. - Add Zod schemas for Bolagsverket response payloads to ensure proper validation. - Implement tests for MCP server's list accrual schedules, confirming registration and scope mapping. - Add consistency tests for IXBRL document generation, ensuring duplicate facts and XML escaping are handled correctly. - Introduce typed domain errors for accrual schedules to improve error handling in the service. - Add tests for resolving consent with Briox token refresh concurrency, ensuring proper token management and error handling. * fix(tests): update payload size guard comments to reflect recent changes in tool descriptions and ceiling adjustments * fix(gitattributes): mark generated JSON files in bokslut taxonomy as linguist-generated * feat(migrations): add backfill for invoices.journal_entry_id and fallback for next_voucher_number user_id * feat(bokslut): enhance compliance and financial processing features with new submission details and security measures --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
408 lines
14 KiB
TypeScript
408 lines
14 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useEffect, useState } from 'react'
|
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Badge } from '@/components/ui/badge'
|
|
import { Skeleton } from '@/components/ui/skeleton'
|
|
import { Checkbox } from '@/components/ui/checkbox'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Label } from '@/components/ui/label'
|
|
import { ArrowRight, AlertTriangle, Loader2 } from 'lucide-react'
|
|
import { formatCurrency } from '@/lib/utils'
|
|
import { useToast } from '@/components/ui/use-toast'
|
|
import { DepreciationPanel } from './DepreciationPanel'
|
|
import { EfDeclarationSection } from './EfDeclarationSection'
|
|
import type {
|
|
DispositionsProposal,
|
|
ProposedDisposition,
|
|
DispositionKind,
|
|
} from '@/lib/bokslut/types'
|
|
|
|
interface DispositionsStepProps {
|
|
periodId: string
|
|
onBack: () => void
|
|
onContinue: () => void
|
|
}
|
|
|
|
interface UiState {
|
|
/** kind → user-controlled selection state */
|
|
selections: Record<string, { accept: boolean; overrideAmount?: number; lockedSkip: boolean }>
|
|
}
|
|
|
|
/**
|
|
* Phase 2 bokslutsdispositioner step. Fetches proposals from the dispositions
|
|
* API, lets the user adjust amounts (or skip) per proposal, then POSTs the
|
|
* accepted ones. Mandatory p-fond reversals (cohort ≥ 6 years old) cannot be
|
|
* skipped — checkbox stays disabled-on.
|
|
*
|
|
* EF companies get an empty `proposals` array from the server, so this step
|
|
* renders a short pass-through note and lets the user continue.
|
|
*/
|
|
export function DispositionsStep({ periodId, onBack, onContinue }: DispositionsStepProps) {
|
|
const { toast } = useToast()
|
|
const [proposal, setProposal] = useState<DispositionsProposal | null>(null)
|
|
const [loading, setLoading] = useState(true)
|
|
const [fetchError, setFetchError] = useState<string | null>(null)
|
|
const [ui, setUi] = useState<UiState>({ selections: {} })
|
|
const [posting, setPosting] = useState(false)
|
|
const [postError, setPostError] = useState<string | null>(null)
|
|
|
|
// ---- Fetch proposals ----
|
|
const loadProposals = useCallback(async () => {
|
|
setLoading(true)
|
|
setFetchError(null)
|
|
try {
|
|
const res = await fetch(
|
|
`/api/bookkeeping/fiscal-periods/${periodId}/bokslutsdispositioner`,
|
|
)
|
|
const body = await res.json()
|
|
if (!res.ok) {
|
|
setFetchError(body?.error?.message ?? 'Kunde inte ladda dispositioner')
|
|
return
|
|
}
|
|
const data = body.data as DispositionsProposal
|
|
setProposal(data)
|
|
const selections: UiState['selections'] = {}
|
|
data.proposals.forEach((p, index) => {
|
|
// Key must match the render loop and buildPostItems, which both pass
|
|
// the array index — omitting it here defaulted every non-ateforing
|
|
// key to ":0", so only the first proposal card ever rendered.
|
|
const key = proposalKey(p, index)
|
|
selections[key] = {
|
|
accept: true,
|
|
overrideAmount: p.amount,
|
|
lockedSkip: Boolean(p.required),
|
|
}
|
|
})
|
|
setUi({ selections })
|
|
} catch {
|
|
setFetchError('Kunde inte ladda dispositioner')
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}, [periodId])
|
|
|
|
useEffect(() => {
|
|
void loadProposals()
|
|
}, [loadProposals])
|
|
|
|
// ---- POST accepted dispositions ----
|
|
const handleCommit = useCallback(async () => {
|
|
if (!proposal) return
|
|
setPosting(true)
|
|
setPostError(null)
|
|
try {
|
|
const items = buildPostItems(proposal, ui)
|
|
if (items.length === 0) {
|
|
// Nothing selected — just move on
|
|
onContinue()
|
|
return
|
|
}
|
|
const res = await fetch(`/api/bookkeeping/fiscal-periods/${periodId}/bokslutsdispositioner`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ items }),
|
|
})
|
|
const body = await res.json()
|
|
if (!res.ok) {
|
|
setPostError(body?.error?.message ?? 'Kunde inte bokföra dispositioner')
|
|
return
|
|
}
|
|
const created = body.data?.created ?? []
|
|
toast({
|
|
title: `${created.length} verifikation${created.length === 1 ? '' : 'er'} bokförd${
|
|
created.length === 1 ? '' : 'a'
|
|
}`,
|
|
description: 'Dispositionerna ligger nu i bokföringen.',
|
|
})
|
|
onContinue()
|
|
} catch (err) {
|
|
setPostError(err instanceof Error ? err.message : 'Okänt fel')
|
|
} finally {
|
|
setPosting(false)
|
|
}
|
|
}, [proposal, ui, periodId, onContinue, toast])
|
|
|
|
// ---- Render branches ----
|
|
if (loading) {
|
|
return (
|
|
<Card>
|
|
<CardContent className="p-6 space-y-3">
|
|
<Skeleton className="h-6 w-1/3" />
|
|
<Skeleton className="h-20 w-full" />
|
|
<Skeleton className="h-20 w-full" />
|
|
</CardContent>
|
|
</Card>
|
|
)
|
|
}
|
|
|
|
if (fetchError) {
|
|
return (
|
|
<Card>
|
|
<CardContent className="p-6">
|
|
<p className="text-destructive">{fetchError}</p>
|
|
</CardContent>
|
|
</Card>
|
|
)
|
|
}
|
|
|
|
if (!proposal) return null
|
|
|
|
// EF: depreciation can apply (skattemässig hanteras separat); replace the
|
|
// AB-only dispositioner with a NE-bilaga declaration section.
|
|
if (proposal.entityType !== 'aktiebolag') {
|
|
const fiscalYear = parseInt(proposal.fiscalPeriod.period_end.slice(0, 4), 10)
|
|
return (
|
|
<div className="space-y-6">
|
|
<DepreciationPanel periodId={periodId} onPosted={() => void loadProposals()} />
|
|
<EfDeclarationSection
|
|
fiscalPeriodId={periodId}
|
|
bookedSurplus={proposal.netResultBefore}
|
|
fiscalYear={fiscalYear}
|
|
/>
|
|
<div className="flex justify-between">
|
|
<Button variant="outline" onClick={onBack}>Tillbaka</Button>
|
|
<Button onClick={onContinue}>
|
|
Fortsätt <ArrowRight className="ml-1 h-4 w-4" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
if (proposal.proposals.length === 0) {
|
|
return (
|
|
<div className="space-y-6">
|
|
<DepreciationPanel periodId={periodId} onPosted={() => void loadProposals()} />
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle className="text-base">Inga dispositioner föreslagna</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="text-sm text-muted-foreground">
|
|
Bolaget har varken pensionskostnader, periodiseringsfonder att hantera, vinst att skatta
|
|
på, eller skattemässiga avskrivningar att boka.
|
|
</CardContent>
|
|
</Card>
|
|
<div className="flex justify-between">
|
|
<Button variant="outline" onClick={onBack}>Tillbaka</Button>
|
|
<Button onClick={onContinue}>
|
|
Fortsätt <ArrowRight className="ml-1 h-4 w-4" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<DepreciationPanel periodId={periodId} onPosted={() => void loadProposals()} />
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle className="text-base">Bokslutsdispositioner</CardTitle>
|
|
<p className="text-sm text-muted-foreground">
|
|
Resultat före dispositioner:{' '}
|
|
<span className="tabular-nums font-medium">
|
|
{formatCurrency(proposal.netResultBefore)}
|
|
</span>
|
|
. Justera beloppen och bocka av de dispositioner du vill boka.
|
|
</p>
|
|
</CardHeader>
|
|
</Card>
|
|
|
|
{proposal.proposals.map((p, i) => {
|
|
const key = proposalKey(p, i)
|
|
const sel = ui.selections[key]
|
|
if (!sel) return null
|
|
return (
|
|
<ProposalCard
|
|
key={key}
|
|
proposal={p}
|
|
accept={sel.accept}
|
|
overrideAmount={sel.overrideAmount}
|
|
lockedSkip={sel.lockedSkip}
|
|
onChange={(next) => {
|
|
setUi((prev) => ({
|
|
selections: { ...prev.selections, [key]: { ...sel, ...next } },
|
|
}))
|
|
}}
|
|
/>
|
|
)
|
|
})}
|
|
|
|
{postError && (
|
|
<Card>
|
|
<CardContent className="p-4 text-sm text-destructive">{postError}</CardContent>
|
|
</Card>
|
|
)}
|
|
|
|
<div className="flex justify-between">
|
|
<Button variant="outline" onClick={onBack} disabled={posting}>
|
|
Tillbaka
|
|
</Button>
|
|
<Button onClick={handleCommit} disabled={posting}>
|
|
{posting ? (
|
|
<>
|
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" /> Bokför…
|
|
</>
|
|
) : (
|
|
<>
|
|
Bokför valda dispositioner <ArrowRight className="ml-1 h-4 w-4" />
|
|
</>
|
|
)}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function ProposalCard({
|
|
proposal,
|
|
accept,
|
|
overrideAmount,
|
|
lockedSkip,
|
|
onChange,
|
|
}: {
|
|
proposal: ProposedDisposition
|
|
accept: boolean
|
|
overrideAmount: number | undefined
|
|
lockedSkip: boolean
|
|
onChange: (next: { accept?: boolean; overrideAmount?: number }) => void
|
|
}) {
|
|
const overridable = isOverridable(proposal.kind)
|
|
const displayedAmount = overridable ? overrideAmount ?? proposal.amount : proposal.amount
|
|
|
|
return (
|
|
<Card>
|
|
<CardHeader>
|
|
<div className="flex items-start justify-between gap-4">
|
|
<div className="flex-1">
|
|
<CardTitle className="text-base">{proposal.label}</CardTitle>
|
|
<p className="text-sm text-muted-foreground mt-1">{proposal.description}</p>
|
|
{proposal.required && (
|
|
<Badge variant="warning" className="mt-2 gap-1">
|
|
<AlertTriangle className="h-3.5 w-3.5" /> Obligatorisk
|
|
</Badge>
|
|
)}
|
|
</div>
|
|
<p className="font-display text-2xl tabular-nums shrink-0">
|
|
{formatCurrency(displayedAmount)}
|
|
</p>
|
|
</div>
|
|
</CardHeader>
|
|
<CardContent className="space-y-3">
|
|
{proposal.warnings.map((w, i) => (
|
|
<p key={i} className="text-sm text-warning-foreground">
|
|
{w}
|
|
</p>
|
|
))}
|
|
{overridable && (
|
|
<div className="flex items-center gap-3">
|
|
<Label htmlFor={`amount-${proposal.kind}`} className="text-sm shrink-0">
|
|
Belopp (kr)
|
|
</Label>
|
|
<Input
|
|
id={`amount-${proposal.kind}`}
|
|
type="number"
|
|
step="1"
|
|
className="max-w-[180px] tabular-nums"
|
|
value={overrideAmount ?? proposal.amount}
|
|
onChange={(e) => {
|
|
const value = parseInt(e.target.value, 10)
|
|
onChange({ overrideAmount: Number.isFinite(value) ? value : 0 })
|
|
}}
|
|
/>
|
|
</div>
|
|
)}
|
|
<div className="flex items-center gap-2 pt-1">
|
|
<Checkbox
|
|
id={`accept-${proposal.kind}`}
|
|
checked={accept}
|
|
disabled={lockedSkip}
|
|
onCheckedChange={(checked) => onChange({ accept: Boolean(checked) })}
|
|
/>
|
|
<Label
|
|
htmlFor={`accept-${proposal.kind}`}
|
|
className="text-sm cursor-pointer select-none"
|
|
>
|
|
{accept ? 'Boka denna disposition' : 'Hoppa över'}
|
|
{lockedSkip && (
|
|
<span className="text-muted-foreground ml-2 text-xs">(kan inte hoppas över)</span>
|
|
)}
|
|
</Label>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
)
|
|
}
|
|
|
|
function isOverridable(kind: DispositionKind): boolean {
|
|
// Bolagsskatt and SLP are derived from posted entries — overriding the amount
|
|
// would silently break the journal posting (the calculator would still
|
|
// recompute server-side). p-fond avsättning and överavskrivningar take a
|
|
// desired amount as input, so editing is meaningful. p-fond återföring is
|
|
// composed of mandatory cohorts and isn't safely editable from a single
|
|
// amount field.
|
|
return kind === 'periodiseringsfond_avsattning' || kind === 'overavskrivningar'
|
|
}
|
|
|
|
function proposalKey(p: ProposedDisposition, index = 0): string {
|
|
// For återföring there can be multiple cards (one per cohort) — disambiguate
|
|
// by including the line account in the key. For other kinds, the kind is unique.
|
|
if (p.kind === 'periodiseringsfond_ateforing') {
|
|
return `${p.kind}:${p.lines[0]?.account_number ?? index}`
|
|
}
|
|
return `${p.kind}:${index}`
|
|
}
|
|
|
|
interface PostItem {
|
|
kind: DispositionKind
|
|
[key: string]: unknown
|
|
}
|
|
|
|
function buildPostItems(proposal: DispositionsProposal, ui: UiState): PostItem[] {
|
|
const items: PostItem[] = []
|
|
// Group återföring entries — server expects a single item with a `returns`
|
|
// map keyed by cohort account.
|
|
const ateforingReturns: Record<string, number> = {}
|
|
for (const p of proposal.proposals) {
|
|
const key = proposalKey(p, proposal.proposals.indexOf(p))
|
|
const sel = ui.selections[key]
|
|
if (!sel || !sel.accept) continue
|
|
|
|
switch (p.kind) {
|
|
case 'bolagsskatt':
|
|
items.push({ kind: 'bolagsskatt', manualAdjustments: {} })
|
|
break
|
|
case 'sarskild_loneskatt':
|
|
items.push({ kind: 'sarskild_loneskatt' })
|
|
break
|
|
case 'periodiseringsfond_avsattning':
|
|
items.push({
|
|
kind: 'periodiseringsfond_avsattning',
|
|
desiredAmount: sel.overrideAmount ?? p.amount,
|
|
})
|
|
break
|
|
case 'periodiseringsfond_ateforing': {
|
|
const account = p.lines[0]?.account_number
|
|
if (account) ateforingReturns[account] = p.amount
|
|
break
|
|
}
|
|
case 'overavskrivningar':
|
|
items.push({
|
|
kind: 'overavskrivningar',
|
|
additionalAmount: sel.overrideAmount ?? p.amount,
|
|
})
|
|
break
|
|
case 'uppskjuten_skatt':
|
|
// K3 only — server recomputes the amount; client just signals intent.
|
|
items.push({ kind: 'uppskjuten_skatt' })
|
|
break
|
|
}
|
|
}
|
|
if (Object.keys(ateforingReturns).length > 0) {
|
|
items.push({ kind: 'periodiseringsfond_ateforing', returns: ateforingReturns })
|
|
}
|
|
return items
|
|
}
|