def test_get_credits_by_subscription_and_features_only_returns_active_credits( self): # Add credits to subscription product_credit = self.add_product_credit(100.00) sms_credit = self.add_sms_credit(200.00) user_credit = self.add_user_credit(300.00) # Deactivate one credit sms_credit.is_active = False sms_credit.save() # Check that get_credits_by_subscription_and_features only returns active credit lines product_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, is_product=True) self.assertEqual(1, product_credits.count()) self.assertEqual(product_credit, product_credits.first()) user_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, feature_type=FeatureType.USER) self.assertEqual(1, user_credits.count()) self.assertEqual(user_credit, user_credits.first()) sms_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, feature_type=FeatureType.SMS) self.assertEqual(0, sms_credits.count())
def get_credits_available_for_product_in_subscription(current_subscription): return sum([ credit_line.balance for credit_line in itertools.chain( CreditLine.get_credits_by_subscription_and_features(current_subscription, is_product=True), CreditLine.get_credits_by_subscription_and_features(current_subscription), ) ])
def get_credits_available_for_product_in_subscription(current_subscription): return sum([ credit_line.balance for credit_line in itertools.chain( CreditLine.get_credits_by_subscription_and_features(current_subscription, is_product=True), CreditLine.get_credits_by_subscription_and_features(current_subscription), ) ])
def test_get_credits_by_subscription_and_features_only_returns_active_credits(self): # Add credits to subscription product_credit = self.add_product_credit(100.00) sms_credit = self.add_sms_credit(200.00) user_credit = self.add_user_credit(300.00) # Deactivate one credit sms_credit.is_active = False sms_credit.save() # Check that get_credits_by_subscription_and_features only returns active credit lines product_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, is_product=True ) self.assertEqual(1, product_credits.count()) self.assertEqual(product_credit, product_credits.first()) user_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, feature_type=FeatureType.USER ) self.assertEqual(1, user_credits.count()) self.assertEqual(user_credit, user_credits.first()) sms_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, feature_type=FeatureType.SMS ) self.assertEqual(0, sms_credits.count())
def add_product_credit(self, amount): product_credit = CreditLine.add_credit(amount, subscription=self.subscription, is_product=True) product_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, is_product=True ) self.assertEqual(1, product_credits.count()) self.assertEqual(product_credit, product_credits.first()) return product_credit
def add_user_credit(self, amount): user_credit = CreditLine.add_credit(amount, subscription=self.subscription, feature_type=FeatureType.USER) user_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, feature_type=FeatureType.USER ) self.assertEqual(1, user_credits.count()) self.assertEqual(user_credit, user_credits.first()) return user_credit
def add_sms_credit(self, amount): sms_credit = CreditLine.add_credit(amount, subscription=self.subscription, feature_type=FeatureType.SMS) sms_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, feature_type=FeatureType.SMS ) self.assertEqual(1, sms_credits.count()) self.assertEqual(sms_credit, sms_credits.first()) return sms_credit
def add_product_credit(self, amount): product_credit = CreditLine.add_credit(amount, subscription=self.subscription, is_product=True) product_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, is_product=True ) self.assertEqual(1, product_credits.count()) self.assertEqual(product_credit, product_credits.first()) return product_credit
def add_user_credit(self, amount): user_credit = CreditLine.add_credit(amount, subscription=self.subscription, feature_type=FeatureType.USER) user_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, feature_type=FeatureType.USER ) self.assertEqual(1, user_credits.count()) self.assertEqual(user_credit, user_credits.first()) return user_credit
def add_sms_credit(self, amount): sms_credit = CreditLine.add_credit(amount, subscription=self.subscription, feature_type=FeatureType.SMS) sms_credits = CreditLine.get_credits_by_subscription_and_features( subscription=self.subscription, feature_type=FeatureType.SMS ) self.assertEqual(1, sms_credits.count()) self.assertEqual(sms_credit, sms_credits.first()) return sms_credit
def _get_credit_info(self, subscription): return { 'general_credit': self._fmt_credit( self._credit_grand_total( CreditLine.get_credits_by_subscription_and_features( subscription))), 'account_general_credit': self._fmt_credit( self._credit_grand_total( CreditLine.get_credits_for_account(subscription.account) if subscription.account else None)), }
def test_active_subscription_credits_transferred_to_account(self): credit_amount = random.randint(1, 10) CreditLine.add_credit( credit_amount, feature_type=FeatureType.SMS, subscription=self.current_subscription, ) self.domain.delete() subscription_credits = CreditLine.get_credits_by_subscription_and_features( self.current_subscription, feature_type=FeatureType.SMS, ) self.assertEqual(len(subscription_credits), 1) self.assertEqual(subscription_credits[0].balance, Decimal('0.0000')) account_credits = CreditLine.get_credits_for_account( self.current_subscription.account, feature_type=FeatureType.SMS, ) self.assertEqual(len(account_credits), 1) self.assertEqual(account_credits[0].balance, Decimal(credit_amount))
def test_active_subscription_credits_transferred_to_account(self): credit_amount = random.randint(1, 10) CreditLine.add_credit( credit_amount, feature_type=FeatureType.SMS, subscription=self.current_subscription, ) self.domain.delete() subscription_credits = CreditLine.get_credits_by_subscription_and_features( self.current_subscription, feature_type=FeatureType.SMS, ) self.assertEqual(len(subscription_credits), 1) self.assertEqual(subscription_credits[0].balance, Decimal('0.0000')) account_credits = CreditLine.get_credits_for_account( self.current_subscription.account, feature_type=FeatureType.SMS, ) self.assertEqual(len(account_credits), 1) self.assertEqual(account_credits[0].balance, Decimal(credit_amount))
def test_active_subscription_credits_transferred_to_account(self): credit_amount = random.randint(1, 10) CreditLine.add_credit( credit_amount, feature_type=FeatureType.SMS, subscription=self.current_subscription, ) self.domain.delete() # Check that get_credits_by_subscription_and_features does not return the old deactivated credit line subscription_credits = CreditLine.get_credits_by_subscription_and_features( self.current_subscription, feature_type=FeatureType.SMS, ) self.assertEqual(len(subscription_credits), 0) # Check that old credit line has been tranferred to accoun account_credits = CreditLine.get_credits_for_account( self.current_subscription.account, feature_type=FeatureType.SMS, ) self.assertEqual(len(account_credits), 1) self.assertEqual(account_credits[0].balance, Decimal(credit_amount))
def test_active_subscription_credits_transferred_to_account(self): credit_amount = random.randint(1, 10) CreditLine.add_credit( credit_amount, feature_type=FeatureType.SMS, subscription=self.current_subscription, ) self.domain.delete() # Check that get_credits_by_subscription_and_features does not return the old deactivated credit line subscription_credits = CreditLine.get_credits_by_subscription_and_features( self.current_subscription, feature_type=FeatureType.SMS, ) self.assertEqual(len(subscription_credits), 0) # Check that old credit line has been tranferred to accoun account_credits = CreditLine.get_credits_for_account( self.current_subscription.account, feature_type=FeatureType.SMS, ) self.assertEqual(len(account_credits), 1) self.assertEqual(account_credits[0].balance, Decimal(credit_amount))
def send_prepaid_credits_export(): if settings.ENTERPRISE_MODE: return headers = [ 'Account Name', 'Project Space', 'Edition', 'Start Date', 'End Date', '# General Credits', '# Product Credits', '# User Credits', '# SMS Credits', 'Last Date Modified' ] body = [] for subscription in Subscription.visible_objects.filter( service_type=SubscriptionType.PRODUCT, ).order_by('subscriber__domain', 'id'): general_credit_lines = CreditLine.get_credits_by_subscription_and_features(subscription) product_credit_lines = CreditLine.get_credits_by_subscription_and_features(subscription, is_product=True) user_credit_lines = CreditLine.get_credits_by_subscription_and_features( subscription, feature_type=FeatureType.USER) sms_credit_lines = CreditLine.get_credits_by_subscription_and_features( subscription, feature_type=FeatureType.SMS) all_credit_lines = general_credit_lines | product_credit_lines | user_credit_lines | sms_credit_lines body.append([ subscription.account.name, subscription.subscriber.domain, subscription.plan_version.plan.edition, subscription.date_start, subscription.date_end, sum(credit_line.balance for credit_line in general_credit_lines), sum(credit_line.balance for credit_line in product_credit_lines), sum(credit_line.balance for credit_line in user_credit_lines), sum(credit_line.balance for credit_line in sms_credit_lines), max( credit_line.last_modified for credit_line in all_credit_lines ).strftime(SERVER_DATETIME_FORMAT_NO_SEC) if all_credit_lines else 'N/A', ]) for account in BillingAccount.objects.order_by('name', 'id'): general_credit_lines = CreditLine.get_credits_for_account(account) product_credit_lines = CreditLine.get_credits_for_account(account, is_product=True) user_credit_lines = CreditLine.get_credits_for_account(account, feature_type=FeatureType.USER) sms_credit_lines = CreditLine.get_credits_for_account(account, feature_type=FeatureType.SMS) all_credit_lines = general_credit_lines | product_credit_lines | user_credit_lines | sms_credit_lines body.append([ account.name, '', '', '', '', sum(credit_line.balance for credit_line in general_credit_lines), sum(credit_line.balance for credit_line in product_credit_lines), sum(credit_line.balance for credit_line in user_credit_lines), sum(credit_line.balance for credit_line in sms_credit_lines), max( credit_line.last_modified for credit_line in all_credit_lines ).strftime(SERVER_DATETIME_FORMAT_NO_SEC) if all_credit_lines else 'N/A', ]) file_obj = io.StringIO() writer = csv.writer(file_obj) writer.writerow(headers) for row in body: writer.writerow([ val if isinstance(val, six.text_type) else bytes(val) for val in row ]) date_string = datetime.datetime.utcnow().strftime(SERVER_DATE_FORMAT) filename = 'prepaid-credits-export_%s_%s.csv' % (settings.SERVER_ENVIRONMENT, date_string) send_HTML_email( '[%s] Prepaid Credits Export - %s' % (settings.SERVER_ENVIRONMENT, date_string), settings.ACCOUNTS_EMAIL, 'See attached file.', file_attachments=[{'file_obj': file_obj, 'title': filename, 'mimetype': 'text/csv'}], )
def send_prepaid_credits_export(): if settings.ENTERPRISE_MODE: return headers = [ 'Account Name', 'Project Space', 'Edition', 'Start Date', 'End Date', '# General Credits', '# Product Credits', '# User Credits', '# SMS Credits', 'Last Date Modified' ] body = [] for subscription in Subscription.visible_objects.filter( service_type=SubscriptionType.PRODUCT, ).order_by('subscriber__domain', 'id'): general_credit_lines = CreditLine.get_credits_by_subscription_and_features(subscription) product_credit_lines = CreditLine.get_credits_by_subscription_and_features(subscription, is_product=True) user_credit_lines = CreditLine.get_credits_by_subscription_and_features( subscription, feature_type=FeatureType.USER) sms_credit_lines = CreditLine.get_credits_by_subscription_and_features( subscription, feature_type=FeatureType.SMS) all_credit_lines = general_credit_lines | product_credit_lines | user_credit_lines | sms_credit_lines body.append([ subscription.account.name, subscription.subscriber.domain, subscription.plan_version.plan.edition, subscription.date_start, subscription.date_end, sum(credit_line.balance for credit_line in general_credit_lines), sum(credit_line.balance for credit_line in product_credit_lines), sum(credit_line.balance for credit_line in user_credit_lines), sum(credit_line.balance for credit_line in sms_credit_lines), max( credit_line.last_modified for credit_line in all_credit_lines ).strftime(SERVER_DATETIME_FORMAT_NO_SEC) if all_credit_lines else 'N/A', ]) for account in BillingAccount.objects.order_by('name', 'id'): general_credit_lines = CreditLine.get_credits_for_account(account) product_credit_lines = CreditLine.get_credits_for_account(account, is_product=True) user_credit_lines = CreditLine.get_credits_for_account(account, feature_type=FeatureType.USER) sms_credit_lines = CreditLine.get_credits_for_account(account, feature_type=FeatureType.SMS) all_credit_lines = general_credit_lines | product_credit_lines | user_credit_lines | sms_credit_lines body.append([ account.name, '', '', '', '', sum(credit_line.balance for credit_line in general_credit_lines), sum(credit_line.balance for credit_line in product_credit_lines), sum(credit_line.balance for credit_line in user_credit_lines), sum(credit_line.balance for credit_line in sms_credit_lines), max( credit_line.last_modified for credit_line in all_credit_lines ).strftime(SERVER_DATETIME_FORMAT_NO_SEC) if all_credit_lines else 'N/A', ]) file_obj = io.StringIO() writer = csv.writer(file_obj) writer.writerow(headers) for row in body: writer.writerow([ val if isinstance(val, six.text_type) else six.binary_type(val) for val in row ]) date_string = datetime.datetime.utcnow().strftime(SERVER_DATE_FORMAT) filename = 'prepaid-credits-export_%s_%s.csv' % (settings.SERVER_ENVIRONMENT, date_string) send_HTML_email( '[%s] Prepaid Credits Export - %s' % (settings.SERVER_ENVIRONMENT, date_string), settings.ACCOUNTS_EMAIL, 'See attached file.', file_attachments=[{'file_obj': file_obj, 'title': filename, 'mimetype': 'text/csv'}], )
def _get_credit_total(self, subscription): credit_lines = CreditLine.get_credits_by_subscription_and_features( subscription) return sum([c.balance for c in credit_lines])