From 077c665c53a2869716cd81990777d81fb96f4a16 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Thu, 3 Sep 2020 17:16:18 +0200 Subject: [PATCH] ++update --- uncloud_pay/models.py | 52 ++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index d1cf6a3..d65c7d2 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -372,6 +372,24 @@ class Order(models.Model): return next_date + def get_ending_date_for_bill(bill): + """ + Determine the ending date given a specific bill + """ + + # If the order is quit, charge the final amount (?) + if order.ending_date: + this_ending_date = order.ending_date + else: + if order.earliest_ending_date > bill.ending_date: + this_ending_date = order.earliest_ending_date + else: + this_ending_date = bill.ending_date + + return this_ending_date + + + @property def count_billed(self): """ @@ -381,7 +399,6 @@ class Order(models.Model): return sum([ br.quantity for br in self.bill_records.all() ]) - @property def count_used(self, when=None): """ How many times this order was billed so far. @@ -401,13 +418,23 @@ class Order(models.Model): return (when - self.starting_date) / self.default_recurring_period @property - def fully_billed(self, when=None): + def all_usage_billed(self, when=None): """ Returns true if this order does not need any further billing ever. In other words: is this order "closed"? """ - if self.count_billed == + if self.count_billed == self.count_used(when): + return True + else: + return False + + @property + def is_closed(self): + if self.all_usage_billed and self.ending_date: + return True + else: + return False @property def is_recurring(self): @@ -567,7 +594,7 @@ class Bill(models.Model): this_starting_date = start_after(last_bill_record.ending_date) - ending_date = cls.get_bill_record_ending_date(order, bill) + ending_date = order.get_ending_date_for_bill(bill) return BillRecord.objects.create(bill=bill, order=order, @@ -595,23 +622,6 @@ class Bill(models.Model): bill_record_for_this_bill.save() - @staticmethod - def get_bill_record_ending_date(order, bill): - """ - Determine the ending date of the billing record - """ - - # If the order is quit, charge the final amount (?) - if order.ending_date: - this_ending_date = order.ending_date - else: - if order.earliest_ending_date > bill.ending_date: - this_ending_date = order.earliest_ending_date - else: - this_ending_date = bill.ending_date - - return this_ending_date - @classmethod def create_next_bill_for_user_address_old(cls, owner,