Skip to content

Commit

Permalink
Fixed should_be_billed for canceled subscriptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanpetrea committed Aug 10, 2018
1 parent a0c83d8 commit 3b718bb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
11 changes: 9 additions & 2 deletions silver/models/subscriptions.py
Expand Up @@ -498,12 +498,19 @@ def should_be_billed(self, billing_date, generate_documents_datetime=None):
if generate_documents_datetime < cycle_start_datetime + generate_after:
return False

plan_billed_up_to = self.billed_up_to_dates['plan_billed_up_to']
billed_up_to_dates = self.billed_up_to_dates
plan_billed_up_to = billed_up_to_dates['plan_billed_up_to']
metered_features_billed_up_to = billed_up_to_dates['metered_features_billed_up_to']

# We want to bill the subscription if the plan hasn't been billed for this cycle or
# if the subscription has been canceled and the plan won't be billed for this cycle.
if self.prebill_plan or self.state == self.STATES.CANCELED:
return plan_billed_up_to < cycle_start_date
plan_should_be_billed = plan_billed_up_to < cycle_start_date

if self.state == self.STATES.CANCELED:
return metered_features_billed_up_to < cycle_start_date or plan_should_be_billed

return plan_should_be_billed

# wait until the cycle that is going to be billed ends:
billed_cycle_end_date = self.cycle_end_date(plan_billed_up_to + ONE_DAY)
Expand Down
71 changes: 71 additions & 0 deletions silver/tests/unit/test_subscription.py
Expand Up @@ -738,6 +738,77 @@ def test_already_billed_sub_wa_cb(self):
assert subscription.should_be_billed(incorrect_billing_date_2) is False
assert subscription.should_be_billed(incorrect_billing_date_3) is False

def test_canceled_sub_with_billed_plan_but_not_metered_features_1(self):
plan = PlanFactory.create(generate_after=100,
interval=Plan.INTERVALS.MONTH,
interval_count=1)
subscription = SubscriptionFactory.create(
plan=plan,
state=Subscription.STATES.CANCELED,
start_date=datetime.date(2015, 1, 1),
cancel_date=datetime.date(2018, 1, 20)
)

billing_log = BillingLog.objects.create(
subscription=subscription,
billing_date=datetime.date(2015, 9, 2),
plan_billed_up_to=datetime.date(2018, 1, 31),
metered_features_billed_up_to=datetime.date(2017, 12, 31)
)

correct_billing_date_1 = subscription.cancel_date + datetime.timedelta(days=1)
correct_billing_date_2 = datetime.date(2018, 2, 1)
incorrect_billing_date = correct_billing_date_1 - datetime.timedelta(days=1)

assert \
billing_log.metered_features_billed_up_to \
< incorrect_billing_date \
<= subscription.cancel_date \
< correct_billing_date_1 \
< billing_log.plan_billed_up_to \
< correct_billing_date_2 \

assert subscription.should_be_billed(correct_billing_date_1)
assert subscription.should_be_billed(correct_billing_date_2)
assert not subscription.should_be_billed(incorrect_billing_date)

def test_canceled_sub_with_billed_plan_but_not_metered_features_2(self):
# Like previous test, but this time there's cycle_billing_duration added to the mix
plan = PlanFactory.create(generate_after=100,
interval=Plan.INTERVALS.MONTH,
interval_count=1,
cycle_billing_duration=datetime.timedelta(days=5))

subscription = SubscriptionFactory.create(
plan=plan,
state=Subscription.STATES.CANCELED,
start_date=datetime.date(2015, 1, 1),
cancel_date=datetime.date(2018, 1, 20)
)

billing_log = BillingLog.objects.create(
subscription=subscription,
billing_date=datetime.date(2015, 9, 2),
plan_billed_up_to=datetime.date(2018, 1, 31),
metered_features_billed_up_to=datetime.date(2017, 12, 31)
)

correct_billing_date = datetime.date(2018, 2, 1)
incorrect_billing_date_1 = subscription.cancel_date + datetime.timedelta(days=1)
incorrect_billing_date_2 = correct_billing_date - datetime.timedelta(days=1)

assert \
billing_log.metered_features_billed_up_to \
< subscription.cancel_date \
< incorrect_billing_date_1 \
< incorrect_billing_date_2 \
<= billing_log.plan_billed_up_to \
< correct_billing_date

assert subscription.should_be_billed(correct_billing_date)
assert not subscription.should_be_billed(incorrect_billing_date_1)
assert not subscription.should_be_billed(incorrect_billing_date_2)

@freeze_time('2015-01-01')
def test_updateable_buckets_active_subscription(self):
plan = PlanFactory.create(generate_after=24 * 60,
Expand Down

0 comments on commit 3b718bb

Please sign in to comment.