Changes
Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
co-authored by
renee-png
parent
839ca6c2e0
commit
616bc3b2ba
@@ -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"]
|
||||
|
||||
@@ -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$;
|
||||
Reference in New Issue
Block a user