* 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>
94 lines
3.0 KiB
TypeScript
94 lines
3.0 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponse } from '@/lib/errors/get-structured-error'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
import { textColumn, integerColumn } from '@/lib/reports/xlsx-export'
|
|
import { buildRegisterExport, parseExportFormat, todayIso } from '@/lib/export/register-export'
|
|
import type { Customer } from '@/types'
|
|
|
|
/**
|
|
* GET /api/export/customers[?format=csv]
|
|
*
|
|
* Downloads the customer register as xlsx (default) or csv. Read-only — viewers
|
|
* may export. Headers match the customer importer's detector keywords so files
|
|
* round-trip.
|
|
*/
|
|
export const GET = withRouteContext(
|
|
'customer.export',
|
|
async (request, ctx) => {
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
const format = parseExportFormat(new URL(request.url).searchParams.get('format'))
|
|
|
|
try {
|
|
const { data: companyRow } = await supabase
|
|
.from('company_settings')
|
|
.select('company_name')
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
const customers = (await fetchAllRows(({ from, to }) =>
|
|
supabase
|
|
.from('customers')
|
|
.select('*')
|
|
.eq('company_id', companyId)
|
|
.order('name', { ascending: true })
|
|
.range(from, to),
|
|
)) as unknown as Customer[]
|
|
|
|
const { buffer, contentType, filename } = buildRegisterExport(
|
|
[
|
|
{
|
|
name: 'Kunder',
|
|
columns: [
|
|
textColumn('Namn'),
|
|
textColumn('Org-/personnummer'),
|
|
textColumn('Kundtyp'),
|
|
textColumn('E-post'),
|
|
textColumn('Telefon'),
|
|
textColumn('Adress'),
|
|
textColumn('Adressrad 2'),
|
|
textColumn('Postnummer'),
|
|
textColumn('Ort'),
|
|
textColumn('Land'),
|
|
textColumn('VAT-nummer'),
|
|
integerColumn('Betalningsvillkor'),
|
|
textColumn('Anteckning'),
|
|
],
|
|
rows: customers,
|
|
mapRow: (c) => [
|
|
c.name,
|
|
c.org_number ?? c.personal_number,
|
|
c.customer_type,
|
|
c.email,
|
|
c.phone,
|
|
c.address_line1,
|
|
c.address_line2,
|
|
c.postal_code,
|
|
c.city,
|
|
c.country,
|
|
c.vat_number,
|
|
c.default_payment_terms,
|
|
c.notes,
|
|
],
|
|
},
|
|
],
|
|
{ format, slug: 'kunder', companyName: companyRow?.company_name ?? '', date: todayIso() },
|
|
)
|
|
|
|
log.info('register exported', { entity: 'customers', format, rowCount: customers.length })
|
|
|
|
return new NextResponse(new Uint8Array(buffer), {
|
|
headers: {
|
|
'Content-Type': contentType,
|
|
'Content-Disposition': `attachment; filename="${filename}"`,
|
|
'Cache-Control': 'no-store',
|
|
},
|
|
})
|
|
} catch (err) {
|
|
log.error('customer export failed', err as Error)
|
|
return errorResponse(err, log, { requestId })
|
|
}
|
|
},
|
|
)
|