Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE OR REPLACE FUNCTION get_membership_status
- (membership_start DATE,
- membership_end DATE,
- upfront BOOLEAN,
- autorenew INT,
- autorenew_forever BOOLEAN,
- payment_gateway_id BIGINT,
- price NUMERIC(14,2),
- cancellation_fee NUMERIC(14,2),
- cancelled TIMESTAMPTZ,
- current_balance NUMERIC(14,2))
- RETURNS TEXT AS $$
- SELECT CASE WHEN membership_start > current_date THEN 'future_start'
- WHEN membership_end IS NOT NULL AND membership_end < current_date AND cancelled IS NOT NULL THEN 'cancelled'
- WHEN membership_end IS NOT NULL AND membership_end < current_date THEN 'expired'
- WHEN current_balance > 0 THEN 'dishonoured'
- WHEN upfront IS FALSE AND payment_gateway_id IS NULL AND price > 0 THEN 'gateway_unlinked'
- WHEN upfront IS TRUE THEN 'upfront'
- WHEN cancelled IS NOT NULL THEN 'cancellation_pending'
- WHEN cancellation_fee = 0 THEN 'month_to_month'
- WHEN autorenew > 0 THEN 'in_contract'
- WHEN autorenew_forever IS TRUE THEN 'month_to_month'
- WHEN upfront IS NULL THEN 'lead'
- ELSE 'unknown' END;
- $$ LANGUAGE sql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement