* chore: repo-wide bloat sweep, remove dead code and fold duplicate helpers Remove 33 dead files, ~270 unreferenced exports/types, 13 dead i18n namespaces and 4 unused dependencies; fold byte-identical helper copies into one canonical home each (lib/utils chunk/sleep/utcDateStamp, lib/dates/iso, lib/invariants/uuid, lib/xml/escape, lib/reports/sru/format, lib/pdf/number-text, lib/browser/panel-request, lib/api/v1/body + v1ValidationError rolled out to ~55 v1 routes, booking-template schemas). No behaviour change: v1 bodies and status codes, MCP tool schemas, DB writes and money math are untouched. Naive ore rounding was deliberately not swapped for roundOre; see DECISIONS.md 2026-09-02 for the full list of things left alone on purpose. tsc, lint, 19588 unit tests and check:guards green; antipattern baseline ratcheted (naive-ore-round 622 -> 620, hand-rolled-invariant 115 -> 113). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * test(transactions): import RawTransaction from @/types after the ingest re-export removal CI's type ratchet (check:types, full tsconfig) caught the one test file that still imported the type through lib/transactions/ingest. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
69 lines
2.7 KiB
TypeScript
69 lines
2.7 KiB
TypeScript
import type { FiscalPeriod } from '@/types'
|
|
import { addDaysIso as addDays } from '@/lib/dates/iso'
|
|
|
|
/** Inclusive ISO date bounds (yyyy-MM-dd) for a period filter. */
|
|
export interface PeriodBounds {
|
|
start: string
|
|
end: string
|
|
}
|
|
|
|
export type Quarter = 1 | 2 | 3 | 4
|
|
|
|
export const QUARTERS: Quarter[] = [1, 2, 3, 4]
|
|
|
|
type PeriodDates = Pick<FiscalPeriod, 'period_start' | 'period_end'>
|
|
|
|
function toIso(year: number, month: number, day: number): string {
|
|
return `${String(year).padStart(4, '0')}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`
|
|
}
|
|
|
|
// Month arithmetic on ISO date strings. Day-of-month overflow clamps to the
|
|
// last day of the target month (2026-01-31 + 1 month = 2026-02-28), matching
|
|
// how fiscal quarters are counted from the period start.
|
|
function addMonths(iso: string, months: number): string {
|
|
const [y, m, d] = iso.split('-').map(Number)
|
|
const total = y * 12 + (m - 1) + months
|
|
const year = Math.floor(total / 12)
|
|
const monthIndex = total - year * 12
|
|
const lastDay = new Date(Date.UTC(year, monthIndex + 1, 0)).getUTCDate()
|
|
return toIso(year, monthIndex + 1, Math.min(d, lastDay))
|
|
}
|
|
|
|
/**
|
|
* Date bounds for one quarter of a fiscal period. Quarters follow the fiscal
|
|
* year, not the calendar: Q1 starts at period_start, so a brutet rakenskapsar
|
|
* (e.g. July to June) gets Q1 = Jul-Sep. Rules for irregular period lengths:
|
|
* - A quarter whose start falls after period_end does not exist (returns
|
|
* null); the caller disables that choice.
|
|
* - Q4 always runs to period_end, so an extended first year (up to 18 months)
|
|
* keeps every transaction reachable through some quarter.
|
|
* - A quarter end never extends past period_end.
|
|
*/
|
|
export function quarterBounds(period: PeriodDates, quarter: Quarter): PeriodBounds | null {
|
|
const start = addMonths(period.period_start, (quarter - 1) * 3)
|
|
if (start > period.period_end) return null
|
|
if (quarter === 4) return { start, end: period.period_end }
|
|
const end = addDays(addMonths(period.period_start, quarter * 3), -1)
|
|
return { start, end: end > period.period_end ? period.period_end : end }
|
|
}
|
|
|
|
/**
|
|
* Bounds for the active period filter: the whole fiscal period, one of its
|
|
* quarters, or null when no filter is applied (or the quarter does not exist
|
|
* within the period).
|
|
*/
|
|
export function resolvePeriodBounds(
|
|
period: PeriodDates | null,
|
|
quarter: Quarter | null,
|
|
): PeriodBounds | null {
|
|
if (!period) return null
|
|
if (quarter === null) return { start: period.period_start, end: period.period_end }
|
|
return quarterBounds(period, quarter)
|
|
}
|
|
|
|
/** True when an ISO date falls inside the bounds. No bounds = everything. */
|
|
export function isWithinBounds(date: string, bounds: PeriodBounds | null): boolean {
|
|
if (!bounds) return true
|
|
return date >= bounds.start && date <= bounds.end
|
|
}
|