91e2c1705a
Per-line VAT rates: - Add generatePerRateLines() to group invoice items by vat_rate with separate revenue + VAT lines per rate group (invoice-entries.ts) - Add getAvailableVatRates() and getVatTreatmentForRate() (vat-rules.ts) - PDF template shows per-line VAT column and per-rate totals for mixed-rate invoices - Invoice create/review UI supports per-line rate selection - Types: add vat_rate/vat_amount to InvoiceItem, vat_rate to CreateInvoiceItemInput Invoice document types (proforma, delivery note): - Add InvoiceDocumentType, document_type and converted_from_id to Invoice type - PDF hides prices for delivery notes, adds proforma notice - Email templates support all document types - mark-paid skips journal entries for non-invoice document types - Migration 031: invoice_document_type Accounting method support: - Add AccountingMethod type (accrual/cash) - Migration 032: add_accounting_method column to company_settings VAT declaration rewrite: - Rewrite to read directly from general ledger (26xx/3xxx account lines) instead of aggregating invoices/transactions/receipts - ACCOUNT_RUTA mapping drives momsdeklaration boxes from GL balances Bank reconciliation: - Transaction ingest now pre-fetches unlinked GL lines and attempts auto-reconciliation during import - Add transaction.reconciled event type - Add ReconciliationMethod type and reconciliation_method on Transaction - Migration 030: bank_reconciliation - New reconciliation engine, API routes, and BankReconciliationView component Pagination (fetchAllRows): - New lib/supabase/fetch-all.ts overcomes PostgREST 1000-row limit - Adopted in all report generators, SIE/SRU export, account list APIs Fiscal period validation: - New validate-period-duration.ts enforces max 18 months per BFL 3 kap. - Applied in period-service.ts and fiscal-periods API Account mapper simplification: - Remove Levenshtein/fuzzy matching, use exact account number match only Swedbank parser improvements: - Support abbreviated headers (Clnr, Bokfdag, Radnr) - Use Referens column as counterparty Chart of accounts management: - Add DELETE endpoint with system account and usage protection - PUT uses partial updates - New AccountCombobox, AddAccountDialog, EditAccountDialog, ChartOfAccountsManager Tax deadline corrections: - Rewrite inkomstdeklaration_ab using Skatteverket lookup table - Rewrite arsredovisning deadline to 7 months after FY end per ÅRL 8:3 Onboarding first fiscal year: - Add first fiscal year toggle with date pickers and 18-month validation UI terminology: - Change "okategoriserad/kategorisera" to "obokförd/bokföra" throughout Report column fix: - Fix start_date/end_date to period_start/period_end in report queries Supplier invoice input: - CreateSupplierInvoiceItemInput uses amount field (legacy quantity/unit_price kept) Misc: - SIE import uses upsert for idempotent account creation - account-descriptions.ts falls back to BAS reference data - Add invoice_default_notes to CompanySettings - Update CLAUDE.md to reflect current project state Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
116 lines
6.6 KiB
SQL
116 lines
6.6 KiB
SQL
-- ============================================================
|
|
-- clear-user-data.sql
|
|
-- Deletes ALL data for a given user from erp-base, including
|
|
-- the auth.users row. Handles circular FKs and temporarily
|
|
-- disables enforcement/audit triggers.
|
|
--
|
|
-- Usage:
|
|
-- 1. Set the target email below
|
|
-- 2. Run via Supabase SQL Editor or MCP execute_sql
|
|
-- ============================================================
|
|
|
|
-- Step 0: Look up the user ID (run this first to verify)
|
|
-- SELECT id, email FROM auth.users WHERE email = 'user@example.com';
|
|
|
|
DO $$
|
|
DECLARE
|
|
-- >>> SET THE TARGET USER EMAIL HERE <<<
|
|
target_email TEXT := 'user@example.com';
|
|
target_user_id UUID;
|
|
BEGIN
|
|
-- Resolve email to user ID
|
|
SELECT id INTO target_user_id FROM auth.users WHERE email = target_email;
|
|
|
|
IF target_user_id IS NULL THEN
|
|
RAISE EXCEPTION 'No user found with email: %', target_email;
|
|
END IF;
|
|
|
|
RAISE NOTICE 'Clearing all data for user % (%)', target_email, target_user_id;
|
|
|
|
-- Disable all user-defined triggers (enforcement, audit, updated_at)
|
|
-- Does NOT disable system FK triggers
|
|
ALTER TABLE public.journal_entries DISABLE TRIGGER USER;
|
|
ALTER TABLE public.journal_entry_lines DISABLE TRIGGER USER;
|
|
ALTER TABLE public.document_attachments DISABLE TRIGGER USER;
|
|
ALTER TABLE public.fiscal_periods DISABLE TRIGGER USER;
|
|
ALTER TABLE public.transactions DISABLE TRIGGER USER;
|
|
ALTER TABLE public.receipts DISABLE TRIGGER USER;
|
|
ALTER TABLE public.invoices DISABLE TRIGGER USER;
|
|
ALTER TABLE public.sie_imports DISABLE TRIGGER USER;
|
|
ALTER TABLE public.cost_centers DISABLE TRIGGER USER;
|
|
ALTER TABLE public.supplier_invoices DISABLE TRIGGER USER;
|
|
ALTER TABLE public.customers DISABLE TRIGGER USER;
|
|
ALTER TABLE public.suppliers DISABLE TRIGGER USER;
|
|
ALTER TABLE public.chart_of_accounts DISABLE TRIGGER USER;
|
|
ALTER TABLE public.audit_log DISABLE TRIGGER USER;
|
|
ALTER TABLE public.company_settings DISABLE TRIGGER USER;
|
|
ALTER TABLE public.profiles DISABLE TRIGGER USER;
|
|
|
|
-- Break circular / self-referencing FK constraints
|
|
UPDATE public.fiscal_periods SET closing_entry_id = NULL, opening_balance_entry_id = NULL, previous_period_id = NULL WHERE user_id = target_user_id;
|
|
UPDATE public.transactions SET receipt_id = NULL, journal_entry_id = NULL, invoice_id = NULL, potential_invoice_id = NULL, supplier_invoice_id = NULL, bank_connection_id = NULL WHERE user_id = target_user_id;
|
|
UPDATE public.receipts SET matched_transaction_id = NULL, document_id = NULL WHERE user_id = target_user_id;
|
|
UPDATE public.invoices SET credited_invoice_id = NULL, converted_from_id = NULL WHERE user_id = target_user_id;
|
|
UPDATE public.document_attachments SET original_id = NULL, superseded_by_id = NULL, journal_entry_id = NULL, journal_entry_line_id = NULL WHERE user_id = target_user_id;
|
|
UPDATE public.sie_imports SET opening_balance_entry_id = NULL, fiscal_period_id = NULL WHERE user_id = target_user_id;
|
|
UPDATE public.cost_centers SET parent_id = NULL WHERE user_id = target_user_id;
|
|
|
|
-- Delete child/leaf tables first, then parent tables
|
|
DELETE FROM public.receipt_line_items WHERE receipt_id IN (SELECT id FROM public.receipts WHERE user_id = target_user_id);
|
|
DELETE FROM public.invoice_items WHERE invoice_id IN (SELECT id FROM public.invoices WHERE user_id = target_user_id);
|
|
DELETE FROM public.invoice_reminders WHERE user_id = target_user_id;
|
|
DELETE FROM public.supplier_invoice_items WHERE supplier_invoice_id IN (SELECT id FROM public.supplier_invoices WHERE user_id = target_user_id);
|
|
DELETE FROM public.supplier_invoice_payments WHERE supplier_invoice_id IN (SELECT id FROM public.supplier_invoices WHERE user_id = target_user_id);
|
|
DELETE FROM public.document_attachments WHERE user_id = target_user_id;
|
|
DELETE FROM public.journal_entry_lines WHERE journal_entry_id IN (SELECT id FROM public.journal_entries WHERE user_id = target_user_id);
|
|
DELETE FROM public.journal_entries WHERE user_id = target_user_id;
|
|
DELETE FROM public.receipts WHERE user_id = target_user_id;
|
|
DELETE FROM public.transactions WHERE user_id = target_user_id;
|
|
DELETE FROM public.invoices WHERE user_id = target_user_id;
|
|
DELETE FROM public.supplier_invoices WHERE user_id = target_user_id;
|
|
DELETE FROM public.suppliers WHERE user_id = target_user_id;
|
|
DELETE FROM public.customers WHERE user_id = target_user_id;
|
|
DELETE FROM public.sie_imports WHERE user_id = target_user_id;
|
|
DELETE FROM public.sie_account_mappings WHERE user_id = target_user_id;
|
|
DELETE FROM public.voucher_sequences WHERE user_id = target_user_id;
|
|
DELETE FROM public.fiscal_periods WHERE user_id = target_user_id;
|
|
DELETE FROM public.chart_of_accounts WHERE user_id = target_user_id;
|
|
DELETE FROM public.bank_connections WHERE user_id = target_user_id;
|
|
DELETE FROM public.mapping_rules WHERE user_id = target_user_id;
|
|
DELETE FROM public.deadlines WHERE user_id = target_user_id;
|
|
DELETE FROM public.calendar_feeds WHERE user_id = target_user_id;
|
|
DELETE FROM public.push_subscriptions WHERE user_id = target_user_id;
|
|
DELETE FROM public.notification_log WHERE user_id = target_user_id;
|
|
DELETE FROM public.notification_settings WHERE user_id = target_user_id;
|
|
DELETE FROM public.cost_centers WHERE user_id = target_user_id;
|
|
DELETE FROM public.projects WHERE user_id = target_user_id;
|
|
DELETE FROM public.bank_file_imports WHERE user_id = target_user_id;
|
|
DELETE FROM public.audit_log WHERE user_id = target_user_id;
|
|
DELETE FROM public.extension_toggles WHERE user_id = target_user_id;
|
|
DELETE FROM public.company_settings WHERE user_id = target_user_id;
|
|
DELETE FROM public.profiles WHERE id = target_user_id;
|
|
|
|
-- Re-enable all user-defined triggers
|
|
ALTER TABLE public.journal_entries ENABLE TRIGGER USER;
|
|
ALTER TABLE public.journal_entry_lines ENABLE TRIGGER USER;
|
|
ALTER TABLE public.document_attachments ENABLE TRIGGER USER;
|
|
ALTER TABLE public.fiscal_periods ENABLE TRIGGER USER;
|
|
ALTER TABLE public.transactions ENABLE TRIGGER USER;
|
|
ALTER TABLE public.receipts ENABLE TRIGGER USER;
|
|
ALTER TABLE public.invoices ENABLE TRIGGER USER;
|
|
ALTER TABLE public.sie_imports ENABLE TRIGGER USER;
|
|
ALTER TABLE public.cost_centers ENABLE TRIGGER USER;
|
|
ALTER TABLE public.supplier_invoices ENABLE TRIGGER USER;
|
|
ALTER TABLE public.customers ENABLE TRIGGER USER;
|
|
ALTER TABLE public.suppliers ENABLE TRIGGER USER;
|
|
ALTER TABLE public.chart_of_accounts ENABLE TRIGGER USER;
|
|
ALTER TABLE public.audit_log ENABLE TRIGGER USER;
|
|
ALTER TABLE public.company_settings ENABLE TRIGGER USER;
|
|
ALTER TABLE public.profiles ENABLE TRIGGER USER;
|
|
|
|
-- Delete the auth user
|
|
DELETE FROM auth.users WHERE id = target_user_id;
|
|
|
|
RAISE NOTICE 'Done. User % and all associated data have been deleted.', target_email;
|
|
END $$;
|