remove big mistake: orders from product

Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
Nico Schottelius 2020-09-28 20:44:50 +02:00
commit 1aead50170
8 changed files with 287 additions and 35 deletions

View file

@ -354,17 +354,18 @@ class Order(models.Model):
return self.starting_date + datetime.timedelta(seconds=self.recurring_period)
@property
def next_ending_date(self):
def next_cancel_or_downgrade_date(self):
"""
Return the next proper ending date after n times the
recurring_period, where n is an integer.
recurring_period, where n is an integer that applies for downgrading
or cancelling.
"""
if self.recurring_period > 0:
now = timezone.now()
delta = now - self.starting_date
num_times = math.ceil(delta.total_seconds() / self.recurring_period)
num_times = ceil(delta.total_seconds() / self.recurring_period)
next_date = self.starting_date + datetime.timedelta(seconds= num_times * self.recurring_period)
else:
@ -452,8 +453,9 @@ class Order(models.Model):
if self.ending_date and self.ending_date < self.starting_date:
raise ValidationError("End date cannot be before starting date")
if self.ending_date and self.ending_date < self.earliest_ending_date:
raise ValidationError("Ending date is before minimum duration (starting_date + recurring period)")
# do not check this if we upgrade
# if self.ending_date and self.ending_date < self.earliest_ending_date:
# raise ValidationError("Ending date is before minimum duration (starting_date + recurring period)")
super().save(*args, **kwargs)
@ -853,11 +855,9 @@ class BillRecord(models.Model):
class Product(UncloudModel):
"""
A product is something a user orders. To record the pricing, we
A product is something a user can order. To record the pricing, we
create order that define a state in time.
A product can *depend* on other products.
A product can have *one* one_time_order and/or *one*
recurring_order.
@ -872,12 +872,12 @@ class Product(UncloudModel):
description = "Generic Product"
orders = models.ManyToManyField(Order)
status = models.CharField(max_length=32,
choices=UncloudStatus.choices,
default=UncloudStatus.AWAITING_PAYMENT)
# config = models.JSONField()
# Default period for all products
default_recurring_period = RecurringPeriod.PER_30D
@ -941,6 +941,7 @@ class Product(UncloudModel):
self.orders.add(recurring_order)
# FIXME: this could/should be part of Order (?)
def create_or_update_recurring_order(self, when_to_start=None, recurring_period=None):
if not self.recurring_price:
return
@ -954,8 +955,8 @@ class Product(UncloudModel):
if self.last_recurring_order:
if self.recurring_price < self.last_recurring_order.price:
if when_to_start < self.last_recurring_order.next_ending_date:
when_to_start = start_after(self.last_recurring_order.next_ending_date)
if when_to_start < self.last_recurring_order.next_cancel_or_downgrade_date:
when_to_start = start_after(self.last_recurring_order.next_cancel_or_downgrade_date)
when_to_end = end_before(when_to_start)
@ -967,10 +968,9 @@ class Product(UncloudModel):
description=str(self),
replaces=self.last_recurring_order)
self.last_recurring_order.end_date = when_to_end
self.last_recurring_order.replace_with(new_order)
self.orders.add(new_order)
else:
# This might be a bug as it might (re-)create the one time order
self.create_order(when_to_start, recurring_period)
@property