Files
accounted/lib/import/articles/__tests__/column-detector.test.ts
T
Jakob Wennberg 2d6ddeafc5 feat(import/export): article import + register export (xlsx/csv) (#750)
* feat(import/export): article import + register export (xlsx/csv)

Add CSV/Excel import for the article register (artiklar), mirroring the
existing customer/supplier import pipeline, plus Excel + CSV export for
articles, customers and suppliers.

Import (lib/import/articles + app/api/import/articles):
- Column auto-detection tuned to Fortnox/Visma/Bokio export headers,
  Swedish-decimal price parsing, VAT snapped to {0,6,12,25}, type/unit
  normalization.
- Dedup by article number then name; 23505 soft-skip; auto-number
  backfill; revenue-account override kept only when active, otherwise
  dropped with a warning (never mutates the chart of accounts).
- New "Artiklar" flow in the /import hub.

Export (app/api/export/* + lib/export/register-export):
- Read-only xlsx (default) / csv (?format=csv, UTF-8 BOM) downloads.
- Headers chosen so files round-trip back through the importer.
- "Exportera" menu added to the articles, customers and suppliers pages.

Refs #746. Direct Fortnox/Visma API article fetch tracked in #749.

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

* fix(import/export): address PR review — lint ratchet + export hardening

- xlsx-export: keep `SheetSpec<any>` on the eslint-disabled line (fixes the
  core-only lint ratchet regression: no-explicit-any 16 -> 15) and define
  UTF8_BOM as an explicit `` escape instead of a raw BOM character.
- export routes (articles/customers/suppliers): move the data queries inside
  the try/catch, add `Cache-Control: no-store`, and emit a `register exported`
  audit log line (entity, format, rowCount).
- articles parse route: validate `column_overrides` against a Zod schema before
  trusting it to drive the parser.

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

* fix(import): drop öre-round pattern on article column-detector confidence

The confidence score is a 0-1 heuristic, not money, and is only compared
against the 0.8 skip-mapping threshold. Removing the Math.round(x*100)/100
form clears the core-only antipattern ratchet (naive-ore-round 660 -> 659).

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

* feat(import): flag adjusted VAT rows in the article import edit step

Surface VAT snapping/defaulting per row, not just as a file-level warning:
the parser sets `vat_rate_adjusted`, the edit step highlights those rows'
VAT selector and shows a count banner, and confirming a rate clears the flag.
Addresses the Swedish-compliance review note that silent snapping could
otherwise store a wrong VAT rate at scale.

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

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 14:38:44 +02:00

63 lines
2.3 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { detectArticleColumns } from '../column-detector'
describe('detectArticleColumns', () => {
it('detects a rich Swedish header at high confidence', () => {
const cols = detectArticleColumns([
'Artikelnummer', 'Benämning', 'Typ', 'Enhet', 'Pris', 'Moms', 'Försäljningskonto',
])
expect(cols.name_col).toBe(1)
expect(cols.article_number_col).toBe(0)
expect(cols.type_col).toBe(2)
expect(cols.unit_col).toBe(3)
expect(cols.price_col).toBe(4)
expect(cols.vat_rate_col).toBe(5)
expect(cols.revenue_account_col).toBe(6)
expect(cols.confidence).toBeGreaterThanOrEqual(0.8)
})
it('keeps price, VAT and account in distinct columns (no keyword collision)', () => {
const cols = detectArticleColumns([
'Benämning', 'Pris exkl moms', 'Moms %', 'Försäljningskonto',
])
expect(cols.price_col).toBe(1) // "Pris exkl moms" → price, not VAT
expect(cols.vat_rate_col).toBe(2)
expect(cols.revenue_account_col).toBe(3)
expect(cols.price_col).not.toBe(cols.vat_rate_col)
})
it('does not read Fortnox "Momskod" as the article number', () => {
const cols = detectArticleColumns(['Benämning', 'Momskod'])
expect(cols.vat_rate_col).toBe(1)
expect(cols.article_number_col).toBeNull()
})
it('claims EAN before the generic article number', () => {
const cols = detectArticleColumns(['Benämning', 'EAN-nummer', 'Artikelnummer'])
expect(cols.ean_col).toBe(1)
expect(cols.article_number_col).toBe(2)
})
it('detects the English name column separately from the main name', () => {
const cols = detectArticleColumns(['Benämning', 'Benämning engelska'])
expect(cols.name_col).toBe(0)
expect(cols.name_en_col).toBe(1)
})
it('detects Fortnox-style headers', () => {
const cols = detectArticleColumns([
'Artikelnr', 'Benämning', 'Försäljningspris', 'Inköpspris', 'Momskod', 'Enhet',
])
expect(cols.article_number_col).toBe(0)
expect(cols.price_col).toBe(2)
expect(cols.cost_price_col).toBe(3)
expect(cols.vat_rate_col).toBe(4)
})
it('reports low confidence when only a name column is present', () => {
const cols = detectArticleColumns(['Benämning'])
expect(cols.name_col).toBe(0)
expect(cols.confidence).toBeLessThan(0.8)
})
})