Add order termination logic
This commit is contained in:
parent
1b97fc8fc7
commit
89e853b490
2 changed files with 28 additions and 2 deletions
|
@ -553,8 +553,14 @@ class Bill(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def final(self):
|
def final(self):
|
||||||
# A bill is final when its ending date is passed.
|
# A bill is final when its ending date is passed, or when all of its
|
||||||
return self.ending_date < timezone.now()
|
# orders have been terminated.
|
||||||
|
every_order_terminated = True
|
||||||
|
billing_period_is_over = self.ending_date < timezone.now()
|
||||||
|
for order in self.order_set.all():
|
||||||
|
every_order_terminated = every_order_terminated and order.is_terminated
|
||||||
|
|
||||||
|
return billing_period_is_over or every_order_terminated
|
||||||
|
|
||||||
def activate_products(self):
|
def activate_products(self):
|
||||||
for order in self.order_set.all():
|
for order in self.order_set.all():
|
||||||
|
@ -850,6 +856,14 @@ class Order(models.Model):
|
||||||
on_delete=models.PROTECT,
|
on_delete=models.PROTECT,
|
||||||
blank=True,
|
blank=True,
|
||||||
null=True)
|
null=True)
|
||||||
|
@property
|
||||||
|
def is_terminated(self):
|
||||||
|
return self.ending_date != None and self.ending_date < timezone.now()
|
||||||
|
|
||||||
|
def terminate(self):
|
||||||
|
if not self.is_terminated:
|
||||||
|
self.ending_date = timezone.now()
|
||||||
|
self.save()
|
||||||
|
|
||||||
# Trigger initial bill generation at order creation.
|
# Trigger initial bill generation at order creation.
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
|
|
@ -313,3 +313,15 @@ class AdminOrderViewSet(mixins.ListModelMixin,
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return Order.objects.all()
|
return Order.objects.all()
|
||||||
|
|
||||||
|
@action(detail=True, methods=['post'])
|
||||||
|
def terminate(self, request, pk):
|
||||||
|
order = self.get_object()
|
||||||
|
if order.is_terminated:
|
||||||
|
return Response(
|
||||||
|
{'error': 'Order is already terminated.'},
|
||||||
|
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
else:
|
||||||
|
order.terminate()
|
||||||
|
return Response({}, status=status.HTTP_200_OK)
|
||||||
|
|
Loading…
Reference in a new issue