fix(import): let a skattekontoutdrag that does not sum through a confirm gate (#1675)

* fix(import): let a skattekontoutdrag that does not sum through a confirm gate

The skattekonto file parser refused any statement where ingående saldo plus
händelser did not equal utgående saldo with a bare 400 and no figures. A
real export hit it on 2026-08-18 and the user had no way forward, and the
logs carried nothing to diagnose it with. Nothing is booked at import and
the dedup contract makes a later complete re-import safe, so refusing the
file only blocked the rows that WERE readable.

- Parser: report events_sum / sum_difference / unreadable_amount_rows
  instead of just a boolean; reduce several marker pairs to the earliest
  opening and latest closing (per-year sections, newest-first files); read
  a marker saldo from a trailing running-saldo column when the belopp cell
  is empty; accept U+2212 and dash lookalikes as minus and a leading plus.
- Route: no longer 400s on sum_valid=false; logs the figures (amounts and
  counts, never row text) so the next report is diagnosable. Zero readable
  rows still refuses. SKATTEKONTO_FILE_SUM_MISMATCH removed (unused).
- Preview: an "Utdraget summerar inte" card with ingående, händelser,
  ingående+händelser, utgående and differens plus a confirm checkbox that
  gates the import button, mirroring the orgnr-mismatch gate. A one-line
  note explains that nothing is booked at import and that events already
  carrying a 1630 verifikat are offered as a link, not a second booking.

Verified end to end in the sandbox: gate renders, import proceeds after
confirmation, rows land on /skattekonto with Matcha/Bokför.

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

* fix(import): round the derived händelser total and fall back to the date cell for an invalid marker date

Review nits on #1675.

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>
This commit is contained in:
Jakob Wennberg
2026-08-18 10:33:51 +02:00
committed by GitHub
co-authored by Claude Fable 5 Jakob Wennberg
parent 2b5b813b7a
commit 387e1fb7f1
10 changed files with 314 additions and 42 deletions
@@ -92,14 +92,35 @@ describe('POST /api/import/skattekonto-file/parse', () => {
expect(body.error.code).toBe('SKATTEKONTO_FILE_NOT_RECOGNIZED')
})
it('rejects a statement that does not sum', async () => {
it('returns a statement that does not sum with the gap for the preview gate', async () => {
enqueue({ data: null }) // no prior import
enqueue({ data: { org_number: '556677-8899' } }) // company_settings
enqueue({ data: [] }) // existing rows page
const broken = MODERN_CSV.replace('"23 490"', '"99 999"')
const { status, body } = await jsonOf(
await POST(makeFileRequest(broken, 'Kontoutdrag 556677-8899 2026-05-03--2026-08-01.csv'), emptyParams),
)
expect(status).toBe(200)
expect(body.data.parse_result.sum_valid).toBe(false)
expect(body.data.parse_result.opening_saldo).toBe(-500)
expect(body.data.parse_result.events_sum).toBe(23490)
expect(body.data.parse_result.closing_saldo).toBe(99999)
expect(body.data.parse_result.sum_difference).toBe(76509)
// The rows are still returned: nothing is booked at import, so the user
// can import the events that ARE in the file and confirm the gap.
expect(body.data.parse_result.rows).toHaveLength(2)
})
it('still refuses a statement with no readable events', async () => {
enqueue({ data: null }) // no prior import
const empty = [
'"Testbolaget AB";"556677-8899";""',
'"";"Ingående saldo 2026-05-03";"-500"',
'"";"Utgående saldo 2026-08-01";"-500"',
].join('\r\n')
const { status, body } = await jsonOf(await POST(makeFileRequest(empty), emptyParams))
expect(status).toBe(400)
expect(body.error.code).toBe('SKATTEKONTO_FILE_SUM_MISMATCH')
expect(body.error.code).toBe('SKATTEKONTO_FILE_NO_ROWS')
})
it('parses a valid statement and partitions against existing rows', async () => {
+19 -9
View File
@@ -75,19 +75,29 @@ export const POST = withRouteContext(
const parseResult = parseSkattekontoFile(content, file.name)
if (parseResult.sum_valid === false) {
return errorResponseFromCode('SKATTEKONTO_FILE_SUM_MISMATCH', opLog, {
requestId,
details: {
openingSaldo: parseResult.opening_saldo,
closingSaldo: parseResult.closing_saldo,
},
})
}
if (parseResult.rows.length === 0) {
return errorResponseFromCode('SKATTEKONTO_FILE_NO_ROWS', opLog, { requestId })
}
// A statement that does not sum is no longer refused: the preview shows
// the gap and asks for confirmation (see parser.ts). Log the figures so
// a report of "utdraget summerar inte" can be diagnosed without the
// file: amounts and counts only, never row text.
if (parseResult.sum_valid === false) {
opLog.warn('skattekonto file does not sum', {
openingSaldo: parseResult.opening_saldo,
closingSaldo: parseResult.closing_saldo,
eventsSum: parseResult.events_sum,
sumDifference: parseResult.sum_difference,
parsedRows: parseResult.stats.parsed_rows,
skippedRows: parseResult.stats.skipped_rows,
unreadableAmountRows: parseResult.stats.unreadable_amount_rows,
dateFrom: parseResult.date_from,
dateTo: parseResult.date_to,
variant: parseResult.variant,
})
}
// Wrong-company guard: the modern export names its orgnr in the header
// row. A mismatch is surfaced for the preview step to confirm, not a
// hard block (legacy files have no header at all).