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
@@ -82,6 +82,38 @@ describe('Non-standard accounts removed', () => {
}
})
// BAS 2026 restructured kontogrupp 12: bilar/datorer moved under 1210 (för
// produktion) and 1220 (ej för produktion), and 1230/1240/1250/1260 became
// free heads. The pre-2026 sub-accounts under those heads are gone from the
// official chart (bas.se BAS 2026 v2), so the catalog must not hand them out
// with their old names next to a head that says "fritt konto" (#2413).
describe('BAS 2026 kontogrupp 12: free heads and their contra accounts agree', () => {
const retired = ['1241', '1242', '1251', '1261']
for (const num of retired) {
it(`${num} (retired in BAS 2026) is not in the catalog`, () => {
expect(isStandardBASAccount(num)).toBe(false)
})
}
const pairs: Array<[head: string, contra: string]> = [
['1240', '1249'],
['1250', '1259'],
['1260', '1269'],
]
for (const [head, contra] of pairs) {
it(`${contra} is named after the free head ${head}, not a retired bilar/datorer account`, () => {
const headName = getBASReference(head)?.account_name ?? ''
const contraName = getBASReference(contra)?.account_name ?? ''
expect(headName.startsWith('(Fritt konto för ')).toBe(true)
// "(Fritt konto för X)" on the head; "... (fritt konto för X)" on the contra.
const subject = headName.slice('(Fritt konto för '.length, -1)
expect(contraName).toContain(`(fritt konto för ${subject})`)
expect(contraName).toMatch(/^Ackumulerade avskrivningar/)
expect(contraName.toLowerCase()).not.toMatch(/bilar|datorer|inventarier och verktyg$/)
})
}
})
describe('Class 2 account_type correctness', () => {
it('20xx accounts are equity', () => {
const group20 = BAS_REFERENCE.filter((a) => a.account_group === '20')