fix(reports): always emit compulsory #FORMAT PC8 in SIE export (#1466)

#FORMAT is a compulsory record in every SIE type and PC8 is its only
legal value. We only emitted it when the caller opted into cp437 byte
encoding, so the default UTF-8 download had no #FORMAT line and strict
importers (Visma Spiris) rejected the file with 'Etiketten #FORMAT
saknas i filen'. Cloud exporters (Fortnox, Bokio) ship UTF-8 bytes with
#FORMAT PC8 and importers detect the real encoding from the bytes, so
the tag is now unconditional.

Also formats #ORGNR as nnnnnn-nnnn per spec; company_settings stores
the org number without a hyphen.

Co-authored-by: Jakob Wennberg <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
Jakob Wennberg
2026-08-08 15:04:07 +02:00
committed by GitHub
co-authored by Jakob Wennberg Claude Fable 5
parent 799fa1246a
commit ddbe9b1379
6 changed files with 36 additions and 14 deletions
+1
View File
File diff suppressed because one or more lines are too long
-1
View File
@@ -36,7 +36,6 @@ export const GET = withRouteContext(
company_name: company.company_name || 'Unknown',
org_number: company.org_number,
exclude_year_end_closing: excludeClosing,
emit_format_pc8: useCP437,
})
const body = useCP437 ? Buffer.from(encodeSIEToCP437(sieContent)) : sieContent
@@ -28,7 +28,7 @@ registerEndpoint({
pitfalls: [
'`period_id` is required.',
'The response is text/plain with Content-Disposition: attachment: clients should treat as a binary download. Filename uses the pattern `export_{period_id}.se`.',
'Default encoding is UTF-8 (no #FORMAT PC8 tag). Pass `encoding=cp437` to get a spec-compliant CP437-encoded file with #FORMAT PC8, required by some legacy desktop bookkeeping software.',
'The compulsory #FORMAT PC8 tag is always present, but default byte encoding is UTF-8 (the de-facto cloud convention; importers detect encoding from the bytes). Pass `encoding=cp437` for actual CP437 bytes, required by some legacy desktop bookkeeping software.',
'Only `posted` entries are exported; drafts and reversed entries\' originals are included but marked accordingly.',
],
example: {
@@ -76,7 +76,6 @@ export const GET = withApiV1<{ params: Promise<{ companyId: string }> }>(
company_name: (company as { company_name: string | null }).company_name || 'Unknown',
org_number: (company as { org_number: string | null }).org_number,
exclude_year_end_closing: excludeClosing,
emit_format_pc8: useCP437,
}),
{ log: ctx.log, requestId: ctx.requestId, reportName: 'sie-export' },
)
+26 -7
View File
@@ -97,13 +97,32 @@ describe('generateSIEExport', () => {
const lines = output.split('\r\n')
expect(lines[0]).toBe('#FLAGGA 0')
expect(lines[1]).toBe('#SIETYP 4')
expect(lines[2]).toMatch(/^#PROGRAM "ERPBase" "1\.0"$/)
expect(lines[3]).toMatch(/^#GEN \d{8}$/)
expect(lines[4]).toBe('#ORGNR 556677-8899')
expect(lines[5]).toBe('#FNAMN "Test AB"')
expect(lines[6]).toBe('#RAR 0 20240101 20241231')
expect(output).not.toContain('#FORMAT PC8')
expect(lines[1]).toBe('#FORMAT PC8')
expect(lines[2]).toBe('#SIETYP 4')
expect(lines[3]).toMatch(/^#PROGRAM "ERPBase" "1\.0"$/)
expect(lines[4]).toMatch(/^#GEN \d{8}$/)
expect(lines[5]).toBe('#ORGNR 556677-8899')
expect(lines[6]).toBe('#FNAMN "Test AB"')
expect(lines[7]).toBe('#RAR 0 20240101 20241231')
})
it('hyphenates a bare 10-digit org_number in #ORGNR', async () => {
results = [
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
{ data: null, error: null }, // prevPeriod
{ data: [], error: null }, // accounts
{ data: [], error: null }, // journal_entries (no entries -> line fetch skipped)
{ data: [], error: null }, // dimensions
{ data: [], error: null }, // dimension_values
{ data: [], error: null }, // opening balances RPC
]
const output = await generateSIEExport(supabase, 'company-1', {
...baseOptions,
org_number: '5566778899',
})
expect(output).toContain('#ORGNR 556677-8899')
})
it('omits #ORGNR when org_number is null', async () => {
+8 -2
View File
@@ -2,6 +2,7 @@ import type { SupabaseClient } from '@supabase/supabase-js'
import { fetchAllRows } from '@/lib/supabase/fetch-all'
import { fetchLinesByEntryIds } from '@/lib/bookkeeping/entry-lines'
import { getBranding } from '@/lib/branding/service'
import { formatOrgNumber } from '@/lib/utils'
import { createLogger } from '@/lib/logger'
import { getOpeningBalances } from './opening-balances'
import type { SIEExportOptions, JournalEntry, JournalEntryLine, BASAccount } from '@/types'
@@ -158,14 +159,19 @@ export async function generateSIEExport(
// === Header ===
lines.push('#FLAGGA 0')
if (options.emit_format_pc8) lines.push('#FORMAT PC8')
// #FORMAT is compulsory in every SIE type and PC8 is its only legal value.
// Strict importers (e.g. Visma Spiris) reject files without it, and they
// detect the actual byte encoding themselves, so it is emitted even when
// the output is served as UTF-8.
lines.push('#FORMAT PC8')
lines.push('#SIETYP 4')
const programName = sanitizeProgramName(options.program_name || getBranding().appName)
lines.push(`#PROGRAM "${programName}" "1.0"`)
lines.push(`#GEN ${formatSIEDate(now)}`)
if (options.org_number) {
lines.push(`#ORGNR ${options.org_number}`)
// Spec format is nnnnnn-nnnn; company_settings may store it without the hyphen.
lines.push(`#ORGNR ${formatOrgNumber(options.org_number)}`)
}
lines.push(`#FNAMN "${escapeQuotes(options.company_name)}"`)
-2
View File
@@ -2141,8 +2141,6 @@ export interface SIEExportOptions {
* our closing entry would zero out the P&L accounts.
*/
exclude_year_end_closing?: boolean
/** Emit #FORMAT PC8 in the header. Set true when the caller will encode the output as CP437. */
emit_format_pc8?: boolean
}
// Input types for creating entries