fix(bookkeeping): name 1249/1259/1269 after their BAS 2026 free heads, drop retired 12xx sub-accounts (#2419)

* fix(bookkeeping): name 1249/1259/1269 after their BAS 2026 free heads, drop retired 12xx sub-accounts

A sole trader booking a tractor activated 1240 and 1249 from the account
picker and got a machinery head labelled "(Fritt konto för Maskiner och
andra tekniska anläggningar)" next to a contra account labelled
"Ack. avskrivningar på bilar och andra transportmedel".

Why it occurred: BAS 2026 restructured kontogrupp 12. Bilar and datorer
moved under 1210 (för produktion) and 1220 (ej för produktion), and
1230/1240/1250/1260 became free heads. The catalog in
lib/bookkeeping/bas-data/ followed for the heads (#463) but kept seven
sub-accounts the official chart no longer has (1241, 1242, 1249, 1251,
1259, 1261, 1269) with their pre-2026 names. Every picker activation of a
12xx contra account therefore produced the contradiction; prod carries
the 1240/1249 pair in 173 charts, 1250/1259 in 153 and 1260/1269 in 78.

What was removed instead of patched: 1241, 1242, 1251 and 1261 leave the
catalog entirely (bas.se BAS 2026 v2 has no such accounts; the SIE
mapper already self-maps unknown sub-accounts by number). 1249/1259/1269
stay because the asset module's vehicle and computer defaults and 31 live
assets in prod depend on them; they are renamed after their heads so the
pair reads as one thing.

Why this and not the proposal: the reporter asked for 1249 to be renamed
to "ack. avskr. maskiner", which fixes one number and leaves 1259/1269
and the four retired asset accounts contradicting their heads. Dropping
1249/1259/1269 and moving the asset defaults to BAS 2026 (1226/1224 on
1229) is the right long-term shape but changes what a new vehicle or
computer asset books to; that decision is the founder's and is tracked
in #2414.

The migration renames a contra account only when its name is
byte-identical to one of the two catalog literals AND the company's head
carries the BAS 2026 free label, so old-BAS imports (1240 "Bilar och
andra transportmedel") and every user rename stay untouched. Applied and
pg-tested on staging.

Fixes #2413

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NXSuVejFCvRDyNXF1otEPd

* fix(bookkeeping): skip 12xx contra accounts with journal lines in the label backfill

Skeptic refutation: lib/import/account-sync.ts creates missing accounts
with the catalog name when the SIE #KONTO names are not carried, so an
old-BAS vehicle chart can hold the exact free-head + bilar-contra pair
with years of depreciation booked on 1249 (8 such charts in prod). The
backfill now also requires that the contra account has no journal lines:
a label with history is the user's to change. Migration re-issued under
a fresh version, applied and pg-tested on staging.

Refs #2413

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NXSuVejFCvRDyNXF1otEPd

* fix(bookkeeping): lock journal_entry_lines while the 12xx label backfill checks history

CodeRabbit (Major): under READ COMMITTED a posting could commit between
the NOT EXISTS history check and the rename. A SHARE lock on
journal_entry_lines for the migration transaction makes the two atomic;
inserts wait milliseconds, reads are unaffected. Migration re-issued
under a fresh version, applied and pg-tested on staging.

Refs #2413

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NXSuVejFCvRDyNXF1otEPd

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Mattsson
2026-09-08 13:34:50 +02:00
committed by GitHub
co-authored by Claude Fable 5.1
parent 2303f75a7b
commit 59d5b7b366
6 changed files with 410 additions and 153 deletions
@@ -0,0 +1,171 @@
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import { insertPostedJournalEntry, seedCompany } from '@/tests/pg/fixtures'
import { getPool } from '@/tests/pg/setup'
/**
* pg-real coverage for 20260908113353_backfill_bas2026_retired_12xx_labels.sql
* (#2413).
*
* The backfill renames 1249/1259/1269 only where the chart carries the
* contradictory pair the catalog used to hand out: a BAS 2026 free head
* (1240/1250/1260) next to a contra account still named after the retired
* bilar/inventarier/datorer accounts. Guards under test:
* - the pair is renamed (both catalog literals for the contra name)
* - an old-BAS chart (1240 "Bilar och andra transportmedel") is untouched
* - a user-renamed contra account is untouched
* - a contra account without its head is untouched
* - a contra account with journal lines (old-BAS SIE import re-labelled by
* the catalog on create) is untouched
* - idempotent (re-run is a no-op)
*/
const BACKFILL_SQL = readFileSync(
join(process.cwd(), 'supabase/migrations/20260908113353_backfill_bas2026_retired_12xx_labels.sql'),
'utf8',
)
const FREE_MASKINER = '(Fritt konto för Maskiner och andra tekniska anläggningar)'
const FREE_INVENTARIER = '(Fritt konto för Inventarier, verktyg och installationer)'
const CONTRA_MASKINER =
'Ackumulerade avskrivningar (fritt konto för Maskiner och andra tekniska anläggningar)'
const CONTRA_INVENTARIER =
'Ackumulerade avskrivningar (fritt konto för Inventarier, verktyg och installationer)'
async function runBackfill(): Promise<void> {
await getPool().query(BACKFILL_SQL)
}
async function insertAccounts(
userId: string,
companyId: string,
rows: Array<[number: string, name: string]>,
): Promise<void> {
for (const [number, name] of rows) {
await getPool().query(
`INSERT INTO public.chart_of_accounts
(user_id, company_id, account_number, account_name, account_class, account_group,
account_type, normal_balance, is_active)
VALUES ($1, $2, $3, $4, 1, '12', 'asset', $5, true)`,
[userId, companyId, number, name, number.endsWith('9') ? 'credit' : 'debit'],
)
}
}
async function names(companyId: string): Promise<Record<string, string>> {
const { rows } = await getPool().query<{ account_number: string; account_name: string }>(
`SELECT account_number, account_name FROM public.chart_of_accounts WHERE company_id = $1`,
[companyId],
)
return Object.fromEntries(rows.map((r) => [r.account_number, r.account_name]))
}
describe('backfill_bas2026_retired_12xx_labels', () => {
it('renames the contra account under a BAS 2026 free head, both catalog literals', async () => {
const { userId, companyId } = await seedCompany()
await insertAccounts(userId, companyId, [
['1240', FREE_MASKINER],
['1249', 'Ack. avskrivningar på bilar och andra transportmedel'],
['1250', FREE_INVENTARIER],
['1259', 'Ackumulerade avskrivningar på inventarier och verktyg'],
['1260', FREE_INVENTARIER],
['1269', 'Ack. avskrivningar på datorer'],
])
await runBackfill()
const after = await names(companyId)
expect(after['1249']).toBe(CONTRA_MASKINER)
expect(after['1259']).toBe(CONTRA_INVENTARIER)
expect(after['1269']).toBe(CONTRA_INVENTARIER)
expect(after['1240']).toBe(FREE_MASKINER)
})
it('leaves an old-BAS chart alone: 1240 Bilar next to 1249 Ack. avskr. bilar is consistent', async () => {
const { userId, companyId } = await seedCompany()
await insertAccounts(userId, companyId, [
['1240', 'Bilar och andra transportmedel'],
['1249', 'Ackumulerade avskrivningar på bilar och andra transportmedel'],
['1250', 'Datorer'],
['1259', 'Ackumulerade avskrivningar på datorer'],
])
await runBackfill()
const after = await names(companyId)
expect(after['1249']).toBe('Ackumulerade avskrivningar på bilar och andra transportmedel')
expect(after['1259']).toBe('Ackumulerade avskrivningar på datorer')
})
it('leaves a user rename alone', async () => {
const { userId, companyId } = await seedCompany()
await insertAccounts(userId, companyId, [
['1240', FREE_MASKINER],
['1249', 'Ack. avskrivningar maskiner och andra tekniska anläggningar'],
])
await runBackfill()
expect((await names(companyId))['1249']).toBe(
'Ack. avskrivningar maskiner och andra tekniska anläggningar',
)
})
it('leaves a contra account without its free head alone', async () => {
const { userId, companyId } = await seedCompany()
await insertAccounts(userId, companyId, [
['1249', 'Ack. avskrivningar på bilar och andra transportmedel'],
])
await runBackfill()
expect((await names(companyId))['1249']).toBe(
'Ack. avskrivningar på bilar och andra transportmedel',
)
})
it('leaves a contra account with journal lines alone, even under a free head', async () => {
// lib/import/account-sync.ts creates missing accounts with the catalog
// name when the SIE #KONTO names are not carried, so an old-BAS vehicle
// chart can hold the exact pair with real depreciation booked on 1249.
const { userId, companyId, fiscalPeriodId } = await seedCompany()
await insertAccounts(userId, companyId, [
['1240', FREE_MASKINER],
['1249', 'Ack. avskrivningar på bilar och andra transportmedel'],
['7832', 'Avskrivningar på inventarier'],
])
await insertPostedJournalEntry({
userId,
companyId,
fiscalPeriodId,
sourceType: 'import',
lines: [
{ accountNumber: '7832', debitAmount: 2500, creditAmount: 0 },
{ accountNumber: '1249', debitAmount: 0, creditAmount: 2500 },
],
})
await runBackfill()
expect((await names(companyId))['1249']).toBe(
'Ack. avskrivningar på bilar och andra transportmedel',
)
})
it('is idempotent', async () => {
const { userId, companyId } = await seedCompany()
await insertAccounts(userId, companyId, [
['1260', FREE_INVENTARIER],
['1269', 'Ackumulerade avskrivningar på datorer'],
])
await runBackfill()
const first = await names(companyId)
await runBackfill()
const second = await names(companyId)
expect(first['1269']).toBe(CONTRA_INVENTARIER)
expect(second).toEqual(first)
})
})