activity: Show effective rate of realms in /activity page.

This commit is contained in:
Vishnu KS 2021-06-09 17:16:12 +05:30 committed by Tim Abbott
parent 0b4338b998
commit 40ab415005
5 changed files with 54 additions and 5 deletions

View File

@ -80,10 +80,12 @@ if settings.BILLING_ENABLED:
attach_discount_to_realm,
downgrade_at_the_end_of_billing_cycle,
downgrade_now_without_creating_additional_invoices,
estimate_annual_recurring_revenue_by_realm,
get_current_plan_by_realm,
get_customer_by_realm,
get_discount_for_realm,
get_latest_seat_count,
get_realms_to_default_discount_dict,
make_end_of_cycle_updates_if_needed,
update_billing_method_of_current_plan,
update_sponsorship_status,
@ -761,12 +763,24 @@ def realm_summary_table(realm_minutes: Dict[str, float]) -> str:
# estimate annual subscription revenue
total_amount = 0
if settings.BILLING_ENABLED:
from corporate.lib.stripe import estimate_annual_recurring_revenue_by_realm
estimated_arrs = estimate_annual_recurring_revenue_by_realm()
realms_to_default_discount = get_realms_to_default_discount_dict()
for row in rows:
if row["string_id"] in estimated_arrs:
row["amount"] = estimated_arrs[row["string_id"]]
string_id = row["string_id"]
if string_id in estimated_arrs:
row["amount"] = estimated_arrs[string_id]
if row["plan_type"] == Realm.STANDARD:
row["effective_rate"] = 100 - int(realms_to_default_discount.get(string_id, 0))
elif row["plan_type"] == Realm.STANDARD_FREE:
row["effective_rate"] = 0
elif row["plan_type"] == Realm.LIMITED and string_id in realms_to_default_discount:
row["effective_rate"] = 100 - int(realms_to_default_discount[string_id])
else:
row["effective_rate"] = ""
total_amount += sum(estimated_arrs.values())
# augment data with realm_minutes
@ -808,6 +822,7 @@ def realm_summary_table(realm_minutes: Dict[str, float]) -> str:
string_id="Total",
plan_type_string="",
amount=total_amount,
effective_rate="",
stats_link="",
date_created_day="",
realm_owner_emails="",

View File

@ -895,6 +895,14 @@ def estimate_annual_recurring_revenue_by_realm() -> Dict[str, int]: # nocoverag
return annual_revenue
def get_realms_to_default_discount_dict() -> Dict[str, Decimal]:
realms_to_default_discount = {}
customers = Customer.objects.exclude(default_discount=None).exclude(default_discount=0)
for customer in customers:
realms_to_default_discount[customer.realm.string_id] = customer.default_discount
return realms_to_default_discount
# During realm deactivation we instantly downgrade the plan to Limited.
# Extra users added in the final month are not charged. Also used
# for the cancellation of Free Trial.

View File

@ -32,6 +32,7 @@ from corporate.lib.stripe import (
get_discount_for_realm,
get_latest_seat_count,
get_price_per_license,
get_realms_to_default_discount_dict,
invoice_plan,
invoice_plans_as_needed,
make_end_of_cycle_updates_if_needed,
@ -2950,6 +2951,24 @@ class BillingHelpersTest(ZulipTestCase):
)
self.assertEqual(get_current_plan_by_realm(realm), plan)
def test_get_realms_to_default_discount_dict(self) -> None:
Customer.objects.create(realm=get_realm("zulip"), stripe_customer_id="cus_1")
lear_customer = Customer.objects.create(realm=get_realm("lear"), stripe_customer_id="cus_2")
lear_customer.default_discount = 30
lear_customer.save(update_fields=["default_discount"])
zephyr_customer = Customer.objects.create(
realm=get_realm("zephyr"), stripe_customer_id="cus_3"
)
zephyr_customer.default_discount = 0
zephyr_customer.save(update_fields=["default_discount"])
self.assertEqual(
get_realms_to_default_discount_dict(),
{
"lear": Decimal("30.0000"),
},
)
class LicenseLedgerTest(StripeTestCase):
def test_add_plan_renewal_if_needed(self) -> None:

View File

@ -24,6 +24,7 @@
<th>Created (green if ≤12wk)</th>
<th>Plan type</th>
<th>ARR</th>
<th>Effective rate (%)</th>
<th></th>
<th>DAU</th>
<th>WAU</th>
@ -60,6 +61,12 @@
{% endif %}
</td>
<td class="number">
{% if row.effective_rate != "" %}
{{ row.effective_rate }}
{% endif %}
</td>
<td>
{% if not loop.first %}
<a class="envelope-link" data-admin-emails="{{ row.realm_owner_emails }}">

View File

@ -52,7 +52,7 @@ class ActivityTest(ZulipTestCase):
result = self.client_get("/activity")
self.assertEqual(result.status_code, 200)
self.assert_length(queries, 18)
self.assert_length(queries, 19)
flush_per_request_caches()
with queries_captured() as queries: