<s> from __future__ import unicode_literals <EOL> import decimal <EOL> from django . db import models <EOL> class StripeObjectManager ( models . Manager ) : <EOL> def exists_by_json ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return self . filter ( stripe_id = data [ "<STR_LIT:id>" ] ) . exists ( ) <EOL> def get_by_json ( self , data , field_name = "<STR_LIT:id>" ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( stripe_id = data [ field_name ] ) <EOL> class CustomerManager ( models . Manager ) : <EOL> def started_during ( self , year , month ) : <EOL> return self . exclude ( <EOL> current_subscription__status = "<STR_LIT>" <EOL> ) . filter ( <EOL> current_subscription__start__year = year , <EOL> current_subscription__start__month = month <EOL> ) <EOL> def active ( self ) : <EOL> return self . filter ( <EOL> current_subscription__status = "<STR_LIT>" <EOL> ) <EOL> def canceled ( self ) : <EOL> return self . filter ( <EOL> current_subscription__status = "<STR_LIT>" <EOL> ) <EOL> def canceled_during ( self , year , month ) : <EOL> return self . canceled ( ) . filter ( <EOL> current_subscription__canceled_at__year = year , <EOL> current_subscription__canceled_at__month = month , <EOL> ) <EOL> def started_plan_summary_for ( self , year , month ) : <EOL> return self . started_during ( year , month ) . values ( <EOL> "<STR_LIT>" <EOL> ) . order_by ( ) . annotate ( <EOL> count = models . Count ( "<STR_LIT>" ) <EOL> ) <EOL> def active_plan_summary ( self ) : <EOL> return self . active ( ) . values ( <EOL> "<STR_LIT>" <EOL> ) . order_by ( ) . annotate ( <EOL> count = models . Count ( "<STR_LIT>" ) <EOL> ) <EOL> def canceled_plan_summary_for ( self , year , month ) : <EOL> return self . canceled_during ( year , month ) . values ( <EOL> "<STR_LIT>" <EOL> ) . order_by ( ) . annotate ( <EOL> count = models . Count ( "<STR_LIT>" ) <EOL> ) <EOL> def churn ( self ) : <EOL> canceled = self . canceled ( ) . count ( ) <EOL> active = self . active ( ) . count ( ) <EOL> return decimal . Decimal ( str ( canceled ) ) / decimal . Decimal ( str ( active ) ) <EOL> class TransferManager ( models . Manager ) : <EOL> def during ( self , year , month ) : <EOL> return self . filter ( <EOL> date__year = year , <EOL> date__month = month <EOL> ) <EOL> def paid_totals_for ( self , year , month ) : <EOL> return self . during ( year , month ) . filter ( <EOL> status = "<STR_LIT>" <EOL> ) . aggregate ( <EOL> total_gross = models . Sum ( "<STR_LIT>" ) , <EOL> total_net = models . Sum ( "<STR_LIT>" ) , <EOL> total_charge_fees = models . Sum ( "<STR_LIT>" ) , <EOL> total_adjustment_fees = models . Sum ( "<STR_LIT>" ) , <EOL> total_refund_gross = models . Sum ( "<STR_LIT>" ) , <EOL> total_refund_fees = models . Sum ( "<STR_LIT>" ) , <EOL> total_validation_fees = models . Sum ( "<STR_LIT>" ) , <EOL> total_amount = models . Sum ( "<STR_LIT>" ) <EOL> ) <EOL> class ChargeManager ( models . Manager ) : <EOL> def during ( self , year , month ) : <EOL> return self . filter ( charge_created__year = year , charge_created__month = month ) <EOL> def paid_totals_for ( self , year , month ) : <EOL> return self . during ( year , month ) . filter ( paid = True ) . aggregate ( <EOL> total_amount = models . Sum ( "<STR_LIT>" ) , <EOL> total_fee = models . Sum ( "<STR_LIT>" ) , <EOL> total_refunded = models . Sum ( "<STR_LIT>" ) <EOL> ) </s>
