Commit da4943b1 authored by Birin Sanchez's avatar Birin Sanchez
Browse files

Downgrading invitees of a Family pack bundle honours the subscription


billing time of the Family pack owner. So from Family pack (annual) to
Complete (annual) and Family pack (monthly) to Complete (monthly).
Signed-off-by: Birin Sanchez's avatarBirin Sanchez <birin.sanchez@puri.sm>
parent 5a9b399d
Pipeline #24361 passed with stage
in 32 seconds
......@@ -156,38 +156,44 @@ def process_bundle_changes():
for sub in subs:
order_id = sub.get('parent_id', -1)
sub_status = sub.get('status', '')
sub_id = str(sub.get('id', 0))
# save old_sub for later if needed by downgrade_invitees
if sub_id == pending_change.old_subscription_id:
old_sub = parse_subscription(sub)
if (
order_id == int(pending_change.new_order_id)
and sub_status == 'active'
):
is_active = True
break
# if subscription is active and order is paid we process the
# bundle change
if is_paid and is_active:
set_subscription_status(pending_change.old_subscription_id,
'cancelled')
# save old subs renewal date for later
old_ren_date = pending_change.user.limit_set.first().renewal_date
forced_update(pending_change.user)
# if this is a Family pack downgrade deal with the
# invitees
if pending_change.is_family_downgrade():
downgrade_invitees(pending_change.user, old_ren_date)
downgrade_invitees(pending_change.user, old_sub)
pending_change.is_pending = False
pending_change.save()
def downgrade_invitees(user, renewal_date=None):
def downgrade_invitees(user, old_sub=None):
invs = user.get_invitees()
if renewal_date is None:
if old_sub is None:
ren_date = timezone.now() + timezone.timedelta(days=30)
b_period = 'monthly'
else:
ren_date = renewal_date
ren_date = old_sub['next_renewal']
b_period = old_sub['billing_period']
for inv in invs:
# Create Basic monthly subscription if the invitee has a
# recovery email
......@@ -206,7 +212,7 @@ def downgrade_invitees(user, renewal_date=None):
# create new Basic subs
order_info = create_user_subscription(
inv, bundle_type='COMPLETE', paid=True,
next_payment_date=ren_date
next_payment_date=ren_date, billing_period=b_period
)
if order_info.get('id', None) is not None:
forced_update(inv)
......@@ -316,8 +322,15 @@ def create_invitee_subscription(user):
except Invitation.DoesNotExist:
return result
renewal_date = invitation.owner.limit_set.first().renewal_date
result = create_user_subscription(user, bundle_type='INVITED', paid=True,
billing_email=user.email,
next_payment_date=renewal_date)
owner_subs = invitation.owner.get_woocommerce_subscriptions(
status='active')
if owner_subs:
p_sub = parse_subscription(owner_subs[0])
b_period = p_sub['billing_period']
renewal_date = p_sub['next_renewal']
result = create_user_subscription(user, bundle_type='INVITED',
paid=True, billing_email=user.email,
billing_period=b_period,
next_payment_date=renewal_date)
return result
......@@ -437,7 +437,7 @@ class User(AbstractUser):
orders = self.woo_get_json(
'orders?customer={}'.format(user_wc_id))
if len(orders) >= 1:
if orders is not None and len(orders) >= 1:
return orders
else:
return None
......@@ -454,7 +454,7 @@ class User(AbstractUser):
query = 'subscriptions?customer={}'.format(user_wc_id)
subscriptions = self.woo_get_json(query)
if len(subscriptions) >= 1:
if subscriptions is not None and len(subscriptions) >= 1:
return subscriptions
else:
return None
......@@ -575,10 +575,11 @@ class User(AbstractUser):
accounts it always return True.
"""
# Find Invitee subscription varitaion_id
# Find Invitee subscription varitaion_ids
inv_var_ids = []
for k, v in get_available_bundles().items():
if v['type'] == 'INVITED':
inv_var_id = k
inv_var_ids.append(k)
if self.account_type != AccountType.GROUP:
return True
......@@ -590,7 +591,7 @@ class User(AbstractUser):
return False
line_items = subs[0]['line_items']
var_id = str(line_items[0]['variation_id'])
if invitee.email == '' or var_id != inv_var_id:
if invitee.email == '' or var_id not in inv_var_ids:
return False
return True
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment