Files
accounted/lib/salary/agi/xml-generator.ts
T
MattssonandClaude Opus 4.7 064fb7f7a9 Add/white label (#381)
* feat(branding): add BrandingService with default-preserving env layer

Introduce lib/branding/service.ts mirroring lib/email/service.ts. Defaults
match current gnubok values exactly, so production behaviour is unchanged
unless an env var (NEXT_PUBLIC_BRANDING_*, BRANDING_*) or extension override
(via registerBrandingService) is set.

Resolution order: defaults < env vars < extension override.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(branding): route root layout, manifest, and PWA assets through branding service

- app/layout.tsx now reads title, description, themeColor, and apple-touch-icon
  from getBranding() instead of hardcoded values.
- public/manifest.json replaced by dynamic app/manifest.ts so PWA name,
  short_name, description, theme_color, background_color, and icon paths
  are resolved at request time.

The manifest now serves at /manifest.webmanifest (Next.js convention for
the metadata file route). The previous /manifest.json URL is no longer
populated; nothing in core references it after this commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(branding): route email service and templates through branding service

- resend-service.ts: From line uses getBranding().appName instead of
  hardcoded "Gnubok" in both the with-fromName and bare cases.
- invite-templates.ts: subject, HTML header, body, plain text, and the
  team-invite variants all read from branding (sentence case in prose,
  uppercased for the styled <p> header).
- consent-notification-templates.ts: signature fallback (companyName ||
  branding) for both HTML and plain text variants.

Defaults preserve the exact current strings ("Gnubok", "GNUBOK", "gnubok"
in their respective contexts) so no email content changes for production.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(branding): route OAuth consent page through branding service

The MCP OAuth consent page rendered for Claude Desktop / Claude.ai
connector flows now reads the app name from getBranding() for both the
HTML <title> and the body copy. Default still produces "gnubok" in
lowercase prose, matching current behaviour.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(branding): route auth, dashboard, and onboarding text through branding service

Replace user-visible "gnubok" / "Gnubok" references with calls to
getBranding(). Touches:

- Auth pages (login, register, mfa/enroll): logo src/alt, MFA TOTP
  friendlyName.
- Onboarding (companies/new, invite, sandbox, WelcomeOnboarding,
  Step2CompanyDetails, NewUserChecklist, BankIdCompanyPicker,
  ArcimMigrationWorkspace): logo, headings, error/help text.
- Dashboard fallback (companyName="gnubok") and settings (backup copy,
  ApiKeysPanel MCP connector name + login note, CompanyDangerZone,
  retention-notice).
- API routes (support contact subject prefix, enable-banking consent
  email companyName fallback, AI inbox receipt-request appUrl,
  pain001 messageId prefix).
- MCP server "open the gnubok web app" review message.
- Salary/reports filings (AGI Programnamn, KU10 Programnamn,
  payslip footer, full-archive system metadata, SRU #PROGRAM line).

Internal identifiers (cookie names gnubok-company-id /
gnubok-invite-token, API key prefix gnubok_sk_, invite token prefix
gnubok_inv_, MCP tool names, npm package gnubok-mcp, GNUBOK_API_KEY
env name) are deliberately left unchanged — they're stable contracts
that whitelabels must not break.

Defaults match current behaviour exactly.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(branding): support legal page field-level swaps for entity and contact

Privacy and DPA pages now interpolate appName, legalEntity, and
privacyEmail from the branding service instead of hardcoding "Gnubok",
"Arcim", and "privacy@gnubok.se". Page metadata uses generateMetadata()
so titles also reflect the brand.

lib/support.ts now falls back to getBranding().supportEmail when
SUPPORT_RECIPIENT_EMAIL is unset, so a single BRANDING_SUPPORT_EMAIL
env var configures both the support form recipient and the displayed
support address.

Whitelabels with a different legal jurisdiction or entirely different
DPA text should override the page route from an extension. Phase 1
intentionally only supports field-level swaps.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* docs(branding): add WHITELABEL.md and example branding extension

WHITELABEL.md: fork checklist, env var reference, the "do not change"
list (cookies, API key prefixes, invite token prefixes, MCP tool names,
gnubok-mcp npm package, GNUBOK_API_KEY env name), out-of-scope items,
the upstream sync workflow YAML to copy into a fork, conflict avoidance
guidance, and a verification checklist.

extensions/general/_example-branding/: copy-paste starter extension with
index.ts (commented placeholder values for registerBrandingService),
manifest.json, and README.md. Disabled by default (not added to
extensions.config.json); whitelabels cp the folder, edit, and enable.

sectors.test.ts: bumped expected extension count 12 -> 13 to account
for the new starter extension on disk. The generated registry is
unchanged because the example is disabled.

The sync workflow YAML is documented inline in WHITELABEL.md rather
than checked in as a workflow file. It's only meaningful in a fork --
gnubok itself has nothing to sync from.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(branding): address PR review — lazy support email + escape brand in HTML/XML

Three issues from code review:

P1 — lib/support.ts: SUPPORT_RECIPIENT_EMAIL was a module-level const,
evaluated at import time before extensions register branding overrides
via ensureInitialized(). Convert to getSupportRecipientEmail() lazy
accessor; update the only caller in app/api/support/contact/route.ts.
Extension-supplied supportEmail values now route correctly.

P2 — app/api/mcp-oauth/authorize/route.ts: appName was interpolated
into the consent page HTML without escapeHtml(), inconsistent with
the existing escaping of companyName. Wrap appName.toLowerCase() in
escapeHtml() at use sites in <title> and the body paragraph.

P2 — lib/salary/agi/xml-generator.ts and lib/salary/ku/ku10-generator.ts:
appName placed inside <gem:Programnamn> / <Programnamn> XML elements
without escapeXml(), the helper already used for other admin-controlled
fields in the same files. Wrap accordingly to prevent malformed XML if
a brand name contains XML reserved characters.

All admin-controlled inputs only — no user-exploitable path. Defense in
depth, not a known incident.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(branding): security follow-up — lazy metadata, SRU/email header sanitization

Self-audit after the PR review surfaced four more concerns. Fixes them
with the same defense-in-depth posture as the prior review fixes.

1. app/layout.tsx — same eager-evaluation class as P1 support.ts. The
   module-level `const branding = getBranding()` froze branding before
   extensions registered, so extension-based overrides for title,
   description, themeColor, and apple-touch-icon silently never applied.
   - Convert to generateMetadata() / generateViewport() (lazy, run per
     request, see extension-registered overrides).
   - Inline getBranding() inside RootLayout for the apple-touch-icon
     href so it picks up overrides too.
   - Add ensureInitialized() at module level so extensions are loaded
     before the first metadata call. Mirrors the API route pattern.

2. app/manifest.ts — same class. The dynamic manifest function reads
   getBranding() per request, but if the manifest is requested before
   any other module has triggered ensureInitialized(), extensions are
   still unloaded. Add ensureInitialized() at module level.

3. lib/reports/ink2/sru-generator.ts — appName interpolated into the
   SRU `#PROGRAM` directive without sanitization. SRU's reserved char
   is `#` (directive marker) and CRLF injects new directives. Wrap in
   the existing sanitizeString() helper to match the pattern used for
   other admin-controlled fields in this file (#NAMN, #ADRESS, etc.).

4. extensions/general/email/lib/resend-service.ts — appName and the
   user-controlled fromName both flow into the From header. Resend's
   API does its own validation, but defense in depth: strip CRLF and
   angle brackets via a small sanitizeHeaderPart() helper before
   building the header string. fromName was a pre-existing surface;
   appName is new with this whitelabel work.

All four are admin-controlled inputs (env vars or extension code),
not user-exploitable. No known incidents — defense in depth, and
correctness for extension-based whitelabels.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 16:32:26 +02:00

386 lines
18 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { decryptPersonnummer } from '../personnummer'
import { getBranding } from '@/lib/branding/service'
/**
* AGI XML generator — Arbetsgivardeklaration på individnivå.
*
* Produces XML conforming to Skatteverket's schema:
* http://xmls.skatteverket.se/se/skatteverket/da/instans/schema/1.1
*
* The XML can be uploaded on Skatteverket's AGI e-tjänst. For programmatic
* submission use the JSON API flow via the skatteverket extension instead.
*
* Sources verified against Skatteverket's schema + technical description
* (SKV 269, teknisk beskrivning 1.1.16):
* - Root: <Skatteverket omrade="Arbetsgivardeklaration">
* - HU totals: SummaSkatteavdr (497), SummaArbAvgSlf (487), TotalSjuklonekostnad (499)
* - IU identity: BetalningsmottagarId (215), Specifikationsnummer (570)
* - IU amounts: KontantErsattningUlagAG (011), AvdrPrelSkatt (001)
* - Every HU and IU must include AgRegistreradId (201) + RedovisningsPeriod (006)
*
* CRITICAL: FK570 (specifikationsnummer) must stay consistent per employee.
* Corrections are detected by Skatteverket matching the same FK570.
*
* NOT HANDLED HERE (future work):
* - <Franvarouppgift>: separate top-level section for parental leave events
* (FK821 FranvaroDatum, FK823 FranvaroTyp={TILLFALLIG_FORALDRAPENNING|
* FORALDRAPENNING}, etc.). Requires per-event date records, not a simple
* day count. Per-employee sick days are NOT reported via AGI at all —
* they go to Försäkringskassan separately.
*/
const INSTANS_NS = 'http://xmls.skatteverket.se/se/skatteverket/da/instans/schema/1.1'
const KOMPONENT_NS = 'http://xmls.skatteverket.se/se/skatteverket/da/komponent/schema/1.1'
export interface AGIEmployeeData {
personnummer: string // Encrypted — decrypted for XML
specificationNumber: number // FK570 — MUST stay consistent per employee
grossSalary: number // FK011 KontantErsattningUlagAG
taxWithheld: number // FK001 AvdrPrelSkatt
avgifterBasis: number // Retained for backwards compat; equals grossSalary for standard cases. Not emitted separately (FK011 already captures basis).
fSkattPayment?: number // FK131 KontantErsattningEjUlagSA
benefitCar?: number // FK013 SkatteplBilformanUlagAG
benefitFuel?: number // FK018 DrivmVidBilformanUlagAG
benefitHousing?: number // FK043 BostadsformanEjSmahusUlagAG (non-småhus default)
benefitOther?: number // FK012 SkatteplOvrigaFormanerUlagAG
/** @deprecated Meal benefit element name not verified against schema; kept for snapshot compatibility only (not emitted). */
benefitMeals?: number
/** @deprecated Per-employee sick days are not reported via AGI (goes to Försäkringskassan separately). Kept for snapshot compatibility. */
sickDays?: number
/** @deprecated VAB is reported via top-level <Franvarouppgift> as per-event records, not as an IU day count. Kept for snapshot compatibility. */
vabDays?: number
/** @deprecated Parental leave is reported via top-level <Franvarouppgift> as per-event records, not as an IU day count. Kept for snapshot compatibility. */
parentalDays?: number
}
export interface AGICompanyData {
orgNumber: string // 10 digits after stripping dashes
companyName: string
periodYear: number
periodMonth: number
contactName: string
contactPhone: string
contactEmail: string
}
export interface AGITotals {
totalTax: number // FK497 SummaSkatteavdr
totalAvgifterBasis: number // retained for compat (sum of IU underlag)
totalAvgifterAmount: number // FK487 SummaArbAvgSlf (sum of calculated avgifter across categories)
/**
* FK499 TotalSjuklonekostnad — company's total sjuklön cost for the period
* (sum of sjuklön paid days 2–14 across all employees). Required per 2025+ rules.
* Day 1 is karens (unpaid); day 15+ is Försäkringskassan, not employer.
*/
totalSjuklonekostnad?: number
avgifterByCategory: {
standard?: { basis: number; amount: number }
reduced65plus?: { basis: number; amount: number }
youth?: { basis: number; amount: number }
}
}
/**
* Thrown when required AGI data is missing. Caller should surface the message
* to the user so they can fill in the missing field (org number, contact info).
*/
export class AGIIncompleteDataError extends Error {
constructor(message: string, public readonly missingFields: string[]) {
super(message)
this.name = 'AGIIncompleteDataError'
}
}
function assertRequiredCompanyData(company: AGICompanyData): void {
const missing: string[] = []
const orgNumberDigits = (company.orgNumber || '').replace(/\D/g, '')
// Skatteverket's IDENTITET type requires either 10 digits (AB orgnr, we prefix
// with "16") or 12 digits (personnummer for enskild firma). Any other length
// is a data-entry error that we cannot silently fix.
if (orgNumberDigits.length !== 10 && orgNumberDigits.length !== 12) missing.push('organisationsnummer')
if (!company.contactName.trim()) missing.push('kontaktperson (namn)')
if (!company.contactPhone.trim()) missing.push('telefon')
if (!company.contactEmail.trim()) missing.push('e-post')
if (missing.length > 0) {
throw new AGIIncompleteDataError(
`AGI kan inte genereras — följande uppgifter saknas: ${missing.join(', ')}. ` +
'Fyll i dem under Inställningar → Företag och Inställningar → Lön.',
missing
)
}
}
/**
* Skatteverket's IDENTITET pattern (from the AGI XSD). Accepts:
* - 12-digit personnummer YYYYMMDDXXXX (real dates 19xx/20xx, incl. leap days
* and samordningsnummer where day = actual_day + 60)
* - 12-digit AB/organisationsnummer: literal "16" + 10-digit orgnr, where the
* 3rd digit (first of the 10-digit orgnr) is 1-3, 5, 6, 7, 8 or 9 (NOT 4,
* and with specific restrictions) and the 5th is 2-9.
*
* Mirrored here so we can fail fast with a user-friendly message instead of
* emitting XML that Skatteverket's validator will reject cryptically.
*/
const IDENTITET_PATTERN = /^(((19|20)[0-9][0-9])((((01|03|05|07|08|10|12)(6[1-9]|7[0-9]|8[0-9]|9[0-1]))|((04|06|09|11)(6[1-9]|7[0-9]|8[0-9]|90))|((02)(6[1-9]|7[0-9]|8[0-8])))|00[6-9][0-9]|[0-9][0-9]60)|(((19|20)(04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)(0289))|(20000289)))(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])[0-9]|16(1[0-9]|2[0-9]|3[0-9]|5[0-9]|6[0-4]|66|68|7[0-9]|8[0-9]|9[0-9])[2-9]\d{7}|((((19|20)[0-9][0-9])(((01|03|05|07|08|10|12)(0[1-9]|1[0-9]|2[0-9]|3[0-1]))|((04|06|09|11)(0[1-9]|1[0-9]|2[0-9]|30))|((02)(0[1-9]|1[0-9]|2[0-8]))))|(((19|20)(04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)(0229))|(20000229)))(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])[0-9]$/
/**
* Normalize an org number or personnummer to Skatteverket's 12-character
* IDENTITET format, required by the AGI schema for Avsandare/Organisationsnummer,
* AgRegistreradId, and Arendeagare.
*
* - 10-digit orgnr (AB e.g. 5561234567) → prefixed with "16" → 165561234567
* - 12-digit personnummer (EF e.g. 196904206942) → used as-is
*
* Throws AGIIncompleteDataError if the resulting value cannot match the
* IDENTITET pattern — this catches bogus test data (e.g. "420694-2069") before
* the file reaches Skatteverket.
*/
function toIdentitet(raw: string): string {
const digits = raw.replace(/\D/g, '')
let candidate: string
if (digits.length === 12) candidate = digits
else if (digits.length === 10) candidate = `16${digits}`
else {
throw new AGIIncompleteDataError(
`Ogiltigt organisations-/personnummer (${digits.length} siffror). ` +
'Ange ett giltigt svenskt organisationsnummer (10 siffror, t.ex. 556123-4567) ' +
'eller fullständigt personnummer (12 siffror, YYYYMMDD-XXXX) under Inställningar → Företag.',
['organisationsnummer']
)
}
if (!IDENTITET_PATTERN.test(candidate)) {
throw new AGIIncompleteDataError(
`Ogiltigt organisationsnummer "${raw}" — värdet är inte ett svenskt organisationsnummer eller personnummer enligt Skatteverkets format. ` +
'Kontrollera värdet under Inställningar → Företag. För AB ska det vara 10 siffror (t.ex. 556123-4567). ' +
'För enskild firma ska det vara ett fullständigt 12-siffrigt personnummer (YYYYMMDD-XXXX).',
['organisationsnummer']
)
}
return candidate
}
/**
* Generate AGI XML for a period.
*
* Throws AGIIncompleteDataError if required fields (orgNumber, contact info)
* are missing — we never emit partial XML that Skatteverket would reject.
*/
export function generateAGIXml(
company: AGICompanyData,
employees: AGIEmployeeData[],
totals: AGITotals,
_isCorrection: boolean = false
): string {
assertRequiredCompanyData(company)
const orgIdentitet = toIdentitet(company.orgNumber)
const period = `${company.periodYear}${String(company.periodMonth).padStart(2, '0')}`
const createdAt = new Date().toISOString().replace(/\.\d+Z$/, '')
const lines: string[] = []
lines.push('<?xml version="1.0" encoding="UTF-8"?>')
lines.push(
`<Skatteverket omrade="Arbetsgivardeklaration" xmlns="${INSTANS_NS}" xmlns:gem="${KOMPONENT_NS}">`
)
// ── Avsandare (komponent namespace) ──────────────────────────
lines.push(' <gem:Avsandare>')
lines.push(` <gem:Programnamn>${escapeXml(getBranding().appName.toLowerCase())}</gem:Programnamn>`)
lines.push(` <gem:Organisationsnummer>${orgIdentitet}</gem:Organisationsnummer>`)
lines.push(' <gem:TekniskKontaktperson>')
lines.push(` <gem:Namn>${escapeXml(company.contactName)}</gem:Namn>`)
lines.push(` <gem:Telefon>${escapeXml(company.contactPhone)}</gem:Telefon>`)
lines.push(` <gem:Epostadress>${escapeXml(company.contactEmail)}</gem:Epostadress>`)
lines.push(' </gem:TekniskKontaktperson>')
lines.push(` <gem:Skapad>${createdAt}</gem:Skapad>`)
lines.push(' </gem:Avsandare>')
// ── Blankettgemensamt (komponent namespace) ──────────────────
lines.push(' <gem:Blankettgemensamt>')
lines.push(' <gem:Arbetsgivare>')
lines.push(` <gem:AgRegistreradId>${orgIdentitet}</gem:AgRegistreradId>`)
lines.push(' <gem:Kontaktperson>')
lines.push(` <gem:Namn>${escapeXml(company.contactName)}</gem:Namn>`)
lines.push(` <gem:Telefon>${escapeXml(company.contactPhone)}</gem:Telefon>`)
lines.push(` <gem:Epostadress>${escapeXml(company.contactEmail)}</gem:Epostadress>`)
lines.push(' </gem:Kontaktperson>')
lines.push(' </gem:Arbetsgivare>')
lines.push(' </gem:Blankettgemensamt>')
// ── Blankett: Huvuduppgift (komponent namespace) ─────────────
lines.push(' <gem:Blankett>')
lines.push(' <gem:Arendeinformation>')
lines.push(` <gem:Arendeagare>${orgIdentitet}</gem:Arendeagare>`)
lines.push(` <gem:Period>${period}</gem:Period>`)
lines.push(' </gem:Arendeinformation>')
lines.push(' <gem:Blankettinnehall>')
// HU/IU substitute for the abstract gem:Uppgift element in the komponent
// namespace (substitution group head). Use the concrete element directly —
// gem:Uppgift itself is abstract and cannot appear in an instance document.
lines.push(' <gem:HU>')
// AgRegistreradId is wrapped in ArbetsgivareHUGROUP, all payload elements
// live in the komponent namespace (gem: prefix).
lines.push(' <gem:ArbetsgivareHUGROUP>')
lines.push(` <gem:AgRegistreradId faltkod="201">${orgIdentitet}</gem:AgRegistreradId>`)
lines.push(' </gem:ArbetsgivareHUGROUP>')
lines.push(` <gem:RedovisningsPeriod faltkod="006">${period}</gem:RedovisningsPeriod>`)
// FK497 — Summa skatteavdrag (total from all IU)
if (totals.totalTax > 0) {
lines.push(` <gem:SummaSkatteavdr faltkod="497">${formatAmount(totals.totalTax)}</gem:SummaSkatteavdr>`)
}
// FK487 — Summa arbetsgivaravgifter och SLF (calculated total, NOT basis)
if (totals.totalAvgifterAmount > 0) {
lines.push(` <gem:SummaArbAvgSlf faltkod="487">${formatAmount(totals.totalAvgifterAmount)}</gem:SummaArbAvgSlf>`)
}
// FK499 — Total sjuklönekostnad (legal requirement from 2025 when > 0)
if (totals.totalSjuklonekostnad && totals.totalSjuklonekostnad > 0) {
lines.push(` <gem:TotalSjuklonekostnad faltkod="499">${formatAmount(totals.totalSjuklonekostnad)}</gem:TotalSjuklonekostnad>`)
}
lines.push(' </gem:HU>')
lines.push(' </gem:Blankettinnehall>')
lines.push(' </gem:Blankett>')
// ── Blankett: Individuppgift (one per employee) ──────────────
for (const emp of employees) {
let pnr: string
try {
pnr = decryptPersonnummer(emp.personnummer)
} catch {
throw new Error(
`Kunde inte dekryptera personnummer för anställd med FK570=${emp.specificationNumber}. ` +
'AGI kan inte genereras utan giltigt personnummer.'
)
}
lines.push(' <gem:Blankett>')
lines.push(' <gem:Arendeinformation>')
lines.push(` <gem:Arendeagare>${orgIdentitet}</gem:Arendeagare>`)
lines.push(` <gem:Period>${period}</gem:Period>`)
lines.push(' </gem:Arendeinformation>')
lines.push(' <gem:Blankettinnehall>')
lines.push(' <gem:IU>')
// Identity groups wrap AgRegistreradId and BetalningsmottagarId in IU.
lines.push(' <gem:ArbetsgivareIUGROUP>')
lines.push(` <gem:AgRegistreradId faltkod="201">${orgIdentitet}</gem:AgRegistreradId>`)
lines.push(' </gem:ArbetsgivareIUGROUP>')
// BetalningsmottagarId must be inside BetalningsmottagareIDChoice (an
// xs:choice allowing BetalningsmottagarId | Fodelsetid | AnnatId).
lines.push(' <gem:BetalningsmottagareIUGROUP>')
lines.push(' <gem:BetalningsmottagareIDChoice>')
lines.push(` <gem:BetalningsmottagarId faltkod="215">${pnr}</gem:BetalningsmottagarId>`)
lines.push(' </gem:BetalningsmottagareIDChoice>')
lines.push(' </gem:BetalningsmottagareIUGROUP>')
lines.push(` <gem:RedovisningsPeriod faltkod="006">${period}</gem:RedovisningsPeriod>`)
lines.push(` <gem:Specifikationsnummer faltkod="570">${emp.specificationNumber}</gem:Specifikationsnummer>`)
// FK011 — Kontant ersättning, underlag arbetsgivaravgifter (= gross salary)
if (emp.grossSalary > 0) {
lines.push(` <gem:KontantErsattningUlagAG faltkod="011">${formatAmount(emp.grossSalary)}</gem:KontantErsattningUlagAG>`)
}
// FK001 — Avdragen preliminärskatt
if (emp.taxWithheld > 0) {
lines.push(` <gem:AvdrPrelSkatt faltkod="001">${formatAmount(emp.taxWithheld)}</gem:AvdrPrelSkatt>`)
}
// FK013 — Bilförmån (skattepliktig, underlag AG)
if (emp.benefitCar && emp.benefitCar > 0) {
lines.push(` <gem:SkatteplBilformanUlagAG faltkod="013">${formatAmount(emp.benefitCar)}</gem:SkatteplBilformanUlagAG>`)
}
// FK018 — Drivmedel vid bilförmån
if (emp.benefitFuel && emp.benefitFuel > 0) {
lines.push(` <gem:DrivmVidBilformanUlagAG faltkod="018">${formatAmount(emp.benefitFuel)}</gem:DrivmVidBilformanUlagAG>`)
}
// FK043 — Bostadsförmån (ej småhus). TODO: for single-family home use
// BostadsformanSmahusUlagAG (FK041); currently defaults to non-småhus.
if (emp.benefitHousing && emp.benefitHousing > 0) {
lines.push(` <gem:BostadsformanEjSmahusUlagAG faltkod="043">${formatAmount(emp.benefitHousing)}</gem:BostadsformanEjSmahusUlagAG>`)
}
// FK012 — Övriga skattepliktiga förmåner
if (emp.benefitOther && emp.benefitOther > 0) {
lines.push(` <gem:SkatteplOvrigaFormanerUlagAG faltkod="012">${formatAmount(emp.benefitOther)}</gem:SkatteplOvrigaFormanerUlagAG>`)
}
// Meal benefit: element name not verified in the component schema yet.
// Intentionally omitted until we have an authoritative mapping.
void emp.benefitMeals
// FK131 — Ersättning till mottagare med F-skattsedel (ej underlag SA)
if (emp.fSkattPayment && emp.fSkattPayment > 0) {
lines.push(` <gem:KontantErsattningEjUlagSA faltkod="131">${formatAmount(emp.fSkattPayment)}</gem:KontantErsattningEjUlagSA>`)
}
// Sjuk/VAB/föräldra-dagar flows elsewhere:
// - Per-employee sick days are reported to Försäkringskassan, not AGI.
// The company-level total goes in HU as TotalSjuklonekostnad (FK499).
// - VAB and parental leave are reported via the top-level
// <Franvarouppgift> section (FK820-827) as per-event date records,
// not as per-IU day counts. Not implemented in this generator yet.
void emp.sickDays
void emp.vabDays
void emp.parentalDays
lines.push(' </gem:IU>')
lines.push(' </gem:Blankettinnehall>')
lines.push(' </gem:Blankett>')
}
lines.push('</Skatteverket>')
return lines.join('\n')
}
/**
* Build individuppgifter snapshot for storage in agi_declarations table.
* Used for corrections — must reference same FK570.
*/
export function buildIndividuppgifterSnapshot(
employees: AGIEmployeeData[]
): Record<string, unknown>[] {
return employees.map(emp => {
let pnr: string
try {
pnr = decryptPersonnummer(emp.personnummer)
} catch {
pnr = 'DECRYPTION_FAILED'
}
return {
personnummer: pnr,
fk570: emp.specificationNumber,
ruta011: emp.grossSalary,
ruta001: emp.taxWithheld,
ruta020: emp.avgifterBasis,
fk821: emp.sickDays || 0,
fk822: emp.vabDays || 0,
fk823: emp.parentalDays || 0,
}
})
}
// ============================================================
// Helpers
// ============================================================
function escapeXml(str: string): string {
return str
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
}
function formatAmount(amount: number): string {
return Math.round(amount).toString()
}