billing: Rename add_plan_renewal_to_license_ledger_if_needed.

This commit is contained in:
Rishi Gupta 2019-04-10 15:24:45 -07:00
parent 03d21c6317
commit a529080f01
3 changed files with 10 additions and 10 deletions

View File

@ -113,7 +113,7 @@ def next_invoice_date(plan: CustomerPlan) -> datetime:
def renewal_amount(plan: CustomerPlan, event_time: datetime) -> int: # nocoverage: TODO
if plan.fixed_price is not None:
return plan.fixed_price
last_ledger_entry = add_plan_renewal_to_license_ledger_if_needed(plan, event_time)
last_ledger_entry = make_end_of_cycle_updates_if_needed(plan, event_time)
if last_ledger_entry.licenses_at_next_renewal is None:
return 0
assert(plan.price_per_license is not None) # for mypy
@ -216,7 +216,7 @@ def do_replace_payment_source(user: UserProfile, stripe_token: str,
# event_time should roughly be timezone_now(). Not designed to handle
# event_times in the past or future
# TODO handle downgrade
def add_plan_renewal_to_license_ledger_if_needed(plan: CustomerPlan, event_time: datetime) -> LicenseLedger:
def make_end_of_cycle_updates_if_needed(plan: CustomerPlan, event_time: datetime) -> LicenseLedger:
last_ledger_entry = LicenseLedger.objects.filter(plan=plan).order_by('-id').first()
last_renewal = LicenseLedger.objects.filter(plan=plan, is_renewal=True) \
.order_by('-id').first().event_time
@ -361,7 +361,7 @@ def process_initial_upgrade(user: UserProfile, licenses: int, automanage_license
def update_license_ledger_for_automanaged_plan(realm: Realm, plan: CustomerPlan,
event_time: datetime) -> None:
last_ledger_entry = add_plan_renewal_to_license_ledger_if_needed(plan, event_time)
last_ledger_entry = make_end_of_cycle_updates_if_needed(plan, event_time)
# todo: handle downgrade, where licenses_at_next_renewal should be 0
licenses_at_next_renewal = get_seat_count(realm)
licenses = max(licenses_at_next_renewal, last_ledger_entry.licenses)
@ -383,7 +383,7 @@ def update_license_ledger_if_needed(realm: Realm, event_time: datetime) -> None:
def invoice_plan(plan: CustomerPlan, event_time: datetime) -> None:
if plan.invoicing_status == CustomerPlan.STARTED:
raise NotImplementedError('Plan with invoicing_status==STARTED needs manual resolution.')
add_plan_renewal_to_license_ledger_if_needed(plan, event_time)
make_end_of_cycle_updates_if_needed(plan, event_time)
assert(plan.invoiced_through is not None)
licenses_base = plan.invoiced_through.licenses
invoice_item_created = False

View File

@ -28,7 +28,7 @@ from corporate.lib.stripe import catch_stripe_errors, attach_discount_to_realm,
MIN_INVOICED_LICENSES, \
add_months, next_month, \
compute_plan_parameters, update_or_create_stripe_customer, \
process_initial_upgrade, add_plan_renewal_to_license_ledger_if_needed, \
process_initial_upgrade, make_end_of_cycle_updates_if_needed, \
update_license_ledger_if_needed, update_license_ledger_for_automanaged_plan, \
invoice_plan, invoice_plans_as_needed, get_discount_for_realm
from corporate.models import Customer, CustomerPlan, LicenseLedger
@ -1010,11 +1010,11 @@ class LicenseLedgerTest(StripeTestCase):
self.assertEqual(LicenseLedger.objects.count(), 1)
plan = CustomerPlan.objects.get()
# Plan hasn't renewed yet
add_plan_renewal_to_license_ledger_if_needed(plan, self.next_year - timedelta(days=1))
make_end_of_cycle_updates_if_needed(plan, self.next_year - timedelta(days=1))
self.assertEqual(LicenseLedger.objects.count(), 1)
# Plan needs to renew
# TODO: do_deactivate_user for a user, so that licenses_at_next_renewal != licenses
ledger_entry = add_plan_renewal_to_license_ledger_if_needed(plan, self.next_year)
ledger_entry = make_end_of_cycle_updates_if_needed(plan, self.next_year)
self.assertEqual(LicenseLedger.objects.count(), 2)
ledger_params = {
'plan': plan, 'is_renewal': True, 'event_time': self.next_year,
@ -1022,7 +1022,7 @@ class LicenseLedgerTest(StripeTestCase):
for key, value in ledger_params.items():
self.assertEqual(getattr(ledger_entry, key), value)
# Plan needs to renew, but we already added the plan_renewal ledger entry
add_plan_renewal_to_license_ledger_if_needed(plan, self.next_year + timedelta(days=1))
make_end_of_cycle_updates_if_needed(plan, self.next_year + timedelta(days=1))
self.assertEqual(LicenseLedger.objects.count(), 2)
def test_update_license_ledger_if_needed(self) -> None:

View File

@ -22,7 +22,7 @@ from corporate.lib.stripe import STRIPE_PUBLISHABLE_KEY, \
unsign_string, BillingError, process_downgrade, do_replace_payment_source, \
MIN_INVOICED_LICENSES, DEFAULT_INVOICE_DAYS_UNTIL_DUE, \
start_of_next_billing_cycle, renewal_amount, \
add_plan_renewal_to_license_ledger_if_needed
make_end_of_cycle_updates_if_needed
from corporate.models import Customer, CustomerPlan, \
get_current_plan
@ -168,7 +168,7 @@ def billing_home(request: HttpRequest) -> HttpResponse:
CustomerPlan.PLUS: 'Zulip Plus',
}[plan.tier]
now = timezone_now()
last_ledger_entry = add_plan_renewal_to_license_ledger_if_needed(plan, now)
last_ledger_entry = make_end_of_cycle_updates_if_needed(plan, now)
licenses = last_ledger_entry.licenses
licenses_used = get_seat_count(user.realm)
# Should do this in javascript, using the user's timezone