Files
accounted/supabase/migrations/20260723003500_backfill_final_closing_entry_links.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

26 lines
889 B
SQL

-- Repair only unambiguous final-closing links created by the canonical
-- year-end workflow. Other legacy closed periods remain unlinked so statutory
-- reporting fails explicitly instead of excluding unrelated year-end entries.
WITH exact_candidates AS (
SELECT
fp.id AS fiscal_period_id,
(array_agg(je.id))[1] AS closing_entry_id
FROM public.fiscal_periods fp
JOIN public.journal_entries je
ON je.company_id = fp.company_id
AND je.fiscal_period_id = fp.id
AND je.source_type = 'year_end'
AND je.status = 'posted'
AND je.description = 'Årsbokslut ' || fp.name
WHERE fp.is_closed = true
AND fp.closing_entry_id IS NULL
GROUP BY fp.id
HAVING count(*) = 1
)
UPDATE public.fiscal_periods fp
SET closing_entry_id = candidate.closing_entry_id
FROM exact_candidates candidate
WHERE fp.id = candidate.fiscal_period_id
AND fp.closing_entry_id IS NULL;