Per-line VAT rates: - Add generatePerRateLines() to group invoice items by vat_rate with separate revenue + VAT lines per rate group (invoice-entries.ts) - Add getAvailableVatRates() and getVatTreatmentForRate() (vat-rules.ts) - PDF template shows per-line VAT column and per-rate totals for mixed-rate invoices - Invoice create/review UI supports per-line rate selection - Types: add vat_rate/vat_amount to InvoiceItem, vat_rate to CreateInvoiceItemInput Invoice document types (proforma, delivery note): - Add InvoiceDocumentType, document_type and converted_from_id to Invoice type - PDF hides prices for delivery notes, adds proforma notice - Email templates support all document types - mark-paid skips journal entries for non-invoice document types - Migration 031: invoice_document_type Accounting method support: - Add AccountingMethod type (accrual/cash) - Migration 032: add_accounting_method column to company_settings VAT declaration rewrite: - Rewrite to read directly from general ledger (26xx/3xxx account lines) instead of aggregating invoices/transactions/receipts - ACCOUNT_RUTA mapping drives momsdeklaration boxes from GL balances Bank reconciliation: - Transaction ingest now pre-fetches unlinked GL lines and attempts auto-reconciliation during import - Add transaction.reconciled event type - Add ReconciliationMethod type and reconciliation_method on Transaction - Migration 030: bank_reconciliation - New reconciliation engine, API routes, and BankReconciliationView component Pagination (fetchAllRows): - New lib/supabase/fetch-all.ts overcomes PostgREST 1000-row limit - Adopted in all report generators, SIE/SRU export, account list APIs Fiscal period validation: - New validate-period-duration.ts enforces max 18 months per BFL 3 kap. - Applied in period-service.ts and fiscal-periods API Account mapper simplification: - Remove Levenshtein/fuzzy matching, use exact account number match only Swedbank parser improvements: - Support abbreviated headers (Clnr, Bokfdag, Radnr) - Use Referens column as counterparty Chart of accounts management: - Add DELETE endpoint with system account and usage protection - PUT uses partial updates - New AccountCombobox, AddAccountDialog, EditAccountDialog, ChartOfAccountsManager Tax deadline corrections: - Rewrite inkomstdeklaration_ab using Skatteverket lookup table - Rewrite arsredovisning deadline to 7 months after FY end per ÅRL 8:3 Onboarding first fiscal year: - Add first fiscal year toggle with date pickers and 18-month validation UI terminology: - Change "okategoriserad/kategorisera" to "obokförd/bokföra" throughout Report column fix: - Fix start_date/end_date to period_start/period_end in report queries Supplier invoice input: - CreateSupplierInvoiceItemInput uses amount field (legacy quantity/unit_price kept) Misc: - SIE import uses upsert for idempotent account creation - account-descriptions.ts falls back to BAS reference data - Add invoice_default_notes to CompanySettings - Update CLAUDE.md to reflect current project state Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
196 lines
6.5 KiB
TypeScript
196 lines
6.5 KiB
TypeScript
/**
|
|
* Swedbank CSV format parser
|
|
*
|
|
* Format: Comma-delimited, PERIOD decimal separator (exception among Swedish banks!)
|
|
* Columns (real export): Radnr, Clnr, Kontonr, Produkt, Valuta, Bokfdag, Transdag,
|
|
* Valutadag, Referens, Text, Belopp, Saldo
|
|
* Date format: YYYY-MM-DD
|
|
* Encoding: UTF-8 or Windows-1252
|
|
*
|
|
* Notes:
|
|
* - First line is metadata (e.g. "* Transaktionsrapport Period ..."), SKIP it
|
|
* - Second line is the actual header
|
|
* - Uses period as decimal separator (unlike Nordea/SEB/Handelsbanken)
|
|
* - Headers may be abbreviated (Clnr vs Clearingnummer, Bokfdag vs Bokföringsdatum)
|
|
* - Referens column contains counterparty/payee name
|
|
* - Text column contains transaction type (e.g. "Bg-bet. via internet")
|
|
*/
|
|
|
|
import type { BankFileFormat, BankFileParseResult, ParsedBankTransaction, BankFileParseIssue } from '../types'
|
|
import { prepareContent } from '../encoding'
|
|
import { parseCSVLine } from './nordea'
|
|
|
|
/**
|
|
* Check if a header value matches any of the given patterns (case-insensitive)
|
|
*/
|
|
function matchesHeader(header: string, patterns: string[]): boolean {
|
|
const h = header.toLowerCase()
|
|
return patterns.some((p) => h === p || h.includes(p))
|
|
}
|
|
|
|
/**
|
|
* Check if a line looks like a Swedbank header row
|
|
*/
|
|
function isSwedbankHeader(line: string): boolean {
|
|
const lower = line.toLowerCase()
|
|
return (
|
|
// Full names (legacy or alternative exports)
|
|
lower.includes('clearingnummer') || lower.includes('radnummer') ||
|
|
// Abbreviated names (current export format)
|
|
/\bradnr\b/.test(lower) || /\bclnr\b/.test(lower) ||
|
|
// Combination of typical Swedbank columns
|
|
(lower.includes('bokfdag') && lower.includes('belopp'))
|
|
)
|
|
}
|
|
|
|
export const swedbankFormat: BankFileFormat = {
|
|
id: 'swedbank',
|
|
name: 'Swedbank',
|
|
description: 'Swedbank CSV (comma-delimited, period decimal)',
|
|
fileExtensions: ['.csv', '.txt'],
|
|
|
|
detect(content: string, _filename: string): boolean {
|
|
const prepared = prepareContent(content)
|
|
const lines = prepared.split('\n')
|
|
const line1 = lines[0] || ''
|
|
const line2 = lines[1] || ''
|
|
|
|
return isSwedbankHeader(line1) || isSwedbankHeader(line2)
|
|
},
|
|
|
|
parse(content: string): BankFileParseResult {
|
|
const prepared = prepareContent(content)
|
|
const lines = prepared.split('\n').filter((line) => line.trim() !== '')
|
|
|
|
const transactions: ParsedBankTransaction[] = []
|
|
const issues: BankFileParseIssue[] = []
|
|
let skippedRows = 0
|
|
|
|
// Find the header row — may be line 0 or line 1 (if line 0 is metadata)
|
|
let headerLineIdx = -1
|
|
for (let i = 0; i < Math.min(lines.length, 3); i++) {
|
|
if (isSwedbankHeader(lines[i])) {
|
|
headerLineIdx = i
|
|
break
|
|
}
|
|
}
|
|
|
|
if (headerLineIdx === -1) {
|
|
issues.push({
|
|
row: 1,
|
|
message: 'Could not find Swedbank header row',
|
|
severity: 'error',
|
|
})
|
|
return {
|
|
format: 'swedbank',
|
|
format_name: 'Swedbank',
|
|
transactions: [],
|
|
date_from: null,
|
|
date_to: null,
|
|
issues,
|
|
stats: { total_rows: 0, parsed_rows: 0, skipped_rows: 0, total_income: 0, total_expenses: 0 },
|
|
}
|
|
}
|
|
|
|
const headerLine = lines[headerLineIdx] || ''
|
|
const headers = parseCSVLine(headerLine, ',').map((h) =>
|
|
h.trim().toLowerCase().replace(/"/g, '')
|
|
)
|
|
|
|
// Find column indices — support both abbreviated and full header names
|
|
const dateIdx = headers.findIndex((h) =>
|
|
matchesHeader(h, ['bokfdag', 'bokföringsdatum', 'datum'])
|
|
)
|
|
const descIdx = headers.findIndex((h) => h === 'text' || h.includes('beskrivning'))
|
|
const amountIdx = headers.findIndex((h) => h === 'belopp')
|
|
const balanceIdx = headers.findIndex((h) => h === 'saldo')
|
|
const referenceIdx = headers.findIndex((h) => h === 'referens')
|
|
|
|
if (dateIdx === -1 || amountIdx === -1) {
|
|
issues.push({
|
|
row: headerLineIdx + 1,
|
|
message: `Could not identify required columns (datum, belopp). Found headers: ${headers.join(', ')}`,
|
|
severity: 'error',
|
|
})
|
|
return {
|
|
format: 'swedbank',
|
|
format_name: 'Swedbank',
|
|
transactions: [],
|
|
date_from: null,
|
|
date_to: null,
|
|
issues,
|
|
stats: { total_rows: 0, parsed_rows: 0, skipped_rows: 0, total_income: 0, total_expenses: 0 },
|
|
}
|
|
}
|
|
|
|
// Data starts after header
|
|
for (let i = headerLineIdx + 1; i < lines.length; i++) {
|
|
const line = lines[i].trim()
|
|
if (!line) continue
|
|
|
|
const fields = parseCSVLine(line, ',').map((f) => f.trim().replace(/^"|"$/g, ''))
|
|
|
|
const date = fields[dateIdx]
|
|
const reference = referenceIdx >= 0 ? fields[referenceIdx]?.trim() : null
|
|
const textDesc = descIdx >= 0 ? fields[descIdx]?.trim() : null
|
|
const amountStr = fields[amountIdx]
|
|
const balanceStr = balanceIdx >= 0 ? fields[balanceIdx] : undefined
|
|
|
|
if (!date || !amountStr) {
|
|
skippedRows++
|
|
continue
|
|
}
|
|
|
|
// Swedbank uses PERIOD decimal separator
|
|
const amount = parseFloat(amountStr.replace(/\s/g, ''))
|
|
if (isNaN(amount)) {
|
|
issues.push({ row: i + 1, message: `Invalid amount: ${amountStr}`, severity: 'warning' })
|
|
skippedRows++
|
|
continue
|
|
}
|
|
|
|
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) {
|
|
issues.push({ row: i + 1, message: `Invalid date: ${date}`, severity: 'warning' })
|
|
skippedRows++
|
|
continue
|
|
}
|
|
|
|
const balance = balanceStr ? parseFloat(balanceStr.replace(/\s/g, '')) : null
|
|
|
|
// Build description: use reference (counterparty) as primary, text as secondary
|
|
const description = reference && textDesc
|
|
? `${reference} — ${textDesc}`
|
|
: reference || textDesc || 'Unknown'
|
|
|
|
transactions.push({
|
|
date,
|
|
description,
|
|
amount,
|
|
currency: 'SEK',
|
|
balance: isNaN(balance as number) ? null : balance,
|
|
reference: null,
|
|
counterparty: reference || null,
|
|
raw_line: line,
|
|
})
|
|
}
|
|
|
|
const dates = transactions.map((t) => t.date).sort()
|
|
|
|
return {
|
|
format: 'swedbank',
|
|
format_name: 'Swedbank',
|
|
transactions,
|
|
date_from: dates[0] || null,
|
|
date_to: dates[dates.length - 1] || null,
|
|
issues,
|
|
stats: {
|
|
total_rows: lines.length - headerLineIdx - 1,
|
|
parsed_rows: transactions.length,
|
|
skipped_rows: skippedRows,
|
|
total_income: Math.round(transactions.filter((t) => t.amount > 0).reduce((s, t) => s + t.amount, 0) * 100) / 100,
|
|
total_expenses: Math.round(transactions.filter((t) => t.amount < 0).reduce((s, t) => s + t.amount, 0) * 100) / 100,
|
|
},
|
|
}
|
|
},
|
|
}
|