Add Line item

This commit is contained in:
PCoder 2019-04-13 13:42:04 +02:00
parent 061ef7d036
commit 8816793803
3 changed files with 79 additions and 0 deletions

View file

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2019-04-13 11:38
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
import utils.mixins
class Migration(migrations.Migration):
dependencies = [
('hosting', '0051_auto_20190403_0703'),
]
operations = [
migrations.CreateModel(
name='HostingBillLineItem',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('amount', models.PositiveSmallIntegerField()),
('description', models.CharField(max_length=255)),
('discountable', models.BooleanField()),
('metadata', models.CharField(max_length=128)),
('period_start', models.DateTimeField()),
('period_end', models.DateTimeField()),
('proration', models.BooleanField()),
('quantity', models.PositiveIntegerField()),
('unit_amount', models.PositiveIntegerField()),
('monthly_hosting_bill', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='hosting.MonthlyHostingBill')),
],
options={
'permissions': (('view_hostingbilllineitem', 'View Monthly Hosting Bill Line Item'),),
},
bases=(utils.mixins.AssignPermissionsMixin, models.Model),
),
]

View file

@ -1,3 +1,4 @@
import json
import logging
import os
import pytz
@ -235,6 +236,9 @@ class HostingBill(AssignPermissionsMixin, models.Model):
class MonthlyHostingBill(AssignPermissionsMixin, models.Model):
"""
Corresponds to Invoice object of Stripe
"""
customer = models.ForeignKey(StripeCustomer)
order = models.ForeignKey(HostingOrder)
created = models.DateTimeField(help_text="When the invoice was created")
@ -326,6 +330,21 @@ class MonthlyHostingBill(AssignPermissionsMixin, models.Model):
subscription_ids_csv=args['subscription_ids_csv'],
)
if 'line_items' in args['line_items']:
line_items = args['line_items']
for item in line_items:
line_item_instance = HostingBillLineItem.objects.create(
monthly_hosting_bill=instance,
amount=item.amount,
description=item.description,
discountable=item.discountable,
metadata=json.dumps(item.metadata),
period_start=datetime.utcfromtimestamp(item.period.start).replace(tzinfo=pytz.utc), period_end=datetime.utcfromtimestamp(item.period.end).replace(tzinfo=pytz.utc),
proration=item.proration,
quantity=item.quantity,
unit_amount=item.unit_amount
)
line_item_instance.assign_permission(instance.customer.user)
instance.assign_permissions(instance.customer.user)
return instance
@ -366,6 +385,28 @@ class MonthlyHostingBill(AssignPermissionsMixin, models.Model):
return return_value
class HostingBillLineItem(AssignPermissionsMixin, models.Model):
"""
Corresponds to InvoiceItem object of Stripe
"""
monthly_hosting_bill = models.ForeignKey(MonthlyHostingBill)
amount = models.PositiveSmallIntegerField()
description = models.CharField(max_length=255)
discountable = models.BooleanField()
metadata = models.CharField(max_length=128)
period_start = models.DateTimeField()
period_end = models.DateTimeField()
proration = models.BooleanField()
quantity = models.PositiveIntegerField()
unit_amount = models.PositiveIntegerField()
permissions = ('view_hostingbilllineitem',)
class Meta:
permissions = (
('view_hostingbilllineitem', 'View Monthly Hosting Bill Line Item'),
)
class VMDetail(models.Model):
user = models.ForeignKey(CustomUser)
vm_id = models.IntegerField(default=0)

View file

@ -159,6 +159,7 @@ class StripeUtils(object):
'subscription_ids_csv': ','.join(
[line.id if line.type == 'subscription' else '' for line in invoice.lines.data]
),
'line_items': invoice.lines.data
}
starting_after = invoice.id
return_list.append(invoice_details)