2024-01-10 14:59:49 +01:00
|
|
|
from decimal import Decimal
|
2023-12-08 08:25:05 +01:00
|
|
|
from enum import Enum
|
2021-08-29 15:33:29 +02:00
|
|
|
from typing import Any, Dict, Optional, Union
|
2018-09-25 14:02:43 +02:00
|
|
|
|
2021-08-29 15:33:29 +02:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2018-09-25 14:02:43 +02:00
|
|
|
from django.db import models
|
2023-05-30 05:12:51 +02:00
|
|
|
from django.db.models import CASCADE, Q
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2018-09-25 14:02:43 +02:00
|
|
|
|
2021-07-09 19:56:55 +02:00
|
|
|
from zerver.models import Realm, UserProfile
|
2023-11-09 20:40:42 +01:00
|
|
|
from zilencer.models import RemoteRealm, RemoteZulipServer
|
2018-09-25 14:02:43 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2018-09-25 14:02:43 +02:00
|
|
|
class Customer(models.Model):
|
2021-06-03 13:39:18 +02:00
|
|
|
"""
|
|
|
|
This model primarily serves to connect a Realm with
|
|
|
|
the corresponding Stripe customer object for payment purposes
|
|
|
|
and the active plan, if any.
|
|
|
|
"""
|
|
|
|
|
2023-11-17 21:13:37 +01:00
|
|
|
# The actual model object that this customer is associated
|
|
|
|
# with. Exactly one of the following will be non-null.
|
2022-08-15 19:10:58 +02:00
|
|
|
realm = models.OneToOneField(Realm, on_delete=CASCADE, null=True)
|
2023-11-17 21:13:37 +01:00
|
|
|
remote_realm = models.OneToOneField(RemoteRealm, on_delete=CASCADE, null=True)
|
2022-08-15 19:10:58 +02:00
|
|
|
remote_server = models.OneToOneField(RemoteZulipServer, on_delete=CASCADE, null=True)
|
2023-11-17 21:13:37 +01:00
|
|
|
|
2022-08-15 19:10:58 +02:00
|
|
|
stripe_customer_id = models.CharField(max_length=255, null=True, unique=True)
|
|
|
|
sponsorship_pending = models.BooleanField(default=False)
|
2019-01-25 02:14:07 +01:00
|
|
|
# A percentage, like 85.
|
2022-08-15 19:10:58 +02:00
|
|
|
default_discount = models.DecimalField(decimal_places=4, max_digits=7, null=True)
|
2023-12-14 19:47:40 +01:00
|
|
|
minimum_licenses = models.PositiveIntegerField(null=True)
|
2024-01-09 15:33:55 +01:00
|
|
|
# Used for limiting a default_discount or a fixed_price
|
|
|
|
# to be used only for a particular CustomerPlan tier.
|
|
|
|
required_plan_tier = models.SmallIntegerField(null=True)
|
2021-06-17 16:25:40 +02:00
|
|
|
# Some non-profit organizations on manual license management pay
|
|
|
|
# only for their paid employees. We don't prevent these
|
|
|
|
# organizations from adding more users than the number of licenses
|
|
|
|
# they purchased.
|
2023-04-10 20:23:12 +02:00
|
|
|
exempt_from_license_number_check = models.BooleanField(default=False)
|
2018-09-25 14:02:43 +02:00
|
|
|
|
2023-12-20 07:24:21 +01:00
|
|
|
# In cents.
|
|
|
|
flat_discount = models.IntegerField(default=2000)
|
|
|
|
# Number of months left in the flat discount period.
|
|
|
|
flat_discounted_months = models.IntegerField(default=0)
|
|
|
|
|
2023-05-30 05:12:51 +02:00
|
|
|
class Meta:
|
2023-11-17 21:13:37 +01:00
|
|
|
# Enforce that at least one of these is set.
|
2023-05-30 05:12:51 +02:00
|
|
|
constraints = [
|
|
|
|
models.CheckConstraint(
|
2023-11-17 21:13:37 +01:00
|
|
|
check=Q(realm__isnull=False)
|
|
|
|
| Q(remote_server__isnull=False)
|
|
|
|
| Q(remote_realm__isnull=False),
|
|
|
|
name="has_associated_model_object",
|
2023-05-30 05:12:51 +02:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-04-12 22:40:35 +02:00
|
|
|
def __str__(self) -> str:
|
2023-10-30 22:25:10 +01:00
|
|
|
if self.realm is not None:
|
|
|
|
return f"{self.realm!r} (with stripe_customer_id: {self.stripe_customer_id})"
|
2024-01-25 15:49:19 +01:00
|
|
|
elif self.remote_realm is not None:
|
|
|
|
return f"{self.remote_realm!r} (with stripe_customer_id: {self.stripe_customer_id})"
|
2023-10-30 22:25:10 +01:00
|
|
|
else:
|
|
|
|
return f"{self.remote_server!r} (with stripe_customer_id: {self.stripe_customer_id})"
|
2023-04-12 22:40:35 +02:00
|
|
|
|
2024-01-10 14:59:49 +01:00
|
|
|
def get_discount_for_plan_tier(self, plan_tier: int) -> Optional[Decimal]:
|
|
|
|
if self.required_plan_tier is None or self.required_plan_tier == plan_tier:
|
|
|
|
return self.default_discount
|
2024-01-10 17:20:08 +01:00
|
|
|
return None
|
2024-01-10 14:59:49 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-03-23 13:35:04 +01:00
|
|
|
def get_customer_by_realm(realm: Realm) -> Optional[Customer]:
|
|
|
|
return Customer.objects.filter(realm=realm).first()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-11-09 20:40:42 +01:00
|
|
|
def get_customer_by_remote_server(remote_server: RemoteZulipServer) -> Optional[Customer]:
|
|
|
|
return Customer.objects.filter(remote_server=remote_server).first()
|
|
|
|
|
|
|
|
|
|
|
|
def get_customer_by_remote_realm(remote_realm: RemoteRealm) -> Optional[Customer]: # nocoverage
|
|
|
|
return Customer.objects.filter(remote_realm=remote_realm).first()
|
|
|
|
|
|
|
|
|
2021-08-29 15:33:29 +02:00
|
|
|
class Event(models.Model):
|
|
|
|
stripe_event_id = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
type = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
RECEIVED = 1
|
|
|
|
EVENT_HANDLER_STARTED = 30
|
|
|
|
EVENT_HANDLER_FAILED = 40
|
|
|
|
EVENT_HANDLER_SUCCEEDED = 50
|
|
|
|
status = models.SmallIntegerField(default=RECEIVED)
|
|
|
|
|
|
|
|
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
|
|
|
object_id = models.PositiveIntegerField(db_index=True)
|
|
|
|
content_object = GenericForeignKey("content_type", "object_id")
|
|
|
|
|
|
|
|
handler_error = models.JSONField(default=None, null=True)
|
|
|
|
|
|
|
|
def get_event_handler_details_as_dict(self) -> Dict[str, Any]:
|
|
|
|
details_dict = {}
|
|
|
|
details_dict["status"] = {
|
|
|
|
Event.RECEIVED: "not_started",
|
|
|
|
Event.EVENT_HANDLER_STARTED: "started",
|
|
|
|
Event.EVENT_HANDLER_FAILED: "failed",
|
|
|
|
Event.EVENT_HANDLER_SUCCEEDED: "succeeded",
|
|
|
|
}[self.status]
|
|
|
|
if self.handler_error:
|
|
|
|
details_dict["error"] = self.handler_error
|
|
|
|
return details_dict
|
|
|
|
|
|
|
|
|
|
|
|
def get_last_associated_event_by_type(
|
2024-02-10 07:47:32 +01:00
|
|
|
content_object: Union["Invoice", "PaymentIntent", "Session"], event_type: str
|
2021-08-29 15:33:29 +02:00
|
|
|
) -> Optional[Event]:
|
|
|
|
content_type = ContentType.objects.get_for_model(type(content_object))
|
|
|
|
return Event.objects.filter(
|
|
|
|
content_type=content_type, object_id=content_object.id, type=event_type
|
|
|
|
).last()
|
|
|
|
|
|
|
|
|
|
|
|
class Session(models.Model):
|
2022-08-15 19:10:58 +02:00
|
|
|
customer = models.ForeignKey(Customer, on_delete=CASCADE)
|
|
|
|
stripe_session_id = models.CharField(max_length=255, unique=True)
|
2021-08-29 15:33:29 +02:00
|
|
|
|
|
|
|
CARD_UPDATE_FROM_BILLING_PAGE = 40
|
2023-11-18 11:29:04 +01:00
|
|
|
CARD_UPDATE_FROM_UPGRADE_PAGE = 50
|
2022-08-15 19:10:58 +02:00
|
|
|
type = models.SmallIntegerField()
|
2021-08-29 15:33:29 +02:00
|
|
|
|
|
|
|
CREATED = 1
|
|
|
|
COMPLETED = 10
|
2022-08-15 19:10:58 +02:00
|
|
|
status = models.SmallIntegerField(default=CREATED)
|
2021-08-29 15:33:29 +02:00
|
|
|
|
2023-11-21 12:41:38 +01:00
|
|
|
# Did the user opt to manually manage licenses before clicking on update button?
|
|
|
|
is_manual_license_management_upgrade_session = models.BooleanField(default=False)
|
|
|
|
|
2023-12-18 13:02:36 +01:00
|
|
|
# CustomerPlan tier that the user is upgrading to.
|
|
|
|
tier = models.SmallIntegerField(null=True)
|
|
|
|
|
2021-08-29 15:33:29 +02:00
|
|
|
def get_status_as_string(self) -> str:
|
|
|
|
return {Session.CREATED: "created", Session.COMPLETED: "completed"}[self.status]
|
|
|
|
|
|
|
|
def get_type_as_string(self) -> str:
|
|
|
|
return {
|
|
|
|
Session.CARD_UPDATE_FROM_BILLING_PAGE: "card_update_from_billing_page",
|
2023-11-18 11:29:04 +01:00
|
|
|
Session.CARD_UPDATE_FROM_UPGRADE_PAGE: "card_update_from_upgrade_page",
|
2021-08-29 15:33:29 +02:00
|
|
|
}[self.type]
|
|
|
|
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
|
|
session_dict: Dict[str, Any] = {}
|
|
|
|
|
|
|
|
session_dict["status"] = self.get_status_as_string()
|
|
|
|
session_dict["type"] = self.get_type_as_string()
|
2024-01-29 00:32:21 +01:00
|
|
|
session_dict["is_manual_license_management_upgrade_session"] = (
|
|
|
|
self.is_manual_license_management_upgrade_session
|
|
|
|
)
|
2023-12-18 13:02:36 +01:00
|
|
|
session_dict["tier"] = self.tier
|
2021-08-29 15:33:29 +02:00
|
|
|
event = self.get_last_associated_event()
|
|
|
|
if event is not None:
|
|
|
|
session_dict["event_handler"] = event.get_event_handler_details_as_dict()
|
|
|
|
return session_dict
|
|
|
|
|
|
|
|
def get_last_associated_event(self) -> Optional[Event]:
|
|
|
|
if self.status == Session.CREATED:
|
|
|
|
return None
|
|
|
|
return get_last_associated_event_by_type(self, "checkout.session.completed")
|
|
|
|
|
|
|
|
|
2024-02-10 07:47:32 +01:00
|
|
|
class PaymentIntent(models.Model): # nocoverage
|
2022-08-15 19:10:58 +02:00
|
|
|
customer = models.ForeignKey(Customer, on_delete=CASCADE)
|
|
|
|
stripe_payment_intent_id = models.CharField(max_length=255, unique=True)
|
2021-08-29 15:33:29 +02:00
|
|
|
|
|
|
|
REQUIRES_PAYMENT_METHOD = 1
|
|
|
|
REQUIRES_CONFIRMATION = 20
|
|
|
|
REQUIRES_ACTION = 30
|
|
|
|
PROCESSING = 40
|
|
|
|
REQUIRES_CAPTURE = 50
|
|
|
|
CANCELLED = 60
|
|
|
|
SUCCEEDED = 70
|
|
|
|
|
2022-08-15 19:10:58 +02:00
|
|
|
status = models.SmallIntegerField()
|
2021-08-29 15:33:29 +02:00
|
|
|
last_payment_error = models.JSONField(default=None, null=True)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_status_integer_from_status_text(cls, status_text: str) -> int:
|
|
|
|
return getattr(cls, status_text.upper())
|
|
|
|
|
|
|
|
def get_status_as_string(self) -> str:
|
|
|
|
return {
|
|
|
|
PaymentIntent.REQUIRES_PAYMENT_METHOD: "requires_payment_method",
|
|
|
|
PaymentIntent.REQUIRES_CONFIRMATION: "requires_confirmation",
|
|
|
|
PaymentIntent.REQUIRES_ACTION: "requires_action",
|
|
|
|
PaymentIntent.PROCESSING: "processing",
|
|
|
|
PaymentIntent.REQUIRES_CAPTURE: "requires_capture",
|
|
|
|
PaymentIntent.CANCELLED: "cancelled",
|
|
|
|
PaymentIntent.SUCCEEDED: "succeeded",
|
|
|
|
}[self.status]
|
|
|
|
|
|
|
|
def get_last_associated_event(self) -> Optional[Event]:
|
|
|
|
if self.status == PaymentIntent.SUCCEEDED:
|
|
|
|
event_type = "payment_intent.succeeded"
|
2023-11-18 11:29:04 +01:00
|
|
|
# TODO: Add test for this case. Not sure how to trigger naturally.
|
|
|
|
else: # nocoverage
|
|
|
|
return None # nocoverage
|
2021-08-29 15:33:29 +02:00
|
|
|
return get_last_associated_event_by_type(self, event_type)
|
|
|
|
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
|
|
payment_intent_dict: Dict[str, Any] = {}
|
|
|
|
payment_intent_dict["status"] = self.get_status_as_string()
|
|
|
|
event = self.get_last_associated_event()
|
|
|
|
if event is not None:
|
|
|
|
payment_intent_dict["event_handler"] = event.get_event_handler_details_as_dict()
|
|
|
|
return payment_intent_dict
|
|
|
|
|
|
|
|
|
2024-02-02 13:04:41 +01:00
|
|
|
class Invoice(models.Model):
|
|
|
|
customer = models.ForeignKey(Customer, on_delete=CASCADE)
|
|
|
|
stripe_invoice_id = models.CharField(max_length=255, unique=True)
|
|
|
|
|
|
|
|
SENT = 1
|
|
|
|
PAID = 2
|
2024-02-10 07:47:32 +01:00
|
|
|
VOID = 3
|
2024-02-02 13:04:41 +01:00
|
|
|
status = models.SmallIntegerField()
|
|
|
|
|
2024-02-10 07:47:32 +01:00
|
|
|
def get_status_as_string(self) -> str:
|
|
|
|
return {
|
|
|
|
Invoice.SENT: "sent",
|
|
|
|
Invoice.PAID: "paid",
|
|
|
|
Invoice.VOID: "void",
|
|
|
|
}[self.status]
|
|
|
|
|
|
|
|
def get_last_associated_event(self) -> Optional[Event]:
|
|
|
|
if self.status == Invoice.PAID:
|
|
|
|
event_type = "invoice.paid"
|
|
|
|
# TODO: Add test for this case. Not sure how to trigger naturally.
|
|
|
|
else: # nocoverage
|
|
|
|
return None # nocoverage
|
|
|
|
return get_last_associated_event_by_type(self, event_type)
|
|
|
|
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
|
|
stripe_invoice_dict: Dict[str, Any] = {}
|
|
|
|
stripe_invoice_dict["status"] = self.get_status_as_string()
|
|
|
|
event = self.get_last_associated_event()
|
|
|
|
if event is not None:
|
|
|
|
stripe_invoice_dict["event_handler"] = event.get_event_handler_details_as_dict()
|
|
|
|
return stripe_invoice_dict
|
|
|
|
|
2024-02-02 13:04:41 +01:00
|
|
|
|
2024-01-17 13:55:25 +01:00
|
|
|
class AbstractCustomerPlan(models.Model):
|
|
|
|
# A customer can only have one ACTIVE / CONFIGURED plan,
|
|
|
|
# but old, inactive / processed plans are preserved to allow
|
|
|
|
# auditing - so there can be multiple CustomerPlan / CustomerPlanOffer
|
|
|
|
# objects pointing to one Customer.
|
|
|
|
customer = models.ForeignKey(Customer, on_delete=CASCADE)
|
|
|
|
|
|
|
|
fixed_price = models.IntegerField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
|
|
|
class CustomerPlanOffer(AbstractCustomerPlan):
|
|
|
|
"""
|
|
|
|
This is for storing offers configured via /support which
|
|
|
|
the customer is yet to buy or schedule a purchase.
|
|
|
|
|
|
|
|
Once customer buys or schedules a purchase, we create a
|
|
|
|
CustomerPlan record. The record in this table stays for
|
|
|
|
audit purpose with status=PROCESSED.
|
|
|
|
"""
|
|
|
|
|
|
|
|
TIER_SELF_HOSTED_BASIC = 103
|
|
|
|
TIER_SELF_HOSTED_BUSINESS = 104
|
|
|
|
tier = models.SmallIntegerField()
|
|
|
|
|
|
|
|
# Whether the offer is:
|
|
|
|
# * only configured
|
|
|
|
# * processed by the customer to buy or schedule a purchase.
|
|
|
|
CONFIGURED = 1
|
|
|
|
PROCESSED = 2
|
|
|
|
status = models.SmallIntegerField()
|
|
|
|
|
2024-02-02 13:04:41 +01:00
|
|
|
# ID of invoice sent when chose to 'Pay by invoice'.
|
|
|
|
sent_invoice_id = models.CharField(max_length=255, null=True)
|
|
|
|
|
2024-01-17 13:55:25 +01:00
|
|
|
@override
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"{self.name} (status: {self.get_plan_status_as_text()})"
|
|
|
|
|
|
|
|
def get_plan_status_as_text(self) -> str:
|
|
|
|
return {
|
|
|
|
self.CONFIGURED: "Configured",
|
|
|
|
self.PROCESSED: "Processed",
|
|
|
|
}[self.status]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def name_from_tier(tier: int) -> str: # nocoverage
|
|
|
|
return {
|
|
|
|
CustomerPlanOffer.TIER_SELF_HOSTED_BASIC: "Zulip Basic",
|
|
|
|
CustomerPlanOffer.TIER_SELF_HOSTED_BUSINESS: "Zulip Business",
|
|
|
|
}[tier]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str: # nocoverage
|
|
|
|
# TODO: This is used in `check_customer_not_on_paid_plan` as
|
|
|
|
# 'next_plan.name'. Related to sponsorship, add coverage.
|
|
|
|
return self.name_from_tier(self.tier)
|
|
|
|
|
|
|
|
|
|
|
|
class CustomerPlan(AbstractCustomerPlan):
|
2021-06-03 13:39:18 +02:00
|
|
|
"""
|
|
|
|
This is for storing most of the fiddly details
|
|
|
|
of the customer's plan.
|
|
|
|
"""
|
|
|
|
|
2022-08-15 19:10:58 +02:00
|
|
|
automanage_licenses = models.BooleanField(default=False)
|
|
|
|
charge_automatically = models.BooleanField(default=False)
|
2021-06-08 12:43:44 +02:00
|
|
|
|
2024-01-17 13:55:25 +01:00
|
|
|
# Both of the price_per_license and fixed_price are in cents. Exactly
|
|
|
|
# one of them should be set. fixed_price is only for manual deals, and
|
2018-12-15 09:33:25 +01:00
|
|
|
# can't be set via the self-serve billing system.
|
2022-08-15 19:10:58 +02:00
|
|
|
price_per_license = models.IntegerField(null=True)
|
2018-12-15 09:33:25 +01:00
|
|
|
|
2019-01-25 02:14:07 +01:00
|
|
|
# Discount that was applied. For display purposes only.
|
2022-08-15 19:10:58 +02:00
|
|
|
discount = models.DecimalField(decimal_places=4, max_digits=6, null=True)
|
2018-12-15 09:33:25 +01:00
|
|
|
|
2021-06-17 16:25:40 +02:00
|
|
|
# Initialized with the time of plan creation. Used for calculating
|
|
|
|
# start of next billing cycle, next invoice date etc. This value
|
|
|
|
# should never be modified. The only exception is when we change
|
|
|
|
# the status of the plan from free trial to active and reset the
|
|
|
|
# billing_cycle_anchor.
|
2022-08-15 19:10:58 +02:00
|
|
|
billing_cycle_anchor = models.DateTimeField()
|
2021-06-17 16:25:40 +02:00
|
|
|
|
2023-11-30 07:55:53 +01:00
|
|
|
BILLING_SCHEDULE_ANNUAL = 1
|
|
|
|
BILLING_SCHEDULE_MONTHLY = 2
|
2023-11-07 17:01:59 +01:00
|
|
|
BILLING_SCHEDULES = {
|
2023-11-30 07:55:53 +01:00
|
|
|
BILLING_SCHEDULE_ANNUAL: "Annual",
|
|
|
|
BILLING_SCHEDULE_MONTHLY: "Monthly",
|
2023-11-07 17:01:59 +01:00
|
|
|
}
|
2022-08-15 19:10:58 +02:00
|
|
|
billing_schedule = models.SmallIntegerField()
|
2018-12-15 09:33:25 +01:00
|
|
|
|
2021-06-17 16:25:40 +02:00
|
|
|
# The next date the billing system should go through ledger
|
|
|
|
# entries and create invoices for additional users or plan
|
|
|
|
# renewal. Since we use a daily cron job for invoicing, the
|
|
|
|
# invoice will be generated the first time the cron job runs after
|
|
|
|
# next_invoice_date.
|
2022-08-15 19:10:58 +02:00
|
|
|
next_invoice_date = models.DateTimeField(db_index=True, null=True)
|
2021-06-17 16:25:40 +02:00
|
|
|
|
2024-01-08 13:28:06 +01:00
|
|
|
# Flag to track if an email has been sent to Zulip team for
|
|
|
|
# invoice overdue by >= one day. Helps to send an email only once
|
|
|
|
# and not every time when cron run.
|
|
|
|
invoice_overdue_email_sent = models.BooleanField(default=False)
|
|
|
|
|
2024-02-15 08:00:20 +01:00
|
|
|
# Flag to track if an email has been sent to Zulip team to
|
|
|
|
# review the pricing, 60 days before the end date. Helps to send
|
|
|
|
# an email only once and not every time when cron run.
|
|
|
|
reminder_to_review_plan_email_sent = models.BooleanField(default=False)
|
|
|
|
|
2021-06-17 16:25:40 +02:00
|
|
|
# On next_invoice_date, we go through ledger entries that were
|
|
|
|
# created after invoiced_through and process them by generating
|
|
|
|
# invoices for any additional users and/or plan renewal. Once the
|
|
|
|
# invoice is generated, we update the value of invoiced_through
|
|
|
|
# and set it to the last ledger entry we processed.
|
2022-08-15 19:10:58 +02:00
|
|
|
invoiced_through = models.ForeignKey(
|
2021-02-12 08:20:45 +01:00
|
|
|
"LicenseLedger", null=True, on_delete=CASCADE, related_name="+"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2022-08-15 19:10:58 +02:00
|
|
|
end_date = models.DateTimeField(null=True)
|
2021-06-17 16:25:40 +02:00
|
|
|
|
2023-11-30 08:07:12 +01:00
|
|
|
INVOICING_STATUS_DONE = 1
|
|
|
|
INVOICING_STATUS_STARTED = 2
|
|
|
|
INVOICING_STATUS_INITIAL_INVOICE_TO_BE_SENT = 3
|
2021-06-17 16:25:40 +02:00
|
|
|
# This status field helps ensure any errors encountered during the
|
|
|
|
# invoicing process do not leave our invoicing system in a broken
|
|
|
|
# state.
|
2023-11-30 08:07:12 +01:00
|
|
|
invoicing_status = models.SmallIntegerField(default=INVOICING_STATUS_DONE)
|
2018-12-15 09:33:25 +01:00
|
|
|
|
2023-11-30 07:43:06 +01:00
|
|
|
TIER_CLOUD_STANDARD = 1
|
|
|
|
TIER_CLOUD_PLUS = 2
|
|
|
|
# Reserved tier IDs for future use
|
|
|
|
TIER_CLOUD_COMMUNITY = 3
|
|
|
|
TIER_CLOUD_ENTERPRISE = 4
|
|
|
|
|
|
|
|
TIER_SELF_HOSTED_BASE = 100
|
|
|
|
TIER_SELF_HOSTED_LEGACY = 101
|
|
|
|
TIER_SELF_HOSTED_COMMUNITY = 102
|
2023-12-18 23:57:32 +01:00
|
|
|
TIER_SELF_HOSTED_BASIC = 103
|
|
|
|
TIER_SELF_HOSTED_BUSINESS = 104
|
2023-11-30 07:43:06 +01:00
|
|
|
TIER_SELF_HOSTED_ENTERPRISE = 105
|
2022-08-15 19:10:58 +02:00
|
|
|
tier = models.SmallIntegerField()
|
2018-12-15 09:33:25 +01:00
|
|
|
|
|
|
|
ACTIVE = 1
|
2019-04-10 19:43:16 +02:00
|
|
|
DOWNGRADE_AT_END_OF_CYCLE = 2
|
2020-04-23 20:10:15 +02:00
|
|
|
FREE_TRIAL = 3
|
2020-06-15 20:09:24 +02:00
|
|
|
SWITCH_TO_ANNUAL_AT_END_OF_CYCLE = 4
|
2023-11-23 05:24:49 +01:00
|
|
|
SWITCH_PLAN_TIER_NOW = 5
|
2023-11-20 13:01:25 +01:00
|
|
|
SWITCH_TO_MONTHLY_AT_END_OF_CYCLE = 6
|
2023-11-25 14:44:47 +01:00
|
|
|
DOWNGRADE_AT_END_OF_FREE_TRIAL = 7
|
2023-12-04 14:03:24 +01:00
|
|
|
SWITCH_PLAN_TIER_AT_PLAN_END = 8
|
2019-04-10 19:43:16 +02:00
|
|
|
# "Live" plans should have a value < LIVE_STATUS_THRESHOLD.
|
|
|
|
# There should be at most one live plan per customer.
|
|
|
|
LIVE_STATUS_THRESHOLD = 10
|
|
|
|
ENDED = 11
|
|
|
|
NEVER_STARTED = 12
|
2022-08-15 19:10:58 +02:00
|
|
|
status = models.SmallIntegerField(default=ACTIVE)
|
2018-12-15 09:33:25 +01:00
|
|
|
|
2024-02-15 11:23:42 +01:00
|
|
|
# Currently, all the fixed-price plans are configured for one year.
|
|
|
|
# In future, we might change this to a field.
|
|
|
|
FIXED_PRICE_PLAN_DURATION_MONTHS = 12
|
|
|
|
|
2021-06-17 16:25:40 +02:00
|
|
|
# TODO maybe override setattr to ensure billing_cycle_anchor, etc
|
|
|
|
# are immutable.
|
2018-12-15 09:33:25 +01:00
|
|
|
|
2023-12-14 09:46:57 +01:00
|
|
|
@override
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"{self.name} (status: {self.get_plan_status_as_text()})"
|
|
|
|
|
2023-12-01 12:42:07 +01:00
|
|
|
@staticmethod
|
|
|
|
def name_from_tier(tier: int) -> str:
|
2023-12-02 04:19:59 +01:00
|
|
|
# NOTE: Check `statement_descriptor` values after updating this.
|
|
|
|
# Stripe has a 22 character limit on the statement descriptor length.
|
|
|
|
# https://stripe.com/docs/payments/account/statement-descriptors
|
2020-07-03 20:14:31 +02:00
|
|
|
return {
|
2023-11-30 07:43:06 +01:00
|
|
|
CustomerPlan.TIER_CLOUD_STANDARD: "Zulip Cloud Standard",
|
2023-12-02 04:19:59 +01:00
|
|
|
CustomerPlan.TIER_CLOUD_PLUS: "Zulip Cloud Plus",
|
2023-11-30 07:43:06 +01:00
|
|
|
CustomerPlan.TIER_CLOUD_ENTERPRISE: "Zulip Enterprise",
|
2024-02-13 05:58:20 +01:00
|
|
|
CustomerPlan.TIER_SELF_HOSTED_LEGACY: "Free (legacy plan)",
|
2023-12-18 23:57:32 +01:00
|
|
|
CustomerPlan.TIER_SELF_HOSTED_BASIC: "Zulip Basic",
|
2023-12-02 04:19:59 +01:00
|
|
|
CustomerPlan.TIER_SELF_HOSTED_BUSINESS: "Zulip Business",
|
2023-12-05 07:41:34 +01:00
|
|
|
CustomerPlan.TIER_SELF_HOSTED_COMMUNITY: "Community",
|
2023-12-01 12:42:07 +01:00
|
|
|
}[tier]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
return self.name_from_tier(self.tier)
|
2020-07-03 20:14:31 +02:00
|
|
|
|
2020-07-03 20:21:13 +02:00
|
|
|
def get_plan_status_as_text(self) -> str:
|
|
|
|
return {
|
|
|
|
self.ACTIVE: "Active",
|
2023-12-15 22:26:25 +01:00
|
|
|
self.DOWNGRADE_AT_END_OF_CYCLE: "Downgrade end of cycle",
|
2020-07-03 20:21:13 +02:00
|
|
|
self.FREE_TRIAL: "Free trial",
|
2023-12-15 22:26:25 +01:00
|
|
|
self.SWITCH_TO_ANNUAL_AT_END_OF_CYCLE: "Scheduled switch to annual",
|
|
|
|
self.SWITCH_TO_MONTHLY_AT_END_OF_CYCLE: "Scheduled switch to monthly",
|
|
|
|
self.DOWNGRADE_AT_END_OF_FREE_TRIAL: "Downgrade end of free trial",
|
|
|
|
self.SWITCH_PLAN_TIER_AT_PLAN_END: "New plan scheduled",
|
2020-07-03 20:21:13 +02:00
|
|
|
self.ENDED: "Ended",
|
2021-02-12 08:19:30 +01:00
|
|
|
self.NEVER_STARTED: "Never started",
|
2020-07-03 20:21:13 +02:00
|
|
|
}[self.status]
|
2020-07-03 20:14:31 +02:00
|
|
|
|
2020-12-25 19:12:30 +01:00
|
|
|
def licenses(self) -> int:
|
2021-07-24 18:16:48 +02:00
|
|
|
ledger_entry = LicenseLedger.objects.filter(plan=self).order_by("id").last()
|
|
|
|
assert ledger_entry is not None
|
|
|
|
return ledger_entry.licenses
|
2020-12-25 19:12:30 +01:00
|
|
|
|
2020-12-30 18:57:35 +01:00
|
|
|
def licenses_at_next_renewal(self) -> Optional[int]:
|
2023-11-25 14:44:47 +01:00
|
|
|
if self.status in (
|
|
|
|
CustomerPlan.DOWNGRADE_AT_END_OF_CYCLE,
|
|
|
|
CustomerPlan.DOWNGRADE_AT_END_OF_FREE_TRIAL,
|
|
|
|
):
|
2020-12-30 18:57:35 +01:00
|
|
|
return None
|
2021-07-24 18:16:48 +02:00
|
|
|
ledger_entry = LicenseLedger.objects.filter(plan=self).order_by("id").last()
|
|
|
|
assert ledger_entry is not None
|
|
|
|
return ledger_entry.licenses_at_next_renewal
|
2020-12-30 18:57:35 +01:00
|
|
|
|
2020-11-11 14:02:47 +01:00
|
|
|
def is_free_trial(self) -> bool:
|
|
|
|
return self.status == CustomerPlan.FREE_TRIAL
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-03-24 14:14:03 +01:00
|
|
|
def get_current_plan_by_customer(customer: Customer) -> Optional[CustomerPlan]:
|
2019-04-10 19:43:16 +02:00
|
|
|
return CustomerPlan.objects.filter(
|
2021-02-12 08:19:30 +01:00
|
|
|
customer=customer, status__lt=CustomerPlan.LIVE_STATUS_THRESHOLD
|
|
|
|
).first()
|
|
|
|
|
2018-12-12 23:23:15 +01:00
|
|
|
|
2024-02-22 05:30:41 +01:00
|
|
|
def is_legacy_customer(customer: Customer) -> bool:
|
|
|
|
return CustomerPlan.objects.filter(
|
|
|
|
customer=customer, tier=CustomerPlan.TIER_SELF_HOSTED_LEGACY
|
|
|
|
).exists()
|
|
|
|
|
|
|
|
|
2020-03-24 14:22:27 +01:00
|
|
|
def get_current_plan_by_realm(realm: Realm) -> Optional[CustomerPlan]:
|
|
|
|
customer = get_customer_by_realm(realm)
|
|
|
|
if customer is None:
|
|
|
|
return None
|
|
|
|
return get_current_plan_by_customer(customer)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-12-28 07:20:30 +01:00
|
|
|
class LicenseLedger(models.Model):
|
2021-06-03 13:39:18 +02:00
|
|
|
"""
|
|
|
|
This table's purpose is to store the current, and historical,
|
|
|
|
count of "seats" purchased by the organization.
|
|
|
|
|
|
|
|
Because we want to keep historical data, when the purchased
|
|
|
|
seat count changes, a new LicenseLedger object is created,
|
|
|
|
instead of updating the old one. This lets us preserve
|
|
|
|
the entire history of how the seat count changes, which is
|
|
|
|
important for analytics as well as auditing and debugging
|
|
|
|
in case of issues.
|
|
|
|
"""
|
|
|
|
|
2022-08-15 19:10:58 +02:00
|
|
|
plan = models.ForeignKey(CustomerPlan, on_delete=CASCADE)
|
2021-06-17 16:25:40 +02:00
|
|
|
|
2018-12-28 07:20:30 +01:00
|
|
|
# Also True for the initial upgrade.
|
2022-08-15 19:10:58 +02:00
|
|
|
is_renewal = models.BooleanField(default=False)
|
2021-06-17 16:25:40 +02:00
|
|
|
|
2022-08-15 19:10:58 +02:00
|
|
|
event_time = models.DateTimeField()
|
2021-06-17 16:25:40 +02:00
|
|
|
|
|
|
|
# The number of licenses ("seats") purchased by the the organization at the time of ledger
|
|
|
|
# entry creation. Normally, to add a user the organization needs at least one spare license.
|
|
|
|
# Once a license is purchased, it is valid till the end of the billing period, irrespective
|
|
|
|
# of whether the license is used or not. So the value of licenses will never decrease for
|
|
|
|
# subsequent LicenseLedger entries in the same billing period.
|
2022-08-15 19:10:58 +02:00
|
|
|
licenses = models.IntegerField()
|
2021-06-17 16:25:40 +02:00
|
|
|
|
|
|
|
# The number of licenses the organization needs in the next billing cycle. The value of
|
|
|
|
# licenses_at_next_renewal can increase or decrease for subsequent LicenseLedger entries in
|
|
|
|
# the same billing period. For plans on automatic license management this value is usually
|
|
|
|
# equal to the number of activated users in the organization.
|
2022-08-15 19:10:58 +02:00
|
|
|
licenses_at_next_renewal = models.IntegerField(null=True)
|
2021-07-09 19:56:55 +02:00
|
|
|
|
|
|
|
|
2023-12-08 08:25:05 +01:00
|
|
|
class SponsoredPlanTypes(Enum):
|
|
|
|
# unspecified used for cloud sponsorship requests
|
|
|
|
UNSPECIFIED = ""
|
|
|
|
COMMUNITY = "Community"
|
2023-12-18 23:57:32 +01:00
|
|
|
BASIC = "Basic"
|
2023-12-08 08:25:05 +01:00
|
|
|
BUSINESS = "Business"
|
|
|
|
|
|
|
|
|
2021-07-09 19:56:55 +02:00
|
|
|
class ZulipSponsorshipRequest(models.Model):
|
2023-11-27 06:16:25 +01:00
|
|
|
customer = models.ForeignKey(Customer, on_delete=CASCADE)
|
2023-11-28 17:00:37 +01:00
|
|
|
requested_by = models.ForeignKey(UserProfile, on_delete=CASCADE, null=True, blank=True)
|
2021-07-09 19:56:55 +02:00
|
|
|
|
2022-08-15 19:10:58 +02:00
|
|
|
org_type = models.PositiveSmallIntegerField(
|
2021-07-09 19:56:55 +02:00
|
|
|
default=Realm.ORG_TYPES["unspecified"]["id"],
|
|
|
|
choices=[(t["id"], t["name"]) for t in Realm.ORG_TYPES.values()],
|
|
|
|
)
|
|
|
|
|
|
|
|
MAX_ORG_URL_LENGTH: int = 200
|
2022-08-15 19:10:58 +02:00
|
|
|
org_website = models.URLField(max_length=MAX_ORG_URL_LENGTH, blank=True, null=True)
|
2021-07-09 19:56:55 +02:00
|
|
|
|
2022-08-15 19:10:58 +02:00
|
|
|
org_description = models.TextField(default="")
|
2023-11-11 16:15:33 +01:00
|
|
|
expected_total_users = models.TextField(default="")
|
|
|
|
paid_users_count = models.TextField(default="")
|
|
|
|
paid_users_description = models.TextField(default="")
|
2023-12-08 08:25:05 +01:00
|
|
|
|
|
|
|
requested_plan = models.CharField(
|
|
|
|
max_length=50,
|
|
|
|
choices=[(plan.value, plan.name) for plan in SponsoredPlanTypes],
|
|
|
|
default=SponsoredPlanTypes.UNSPECIFIED.value,
|
|
|
|
)
|