Modify limits report to include category inclusions as well

* A TimeOrStockLimit limit can apply a limit to a whole category, or
  to specific products
* This report was only counting the products directly listed
* Take advantage of the new all_products property to include the
  products indirectly listed as well as those directly listed
This commit is contained in:
James Polley 2018-01-02 12:50:51 +11:00
parent 6a9652dfd2
commit 1fd8364456

View file

@ -248,7 +248,7 @@ def group_by_cart_status(queryset, order, values):
def limits(request, form): def limits(request, form):
''' Shows the summary of sales against stock limits. ''' ''' Shows the summary of sales against stock limits. '''
line_items = commerce.ProductItem.objects.filter( product_items = commerce.ProductItem.objects.filter(
cart__status=commerce.Invoice.STATUS_PAID, cart__status=commerce.Invoice.STATUS_PAID,
).values( ).values(
"product", "product__name", "product", "product__name",
@ -256,10 +256,6 @@ def limits(request, form):
total_quantity=Sum("quantity") total_quantity=Sum("quantity")
) )
quantities = collections.defaultdict(int)
for line_item in line_items.all():
quantities[line_item['product__name']] += line_item['total_quantity']
limits = conditions.TimeOrStockLimitFlag.objects.all().order_by("-limit") limits = conditions.TimeOrStockLimitFlag.objects.all().order_by("-limit")
headings = ["Product", "Quantity"] headings = ["Product", "Quantity"]
@ -268,10 +264,10 @@ def limits(request, form):
for limit in limits: for limit in limits:
data = [] data = []
total = 0 total = 0
for product in limit.products.all(): limit_items = product_items.filter(product__in=limit.all_products)
if product.name in quantities: for product in limit_items:
total += quantities[product.name] data.append([product['product__name'], product['total_quantity']])
data.append([product.name, quantities[product.name]]) total += product['total_quantity']
if limit.limit: if limit.limit:
data.append(['(TOTAL)', '%s/%s' % (total, limit.limit)]) data.append(['(TOTAL)', '%s/%s' % (total, limit.limit)])
else: else: