Files
accounted/supabase/migrations/20260324120000_categorization_templates.sql
T
Jakob Wennberg d8e0a22495 feat: counterparty templates, Skatteverket extension, complete VAT form (#117)
* feat: separate AR/AP/accounting into distinct nav groups (#92)

Split the flat "Finans" sidebar group into three visually distinct
sections — Försäljning (AR), Inköp (AP), and Redovisning — so users
coming from Fortnox immediately find customer invoicing and supplier
invoices as top-level concepts.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: journal entry detail view, correction chain, and account name display

- Add journal entry detail page at /bookkeeping/[id] with full entry view
- Add correction chain API and component showing storno relationships
- Add JournalEntryStatusBadge component for entry status display
- Show debit/credit account names in template picker and review dialogs
- Expand client-side BAS account name mapping with additional accounts
- Show account codes on transaction inbox suggestion buttons

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: address review feedback — N+1 query, duplicate name, nav dedup

- Batch reverse-lookup into single query per BFS iteration (was N+1)
- Differentiate account 2393 from 2893 in display names
- Extract shared loop for desktop/mobile nav group rendering

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: counterparty templates, Skatteverket extension, VAT form completeness, and UI cleanup

- Add counterparty-based categorization templates (learned from user approvals
  and auto-ingestion) with fuzzy matching in the mapping engine
- Add Skatteverket extension for direct VAT declaration submission via API
- Complete VAT declaration form with all 30 SKV 4700 boxes (ruta 08, 35-42, 50, 60-62)
- Fix ruta 49 formula to include import VAT (ruta 60+61+62)
- Simplify dashboard UI: remove redundant icons from stat cards, customer cards,
  invoice list, supplier invoices; use Badge variants consistently
- Add SkatteverketPanel component to reports page
- Add categorization_templates and skatteverket_tokens migrations
- Update tests and helpers for new types

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: address PR review feedback — VAT detection, migration timestamps, dedup

- Fix detectVatTreatment to derive actual rate (12%/6%) from VAT line
  description instead of hardcoding standard_25
- Rename skatteverket_tokens migration to 20260324120001 to avoid
  duplicate timestamp with categorization_templates (fixes Supabase
  deployment failure)
- Make refreshAccessToken accept previousRefreshCount param to enforce
  refresh limit contract at the type level
- Fix rate limiter TOCTOU by claiming slot before await
- Extract formatRedovisare/formatRedovisningsperiod to shared
  lib/skatteverket/format.ts — eliminates duplication between
  mappers.ts and SkatteverketPanel.tsx

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 13:41:12 +01:00

67 lines
3.0 KiB
SQL

-- Migration: categorization_templates
-- Per-tenant counterparty-based categorization templates.
-- Learned from user categorizations, SIE imports, or SNI defaults.
-- Slots into the categorization chain between mapping_rules and static booking templates.
CREATE TABLE public.categorization_templates (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES auth.users ON DELETE CASCADE NOT NULL,
-- Counterparty identity
counterparty_name TEXT NOT NULL, -- normalized canonical name
counterparty_aliases TEXT[] NOT NULL DEFAULT '{}', -- raw name variants seen
-- Accounting mapping
debit_account TEXT NOT NULL, -- BAS account number (string)
credit_account TEXT NOT NULL, -- BAS account number (string)
vat_treatment TEXT, -- standard_25, reduced_12, etc.
vat_account TEXT, -- 2611/2621/2631/2641
category TEXT, -- TransactionCategory for reverse lookup
-- Confidence signals
occurrence_count INTEGER NOT NULL DEFAULT 1,
confidence NUMERIC NOT NULL DEFAULT 0.5
CHECK (confidence >= 0 AND confidence <= 1),
last_seen_date DATE,
-- Source tracking
source TEXT NOT NULL DEFAULT 'user_approved'
CHECK (source IN ('sie_import', 'user_approved', 'sni_default', 'auto_learned')),
-- Metadata
is_active BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
-- One template per counterparty per user (upsert-friendly)
UNIQUE (user_id, counterparty_name)
);
-- RLS
ALTER TABLE public.categorization_templates ENABLE ROW LEVEL SECURITY;
CREATE POLICY "categorization_templates_select" ON public.categorization_templates
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "categorization_templates_insert" ON public.categorization_templates
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "categorization_templates_update" ON public.categorization_templates
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "categorization_templates_delete" ON public.categorization_templates
FOR DELETE USING (auth.uid() = user_id);
-- Indexes
CREATE INDEX idx_categorization_templates_user_id
ON public.categorization_templates (user_id);
CREATE INDEX idx_categorization_templates_counterparty
ON public.categorization_templates (user_id, counterparty_name);
CREATE INDEX idx_categorization_templates_aliases
ON public.categorization_templates USING GIN (counterparty_aliases);
CREATE INDEX idx_categorization_templates_active
ON public.categorization_templates (user_id, is_active)
WHERE is_active = TRUE;
-- updated_at trigger
CREATE TRIGGER categorization_templates_updated_at
BEFORE UPDATE ON public.categorization_templates
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();