Report for total items sold.
This commit is contained in:
parent
d131b547f6
commit
a320f822fc
1 changed files with 18 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
||||||
import forms
|
import forms
|
||||||
|
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
from django.db.models import Sum
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
|
@ -84,14 +85,28 @@ def items_sold(request):
|
||||||
|
|
||||||
# TODO augment the form to allow us to filter by invoice status.
|
# TODO augment the form to allow us to filter by invoice status.
|
||||||
line_items = commerce.LineItem.objects.filter(
|
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,
|
invoice__status=commerce.Invoice.STATUS_PAID,
|
||||||
).select_related("invoice")
|
).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 = []
|
data = []
|
||||||
for line in line_items:
|
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)
|
return Report(form, headings, data)
|
||||||
|
|
Loading…
Reference in a new issue