Adding subtotal, VAT and total to order. Also redirect to shop index if there are no products.

This commit is contained in:
Víðir Valberg Guðmundsson 2016-05-16 16:09:25 +02:00
parent 373a4cf65b
commit 9eefc6bdfd
3 changed files with 45 additions and 9 deletions

View file

@ -70,6 +70,24 @@ class Order(CreatedUpdatedModel):
sum=Sum('orderproductrelation__quantity')
)['sum']
@property
def subtotal(self):
return self.products.aggregate(
sum=Sum(
models.F('orderproductrelation__product__price') *
models.F('orderproductrelation__quantity'),
output_field=models.IntegerField()
)
)['sum']
@property
def vat(self):
return (self.subtotal/100)*25
@property
def total(self):
return self.subtotal + self.vat
class ProductCategory(CreatedUpdatedModel, UUIDModel):
class Meta:

View file

@ -48,7 +48,26 @@
{% endfor %}
{# TODO: Add total + VAT info #}
<tr>
<td colspan="2">
<td>
<strong>Subtotal</strong>
<td>
{{ order.subtotal }}
<tr>
<td colspan="2">
<td>
<strong>VAT (25%)</strong>
<td>
{{ order.vat }}
<tr>
<td colspan="2">
<td>
<strong>Total</strong>
<td>
{{ order.total }}
</table>

View file

@ -60,16 +60,13 @@ class OrderDetailView(LoginRequiredMixin, DetailView):
context_object_name = 'order'
def get(self, request, *args, **kwargs):
if self.get_object().user != request.user:
order = self.get_object()
if order.user != request.user:
raise Http404("Order not found")
if self.get_object().paid:
messages.error(request, 'This order is already paid for!')
return HttpResponseRedirect('shop:order_detail')
if not self.get_object().products:
messages.error(request, 'This order contains no products!')
return HttpResponseRedirect('shop:order_detail')
if not order.products.count() > 0:
return HttpResponseRedirect(reverse_lazy('shop:index'))
return super(OrderDetailView, self).get(request, *args, **kwargs)
@ -111,6 +108,8 @@ class OrderDetailView(LoginRequiredMixin, DetailView):
product_remove = request.POST.get('remove_product')
if product_remove:
order.orderproductrelation_set.filter(pk=product_remove).delete()
if not order.products.count() > 0:
return HttpResponseRedirect(reverse_lazy('shop:index'))
return super(OrderDetailView, self).get(request, *args, **kwargs)