From 616bc3b2ba84a770a4d68c55b2b95e97bbe17928 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sun, 19 Apr 2026 01:24:51 +0000 Subject: [PATCH] Changes Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com> --- src/integrations/supabase/types.ts | 10 +++++ ...7_24de63a7-2b0f-4524-abd1-4da095af9cb7.sql | 42 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 supabase/migrations/20260419012447_24de63a7-2b0f-4524-abd1-4da095af9cb7.sql diff --git a/src/integrations/supabase/types.ts b/src/integrations/supabase/types.ts index aa50265..7246c52 100644 --- a/src/integrations/supabase/types.ts +++ b/src/integrations/supabase/types.ts @@ -2896,6 +2896,7 @@ export type Database = { amount: number applied_invoice_id: string | null applied_payment_id: string | null + case_id: string | null client_id: string created_at: string created_by: string | null @@ -2911,6 +2912,7 @@ export type Database = { amount: number applied_invoice_id?: string | null applied_payment_id?: string | null + case_id?: string | null client_id: string created_at?: string created_by?: string | null @@ -2926,6 +2928,7 @@ export type Database = { amount?: number applied_invoice_id?: string | null applied_payment_id?: string | null + case_id?: string | null client_id?: string created_at?: string created_by?: string | null @@ -2952,6 +2955,13 @@ export type Database = { referencedRelation: "invoice_payments" referencedColumns: ["id"] }, + { + foreignKeyName: "trust_ledger_entries_case_id_fkey" + columns: ["case_id"] + isOneToOne: false + referencedRelation: "cases" + referencedColumns: ["id"] + }, { foreignKeyName: "trust_ledger_entries_client_id_fkey" columns: ["client_id"] diff --git a/supabase/migrations/20260419012447_24de63a7-2b0f-4524-abd1-4da095af9cb7.sql b/supabase/migrations/20260419012447_24de63a7-2b0f-4524-abd1-4da095af9cb7.sql new file mode 100644 index 0000000..6ab2fbd --- /dev/null +++ b/supabase/migrations/20260419012447_24de63a7-2b0f-4524-abd1-4da095af9cb7.sql @@ -0,0 +1,42 @@ +-- Add case_id to trust_ledger_entries to track per-case trust balances +ALTER TABLE public.trust_ledger_entries + ADD COLUMN IF NOT EXISTS case_id uuid REFERENCES public.cases(id) ON DELETE SET NULL; + +CREATE INDEX IF NOT EXISTS trust_ledger_case_idx + ON public.trust_ledger_entries (case_id, entry_date DESC); + +-- Backfill case_id from linked invoices where possible +UPDATE public.trust_ledger_entries t + SET case_id = i.case_id + FROM public.invoices i + WHERE t.case_id IS NULL + AND i.case_id IS NOT NULL + AND (t.source_invoice_id = i.id OR t.applied_invoice_id = i.id); + +-- Update retainer-payment trigger to copy case_id from the invoice +CREATE OR REPLACE FUNCTION public.tg_trust_deposit_from_payment() + RETURNS trigger + LANGUAGE plpgsql + SECURITY DEFINER + SET search_path TO 'public' +AS $function$ +DECLARE + v_client_id uuid; + v_case_id uuid; + v_is_retainer boolean; +BEGIN + SELECT i.client_id, i.case_id, i.is_retainer + INTO v_client_id, v_case_id, v_is_retainer + FROM public.invoices i WHERE i.id = NEW.invoice_id; + + IF v_is_retainer IS TRUE AND v_client_id IS NOT NULL AND NEW.amount > 0 THEN + INSERT INTO public.trust_ledger_entries + (client_id, case_id, entry_date, entry_type, amount, note, + source_invoice_id, source_payment_id, created_by) + VALUES + (v_client_id, v_case_id, NEW.paid_on, 'deposit', NEW.amount, + 'Retainer payment', NEW.invoice_id, NEW.id, NEW.created_by); + END IF; + RETURN NEW; +END; +$function$; \ No newline at end of file