Files
accounted/supabase/migrations/20260513140000_backfill_bas_2026_account_labels.sql
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

75 lines
3.2 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Backfill: align chart_of_accounts labels with BAS 2026 official chart.
--
-- Companion to the TS reference fix in lib/bookkeeping/bas-data/. The
-- TS file is the source of truth for the kontoplan UI and lookups, but
-- existing companies' chart_of_accounts rows were seeded with older /
-- typo'd labels that this migration cleans up.
--
-- Safety: every WHERE clause matches an EXACT bad name string. Companies
-- that have manually renamed an account are left alone. No row is deleted;
-- this migration only updates account_name.
BEGIN;
-- 1. Hyphenation fix: 4075-4078 "EUland" -> "EU-land"
UPDATE public.chart_of_accounts
SET account_name = REPLACE(account_name, 'EUland', 'EU-land'),
updated_at = now()
WHERE account_number IN ('4075', '4076', '4077', '4078')
AND account_name LIKE '%EUland%';
-- 2. Hyphenation fix: 8411 "förlagsoch" -> "förlags- och"
UPDATE public.chart_of_accounts
SET account_name = REPLACE(account_name, 'förlagsoch', 'förlags- och'),
updated_at = now()
WHERE account_number = '8411'
AND account_name LIKE '%förlagsoch%';
-- 3. BAS 2026 freed accounts 1250/1260; rename to the free-account label.
-- Exact-match guard on the old seeded name leaves any customer rename alone.
UPDATE public.chart_of_accounts
SET account_name = '(Fritt konto för Inventarier, verktyg och installationer)',
updated_at = now()
WHERE account_number = '1250'
AND account_name = 'Inventarier och verktyg';
UPDATE public.chart_of_accounts
SET account_name = '(Fritt konto för Inventarier, verktyg och installationer)',
updated_at = now()
WHERE account_number = '1260'
AND account_name = 'Datorer';
-- 4. Periodiseringsfond name backfill.
-- BAS 2026 uses deterministic year mapping for accounts 2120-2139:
-- 2120 -> 2020, 2121 -> 2021, 2122 -> 2022, 2123 -> 2023,
-- 2124 -> 2024, 2125 -> 2025, 2126 -> 2026, 2127 -> 2027,
-- 2129 -> 2019,
-- 213x -> same year as 212x with "– nr 2" suffix.
--
-- We only rename rows where the current name is the generic
-- "Periodiseringsfond" (no year suffix at all). Rows that already
-- carry a year — even a wrong one — are LEFT ALONE because they may
-- refer to a legacy fond from a previous BAS numbering cycle that the
-- user is intentionally tracking. The kontoplan UI lets users rename
-- those manually.
UPDATE public.chart_of_accounts
SET account_name = CASE account_number
WHEN '2120' THEN 'Periodiseringsfond 2020'
WHEN '2130' THEN 'Periodiseringsfond 2020 – nr 2'
WHEN '2131' THEN 'Periodiseringsfond 2021 – nr 2'
WHEN '2132' THEN 'Periodiseringsfond 2022 – nr 2'
WHEN '2133' THEN 'Periodiseringsfond 2023 – nr 2'
WHEN '2134' THEN 'Periodiseringsfond 2024 – nr 2'
WHEN '2135' THEN 'Periodiseringsfond 2025 – nr 2'
WHEN '2136' THEN 'Periodiseringsfond 2026 – nr 2'
WHEN '2137' THEN 'Periodiseringsfond 2027 – nr 2'
WHEN '2139' THEN 'Periodiseringsfond 2019 – nr 2'
END,
updated_at = now()
WHERE account_number IN ('2120','2130','2131','2132','2133','2134','2135','2136','2137','2139')
AND account_name = 'Periodiseringsfond';
COMMIT;
NOTIFY pgrst, 'reload schema';