|
| 1 | +-- ============================================================================= |
| 2 | +-- 038 — Loan EMI model: schedule + payments tables, loans columns. |
| 3 | +-- |
| 4 | +-- Adds an optional EMI repayment track to the loan system. The existing |
| 5 | +-- "accrual" model is unchanged; new loans can opt into "emi" via the |
| 6 | +-- repayment_model discriminator column on public.loans. |
| 7 | +-- |
| 8 | +-- New objects |
| 9 | +-- ----------- |
| 10 | +-- public.loans (ALTER) — term_months, interest_rate_pct, |
| 11 | +-- emi_amount, schedule_generated_at, |
| 12 | +-- repayment_model |
| 13 | +-- public.loan_emi_schedule — one row per installment per loan |
| 14 | +-- public.loan_emi_payments — junction: installment ↔ transaction |
| 15 | +-- |
| 16 | +-- RLS style mirrors 009_loan_interest_accruals: |
| 17 | +-- SELECT → every authenticated user |
| 18 | +-- ALL → authenticated + is_admin() guard |
| 19 | +-- ============================================================================= |
| 20 | + |
| 21 | +begin; |
| 22 | + |
| 23 | +-- ----------------------------------------------------------------------------- |
| 24 | +-- (a) Extend public.loans with EMI columns |
| 25 | +-- ----------------------------------------------------------------------------- |
| 26 | + |
| 27 | +alter table public.loans |
| 28 | + add column if not exists term_months integer, |
| 29 | + add column if not exists interest_rate_pct numeric, |
| 30 | + add column if not exists emi_amount numeric(12,2), |
| 31 | + add column if not exists schedule_generated_at timestamptz, |
| 32 | + add column if not exists repayment_model text not null default 'accrual' |
| 33 | + check (repayment_model in ('accrual', 'emi')); |
| 34 | + |
| 35 | +-- ----------------------------------------------------------------------------- |
| 36 | +-- (b) CREATE TABLE public.loan_emi_schedule |
| 37 | +-- One row per installment. Mirrors loan_interest_accruals in style. |
| 38 | +-- ----------------------------------------------------------------------------- |
| 39 | + |
| 40 | +create table if not exists public.loan_emi_schedule ( |
| 41 | + id uuid primary key default gen_random_uuid(), |
| 42 | + loan_id uuid not null references public.loans(id) on delete cascade, |
| 43 | + installment_no integer not null, |
| 44 | + due_date date not null, |
| 45 | + opening_balance numeric(12,2) not null, |
| 46 | + emi_amount numeric(12,2) not null, |
| 47 | + principal_due numeric(12,2) not null, |
| 48 | + interest_due numeric(12,2) not null, |
| 49 | + closing_balance numeric(12,2) not null, |
| 50 | + principal_paid numeric(12,2) not null default 0, |
| 51 | + interest_paid numeric(12,2) not null default 0, |
| 52 | + status text not null default 'scheduled' |
| 53 | + check (status in ('scheduled', 'paid', 'partially_paid', 'overdue', 'waived')), |
| 54 | + late_fee_charged numeric(12,2) not null default 0, |
| 55 | + late_fee_txn_id uuid references public.transactions(id), |
| 56 | + paid_at timestamptz, |
| 57 | + created_at timestamptz not null default now(), |
| 58 | + unique (loan_id, installment_no) |
| 59 | +); |
| 60 | + |
| 61 | +create index if not exists idx_emi_schedule_loan |
| 62 | + on public.loan_emi_schedule (loan_id); |
| 63 | + |
| 64 | +create index if not exists idx_emi_schedule_due |
| 65 | + on public.loan_emi_schedule (due_date) |
| 66 | + where status in ('scheduled', 'partially_paid', 'overdue'); |
| 67 | + |
| 68 | +-- ----------------------------------------------------------------------------- |
| 69 | +-- (c) CREATE TABLE public.loan_emi_payments |
| 70 | +-- Junction: installment ↔ transaction. Mirrors loan_interest_payments. |
| 71 | +-- ----------------------------------------------------------------------------- |
| 72 | + |
| 73 | +create table if not exists public.loan_emi_payments ( |
| 74 | + schedule_id uuid not null references public.loan_emi_schedule(id) on delete restrict, |
| 75 | + transaction_id uuid not null references public.transactions(id) on delete restrict, |
| 76 | + principal_applied numeric(12,2) not null default 0, |
| 77 | + interest_applied numeric(12,2) not null default 0, |
| 78 | + applied_at timestamptz not null default now(), |
| 79 | + primary key (schedule_id, transaction_id) |
| 80 | +); |
| 81 | + |
| 82 | +create index if not exists idx_emi_payments_txn |
| 83 | + on public.loan_emi_payments (transaction_id); |
| 84 | + |
| 85 | +-- ----------------------------------------------------------------------------- |
| 86 | +-- (d) RLS — mirrors 009_loan_interest_accruals exactly |
| 87 | +-- ----------------------------------------------------------------------------- |
| 88 | + |
| 89 | +alter table public.loan_emi_schedule enable row level security; |
| 90 | +drop policy if exists "emi_schedule_read_authenticated" on public.loan_emi_schedule; |
| 91 | +create policy "emi_schedule_read_authenticated" |
| 92 | + on public.loan_emi_schedule |
| 93 | + for select to authenticated using (true); |
| 94 | +drop policy if exists "emi_schedule_write_admin" on public.loan_emi_schedule; |
| 95 | +create policy "emi_schedule_write_admin" |
| 96 | + on public.loan_emi_schedule |
| 97 | + for all to authenticated |
| 98 | + using (public.is_admin()) with check (public.is_admin()); |
| 99 | + |
| 100 | +alter table public.loan_emi_payments enable row level security; |
| 101 | +drop policy if exists "emi_payments_read_authenticated" on public.loan_emi_payments; |
| 102 | +create policy "emi_payments_read_authenticated" |
| 103 | + on public.loan_emi_payments |
| 104 | + for select to authenticated using (true); |
| 105 | +drop policy if exists "emi_payments_write_admin" on public.loan_emi_payments; |
| 106 | +create policy "emi_payments_write_admin" |
| 107 | + on public.loan_emi_payments |
| 108 | + for all to authenticated |
| 109 | + using (public.is_admin()) with check (public.is_admin()); |
| 110 | + |
| 111 | +commit; |
0 commit comments