Files
accounted/supabase/migrations/20260513140000_backfill_bas_2026_account_labels.sql
Mattsson 9e0b89ee7e Bug/baskontoplan numbers (#463)
* feat(settings): add option for company name position in invoice PDF

* feat(migrations): add backfill for VAT account labels to correct bad seed data

* feat(migrations): add backfill for VAT account labels to correct bad seed data

* fix(ui): improve accessibility for company name position toggle in PDF settings

* fix(bookkeeping): align BAS 2026 reference data with official PDF

Reconciled lib/bookkeeping/bas-data/ against the BAS 2026 v1.1 official
chart (1286 accounts). All real discrepancies fixed:

- 2089 Fond för utvecklingsutgifter: k2_excluded → true
- 8417 Räntekostnader för dold räntekompensation: k2_excluded → true
- 1250, 1260 renamed to "(Fritt konto för Inventarier, verktyg och
  installationer)" — BAS 2026 freed these slots
- Periodiseringsfond 2120-2139: added year suffixes (2120 = "...2020"
  etc.) and added 8 missing accounts (2121-2127, 2129) for years
  2019, 2021-2027. Dropped phantom 2022/2024 prior-parser garbage.
- 4075-4078: EUland → EU-land
- 8411: förlagsoch → förlags- och

Verified: 1282 of 1286 PDF accounts match exactly after edits (remaining
4 are PDF-parser artifacts, not real data). Build clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(migrations): backfill BAS 2026 account labels in chart_of_accounts

Companion to the TS reference fix. Updates existing companies' rows where
they still carry seed-data typos or generic names that don't match BAS 2026:

- 4075-4078: EUland → EU-land (hyphenation)
- 8411: förlagsoch → förlags- och (hyphenation)
- 2120, 2130-2137, 2139: rename "Periodiseringsfond" (generic, no year) to
  the BAS 2026 canonical name with year suffix

Defensive: every WHERE clause matches an EXACT current value. Rows that
have been manually renamed by users — including those with a wrong year
that may reference legacy fonds from an earlier BAS numbering cycle — are
left untouched. No row is deleted.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(migrations): fix wrong-year labels on Periodiseringsfond accounts

Follow-up to 20260513140000. The first backfill only renamed accounts whose
name was the generic "Periodiseringsfond" (no year). Many customers were
seeded from an older BAS numbering cycle where 2126 = "2016", 2127 = "2017",
etc. — BAS 2026 reuses those account numbers for years 2026/2027.

This migration aligns the year tag with the BAS 2026 meaning of each
account number across 2120-2127, 2129, 2130-2137, 2139. Only rows whose
name still starts with "Periodiseringsfond" are touched — customers who
renamed the account to something custom keep their name.

Verified on staging: all 18 accounts now carry BAS 2026 canonical names.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(data): update account names and descriptions for clarity and consistency

* fix(migrations): backfill account names for BAS 2026 freed accounts and refine Periodiseringsfond name matching

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 11:54:06 +02:00

75 lines
3.2 KiB
PL/PgSQL
Raw Permalink 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';