Files
accounted/supabase/migrations/20260813165814_peppol_delivery_retention_basis.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

47 lines
1.5 KiB
PL/PgSQL

-- A Peppol archive record must inherit the legally corrected BFL retention
-- date from the fiscal period containing the invoice. Refuse staging when the
-- period is missing instead of guessing from the invoice date.
CREATE OR REPLACE FUNCTION public.enforce_peppol_delivery_retention_basis()
RETURNS trigger
LANGUAGE plpgsql
SECURITY INVOKER
SET search_path = pg_catalog, public
AS $$
DECLARE
expected_retention date;
BEGIN
SELECT period.retention_expires_at
INTO expected_retention
FROM public.invoices AS invoice
JOIN public.fiscal_periods AS period
ON period.company_id = invoice.company_id
AND invoice.invoice_date BETWEEN period.period_start AND period.period_end
WHERE invoice.id = NEW.invoice_id
AND invoice.company_id = NEW.company_id
ORDER BY period.period_end DESC
LIMIT 1;
IF expected_retention IS NULL THEN
RAISE EXCEPTION 'Peppol delivery requires a fiscal period retention basis'
USING ERRCODE = 'P0002';
END IF;
IF NEW.retention_expires_at IS DISTINCT FROM expected_retention THEN
RAISE EXCEPTION 'Peppol retention date must match the invoice fiscal period'
USING ERRCODE = '23514';
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER enforce_peppol_delivery_retention_basis
BEFORE INSERT ON public.peppol_deliveries
FOR EACH ROW EXECUTE FUNCTION public.enforce_peppol_delivery_retention_basis();
REVOKE ALL ON FUNCTION public.enforce_peppol_delivery_retention_basis()
FROM PUBLIC, anon, authenticated;
NOTIFY pgrst, 'reload schema';