Files
accounted/supabase/migrations/20260717150000_fskatt_deadline_amount_gate_and_dismiss.sql
Jakob Wennberg 3c0bf3f584 feat(deadlines): gate F-skatt reminders on debited preliminary tax + durable dismissal (#1057)
* feat(deadlines): gate F-skatt reminders on debited preliminary tax, add durable dismissal

The f_skatt deadline was gated on the F-skatt approval flag (DB default
true), giving nearly every company 12 monthly payment reminders for a tax
Skatteverket may not have debited at all (64% of all system deadline rows,
one lifetime completion). Approval carries no recurring obligation; the
monthly duty is payment of debiterad preliminarskatt and exists only while
the debited amount is > 0 (SFL 62 kap. 4-5 par., 55 kap. 2 par.).

- Gate the f_skatt deadline on preliminary_tax_monthly > 0 (field already
  collected at onboarding, previously unread) and retitle it as a payment.
- Storforetag keep the 12th in August (January-only 17th, 62 kap. 3 par.).
- Declare the prod-only preliminary_tax_monthly column in a migration so
  installs built purely from migrations stop failing tax-settings saves.
- Add deadlines.dismissed_at: DELETE on a system deadline now soft-dismisses
  it durably (hard deletes were resurrected by the nightly backfill within
  24h); generator, backfill, and every read surface respect it.
- Prune upcoming f_skatt rows for companies with no debited amount.

Closes part of #1028.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(deadlines): include dismissed_at in DeadlineForm payload

The Deadline type gained the required dismissed_at field; the form's
submit payload literal must carry it for the Omit<Deadline, ...> shape.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(deadlines): make system-deadline dismissal atomic

Constrain the dismiss update to source='system' and verify a row was
actually updated: a concurrent regeneration can delete the row between
lookup and update, and the route must not report a phantom success.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-17 15:48:25 +02:00

42 lines
1.9 KiB
SQL

-- F-skatt deadline re-gating (issue #1028) + durable deadline dismissal.
--
-- Approval for F-skatt carries no recurring obligation: the recurring duty is
-- PAYMENT of debiterad preliminarskatt, and only when Skatteverket has debited
-- an amount > 0 kr (below 2 400 kr nothing is debited at all, SFL 55 kap. 2 §).
-- The deadline generator previously gated on the f_skatt approval flag, whose
-- column default is true, so nearly every company received 12 payment
-- reminders per year for a tax that may not be debited. The generator now
-- gates on preliminary_tax_monthly > 0 instead.
-- 1) Schema alignment: preliminary_tax_monthly has existed on the hosted
-- database since before migration discipline, but no migration ever
-- declared it. Installs built purely from migrations (self-hosted, local,
-- preview branches) lack the column and fail every tax-settings save that
-- includes the field. No-op on hosted.
ALTER TABLE public.company_settings
ADD COLUMN IF NOT EXISTS preliminary_tax_monthly numeric;
-- 2) Durable dismissal for system-generated deadlines. Hard-deleting a system
-- row never worked: the nightly backfill cron treats the missing row as a
-- repair case and recreates it within 24 hours. A dismissed row stays in
-- the table, is hidden from every surface, and satisfies the generator and
-- backfill the same way a completed row does.
ALTER TABLE public.deadlines
ADD COLUMN IF NOT EXISTS dismissed_at timestamptz;
-- 3) Prune upcoming F-skatt payment reminders for companies with no debited
-- preliminary tax on record. Completed rows are kept (filing history).
DELETE FROM public.deadlines d
WHERE d.source = 'system'
AND d.tax_deadline_type = 'f_skatt'
AND d.is_completed = false
AND d.due_date >= current_date
AND NOT EXISTS (
SELECT 1
FROM public.company_settings cs
WHERE cs.company_id = d.company_id
AND coalesce(cs.preliminary_tax_monthly, 0) > 0
);
NOTIFY pgrst, 'reload schema';