Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION get_membership_status
  2.     (membership_start DATE,
  3.      membership_end DATE,
  4.      upfront BOOLEAN,
  5.      autorenew INT,
  6.      autorenew_forever BOOLEAN,
  7.      payment_gateway_id BIGINT,
  8.      price NUMERIC(14,2),
  9.      cancellation_fee NUMERIC(14,2),
  10.      cancelled TIMESTAMPTZ,
  11.      current_balance NUMERIC(14,2))
  12.     RETURNS TEXT AS $$
  13.     SELECT CASE WHEN membership_start > current_date THEN 'future_start'
  14.                 WHEN membership_end IS NOT NULL AND membership_end < current_date AND cancelled IS NOT NULL THEN 'cancelled'
  15.                 WHEN membership_end IS NOT NULL AND membership_end < current_date THEN 'expired'
  16.                 WHEN current_balance > 0 THEN 'dishonoured'
  17.                 WHEN upfront IS FALSE AND payment_gateway_id IS NULL AND price > 0 THEN 'gateway_unlinked'
  18.                 WHEN upfront IS TRUE THEN 'upfront'
  19.                 WHEN cancelled IS NOT NULL THEN 'cancellation_pending'
  20.                 WHEN cancellation_fee = 0 THEN 'month_to_month'
  21.                 WHEN autorenew > 0 THEN 'in_contract'
  22.                 WHEN autorenew_forever IS TRUE THEN 'month_to_month'
  23.                 WHEN upfront IS NULL THEN 'lead'
  24.                 ELSE 'unknown' END;
  25. $$ LANGUAGE sql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement