Bug/accounting ps eu (#474)
* feat(api): implement commit functionality for journal entries * fix(extensions): make ExtensionSettings.clear() a real delete so disconnect flows work The 2026-03-30 multi-tenant refactor dropped all RLS policies on extension_data and recreated only SELECT/INSERT/UPDATE. Combined with `value jsonb NOT NULL`, every extension that called `settings.set(key, null)` to clear stored state (cloud-backup disconnect, skatteverket OAuth/AGI cleanup, arcim-migration consent reset) silently failed — the upsert hit the NOT NULL constraint and the error was swallowed, leaving users stuck with stale connection rows. Adds an `extension_data_delete` RLS policy, a `clear(key)` method backed by a real DELETE, switches the four affected handlers, and makes `set()` throw on Supabase error so this class of silent failure can't recur. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(journal-entries): add draft saving functionality to journal entry form * feat: add periodisk sammanställning report generation and CSV export - Implemented period date helpers in `period-dates.ts` for calculating start and end dates based on period type (monthly, quarterly, yearly). - Created `periodisk-sammanstallning.ts` to generate the periodisk sammanställning report, including data fetching, validation, and warning handling. - Developed CSV serializer in `periodisk-sammanstallning-csv.ts` for exporting the report in SKV574008 format. - Added new columns to `company_settings` for storing periodisk sammanställning settings and tax contact information via migration. - Introduced a new migration to add a `paid_with_private_funds` flag to `supplier_invoices` for tracking out-of-pocket expenses. - Updated journal entries to include the new source type for privately paid supplier invoices. * feat(migrations): add paid_with_private_funds flag to supplier_invoices and expand journal_entries.source_type CHECK * fix(ai_requests): drop existing policies and trigger before creating new ones * fix(migrations): ensure extension_data has a proper DELETE policy for ExtensionSettings.clear() * fix(supplier-invoices): update error handling for invalid input in POST request * fix: correct capitalization in project title * fix(migrations): resolve duplicate version 20260513120000 Two migrations shared the same timestamp prefix, causing schema_migrations_pkey collision on Supabase preview branches. Bump extension_data_delete_policy to 20260513120001. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- 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
ba45b8f661
commit
c8461397c8
@@ -62,14 +62,18 @@ CREATE INDEX IF NOT EXISTS idx_ai_requests_subject
|
||||
-- RLS: company-scoped using user_company_ids()
|
||||
ALTER TABLE public.ai_requests ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
DROP POLICY IF EXISTS "ai_requests_select" ON public.ai_requests;
|
||||
CREATE POLICY "ai_requests_select" ON public.ai_requests
|
||||
FOR SELECT USING (company_id IN (SELECT public.user_company_ids()));
|
||||
DROP POLICY IF EXISTS "ai_requests_insert" ON public.ai_requests;
|
||||
CREATE POLICY "ai_requests_insert" ON public.ai_requests
|
||||
FOR INSERT WITH CHECK (company_id IN (SELECT public.user_company_ids()));
|
||||
DROP POLICY IF EXISTS "ai_requests_update" ON public.ai_requests;
|
||||
CREATE POLICY "ai_requests_update" ON public.ai_requests
|
||||
FOR UPDATE USING (company_id IN (SELECT public.user_company_ids()));
|
||||
|
||||
-- updated_at trigger
|
||||
DROP TRIGGER IF EXISTS ai_requests_updated_at ON public.ai_requests;
|
||||
CREATE TRIGGER ai_requests_updated_at
|
||||
BEFORE UPDATE ON public.ai_requests
|
||||
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
-- The 2026-03-30 multi-tenant refactor (20260330130000) dropped all policies on
|
||||
-- extension_data and recreated only SELECT / INSERT / UPDATE — leaving DELETE
|
||||
-- without a policy. Combined with `value jsonb NOT NULL`, this silently broke
|
||||
-- every extension that tried to clear stored state via upsert with value=null
|
||||
-- (cloud-backup disconnect, skatteverket OAuth/AGI cleanup, arcim-migration
|
||||
-- consent reset). The fix is to expose a real DELETE on the row and have
|
||||
-- ExtensionSettings.clear() use it.
|
||||
|
||||
DROP POLICY IF EXISTS "extension_data_delete" ON public.extension_data;
|
||||
CREATE POLICY "extension_data_delete" ON public.extension_data
|
||||
FOR DELETE USING (company_id IN (SELECT public.user_company_ids()));
|
||||
|
||||
NOTIFY pgrst, 'reload schema';
|
||||
@@ -0,0 +1,42 @@
|
||||
-- Add fields to company_settings needed for periodisk sammanställning (SKV 5740).
|
||||
--
|
||||
-- periodisk_sammanstallning_period:
|
||||
-- Separate from moms_period because:
|
||||
-- 1. PS does not support 'yearly' (only monthly or quarterly).
|
||||
-- 2. Goods sellers must report monthly even if VAT is quarterly
|
||||
-- (35 kap. 2 § SFL).
|
||||
-- 3. Services-only sellers can apply for quarterly independently.
|
||||
--
|
||||
-- tax_contact_*:
|
||||
-- The SKV574008 file header requires contact person + phone + email of the
|
||||
-- declarant. Stored on company_settings so it is reusable for future
|
||||
-- Skatteverket filings (e.g. AGI, momsdeklaration export).
|
||||
|
||||
ALTER TABLE public.company_settings
|
||||
ADD COLUMN IF NOT EXISTS periodisk_sammanstallning_period text,
|
||||
ADD COLUMN IF NOT EXISTS tax_contact_name text,
|
||||
ADD COLUMN IF NOT EXISTS tax_contact_phone text,
|
||||
ADD COLUMN IF NOT EXISTS tax_contact_email text;
|
||||
|
||||
-- Backfill: monthly default for everyone except quarterly VAT-payers.
|
||||
UPDATE public.company_settings
|
||||
SET periodisk_sammanstallning_period = CASE
|
||||
WHEN moms_period = 'quarterly' THEN 'quarterly'
|
||||
ELSE 'monthly'
|
||||
END
|
||||
WHERE periodisk_sammanstallning_period IS NULL;
|
||||
|
||||
ALTER TABLE public.company_settings
|
||||
ALTER COLUMN periodisk_sammanstallning_period SET DEFAULT 'monthly';
|
||||
|
||||
ALTER TABLE public.company_settings
|
||||
ALTER COLUMN periodisk_sammanstallning_period SET NOT NULL;
|
||||
|
||||
ALTER TABLE public.company_settings
|
||||
DROP CONSTRAINT IF EXISTS company_settings_ps_period_check;
|
||||
|
||||
ALTER TABLE public.company_settings
|
||||
ADD CONSTRAINT company_settings_ps_period_check
|
||||
CHECK (periodisk_sammanstallning_period IN ('monthly', 'quarterly'));
|
||||
|
||||
NOTIFY pgrst, 'reload schema';
|
||||
@@ -0,0 +1,49 @@
|
||||
-- Migration: add paid_with_private_funds flag to supplier_invoices
|
||||
--
|
||||
-- Marks a supplier invoice / kvitto that the owner paid out-of-pocket with
|
||||
-- private funds, so the company owes them back (eget utlägg).
|
||||
--
|
||||
-- When this flag is set at creation time the registration path bypasses the
|
||||
-- normal AP (2440) leg entirely and books the expense directly against
|
||||
-- shareholder/owner accounts:
|
||||
-- AB → credit 2893 (Skulder till närstående personer, kortfristig del)
|
||||
-- EF → credit 2018 (Övriga egna insättningar)
|
||||
--
|
||||
-- The invoice is created with status='paid' from the start; the regular
|
||||
-- mark-paid flow stays unreachable for these rows (existing status guard in
|
||||
-- /api/supplier-invoices/[id]/mark-paid already rejects status='paid').
|
||||
--
|
||||
-- BFL 5 kap verifikationskrav is unchanged — the invoice row still acts as
|
||||
-- the underlying business document; only the journal-entry shape differs.
|
||||
-- Retroactively flipping the flag on existing rows is not supported in v1
|
||||
-- (the original AP journal entry is immutable per migration 017 trigger).
|
||||
|
||||
ALTER TABLE public.supplier_invoices
|
||||
ADD COLUMN IF NOT EXISTS paid_with_private_funds boolean NOT NULL DEFAULT false;
|
||||
|
||||
-- Partial index: most invoices are AP-flow, only a minority are private
|
||||
-- utlägg. The reports we'll likely build (e.g. "what does the company owe
|
||||
-- the owner") need to find these fast.
|
||||
CREATE INDEX IF NOT EXISTS idx_supplier_invoices_private_funds
|
||||
ON public.supplier_invoices (company_id, invoice_date)
|
||||
WHERE paid_with_private_funds = true;
|
||||
|
||||
-- Expand journal_entries.source_type CHECK to include the new entry type.
|
||||
-- See 20260304075837 for the previous expansion pattern.
|
||||
ALTER TABLE public.journal_entries
|
||||
DROP CONSTRAINT IF EXISTS journal_entries_source_type_check;
|
||||
|
||||
ALTER TABLE public.journal_entries
|
||||
ADD CONSTRAINT journal_entries_source_type_check
|
||||
CHECK (source_type IN (
|
||||
'manual', 'bank_transaction', 'invoice_created',
|
||||
'invoice_paid', 'invoice_cash_payment', 'credit_note', 'salary_payment',
|
||||
'opening_balance', 'year_end',
|
||||
'storno', 'correction', 'import', 'system',
|
||||
'supplier_invoice_registered', 'supplier_invoice_paid',
|
||||
'supplier_invoice_cash_payment', 'supplier_credit_note',
|
||||
'currency_revaluation',
|
||||
'supplier_invoice_privately_paid'
|
||||
));
|
||||
|
||||
NOTIFY pgrst, 'reload schema';
|
||||
Reference in New Issue
Block a user