| 1 | CREATE OR REPLACE FUNCTION public.fn_get_customer_monthly_recurring_charge(p_customer_id bigint)
|
|---|
| 2 | RETURNS numeric
|
|---|
| 3 | LANGUAGE sql
|
|---|
| 4 | STABLE
|
|---|
| 5 | AS $$
|
|---|
| 6 | SELECT COALESCE(SUM(amount), 0)
|
|---|
| 7 | FROM (
|
|---|
| 8 | SELECT p.monthly_fee AS amount
|
|---|
| 9 | FROM public.accounts a
|
|---|
| 10 | JOIN public.subscriptions s ON s.account_id = a.account_id
|
|---|
| 11 | JOIN public.plans p ON p.plan_id = s.plan_id
|
|---|
| 12 | WHERE a.customer_id = p_customer_id
|
|---|
| 13 | AND s.status = 'active'
|
|---|
| 14 | AND p.status = 'active'
|
|---|
| 15 | UNION ALL
|
|---|
| 16 | SELECT sa.price_at_activation
|
|---|
| 17 | FROM public.accounts a
|
|---|
| 18 | JOIN public.subscriptions s ON s.account_id = a.account_id
|
|---|
| 19 | JOIN public.subscription_addons sa ON sa.subscription_id = s.subscription_id
|
|---|
| 20 | JOIN public.addons ad ON ad.addon_id = sa.addon_id
|
|---|
| 21 | WHERE a.customer_id = p_customer_id
|
|---|
| 22 | AND s.status = 'active'
|
|---|
| 23 | AND sa.status = 'active'
|
|---|
| 24 | AND sa.deactivation_date IS NULL
|
|---|
| 25 | AND ad.is_recurring
|
|---|
| 26 | AND ad.status = 'active'
|
|---|
| 27 | ) recurring_items;
|
|---|
| 28 | $$;
|
|---|
| 29 |
|
|---|