fix(bookkeeping): a following year's own IB no longer blocks nollställ, and a re-dated räkenskapsår gets the right name (#2286)

Customer report (Aisen & Adison AB, 2026-09-03): Fortnox years 2024-2026
imported first, then the first year 2022/2023 backfilled. Two bugs surfaced.

1. The backfilled year was saved as "Räkenskapsår 2027": CreatePeriodDialog
   seeds the next forward year and kept that name when the user re-dated the
   form. The name now follows the typed dates until the user edits the name
   (fiscalYearName exported from suggest-fiscal-period).

2. Nollställ of the backfilled year was refused with next_year_dependency
   because 2024 carried an opening-balance verifikat. Any IB in the next year
   counted as reliance, so a backfilled year could never be reset, while a
   next year WITHOUT an IB (whose balansrapport really rolls from this year)
   was allowed. Migration 20260904163000 redefines fiscal_year_reset_snapshot:
   the block fires only when the next year is locked, closed or has its own
   closing entry; a bokslut-generated IB is still refused via this year's
   closing_entry_id (year_end_state). The snapshot returns next_period
   {id, name, has_opening_balances} and the dialog states that the following
   year's IB stays as it is.

pg-real: reset-fiscal-year.pg.test.ts pins the narrowed guard (closed next
year, next year with closing entry, next year with its own IB survives the
reset untouched).

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-09-04 18:49:23 +02:00
committed by GitHub
co-authored by Jakob Wennberg Claude Fable 5.1
parent db289e3bdc
commit 9618bab273
12 changed files with 434 additions and 12 deletions
+93 -5
View File
@@ -1,5 +1,6 @@
/**
* pg-real tests for the fiscal-year reset RPCs (migration 20260825150000).
* pg-real tests for the fiscal-year reset RPCs (migration 20260825150000,
* next-year dependency narrowed in 20260904163000).
*
* Pins: the actor gate (service-role p_user_id, owner/admin only), every
* eligibility guard (locked, closed, company lock date, year-end state,
@@ -29,6 +30,7 @@ type RpcResult = {
eligible?: boolean
blockers?: Array<{ code: string; count?: number; date?: string }>
counts?: { vouchers: number; documents_to_detach: number }
next_period?: { id: string; name: string; has_opening_balances: boolean } | null
deleted?: number
detached_documents?: number
period_name?: string
@@ -212,7 +214,7 @@ describe('reset_fiscal_year: eligibility guards', () => {
)
})
it('refuses when a later year depends on this year (IB generated)', async () => {
it('refuses when a later year is finalised on top of this one (closed)', async () => {
const { companyId, userId, fiscalPeriodId } = await seedCompany()
const nextId = await insertFiscalPeriod({
userId,
@@ -220,11 +222,10 @@ describe('reset_fiscal_year: eligibility guards', () => {
name: '2027',
periodStart: '2027-01-01',
periodEnd: '2027-12-31',
isClosed: true,
})
await getPool().query(
`UPDATE public.fiscal_periods
SET previous_period_id = $1, opening_balances_set = true
WHERE id = $2`,
`UPDATE public.fiscal_periods SET previous_period_id = $1 WHERE id = $2`,
[fiscalPeriodId, nextId],
)
@@ -235,6 +236,86 @@ describe('reset_fiscal_year: eligibility guards', () => {
)
})
it('refuses when a later year has its own closing entry', async () => {
const { companyId, userId, fiscalPeriodId } = await seedCompany()
const nextId = await insertFiscalPeriod({
userId,
companyId,
name: '2027',
periodStart: '2027-01-01',
periodEnd: '2027-12-31',
})
const nextClosingId = await insertPostedEntry({
companyId,
userId,
fiscalPeriodId: nextId,
sourceType: 'year_end',
entryDate: '2027-12-31',
})
await getPool().query(
`UPDATE public.fiscal_periods
SET previous_period_id = $1, closing_entry_id = $2
WHERE id = $3`,
[fiscalPeriodId, nextClosingId, nextId],
)
const result = await callReset(companyId, fiscalPeriodId, '2026', userId)
expect(result).toMatchObject({ ok: false, code: 'FISCAL_YEAR_RESET_INELIGIBLE' })
expect(result.blockers).toEqual(
expect.arrayContaining([expect.objectContaining({ code: 'next_year_dependency' })]),
)
})
it('does not treat the later year\'s own opening balances as a dependency', async () => {
// The backfill shape: the first imported year carries its own IB (from
// the SIE file's #IB, or resynced from the backfilled year's #UB). Its
// IB is a verifikat of its own and survives the reset untouched; the
// preview discloses it instead of refusing.
const { companyId, userId, fiscalPeriodId } = await seedCompany()
await insertPostedEntry({ companyId, userId, fiscalPeriodId, sourceType: 'import' })
const nextId = await insertFiscalPeriod({
userId,
companyId,
name: '2027',
periodStart: '2027-01-01',
periodEnd: '2027-12-31',
})
const nextIbId = await insertPostedEntry({
companyId,
userId,
fiscalPeriodId: nextId,
sourceType: 'opening_balance',
entryDate: '2027-01-01',
})
await getPool().query(
`UPDATE public.fiscal_periods
SET previous_period_id = $1,
opening_balance_entry_id = $2,
opening_balances_set = true
WHERE id = $3`,
[fiscalPeriodId, nextIbId, nextId],
)
const eligibility = await callEligibility(companyId, fiscalPeriodId, userId)
expect(eligibility).toMatchObject({
ok: true,
eligible: true,
blockers: [],
next_period: { id: nextId, name: '2027', has_opening_balances: true },
})
const result = await callReset(companyId, fiscalPeriodId, '2026', userId)
expect(result.ok).toBe(true)
expect(result.deleted).toBe(1)
expect(await entryCount(companyId, nextId)).toBe(1)
const { rows } = await getPool().query<{ opening_balance_entry_id: string; opening_balances_set: boolean }>(
`SELECT opening_balance_entry_id, opening_balances_set FROM public.fiscal_periods WHERE id = $1`,
[nextId],
)
expect(rows[0]).toEqual({ opening_balance_entry_id: nextIbId, opening_balances_set: true })
})
it('allows a later year that carries no dependency yet', async () => {
const { companyId, userId, fiscalPeriodId } = await seedCompany()
await insertPostedEntry({ companyId, userId, fiscalPeriodId })
@@ -250,6 +331,13 @@ describe('reset_fiscal_year: eligibility guards', () => {
[fiscalPeriodId, nextId],
)
const eligibility = await callEligibility(companyId, fiscalPeriodId, userId)
expect(eligibility).toMatchObject({
ok: true,
eligible: true,
next_period: { id: nextId, name: '2027', has_opening_balances: false },
})
const result = await callReset(companyId, fiscalPeriodId, '2026', userId)
expect(result.ok).toBe(true)
expect(result.deleted).toBe(1)