Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

corporate: Move renewal_amount to BillingSession framework.

Lauryn Menard484c0df07696
diff --git a/corporate/lib/analytics.py b/corporate/lib/analytics.py
index 0739f4ba78e..da43e0b9807 100644
--- a/corporate/lib/analytics.py
+++ b/corporate/lib/analytics.py
@@ -3,7 +3,7 @@ from typing import Any, Dict
from django.utils.timezone import now as timezone_now
-from corporate.lib.stripe import renewal_amount
+from corporate.lib.stripe import RealmBillingSession
from corporate.models import Customer, CustomerPlan
from zerver.lib.utils import assert_is_not_none
@@ -31,7 +31,9 @@ def estimate_annual_recurring_revenue_by_realm() -> Dict[str, int]: # nocoverag
if plan.customer.realm is not None:
# TODO: figure out what to do for plans that don't automatically
# renew, but which probably will renew
- renewal_cents = renewal_amount(plan, timezone_now())
+ renewal_cents = RealmBillingSession(
+ realm=plan.customer.realm
+ ).get_customer_plan_renewal_amount(plan, timezone_now())
if plan.billing_schedule == CustomerPlan.BILLING_SCHEDULE_MONTHLY:
renewal_cents *= 12
# TODO: Decimal stuff
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py
index 64a4e54d4fb..2f9a9f6dd69 100644
--- a/corporate/lib/stripe.py
+++ b/corporate/lib/stripe.py
@@ -310,28 +310,6 @@ def next_invoice_date(plan: CustomerPlan) -> Optional[datetime]:
return dt
-def renewal_amount(
- plan: CustomerPlan, event_time: datetime, last_ledger_entry: Optional[LicenseLedger] = None
-) -> int: # nocoverage: TODO
- if plan.fixed_price is not None:
- return plan.fixed_price
- new_plan = None
- if last_ledger_entry is None:
- realm = plan.customer.realm
- billing_session = RealmBillingSession(user=None, realm=realm)
- new_plan, last_ledger_entry = billing_session.make_end_of_cycle_updates_if_needed(
- plan, event_time
- )
- if last_ledger_entry is None:
- return 0
- if last_ledger_entry.licenses_at_next_renewal is None:
- return 0
- if new_plan is not None:
- plan = new_plan
- assert plan.price_per_license is not None # for mypy
- return plan.price_per_license * last_ledger_entry.licenses_at_next_renewal
-
-
def get_amount_to_credit_for_plan_tier_change(
current_plan: CustomerPlan, plan_change_date: datetime
) -> int:
@@ -1683,6 +1661,26 @@ class BillingSession(ABC):
).first()
return None
+ def get_customer_plan_renewal_amount(
+ self,
+ plan: CustomerPlan,
+ event_time: datetime,
+ last_ledger_entry: Optional[LicenseLedger] = None,
+ ) -> int:
+ if plan.fixed_price is not None:
+ return plan.fixed_price
+ new_plan = None
+ if last_ledger_entry is None:
+ new_plan, last_ledger_entry = self.make_end_of_cycle_updates_if_needed(plan, event_time)
+ if last_ledger_entry is None:
+ return 0 # nocoverage
+ if last_ledger_entry.licenses_at_next_renewal is None:
+ return 0 # nocoverage
+ if new_plan is not None:
+ plan = new_plan # nocoverage
+ assert plan.price_per_license is not None # for mypy
+ return plan.price_per_license * last_ledger_entry.licenses_at_next_renewal
+
def get_billing_context_from_plan(
self,
customer: Customer,
@@ -1732,7 +1730,7 @@ class BillingSession(ABC):
renewal_cents = monthly_price_per_license * licenses_at_next_renewal
price_per_license = format_money(monthly_price_per_license)
else:
- renewal_cents = renewal_amount(plan, now, last_ledger_entry)
+ renewal_cents = self.get_customer_plan_renewal_amount(plan, now, last_ledger_entry)
if plan.price_per_license is None:
price_per_license = ""