feat(dimensions): PR2 registry — CRUD API, register UI, settings toggle, SIE export on the new registry (#858)
* feat(dimensions): PR2 registry — CRUD API, register UI, settings toggle, SIE export on the new registry
Phase 2 of dev_docs/dimensions_implementation_plan.md. Companies with
dimensions_enabled=false (default) see zero change.
API:
- Dashboard CRUD: GET /api/dimensions (lazy-seeds system dims 1/6 via the
ensure_company_dimensions RPC), PATCH /api/dimensions/[id] (is_system
rename blocked), POST/PATCH/DELETE values (code immutable after creation;
strict Fortnox code format ^[A-Za-z0-9ÅÄÖåäö_+\-]{1,20}$ at the API layer;
retention-trigger deletes surface the Swedish "arkivera istället" message
as 409 DIMENSION_VALUE_REFERENCED).
- POST /api/dimensions/import-existing — scans journal_entry_lines.dimensions
for unregistered codes and mints inactive placeholder registry rows.
- v1 public API: GET dimensions + POST values (Idempotency-Key, dry-run),
registered in the OpenAPI spec (102→104 endpoints).
- dimensions_enabled boolean on company_settings (new migration,
UI-visibility only, never correctness-bearing) exposed through the
existing settings read/update path.
SIE export (lib/reports/sie-export.ts):
- Reads the new dimensions/dimension_values registry; legacy
cost_centers/projects tables now have zero readers (drop migration next).
- Fixes the latent Visma-rejection bug: #OBJEKT now declared for INACTIVE
values referenced by lines.
- Generic-N: #DIM/#UNDERDIM loop sorted by sie_dim_no; #TRANS object lists
serialize from the line JSONB map (sorted, '01'→'1' collapse); orphan
codes/dims synthesize declarations from the SIE reserved-number seed —
every referenced (dim, code) pair is guaranteed declared.
UI:
- /dimensions register (Register-recipe): tabs per dimension, search,
sortable table, value dialog (code immutable on edit, projekt dates on
dim 6), archive-not-delete affordances.
- DimensionCombobox shipped (mounts in the tagging PR).
- Settings toggle "Aktivera kostnadsställen & projekt" — toggle-on runs the
import-existing scan and links to the register.
- Nav row in redovisning, rendered only when dimensions_enabled (same
mechanism as pays_salaries).
- dimensions.* i18n namespace (51 keys, sv/en parity).
- Sandbox seed: demo dims + values, revenue line tagged {"1":"BUTIK","6":"P001"}.
Verified: 6328/6328 unit tests, guard + coverage gate green, tsc parity with
main (210=210), production build passes.
Co-Authored-By: Claude Fable 5 <[email protected]>
* fix(dimensions): PR2 review round — atomic archived-create, UNDERDIM ordering, import robustness, date semantics
- POST values accepts is_active so "create as archived" is atomic; the UI's
fragile create-then-PATCH fallback is deleted (PR Agent finding 1).
- DimensionCombobox blur revert reads the committed value/values through refs
so a selection landing inside the 150ms window always wins (finding 2).
- import-existing sanitizes candidate codes like the PR1 backfill and upserts
with ignoreDuplicates — one bad/duplicate code can no longer abort the
batch; created counted from returned rows (finding 3).
- SIE export emits all root #DIM before any #UNDERDIM so a parent always
precedes a lower-numbered child (SIE4 declaration order — Swedish review);
synthesized placeholder declarations now log one structured warning
(BFNAR 2013:2 behandlingshistorik) + defence-in-depth comment.
- Value dates rejected (400 DIMENSION_VALUE_DATES_NOT_ALLOWED) when the
parent dimension is flow-period (resets_annually=true); explicit null
still clears (Swedish review).
- Sandbox seed logs seeded dimension codes; GET /api/dimensions documents
the deliberate absence of dimensions_enabled gating (UI-visibility flag,
not a security boundary — compliance-swarm V8.2.1 rejected by design).
Co-Authored-By: Claude Fable 5 <[email protected]>
---------
Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5
parent
8cc2efb083
commit
8bb49c07a2
@@ -713,6 +713,67 @@ export const CorrectJournalEntrySchema = z.object({
|
||||
lines: z.array(CreateJournalEntryLineSchema).min(2, 'At least two lines are required for double-entry'),
|
||||
})
|
||||
|
||||
// ============================================================
|
||||
// Dimension registry schemas (kostnadsställe/projekt)
|
||||
// ============================================================
|
||||
// dev_docs/dimensions_implementation_plan.md §6. The registry tables
|
||||
// (dimensions/dimension_values) shipped in 20260702084500_dimensions_substrate.
|
||||
|
||||
/**
|
||||
* Object code for USER-CREATED dimension values: strict Fortnox format.
|
||||
* Deliberately tighter than both the DB CHECK (1..40 chars, no `"{}`') and
|
||||
* DimensionsBagSchema (line-level values) — legacy free-text codes from the
|
||||
* backfill/SIE import must survive on lines, but new registry codes minted
|
||||
* through the API stay portable to Fortnox/Visma.
|
||||
*/
|
||||
const dimensionValueCode = z
|
||||
.string()
|
||||
.regex(
|
||||
/^[A-Za-z0-9ÅÄÖåäö_+\-]{1,20}$/,
|
||||
'Koden får bara innehålla bokstäver (A–Ö), siffror, _, + och - (max 20 tecken)',
|
||||
)
|
||||
|
||||
const dimensionValueDates = {
|
||||
start_date: isoDate.nullable().optional(),
|
||||
end_date: isoDate.nullable().optional(),
|
||||
}
|
||||
|
||||
/** PATCH /api/dimensions/[id] — name is rejected route-side for is_system dims. */
|
||||
export const UpdateDimensionSchema = z
|
||||
.object({
|
||||
name: z.string().min(1).max(80).optional(),
|
||||
is_active: z.boolean().optional(),
|
||||
sort_order: z.number().int().min(0).optional(),
|
||||
})
|
||||
.refine((body) => Object.values(body).some((v) => v !== undefined), {
|
||||
message: 'Minst ett fält måste anges',
|
||||
})
|
||||
|
||||
/** POST /api/dimensions/[id]/values — code is immutable after creation (v1: no rename). */
|
||||
export const CreateDimensionValueSchema = z
|
||||
.object({
|
||||
code: dimensionValueCode,
|
||||
name: z.string().min(1).max(120),
|
||||
/** Omitted → true. Lets "create as archived" be a single atomic POST. */
|
||||
is_active: z.boolean().optional(),
|
||||
...dimensionValueDates,
|
||||
})
|
||||
.refine(
|
||||
(body) => !body.start_date || !body.end_date || body.end_date >= body.start_date,
|
||||
{ message: 'Slutdatum får inte vara före startdatum', path: ['end_date'] },
|
||||
)
|
||||
|
||||
/** PATCH /api/dimensions/[id]/values/[valueId] — no `code` field by design. */
|
||||
export const UpdateDimensionValueSchema = z
|
||||
.object({
|
||||
name: z.string().min(1).max(120).optional(),
|
||||
is_active: z.boolean().optional(),
|
||||
...dimensionValueDates,
|
||||
})
|
||||
.refine((body) => Object.values(body).some((v) => v !== undefined), {
|
||||
message: 'Minst ett fält måste anges',
|
||||
})
|
||||
|
||||
/**
|
||||
* Move a posted verifikation to a different date (and thereby fiscal period)
|
||||
* without changing its lines — fixes a booking entered with the wrong
|
||||
@@ -1172,6 +1233,9 @@ export const UpdateSettingsSchema = z.object({
|
||||
.optional(),
|
||||
// AI agent flow
|
||||
ai_flow_enabled: z.boolean().optional(),
|
||||
// Dimensions (kostnadsställe/projekt) — UI-visibility toggle only, never
|
||||
// load-bearing for correctness (dev_docs/dimensions_implementation_plan.md §2).
|
||||
dimensions_enabled: z.boolean().optional(),
|
||||
// Salary payment file
|
||||
preferred_payment_format: z.enum(['bg_lb', 'pain001']).optional(),
|
||||
}).refine(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`v1 spec snapshot > matches the recorded endpoint count > endpoint-count 1`] = `102`;
|
||||
exports[`v1 spec snapshot > matches the recorded endpoint count > endpoint-count 1`] = `104`;
|
||||
|
||||
exports[`v1 spec snapshot > matches the recorded endpoint key set > endpoint-keys 1`] = `
|
||||
[
|
||||
@@ -14,6 +14,7 @@ exports[`v1 spec snapshot > matches the recorded endpoint key set > endpoint-key
|
||||
"GET /api/v1/companies/:companyId/compliance/check",
|
||||
"GET /api/v1/companies/:companyId/customers",
|
||||
"GET /api/v1/companies/:companyId/customers/:id",
|
||||
"GET /api/v1/companies/:companyId/dimensions",
|
||||
"GET /api/v1/companies/:companyId/documents/:id/download",
|
||||
"GET /api/v1/companies/:companyId/employees",
|
||||
"GET /api/v1/companies/:companyId/employees/:id",
|
||||
@@ -60,6 +61,7 @@ exports[`v1 spec snapshot > matches the recorded endpoint key set > endpoint-key
|
||||
"PATCH /api/v1/companies/:companyId/webhooks/:id",
|
||||
"POST /api/v1/companies/:companyId/customers",
|
||||
"POST /api/v1/companies/:companyId/customers/bulk-create",
|
||||
"POST /api/v1/companies/:companyId/dimensions/:id/values",
|
||||
"POST /api/v1/companies/:companyId/documents",
|
||||
"POST /api/v1/companies/:companyId/documents/:id/link",
|
||||
"POST /api/v1/companies/:companyId/employees",
|
||||
|
||||
@@ -132,4 +132,8 @@ import '@/app/api/v1/companies/[companyId]/webhooks/[id]/rotate-secret/route'
|
||||
// Inbox item stamp.
|
||||
import '@/app/api/v1/companies/[companyId]/inbox-items/[id]/stamp/route'
|
||||
|
||||
// Dimensions PR2 — registry list + value creation (kostnadsställe/projekt).
|
||||
import '@/app/api/v1/companies/[companyId]/dimensions/route'
|
||||
import '@/app/api/v1/companies/[companyId]/dimensions/[id]/values/route'
|
||||
|
||||
export {}
|
||||
|
||||
@@ -181,6 +181,12 @@ export const V1_ENDPOINT_SCOPES: Record<string, ApiKeyScope> = {
|
||||
'POST /api/v1/companies/:companyId/salary-runs/:id/book': 'payroll:write',
|
||||
'POST /api/v1/companies/:companyId/salary-runs/:id/generate-agi': 'payroll:write',
|
||||
|
||||
// Dimensions (kostnadsställe/projekt) — dimensions PR2. Reads ride
|
||||
// reports:read (registry data feeds report filters/pickers); value creation
|
||||
// is bookkeeping:write (it mints codes that journal lines reference).
|
||||
'GET /api/v1/companies/:companyId/dimensions': 'reports:read',
|
||||
'POST /api/v1/companies/:companyId/dimensions/:id/values': 'bookkeeping:write',
|
||||
|
||||
// Webhooks (Phase 6 PR-1)
|
||||
'GET /api/v1/companies/:companyId/webhooks': 'webhooks:manage',
|
||||
'POST /api/v1/companies/:companyId/webhooks': 'webhooks:manage',
|
||||
|
||||
@@ -2462,6 +2462,75 @@ const ASSETS: Record<string, StructuredErrorEntry> = {
|
||||
},
|
||||
}
|
||||
|
||||
// Dimensions registry (kostnadsställe/projekt) — dev_docs/dimensions_implementation_plan.md §6
|
||||
const DIMENSION: Record<string, StructuredErrorEntry> = {
|
||||
DIMENSION_NOT_FOUND: {
|
||||
httpStatus: 404,
|
||||
message_sv: 'Dimensionen kunde inte hittas.',
|
||||
message_en: 'Dimension not found.',
|
||||
},
|
||||
DIMENSION_SYSTEM_RENAME: {
|
||||
httpStatus: 400,
|
||||
message_sv: 'Systemdimensioner kan inte döpas om.',
|
||||
message_en: 'System dimensions (kostnadsställe/projekt) cannot be renamed.',
|
||||
},
|
||||
DIMENSION_UPDATE_FAILED: {
|
||||
httpStatus: 500,
|
||||
message_sv: 'Dimensionen kunde inte uppdateras.',
|
||||
message_en: 'Failed to update dimension.',
|
||||
},
|
||||
DIMENSION_VALUE_NOT_FOUND: {
|
||||
httpStatus: 404,
|
||||
message_sv: 'Dimensionsvärdet kunde inte hittas.',
|
||||
message_en: 'Dimension value not found.',
|
||||
},
|
||||
DIMENSION_VALUE_DUPLICATE_CODE: {
|
||||
httpStatus: 409,
|
||||
message_sv: 'Ett värde med samma kod finns redan i dimensionen.',
|
||||
message_en: 'A value with that code already exists in the dimension.',
|
||||
},
|
||||
DIMENSION_VALUE_DATES_NOT_ALLOWED: {
|
||||
httpStatus: 400,
|
||||
message_sv: 'Datum kan bara sättas på ackumulerande dimensioner (t.ex. projekt).',
|
||||
message_en:
|
||||
'Start/end dates can only be set on accumulating dimensions (e.g. projects) — this dimension resets annually.',
|
||||
},
|
||||
DIMENSION_VALUE_CREATE_FAILED: {
|
||||
httpStatus: 500,
|
||||
message_sv: 'Dimensionsvärdet kunde inte skapas.',
|
||||
message_en: 'Failed to create dimension value.',
|
||||
},
|
||||
DIMENSION_VALUE_UPDATE_FAILED: {
|
||||
httpStatus: 500,
|
||||
message_sv: 'Dimensionsvärdet kunde inte uppdateras.',
|
||||
message_en: 'Failed to update dimension value.',
|
||||
},
|
||||
// The DB retention trigger (enforce_dimension_value_retention) raises when a
|
||||
// code is referenced by posted/reversed lines. Routes surface the trigger's
|
||||
// own Swedish message via `messageSv` so the code + kod appear in the toast.
|
||||
DIMENSION_VALUE_REFERENCED: {
|
||||
httpStatus: 409,
|
||||
message_sv:
|
||||
'Värdet används på bokförda verifikat och kan inte tas bort — arkivera det istället.',
|
||||
message_en:
|
||||
'The value is referenced by posted vouchers and cannot be deleted — archive (inactivate) it instead.',
|
||||
remediation: {
|
||||
description:
|
||||
'Archive the value instead: PATCH the dimension value with { "is_active": false }. Codes referenced by posted lines are retained for the BFL 7-year period.',
|
||||
},
|
||||
},
|
||||
DIMENSION_VALUE_DELETE_FAILED: {
|
||||
httpStatus: 500,
|
||||
message_sv: 'Dimensionsvärdet kunde inte tas bort.',
|
||||
message_en: 'Failed to delete dimension value.',
|
||||
},
|
||||
DIMENSION_IMPORT_FAILED: {
|
||||
httpStatus: 500,
|
||||
message_sv: 'Import av befintliga dimensionskoder misslyckades.',
|
||||
message_en: 'Failed to import existing dimension codes from journal lines.',
|
||||
},
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────
|
||||
// Combined registry
|
||||
// ─────────────────────────────────────────────────────────────────
|
||||
@@ -2505,6 +2574,7 @@ const REGISTRY: Record<string, StructuredErrorEntry> = {
|
||||
...SKATTEVERKET,
|
||||
...BOLAGSVERKET,
|
||||
...ASSETS,
|
||||
...DIMENSION,
|
||||
}
|
||||
|
||||
export function getErrorEntry(code: string): StructuredErrorEntry | undefined {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+221
-37
@@ -1,9 +1,12 @@
|
||||
import type { SupabaseClient } from '@supabase/supabase-js'
|
||||
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
||||
import { getBranding } from '@/lib/branding/service'
|
||||
import { createLogger } from '@/lib/logger'
|
||||
import { getOpeningBalances } from './opening-balances'
|
||||
import type { SIEExportOptions, JournalEntry, JournalEntryLine, BASAccount } from '@/types'
|
||||
|
||||
const log = createLogger('reports:sie-export')
|
||||
|
||||
function sanitizeProgramName(str: string): string {
|
||||
return str.replace(/"/g, '').replace(/[\r\n]/g, ' ').substring(0, 60)
|
||||
}
|
||||
@@ -142,19 +145,21 @@ export async function generateSIEExport(
|
||||
entry.lines = linesByEntryId.get(entry.id) || []
|
||||
}
|
||||
|
||||
// Fetch cost centers and projects for dimension records
|
||||
const { data: costCenters } = await supabase
|
||||
.from('cost_centers')
|
||||
.select('*')
|
||||
// Fetch the dimension registry (#DIM/#UNDERDIM + #OBJEKT source).
|
||||
// Deliberately NO is_active filter: lines referencing archived codes still
|
||||
// serialize into #TRANS object lists, and Visma rejects files whose #TRANS
|
||||
// references an undeclared #OBJEKT (plan §5 latent bug #2 — the legacy
|
||||
// cost_centers/projects read filtered is_active=true and dropped them).
|
||||
const { data: registryDimensions } = await supabase
|
||||
.from('dimensions')
|
||||
.select('id, sie_dim_no, parent_sie_dim_no, name')
|
||||
.eq('company_id', companyId)
|
||||
.eq('is_active', true)
|
||||
.order('code')
|
||||
.order('sie_dim_no')
|
||||
|
||||
const { data: projects } = await supabase
|
||||
.from('projects')
|
||||
.select('*')
|
||||
const { data: registryValues } = await supabase
|
||||
.from('dimension_values')
|
||||
.select('dimension_id, code, name')
|
||||
.eq('company_id', companyId)
|
||||
.eq('is_active', true)
|
||||
.order('code')
|
||||
|
||||
const lines: string[] = []
|
||||
@@ -183,25 +188,10 @@ export async function generateSIEExport(
|
||||
lines.push(`#RAR -1 ${dateStringToSIE(prevPeriod.period_start)} ${dateStringToSIE(prevPeriod.period_end)}`)
|
||||
}
|
||||
|
||||
// === Dimension definitions ===
|
||||
// SIE standard: dimension 1 = kostnadsställe, dimension 6 = projekt
|
||||
const hasCostCenters = costCenters && costCenters.length > 0
|
||||
const hasProjects = projects && projects.length > 0
|
||||
|
||||
if (hasCostCenters) {
|
||||
lines.push('#DIM 1 "Kostnadsställe"')
|
||||
}
|
||||
if (hasProjects) {
|
||||
lines.push('#DIM 6 "Projekt"')
|
||||
}
|
||||
|
||||
// === Dimension objects (#OBJEKT) ===
|
||||
for (const cc of costCenters || []) {
|
||||
lines.push(`#OBJEKT 1 "${escapeQuotes(cc.code)}" "${escapeQuotes(cc.name)}"`)
|
||||
}
|
||||
for (const proj of projects || []) {
|
||||
lines.push(`#OBJEKT 6 "${escapeQuotes(proj.code)}" "${escapeQuotes(proj.name)}"`)
|
||||
}
|
||||
// === Dimension definitions (#DIM / #UNDERDIM) + objects (#OBJEKT) ===
|
||||
lines.push(
|
||||
...buildDimensionSection(registryDimensions ?? [], registryValues ?? [], allLines)
|
||||
)
|
||||
|
||||
// === Chart of accounts ===
|
||||
for (const account of (accounts as BASAccount[]) || []) {
|
||||
@@ -257,14 +247,12 @@ export async function generateSIEExport(
|
||||
? ` "${escapeQuotes(line.line_description)}"`
|
||||
: ''
|
||||
|
||||
// Build dimension object list for #TRANS line
|
||||
const dimParts: string[] = []
|
||||
if (line.cost_center) {
|
||||
dimParts.push(`1 "${escapeQuotes(line.cost_center)}"`)
|
||||
}
|
||||
if (line.project) {
|
||||
dimParts.push(`6 "${escapeQuotes(line.project)}"`)
|
||||
}
|
||||
// Build dimension object list for #TRANS from the jsonb map — the
|
||||
// single source of truth. The legacy cost_center/project columns are
|
||||
// derived mirrors of keys '1'/'6' and are no longer read here.
|
||||
const dimParts = lineDimensionEntries(line.dimensions).map(
|
||||
([dimNo, code]) => `${dimNo} "${escapeQuotes(code)}"`
|
||||
)
|
||||
const objList = dimParts.length > 0 ? `{${dimParts.join(' ')}}` : '{}'
|
||||
|
||||
lines.push(`\t#TRANS ${line.account_number} ${objList} ${formatAmount(amount)} ${entryDate}${lineDesc}`)
|
||||
@@ -334,6 +322,202 @@ function escapeQuotes(str: string): string {
|
||||
return str.replace(/"/g, '\\"')
|
||||
}
|
||||
|
||||
// ── Dimensions (#DIM / #UNDERDIM / #OBJEKT) ─────────────────────────────────
|
||||
|
||||
interface RegistryDimension {
|
||||
id: string
|
||||
sie_dim_no: number
|
||||
parent_sie_dim_no: number | null
|
||||
name: string
|
||||
}
|
||||
|
||||
interface RegistryValue {
|
||||
dimension_id: string
|
||||
code: string
|
||||
name: string
|
||||
}
|
||||
|
||||
/**
|
||||
* SIE reserved dimension numbers. Used to synthesize a #DIM declaration for
|
||||
* dimension numbers referenced by exported lines but absent from the registry
|
||||
* — free-text writers can still mint arbitrary numbers until the write-path
|
||||
* PR lands, and an undeclared dimension would make importers reject the file.
|
||||
* Dim 2 (kostnadsbärare) is a reserved sub-dimension of 1 → #UNDERDIM.
|
||||
*/
|
||||
const SIE_RESERVED_DIMENSIONS: Record<number, { name: string; parent?: number }> = {
|
||||
1: { name: 'Kostnadsställe' },
|
||||
2: { name: 'Kostnadsbärare', parent: 1 },
|
||||
6: { name: 'Projekt' },
|
||||
7: { name: 'Anställd' },
|
||||
8: { name: 'Kund' },
|
||||
9: { name: 'Leverantör' },
|
||||
10: { name: 'Faktura' },
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a line's jsonb dimensions map ({"1":"KS01","6":"P001"}) into
|
||||
* [dimNo, code] entries sorted by numeric dimension number. Defensive on
|
||||
* shape: non-numeric keys and blank codes are skipped, and duplicate keys
|
||||
* ('01' vs '1') collapse onto the canonical number with last-write-wins —
|
||||
* mirroring normalizeLineDimensions in lib/bookkeeping/dimension-resolver.ts.
|
||||
*/
|
||||
function lineDimensionEntries(dimensions: unknown): Array<[number, string]> {
|
||||
if (!dimensions || typeof dimensions !== 'object' || Array.isArray(dimensions)) {
|
||||
return []
|
||||
}
|
||||
const byDimNo = new Map<number, string>()
|
||||
for (const [key, value] of Object.entries(dimensions as Record<string, unknown>)) {
|
||||
if (!/^\d+$/.test(key)) continue
|
||||
const dimNo = Number(key)
|
||||
if (dimNo < 1) continue
|
||||
const code = typeof value === 'string' ? value.trim() : ''
|
||||
if (!code) continue
|
||||
byDimNo.set(dimNo, code)
|
||||
}
|
||||
return [...byDimNo.entries()].sort((a, b) => a[0] - b[0])
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the #DIM/#UNDERDIM + #OBJEKT section from the dimension registry and
|
||||
* the exported journal lines.
|
||||
*
|
||||
* Completeness guarantee: every (dimNo, code) pair referenced by any exported
|
||||
* line gets an #OBJEKT record — registry rows contribute their name, orphan
|
||||
* codes (no registry row) synthesize name = code, and dimension numbers with
|
||||
* no registry row synthesize a #DIM from the SIE reserved-number seed. A file
|
||||
* whose #TRANS references an undeclared object is rejected by Visma et al.
|
||||
*
|
||||
* Silence guarantee: registry dimensions with no values and no line
|
||||
* references (e.g. lazily seeded system dims 1/6 that were never used) emit
|
||||
* nothing, so companies that never touch dimensions keep dimension-free files.
|
||||
*/
|
||||
function buildDimensionSection(
|
||||
registryDimensions: RegistryDimension[],
|
||||
registryValues: RegistryValue[],
|
||||
journalLines: JournalEntryLine[]
|
||||
): string[] {
|
||||
// Defence in depth: a value row whose dimension_id doesn't resolve in
|
||||
// dimNoById is skipped by construction (the `continue` below). Both fetches
|
||||
// are scoped to the same company_id (query filter + RLS), so every
|
||||
// dimension_id in registryValues should resolve; a miss can only mean the
|
||||
// dimension row vanished mid-export — skipping just omits its #OBJEKT,
|
||||
// never leaks a foreign company's data into the file.
|
||||
const dimsByNo = new Map<number, RegistryDimension>()
|
||||
const dimNoById = new Map<string, number>()
|
||||
for (const dim of registryDimensions) {
|
||||
dimsByNo.set(dim.sie_dim_no, dim)
|
||||
dimNoById.set(dim.id, dim.sie_dim_no)
|
||||
}
|
||||
|
||||
// Registry values grouped by dimension number: dimNo → (code → name)
|
||||
const valuesByDimNo = new Map<number, Map<string, string>>()
|
||||
for (const value of registryValues) {
|
||||
const dimNo = dimNoById.get(value.dimension_id)
|
||||
if (dimNo === undefined) continue
|
||||
let codeMap = valuesByDimNo.get(dimNo)
|
||||
if (!codeMap) {
|
||||
codeMap = new Map()
|
||||
valuesByDimNo.set(dimNo, codeMap)
|
||||
}
|
||||
if (!codeMap.has(value.code)) codeMap.set(value.code, value.name)
|
||||
}
|
||||
|
||||
// (dimNo, code) pairs referenced by exported lines
|
||||
const referencedByDimNo = new Map<number, Set<string>>()
|
||||
for (const line of journalLines) {
|
||||
for (const [dimNo, code] of lineDimensionEntries(line.dimensions)) {
|
||||
let codes = referencedByDimNo.get(dimNo)
|
||||
if (!codes) {
|
||||
codes = new Set()
|
||||
referencedByDimNo.set(dimNo, codes)
|
||||
}
|
||||
codes.add(code)
|
||||
}
|
||||
}
|
||||
|
||||
const emitDimNos = new Set<number>([
|
||||
...valuesByDimNo.keys(),
|
||||
...referencedByDimNo.keys(),
|
||||
])
|
||||
|
||||
const declarationFor = (dimNo: number): { name: string; parent: number | null } => {
|
||||
const dim = dimsByNo.get(dimNo)
|
||||
if (dim) return { name: dim.name, parent: dim.parent_sie_dim_no ?? null }
|
||||
const reserved = SIE_RESERVED_DIMENSIONS[dimNo]
|
||||
return { name: reserved?.name ?? `Dimension ${dimNo}`, parent: reserved?.parent ?? null }
|
||||
}
|
||||
|
||||
// An #UNDERDIM must not reference an undeclared parent — pull parents into
|
||||
// the emit set transitively (the has-guard also breaks registry cycles).
|
||||
const pending = [...emitDimNos]
|
||||
while (pending.length > 0) {
|
||||
const { parent } = declarationFor(pending.pop()!)
|
||||
if (parent !== null && !emitDimNos.has(parent)) {
|
||||
emitDimNos.add(parent)
|
||||
pending.push(parent)
|
||||
}
|
||||
}
|
||||
|
||||
const sortedDimNos = [...emitDimNos].sort((a, b) => a - b)
|
||||
const out: string[] = []
|
||||
|
||||
// Synthesized placeholders collected for the operator warning below:
|
||||
// #DIM "Dimension n" fallbacks and #OBJEKT rows with name = code.
|
||||
const synthesizedDimNos: number[] = []
|
||||
const orphanObjects: Array<{ dimNo: number; code: string }> = []
|
||||
|
||||
// Two passes: every root #DIM first (sorted by sie_dim_no), then every
|
||||
// #UNDERDIM (sorted by sie_dim_no). SIE4 requires a parent to be declared
|
||||
// before any #UNDERDIM referencing it, and a child may carry a LOWER
|
||||
// number than its parent — so a single numeric sort is not enough.
|
||||
for (const dimNo of sortedDimNos) {
|
||||
const { name, parent } = declarationFor(dimNo)
|
||||
if (parent !== null) continue
|
||||
if (!dimsByNo.has(dimNo) && !SIE_RESERVED_DIMENSIONS[dimNo]) {
|
||||
synthesizedDimNos.push(dimNo)
|
||||
}
|
||||
out.push(`#DIM ${dimNo} "${escapeQuotes(name)}"`)
|
||||
}
|
||||
|
||||
// "Dimension n" fallbacks never carry a parent (declarationFor only
|
||||
// assigns parents from the registry or the reserved seed), so #UNDERDIM
|
||||
// lines are never synthesized placeholders.
|
||||
for (const dimNo of sortedDimNos) {
|
||||
const { name, parent } = declarationFor(dimNo)
|
||||
if (parent === null) continue
|
||||
out.push(`#UNDERDIM ${dimNo} "${escapeQuotes(name)}" ${parent}`)
|
||||
}
|
||||
|
||||
for (const dimNo of sortedDimNos) {
|
||||
const registry = valuesByDimNo.get(dimNo) ?? new Map<string, string>()
|
||||
const codes = new Set<string>([
|
||||
...registry.keys(),
|
||||
...(referencedByDimNo.get(dimNo) ?? []),
|
||||
])
|
||||
for (const code of [...codes].sort((a, b) => a.localeCompare(b, 'sv'))) {
|
||||
const registryName = registry.get(code)
|
||||
if (registryName === undefined) {
|
||||
orphanObjects.push({ dimNo, code })
|
||||
}
|
||||
out.push(
|
||||
`#OBJEKT ${dimNo} "${escapeQuotes(code)}" "${escapeQuotes(registryName ?? code)}"`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Operators must know the file contains synthesized placeholder names
|
||||
// (BFNAR 2013:2 behandlingshistorik) — one structured warning listing the
|
||||
// pairs; silent when the registry covered everything.
|
||||
if (orphanObjects.length > 0 || synthesizedDimNos.length > 0) {
|
||||
log.warn('SIE export synthesized placeholder dimension declarations', {
|
||||
orphanObjects,
|
||||
synthesizedDimNos,
|
||||
})
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate net balances per account from journal entries
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user