fix(accruals): carry origin dimensions onto dissolution entries (#1419)

* fix(accruals): carry origin dimensions onto dissolution entries

A project-tagged deferred invoice line lost its dimensions bag on every
monthly dissolution: the schedule never stored the bag, so dissolution
lines booked untagged and the per-project P&L drifted from the origin.

Persist the merged bag (invoice default_dimensions with the item bag on
top, same merge the origin generators use) on accrual_schedules and
attach it to BOTH dissolution lines: both origin generators tag the
interim 17xx/29xx line too, so per-dimension views of the interim
account keep netting to zero. Pre-existing schedules stay at '{}' and
keep today's untagged behavior; backfill is a separate follow-up.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(accruals): let dissolutions post when the tagged dimension value is archived

Carrying the origin's dimensions bag onto dissolution lines pulled every
monthly periodisering into validateEntryDimensions. With dimensions_enabled,
a project archived after the origin invoice was booked made the validator
reject the dissolution: the accrual service caught the rejection, wrote
last_error and left the installment pending forever. The remaining months of
prepaid cost would never reach 5xxx/6xxx, the interim 17xx/29xx account would
stay overstated, and the trial balance still balances, so no year-end check
fires and the arsredovisning is filed with understated cost.

Add a second, narrow exempt set (DIMENSION_VALIDATION_EXEMPT_SOURCE_TYPES =
{'accrual'}) with an isDimensionValidationExemptSource() helper, and skip the
soft registry validation for it in createDraftEntry. A dissolution is the
mechanical continuation of an already-approved, already-posted decision: the
same category as a storno, which the engine already bypasses.

The tag is kept, never stripped: an archived value still exists in the
registry and the cost genuinely belongs to that project. The rules set
(required/default/fixed) stays untouched, and the other two
validateEntryDimensions call sites keep validating: updateDraftEntry is a
user edit of an editable draft, and replaceOpeningBalanceEntry rejects any
source_type other than opening_balance.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-08-06 10:04:20 +02:00
committed by GitHub
co-authored by Claude Fable 5 Jakob Wennberg
parent d7320a6c87
commit 31fba5e8f9
11 changed files with 707 additions and 33 deletions
@@ -125,7 +125,10 @@ const DIMENSION_TABLES: Record<string, TableResult> = {
},
}
function makeInput(dimensions?: Record<string, string>): CreateJournalEntryInput {
function makeInput(
dimensions?: Record<string, string>,
overrides: Partial<CreateJournalEntryInput> = {}
): CreateJournalEntryInput {
return {
fiscal_period_id: 'period-1',
entry_date: '2026-06-15',
@@ -138,6 +141,7 @@ function makeInput(dimensions?: Record<string, string>): CreateJournalEntryInput
{ account_number: '4010', debit_amount: 100, credit_amount: 0, dimensions },
{ account_number: '1930', debit_amount: 0, credit_amount: 100, dimensions },
],
...overrides,
}
}
@@ -231,6 +235,88 @@ describe('createDraftEntry: dimension validation wiring', () => {
})
})
/**
* Accrual dissolutions (source_type 'accrual') are exempt from the soft
* registry validation: they replay the origin entry's dimensions bag month
* after month, so a value archived (or removed from the registry) after the
* origin was posted must not be able to stop the remaining installments from
* booking. Exemption is by source type only, and it does NOT strip the tag.
*/
describe('createDraftEntry: accrual dissolution exemption', () => {
const accrual = { source_type: 'accrual' as const, source_id: 'sched-1' }
it('posts a dissolution tagged with a value archived since the origin entry', async () => {
const { supabase, inserts, queriedTables } = buildSupabase({
...BASE_TABLES,
...DIMENSION_TABLES,
dimension_values: {
data: [{ dimension_id: 'dim-proj', code: 'P001', is_active: false }],
},
})
const entry = await createDraftEntry(
supabase as never,
'company-1',
'user-1',
makeInput({ '6': 'P001' }, accrual)
)
expect(entry.id).toBe('entry-1')
// The tag survives: an archived value still exists in the registry and the
// cost genuinely belongs to that project.
const lineRows = inserts.journal_entry_lines[0] as Array<Record<string, unknown>>
expect(lineRows[0].dimensions).toEqual({ '6': 'P001' })
expect(lineRows[1].dimensions).toEqual({ '6': 'P001' })
// Registry never consulted at all for an exempt source.
expect(queriedTables()).not.toContain('dimensions')
expect(queriedTables()).not.toContain('dimension_values')
})
it('posts a dissolution tagged with a code that is not in the registry', async () => {
const { supabase, inserts } = buildSupabase({
...BASE_TABLES,
...DIMENSION_TABLES,
dimension_values: { data: [] },
})
const entry = await createDraftEntry(
supabase as never,
'company-1',
'user-1',
makeInput({ '6': 'P999' }, accrual)
)
expect(entry.id).toBe('entry-1')
const lineRows = inserts.journal_entry_lines[0] as Array<Record<string, unknown>>
expect(lineRows[0].dimensions).toEqual({ '6': 'P999' })
})
it('still rejects the same archived value on an operational source', async () => {
const { supabase } = buildSupabase({
...BASE_TABLES,
...DIMENSION_TABLES,
dimension_values: {
data: [{ dimension_id: 'dim-proj', code: 'P001', is_active: false }],
},
})
// The exemption is narrow: only 'accrual' skips validation, every
// operational source keeps its typed Swedish rejection.
await expect(
createDraftEntry(supabase as never, 'company-1', 'user-1', makeInput({ '6': 'P001' }))
).rejects.toBeInstanceOf(DimensionValidationError)
await expect(
createDraftEntry(
supabase as never,
'company-1',
'user-1',
makeInput({ '6': 'P001' }, { source_type: 'supplier_invoice_registered' })
)
).rejects.toBeInstanceOf(DimensionValidationError)
})
})
describe('updateDraftEntry: dimension validation wiring', () => {
it('rejects an unknown code before the header or lines are touched', async () => {
const { supabase, updates, queriedTables } = buildSupabase({