Files
accounted/lib/reports/period-dates.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

81 lines
1.9 KiB
TypeScript

/**
* Period date helpers shared between report generators (momsdeklaration,
* periodisk sammanställning, etc.). Kept tiny on purpose: anything domain-
* specific belongs in the calling module.
*/
import type { MomsPeriod } from '@/types'
export type PeriodType = MomsPeriod
/**
* Calculate inclusive start and end dates for a fiscal-calendar period.
*
* monthly: period 1-12, one calendar month
* quarterly: period 1-4, three calendar months
* yearly: period 1, full calendar year
*/
export function calculatePeriodDates(
periodType: PeriodType,
year: number,
period: number,
): { start: string; end: string } {
let startMonth: number
let endMonth: number
switch (periodType) {
case 'monthly':
startMonth = period
endMonth = period
break
case 'quarterly':
startMonth = (period - 1) * 3 + 1
endMonth = period * 3
break
case 'yearly':
startMonth = 1
endMonth = 12
break
default:
startMonth = 1
endMonth = 12
}
const startDate = new Date(year, startMonth - 1, 1)
const endDate = new Date(year, endMonth, 0)
return {
start: formatDate(startDate),
end: formatDate(endDate),
}
}
export function formatDate(date: Date): string {
const y = date.getFullYear()
const m = String(date.getMonth() + 1).padStart(2, '0')
const d = String(date.getDate()).padStart(2, '0')
return `${y}-${m}-${d}`
}
const SWEDISH_MONTHS = [
'Januari', 'Februari', 'Mars', 'April', 'Maj', 'Juni',
'Juli', 'Augusti', 'September', 'Oktober', 'November', 'December',
] as const
export function formatPeriodLabel(
periodType: PeriodType,
year: number,
period: number,
): string {
switch (periodType) {
case 'monthly':
return `${SWEDISH_MONTHS[period - 1]} ${year}`
case 'quarterly':
return `Kvartal ${period} ${year}`
case 'yearly':
return `Helår ${year}`
default:
return `${year}`
}
}