2018-09-25 14:02:43 +02:00
|
|
|
import datetime
|
2018-12-12 19:41:03 +01:00
|
|
|
from decimal import Decimal
|
|
|
|
from typing import Optional
|
2018-09-25 14:02:43 +02:00
|
|
|
|
|
|
|
from django.db import models
|
2018-12-30 03:45:23 +01:00
|
|
|
from django.db.models import CASCADE
|
2018-09-25 14:02:43 +02:00
|
|
|
|
|
|
|
from zerver.models import Realm, RealmAuditLog
|
|
|
|
|
|
|
|
class Customer(models.Model):
|
2018-12-30 03:45:23 +01:00
|
|
|
realm = models.OneToOneField(Realm, on_delete=CASCADE) # type: Realm
|
2018-09-25 14:02:43 +02:00
|
|
|
stripe_customer_id = models.CharField(max_length=255, unique=True) # type: str
|
2018-12-15 09:33:25 +01:00
|
|
|
# Deprecated .. delete once everyone is migrated to new billing system
|
2018-09-25 14:02:43 +02:00
|
|
|
has_billing_relationship = models.BooleanField(default=False) # type: bool
|
2018-12-12 19:41:03 +01:00
|
|
|
default_discount = models.DecimalField(decimal_places=4, max_digits=7, null=True) # type: Optional[Decimal]
|
2018-09-25 14:02:43 +02:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return "<Customer %s %s>" % (self.realm, self.stripe_customer_id)
|
|
|
|
|
2018-12-15 09:33:25 +01:00
|
|
|
class CustomerPlan(models.Model):
|
2018-12-30 03:45:23 +01:00
|
|
|
customer = models.ForeignKey(Customer, on_delete=CASCADE) # type: Customer
|
2018-12-15 09:33:25 +01:00
|
|
|
licenses = models.IntegerField() # type: int
|
|
|
|
automanage_licenses = models.BooleanField(default=False) # type: bool
|
|
|
|
charge_automatically = models.BooleanField(default=False) # type: bool
|
|
|
|
|
|
|
|
# Both of these are in cents. Exactly one of price_per_license or
|
|
|
|
# fixed_price should be set. fixed_price is only for manual deals, and
|
|
|
|
# can't be set via the self-serve billing system.
|
|
|
|
price_per_license = models.IntegerField(null=True) # type: Optional[int]
|
|
|
|
fixed_price = models.IntegerField(null=True) # type: Optional[int]
|
|
|
|
|
|
|
|
# A percentage, like 85
|
|
|
|
discount = models.DecimalField(decimal_places=4, max_digits=6, null=True) # type: Optional[Decimal]
|
|
|
|
|
|
|
|
billing_cycle_anchor = models.DateTimeField() # type: datetime.datetime
|
2018-12-12 23:23:15 +01:00
|
|
|
ANNUAL = 1
|
|
|
|
MONTHLY = 2
|
2018-12-15 09:33:25 +01:00
|
|
|
billing_schedule = models.SmallIntegerField() # type: int
|
|
|
|
|
|
|
|
# This is like analytic's FillState, but for billing
|
|
|
|
billed_through = models.DateTimeField() # type: datetime.datetime
|
|
|
|
next_billing_date = models.DateTimeField(db_index=True) # type: datetime.datetime
|
|
|
|
|
|
|
|
STANDARD = 1
|
|
|
|
PLUS = 2 # not available through self-serve signup
|
|
|
|
ENTERPRISE = 10
|
|
|
|
tier = models.SmallIntegerField() # type: int
|
|
|
|
|
|
|
|
ACTIVE = 1
|
|
|
|
ENDED = 2
|
|
|
|
NEVER_STARTED = 3
|
|
|
|
# You can only have 1 active subscription at a time
|
|
|
|
status = models.SmallIntegerField(default=ACTIVE) # type: int
|
|
|
|
|
|
|
|
# TODO maybe override setattr to ensure billing_cycle_anchor, etc are immutable
|
|
|
|
|
|
|
|
def get_active_plan(customer: Customer) -> Optional[CustomerPlan]:
|
|
|
|
return CustomerPlan.objects.filter(customer=customer, status=CustomerPlan.ACTIVE).first()
|
2018-12-12 23:23:15 +01:00
|
|
|
|
|
|
|
# Everything below here is legacy
|
|
|
|
|
2018-09-25 14:02:43 +02:00
|
|
|
class Plan(models.Model):
|
|
|
|
# The two possible values for nickname
|
|
|
|
CLOUD_MONTHLY = 'monthly'
|
|
|
|
CLOUD_ANNUAL = 'annual'
|
|
|
|
nickname = models.CharField(max_length=40, unique=True) # type: str
|
|
|
|
|
|
|
|
stripe_plan_id = models.CharField(max_length=255, unique=True) # type: str
|
|
|
|
|
|
|
|
class Coupon(models.Model):
|
|
|
|
percent_off = models.SmallIntegerField(unique=True) # type: int
|
|
|
|
stripe_coupon_id = models.CharField(max_length=255, unique=True) # type: str
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return '<Coupon: %s %s %s>' % (self.percent_off, self.stripe_coupon_id, self.id)
|
|
|
|
|
|
|
|
class BillingProcessor(models.Model):
|
2018-12-30 03:45:23 +01:00
|
|
|
log_row = models.ForeignKey(RealmAuditLog, on_delete=CASCADE) # RealmAuditLog
|
2018-09-25 14:02:43 +02:00
|
|
|
# Exactly one processor, the global processor, has realm=None.
|
2018-12-30 03:45:23 +01:00
|
|
|
realm = models.OneToOneField(Realm, null=True, on_delete=CASCADE) # type: Realm
|
2018-09-25 14:02:43 +02:00
|
|
|
|
|
|
|
DONE = 'done'
|
|
|
|
STARTED = 'started'
|
|
|
|
SKIPPED = 'skipped' # global processor only
|
|
|
|
STALLED = 'stalled' # realm processors only
|
|
|
|
state = models.CharField(max_length=20) # type: str
|
|
|
|
|
|
|
|
last_modified = models.DateTimeField(auto_now=True) # type: datetime.datetime
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return '<BillingProcessor: %s %s %s>' % (self.realm, self.log_row, self.id)
|