Update the monthly revenue script to work with discounts.
This commit is contained in:
parent
135859bc30
commit
e445df69ca
1 changed files with 18 additions and 9 deletions
|
@ -1,34 +1,43 @@
|
||||||
from app import stripe
|
from app import billing
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
EXCLUDE_CID = {'cus_2iVlmwz8CpHgOj'}
|
|
||||||
|
|
||||||
offset = 0
|
offset = 0
|
||||||
total_monthly_revenue = 0
|
|
||||||
|
|
||||||
def empty_tuple():
|
def empty_tuple():
|
||||||
return (0, 0)
|
return (0, 0)
|
||||||
|
|
||||||
plan_revenue = defaultdict(empty_tuple)
|
plan_revenue = defaultdict(empty_tuple)
|
||||||
|
|
||||||
batch = stripe.Customer.all(count=100, offset=offset)
|
batch = billing.Customer.all(count=100, offset=offset)
|
||||||
while batch.data:
|
while batch.data:
|
||||||
for cust in batch.data:
|
for cust in batch.data:
|
||||||
if cust.id not in EXCLUDE_CID and cust.subscription:
|
if cust.subscription:
|
||||||
sub = cust.subscription
|
sub = cust.subscription
|
||||||
total_monthly_revenue += sub.plan.amount * sub.quantity
|
total_customer_revenue = sub.plan.amount * sub.quantity
|
||||||
|
|
||||||
|
if cust.discount and cust.discount.coupon:
|
||||||
|
coupon = cust.discount.coupon
|
||||||
|
|
||||||
|
if coupon.percent_off:
|
||||||
|
total_customer_revenue *= (1 - coupon.percent_off/100.0)
|
||||||
|
|
||||||
|
if coupon.amount_off:
|
||||||
|
total_customer_revenue -= coupon.amount_off
|
||||||
|
|
||||||
subscribers, revenue = plan_revenue[sub.plan.id]
|
subscribers, revenue = plan_revenue[sub.plan.id]
|
||||||
plan_revenue[sub.plan.id] = (subscribers + 1,
|
plan_revenue[sub.plan.id] = (subscribers + 1,
|
||||||
revenue + sub.plan.amount * sub.quantity)
|
revenue + total_customer_revenue)
|
||||||
offset += len(batch.data)
|
offset += len(batch.data)
|
||||||
batch = stripe.Customer.all(count=100, offset=offset)
|
batch = billing.Customer.all(count=100, offset=offset)
|
||||||
|
|
||||||
def format_money(total_cents):
|
def format_money(total_cents):
|
||||||
dollars = total_cents / 100
|
dollars = total_cents / 100
|
||||||
cents = total_cents % 100
|
cents = total_cents % 100
|
||||||
return dollars, cents
|
return dollars, cents
|
||||||
|
|
||||||
|
total_monthly_revenue = 0
|
||||||
for plan_id, (subs, rev) in plan_revenue.items():
|
for plan_id, (subs, rev) in plan_revenue.items():
|
||||||
|
total_monthly_revenue += rev
|
||||||
d, c = format_money(rev)
|
d, c = format_money(rev)
|
||||||
print '%s: $%d.%02d(%s)' % (plan_id, d, c, subs)
|
print '%s: $%d.%02d(%s)' % (plan_id, d, c, subs)
|
||||||
|
|
||||||
|
|
Reference in a new issue