Commit 89e853b4 authored by fnux's avatar fnux

Add order termination logic

parent 1b97fc8f
Pipeline #1401 failed with stage
in 2 minutes and 40 seconds
......@@ -553,8 +553,14 @@ class Bill(models.Model):
@property
def final(self):
# A bill is final when its ending date is passed.
return self.ending_date < timezone.now()
# A bill is final when its ending date is passed, or when all of its
# 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):
for order in self.order_set.all():
......@@ -850,6 +856,14 @@ class Order(models.Model):
on_delete=models.PROTECT,
blank=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.
def save(self, *args, **kwargs):
......
......@@ -313,3 +313,15 @@ class AdminOrderViewSet(mixins.ListModelMixin,
def get_queryset(self):
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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment