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,97 @@
-- Backfill: retire the pre-2026 labels on 1249/1259/1269 where the company's
-- head account already carries the BAS 2026 free-account label (#2413).
--
-- BAS 2026 restructured kontogrupp 12: 1210 (maskiner, för produktion) and
-- 1220 (inventarier, ej för produktion) got the bilar/datorer sub-accounts,
-- while 1230/1240 became "(Fritt konto för Maskiner och andra tekniska
-- anläggningar)" and 1250/1260 "(Fritt konto för Inventarier, verktyg och
-- installationer)". The catalog in lib/bookkeeping/bas-data/ followed for the
-- heads but kept the retired contra accounts 1249/1259/1269 with their old
-- names, so a company that activated 1240 + 1249 from the picker got a free
-- machinery account whose only contra account said "bilar". The catalog now
-- names those contra accounts after their heads; this backfill does the same
-- for charts that already carry the contradictory pair.
--
-- Safety: a row is renamed only when BOTH hold: its name is byte-identical to
-- one of the two catalog literals it could have been seeded with, AND the
-- company's head account (1240/1250/1260) carries the BAS 2026 free label.
-- A chart imported from an older BAS (1240 "Bilar och andra transportmedel")
-- is internally consistent and is left alone, as is every user rename.
--
-- Third guard: the contra account has no journal lines. A SIE import whose
-- #KONTO names were not carried creates BOTH rows from the catalog
-- (lib/import/account-sync.ts create pass), so an old-BAS vehicle chart can
-- hold exactly the pair above with years of "Avskrivningar bil" postings on
-- 1249. A label with history is the user's to change; this backfill only
-- corrects the label the picker handed out before anything was booked on it.
-- No row is deleted; bookings key on account_number, never on the label.
--
-- The migration runs in one transaction (Supabase CLI wraps it). The SHARE
-- lock below makes the "no journal lines" check and the rename atomic against
-- concurrent postings: inserts on journal_entry_lines wait the few
-- milliseconds this takes, reads are unaffected.
LOCK TABLE public.journal_entry_lines IN SHARE MODE;
UPDATE public.chart_of_accounts a
SET account_name = 'Ackumulerade avskrivningar (fritt konto för Maskiner och andra tekniska anläggningar)',
updated_at = now()
WHERE a.account_number = '1249'
AND a.account_name IN (
'Ack. avskrivningar på bilar och andra transportmedel',
'Ackumulerade avskrivningar på bilar och andra transportmedel'
)
AND EXISTS (
SELECT 1 FROM public.chart_of_accounts h
WHERE h.company_id = a.company_id
AND h.account_number = '1240'
AND h.account_name = '(Fritt konto för Maskiner och andra tekniska anläggningar)'
)
AND NOT EXISTS (
SELECT 1 FROM public.journal_entry_lines l
JOIN public.journal_entries e ON e.id = l.journal_entry_id
WHERE e.company_id = a.company_id
AND l.account_number = a.account_number
);
UPDATE public.chart_of_accounts a
SET account_name = 'Ackumulerade avskrivningar (fritt konto för Inventarier, verktyg och installationer)',
updated_at = now()
WHERE a.account_number = '1259'
AND a.account_name IN (
'Ack. avskrivningar på inventarier och verktyg',
'Ackumulerade avskrivningar på inventarier och verktyg'
)
AND EXISTS (
SELECT 1 FROM public.chart_of_accounts h
WHERE h.company_id = a.company_id
AND h.account_number = '1250'
AND h.account_name = '(Fritt konto för Inventarier, verktyg och installationer)'
)
AND NOT EXISTS (
SELECT 1 FROM public.journal_entry_lines l
JOIN public.journal_entries e ON e.id = l.journal_entry_id
WHERE e.company_id = a.company_id
AND l.account_number = a.account_number
);
UPDATE public.chart_of_accounts a
SET account_name = 'Ackumulerade avskrivningar (fritt konto för Inventarier, verktyg och installationer)',
updated_at = now()
WHERE a.account_number = '1269'
AND a.account_name IN (
'Ack. avskrivningar på datorer',
'Ackumulerade avskrivningar på datorer'
)
AND EXISTS (
SELECT 1 FROM public.chart_of_accounts h
WHERE h.company_id = a.company_id
AND h.account_number = '1260'
AND h.account_name = '(Fritt konto för Inventarier, verktyg och installationer)'
)
AND NOT EXISTS (
SELECT 1 FROM public.journal_entry_lines l
JOIN public.journal_entries e ON e.id = l.journal_entry_id
WHERE e.company_id = a.company_id
AND l.account_number = a.account_number
);