Commit 7d40e111 authored by Sli's avatar Sli

club: ClubSellingView way faster and with multiple selections everywhere

parent af48553e
Pipeline #2154 passed with stage
in 59 minutes and 3 seconds
......@@ -170,21 +170,21 @@ class SellingsForm(forms.Form):
required=False,
widget=SelectDateTime,
)
counter = forms.ModelChoiceField(
counters = forms.ModelMultipleChoiceField(
Counter.objects.order_by("name").all(), label=_("Counter"), required=False
)
def __init__(self, club, *args, **kwargs):
super(SellingsForm, self).__init__(*args, **kwargs)
self.fields["product"] = forms.ModelChoiceField(
self.fields["products"] = forms.ModelMultipleChoiceField(
club.products.order_by("name").filter(archived=False).all(),
label=_("Product"),
label=_("Products"),
required=False,
)
self.fields["archived_product"] = forms.ModelChoiceField(
self.fields["archived_products"] = forms.ModelMultipleChoiceField(
club.products.order_by("name").filter(archived=True).all(),
label=_("Archived product"),
label=_("Archived products"),
required=False,
)
......
......@@ -355,21 +355,29 @@ class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailFormView):
qs = qs.filter(date__gte=form.cleaned_data["begin_date"])
if form.cleaned_data["end_date"]:
qs = qs.filter(date__lte=form.cleaned_data["end_date"])
if form.cleaned_data["counter"]:
qs = qs.filter(counter=form.cleaned_data["counter"])
if form.cleaned_data["counters"]:
qs = qs.filter(counter__in=form.cleaned_data["counters"])
selected_products = []
if form.cleaned_data["product"]:
selected_products.append(form.cleaned_data["product"].id)
if form.cleaned_data["archived_product"]:
selected_products.append(form.cleaned_data["selected_products"].id)
if form.cleaned_data["products"]:
selected_products.extend(form.cleaned_data["products"])
if form.cleaned_data["archived_products"]:
selected_products.extend(form.cleaned_data["selected_products"])
print(selected_products)
if len(selected_products) > 0:
qs = qs.filter(product__id__in=selected_products)
qs = qs.filter(product__in=selected_products)
kwargs["result"] = qs.all().order_by("-id")
kwargs["total"] = sum([s.quantity * s.unit_price for s in qs.all()])
kwargs["total_quantity"] = sum([s.quantity for s in qs.all()])
kwargs["benefit"] = kwargs["total"] - sum(
[s.product.purchase_price for s in qs.exclude(product=None)]
)
for selling in kwargs["result"]:
kwargs["total"] += selling.quantity * selling.unit_price
kwargs["total_quantity"] += selling.quantity
if hasattr(selling, "product"):
kwargs["benefit"] += selling.product.purchase_price
kwargs["benefit"] = kwargs["total"] - kwargs["benefit"]
return kwargs
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment