From 0e84081880404955b5c2746e731f9ee026ba78e9 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 2 Apr 2019 09:18:15 +0200 Subject: [PATCH] Add monthlyhostingbill model + code --- hosting/models.py | 22 ++++++++++++++++++++++ utils/stripe_utils.py | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/hosting/models.py b/hosting/models.py index 707b072d..4e4e2a59 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -232,6 +232,28 @@ class HostingBill(AssignPermissionsMixin, models.Model): return instance +class MonthlyHostingBill(AssignPermissionsMixin, models.Model): + customer = models.ForeignKey(StripeCustomer) + order = models.ForeignKey(HostingOrder) + receipt_number = models.CharField( + help_text="The receipt number that is generated on Stripe" + ) + invoice_number = models.CharField( + help_text="The invoice number that is generated on Stripe" + ) + 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") + + permissions = ('view_monthlyhostingbill',) + + class Meta: + permissions = ( + ('view_monthlyhostingbill', 'View Monthly Hosting'), + ) + + class VMDetail(models.Model): user = models.ForeignKey(CustomUser) vm_id = models.IntegerField(default=0) diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index a3224a0e..d412cbd0 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -122,6 +122,26 @@ class StripeUtils(object): } return card_details + @handleStripeError + def get_all_invoices(self, customer_id): + invoices = stripe.Invoice.list(limit=100, customer=customer_id) + return_list = [] + for invoice in invoices: + invoice_details = { + 'created': invoice.created, + 'receipt_number': invoice.receipt_number, + 'invoice_number': invoice.number, + 'date_paid': invoice.date_paid, + 'period_start': invoice.period_start, + 'period_end': invoice.period_end, + 'paid': invoice.paid, + 'billing_reason': invoice.billing_reason, + 'discount': invoice.discount.coupon.amount_off if invoice.discount else 0, + 'total': invoice.total + } + return_list.append(invoice_details) + return return_list + @handleStripeError def get_cards_details_from_token(self, token): stripe_token = stripe.Token.retrieve(token)