Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-17 01:48:35 +00:00
co-authored by renee-png
parent 435fa8d77c
commit f085202d68
2 changed files with 133 additions and 0 deletions
@@ -0,0 +1,58 @@
-- Add category columns to ledger entries
ALTER TABLE public.collection_ledger_entries
ADD COLUMN IF NOT EXISTS assess numeric NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS late numeric NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS admin numeric NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS legal numeric NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS viol numeric NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS interest numeric NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS bank numeric NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS payment numeric NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS account text;
-- Backfill: split existing debit/credit into the appropriate column based on transaction_type
UPDATE public.collection_ledger_entries
SET
assess = CASE WHEN transaction_type = 'assessment' THEN COALESCE(debit,0) ELSE 0 END,
late = CASE WHEN transaction_type = 'late_fee' THEN COALESCE(debit,0) ELSE 0 END,
admin = CASE WHEN transaction_type = 'admin_fee' THEN COALESCE(debit,0) ELSE 0 END,
legal = CASE WHEN transaction_type = 'legal_fee' THEN COALESCE(debit,0) ELSE 0 END,
viol = CASE WHEN transaction_type = 'violation' THEN COALESCE(debit,0) ELSE 0 END,
interest = CASE WHEN transaction_type = 'interest' THEN COALESCE(debit,0) ELSE 0 END,
bank = CASE WHEN transaction_type IN ('bank_fee') THEN COALESCE(debit,0) ELSE 0 END,
payment = CASE WHEN transaction_type IN ('payment','adjustment') THEN COALESCE(credit,0) ELSE 0 END,
account = transaction_type
WHERE assess = 0 AND late = 0 AND admin = 0 AND legal = 0 AND viol = 0 AND interest = 0 AND bank = 0 AND payment = 0;
-- Add ledger metadata to collections
ALTER TABLE public.collections
ADD COLUMN IF NOT EXISTS name text,
ADD COLUMN IF NOT EXISTS interest_rate_override numeric;
-- Payment allocations: how a payment row is applied across priority buckets
CREATE TABLE IF NOT EXISTS public.collection_payment_allocations (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
entry_id uuid NOT NULL REFERENCES public.collection_ledger_entries(id) ON DELETE CASCADE,
collection_id uuid NOT NULL REFERENCES public.collections(id) ON DELETE CASCADE,
bucket text NOT NULL, -- 'bank' | 'interest' | 'late' | 'legal' | 'admin' | 'viol' | 'assess'
amount numeric NOT NULL DEFAULT 0,
created_at timestamptz NOT NULL DEFAULT now()
);
ALTER TABLE public.collection_payment_allocations ENABLE ROW LEVEL SECURITY;
CREATE POLICY "cpa_select_case" ON public.collection_payment_allocations
FOR SELECT TO authenticated USING (
EXISTS (SELECT 1 FROM public.collections c WHERE c.id = collection_id AND can_access_case(c.case_id, auth.uid()))
);
CREATE POLICY "cpa_insert_case" ON public.collection_payment_allocations
FOR INSERT TO authenticated WITH CHECK (
EXISTS (SELECT 1 FROM public.collections c WHERE c.id = collection_id AND can_access_case(c.case_id, auth.uid()))
);
CREATE POLICY "cpa_delete_case" ON public.collection_payment_allocations
FOR DELETE TO authenticated USING (
EXISTS (SELECT 1 FROM public.collections c WHERE c.id = collection_id AND can_access_case(c.case_id, auth.uid()))
);
CREATE INDEX IF NOT EXISTS idx_cpa_entry ON public.collection_payment_allocations(entry_id);
CREATE INDEX IF NOT EXISTS idx_cpa_collection ON public.collection_payment_allocations(collection_id);