Factors out annotating objects by cart status
This commit is contained in:
parent
2c99114d9f
commit
851c37508a
1 changed files with 25 additions and 21 deletions
|
@ -178,19 +178,8 @@ def credit_note_refunds():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@report_view("Product status", form_type=forms.ProductAndCategoryForm)
|
def group_by_cart_status(queryset, order, values):
|
||||||
def product_status(request, form):
|
queryset = queryset.annotate(
|
||||||
''' Summarises the inventory status of the given items, grouping by
|
|
||||||
invoice status. '''
|
|
||||||
|
|
||||||
products = form.cleaned_data["product"]
|
|
||||||
categories = form.cleaned_data["category"]
|
|
||||||
|
|
||||||
items = commerce.ProductItem.objects.filter(
|
|
||||||
Q(product__in=products) | Q(product__category__in=categories),
|
|
||||||
).select_related("cart", "product")
|
|
||||||
|
|
||||||
items = items.annotate(
|
|
||||||
is_reserved=Case(
|
is_reserved=Case(
|
||||||
When(cart__in=commerce.Cart.reserved_carts(), then=Value(True)),
|
When(cart__in=commerce.Cart.reserved_carts(), then=Value(True)),
|
||||||
default=Value(False),
|
default=Value(False),
|
||||||
|
@ -198,14 +187,8 @@ def product_status(request, form):
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
items = items.order_by(
|
values = queryset.order_by(*order).values(*values)
|
||||||
"product__category__order",
|
values = values.annotate(
|
||||||
"product__order",
|
|
||||||
).values(
|
|
||||||
"product",
|
|
||||||
"product__category__name",
|
|
||||||
"product__name",
|
|
||||||
).annotate(
|
|
||||||
total_paid=Sum(Case(
|
total_paid=Sum(Case(
|
||||||
When(
|
When(
|
||||||
cart__status=commerce.Cart.STATUS_PAID,
|
cart__status=commerce.Cart.STATUS_PAID,
|
||||||
|
@ -242,6 +225,27 @@ def product_status(request, form):
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return values
|
||||||
|
|
||||||
|
|
||||||
|
@report_view("Product status", form_type=forms.ProductAndCategoryForm)
|
||||||
|
def product_status(request, form):
|
||||||
|
''' Summarises the inventory status of the given items, grouping by
|
||||||
|
invoice status. '''
|
||||||
|
|
||||||
|
products = form.cleaned_data["product"]
|
||||||
|
categories = form.cleaned_data["category"]
|
||||||
|
|
||||||
|
items = commerce.ProductItem.objects.filter(
|
||||||
|
Q(product__in=products) | Q(product__category__in=categories),
|
||||||
|
).select_related("cart", "product")
|
||||||
|
|
||||||
|
items = group_by_cart_status(
|
||||||
|
items,
|
||||||
|
["product__category__order", "product__order"],
|
||||||
|
["product", "product__category__name", "product__name"],
|
||||||
|
)
|
||||||
|
|
||||||
headings = [
|
headings = [
|
||||||
"Product", "Paid", "Reserved", "Unreserved", "Refunded",
|
"Product", "Paid", "Reserved", "Unreserved", "Refunded",
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue