2014-06-09 18:44:53 +00:00
|
|
|
from app import billing
|
2013-12-19 02:02:06 +00:00
|
|
|
from collections import defaultdict
|
2013-11-17 22:24:56 +00:00
|
|
|
|
|
|
|
offset = 0
|
|
|
|
|
2013-12-19 02:02:06 +00:00
|
|
|
def empty_tuple():
|
|
|
|
return (0, 0)
|
|
|
|
|
|
|
|
plan_revenue = defaultdict(empty_tuple)
|
|
|
|
|
2014-06-09 18:44:53 +00:00
|
|
|
batch = billing.Customer.all(count=100, offset=offset)
|
2013-11-17 22:24:56 +00:00
|
|
|
while batch.data:
|
|
|
|
for cust in batch.data:
|
2014-06-09 18:44:53 +00:00
|
|
|
if cust.subscription:
|
2013-11-17 22:24:56 +00:00
|
|
|
sub = cust.subscription
|
2014-06-09 18:44:53 +00:00
|
|
|
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
|
|
|
|
|
2013-12-19 02:02:06 +00:00
|
|
|
subscribers, revenue = plan_revenue[sub.plan.id]
|
|
|
|
plan_revenue[sub.plan.id] = (subscribers + 1,
|
2014-06-09 18:44:53 +00:00
|
|
|
revenue + total_customer_revenue)
|
2013-11-17 22:24:56 +00:00
|
|
|
offset += len(batch.data)
|
2014-06-09 18:44:53 +00:00
|
|
|
batch = billing.Customer.all(count=100, offset=offset)
|
2013-11-17 22:24:56 +00:00
|
|
|
|
2013-12-19 02:02:06 +00:00
|
|
|
def format_money(total_cents):
|
|
|
|
dollars = total_cents / 100
|
|
|
|
cents = total_cents % 100
|
|
|
|
return dollars, cents
|
|
|
|
|
2014-06-09 18:44:53 +00:00
|
|
|
total_monthly_revenue = 0
|
2013-12-19 02:02:06 +00:00
|
|
|
for plan_id, (subs, rev) in plan_revenue.items():
|
2014-06-09 18:44:53 +00:00
|
|
|
total_monthly_revenue += rev
|
2013-12-19 02:02:06 +00:00
|
|
|
d, c = format_money(rev)
|
|
|
|
print '%s: $%d.%02d(%s)' % (plan_id, d, c, subs)
|
|
|
|
|
|
|
|
d, c = format_money(total_monthly_revenue)
|
|
|
|
print 'Monthly revenue: $%d.%02d' % (d, c)
|