From c9be8cc50ba2b1d1ab48d6150879ab75988d9e5b Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 8 Aug 2020 21:54:44 +0200 Subject: [PATCH] orders only have 1 price Signed-off-by: Nico Schottelius --- .../migrations/0005_auto_20200808_1954.py | 18 ++++++++ uncloud_pay/models.py | 42 +++++++++++++++---- 2 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 uncloud_pay/migrations/0005_auto_20200808_1954.py diff --git a/uncloud_pay/migrations/0005_auto_20200808_1954.py b/uncloud_pay/migrations/0005_auto_20200808_1954.py new file mode 100644 index 0000000..92f69a0 --- /dev/null +++ b/uncloud_pay/migrations/0005_auto_20200808_1954.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1 on 2020-08-08 19:54 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud_pay', '0004_remove_order_one_time_price'), + ] + + operations = [ + migrations.RenameField( + model_name='order', + old_name='recurring_price', + new_name='price', + ), + ] diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index 1de8061..12ded8d 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -301,15 +301,10 @@ class Order(models.Model): recurring_period = models.IntegerField(choices = RecurringPeriod.choices, default = RecurringPeriod.PER_30D) - one_time_price = models.DecimalField(default=0.0, - max_digits=AMOUNT_MAX_DIGITS, - decimal_places=AMOUNT_DECIMALS, - validators=[MinValueValidator(0)]) - - recurring_price = models.DecimalField(default=0.0, - max_digits=AMOUNT_MAX_DIGITS, - decimal_places=AMOUNT_DECIMALS, - validators=[MinValueValidator(0)]) + price = models.DecimalField(default=0.0, + max_digits=AMOUNT_MAX_DIGITS, + decimal_places=AMOUNT_DECIMALS, + validators=[MinValueValidator(0)]) replaces = models.ForeignKey('self', related_name='replaced_by', @@ -402,6 +397,10 @@ class Bill(models.Model): def __str__(self): return f"Bill {self.owner}-{self.id}" + @property + def sum(self): + pass + @classmethod def create_next_bill_for_user(cls, user): @@ -421,12 +420,20 @@ class Bill(models.Model): ending_date = end_of_month(starting_date) + bill = cls() + 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 + pass + return bill + @classmethod def create_all_bills(cls): for owner in get_user_model().objects.all(): @@ -511,6 +518,8 @@ class BillRecord(models.Model): # 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 @@ -518,6 +527,21 @@ class BillRecord(models.Model): starting_date = models.DateTimeField() ending_date = models.DateTimeField() + def quantity2(self): + if self.order.recurring_period == RecurringPeriod.ONE_TIME: + return 1 + + record_delta = self.ending_date - self.starting_date + + return record_delta.total_seconds()/self.order.recurring_period + + + def sum(self): + if self.order.recurring_period == RecurringPeriod.ONE_TIME: + return 1 + + return self.quantity * 1 + def __str__(self): return f"{self.bill}: {self.quantity} x {self.order}"