From a320f822fcfa928810bb0299e3fe0baf373b9c64 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 25 Aug 2016 21:05:02 +1000 Subject: [PATCH] Report for total items sold. --- registrasion/staff_views.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/registrasion/staff_views.py b/registrasion/staff_views.py index 50c2a18b..8cf1e590 100644 --- a/registrasion/staff_views.py +++ b/registrasion/staff_views.py @@ -1,6 +1,7 @@ import forms from django.db.models import Q +from django.db.models import Sum from django.shortcuts import render from functools import wraps @@ -84,14 +85,28 @@ def items_sold(request): # TODO augment the form to allow us to filter by invoice status. line_items = commerce.LineItem.objects.filter( - Q(product=products) | Q(product__category=categories), + Q(product__in=products) | Q(product__category__in=categories), invoice__status=commerce.Invoice.STATUS_PAID, ).select_related("invoice") - headings = ["invoice_id", "description", "quantity", "price"] + line_items = line_items.order_by( + # sqlite requires an order_by for .values() to work + "-price", "description", + ).values( + "price", "description", + ).annotate( + total_quantity=Sum("quantity"), + ) + + print line_items + + headings = ["description", "quantity", "price", "total"] data = [] for line in line_items: - data.append([line.invoice.id, line.description, line.quantity, line.price]) + data.append([ + line["description"], line["total_quantity"], + line["price"], line["total_quantity"] * line["price"], + ]) return Report(form, headings, data)