Files
accounted/lib/vat/period-selection.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

52 lines
2.0 KiB
TypeScript

/**
* Initial period selection for the VAT declaration view.
*
* The view seeds its cadence (month/quarter/räkenskapsår) from the company's
* configured redovisningsperiod (company_settings.moms_period) and its
* concrete period from the most recently ended one (period-defaults.ts), the
* only one that can actually be filed.
*
* The cadence is deliberately NOT persisted across visits: a company's
* redovisningsperiod is fixed by its Skatteverket registration (SFL 26 kap),
* so the setting has exactly one lawful value and the mount-time re-seed is
* the control that self-heals a temporary in-session detour (e.g. a
* helårsmoms user peeking at quarterly figures). Restoring such a detour
* would keep the filing pipeline open on the wrong period type. The yearly
* cadence's räkenskapsår pick is persisted by FyPicker, which is fine: every
* fiscal year is a legitimate target.
*/
import { mostRecentEndedVatPeriod } from './period-defaults'
import type { MomsPeriod, VatPeriodType } from '@/types'
export interface VatPeriodSelection {
periodType: VatPeriodType
year: number
/** 1-12 for monthly, 1-4 for quarterly, always 1 for yearly. */
period: number
}
/**
* Decide the view's initial period from the company's moms_period setting:
* the setting's cadence, seeded to the most recently ended period in it.
*/
export function resolveInitialVatPeriodSelection(opts: {
momsPeriod: MomsPeriod | null
over40m: boolean
today?: Date
}): VatPeriodSelection {
const { momsPeriod, over40m } = opts
const today = opts.today ?? new Date()
// The 'quarterly' fallback only shapes state that is never shown: the view
// gates on a missing moms_period (and on a missing settings row) before
// rendering a declaration.
const cadence = momsPeriod ?? 'quarterly'
if (cadence === 'yearly') {
return { periodType: 'yearly', year: today.getFullYear(), period: 1 }
}
const ended = mostRecentEndedVatPeriod(cadence, today, { over40m })
return { periodType: cadence, year: ended.year, period: ended.period }
}