26e29f47bc
* 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>
240 lines
8.6 KiB
TypeScript
240 lines
8.6 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { CompanySetupSchema, planCompanySetup } from '../onboarding-input'
|
|
|
|
const base = {
|
|
name: 'Testbolaget AB',
|
|
entity_type: 'aktiebolag' as const,
|
|
org_number: '5560000001',
|
|
vat_registered: true,
|
|
moms_period: 'quarterly' as const,
|
|
accounting_method: 'accrual' as const,
|
|
f_skatt: true,
|
|
}
|
|
|
|
describe('CompanySetupSchema', () => {
|
|
it('accepts a complete aktiebolag setup', () => {
|
|
expect(CompanySetupSchema.safeParse(base).success).toBe(true)
|
|
})
|
|
|
|
it('refuses a VAT-registered company without a moms_period (silent zero-deadline trap)', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, moms_period: undefined })
|
|
expect(result.success).toBe(false)
|
|
if (!result.success) {
|
|
expect(result.error.issues.some((i) => i.path.join('.') === 'moms_period')).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('refuses a moms_period on a company that is not VAT-registered', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, vat_registered: false })
|
|
expect(result.success).toBe(false)
|
|
})
|
|
|
|
it('accepts a non-VAT company with no moms_period', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, vat_registered: false, moms_period: undefined })
|
|
expect(result.success).toBe(true)
|
|
})
|
|
|
|
it('defaults an omitted accounting_method by form and flags it in the plan', () => {
|
|
// Minimal-input decision 2026-08-26: orgnr + moms period is the whole ask.
|
|
const ab = CompanySetupSchema.safeParse({ ...base, accounting_method: undefined })
|
|
expect(ab.success).toBe(true)
|
|
if (ab.success) {
|
|
const plan = planCompanySetup(ab.data)
|
|
expect(plan.ok).toBe(true)
|
|
if (plan.ok) {
|
|
expect(plan.resolved).toEqual({ accountingMethod: 'accrual', accountingMethodDefaulted: true })
|
|
expect(plan.input.settings.accounting_method).toBe('accrual')
|
|
}
|
|
}
|
|
|
|
const ef = CompanySetupSchema.safeParse({
|
|
...base,
|
|
entity_type: 'enskild_firma',
|
|
accounting_method: undefined,
|
|
})
|
|
expect(ef.success).toBe(true)
|
|
if (ef.success) {
|
|
const plan = planCompanySetup(ef.data)
|
|
expect(plan.ok && plan.resolved.accountingMethod).toBe('cash')
|
|
}
|
|
})
|
|
|
|
it('an explicit accounting_method always wins over the form default', () => {
|
|
const parsed = CompanySetupSchema.safeParse({ ...base, accounting_method: 'cash' })
|
|
expect(parsed.success).toBe(true)
|
|
if (parsed.success) {
|
|
const plan = planCompanySetup(parsed.data)
|
|
expect(plan.ok && plan.resolved.accountingMethod).toBe('cash')
|
|
expect(plan.ok && plan.resolved.accountingMethodDefaulted).toBe(false)
|
|
}
|
|
})
|
|
|
|
it('refuses a VAT-registered company without an org number (invoice momsregistreringsnummer)', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, org_number: undefined })
|
|
expect(result.success).toBe(false)
|
|
if (!result.success) {
|
|
expect(result.error.issues.some((i) => i.path.join('.') === 'org_number')).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('accepts a non-VAT company without an org number', () => {
|
|
const result = CompanySetupSchema.safeParse({
|
|
...base,
|
|
org_number: undefined,
|
|
vat_registered: false,
|
|
moms_period: undefined,
|
|
})
|
|
expect(result.success).toBe(true)
|
|
})
|
|
|
|
it('refuses an omitted f_skatt: F-skatt approval is never assumed', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, f_skatt: undefined })
|
|
expect(result.success).toBe(false)
|
|
})
|
|
|
|
it('refuses an enskild firma first fiscal year that does not end on 31 December', () => {
|
|
const result = CompanySetupSchema.safeParse({
|
|
...base,
|
|
entity_type: 'enskild_firma',
|
|
first_fiscal_year: { start: '2026-03-15', end: '2027-06-30' },
|
|
})
|
|
expect(result.success).toBe(false)
|
|
if (!result.success) {
|
|
expect(result.error.issues.some((i) => i.path.join('.') === 'first_fiscal_year.end')).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('refuses a malformed organisationsnummer', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, org_number: '1234' })
|
|
expect(result.success).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('planCompanySetup', () => {
|
|
it('produces the wizard-shaped settings and a calendar-year period by default', () => {
|
|
const plan = planCompanySetup(CompanySetupSchema.parse(base))
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
const year = new Date().getFullYear()
|
|
expect(plan.fiscalPeriod.startDate).toBe(`${year}-01-01`)
|
|
expect(plan.fiscalPeriod.endDate).toBe(`${year}-12-31`)
|
|
expect(plan.input.settings).toMatchObject({
|
|
entity_type: 'aktiebolag',
|
|
company_name: 'Testbolaget AB',
|
|
org_number: '5560000001',
|
|
vat_registered: true,
|
|
vat_number: 'SE556000000101',
|
|
moms_period: 'quarterly',
|
|
accounting_method: 'accrual',
|
|
f_skatt: true,
|
|
fiscal_year_start_month: 1,
|
|
is_first_fiscal_year: false,
|
|
})
|
|
})
|
|
|
|
it('forces enskild firma onto the calendar year regardless of the requested start month', () => {
|
|
const plan = planCompanySetup(
|
|
CompanySetupSchema.parse({ ...base, entity_type: 'enskild_firma', fiscal_year_start_month: 7 })
|
|
)
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
expect(plan.input.settings.fiscal_year_start_month).toBe(1)
|
|
expect(plan.fiscalPeriod.startDate.endsWith('-01-01')).toBe(true)
|
|
})
|
|
|
|
it('uses the first fiscal year dates and derives the start month from its end', () => {
|
|
const plan = planCompanySetup(
|
|
CompanySetupSchema.parse({
|
|
...base,
|
|
first_fiscal_year: { start: '2026-03-15', end: '2027-06-30' },
|
|
})
|
|
)
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
expect(plan.fiscalPeriod).toMatchObject({ startDate: '2026-03-15', endDate: '2027-06-30' })
|
|
expect(plan.fiscalPeriod.name).toContain('Första räkenskapsåret')
|
|
expect(plan.input.settings.fiscal_year_start_month).toBe(7)
|
|
expect(plan.input.settings.is_first_fiscal_year).toBe(true)
|
|
})
|
|
|
|
it('keeps an enskild firma on the calendar year through a first fiscal year ending 31 December', () => {
|
|
const plan = planCompanySetup(
|
|
CompanySetupSchema.parse({
|
|
...base,
|
|
entity_type: 'enskild_firma',
|
|
first_fiscal_year: { start: '2026-03-15', end: '2026-12-31' },
|
|
})
|
|
)
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
expect(plan.fiscalPeriod).toMatchObject({ startDate: '2026-03-15', endDate: '2026-12-31' })
|
|
expect(plan.input.settings.fiscal_year_start_month).toBe(1)
|
|
})
|
|
|
|
it('carries f_skatt=false through instead of defaulting to approved', () => {
|
|
const plan = planCompanySetup(CompanySetupSchema.parse({ ...base, f_skatt: false }))
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
expect(plan.input.settings.f_skatt).toBe(false)
|
|
})
|
|
|
|
it('rejects a first fiscal year longer than 18 months', () => {
|
|
const plan = planCompanySetup(
|
|
CompanySetupSchema.parse({
|
|
...base,
|
|
first_fiscal_year: { start: '2026-01-01', end: '2027-12-31' },
|
|
})
|
|
)
|
|
expect(plan.ok).toBe(false)
|
|
})
|
|
|
|
it('leaves vat_number null and moms_period null for a non-VAT company', () => {
|
|
const plan = planCompanySetup(
|
|
CompanySetupSchema.parse({ ...base, vat_registered: false, moms_period: undefined })
|
|
)
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
expect(plan.input.settings.vat_number).toBeNull()
|
|
expect(plan.input.settings.moms_period).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('CompanySetupSchema: ideell_forening', () => {
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs()
|
|
})
|
|
|
|
it('is refused while the creation flag is off', () => {
|
|
vi.stubEnv('NEXT_PUBLIC_IDEELL_FORENING_ENABLED', '')
|
|
const result = CompanySetupSchema.safeParse({
|
|
name: 'SS Testklubb',
|
|
entity_type: 'ideell_forening',
|
|
org_number: '8144009464',
|
|
vat_registered: false,
|
|
f_skatt: false,
|
|
})
|
|
expect(result.success).toBe(false)
|
|
if (!result.success) {
|
|
expect(result.error.issues.map((i) => i.path.join('.'))).toContain('entity_type')
|
|
}
|
|
})
|
|
|
|
it('defaults to accrual and keeps a broken fiscal year once enabled', () => {
|
|
vi.stubEnv('NEXT_PUBLIC_IDEELL_FORENING_ENABLED', 'true')
|
|
const setup = CompanySetupSchema.parse({
|
|
name: 'SS Testklubb',
|
|
entity_type: 'ideell_forening',
|
|
org_number: '8144009464',
|
|
vat_registered: false,
|
|
f_skatt: false,
|
|
fiscal_year_start_month: 7,
|
|
})
|
|
const plan = planCompanySetup(setup)
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
expect(plan.resolved).toEqual({ accountingMethod: 'accrual', accountingMethodDefaulted: true })
|
|
expect(plan.input.settings.fiscal_year_start_month).toBe(7)
|
|
expect(plan.input.entityType).toBe('ideell_forening')
|
|
})
|
|
})
|