Files
accounted/supabase/migrations/20260418120000_allow_retroactive_first_fiscal_year.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

39 lines
1.3 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.
-- Allow retroactive first fiscal year via SIE import.
--
-- The previous trigger (20260416120000) rejected any non-first-of-month
-- period_start when *any other* fiscal period existed for the company.
-- That blocked a common flow: user onboards and gets a default period
-- (e.g. the current year, starting on day 1), then imports an SIE file
-- covering an older, förlängt första räkenskapsår that starts mid-month
-- (e.g. 2017-07-28 – 2018-12-31).
--
-- Per BFL 3 kap., the forst-in-time (chronologically earliest) fiscal year
-- is the one that may be 6–18 months and start mid-month. That property is
-- about *when* the period starts relative to other periods, not about the
-- order rows were inserted. This migration changes the trigger to reflect
-- that: mid-month start is allowed iff no existing period starts earlier.
CREATE OR REPLACE FUNCTION public.enforce_first_of_month_for_subsequent_periods()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF EXTRACT(DAY FROM NEW.period_start) = 1 THEN
RETURN NEW;
END IF;
IF EXISTS (
SELECT 1 FROM public.fiscal_periods
WHERE company_id = NEW.company_id
AND id IS DISTINCT FROM NEW.id
AND period_start < NEW.period_start
) THEN
RAISE EXCEPTION 'Non-first fiscal period must start on the 1st of a month';
END IF;
RETURN NEW;
END;
$$;
NOTIFY pgrst, 'reload schema';