From 06b5f9feaeaa0a947488ddb45b4f26c022dfbfdf Mon Sep 17 00:00:00 2001 From: Vishnu KS Date: Wed, 11 Nov 2020 18:32:47 +0530 Subject: [PATCH] billing: Create is_free_trial function in CustomerPlan model. --- corporate/lib/stripe.py | 4 ++-- corporate/models.py | 3 +++ corporate/views.py | 5 ++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index e0136259fa..6121936860 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -136,7 +136,7 @@ def next_month(billing_cycle_anchor: datetime, dt: datetime) -> datetime: def start_of_next_billing_cycle(plan: CustomerPlan, event_time: datetime) -> datetime: - if plan.status == CustomerPlan.FREE_TRIAL: + if plan.is_free_trial(): assert plan.next_invoice_date is not None # for mypy return plan.next_invoice_date @@ -358,7 +358,7 @@ def make_end_of_cycle_updates_if_needed( licenses=last_ledger_entry.licenses_at_next_renewal, licenses_at_next_renewal=last_ledger_entry.licenses_at_next_renewal, ) - if plan.status == CustomerPlan.FREE_TRIAL: + if plan.is_free_trial(): plan.invoiced_through = last_ledger_entry assert plan.next_invoice_date is not None plan.billing_cycle_anchor = plan.next_invoice_date.replace(microsecond=0) diff --git a/corporate/models.py b/corporate/models.py index 863488db95..e54aa016b4 100644 --- a/corporate/models.py +++ b/corporate/models.py @@ -117,6 +117,9 @@ class CustomerPlan(models.Model): LicenseLedger.objects.filter(plan=self).order_by("id").last().licenses_at_next_renewal ) + def is_free_trial(self) -> bool: + return self.status == CustomerPlan.FREE_TRIAL + def get_current_plan_by_customer(customer: Customer) -> Optional[CustomerPlan]: return CustomerPlan.objects.filter( diff --git a/corporate/views.py b/corporate/views.py index e0d3040b77..4c81790126 100644 --- a/corporate/views.py +++ b/corporate/views.py @@ -300,7 +300,6 @@ def billing_home(request: HttpRequest) -> HttpResponse: if new_plan is not None: # nocoverage plan = new_plan assert plan is not None # for mypy - free_trial = plan.status == CustomerPlan.FREE_TRIAL downgrade_at_end_of_cycle = plan.status == CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE switch_to_annual_at_end_of_cycle = ( plan.status == CustomerPlan.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE @@ -324,7 +323,7 @@ def billing_home(request: HttpRequest) -> HttpResponse: context.update( plan_name=plan.name, has_active_plan=True, - free_trial=free_trial, + free_trial=plan.is_free_trial(), downgrade_at_end_of_cycle=downgrade_at_end_of_cycle, automanage_licenses=plan.automanage_licenses, switch_to_annual_at_end_of_cycle=switch_to_annual_at_end_of_cycle, @@ -391,7 +390,7 @@ def update_plan( assert plan.fixed_price is None do_change_plan_status(plan, status) elif status == CustomerPlan.ENDED: - assert plan.status == CustomerPlan.FREE_TRIAL + assert plan.is_free_trial() downgrade_now_without_creating_additional_invoices(user.realm) return json_success()