2023-12-02 17:55:39 +01:00
|
|
|
from dataclasses import dataclass
|
2024-01-05 22:22:44 +01:00
|
|
|
from datetime import datetime, timedelta
|
2023-10-31 19:22:55 +01:00
|
|
|
from decimal import Decimal
|
2024-01-17 13:55:25 +01:00
|
|
|
from typing import Optional, TypedDict, Union
|
2021-09-29 19:51:55 +02:00
|
|
|
from urllib.parse import urlencode, urljoin, urlunsplit
|
|
|
|
|
|
|
|
from django.conf import settings
|
2024-01-05 22:22:44 +01:00
|
|
|
from django.db.models import Sum
|
2021-09-29 19:51:55 +02:00
|
|
|
from django.urls import reverse
|
2023-12-02 17:55:39 +01:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2021-09-29 19:51:55 +02:00
|
|
|
|
2023-12-15 18:04:08 +01:00
|
|
|
from corporate.lib.stripe import (
|
|
|
|
BillingSession,
|
|
|
|
RemoteRealmBillingSession,
|
|
|
|
RemoteServerBillingSession,
|
2024-01-31 14:10:45 +01:00
|
|
|
get_configured_fixed_price_plan_offer,
|
2024-01-19 19:40:37 +01:00
|
|
|
start_of_next_billing_cycle,
|
2023-12-15 18:04:08 +01:00
|
|
|
)
|
2023-12-13 18:23:05 +01:00
|
|
|
from corporate.models import (
|
|
|
|
Customer,
|
|
|
|
CustomerPlan,
|
2024-01-17 13:55:25 +01:00
|
|
|
CustomerPlanOffer,
|
2023-12-13 18:23:05 +01:00
|
|
|
ZulipSponsorshipRequest,
|
|
|
|
get_current_plan_by_customer,
|
|
|
|
)
|
2023-12-15 02:14:24 +01:00
|
|
|
from zerver.models import Realm
|
|
|
|
from zerver.models.realms import get_org_type_display_name, get_realm
|
2023-12-06 18:16:20 +01:00
|
|
|
from zilencer.lib.remote_counts import MissingDataError
|
2023-12-15 18:04:08 +01:00
|
|
|
from zilencer.models import (
|
|
|
|
RemoteCustomerUserCount,
|
2024-01-05 22:22:44 +01:00
|
|
|
RemoteInstallationCount,
|
|
|
|
RemotePushDeviceToken,
|
|
|
|
RemoteRealmCount,
|
2023-12-28 19:28:36 +01:00
|
|
|
RemoteZulipServerAuditLog,
|
2023-12-15 18:04:08 +01:00
|
|
|
get_remote_realm_guest_and_non_guest_count,
|
|
|
|
get_remote_server_guest_and_non_guest_count,
|
|
|
|
)
|
2021-09-29 19:51:55 +02:00
|
|
|
|
|
|
|
|
2023-12-13 18:23:05 +01:00
|
|
|
class SponsorshipRequestDict(TypedDict):
|
|
|
|
org_type: str
|
|
|
|
org_website: str
|
|
|
|
org_description: str
|
|
|
|
total_users: str
|
|
|
|
paid_users: str
|
|
|
|
paid_users_description: str
|
|
|
|
requested_plan: str
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SponsorshipData:
|
|
|
|
sponsorship_pending: bool = False
|
|
|
|
default_discount: Optional[Decimal] = None
|
2023-12-14 19:55:38 +01:00
|
|
|
minimum_licenses: Optional[int] = None
|
2024-01-10 17:20:08 +01:00
|
|
|
required_plan_tier: Optional[int] = None
|
2023-12-13 18:23:05 +01:00
|
|
|
latest_sponsorship_request: Optional[SponsorshipRequestDict] = None
|
|
|
|
|
|
|
|
|
2023-12-02 17:55:39 +01:00
|
|
|
@dataclass
|
|
|
|
class PlanData:
|
|
|
|
customer: Optional["Customer"] = None
|
|
|
|
current_plan: Optional["CustomerPlan"] = None
|
2024-01-17 13:55:25 +01:00
|
|
|
next_plan: Union["CustomerPlan", "CustomerPlanOffer", None] = None
|
2023-12-02 17:55:39 +01:00
|
|
|
licenses: Optional[int] = None
|
|
|
|
licenses_used: Optional[int] = None
|
2024-01-19 19:40:37 +01:00
|
|
|
next_billing_cycle_start: Optional[datetime] = None
|
2023-12-06 18:09:34 +01:00
|
|
|
is_legacy_plan: bool = False
|
2023-12-11 17:09:36 +01:00
|
|
|
has_fixed_price: bool = False
|
2023-12-06 18:16:20 +01:00
|
|
|
warning: Optional[str] = None
|
2023-12-18 18:52:19 +01:00
|
|
|
annual_recurring_revenue: Optional[int] = None
|
2023-12-21 20:53:54 +01:00
|
|
|
estimated_next_plan_revenue: Optional[int] = None
|
2023-12-02 17:55:39 +01:00
|
|
|
|
|
|
|
|
2024-01-05 22:22:44 +01:00
|
|
|
@dataclass
|
|
|
|
class MobilePushData:
|
|
|
|
mobile_users: Optional[int] = None
|
|
|
|
mobile_pushes_forwarded: Optional[int] = None
|
|
|
|
|
|
|
|
|
2023-12-13 18:23:05 +01:00
|
|
|
@dataclass
|
|
|
|
class SupportData:
|
2023-12-28 19:28:36 +01:00
|
|
|
date_created: datetime
|
2023-12-13 18:23:05 +01:00
|
|
|
plan_data: PlanData
|
|
|
|
sponsorship_data: SponsorshipData
|
2023-12-15 18:04:08 +01:00
|
|
|
user_data: RemoteCustomerUserCount
|
2024-01-05 22:22:44 +01:00
|
|
|
mobile_push_data: MobilePushData
|
2023-12-13 18:23:05 +01:00
|
|
|
|
|
|
|
|
2023-12-06 15:00:23 +01:00
|
|
|
def get_realm_support_url(realm: Realm) -> str:
|
2021-09-29 19:51:55 +02:00
|
|
|
support_realm_uri = get_realm(settings.STAFF_SUBDOMAIN).uri
|
|
|
|
support_url = urljoin(
|
|
|
|
support_realm_uri,
|
|
|
|
urlunsplit(("", "", reverse("support"), urlencode({"q": realm.string_id}), "")),
|
|
|
|
)
|
|
|
|
return support_url
|
2023-10-31 19:22:55 +01:00
|
|
|
|
|
|
|
|
2023-12-02 17:45:25 +01:00
|
|
|
def get_customer_discount_for_support_view(
|
|
|
|
customer: Optional[Customer] = None,
|
|
|
|
) -> Optional[Decimal]:
|
|
|
|
if customer is None:
|
|
|
|
return None
|
|
|
|
return customer.default_discount
|
2023-12-02 17:55:39 +01:00
|
|
|
|
|
|
|
|
2023-12-13 18:23:05 +01:00
|
|
|
def get_customer_sponsorship_data(customer: Customer) -> SponsorshipData:
|
|
|
|
pending = customer.sponsorship_pending
|
|
|
|
discount = customer.default_discount
|
2023-12-14 19:55:38 +01:00
|
|
|
licenses = customer.minimum_licenses
|
2024-01-10 17:20:08 +01:00
|
|
|
plan_tier = customer.required_plan_tier
|
2023-12-13 18:23:05 +01:00
|
|
|
sponsorship_request = None
|
|
|
|
if pending:
|
|
|
|
last_sponsorship_request = (
|
|
|
|
ZulipSponsorshipRequest.objects.filter(customer=customer).order_by("id").last()
|
|
|
|
)
|
|
|
|
if last_sponsorship_request is not None:
|
|
|
|
org_type_name = get_org_type_display_name(last_sponsorship_request.org_type)
|
|
|
|
if (
|
|
|
|
last_sponsorship_request.org_website is None
|
|
|
|
or last_sponsorship_request.org_website == ""
|
|
|
|
):
|
|
|
|
website = "No website submitted"
|
|
|
|
else:
|
|
|
|
website = last_sponsorship_request.org_website
|
|
|
|
sponsorship_request = SponsorshipRequestDict(
|
|
|
|
org_type=org_type_name,
|
|
|
|
org_website=website,
|
|
|
|
org_description=last_sponsorship_request.org_description,
|
|
|
|
total_users=last_sponsorship_request.expected_total_users,
|
|
|
|
paid_users=last_sponsorship_request.paid_users_count,
|
|
|
|
paid_users_description=last_sponsorship_request.paid_users_description,
|
|
|
|
requested_plan=last_sponsorship_request.requested_plan,
|
|
|
|
)
|
|
|
|
|
|
|
|
return SponsorshipData(
|
|
|
|
sponsorship_pending=pending,
|
|
|
|
default_discount=discount,
|
2023-12-14 19:55:38 +01:00
|
|
|
minimum_licenses=licenses,
|
2024-01-10 17:20:08 +01:00
|
|
|
required_plan_tier=plan_tier,
|
2023-12-13 18:23:05 +01:00
|
|
|
latest_sponsorship_request=sponsorship_request,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-21 20:53:54 +01:00
|
|
|
def get_annual_invoice_count(billing_schedule: int) -> int:
|
|
|
|
if billing_schedule == CustomerPlan.BILLING_SCHEDULE_MONTHLY:
|
|
|
|
return 12
|
|
|
|
else:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
2023-12-02 17:55:39 +01:00
|
|
|
def get_current_plan_data_for_support_view(billing_session: BillingSession) -> PlanData:
|
|
|
|
customer = billing_session.get_customer()
|
|
|
|
plan = None
|
|
|
|
if customer is not None:
|
|
|
|
plan = get_current_plan_by_customer(customer)
|
|
|
|
plan_data = PlanData(
|
|
|
|
customer=customer,
|
|
|
|
current_plan=plan,
|
|
|
|
)
|
2024-01-31 14:10:45 +01:00
|
|
|
|
|
|
|
# A customer with or without a current plan can have a fixed_price next plan configured.
|
|
|
|
if customer and customer.required_plan_tier:
|
|
|
|
plan_data.next_plan = get_configured_fixed_price_plan_offer(
|
|
|
|
customer, customer.required_plan_tier
|
|
|
|
)
|
|
|
|
if plan_data.next_plan:
|
|
|
|
plan_data.estimated_next_plan_revenue = plan_data.next_plan.fixed_price
|
|
|
|
|
2023-12-02 17:55:39 +01:00
|
|
|
if plan is not None:
|
|
|
|
new_plan, last_ledger_entry = billing_session.make_end_of_cycle_updates_if_needed(
|
|
|
|
plan, timezone_now()
|
|
|
|
)
|
|
|
|
if last_ledger_entry is not None:
|
|
|
|
if new_plan is not None:
|
|
|
|
plan_data.current_plan = new_plan # nocoverage
|
|
|
|
plan_data.licenses = last_ledger_entry.licenses
|
2023-12-06 18:16:20 +01:00
|
|
|
try:
|
|
|
|
plan_data.licenses_used = billing_session.current_count_for_billed_licenses()
|
|
|
|
except MissingDataError: # nocoverage
|
2024-01-05 22:18:41 +01:00
|
|
|
plan_data.warning = (
|
|
|
|
"Recent audit log data missing: No information for used licenses"
|
|
|
|
)
|
2023-12-06 18:09:34 +01:00
|
|
|
assert plan_data.current_plan is not None # for mypy
|
2023-12-21 20:53:54 +01:00
|
|
|
|
2024-01-31 14:10:45 +01:00
|
|
|
if plan_data.next_plan is None:
|
|
|
|
plan_data.next_plan = billing_session.get_next_plan(plan_data.current_plan)
|
2023-12-21 20:53:54 +01:00
|
|
|
|
|
|
|
if plan_data.next_plan is not None:
|
|
|
|
if plan_data.next_plan.fixed_price is not None: # nocoverage
|
|
|
|
plan_data.estimated_next_plan_revenue = plan_data.next_plan.fixed_price
|
|
|
|
elif plan_data.current_plan.licenses_at_next_renewal() is not None:
|
|
|
|
next_plan_licenses = plan_data.current_plan.licenses_at_next_renewal()
|
|
|
|
assert next_plan_licenses is not None
|
2024-01-17 13:55:25 +01:00
|
|
|
assert type(plan_data.next_plan) is CustomerPlan
|
2023-12-21 20:53:54 +01:00
|
|
|
assert plan_data.next_plan.price_per_license is not None
|
|
|
|
invoice_count = get_annual_invoice_count(plan_data.next_plan.billing_schedule)
|
|
|
|
plan_data.estimated_next_plan_revenue = (
|
|
|
|
plan_data.next_plan.price_per_license * next_plan_licenses * invoice_count
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
plan_data.estimated_next_plan_revenue = 0 # nocoverage
|
|
|
|
|
2024-01-19 19:40:37 +01:00
|
|
|
if plan_data.current_plan.status in (
|
|
|
|
CustomerPlan.FREE_TRIAL,
|
|
|
|
CustomerPlan.DOWNGRADE_AT_END_OF_FREE_TRIAL,
|
|
|
|
): # nocoverage
|
|
|
|
assert plan_data.current_plan.next_invoice_date is not None
|
|
|
|
plan_data.next_billing_cycle_start = plan_data.current_plan.next_invoice_date
|
|
|
|
else:
|
|
|
|
plan_data.next_billing_cycle_start = start_of_next_billing_cycle(
|
|
|
|
plan_data.current_plan, timezone_now()
|
|
|
|
)
|
|
|
|
|
2023-12-06 18:09:34 +01:00
|
|
|
plan_data.is_legacy_plan = (
|
|
|
|
plan_data.current_plan.tier == CustomerPlan.TIER_SELF_HOSTED_LEGACY
|
|
|
|
)
|
2023-12-11 17:09:36 +01:00
|
|
|
plan_data.has_fixed_price = plan_data.current_plan.fixed_price is not None
|
2023-12-21 20:53:54 +01:00
|
|
|
annual_invoice_count = get_annual_invoice_count(plan_data.current_plan.billing_schedule)
|
2024-01-22 17:12:11 +01:00
|
|
|
if last_ledger_entry is not None:
|
|
|
|
plan_data.annual_recurring_revenue = (
|
|
|
|
billing_session.get_customer_plan_renewal_amount(
|
|
|
|
plan_data.current_plan, last_ledger_entry
|
|
|
|
)
|
|
|
|
* annual_invoice_count
|
2023-12-21 20:53:54 +01:00
|
|
|
)
|
2024-01-22 17:12:11 +01:00
|
|
|
else:
|
|
|
|
plan_data.annual_recurring_revenue = 0 # nocoverage
|
2023-12-02 17:55:39 +01:00
|
|
|
|
|
|
|
return plan_data
|
2023-12-13 18:23:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_data_for_support_view(billing_session: BillingSession) -> SupportData:
|
2023-12-15 18:04:08 +01:00
|
|
|
if isinstance(billing_session, RemoteServerBillingSession):
|
|
|
|
user_data = get_remote_server_guest_and_non_guest_count(billing_session.remote_server.id)
|
2023-12-28 19:28:36 +01:00
|
|
|
date_created = RemoteZulipServerAuditLog.objects.get(
|
|
|
|
event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED,
|
|
|
|
server__id=billing_session.remote_server.id,
|
|
|
|
).event_time
|
2024-01-05 22:22:44 +01:00
|
|
|
mobile_users = (
|
|
|
|
RemotePushDeviceToken.objects.filter(server=billing_session.remote_server)
|
|
|
|
.distinct("user_id", "user_uuid")
|
|
|
|
.count()
|
|
|
|
)
|
|
|
|
mobile_pushes = RemoteInstallationCount.objects.filter(
|
|
|
|
server=billing_session.remote_server,
|
|
|
|
property="mobile_pushes_forwarded::day",
|
|
|
|
end_time__gte=timezone_now() - timedelta(days=7),
|
|
|
|
).aggregate(total_forwarded=Sum("value", default=0))
|
|
|
|
mobile_data = MobilePushData(
|
|
|
|
mobile_users=mobile_users, mobile_pushes_forwarded=mobile_pushes["total_forwarded"]
|
|
|
|
)
|
2023-12-19 16:20:18 +01:00
|
|
|
else:
|
2023-12-15 18:04:08 +01:00
|
|
|
assert isinstance(billing_session, RemoteRealmBillingSession)
|
|
|
|
user_data = get_remote_realm_guest_and_non_guest_count(billing_session.remote_realm)
|
2023-12-28 19:28:36 +01:00
|
|
|
date_created = billing_session.remote_realm.realm_date_created
|
2024-01-05 22:22:44 +01:00
|
|
|
mobile_users = (
|
|
|
|
RemotePushDeviceToken.objects.filter(remote_realm=billing_session.remote_realm)
|
|
|
|
.distinct("user_id", "user_uuid")
|
|
|
|
.count()
|
|
|
|
)
|
|
|
|
mobile_pushes = RemoteRealmCount.objects.filter(
|
|
|
|
remote_realm=billing_session.remote_realm,
|
|
|
|
property="mobile_pushes_forwarded::day",
|
|
|
|
end_time__gte=timezone_now() - timedelta(days=7),
|
|
|
|
).aggregate(total_forwarded=Sum("value", default=0))
|
|
|
|
mobile_data = MobilePushData(
|
|
|
|
mobile_users=mobile_users, mobile_pushes_forwarded=mobile_pushes["total_forwarded"]
|
|
|
|
)
|
2023-12-13 18:23:05 +01:00
|
|
|
plan_data = get_current_plan_data_for_support_view(billing_session)
|
|
|
|
customer = billing_session.get_customer()
|
|
|
|
if customer is not None:
|
|
|
|
sponsorship_data = get_customer_sponsorship_data(customer)
|
|
|
|
else:
|
|
|
|
sponsorship_data = SponsorshipData()
|
|
|
|
|
|
|
|
return SupportData(
|
2023-12-28 19:28:36 +01:00
|
|
|
date_created=date_created,
|
2023-12-13 18:23:05 +01:00
|
|
|
plan_data=plan_data,
|
|
|
|
sponsorship_data=sponsorship_data,
|
2023-12-15 18:04:08 +01:00
|
|
|
user_data=user_data,
|
2024-01-05 22:22:44 +01:00
|
|
|
mobile_push_data=mobile_data,
|
2023-12-13 18:23:05 +01:00
|
|
|
)
|