Files
accounted/lib/bookkeeping/account-classifier-heuristic.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

59 lines
2.2 KiB
TypeScript

/**
* Group-based account classification aligned with BAS 2026. Pure, no data
* import: the server classifier (account-classifier.ts) consults the BAS
* chart first and falls back to this; the client classifier
* (account-classifier-client.ts) uses the lazily loaded chart the same way.
*/
export type AccountType =
| 'asset'
| 'liability'
| 'equity'
| 'revenue'
| 'expense'
| 'untaxed_reserves'
export type NormalBalance = 'debit' | 'credit'
export interface ClassifiedAccount {
account_type: AccountType
normal_balance: NormalBalance
}
/**
* Class-8 groups are subtle: 80/81/82/83/87/88 are intäkter (revenue), 84/89
* are kostnader (expense). The legacy heuristic defaulted everything not in
* 83/84 to expense, which silently misclassified dividends, capital gains and
* bokslutsdispositioner.
*/
export function classifyAccountHeuristic(accountNumber: string): ClassifiedAccount {
const cls = parseInt(accountNumber[0], 10)
const group = parseInt(accountNumber.substring(0, 2), 10)
switch (cls) {
case 1:
return { account_type: 'asset', normal_balance: 'debit' }
case 2:
if (group === 20) return { account_type: 'equity', normal_balance: 'credit' }
if (group === 21) return { account_type: 'untaxed_reserves', normal_balance: 'credit' }
return { account_type: 'liability', normal_balance: 'credit' }
case 3:
return { account_type: 'revenue', normal_balance: 'credit' }
case 4:
case 5:
case 6:
case 7:
return { account_type: 'expense', normal_balance: 'debit' }
case 8:
if (group >= 80 && group <= 83) return { account_type: 'revenue', normal_balance: 'credit' }
if (group === 84) return { account_type: 'expense', normal_balance: 'debit' }
if (group === 85) return { account_type: 'revenue', normal_balance: 'credit' }
if (group === 86) return { account_type: 'expense', normal_balance: 'debit' }
if (group === 87 || group === 88) return { account_type: 'revenue', normal_balance: 'credit' }
if (group === 89) return { account_type: 'expense', normal_balance: 'debit' }
return { account_type: 'expense', normal_balance: 'debit' }
default:
return { account_type: 'expense', normal_balance: 'debit' }
}
}