From 87c1b9e3bc136b306c3f7e9e86ef2793ad29481e Mon Sep 17 00:00:00 2001 From: Vishnu KS Date: Mon, 20 Sep 2021 11:29:36 +0000 Subject: [PATCH] billing: Simplify start_of_next_billing_cycle function. --- corporate/lib/stripe.py | 15 ++++++++------- corporate/views/billing_page.py | 11 ++++++++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 2b3bcfb136..8dfb37ab7f 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -140,10 +140,6 @@ def next_month(billing_cycle_anchor: datetime, dt: datetime) -> datetime: def start_of_next_billing_cycle(plan: CustomerPlan, event_time: datetime) -> datetime: - if plan.is_free_trial(): - assert plan.next_invoice_date is not None # for mypy - return plan.next_invoice_date - months_per_period = { CustomerPlan.ANNUAL: 12, CustomerPlan.MONTHLY: 1, @@ -379,7 +375,13 @@ def make_end_of_cycle_updates_if_needed( ) assert last_ledger_renewal is not None last_renewal = last_ledger_renewal.event_time - next_billing_cycle = start_of_next_billing_cycle(plan, last_renewal) + + if plan.is_free_trial(): + assert plan.next_invoice_date is not None + next_billing_cycle = plan.next_invoice_date + else: + next_billing_cycle = start_of_next_billing_cycle(plan, last_renewal) + if next_billing_cycle <= event_time and last_ledger_entry is not None: licenses_at_next_renewal = last_ledger_entry.licenses_at_next_renewal assert licenses_at_next_renewal is not None @@ -393,8 +395,7 @@ def make_end_of_cycle_updates_if_needed( ) 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) + plan.billing_cycle_anchor = next_billing_cycle.replace(microsecond=0) plan.status = CustomerPlan.ACTIVE plan.save(update_fields=["invoiced_through", "billing_cycle_anchor", "status"]) return None, LicenseLedger.objects.create( diff --git a/corporate/views/billing_page.py b/corporate/views/billing_page.py index 66ba027009..06d9a44ac2 100644 --- a/corporate/views/billing_page.py +++ b/corporate/views/billing_page.py @@ -113,9 +113,14 @@ def billing_home( seat_count = get_latest_seat_count(user.realm) # Should do this in javascript, using the user's timezone - renewal_date = "{dt:%B} {dt.day}, {dt.year}".format( - dt=start_of_next_billing_cycle(plan, now) - ) + if plan.is_free_trial(): + assert plan.next_invoice_date is not None + renewal_date = "{dt:%B} {dt.day}, {dt.year}".format(dt=plan.next_invoice_date) + else: + renewal_date = "{dt:%B} {dt.day}, {dt.year}".format( + dt=start_of_next_billing_cycle(plan, now) + ) + renewal_cents = renewal_amount(plan, now) charge_automatically = plan.charge_automatically assert customer.stripe_customer_id is not None # for mypy