2018-12-15 09:33:25 +01:00
|
|
|
from datetime import datetime
|
2018-12-12 19:41:03 +01:00
|
|
|
from decimal import Decimal
|
2018-01-30 21:03:59 +01:00
|
|
|
from functools import wraps
|
2018-01-30 20:49:25 +01:00
|
|
|
import logging
|
|
|
|
import os
|
2018-12-15 09:33:25 +01:00
|
|
|
from typing import Any, Callable, Dict, Optional, TypeVar, Tuple, cast
|
2018-06-28 00:48:51 +02:00
|
|
|
import ujson
|
2018-01-30 20:49:25 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
2018-06-28 00:48:51 +02:00
|
|
|
from django.db import transaction
|
2018-01-30 21:03:59 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-08-14 03:33:31 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2018-07-13 17:34:39 +02:00
|
|
|
from django.core.signing import Signer
|
2018-01-30 20:49:25 +01:00
|
|
|
import stripe
|
|
|
|
|
2018-01-30 21:03:59 +01:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2018-01-30 20:49:25 +01:00
|
|
|
from zerver.lib.logging_util import log_to_file
|
2018-06-28 00:48:51 +02:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
|
2018-07-13 17:34:39 +02:00
|
|
|
from zerver.lib.utils import generate_random_token
|
2018-08-09 21:38:22 +02:00
|
|
|
from zerver.lib.actions import do_change_plan_type
|
2018-06-28 00:48:51 +02:00
|
|
|
from zerver.models import Realm, UserProfile, RealmAuditLog
|
2018-12-15 09:33:25 +01:00
|
|
|
from corporate.models import Customer, CustomerPlan, Plan, Coupon, \
|
|
|
|
get_active_plan
|
2018-01-30 20:49:25 +01:00
|
|
|
from zproject.settings import get_secret
|
|
|
|
|
|
|
|
STRIPE_PUBLISHABLE_KEY = get_secret('stripe_publishable_key')
|
2018-03-31 04:13:44 +02:00
|
|
|
stripe.api_key = get_secret('stripe_secret_key')
|
2018-01-30 20:49:25 +01:00
|
|
|
|
|
|
|
BILLING_LOG_PATH = os.path.join('/var/log/zulip'
|
|
|
|
if not settings.DEVELOPMENT
|
|
|
|
else settings.DEVELOPMENT_LOG_DIRECTORY,
|
|
|
|
'billing.log')
|
2018-09-25 12:33:30 +02:00
|
|
|
billing_logger = logging.getLogger('corporate.stripe')
|
2018-01-30 20:49:25 +01:00
|
|
|
log_to_file(billing_logger, BILLING_LOG_PATH)
|
|
|
|
log_to_file(logging.getLogger('stripe'), BILLING_LOG_PATH)
|
|
|
|
|
2018-01-30 21:03:59 +01:00
|
|
|
CallableT = TypeVar('CallableT', bound=Callable[..., Any])
|
|
|
|
|
2018-12-22 01:43:44 +01:00
|
|
|
MIN_INVOICED_LICENSES = 30
|
2018-09-08 00:49:54 +02:00
|
|
|
DEFAULT_INVOICE_DAYS_UNTIL_DUE = 30
|
|
|
|
|
2018-03-31 04:13:44 +02:00
|
|
|
def get_seat_count(realm: Realm) -> int:
|
|
|
|
return UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False).count()
|
|
|
|
|
2018-07-13 17:34:39 +02:00
|
|
|
def sign_string(string: str) -> Tuple[str, str]:
|
|
|
|
salt = generate_random_token(64)
|
|
|
|
signer = Signer(salt=salt)
|
|
|
|
return signer.sign(string), salt
|
|
|
|
|
|
|
|
def unsign_string(signed_string: str, salt: str) -> str:
|
|
|
|
signer = Signer(salt=salt)
|
|
|
|
return signer.unsign(signed_string)
|
|
|
|
|
2018-12-15 09:33:25 +01:00
|
|
|
# Be extremely careful changing this function. Historical billing periods
|
|
|
|
# are not stored anywhere, and are just computed on the fly using this
|
|
|
|
# function. Any change you make here should return the same value (or be
|
|
|
|
# within a few seconds) for basically any value from when the billing system
|
|
|
|
# went online to within a year from now.
|
|
|
|
def add_months(dt: datetime, months: int) -> datetime:
|
|
|
|
assert(months >= 0)
|
|
|
|
# It's fine that the max day in Feb is 28 for leap years.
|
|
|
|
MAX_DAY_FOR_MONTH = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30,
|
|
|
|
7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
|
|
|
|
year = dt.year
|
|
|
|
month = dt.month + months
|
|
|
|
while month > 12:
|
|
|
|
year += 1
|
|
|
|
month -= 12
|
|
|
|
day = min(dt.day, MAX_DAY_FOR_MONTH[month])
|
|
|
|
# datetimes don't support leap seconds, so don't need to worry about those
|
|
|
|
return dt.replace(year=year, month=month, day=day)
|
|
|
|
|
|
|
|
def next_month(billing_cycle_anchor: datetime, dt: datetime) -> datetime:
|
|
|
|
estimated_months = round((dt - billing_cycle_anchor).days * 12. / 365)
|
|
|
|
for months in range(max(estimated_months - 1, 0), estimated_months + 2):
|
|
|
|
proposed_next_month = add_months(billing_cycle_anchor, months)
|
|
|
|
if 20 < (proposed_next_month - dt).days < 40:
|
|
|
|
return proposed_next_month
|
|
|
|
raise AssertionError('Something wrong in next_month calculation with '
|
|
|
|
'billing_cycle_anchor: %s, dt: %s' % (billing_cycle_anchor, dt))
|
|
|
|
|
|
|
|
# TODO take downgrade into account
|
|
|
|
def next_renewal_date(plan: CustomerPlan) -> datetime:
|
|
|
|
months_per_period = {
|
|
|
|
CustomerPlan.ANNUAL: 12,
|
|
|
|
CustomerPlan.MONTHLY: 1,
|
|
|
|
}[plan.billing_schedule]
|
|
|
|
periods = 1
|
|
|
|
dt = plan.billing_cycle_anchor
|
|
|
|
while dt <= plan.billed_through:
|
|
|
|
dt = add_months(plan.billing_cycle_anchor, months_per_period * periods)
|
|
|
|
periods += 1
|
|
|
|
return dt
|
|
|
|
|
|
|
|
def renewal_amount(plan: CustomerPlan) -> int: # nocoverage: TODO
|
|
|
|
if plan.fixed_price is not None:
|
|
|
|
basis = plan.fixed_price
|
|
|
|
elif plan.automanage_licenses:
|
|
|
|
assert(plan.price_per_license is not None)
|
|
|
|
basis = plan.price_per_license * get_seat_count(plan.customer.realm)
|
|
|
|
else:
|
|
|
|
assert(plan.price_per_license is not None)
|
|
|
|
basis = plan.price_per_license * plan.licenses
|
|
|
|
if plan.discount is None:
|
|
|
|
return basis
|
|
|
|
# TODO: figure out right thing to do with Decimal
|
|
|
|
return int(float(basis * (100 - plan.discount) / 100) + .00001)
|
|
|
|
|
2018-07-27 17:47:03 +02:00
|
|
|
class BillingError(Exception):
|
2018-08-06 06:16:29 +02:00
|
|
|
# error messages
|
2018-08-11 01:23:42 +02:00
|
|
|
CONTACT_SUPPORT = _("Something went wrong. Please contact %s." % (settings.ZULIP_ADMINISTRATOR,))
|
2018-08-06 06:16:29 +02:00
|
|
|
TRY_RELOADING = _("Something went wrong. Please reload the page.")
|
|
|
|
|
|
|
|
# description is used only for tests
|
2018-12-22 05:29:25 +01:00
|
|
|
def __init__(self, description: str, message: str=CONTACT_SUPPORT) -> None:
|
2018-08-06 06:16:29 +02:00
|
|
|
self.description = description
|
|
|
|
self.message = message
|
2018-07-27 17:47:03 +02:00
|
|
|
|
2018-08-06 23:07:26 +02:00
|
|
|
class StripeCardError(BillingError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class StripeConnectionError(BillingError):
|
|
|
|
pass
|
|
|
|
|
2018-01-30 21:03:59 +01:00
|
|
|
def catch_stripe_errors(func: CallableT) -> CallableT:
|
|
|
|
@wraps(func)
|
|
|
|
def wrapped(*args: Any, **kwargs: Any) -> Any:
|
2018-07-26 10:16:20 +02:00
|
|
|
if settings.DEVELOPMENT and not settings.TEST_SUITE: # nocoverage
|
|
|
|
if STRIPE_PUBLISHABLE_KEY is None:
|
2018-08-06 06:16:29 +02:00
|
|
|
raise BillingError('missing stripe config', "Missing Stripe config. "
|
|
|
|
"See https://zulip.readthedocs.io/en/latest/subsystems/billing.html.")
|
2018-01-30 21:03:59 +01:00
|
|
|
try:
|
|
|
|
return func(*args, **kwargs)
|
2018-08-06 23:07:26 +02:00
|
|
|
# See https://stripe.com/docs/api/python#error_handling, though
|
|
|
|
# https://stripe.com/docs/api/ruby#error_handling suggests there are additional fields, and
|
|
|
|
# https://stripe.com/docs/error-codes gives a more detailed set of error codes
|
2018-01-30 21:03:59 +01:00
|
|
|
except stripe.error.StripeError as e:
|
2018-08-06 23:07:26 +02:00
|
|
|
err = e.json_body.get('error', {})
|
|
|
|
billing_logger.error("Stripe error: %s %s %s %s" % (
|
|
|
|
e.http_status, err.get('type'), err.get('code'), err.get('param')))
|
2018-01-30 21:03:59 +01:00
|
|
|
if isinstance(e, stripe.error.CardError):
|
2018-08-06 23:07:26 +02:00
|
|
|
# TODO: Look into i18n for this
|
|
|
|
raise StripeCardError('card error', err.get('message'))
|
|
|
|
if isinstance(e, stripe.error.RateLimitError) or \
|
|
|
|
isinstance(e, stripe.error.APIConnectionError): # nocoverage TODO
|
|
|
|
raise StripeConnectionError(
|
|
|
|
'stripe connection error',
|
|
|
|
_("Something went wrong. Please wait a few seconds and try again."))
|
|
|
|
raise BillingError('other stripe error', BillingError.CONTACT_SUPPORT)
|
2018-01-30 21:03:59 +01:00
|
|
|
return wrapped # type: ignore # https://github.com/python/mypy/issues/1927
|
|
|
|
|
|
|
|
@catch_stripe_errors
|
2018-08-06 18:22:55 +02:00
|
|
|
def stripe_get_customer(stripe_customer_id: str) -> stripe.Customer:
|
2018-11-17 04:41:42 +01:00
|
|
|
return stripe.Customer.retrieve(stripe_customer_id, expand=["default_source"])
|
2018-03-31 04:13:44 +02:00
|
|
|
|
|
|
|
@catch_stripe_errors
|
2018-12-15 09:33:25 +01:00
|
|
|
def do_create_customer(user: UserProfile, stripe_token: Optional[str]=None) -> Customer:
|
2018-03-31 04:13:44 +02:00
|
|
|
realm = user.realm
|
2018-08-23 06:44:00 +02:00
|
|
|
# We could do a better job of handling race conditions here, but if two
|
|
|
|
# people from a realm try to upgrade at exactly the same time, the main
|
|
|
|
# bad thing that will happen is that we will create an extra stripe
|
|
|
|
# customer that we can delete or ignore.
|
2018-03-31 04:13:44 +02:00
|
|
|
stripe_customer = stripe.Customer.create(
|
|
|
|
description="%s (%s)" % (realm.string_id, realm.name),
|
2018-08-08 13:17:10 +02:00
|
|
|
email=user.email,
|
2018-03-31 04:13:44 +02:00
|
|
|
metadata={'realm_id': realm.id, 'realm_str': realm.string_id},
|
2018-12-12 19:41:03 +01:00
|
|
|
source=stripe_token)
|
2018-06-28 00:48:51 +02:00
|
|
|
event_time = timestamp_to_datetime(stripe_customer.created)
|
2018-08-23 03:40:38 +02:00
|
|
|
with transaction.atomic():
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CUSTOMER_CREATED,
|
|
|
|
event_time=event_time)
|
2018-08-23 07:47:05 +02:00
|
|
|
if stripe_token is not None:
|
|
|
|
RealmAuditLog.objects.create(
|
2018-09-05 09:40:29 +02:00
|
|
|
realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CARD_CHANGED,
|
2018-08-23 07:47:05 +02:00
|
|
|
event_time=event_time)
|
2018-12-15 09:33:25 +01:00
|
|
|
customer = Customer.objects.create(realm=realm, stripe_customer_id=stripe_customer.id)
|
2018-08-22 07:49:48 +02:00
|
|
|
user.is_billing_admin = True
|
|
|
|
user.save(update_fields=["is_billing_admin"])
|
2018-12-15 09:33:25 +01:00
|
|
|
return customer
|
2018-03-31 04:13:44 +02:00
|
|
|
|
2018-08-14 03:33:31 +02:00
|
|
|
@catch_stripe_errors
|
|
|
|
def do_replace_payment_source(user: UserProfile, stripe_token: str) -> stripe.Customer:
|
|
|
|
stripe_customer = stripe_get_customer(Customer.objects.get(realm=user.realm).stripe_customer_id)
|
|
|
|
stripe_customer.source = stripe_token
|
|
|
|
# Deletes existing card: https://stripe.com/docs/api#update_customer-source
|
|
|
|
# This can also have other side effects, e.g. it will try to pay certain past-due
|
|
|
|
# invoices: https://stripe.com/docs/api#update_customer
|
2018-10-18 19:56:17 +02:00
|
|
|
updated_stripe_customer = stripe.Customer.save(stripe_customer)
|
2018-08-14 03:33:31 +02:00
|
|
|
RealmAuditLog.objects.create(
|
2018-09-05 09:40:29 +02:00
|
|
|
realm=user.realm, acting_user=user, event_type=RealmAuditLog.STRIPE_CARD_CHANGED,
|
2018-08-14 03:33:31 +02:00
|
|
|
event_time=timezone_now())
|
|
|
|
return updated_stripe_customer
|
|
|
|
|
2018-12-15 09:33:25 +01:00
|
|
|
# Returns Customer instead of stripe_customer so that we don't make a Stripe
|
|
|
|
# API call if there's nothing to update
|
|
|
|
def update_or_create_stripe_customer(user: UserProfile, stripe_token: Optional[str]=None) -> Customer:
|
|
|
|
realm = user.realm
|
|
|
|
customer = Customer.objects.filter(realm=realm).first()
|
|
|
|
if customer is None:
|
|
|
|
return do_create_customer(user, stripe_token=stripe_token)
|
|
|
|
if stripe_token is not None:
|
|
|
|
do_replace_payment_source(user, stripe_token)
|
|
|
|
return customer
|
|
|
|
|
|
|
|
def compute_plan_parameters(
|
|
|
|
automanage_licenses: bool, billing_schedule: int,
|
|
|
|
discount: Optional[Decimal]) -> Tuple[datetime, datetime, datetime, int]:
|
|
|
|
# Everything in Stripe is stored as timestamps with 1 second resolution,
|
|
|
|
# so standardize on 1 second resolution.
|
|
|
|
# TODO talk about leapseconds?
|
|
|
|
billing_cycle_anchor = timezone_now().replace(microsecond=0)
|
|
|
|
if billing_schedule == CustomerPlan.ANNUAL:
|
|
|
|
# TODO use variables to account for Zulip Plus
|
|
|
|
price_per_license = 8000
|
|
|
|
period_end = add_months(billing_cycle_anchor, 12)
|
|
|
|
elif billing_schedule == CustomerPlan.MONTHLY:
|
|
|
|
price_per_license = 800
|
|
|
|
period_end = add_months(billing_cycle_anchor, 1)
|
|
|
|
else:
|
|
|
|
raise AssertionError('Unknown billing_schedule: {}'.format(billing_schedule))
|
|
|
|
if discount is not None:
|
|
|
|
# There are no fractional cents in Stripe, so round down to nearest integer.
|
|
|
|
price_per_license = int(float(price_per_license * (1 - discount / 100)) + .00001)
|
|
|
|
next_billing_date = period_end
|
|
|
|
if automanage_licenses:
|
|
|
|
next_billing_date = add_months(billing_cycle_anchor, 1)
|
|
|
|
return billing_cycle_anchor, next_billing_date, period_end, price_per_license
|
|
|
|
|
|
|
|
# Only used for cloud signups
|
2018-03-31 04:13:44 +02:00
|
|
|
@catch_stripe_errors
|
2018-12-15 09:33:25 +01:00
|
|
|
def process_initial_upgrade(user: UserProfile, licenses: int, automanage_licenses: bool,
|
|
|
|
billing_schedule: int, stripe_token: Optional[str]) -> None:
|
|
|
|
realm = user.realm
|
|
|
|
customer = update_or_create_stripe_customer(user, stripe_token=stripe_token)
|
|
|
|
# TODO write a test for this
|
|
|
|
if CustomerPlan.objects.filter(customer=customer, status=CustomerPlan.ACTIVE).exists(): # nocoverage
|
2018-11-28 00:20:58 +01:00
|
|
|
# Unlikely race condition from two people upgrading (clicking "Make payment")
|
|
|
|
# at exactly the same time. Doesn't fully resolve the race condition, but having
|
|
|
|
# a check here reduces the likelihood.
|
2018-12-15 09:33:25 +01:00
|
|
|
billing_logger.warning(
|
|
|
|
"Customer {} trying to upgrade, but has an active subscription".format(customer))
|
2018-08-06 06:16:29 +02:00
|
|
|
raise BillingError('subscribing with existing subscription', BillingError.TRY_RELOADING)
|
2018-12-15 09:33:25 +01:00
|
|
|
|
|
|
|
billing_cycle_anchor, next_billing_date, period_end, price_per_license = compute_plan_parameters(
|
|
|
|
automanage_licenses, billing_schedule, customer.default_discount)
|
|
|
|
# The main design constraint in this function is that if you upgrade with a credit card, and the
|
|
|
|
# charge fails, everything should be rolled back as if nothing had happened. This is because we
|
|
|
|
# expect frequent card failures on initial signup.
|
|
|
|
# Hence, if we're going to charge a card, do it at the beginning, even if we later may have to
|
|
|
|
# adjust the number of licenses.
|
|
|
|
charge_automatically = stripe_token is not None
|
|
|
|
if charge_automatically:
|
|
|
|
stripe_charge = stripe.Charge.create(
|
|
|
|
amount=price_per_license * licenses,
|
|
|
|
currency='usd',
|
|
|
|
customer=customer.stripe_customer_id,
|
|
|
|
description="Upgrade to Zulip Standard, ${} x {}".format(price_per_license/100, licenses),
|
|
|
|
receipt_email=user.email,
|
|
|
|
statement_descriptor='Zulip Standard')
|
|
|
|
# Not setting a period start and end, but maybe we should? Unclear what will make things
|
|
|
|
# most similar to the renewal case from an accounting perspective.
|
|
|
|
stripe.InvoiceItem.create(
|
|
|
|
amount=price_per_license * licenses * -1,
|
|
|
|
currency='usd',
|
|
|
|
customer=customer.stripe_customer_id,
|
|
|
|
description="Payment (Card ending in {})".format(cast(stripe.Card, stripe_charge.source).last4),
|
|
|
|
discountable=False)
|
|
|
|
|
|
|
|
# TODO: The correctness of this relies on user creation, deactivation, etc being
|
|
|
|
# in a transaction.atomic() with the relevant RealmAuditLog entries
|
|
|
|
with transaction.atomic():
|
|
|
|
# billed_licenses can greater than licenses if users are added between the start of
|
|
|
|
# this function (process_initial_upgrade) and now
|
|
|
|
billed_licenses = max(get_seat_count(realm), licenses)
|
|
|
|
plan_params = {
|
|
|
|
'licenses': billed_licenses,
|
|
|
|
'automanage_licenses': automanage_licenses,
|
|
|
|
'charge_automatically': charge_automatically,
|
|
|
|
'price_per_license': price_per_license,
|
|
|
|
'discount': customer.default_discount,
|
|
|
|
'billing_cycle_anchor': billing_cycle_anchor,
|
|
|
|
'billing_schedule': billing_schedule,
|
|
|
|
'tier': CustomerPlan.STANDARD}
|
|
|
|
CustomerPlan.objects.create(
|
|
|
|
customer=customer,
|
|
|
|
billed_through=billing_cycle_anchor,
|
|
|
|
next_billing_date=next_billing_date,
|
|
|
|
**plan_params)
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm, acting_user=user, event_time=billing_cycle_anchor,
|
|
|
|
event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED,
|
|
|
|
# TODO: add tests for licenses
|
|
|
|
# Only 'licenses' is guaranteed to be useful to automated tools. The other extra_data
|
|
|
|
# fields can change in the future and are only meant to assist manual debugging.
|
|
|
|
extra_data=ujson.dumps(plan_params))
|
|
|
|
description = 'Zulip Standard'
|
|
|
|
if customer.default_discount is not None: # nocoverage: TODO
|
|
|
|
description += ' (%s%% off)' % (customer.default_discount,)
|
|
|
|
stripe.InvoiceItem.create(
|
|
|
|
currency='usd',
|
|
|
|
customer=customer.stripe_customer_id,
|
|
|
|
description=description,
|
|
|
|
discountable=False,
|
|
|
|
period = {'start': datetime_to_timestamp(billing_cycle_anchor),
|
|
|
|
'end': datetime_to_timestamp(period_end)},
|
|
|
|
quantity=billed_licenses,
|
|
|
|
unit_amount=price_per_license)
|
|
|
|
|
2018-09-08 00:49:54 +02:00
|
|
|
if charge_automatically:
|
|
|
|
billing_method = 'charge_automatically'
|
|
|
|
days_until_due = None
|
|
|
|
else:
|
|
|
|
billing_method = 'send_invoice'
|
|
|
|
days_until_due = DEFAULT_INVOICE_DAYS_UNTIL_DUE
|
2018-12-15 09:33:25 +01:00
|
|
|
stripe_invoice = stripe.Invoice.create(
|
|
|
|
auto_advance=True,
|
2018-09-08 00:49:54 +02:00
|
|
|
billing=billing_method,
|
2018-12-15 09:33:25 +01:00
|
|
|
customer=customer.stripe_customer_id,
|
2018-09-08 00:49:54 +02:00
|
|
|
days_until_due=days_until_due,
|
2018-12-15 09:33:25 +01:00
|
|
|
statement_descriptor='Zulip Standard')
|
|
|
|
stripe.Invoice.finalize_invoice(stripe_invoice)
|
2018-07-27 15:37:04 +02:00
|
|
|
|
2018-12-15 09:33:25 +01:00
|
|
|
do_change_plan_type(realm, Realm.STANDARD)
|
2018-07-03 21:49:55 +02:00
|
|
|
|
2018-12-12 19:41:03 +01:00
|
|
|
def attach_discount_to_realm(user: UserProfile, discount: Decimal) -> None:
|
2018-08-23 07:45:19 +02:00
|
|
|
customer = Customer.objects.filter(realm=user.realm).first()
|
|
|
|
if customer is None:
|
2018-12-15 09:33:25 +01:00
|
|
|
customer = do_create_customer(user)
|
2018-12-12 19:41:03 +01:00
|
|
|
customer.default_discount = discount
|
|
|
|
customer.save()
|
2018-08-23 07:45:19 +02:00
|
|
|
|
2018-12-12 07:47:53 +01:00
|
|
|
def process_downgrade(user: UserProfile) -> None: # nocoverage
|
|
|
|
pass
|
2018-12-15 09:33:25 +01:00
|
|
|
|
|
|
|
def estimate_annual_recurring_revenue_by_realm() -> Dict[str, int]: # nocoverage
|
|
|
|
annual_revenue = {}
|
|
|
|
for plan in CustomerPlan.objects.filter(
|
|
|
|
status=CustomerPlan.ACTIVE).select_related('customer__realm'):
|
|
|
|
renewal_cents = renewal_amount(plan)
|
|
|
|
if plan.billing_schedule == CustomerPlan.MONTHLY:
|
|
|
|
renewal_cents *= 12
|
|
|
|
# TODO: Decimal stuff
|
|
|
|
annual_revenue[plan.customer.realm.string_id] = int(renewal_cents / 100)
|
|
|
|
return annual_revenue
|