| 1 | DROP PROCEDURE IF EXISTS public.proc_change_subscription_plan(bigint, bigint);
|
|---|
| 2 | DROP PROCEDURE IF EXISTS public.proc_change_subscription_plan(bigint, bigint, bigint, text);
|
|---|
| 3 |
|
|---|
| 4 | CREATE PROCEDURE public.proc_change_subscription_plan(
|
|---|
| 5 | p_subscription_id bigint,
|
|---|
| 6 | p_new_plan_id bigint,
|
|---|
| 7 | p_changed_by_employee_id bigint DEFAULT NULL,
|
|---|
| 8 | p_reason text DEFAULT 'Customer requested plan change'
|
|---|
| 9 | )
|
|---|
| 10 | LANGUAGE plpgsql
|
|---|
| 11 | AS $$
|
|---|
| 12 | DECLARE
|
|---|
| 13 | v_old_plan_id bigint;
|
|---|
| 14 | v_old_product_id bigint;
|
|---|
| 15 | v_new_product_id bigint;
|
|---|
| 16 | v_status text;
|
|---|
| 17 | BEGIN
|
|---|
| 18 | SELECT s.plan_id, s.status, p.product_id
|
|---|
| 19 | INTO v_old_plan_id, v_status, v_old_product_id
|
|---|
| 20 | FROM public.subscriptions s
|
|---|
| 21 | JOIN public.plans p ON p.plan_id = s.plan_id
|
|---|
| 22 | WHERE s.subscription_id = p_subscription_id
|
|---|
| 23 | FOR UPDATE OF s;
|
|---|
| 24 |
|
|---|
| 25 | IF NOT FOUND THEN
|
|---|
| 26 | RAISE EXCEPTION 'Subscription % does not exist.', p_subscription_id;
|
|---|
| 27 | END IF;
|
|---|
| 28 | IF v_status NOT IN ('active', 'suspended') THEN
|
|---|
| 29 | RAISE EXCEPTION 'Cannot change plan for subscription with status %.', v_status;
|
|---|
| 30 | END IF;
|
|---|
| 31 | IF v_old_plan_id = p_new_plan_id THEN
|
|---|
| 32 | RAISE EXCEPTION 'Subscription % is already on plan %.', p_subscription_id, p_new_plan_id;
|
|---|
| 33 | END IF;
|
|---|
| 34 |
|
|---|
| 35 | SELECT product_id INTO v_new_product_id
|
|---|
| 36 | FROM public.plans
|
|---|
| 37 | WHERE plan_id = p_new_plan_id AND status = 'active';
|
|---|
| 38 | IF NOT FOUND THEN
|
|---|
| 39 | RAISE EXCEPTION 'Plan % does not exist or is not active.', p_new_plan_id;
|
|---|
| 40 | END IF;
|
|---|
| 41 | IF v_new_product_id <> v_old_product_id THEN
|
|---|
| 42 | RAISE EXCEPTION 'New plan must belong to the same product as the current plan.';
|
|---|
| 43 | END IF;
|
|---|
| 44 | IF p_changed_by_employee_id IS NOT NULL AND NOT EXISTS (
|
|---|
| 45 | SELECT 1 FROM public.employees
|
|---|
| 46 | WHERE employee_id = p_changed_by_employee_id AND employment_status = 'active'
|
|---|
| 47 | ) THEN
|
|---|
| 48 | RAISE EXCEPTION 'Employee % does not exist or is not active.', p_changed_by_employee_id;
|
|---|
| 49 | END IF;
|
|---|
| 50 |
|
|---|
| 51 | PERFORM set_config('app.employee_id', COALESCE(p_changed_by_employee_id::text, ''), true);
|
|---|
| 52 | PERFORM set_config('app.change_reason', COALESCE(NULLIF(p_reason, ''), 'Plan change'), true);
|
|---|
| 53 |
|
|---|
| 54 | UPDATE public.subscriptions
|
|---|
| 55 | SET plan_id = p_new_plan_id
|
|---|
| 56 | WHERE subscription_id = p_subscription_id;
|
|---|
| 57 |
|
|---|
| 58 | RAISE NOTICE 'Subscription % changed from plan % to plan %. Future invoices will use the new plan.',
|
|---|
| 59 | p_subscription_id, v_old_plan_id, p_new_plan_id;
|
|---|
| 60 | END;
|
|---|
| 61 | $$;
|
|---|
| 62 |
|
|---|