* feat(arsredovisning): manual override for medelantal anstallda (Not 2) Why the problem occurred: the ÅRL 5:20 § note was derived only from the employees table, and most aktiebolag that book salary never create a Löner employee record (hand-booked salary, SIE import, migrated history). In prod 148 of 195 aktiebolag with posted 70xx-73xx lines have no employees rows, so their note reads "inga anställda". The reporting company had one row created the same day with a start date halfway through a July-June year: 181/365 = 0.5 FTE rounds to 0. What was removed or simplified: nothing removed. One resolver (resolveMedelantalAnstallda: override, else FTE average) now feeds the K2 note, the K3 note and the iXBRL fact, so no reader can pick a different number. The override sits on arsredovisning_narratives next to the other ÅRL 5 kap. disclosures and rides the existing narrative GET/POST route, service and page save. Why this and not the proposed one: the request asked support to "enable override of Not 2" as free text. A whole number keeps the statutory sentence intact and the iXBRL MedelantaletAnstallda fact taggable; free text would allow a non-compliant note. Rounding 0.5 up globally was rejected (changes every company's note silently, does nothing for companies with no employees rows), as was backdating the hire date (fixes one company, misstates the fact). The iXBRL input also reads the previous period's override so the jämförelseår column shows what last year's document showed. Migration 20260908130127 is additive (nullable INTEGER with a CHECK) and is applied and tracked on staging. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GxiKQke7sY6mrAK9KX4hbD * fix(arsredovisning): size-threshold metrics use the medelantal override too Skeptic refutation on 442fa1bc5: reportMetrics in model.ts still read the FTE average from the employees table, so the ÅRL 1:3 § större- företag test and the K2 relief thresholds could disagree with the figure Not 2 and the iXBRL fact disclose. A SIE-migrated aktiebolag with no employees rows and an override of 60 both years, balansomslutning over 40 MSEK, would have validated as K2-eligible while its own document said 60 employees. Fix: the metrics resolve the employee figure the same way the note does (current period override from report.disclosures, previous period via getMedelantalOverride on that period's narrative row). previous_period on ArsredovisningData now carries the period id so the lookup needs no extra fiscal_periods read. The iXBRL employees-error fallback keeps the previous period's override instead of blanking the jämförelseår. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GxiKQke7sY6mrAK9KX4hbD --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
87 lines
3.4 KiB
TypeScript
87 lines
3.4 KiB
TypeScript
/**
|
||
* Medelantal anställda: time-weighted FTE average across a fiscal period.
|
||
*
|
||
* Per ÅRL 5:20 § the medelantal disclosure is the average number of
|
||
* full-time-equivalent employees over the räkenskapsår, not a snapshot
|
||
* headcount. An employee hired on July 1 of a calendar-year FY counts as
|
||
* 0.5; an employee on 50 % degree employed all year counts as 0.5; an
|
||
* employee hired Mar 1 at 80 % and terminated Aug 31 counts as
|
||
* (184 days / 365 days) × 0.80 ≈ 0.40.
|
||
*
|
||
* Inputs come from public.employees:
|
||
* - employment_start: required (DATE NOT NULL)
|
||
* - employment_end: optional (DATE), when null, employee is still active
|
||
* on the period end date
|
||
* - employment_degree: 0 < degree <= 100, default 100
|
||
*
|
||
* The result is rounded to the nearest whole employee per Swedish ÅR
|
||
* disclosure convention (Skatteverket / FAR practice; ÅRL doesn't specify
|
||
* a precision but whole numbers are universal in K2 ÅR for mindre företag).
|
||
*/
|
||
|
||
export interface EmployeePeriodInput {
|
||
employment_start: string
|
||
employment_end: string | null
|
||
/** 0 < degree <= 100. 100 = full-time. */
|
||
employment_degree: number
|
||
}
|
||
|
||
/** Inclusive day count between two ISO dates (UTC). 2025-01-01..2025-12-31 = 365. */
|
||
function inclusiveDays(startIso: string, endIso: string): number {
|
||
const start = new Date(`${startIso}T00:00:00Z`)
|
||
const end = new Date(`${endIso}T00:00:00Z`)
|
||
if (Number.isNaN(start.getTime()) || Number.isNaN(end.getTime())) return 0
|
||
if (end < start) return 0
|
||
return Math.floor((end.getTime() - start.getTime()) / 86400000) + 1
|
||
}
|
||
|
||
/**
|
||
* Compute the FTE-weighted medelantal anställda for a fiscal period.
|
||
*
|
||
* @param employees rows with employment_start / employment_end / employment_degree
|
||
* @param periodStartIso fiscal period start (inclusive), ISO YYYY-MM-DD
|
||
* @param periodEndIso fiscal period end (inclusive), ISO YYYY-MM-DD
|
||
* @returns rounded whole-number medelantal, or 0 if no qualifying overlap
|
||
*/
|
||
export function computeMedelantalAnstallda(
|
||
employees: EmployeePeriodInput[],
|
||
periodStartIso: string,
|
||
periodEndIso: string,
|
||
): number {
|
||
const periodDays = inclusiveDays(periodStartIso, periodEndIso)
|
||
if (periodDays === 0) return 0
|
||
|
||
let totalFteDays = 0
|
||
for (const e of employees) {
|
||
const overlapStart =
|
||
e.employment_start > periodStartIso ? e.employment_start : periodStartIso
|
||
const employmentEnd = e.employment_end ?? periodEndIso
|
||
const overlapEnd =
|
||
employmentEnd < periodEndIso ? employmentEnd : periodEndIso
|
||
if (overlapStart > overlapEnd) continue
|
||
const overlapDays = inclusiveDays(overlapStart, overlapEnd)
|
||
const degreeFactor = Math.min(100, Math.max(0, e.employment_degree)) / 100
|
||
totalFteDays += overlapDays * degreeFactor
|
||
}
|
||
|
||
return Math.round(totalFteDays / periodDays)
|
||
}
|
||
|
||
/**
|
||
* The figure the årsredovisning discloses: a manual override from
|
||
* arsredovisning_narratives when the user has set one, otherwise the FTE
|
||
* average above. One resolver so the PDF note, the iXBRL fact, and any
|
||
* other reader cannot disagree about which number wins.
|
||
*/
|
||
export function resolveMedelantalAnstallda(
|
||
override: number | null | undefined,
|
||
employees: EmployeePeriodInput[],
|
||
periodStartIso: string,
|
||
periodEndIso: string,
|
||
): number {
|
||
if (typeof override === 'number' && Number.isFinite(override) && override >= 0) {
|
||
return Math.round(override)
|
||
}
|
||
return computeMedelantalAnstallda(employees, periodStartIso, periodEndIso)
|
||
}
|