Complete implementation of fetch_stripe_bills
This commit is contained in:
parent
8dc00c9dd9
commit
6d42f88be1
3 changed files with 97 additions and 27 deletions
|
|
@ -1,9 +1,13 @@
|
|||
import logging
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from hosting.models import UserCardDetail
|
||||
from hosting.models import MonthlyHostingBill
|
||||
from membership.models import CustomUser
|
||||
from utils.stripe_utils import StripeUtils
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = '''Fetches invoices from Stripe and creates bills for a given
|
||||
|
|
@ -18,11 +22,32 @@ class Command(BaseCommand):
|
|||
stripe_utils = StripeUtils()
|
||||
user = CustomUser.objects.get(email=email)
|
||||
if hasattr(user, 'stripecustomer'):
|
||||
self.stdout.write(self.style.SUCCESS('Found %s. Fetching bills for him.' % email))
|
||||
stripe_utils.get_all_invoices(
|
||||
user.stripecustomer.stripe_id
|
||||
self.stdout.write(self.style.SUCCESS(
|
||||
'Found %s. Fetching bills for him.' % email))
|
||||
mhb = MonthlyHostingBill.objects.last(
|
||||
customer=user.stripecustomer
|
||||
)
|
||||
created_gt = {}
|
||||
if mhb is not None:
|
||||
# fetch only invoices which is created after
|
||||
# mhb.created, because we already have invoices till
|
||||
# this date
|
||||
created_gt = {'gt': mhb.created}
|
||||
|
||||
all_invoices_response = stripe_utils.get_all_invoices(
|
||||
user.stripecustomer.stripe_id,
|
||||
created=created_gt
|
||||
)
|
||||
all_invoices = all_invoices_response['response_object']
|
||||
logger.debug(
|
||||
"Obtained {} invoices".format(len(all_invoices))
|
||||
)
|
||||
for invoice in all_invoices:
|
||||
MonthlyHostingBill.create(
|
||||
invoice, stripe_customer=user.stripecustomer
|
||||
)
|
||||
else:
|
||||
self.stdout.write(self.style.SUCCESS('Customer email %s does not have a stripe customer.' % email))
|
||||
self.stdout.write(self.style.SUCCESS(
|
||||
'Customer email %s does not have a stripe customer.' % email))
|
||||
except Exception as e:
|
||||
print(" *** Error occurred. Details {}".format(str(e)))
|
||||
|
|
|
|||
|
|
@ -235,16 +235,24 @@ class HostingBill(AssignPermissionsMixin, models.Model):
|
|||
class MonthlyHostingBill(AssignPermissionsMixin, models.Model):
|
||||
customer = models.ForeignKey(StripeCustomer)
|
||||
order = models.ForeignKey(HostingOrder)
|
||||
created = models.DateTimeField(help_text="When the invoice was created")
|
||||
receipt_number = models.CharField(
|
||||
help_text="The receipt number that is generated on Stripe"
|
||||
help_text="The receipt number that is generated on Stripe",
|
||||
max_length=100
|
||||
)
|
||||
invoice_number = models.CharField(
|
||||
help_text="The invoice number that is generated on Stripe"
|
||||
help_text="The invoice number that is generated on Stripe",
|
||||
max_length=100
|
||||
)
|
||||
billing_period = models.CharField(
|
||||
help_text="The billing period for which the bill is valid"
|
||||
)
|
||||
date_paid = models.DateField(help_text="Date on which the bill was paid")
|
||||
paid_at = models.DateTimeField(help_text="Date on which the bill was paid")
|
||||
period_start = models.DateTimeField()
|
||||
period_end = models.DateTimeField()
|
||||
billing_reason = models.CharField(max_length=25)
|
||||
discount = models.PositiveIntegerField()
|
||||
total = models.IntegerField()
|
||||
lines_data_count = models.IntegerField()
|
||||
invoice_id = models.CharField(unique=True, max_length=100)
|
||||
lines_meta_data_csv = models.TextField()
|
||||
|
||||
permissions = ('view_monthlyhostingbill',)
|
||||
|
||||
|
|
@ -253,6 +261,24 @@ class MonthlyHostingBill(AssignPermissionsMixin, models.Model):
|
|||
('view_monthlyhostingbill', 'View Monthly Hosting'),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def create(cls, stripe_customer, **args):
|
||||
instance = cls.objects.create(args)
|
||||
instance.customer = stripe_customer
|
||||
if len(instance.lines_meta_data_csv) > 0:
|
||||
vm_ids = [vm_id.strip() for vm_id in instance.lines_meta_data_csv.split(",")]
|
||||
if len(vm_ids) == 1:
|
||||
instance.order = HostingOrder.objects.get(vm_id=vm_ids[0])
|
||||
else:
|
||||
logger.debug(
|
||||
"More than one VM_ID"
|
||||
"for MonthlyHostingBill {}".format(instance.invoice_id)
|
||||
)
|
||||
logger.debug("VM_IDS=".format(','.join(vm_ids)))
|
||||
instance.assign_permissions(stripe_customer.user)
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
|
||||
class VMDetail(models.Model):
|
||||
user = models.ForeignKey(CustomUser)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue