* feat(import): import skattekontoutdrag files into the skattekonto pipeline Users can now upload the kontohändelse export from Skatteverket's skattekonto e-service (current CSV layout, verified against a real 2026-08 export, plus legacy .skv files) instead of needing the paid API connection. Parsed rows land in skattekonto_transactions as booked file_import rows and inherit the existing 1630 rules engine, bulk booking, match-to-verifikat and both UIs unchanged. - Core parser lib/import/skattekonto-file/ with strict detection (orgnr header + saldo markers, or two distinct SKV vocabulary terms plus row shape), sum-integrity check (opening + rows must equal closing) and a wrong-company guard against company_settings. - computeDedupKey moves to core (lib/skatteverket/skattekonto-dedup); the extension re-imports it. File rows hash-key; content-signature partitioning skips rows already booked (either key form) and promotes matching upcoming rows in place. - syncSkattekonto gains a takeover step: an id-keyed API row adopts a matching hash-keyed imported row in place, so journal links survive connecting the API after a file import. Upcoming rows can no longer clobber a booked row on hash collision. - New skattekonto_file_imports table (company-scoped file-hash dedup) plus source/file_import_id provenance columns on skattekonto_transactions. - /import gains a Skattekontoutdrag wizard (upload/preview/result, deep link ?mode=skattekonto); the bank-file flow detects skattekonto files and redirects instead of importing them as bank rows. - /skattekonto renders imported rows for unconnected companies (attn line + import CTA) instead of discarding them behind the StartCard. - Free for everyone: the local-data booking/match routes were already ungated; only API sync/saldo stay capability-gated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(skattekonto): align the EF F-skatt rule with the 2012 -> 2013 decision 20260810120000 established that 2012 is not standard BAS and moved the booking templates to 2013 (owner taxes in an enskild firma are an eget uttag), but the skattekonto_rules seed still booked EF preliminarskatt against 2012. The file importer makes this rule fire for every EF F-skatt row, so bring it onto 2013 too. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(import): apply review findings on the skattekonto file import - Fix the takeover candidate comparator: the single-argument sort was an inconsistent relation and could adopt a stale upcoming row ahead of the booked file row in a 3+ candidate queue (regression test added), and page the candidate scan with fetchAllRows so a multi-year window is not silently capped at 1000 rows. - Fail parsing when a statement HAS saldo markers but not both readable balances: a file cut off before "Utgående saldo" previously skipped the sum check entirely. sum_valid stays null only for marker-less legacy files. - Count a promotion only when the UPDATE matched a row, so a concurrent sync cannot inflate promoted_count; log a failed finalize of the import record instead of discarding the error. - Migration (unshipped, edited in place): user_id is nullable with ON DELETE SET NULL so import records and their file-hash dedup survive user deletion, and the INSERT policy binds user_id to auth.uid() so a member cannot attribute an import to a colleague. pg tests cover both. - Make the upload drop zone keyboard-reachable (role, tabIndex, Enter/ Space) and give the six count-bearing strings ICU plural forms in both locales. Skipped with reasons on the PR: binding execute rows to file bytes and re-checking orgnr in execute (same client-trust model as the shipped bank-file execute; Zod + RLS scope writes to the caller's own company), a 404 test (the route has no not-found path), event-bus clearing in the route test (the route touches no events), and FK NOT VALID (new column referencing a brand-new empty table). 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>
232 lines
8.3 KiB
TypeScript
232 lines
8.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { detectSkattekontoFile, parseSkattekontoFile } from '../parser'
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
// Structure-exact mirror of a real 2026-08 export from Skatteverket's
|
|
// skattekonto e-service (name, orgnr and amounts sanitized; the real file is
|
|
// never committed). Every cell double-quoted, semicolon-delimited, CRLF,
|
|
// opening/closing saldo as marker rows, space thousands separator, and the
|
|
// invariant opening + sum(rows) = closing holds.
|
|
const MODERN_CSV = [
|
|
'"Testbolaget AB";"556677-8899";""',
|
|
'"";"";""',
|
|
'"";"Ingående saldo 2026-05-03";"-500"',
|
|
'"2026-06-06";"Kostnadsränta";"-10"',
|
|
'"2026-07-04";"Kostnadsränta";"-5"',
|
|
'"2026-07-11";"Inbetalning bokförd 260710";"24 000"',
|
|
'"2026-07-13";"Arbetsgivaravgift juni 2026";"-15 000"',
|
|
'"2026-07-13";"Avdragen skatt juni 2026";"-9 000"',
|
|
'"2026-07-23";"Inbetalning bokförd 260722";"600"',
|
|
'"2026-07-28";"Inbetalning bokförd 260727";"35 000"',
|
|
'"2026-08-01";"Kostnadsränta";"-5"',
|
|
'"2026-08-01";"Intäktsränta";"7"',
|
|
'"";"Utgående saldo 2026-08-01";"35 087"',
|
|
'',
|
|
].join('\r\n')
|
|
|
|
const MODERN_FILENAME = 'Kontoutdrag 556677-8899 2026-05-03--2026-08-01.csv'
|
|
|
|
// Legacy .skv shape: unquoted, headerless, trailing running-saldo column.
|
|
const LEGACY_SKV = [
|
|
'2024-01-12;Debiterad preliminärskatt;-8000;-8000',
|
|
'2024-01-14;Inbetalning bokförd 240113;8000;0',
|
|
'2024-02-12;Debiterad preliminärskatt;-8000;-8000',
|
|
'2024-02-13;Intäktsränta;12;-7988',
|
|
].join('\n')
|
|
|
|
// Bank fixtures (mirrored from the bank parser tests): must never detect.
|
|
const SEB_BANK_CSV = [
|
|
'Bokföringsdag;Valutadag;Verifikationsnummer;Text;Belopp;Saldo',
|
|
'2024-01-15;2024-01-15;12345;SPOTIFY AB;-99,00;12345,67',
|
|
'2024-01-14;2024-01-14;12346;HEMKÖP FRIDHEMSPLAN;-432,50;12444,67',
|
|
].join('\n')
|
|
|
|
const NORDEA_BANK_CSV = [
|
|
'Datum,Transaktion,Kategori,Belopp,Saldo',
|
|
'2024-01-15,SPOTIFY AB,,"-99,00","12 345,67"',
|
|
'2024-01-14,ICA MAXI LINDHAGEN,,"-432,50","12 444,67"',
|
|
].join('\n')
|
|
|
|
// A minimal semicolon bank export whose descriptions mention tax concepts:
|
|
// one vocabulary hit must not be enough to steal the file.
|
|
const GENERIC_BANK_WITH_TAX_PAYMENT = [
|
|
'2024-01-12;Betalning moms Skatteverket;-12000',
|
|
'2024-01-15;SPOTIFY AB;-99',
|
|
'2024-01-20;Kortköp ICA;-432',
|
|
'2024-01-25;Swish Anna;-200',
|
|
].join('\n')
|
|
|
|
describe('detectSkattekontoFile', () => {
|
|
it('detects the modern export by content', () => {
|
|
expect(detectSkattekontoFile(MODERN_CSV, 'nedladdad-fil.csv')).toBe(true)
|
|
})
|
|
|
|
it('detects by Skatteverket export filename', () => {
|
|
expect(detectSkattekontoFile('', MODERN_FILENAME)).toBe(true)
|
|
})
|
|
|
|
it('detects .skv files by extension', () => {
|
|
expect(detectSkattekontoFile(LEGACY_SKV, 'skattekonto.skv')).toBe(true)
|
|
})
|
|
|
|
it('detects legacy content via vocabulary plus row shape', () => {
|
|
expect(detectSkattekontoFile(LEGACY_SKV, 'export.csv')).toBe(true)
|
|
})
|
|
|
|
it('does not detect SEB bank exports', () => {
|
|
expect(detectSkattekontoFile(SEB_BANK_CSV, 'kontoutdrag.csv')).toBe(false)
|
|
})
|
|
|
|
it('does not detect Nordea bank exports', () => {
|
|
expect(detectSkattekontoFile(NORDEA_BANK_CSV, 'export.csv')).toBe(false)
|
|
})
|
|
|
|
it('does not detect a bank file that merely pays Skatteverket', () => {
|
|
expect(detectSkattekontoFile(GENERIC_BANK_WITH_TAX_PAYMENT, 'bank.csv')).toBe(false)
|
|
})
|
|
|
|
it('does not detect empty content', () => {
|
|
expect(detectSkattekontoFile('', 'fil.csv')).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('parseSkattekontoFile: modern export', () => {
|
|
const result = parseSkattekontoFile(MODERN_CSV, MODERN_FILENAME)
|
|
|
|
it('parses all transaction rows', () => {
|
|
expect(result.rows).toHaveLength(9)
|
|
expect(result.stats).toEqual({ total_rows: 9, parsed_rows: 9, skipped_rows: 0 })
|
|
expect(result.variant).toBe('csv')
|
|
})
|
|
|
|
it('extracts company identity from the header row', () => {
|
|
expect(result.company_name).toBe('Testbolaget AB')
|
|
expect(result.org_number).toBe('556677-8899')
|
|
})
|
|
|
|
it('consumes saldo markers as metadata, never as rows', () => {
|
|
expect(result.opening_saldo).toBe(-500)
|
|
expect(result.closing_saldo).toBe(35087)
|
|
expect(result.rows.some((r) => /saldo/i.test(r.transaktionstext))).toBe(false)
|
|
})
|
|
|
|
it('parses space-separated thousands and signs per SKV convention', () => {
|
|
const inbetalning = result.rows.find(
|
|
(r) => r.transaktionstext === 'Inbetalning bokförd 260710',
|
|
)
|
|
expect(inbetalning?.belopp).toBe(24000)
|
|
const agi = result.rows.find(
|
|
(r) => r.transaktionstext === 'Arbetsgivaravgift juni 2026',
|
|
)
|
|
expect(agi?.belopp).toBe(-15000)
|
|
})
|
|
|
|
it('validates the sum invariant', () => {
|
|
expect(result.sum_valid).toBe(true)
|
|
expect(result.issues.filter((i) => i.severity === 'error')).toHaveLength(0)
|
|
})
|
|
|
|
it('extracts the date range', () => {
|
|
expect(result.date_from).toBe('2026-06-06')
|
|
expect(result.date_to).toBe('2026-08-01')
|
|
})
|
|
})
|
|
|
|
describe('parseSkattekontoFile: legacy .skv', () => {
|
|
const result = parseSkattekontoFile(LEGACY_SKV, 'skattekonto.skv')
|
|
|
|
it('parses unquoted rows and ignores the trailing saldo column', () => {
|
|
expect(result.variant).toBe('skv')
|
|
expect(result.rows).toHaveLength(4)
|
|
expect(result.rows[0]).toMatchObject({
|
|
transaktionsdatum: '2024-01-12',
|
|
transaktionstext: 'Debiterad preliminärskatt',
|
|
belopp: -8000,
|
|
})
|
|
})
|
|
|
|
it('has no header identity and no sum check without markers', () => {
|
|
expect(result.org_number).toBeNull()
|
|
expect(result.company_name).toBeNull()
|
|
expect(result.sum_valid).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('parseSkattekontoFile: robustness', () => {
|
|
it('fails a statement whose closing saldo marker is missing', () => {
|
|
const cutOff = MODERN_CSV.replace('"";"Utgående saldo 2026-08-01";"35 087"\r\n', '')
|
|
const result = parseSkattekontoFile(cutOff, MODERN_FILENAME)
|
|
expect(result.sum_valid).toBe(false)
|
|
expect(result.issues.some((i) => i.severity === 'error')).toBe(true)
|
|
})
|
|
|
|
it('fails a statement whose saldo amount is unreadable', () => {
|
|
const garbled = MODERN_CSV.replace('"35 087"', '"trasigt"')
|
|
const result = parseSkattekontoFile(garbled, MODERN_FILENAME)
|
|
expect(result.sum_valid).toBe(false)
|
|
expect(result.issues.some((i) => i.severity === 'error')).toBe(true)
|
|
})
|
|
|
|
it('flags a sum mismatch as an error', () => {
|
|
const truncated = MODERN_CSV.replace(
|
|
'"2026-07-28";"Inbetalning bokförd 260727";"35 000"\r\n',
|
|
'',
|
|
)
|
|
const result = parseSkattekontoFile(truncated, MODERN_FILENAME)
|
|
expect(result.sum_valid).toBe(false)
|
|
expect(result.issues.some((i) => i.severity === 'error')).toBe(true)
|
|
})
|
|
|
|
it('skips malformed rows with warnings', () => {
|
|
const withBad = [
|
|
'"2026-06-06";"Kostnadsränta";"-10"',
|
|
'"inte-ett-datum";"Trasig rad";"-5"',
|
|
'"2026-06-07";"";"-5"',
|
|
'"2026-06-08";"Kostnadsränta";"abc"',
|
|
].join('\n')
|
|
const result = parseSkattekontoFile(withBad, 'export.csv')
|
|
expect(result.rows).toHaveLength(1)
|
|
expect(result.stats.skipped_rows).toBe(3)
|
|
expect(result.issues.filter((i) => i.severity === 'warning')).toHaveLength(3)
|
|
})
|
|
|
|
it('notes identical rows with an info issue and keeps both', () => {
|
|
const duplicated = [
|
|
'"2026-06-06";"Kostnadsränta";"-10"',
|
|
'"2026-06-06";"Kostnadsränta";"-10"',
|
|
].join('\n')
|
|
const result = parseSkattekontoFile(duplicated, 'export.csv')
|
|
expect(result.rows).toHaveLength(2)
|
|
expect(result.issues.some((i) => i.severity === 'info')).toBe(true)
|
|
})
|
|
|
|
it('strips a BOM before the header row', () => {
|
|
const withBom = '\uFEFF' + MODERN_CSV
|
|
const result = parseSkattekontoFile(withBom, MODERN_FILENAME)
|
|
expect(result.org_number).toBe('556677-8899')
|
|
expect(result.rows).toHaveLength(9)
|
|
})
|
|
|
|
it('handles empty content', () => {
|
|
const result = parseSkattekontoFile('', 'export.csv')
|
|
expect(result.rows).toHaveLength(0)
|
|
expect(result.sum_valid).toBeNull()
|
|
})
|
|
|
|
it('tolerates a dated marker row without importing it', () => {
|
|
const legacyMarkers = [
|
|
'2024-01-01;Ingående saldo;0;0',
|
|
'2024-01-12;Debiterad preliminärskatt;-8000;-8000',
|
|
'2024-01-31;Utgående saldo;-8000;-8000',
|
|
].join('\n')
|
|
const result = parseSkattekontoFile(legacyMarkers, 'skattekonto.skv')
|
|
expect(result.rows).toHaveLength(1)
|
|
expect(result.opening_saldo).toBe(0)
|
|
expect(result.closing_saldo).toBe(-8000)
|
|
expect(result.sum_valid).toBe(true)
|
|
})
|
|
})
|