billing: Simplify start_of_next_billing_cycle function.

This commit is contained in:
Vishnu KS 2021-09-20 11:29:36 +00:00 committed by Tim Abbott
parent aff52722a7
commit 87c1b9e3bc
2 changed files with 16 additions and 10 deletions

View File

@ -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
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(

View File

@ -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
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