fix: allow retroactive first fiscal year via SIE import (#265)
The enforce_period_start_day trigger and its sie-import pre-validation rejected any non-first-of-month period_start whenever *any other* fiscal period existed for the company. That blocked a real user flow: after onboarding creates a default period (e.g. current year, day 1), importing an SIE for an older förlängt första räkenskapsår (e.g. 2017-07-28 – 2018-12-31) failed with "Non-first fiscal period must start on the 1st of a month". Per BFL 3 kap., the chronologically first fiscal year is the one that may be 6–18 months and start mid-month — which is a property of *when* the period starts relative to others, not of insert order. The trigger and pre-validation now allow mid-month start iff no existing period starts earlier. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
c8a5f044c3
commit
8ed943198f
@@ -0,0 +1,38 @@
|
||||
-- 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';
|
||||
Reference in New Issue
Block a user