Files
accounted/components/settings/ShareCapitalForm.tsx
T
Jakob WennbergandClaude Fable 5 8a7fd567bd fix(settings): validate share-capital pair before saving (#1137) (#1160)
* fix(settings): validate share-capital pair before saving (#1137)

Entering aktiekapital without antal aktier (or vice versa) died on the DB
pair constraint company_settings_share_capital_pair as a raw 500 with the
generic 'Vardet uppfyller inte de tillatna kraven' toast. The pair rule
(ARL 5 kap 14 $: the aktiekapital note needs both values) now surfaces as
a clear 400 in the PUT route, checked against effective body-or-stored
values so partial API updates are covered too. The form additionally marks
each field required when its sibling is filled, so the browser blocks a
one-sided submit before the request is sent.

Fixes #1137

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs: decision-log entry for share-capital pair validation placement

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(settings): correct the share-capital note citation to ÅRL 5 kap 34 §

5 kap 14 § is ställda säkerheter; the antal aktier/kvotvärde note is 5 kap 34 §
(flagged by the Swedish compliance review bot, verified against the
swedish-financial-reporting skill).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(settings): assert the pair message on the one-sided-clear rejection

CodeRabbit review on #1160. Its second suggestion (assert the update
payload on the partial-update test) is skipped: createQueuedMockSupabase
proxies away builder args, so payloads are not recordable, same as every
other test in this suite.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 20:07:24 +02:00

89 lines
3.0 KiB
TypeScript

'use client'
import { useState } from 'react'
import { useTranslations } from 'next-intl'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { roundOre } from '@/lib/money'
import { formatCurrency } from '@/lib/utils'
// Deliberately narrow (data minimisation): the form only ever needs the two
// share-capital fields, not the whole CompanySettings object.
interface ShareCapitalFormProps {
settings: {
aktiekapital?: number | null
antal_aktier?: number | null
}
}
/**
* Registered share capital per Bolagsverket, feeding the statutory
* aktiekapital note in the annual report. Kvotvärde (ABL 1 kap 6 §:
* aktiekapital / antal aktier) is derived, never entered.
*/
export function ShareCapitalForm({ settings }: ShareCapitalFormProps) {
const t = useTranslations('settings_company')
const [aktiekapital, setAktiekapital] = useState(
settings.aktiekapital != null ? String(settings.aktiekapital) : '',
)
const [antalAktier, setAntalAktier] = useState(
settings.antal_aktier != null ? String(settings.antal_aktier) : '',
)
const capital = Number(aktiekapital)
const shares = Number(antalAktier)
// Mirror UpdateSettingsSchema: whole-krona capital > 0, positive integer
// share count. No preview for values the server would reject.
const kvotvarde =
Number.isSafeInteger(capital) && capital > 0 && Number.isSafeInteger(shares) && shares > 0
? roundOre(capital / shares)
: null
return (
<section className="space-y-4">
<h2 className="text-sm font-medium uppercase tracking-wider text-muted-foreground">
{t('share_capital_heading')}
</h2>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="aktiekapital">{t('aktiekapital_label')}</Label>
<Input
id="aktiekapital"
name="aktiekapital"
type="number"
inputMode="numeric"
min="1"
step="1"
value={aktiekapital}
onChange={(e) => setAktiekapital(e.target.value)}
required={antalAktier.trim() !== ''}
/>
<p className="text-xs text-muted-foreground">{t('aktiekapital_help')}</p>
</div>
<div className="space-y-2">
<Label htmlFor="antal_aktier">{t('antal_aktier_label')}</Label>
<Input
id="antal_aktier"
name="antal_aktier"
type="number"
inputMode="numeric"
min="1"
step="1"
value={antalAktier}
onChange={(e) => setAntalAktier(e.target.value)}
required={aktiekapital.trim() !== ''}
/>
<p className="text-xs text-muted-foreground">{t('antal_aktier_help')}</p>
</div>
</div>
{kvotvarde !== null && (
<p className="text-xs text-muted-foreground tabular-nums">
{t('kvotvarde_display', { value: formatCurrency(kvotvarde) })}
</p>
)}
</section>
)
}