uncloud pay cleanups

Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
Nico Schottelius 2020-08-08 22:20:49 +02:00
commit 9bf0a99f6a
5 changed files with 324 additions and 239 deletions

View file

@ -340,6 +340,10 @@ class Order(models.Model):
def is_recurring(self):
return not self.recurring_period == RecurringPeriod.ONE_TIME
@property
def is_one_time(self):
return not self.is_recurring
@property
def is_terminated(self):
return self.ending_date != None and self.ending_date < timezone.now()
@ -382,10 +386,6 @@ class Bill(models.Model):
# what is valid for? should this be "final"?
valid = models.BooleanField(default=True)
# Mapping to BillRecords
# https://stackoverflow.com/questions/4443190/djangos-manytomany-relationship-with-additional-fields
bill_records = models.ManyToManyField(Order, through="BillRecord")
class Meta:
constraints = [
models.UniqueConstraint(fields=['owner',
@ -399,13 +399,14 @@ class Bill(models.Model):
@property
def sum(self):
pass
return 0
# for self.billrecord_set.
@classmethod
def create_next_bill_for_user(cls, user):
last_bill = cls.objects.filter(owner=user).order_by('id').last()
all_orders = Order.objects.filter(owner=user).order_by('id')
def create_next_bill_for_user(cls, owner):
last_bill = cls.objects.filter(owner=owner).order_by('id').last()
all_orders = Order.objects.filter(owner=owner).order_by('id')
first_order = all_orders.first()
@ -420,16 +421,21 @@ class Bill(models.Model):
ending_date = end_of_month(starting_date)
bill = cls()
bill, created = cls.objects.get_or_create(
owner=owner,
starting_date=starting_date,
ending_date=ending_date)
for order in all_orders:
# check if order needs to be billed
# check if order has previous billing record
# one time orders
if not order.is_recurring:
pass
if order.is_one_time:
if order.billrecord_set.count() == 0:
br = BillRecord.objects.create(bill=bill,
order=order,
starting_date=starting_date,
ending_date=ending_date)
pass
return bill
@ -515,20 +521,13 @@ class BillRecord(models.Model):
bill = models.ForeignKey(Bill, on_delete=models.CASCADE)
order = models.ForeignKey(Order, on_delete=models.CASCADE)
# How many times the order has been used in this record
quantity = models.DecimalField(max_digits=19, decimal_places=10)
# quantity can actually be derived from starting/ending date
# The timeframe the bill record is for can (and probably often will) differ
# from the bill time
creation_date = models.DateTimeField(auto_now_add=True)
starting_date = models.DateTimeField()
ending_date = models.DateTimeField()
def quantity2(self):
if self.order.recurring_period == RecurringPeriod.ONE_TIME:
def quantity(self):
""" Determine the quantity by the duration"""
if self.order.is_recurring:
return 1
record_delta = self.ending_date - self.starting_date
@ -537,10 +536,7 @@ class BillRecord(models.Model):
def sum(self):
if self.order.recurring_period == RecurringPeriod.ONE_TIME:
return 1
return self.quantity * 1
return self.order.price * self.quantity
def __str__(self):
return f"{self.bill}: {self.quantity} x {self.order}"