* fix(bookkeeping): harden correction account changes

* feat(tax): enhance tax deadline generation with new settings and filing methods

- Added new company settings: tax_turnover_over_40m, vat_has_eu_trade, vat_filing_method, periodisk_sammanstallning_enabled, and periodisk_sammanstallning_filing_method.
- Updated deadline generation logic to accommodate new settings affecting VAT and employer declaration deadlines.
- Implemented tests for new functionality, ensuring that completed obligations are preserved and not replaced by new pending rows.
- Introduced a cron job to backfill missing tax deadlines for companies with settings but no upcoming deadlines.
- Updated API routes for generating tax deadlines and handling cron jobs.
- Modified database schema to include new columns for tax filing profiles and constraints for filing methods.

* fix(invoices): record credit note reconciliation guard

* fix(tax): correct automatic deadline settings

* fix(tax): key AGI deadline to VAT taxable base and add storforetag payment deadline

The 26th filing day for the skattedeklaration (AGI and VAT together) hinges
on one statutory measure, a VAT taxable base above SEK 40 million (SFL 26
kap.), not a separate employer turnover. Drop employer_turnover_over_40m and
derive the AGI schedule from vat_registered plus vat_taxable_base_over_40m,
so a non-VAT-reporting employer is never shown the 26th when its binding
date is the 12th.

Also:
- add a skatteinbetalning deadline row (12th, 17 January) for storforetag,
  whose deducted tax and employer contributions are due before the 26th
  filing date
- normalize legally incoherent over-40m flag combinations to the earlier
  small-company schedule in a follow-up migration
- replace hardcoded 27 December dates with the banking-day adjustment
- extend the 40m help text to cover the SKV-decided early filing election
  and the payment-still-on-the-12th rule
- document the regeneration race repaired by the daily backfill cron

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

* feat(migrations): add AGI and VAT filing logic with employer column removal

* feat(settings): implement VAT registration logic and update related flags; enhance deadline handling

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Mattsson
2026-07-17 00:52:57 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 1443235cec
commit a5e37d3510
32 changed files with 1978 additions and 259 deletions
@@ -0,0 +1,27 @@
-- Add the filing-profile inputs required to calculate statutory tax deadlines.
-- Defaults represent the common small-company digital filing path. EU sales
-- list deadlines stay disabled until the company explicitly opts in because
-- VAT registration alone does not create that filing obligation.
ALTER TABLE public.company_settings
ADD COLUMN IF NOT EXISTS tax_turnover_over_40m boolean NOT NULL DEFAULT false,
ADD COLUMN IF NOT EXISTS vat_has_eu_trade boolean NOT NULL DEFAULT false,
ADD COLUMN IF NOT EXISTS vat_filing_method text NOT NULL DEFAULT 'electronic',
ADD COLUMN IF NOT EXISTS periodisk_sammanstallning_enabled boolean NOT NULL DEFAULT false,
ADD COLUMN IF NOT EXISTS periodisk_sammanstallning_filing_method text NOT NULL DEFAULT 'electronic';
ALTER TABLE public.company_settings
DROP CONSTRAINT IF EXISTS company_settings_vat_filing_method_check;
ALTER TABLE public.company_settings
ADD CONSTRAINT company_settings_vat_filing_method_check
CHECK (vat_filing_method IN ('electronic', 'paper'));
ALTER TABLE public.company_settings
DROP CONSTRAINT IF EXISTS company_settings_ps_filing_method_check;
ALTER TABLE public.company_settings
ADD CONSTRAINT company_settings_ps_filing_method_check
CHECK (periodisk_sammanstallning_filing_method IN ('electronic', 'paper'));
NOTIFY pgrst, 'reload schema';
@@ -0,0 +1,32 @@
-- VAT filing periods use beskattningsunderlag, while employer-declaration
-- deadlines use company turnover. Preserve the former combined answer as the
-- initial value for both settings so existing companies keep their dates.
ALTER TABLE public.company_settings
ADD COLUMN IF NOT EXISTS vat_taxable_base_over_40m boolean NOT NULL DEFAULT false,
ADD COLUMN IF NOT EXISTS employer_turnover_over_40m boolean NOT NULL DEFAULT false;
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'company_settings'
AND column_name = 'tax_turnover_over_40m'
) THEN
EXECUTE '
UPDATE public.company_settings
SET
vat_taxable_base_over_40m = tax_turnover_over_40m,
employer_turnover_over_40m = tax_turnover_over_40m
';
END IF;
END
$$;
-- Keep tax_turnover_over_40m temporarily for rollback compatibility with an
-- older application instance during deployment. New code no longer reads or
-- writes it; remove it in a later migration after the split fields are live.
NOTIFY pgrst, 'reload schema';
@@ -0,0 +1,19 @@
-- The 26th filing day for the skattedeklaration (AGI and VAT together) is
-- triggered by one statutory measure: a VAT taxable base above SEK 40 million
-- (SFL 26 kap.). No separate employer-turnover measure exists, so the split
-- introduced by 20260716120353 could show a non-VAT-reporting employer the
-- 26th when its binding AGI deadline is the 12th. Drop the employer column.
ALTER TABLE public.company_settings
DROP COLUMN IF EXISTS employer_turnover_over_40m;
-- Clear the VAT flag where the stored combination is not legally coherent
-- (the flag requires VAT registration and monthly reporting). Ambiguous
-- companies fall back to the small-company schedule, whose dates are always
-- earlier and therefore never produce a late filing.
UPDATE public.company_settings
SET vat_taxable_base_over_40m = false
WHERE vat_taxable_base_over_40m
AND (vat_registered IS DISTINCT FROM true OR moms_period IS DISTINCT FROM 'monthly');
NOTIFY pgrst, 'reload schema';