Files
accounted/lib/salary/ku/ku10-generator.ts
T
f266c386f3 chore: repo-wide bloat sweep, remove dead code and fold duplicate helpers (#2150)
* 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>
2026-09-02 11:51:16 +02:00

167 lines
6.3 KiB
TypeScript

import { escapeXml } from '@/lib/xml/escape'
import { decryptPersonnummer } from '../personnummer'
import { getBranding } from '@/lib/branding/service'
import { stripOrgNumberFormatting } from '@/lib/invariants/org-number'
/**
* KU10 (Kontrolluppgift): Annual employee income statement.
*
* Per Skatteförfarandelagen 15 kap: Every employer must file KU10 for each
* employee by January 31 of the following year. Reports total income, tax
* withheld, and benefit values for the calendar year.
*
* The KU10 is filed electronically via Skatteverket's Filöverföring or API.
* XML format follows Skatteverket Teknisk beskrivning for KU.
*
* Penalties: Late filing = 500 SEK per KU per commenced 5-day period
* (max 5,000 SEK per KU or 500,000 SEK total per filing deadline).
*/
export interface KU10EmployeeData {
personnummer: string // Encrypted, will be decrypted
specificationNumber: number // FK570
totalGross: number // Ruta 011: Total kontant bruttolön for year
totalTax: number // Ruta 001: Total avdragen skatt for year
totalAvgifterBasis: number // Ruta 020: Total avgiftsunderlag
benefitCar?: number // Ruta 012: Total bilförmån
benefitHousing?: number // Ruta 014: Total bostadsförmån
benefitMeals?: number // Ruta 015: Total kostförmån
benefitOther?: number // Ruta 019: Total övrigt
sickDays?: number // Total sjukdagar
employmentStart?: string // YYYY-MM-DD
employmentEnd?: string // YYYY-MM-DD (if terminated during year)
}
export interface KU10CompanyData {
orgNumber: string
companyName: string
year: number
contactName: string
contactPhone: string
contactEmail: string
}
const KU_ORG_NUMBER_PATTERN = /^16\d{2}[2-9]\d{7}$/
/**
* Normalize the employer identity to the 12-digit format required by the KU
* schema. Skatteverket's KU 12.0 XSD defines OrganisationsnummerTYPE with the
* `16` prefix and does not validate the check digit.
*
* Source: https://www.skatteverket.se/foretag/skatterochavdrag/kontrolluppgifter/testtjanstochtekniskbeskrivning.4.233f91f71260075abe8800073614.html
*/
function normalizeKUOrgNumber(raw: string): string {
const cleaned = stripOrgNumberFormatting(raw)
const normalized = /^\d{10}$/.test(cleaned) ? `16${cleaned}` : cleaned
if (!KU_ORG_NUMBER_PATTERN.test(normalized)) {
throw new Error(
'KU10 kan inte genereras: organisationsnumret måste innehålla 10 siffror ' +
'eller 12 siffror med prefixet 16.'
)
}
return normalized
}
/**
* Generate KU10 XML for all employees for a calendar year.
*
* Per BFL 7 kap: The KU10 file is räkenskapsinformation, retained 7 years.
*/
export function generateKU10Xml(
company: KU10CompanyData,
employees: KU10EmployeeData[]
): string {
const lines: string[] = []
const orgNr = normalizeKUOrgNumber(company.orgNumber)
lines.push('<?xml version="1.0" encoding="UTF-8"?>')
lines.push('<Skatteverket xmlns="http://xmls.skatteverket.se/se/skatteverket/ai/instans/infoForBeskworksgivku/1.0"')
lines.push(' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">')
// Avsändare
lines.push(' <Avsandare>')
lines.push(` <Programnamn>${escapeXml(getBranding().appName.toLowerCase())}</Programnamn>`)
lines.push(` <Organisationsnummer>${orgNr}</Organisationsnummer>`)
lines.push(' <TekniskKontaktperson>')
lines.push(` <Namn>${escapeXml(company.contactName)}</Namn>`)
lines.push(` <Telefon>${escapeXml(company.contactPhone)}</Telefon>`)
lines.push(` <Epostadress>${escapeXml(company.contactEmail)}</Epostadress>`)
lines.push(' </TekniskKontaktperson>')
lines.push(' </Avsandare>')
// Blankettgemensamt
lines.push(' <Blankettgemensamt>')
lines.push(` <Uppgiftslamnare>`)
lines.push(` <UppgiftslamnarId>${orgNr}</UppgiftslamnarId>`)
lines.push(` <NamnUppgiftslamnare>${escapeXml(company.companyName)}</NamnUppgiftslamnare>`)
lines.push(` </Uppgiftslamnare>`)
lines.push(' </Blankettgemensamt>')
// Per-employee KU10
for (const emp of employees) {
let pnr: string
try {
pnr = decryptPersonnummer(emp.personnummer)
} catch {
pnr = '000000000000'
}
lines.push(' <Blankett>')
lines.push(' <Arendeinformation>')
lines.push(` <Arendeagare>${orgNr}</Arendeagare>`)
lines.push(` <Period>${company.year}</Period>`)
lines.push(' </Arendeinformation>')
lines.push(' <Blankettinnehall>')
lines.push(' <KU10>')
// Employee identification
lines.push(` <Personnummer faltkod="215">${pnr}</Personnummer>`)
lines.push(` <Specifikationsnummer faltkod="570">${emp.specificationNumber}</Specifikationsnummer>`)
// Income and tax
if (emp.totalGross > 0) {
lines.push(` <KontantBruttoloen faltkod="011">${Math.round(emp.totalGross)}</KontantBruttoloen>`)
}
if (emp.totalTax > 0) {
lines.push(` <AvdragenSkatt faltkod="001">${Math.round(emp.totalTax)}</AvdragenSkatt>`)
}
// Benefits
if (emp.benefitCar && emp.benefitCar > 0) {
lines.push(` <FormanBil faltkod="012">${Math.round(emp.benefitCar)}</FormanBil>`)
}
if (emp.benefitHousing && emp.benefitHousing > 0) {
lines.push(` <FormanBostad faltkod="014">${Math.round(emp.benefitHousing)}</FormanBostad>`)
}
if (emp.benefitMeals && emp.benefitMeals > 0) {
lines.push(` <FormanKost faltkod="015">${Math.round(emp.benefitMeals)}</FormanKost>`)
}
if (emp.benefitOther && emp.benefitOther > 0) {
lines.push(` <FormanOvrigt faltkod="019">${Math.round(emp.benefitOther)}</FormanOvrigt>`)
}
// Avgifter basis
if (emp.totalAvgifterBasis > 0) {
lines.push(` <UnderlagArbAvg faltkod="020">${Math.round(emp.totalAvgifterBasis)}</UnderlagArbAvg>`)
}
// Employment period (if not full year)
if (emp.employmentStart) {
lines.push(` <Anstallningsdatum faltkod="008">${emp.employmentStart.replace(/-/g, '')}</Anstallningsdatum>`)
}
if (emp.employmentEnd) {
lines.push(` <Avgangsdatum faltkod="009">${emp.employmentEnd.replace(/-/g, '')}</Avgangsdatum>`)
}
lines.push(' </KU10>')
lines.push(' </Blankettinnehall>')
lines.push(' </Blankett>')
}
lines.push('</Skatteverket>')
return lines.join('\n')
}