fix: Swedish VAT compliance — representation, domestic RC, full 26xx mapping, SIE (#206)
* feat: add INK2 declaration improvements, invoice delivery date, and Swedish compliance skills Expand INK2 engine with full INK2S/INK2R support and improved SRU generation. Add delivery_date field to invoices and corresponding PDF/migration support. Add Claude skills for Swedish asset accounting, invoice compliance, SIE import/export, SRU filing, and tax planning. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address PR review — map BAS 4500–4899, strip CRLF in SRU, document P3 - Map BAS accounts 4500–4599 (legoarbeten), 4700–4899 (diverse varuinköpskostnader) to SRU 7512 so they are not silently dropped from INK2R declarations - Strip \r\n in sanitizeString to prevent CRLF injection in SRU fields - Document P3 period suffix limitation for brutet räkenskapsår Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: correct BAS 4500-4599, 4700-4899 mapping from 7512 to 7511 Per the official BAS-to-SRU mapping, these account ranges are cost of goods (legoarbeten, inkurans, svinn) and belong under 7511 (Råvaror och förnödenheter), not 7512 (Handelsvaror). 7512 remains 4600-4699. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: Swedish VAT compliance — representation VAT, domestic RC, full BAS 26xx mapping, SIE encoding - Representation expenses now default to reduced_12 VAT (ML 13 kap 24-25 §§); income tax deduction was abolished 2017 but VAT deduction at 12% remains - Domestic reverse charge (byggtjänster etc.) uses 2647 instead of 2645, with distinct line descriptions for Swedish vs EU/non-EU RC - VAT declaration maps all BAS 26xx variant accounts (egna uttag 2612/2622/2632, uthyrning 2613/2623/2633, VMB 2616/2626/2636, import 2615/2625/2635, domestic RC 2647, frivillig skattskyldighet 2642) and revenue variants (3108/3105/3004/3100) to correct momsdeklaration rutor - SIE parser: remove unreliable #FORMAT PC8 encoding detection (most software exports UTF-8 with PC8 header), parse #FLAGGA for import-already-done warning, default SIE type to 1 when absent, fix RTRANS/BTRANS documentation - SIE export: add #RAR -1 (previous fiscal year), fix UB = IB + movements - Error messages: add pattern matching for locked period trigger errors Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address Greptile review — update ruta49 JSDoc, use null sentinel in error map Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
7a18d89c70
commit
6ccd4f429c
@@ -8,6 +8,7 @@ function makeParsedFile(overrides?: Partial<ParsedSIEFile>): ParsedSIEFile {
|
||||
return {
|
||||
header: {
|
||||
sieType: 4,
|
||||
flagga: 0,
|
||||
program: 'TestProg',
|
||||
programVersion: '1.0',
|
||||
generatedDate: '2024-01-01',
|
||||
|
||||
@@ -440,16 +440,19 @@ describe('validateSIEFile', () => {
|
||||
// --- Fix 2: Windows-1252 encoding detection and decoding ---
|
||||
|
||||
describe('detectEncoding — #FORMAT PC8 detection', () => {
|
||||
it('returns cp437 when #FORMAT PC8 is present in the first 500 bytes', () => {
|
||||
const text = '#FLAGGA 0\n#FORMAT PC8\n#SIETYP 4\n'
|
||||
const encoder = new TextEncoder()
|
||||
it('ignores #FORMAT PC8 and detects UTF-8 from byte patterns', () => {
|
||||
// #FORMAT PC8 is unreliable — most cloud software (Fortnox, Bokio etc.)
|
||||
// exports UTF-8 but still declares #FORMAT PC8.
|
||||
// UTF-8 encoded: "Företagskonto" → 0xC3 0xB6 for ö
|
||||
const text = '#FLAGGA 0\n#FORMAT PC8\n#FNAMN "Företagskonto"\n'
|
||||
const encoder = new TextEncoder() // TextEncoder outputs UTF-8
|
||||
const buf = encoder.encode(text)
|
||||
const encoding = detectEncoding(buf.buffer)
|
||||
expect(encoding).toBe('cp437')
|
||||
expect(encoding).toBe('utf8')
|
||||
})
|
||||
|
||||
it('returns cp437 even when Win-1252 bytes follow #FORMAT PC8', () => {
|
||||
// #FORMAT PC8 header should take priority over any byte analysis
|
||||
it('detects Win-1252 when actual byte values are in Win-1252 range', () => {
|
||||
// Win-1252 bytes for Swedish chars: ö=0xF6, ä=0xE4, å=0xE5
|
||||
const prefix = new TextEncoder().encode('#FORMAT PC8\n#FNAMN F')
|
||||
const buf = new Uint8Array(prefix.length + 3)
|
||||
buf.set(prefix)
|
||||
@@ -457,7 +460,15 @@ describe('detectEncoding — #FORMAT PC8 detection', () => {
|
||||
buf[prefix.length + 1] = 0xe4 // ä in Win-1252
|
||||
buf[prefix.length + 2] = 0xe5 // å in Win-1252
|
||||
const encoding = detectEncoding(buf.buffer)
|
||||
expect(encoding).toBe('cp437')
|
||||
expect(encoding).toBe('windows1252')
|
||||
})
|
||||
|
||||
it('returns utf8 for pure ASCII files (no high bytes)', () => {
|
||||
const text = '#FLAGGA 0\n#FORMAT PC8\n#SIETYP 4\n'
|
||||
const encoder = new TextEncoder()
|
||||
const buf = encoder.encode(text)
|
||||
const encoding = detectEncoding(buf.buffer)
|
||||
expect(encoding).toBe('utf8')
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
+32
-34
@@ -94,24 +94,13 @@ export function detectEncoding(buffer: ArrayBuffer): SIEEncoding {
|
||||
return 'utf8'
|
||||
}
|
||||
|
||||
// Check for #FORMAT PC8 in the first 500 bytes (ASCII-safe, works regardless of encoding)
|
||||
const headerSize = Math.min(bytes.length, 500)
|
||||
const FORMAT_PC8 = [0x23, 0x46, 0x4f, 0x52, 0x4d, 0x41, 0x54, 0x20, 0x50, 0x43, 0x38]
|
||||
for (let i = 0; i <= headerSize - FORMAT_PC8.length; i++) {
|
||||
let match = true
|
||||
for (let j = 0; j < FORMAT_PC8.length; j++) {
|
||||
if (bytes[i + j] !== FORMAT_PC8[j]) {
|
||||
match = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if (match) {
|
||||
return 'cp437'
|
||||
}
|
||||
}
|
||||
// NOTE: #FORMAT PC8 is NOT used for encoding detection.
|
||||
// Almost all SIE files declare #FORMAT PC8 regardless of actual encoding
|
||||
// (Fortnox, Bokio, Dooer etc. export UTF-8 with #FORMAT PC8).
|
||||
// Instead, we detect encoding from actual byte patterns.
|
||||
|
||||
// Scan sample for encoding-specific byte ranges
|
||||
const sampleSize = Math.min(bytes.length, 2000)
|
||||
const sampleSize = Math.min(bytes.length, 4000)
|
||||
let cp437Count = 0 // Swedish chars in 0x80-0x9F (CP437 range)
|
||||
let utf8Count = 0 // Valid UTF-8 multi-byte Swedish sequences
|
||||
let win1252Count = 0 // Swedish chars in 0xC0-0xFF (Win-1252 range)
|
||||
@@ -119,33 +108,35 @@ export function detectEncoding(buffer: ArrayBuffer): SIEEncoding {
|
||||
for (let i = 0; i < sampleSize; i++) {
|
||||
const byte = bytes[i]
|
||||
|
||||
// Check for CP437 Swedish characters
|
||||
if (CP437_MAP[byte]) {
|
||||
cp437Count++
|
||||
}
|
||||
|
||||
// Check for Windows-1252 Swedish characters
|
||||
if (WIN1252_SWEDISH_BYTES.has(byte)) {
|
||||
win1252Count++
|
||||
}
|
||||
|
||||
// Check for UTF-8 multi-byte sequences for Swedish chars
|
||||
// Ä = C3 84, Å = C3 85, Ö = C3 96, ä = C3 A4, å = C3 A5, ö = C3 B6
|
||||
// Check for UTF-8 multi-byte sequences for Swedish chars FIRST
|
||||
// to avoid false CP437/Win-1252 counts from continuation bytes.
|
||||
// Ä = C3 84, Å = C3 85, Ö = C3 96, ä = C3 A4, å = C3 A5, ö = C3 B6, é = C3 A9
|
||||
if (byte === 0xc3 && i + 1 < sampleSize) {
|
||||
const nextByte = bytes[i + 1]
|
||||
if ([0x84, 0x85, 0x96, 0xa4, 0xa5, 0xb6].includes(nextByte)) {
|
||||
if ([0x84, 0x85, 0x96, 0xa4, 0xa5, 0xb6, 0xa9].includes(nextByte)) {
|
||||
utf8Count++
|
||||
i++ // Skip continuation byte to avoid false CP437 count (e.g. 0x84 = ä in CP437)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Check for CP437 Swedish characters (0x80-0x9F range)
|
||||
if (CP437_MAP[byte]) {
|
||||
cp437Count++
|
||||
}
|
||||
|
||||
// Check for Windows-1252 Swedish characters (0xC0-0xFF range)
|
||||
if (WIN1252_SWEDISH_BYTES.has(byte)) {
|
||||
win1252Count++
|
||||
}
|
||||
}
|
||||
|
||||
if (utf8Count > cp437Count && utf8Count > win1252Count) return 'utf8'
|
||||
if (cp437Count > win1252Count) return 'cp437'
|
||||
if (win1252Count > 0) return 'windows1252'
|
||||
return 'cp437'
|
||||
|
||||
// Pure ASCII (no high bytes) — UTF-8 is a superset of ASCII
|
||||
return 'utf8'
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -348,8 +339,10 @@ export function parseSIEFile(content: string): ParsedSIEFile {
|
||||
const issues: ParseIssue[] = []
|
||||
|
||||
// Initialize header with defaults
|
||||
// Per SIE spec: if #SIETYP is absent, assume type 1 (closing balances only)
|
||||
const header: SIEHeader = {
|
||||
sieType: 4,
|
||||
sieType: 1,
|
||||
flagga: null,
|
||||
program: null,
|
||||
programVersion: null,
|
||||
generatedDate: null,
|
||||
@@ -415,7 +408,7 @@ export function parseSIEFile(content: string): ParsedSIEFile {
|
||||
try {
|
||||
switch (tag) {
|
||||
case 'FLAGGA':
|
||||
// Flag for file handling - ignore
|
||||
header.flagga = parseInt(fields[1], 10) || 0
|
||||
break
|
||||
|
||||
case 'FORMAT':
|
||||
@@ -607,8 +600,8 @@ export function parseSIEFile(content: string): ParsedSIEFile {
|
||||
case 'RTRANS':
|
||||
case 'BTRANS': {
|
||||
// #TRANS = final transaction lines (the current state of the voucher)
|
||||
// #RTRANS = removed lines (correction audit trail — original lines that were undone)
|
||||
// #BTRANS = added lines (correction audit trail — new lines that replaced removed ones)
|
||||
// #RTRANS = supplementary/corrected transaction (must be followed by identical #TRANS for backward compat)
|
||||
// #BTRANS = removed/cancelled transaction (programs not understanding BTRANS simply ignore it)
|
||||
//
|
||||
// When a voucher has been corrected, Fortnox/Visma emit all three types.
|
||||
// Only #TRANS represents the final voucher state; #RTRANS and #BTRANS are
|
||||
@@ -732,6 +725,11 @@ export function validateSIEFile(parsed: ParsedSIEFile): ValidationResult {
|
||||
const errors: string[] = []
|
||||
const warnings: string[] = []
|
||||
|
||||
// Check #FLAGGA for already-imported files
|
||||
if (parsed.header.flagga === 1) {
|
||||
warnings.push('Filen är markerad som redan importerad (#FLAGGA 1). Kontrollera att den inte redan har importerats i ett annat system.')
|
||||
}
|
||||
|
||||
// Check for SIE type
|
||||
if (!parsed.header.sieType) {
|
||||
errors.push('SIE-typ saknas (#SIETYP). Filen kanske inte är en giltig SIE-fil — kontrollera att du exporterat i rätt format.')
|
||||
|
||||
@@ -26,6 +26,7 @@ export type ParseIssueSeverity = 'error' | 'warning' | 'info'
|
||||
export interface SIEHeader {
|
||||
// File metadata
|
||||
sieType: SIEType
|
||||
flagga: number | null // #FLAGGA (0 = not imported, 1 = already imported)
|
||||
program: string | null // #PROGRAM
|
||||
programVersion: string | null
|
||||
generatedDate: string | null // #GEN — "YYYY-MM-DD"
|
||||
|
||||
Reference in New Issue
Block a user