* feat(company): ideell förening as a third legal form, behind a flag (#2072 step 1) Why the problem occurred: the legal form was modelled as a binary flag in ~300 files. `EntityType` was a two-member union, but nothing dispatched on it exhaustively: 28 sites defaulted `?? 'enskild_firma'` (invoice, categorize, match, stripe, invoice-inbox) or `?? 'aktiebolag'` (year-end, bokslut, MCP), and every form-dependent choice was an `=== 'aktiebolag' ? A : B` ternary. Widening the union compiled everywhere and changed nothing, so a förening would have booked as an enskild firma in the app and as an aktiebolag in bokslut and MCP, with no error anywhere. The lookup refused föreningar at the door (mapEntityType returned null), which is what the tester hit. What was removed or simplified: the silent defaults. One module, lib/company/entity-type.ts, now holds the list (ENTITY_TYPES), the parser (never defaults), the resolver (settings hint, then companies.entity_type, then throw) and `byEntityType`, whose Record arms make the compiler refuse the next widening until each site has an answer. The form-dependent facts (closing account, owner settlement account, calendar-year lock, default method, K1/K2 label, personnummer vs 16-prefix) live there once instead of in the ternaries. On the SQL side supported_entity_types() replaces four copies of the literal list in the create RPCs. Why this shape and not the proposed one: the tracker asked for the enum widening plus a chart; that alone was the dangerous version (compiles, books wrong). Bundling stiftelse was considered and dropped: identical plumbing but no chart block. Creation sits behind NEXT_PUBLIC_IDEELL_FORENING_ENABLED so the CHECK, RPCs and seed can ship now and the first partner is switched on without a migration; the flag goes when Phase 2 (packs, INK3, årsbokslut, Swish) lands on the tracker. Domain choices (DECISIONS.md 2026-09-08, verify with an accountant before Phase 2): result closes to 2069 with 2068 as prior-year carry; no owner accounts, member settlement on 2890; accrual default; brutet räkenskapsår allowed; K1 label for the 5 000 kr accrual threshold (BFNAR 2010:1); org number gets the 16 prefix. Migration 20260908110835 widens the three CHECK constraints, adds supported_entity_types(), re-creates the three create RPCs with the widened guard and adds the förening block to seed_chart_of_accounts. Applied to staging and covered by ideell-forening-entity-type.pg.test.ts. Part of #2072 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh * fix(company): close the förening paths the skeptic refuted (#2072) Five refutations from the /skeptic pass on 7a05c54d2, each fixed at the shared definition rather than the reported site: 1. Privately paid supplier invoices and the utlägg dialog resolved the owner account in lib/expenses/payer.ts with its own AB/EF ternary, so a förening member's invoice was built on 2893 and then refused by the expense-claim service (which already said 2890), burning an ankomstnummer. The helper now uses ownerSettlementAccount. 2. Booking templates substitute their `_ab` accounts only for an aktiebolag; the `private_expense` template kept its base 2013 for a förening. Template accounts now resolve through templateAccountForForm: EF base, AB override, förening base with owner accounts translated to 2890 (booking-templates.ts and proposal-lines.ts share it). 3. A VAT-registered förening with helårsmoms got no momsdeklaration deadline: the annual VAT rule bailed on anything but AB/EF. A förening is a juridisk person and follows the räkenskapsår schedule (SFL 26 kap 33 §), so the rule now keys on fiscalYearLockedToCalendar instead of the two literals; same in the MCP VAT report. 4. 2069 would have accumulated across years: the year-open omföring was AB-only with 2099/2098 hard-coded. planResultAppropriation now takes the pair from resultClosingAccounts (AB 2099 -> 2098, förening 2069 -> 2068) and skips forms with no carry (EF). 5. With the flag off, a registry lookup that returned "Ideell förening" was prefilled into the onboarding journey, the form picker was skipped and the create step answered "Ogiltig företagsform" with no way back. The journey, the BankID picker, the onboarding page and the MCP lookup now use mapSetupEntityType, which maps only creatable forms, so a flagged-off form falls through to the picker as before. Also: form picker keeps its AB-first order; tests for each fix. Part of #2072 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh * chore(migrations): move ideell förening migration after main's latest version (20260908143051) Two migrations landed on main after the branch forked; a lower version would be skipped by the merge-time apply. Staging history row renamed to match. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh * chore(skills): regenerate accounted-api reference for the widened entity_type enum Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
1157ff1b66
commit
26e29f47bc
@@ -17,6 +17,13 @@ import { TEMPLATE_CATEGORY_LABELS, SCOPE_LABELS, getTemplateScope, applyTemplate
|
||||
import type { BookingTemplateCategory, EntityType } from '@/types'
|
||||
import type { FormLine } from '@/components/bookkeeping/JournalEntryForm'
|
||||
|
||||
// Statutory short forms, kept in Swedish in both locales.
|
||||
const ENTITY_SHORT_LABELS: Record<EntityType, string> = {
|
||||
enskild_firma: 'EF',
|
||||
aktiebolag: 'AB',
|
||||
ideell_forening: 'Förening',
|
||||
}
|
||||
|
||||
interface Props {
|
||||
onApply: (lines: FormLine[], description: string, category?: BookingTemplateCategory) => void
|
||||
entityType?: EntityType
|
||||
@@ -188,8 +195,7 @@ export default function BookingTemplatePicker({ onApply, entityType, defaultAmou
|
||||
<ScopeIcon className="h-3 w-3 shrink-0" />
|
||||
<span>
|
||||
{SCOPE_LABELS[scope]}
|
||||
{t.entity_type !== 'all' &&
|
||||
` · ${t.entity_type === 'enskild_firma' ? 'EF' : 'AB'}`}
|
||||
{t.entity_type !== 'all' && ` · ${ENTITY_SHORT_LABELS[t.entity_type] ?? t.entity_type}`}
|
||||
</span>
|
||||
</div>
|
||||
{t.description && (
|
||||
|
||||
@@ -21,6 +21,9 @@
|
||||
*/
|
||||
|
||||
/** BFNAR 2016:10: the K2 accrual simplification ceiling, in SEK. */
|
||||
import type { EntityType } from '@/types'
|
||||
import { simplifiedYearEndRegelverk } from '@/lib/company/entity-type'
|
||||
|
||||
export const K2_ACCRUAL_THRESHOLD_SEK = 5000
|
||||
|
||||
export interface AccrualAmountInput {
|
||||
@@ -86,7 +89,8 @@ export function shouldShowK2AccrualHint(input: AccrualAmountInput): boolean {
|
||||
* and correct for every aktiebolag.
|
||||
*/
|
||||
export function accrualHintKey(
|
||||
entityType?: 'enskild_firma' | 'aktiebolag' | null,
|
||||
entityType?: EntityType | null,
|
||||
): 'k1_hint' | 'k2_hint' {
|
||||
return entityType === 'enskild_firma' ? 'k1_hint' : 'k2_hint'
|
||||
if (!entityType) return 'k2_hint'
|
||||
return simplifiedYearEndRegelverk(entityType) === 'K1' ? 'k1_hint' : 'k2_hint'
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@ const TEMPLATE_ENTITY_LABELS: Record<string, string> = {
|
||||
all: 'Alla',
|
||||
enskild_firma: 'Enskild firma',
|
||||
aktiebolag: 'Aktiebolag',
|
||||
ideell_forening: 'Ideell förening',
|
||||
}
|
||||
|
||||
interface Props {
|
||||
|
||||
@@ -8,7 +8,8 @@ import { Loader2 } from 'lucide-react'
|
||||
import { AttnLine } from '@/components/ui/attn-line'
|
||||
import { useToast } from '@/components/ui/use-toast'
|
||||
import { switchCompany } from '@/lib/company/actions'
|
||||
import { mapEntityType } from '@/lib/company-lookup/entity-type-map'
|
||||
import { mapEntityType, mapSetupEntityType } from '@/lib/company-lookup/entity-type-map'
|
||||
import { ENTITY_TYPE_LABELS_SV, isEntityType } from '@/lib/company/entity-type'
|
||||
import type { EnrichmentCompanyRole } from '@/lib/company-lookup/types'
|
||||
import { getBranding } from '@/lib/branding/service'
|
||||
import '@/components/onboarding/journey/journey.css'
|
||||
@@ -55,15 +56,13 @@ type SetupState = { kind: 'idle' } | { kind: 'opening'; companyId: string }
|
||||
// terms: kept in Swedish in both locales.
|
||||
function humanEntityType(t: string | null | undefined): string {
|
||||
if (!t) return ''
|
||||
if (t === 'aktiebolag') return 'Aktiebolag'
|
||||
if (t === 'enskild_firma') return 'Enskild firma'
|
||||
if (isEntityType(t)) return ENTITY_TYPE_LABELS_SV[t]
|
||||
return t
|
||||
}
|
||||
|
||||
function humanTicEntityType(t: string): string {
|
||||
const mapped = mapEntityType(t)
|
||||
if (mapped === 'aktiebolag') return 'Aktiebolag'
|
||||
if (mapped === 'enskild_firma') return 'Enskild firma'
|
||||
if (mapped) return ENTITY_TYPE_LABELS_SV[mapped]
|
||||
if (t.toLowerCase().includes('handelsbolag') || t.toLowerCase() === 'hb') return 'Handelsbolag'
|
||||
if (t.toLowerCase().includes('kommanditbolag') || t.toLowerCase() === 'kb') return 'Kommanditbolag'
|
||||
return t
|
||||
@@ -183,7 +182,7 @@ export default function BankIdCompanyPicker({
|
||||
const cleaned = role.companyRegistrationNumber.replace(/[\s-]/g, '')
|
||||
const position = positionLabel(role)
|
||||
const entityLabel = humanTicEntityType(role.legalEntityType)
|
||||
const mappable = mapEntityType(role.legalEntityType) !== null
|
||||
const mappable = mapSetupEntityType(role.legalEntityType) !== null
|
||||
const metaParts = [entityLabel, position].filter(Boolean)
|
||||
if (!mappable) metaParts.push(t('setup_manually'))
|
||||
if (status === 'exists') {
|
||||
|
||||
@@ -34,7 +34,18 @@ import {
|
||||
type FirstYearEndOption,
|
||||
} from '@/lib/onboarding-journey/fiscal-options'
|
||||
import type { EntityType } from '@/types'
|
||||
import { isEntityTypeCreatable, usesPersonnummerAsOrgNumber } from '@/lib/company/entity-type'
|
||||
import JourneyOrb, { type OrbState } from './JourneyOrb'
|
||||
|
||||
/** Display order of the form picker (AB first, as before); flags filter it. */
|
||||
const FORM_PICKER_ORDER: EntityType[] = ['aktiebolag', 'enskild_firma', 'ideell_forening']
|
||||
|
||||
/** i18n key per legal form for the picker chips and the summary card. */
|
||||
const FORM_LABEL_KEY: Record<EntityType, 'journey_form_ab' | 'journey_form_ef' | 'journey_form_forening'> = {
|
||||
aktiebolag: 'journey_form_ab',
|
||||
enskild_firma: 'journey_form_ef',
|
||||
ideell_forening: 'journey_form_forening',
|
||||
}
|
||||
import JourneyTrack from './JourneyTrack'
|
||||
import Question from './Question'
|
||||
import ChipRow from './ChipRow'
|
||||
@@ -379,7 +390,7 @@ export default function OnboardingJourney({
|
||||
const lk = state.ticLookup
|
||||
if (!lk || station > 0) return []
|
||||
const facts: { text: string; warn?: boolean }[] = []
|
||||
if (entity) facts.push({ text: entity === 'aktiebolag' ? t('journey_form_ab') : t('journey_form_ef') })
|
||||
if (entity) facts.push({ text: t(FORM_LABEL_KEY[entity]) })
|
||||
if (lk.address?.city) facts.push({ text: lk.address.city })
|
||||
if (lk.sniCodes[0]?.name) facts.push({ text: lk.sniCodes[0].name })
|
||||
if (lk.registration.fTax) facts.push({ text: 'F-skatt' })
|
||||
@@ -492,10 +503,10 @@ export default function OnboardingJourney({
|
||||
return (
|
||||
<Question title={t('journey_form_title')} info={t('journey_form_info')}>
|
||||
<ChipRow
|
||||
options={[
|
||||
{ key: 'aktiebolag', label: t('journey_form_ab') },
|
||||
{ key: 'enskild_firma', label: t('journey_form_ef') },
|
||||
]}
|
||||
options={FORM_PICKER_ORDER.filter(isEntityTypeCreatable).map((key) => ({
|
||||
key,
|
||||
label: t(FORM_LABEL_KEY[key]),
|
||||
}))}
|
||||
onPick={(k) => dispatch({ type: 'ENTITY_PICKED', entityType: k as EntityType })}
|
||||
{...flyProps}
|
||||
/>
|
||||
@@ -1107,11 +1118,11 @@ function DoneStep({
|
||||
const s = state.settings
|
||||
const shortName = (s.company_name ?? '').split(' ')[0] || ''
|
||||
const rows: [string, string][] = [
|
||||
[t('journey_card_form'), s.entity_type === 'aktiebolag' ? t('journey_form_ab') : t('journey_form_ef')],
|
||||
[t('journey_card_form'), s.entity_type ? t(FORM_LABEL_KEY[s.entity_type]) : ''],
|
||||
]
|
||||
if (s.org_number) {
|
||||
rows.push([
|
||||
s.entity_type === 'enskild_firma' ? t('journey_card_persnr') : t('journey_card_orgnr'),
|
||||
s.entity_type && usesPersonnummerAsOrgNumber(s.entity_type) ? t('journey_card_persnr') : t('journey_card_orgnr'),
|
||||
s.org_number,
|
||||
])
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ export function BookingTemplatesPanel() {
|
||||
all: t('entity_all'),
|
||||
enskild_firma: t('entity_enskild_firma'),
|
||||
aktiebolag: t('entity_aktiebolag'),
|
||||
ideell_forening: t('entity_ideell_forening'),
|
||||
}
|
||||
|
||||
// The panel renders the same session-cached list the pickers use
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useTranslations } from 'next-intl'
|
||||
import { useState } from 'react'
|
||||
import { byEntityType, isEntityType } from '@/lib/company/entity-type'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { HelpPopover } from '@/components/ui/help-popover'
|
||||
import {
|
||||
@@ -83,7 +84,13 @@ export function TaxSettingsForm({
|
||||
{/* Entity type: read-only. Changing it is a support operation. */}
|
||||
<SettingsRow label={t('entity_form_heading')} help={t('entity_form_help')}>
|
||||
<span className="text-sm">
|
||||
{settings.entity_type === 'aktiebolag' ? t('entity_aktiebolag') : t('entity_enskild_firma')}
|
||||
{isEntityType(settings.entity_type)
|
||||
? byEntityType(settings.entity_type, {
|
||||
aktiebolag: t('entity_aktiebolag'),
|
||||
enskild_firma: t('entity_enskild_firma'),
|
||||
ideell_forening: t('entity_ideell_forening'),
|
||||
})
|
||||
: ''}
|
||||
</span>
|
||||
</SettingsRow>
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useTranslations } from 'next-intl'
|
||||
import { useState, useMemo } from 'react'
|
||||
import type { EntityType } from '@/types'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
@@ -51,7 +52,7 @@ export function TemplateForm({
|
||||
)
|
||||
const [description, setDescription] = useState(initialTemplate?.description ?? '')
|
||||
const [category, setCategory] = useState<BookingTemplateCategory>(initialTemplate?.category ?? 'other')
|
||||
const [entityType, setEntityType] = useState<'all' | 'enskild_firma' | 'aktiebolag'>(
|
||||
const [entityType, setEntityType] = useState<'all' | EntityType>(
|
||||
initialTemplate?.entity_type ?? 'all',
|
||||
)
|
||||
const [lines, setLines] = useState<BookingTemplateLibraryLine[]>(() =>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { CompanySettings, EntityType } from '@/types'
|
||||
import { isEntityType } from '@/lib/company/entity-type'
|
||||
|
||||
export interface SupplierInvoiceDefaults {
|
||||
entityType: EntityType
|
||||
@@ -26,8 +27,11 @@ export function deriveSupplierInvoiceDefaults(
|
||||
settings: CompanySettings | null | undefined,
|
||||
fallbackEntityType?: EntityType | null,
|
||||
): SupplierInvoiceDefaults {
|
||||
const entityType =
|
||||
(settings?.entity_type as EntityType | null | undefined) ?? fallbackEntityType ?? 'enskild_firma'
|
||||
// UI prefill only: the server re-resolves the form on submit
|
||||
// (resolveCompanyEntityType), so a not-yet-loaded settings row may fall
|
||||
// back to the company row and, failing that, to the enskild firma defaults.
|
||||
const stored = settings?.entity_type ?? fallbackEntityType
|
||||
const entityType: EntityType = isEntityType(stored) ? stored : 'enskild_firma'
|
||||
return {
|
||||
entityType,
|
||||
accountingMethod: settings?.accounting_method === 'cash' ? 'cash' : 'accrual',
|
||||
|
||||
@@ -330,7 +330,9 @@ export default function QuickReviewDialog({
|
||||
// booked 25% moms against an explicit "Ingen moms" while the preview
|
||||
// showed none. See resolveExplicitVat.
|
||||
const resolvedVat = resolveExplicitVat(vatTreatment, defaultVat)
|
||||
const catDefault = getDefaultAccountForCategory(category)
|
||||
// Only decides whether the account is sent as an override; the server
|
||||
// resolves the form itself, so a missing prop may assume the EF default.
|
||||
const catDefault = getDefaultAccountForCategory(category, entityType ?? 'enskild_firma')
|
||||
const override = accountOverride && accountOverride !== catDefault
|
||||
? accountOverride
|
||||
: undefined
|
||||
|
||||
Reference in New Issue
Block a user