Files
accounted/lib/salary/tax-column.ts
T
1a41119682 perf(bundle): drop the BAS chart and the Node crypto polyfill from the shared client baseline (#1942)
* perf(bundle): drop the BAS chart and the Node crypto polyfill from the shared client baseline

Two chunks rode along in the first-load JS of almost every dashboard route:
the full BAS 2026 chart (315 KB uncompressed, in 81 route manifests) and
the browser polyfill for Node's crypto/vm/Buffer (327 KB, in 26 routes
incl. login and register). Neither was needed on first paint; both got
there through static imports of helpers that happen to live next to code
that needs the data or the builtin.

Node polyfill (4 pure splits, behaviour unchanged, re-exported from the
original modules for server callers):
- lib/auth/bankid-flags.ts: isBankIdEnabled (login, register, security
  settings imported it from bankid.ts, which imports crypto).
- lib/import/bank-file/formats.ts: the format registry + detection (the
  import history imported getFormat from parser.ts, which hashes).
- lib/salary/personnummer-format.ts: parsing/validation/formatting (the
  employee forms reached the encrypting personnummer.ts via tax-column).
- lib/auth/api-key-scopes.ts: scope catalogue, groups, tool map, helpers
  (the API key panel imported STAGING_SCOPES from the key generator).

BAS chart:
- lib/bookkeeping/bas-lazy.ts + use-bas-reference.ts: the chart becomes a
  dynamic import, fetched once per session after first paint; components
  that show BAS names/descriptions call useBasReference() and re-render
  when it lands. Until then (and on the server) only the hardcoded
  account-descriptions answer, so SSR and hydration agree.
- lib/bookkeeping/bas-labels.ts: class/group labels out of bas-reference.ts
  (account-descriptions needed a label and paid for the whole chart).
- lib/bookkeeping/bas-account-numbers.ts (generated, ~11 KB) +
  scripts/generate-bas-account-numbers.ts (--check) + parity test:
  isStandardBASAccountNumber for AddAccountDialog/ChartOfAccountsManager.
- lib/bookkeeping/account-classifier-{heuristic,client}.ts: the BAS-aligned
  heuristic shared by the server classifier and a client variant that uses
  the lazy chart.
- lib/bookkeeping/invoice-accounts.ts: INVOICE_FX_RATE_MISSING,
  InvoiceFxRateMissingError, getRevenueAccount, getOutputVatAccount out of
  invoice-entries.ts, whose engine import pulled account-backfill and the
  chart into SendInvoiceDialog/PaymentBookingDialog.
- CorrectOpeningBalanceDialog re-seeds names when the chart lands;
  OpeningBalanceRowEditor builds its Fuse indexes lazily; the
  ChartOfAccountsManager BAS-katalog tab awaits the chunk.

Tooling:
- scripts/perf/client-import-closure.mjs: static import closure of every
  'use client' module with the shortest chain to a target (file or bare
  specifier); found every path above without a build.
- scripts/checks/client-node-builtin.mjs wired into check:guards: a client
  module reaching a Node builtin is a hard failure (0 today).

Left as is: invoices/[id], its credit page and SendInvoiceDialog still
reach the chart through lib/invoices/issue-credit-note -> invoice-entries
-> engine -> account-backfill; splitting the engine is out of scope here.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(perf): unambiguous import-edge regex in the closure walker (CodeQL js/redos)

One quantifier per span: a greedy [^'"]* up to the specifier quote, which it
cannot cross, so a run of whitespace has a single parse. Same edges as
before (multi-line named imports, re-exports, side-effect imports; type-only
imports still skipped).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 15:07:49 +02:00

58 lines
2.7 KiB
TypeScript

import { extractBirthDate } from './personnummer-format'
/**
* Skattetabell columns (1-6) per Skatteverket. The numbering matches the
* column order in the imported tax-table data (lib/salary/tax-tables-fallback.ts)
* and the project payroll reference (.claude/skills/swedish-payroll/references/tax-tables.md).
*/
export interface TaxColumnOption {
value: number
/** Short label for the select option. */
label: string
/** One-line clarification shown under the select / in the option. */
description: string
}
export const TAX_COLUMN_OPTIONS: TaxColumnOption[] = [
{ value: 1, label: 'Anställd under 66 år', description: 'Standard: det vanligaste valet' },
{ value: 2, label: 'Pensionär 66+ år', description: 'Pension till den som fyllt 66 år vid årets ingång' },
{ value: 3, label: 'Anställd 66+ år', description: 'Lön med förhöjt jobbskatteavdrag' },
{ value: 4, label: 'Sjuk- eller aktivitetsersättning, under 66 år', description: 'Ersättning från Försäkringskassan' },
{ value: 5, label: 'Kolumn 5 (särskilda fall)', description: 'Varierar per år enligt SKVFS' },
{ value: 6, label: 'Pension före 65 år', description: 'Född 1951 eller senare' },
]
export function getTaxColumnOption(value: number): TaxColumnOption | undefined {
return TAX_COLUMN_OPTIONS.find((o) => o.value === value)
}
/**
* Derive the tax column for a salaried EMPLOYEE from their birth year.
*
* Only the unambiguous, dominant case is auto-derived: an employee who has NOT
* turned 66 by the start of the income year → column 1. Skatteverket draws this
* line by birth year ("född 1960 eller senare" = kolumn 1 för inkomståret 2026),
* so we compare birth year, not exact date.
*
* For 66+ the column is genuinely ambiguous from age alone: column 2 (pension)
* vs column 3 (working senior with förhöjt jobbskatteavdrag) depends on the
* income type, which the system can't infer. In that case this returns null and
* the UI asks the user to pick from the named column list.
*
* @param personnummer Full (YYYYMMDDNNNN) or masked (YYYYMMDD-XXXX): only the
* leading 8 birthdate digits are used.
* @param year The income/payment year the column applies to.
* @returns 1 for a confidently-under-66 employee, otherwise null.
*/
export function deriveTaxColumn(personnummer: string, year: number): number | null {
const digits = personnummer.replace(/\D/g, '')
if (digits.length < 8) return null
const { year: birthYear } = extractBirthDate(personnummer)
if (!birthYear || birthYear < 1900 || birthYear > year) return null
// "fyllt 66 år vid årets ingång" → 66+ group. Born in (year - 66) or later
// means they have not turned 66 by Jan 1 of `year` → column 1.
return birthYear >= year - 66 ? 1 : null
}