2019-11-28 08:08:45 +00:00
|
|
|
import datetime
|
2019-04-03 04:12:48 +00:00
|
|
|
import logging
|
|
|
|
|
2019-04-02 07:18:46 +00:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
2019-04-03 04:12:48 +00:00
|
|
|
from hosting.models import MonthlyHostingBill
|
2019-04-02 07:18:46 +00:00
|
|
|
from membership.models import CustomUser
|
|
|
|
from utils.stripe_utils import StripeUtils
|
|
|
|
|
2019-04-03 04:12:48 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-04-02 07:18:46 +00:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = '''Fetches invoices from Stripe and creates bills for a given
|
|
|
|
customer in the MonthlyHostingBill model'''
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument('customer_email', nargs='+', type=str)
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
try:
|
|
|
|
for email in options['customer_email']:
|
2019-11-28 08:08:45 +00:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.SUCCESS(
|
|
|
|
"---------------------------------------------")
|
|
|
|
)
|
2019-04-02 07:18:46 +00:00
|
|
|
stripe_utils = StripeUtils()
|
|
|
|
user = CustomUser.objects.get(email=email)
|
|
|
|
if hasattr(user, 'stripecustomer'):
|
2019-04-03 04:12:48 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS(
|
|
|
|
'Found %s. Fetching bills for him.' % email))
|
2019-04-03 04:22:49 +00:00
|
|
|
mhb = MonthlyHostingBill.objects.filter(
|
|
|
|
customer=user.stripecustomer).last()
|
2019-04-03 04:12:48 +00:00
|
|
|
created_gt = {}
|
|
|
|
if mhb is not None:
|
|
|
|
# fetch only invoices which is created after
|
|
|
|
# mhb.created, because we already have invoices till
|
|
|
|
# this date
|
2019-04-13 13:00:19 +00:00
|
|
|
created_gt = int(mhb.created.timestamp())
|
2019-04-03 04:12:48 +00:00
|
|
|
|
|
|
|
all_invoices_response = stripe_utils.get_all_invoices(
|
|
|
|
user.stripecustomer.stripe_id,
|
2019-04-13 10:54:57 +00:00
|
|
|
created_gt=created_gt
|
2019-04-03 04:12:48 +00:00
|
|
|
)
|
2019-04-03 04:59:05 +00:00
|
|
|
if all_invoices_response['error'] is not None:
|
|
|
|
self.stdout.write(self.style.ERROR(all_invoices_response['error']))
|
2019-11-28 08:08:45 +00:00
|
|
|
user.import_stripe_bill_remark += "{}: {},".format(datetime.datetime.now(), all_invoices_response['error'])
|
|
|
|
user.save()
|
|
|
|
continue
|
2019-04-03 04:12:48 +00:00
|
|
|
all_invoices = all_invoices_response['response_object']
|
2019-04-03 04:59:05 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS("Obtained {} invoices".format(len(all_invoices) if all_invoices is not None else 0)))
|
2019-04-20 05:31:32 +00:00
|
|
|
num_invoice_created = 0
|
2019-04-03 04:12:48 +00:00
|
|
|
for invoice in all_invoices:
|
2019-04-03 07:03:58 +00:00
|
|
|
invoice['customer'] = user.stripecustomer
|
2019-05-13 19:15:38 +00:00
|
|
|
try:
|
|
|
|
existing_mhb = MonthlyHostingBill.objects.get(invoice_id=invoice['invoice_id'])
|
|
|
|
logger.debug("Invoice %s exists already. Not importing." % invoice['invoice_id'])
|
|
|
|
except MonthlyHostingBill.DoesNotExist as dne:
|
|
|
|
logger.debug("Invoice id %s does not exist" % invoice['invoice_id'])
|
2019-11-04 06:35:55 +00:00
|
|
|
|
|
|
|
if MonthlyHostingBill.create(invoice) is not None:
|
|
|
|
num_invoice_created += 1
|
|
|
|
else:
|
2019-11-28 08:08:45 +00:00
|
|
|
user.import_stripe_bill_remark += "{}: Import failed - {},".format(
|
|
|
|
datetime.datetime.now(),
|
|
|
|
invoice['invoice_id'])
|
|
|
|
user.save()
|
2019-11-04 06:35:55 +00:00
|
|
|
logger.error("Did not import invoice for %s"
|
|
|
|
"" % str(invoice))
|
2019-04-20 05:31:32 +00:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.SUCCESS("Number of invoices imported = %s" % num_invoice_created)
|
|
|
|
)
|
2019-04-02 07:18:46 +00:00
|
|
|
else:
|
2019-04-03 04:12:48 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS(
|
|
|
|
'Customer email %s does not have a stripe customer.' % email))
|
2019-11-28 08:08:45 +00:00
|
|
|
user.import_stripe_bill_remark += "{}: No stripecustomer,".format(
|
|
|
|
datetime.datetime.now()
|
|
|
|
)
|
|
|
|
user.save()
|
2019-04-02 07:18:46 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(" *** Error occurred. Details {}".format(str(e)))
|