diff --git a/supabase/migrations/20260807000600_invoicing.sql b/supabase/migrations/20260807000600_invoicing.sql index f7c61a5..7a704bf 100644 --- a/supabase/migrations/20260807000600_invoicing.sql +++ b/supabase/migrations/20260807000600_invoicing.sql @@ -450,12 +450,14 @@ BEGIN END LOOP; -- Step 9: outstanding credits, oldest first, never below zero. + -- Every column is alias-qualified: this function's RETURNS TABLE declares an + -- `amount_cents` output, which would otherwise shadow the table's column. FOR cred IN - SELECT * FROM public.student_credits - WHERE student_id = _student AND NOT is_void - AND amount_applied_cents < amount_cents - AND (expires_on IS NULL OR expires_on >= _from) - ORDER BY issued_at + SELECT sc.* FROM public.student_credits sc + WHERE sc.student_id = _student AND NOT sc.is_void + AND sc.amount_applied_cents < sc.amount_cents + AND (sc.expires_on IS NULL OR sc.expires_on >= _from) + ORDER BY sc.issued_at LOOP EXIT WHEN running_total <= 0; remaining := cred.amount_cents - cred.amount_applied_cents;