Quick and dirty voucher use report

Dumps a list of voucher code, invoice id, invoice details in a listreport.
This commit is contained in:
James Polley 2018-01-13 20:52:52 +11:00
parent 8c0d42ee7f
commit 653cd80891

View file

@ -9,7 +9,7 @@ from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models import F, Q
from django.db.models import F, Q, Subquery, OuterRef
from django.db.models import Count, Max, Sum
from django.db.models import Case, When, Value
from django.db.models.fields.related import RelatedField
@ -314,6 +314,24 @@ def limits(request, form):
headings = ["Discount", "Quantity"]
reports.append(ListReport('Discounts', headings, data))
vprod=Subquery(conditions.VoucherFlag.objects.filter(
voucher__code__in=OuterRef("cart__vouchers__code")).values("products"))
vcat=Subquery(conditions.VoucherFlag.objects.filter(
voucher__code__in=OuterRef("cart__vouchers__code")).values("categories"))
invoices_using_vouchers=commerce.Invoice.objects.filter((
Q(lineitem__product__in=vprod)|
Q(lineitem__product__category__in=vcat)
)).filter(
status=commerce.Invoice.STATUS_PAID).order_by(
"cart__vouchers__code").select_related("user")
vouchers = []
for inv in invoices_using_vouchers:
code=inv.cart.vouchers.all().values("code")[0]["code"]
vouchers.append((code,inv.id,inv))
voucher_headings = ["Code","Invoice#","Details"]
reports.append(ListReport('Vouchers', voucher_headings, vouchers))
return reports