club: ClubSellingView way faster and with multiple selections everywhere

This commit is contained in:
Antoine Bartuccio 2019-11-27 20:37:28 +01:00
parent af48553e35
commit 7d40e11144
Signed by: klmp200
GPG Key ID: E7245548C53F904B
2 changed files with 25 additions and 17 deletions

View File

@ -170,21 +170,21 @@ class SellingsForm(forms.Form):
required=False, required=False,
widget=SelectDateTime, widget=SelectDateTime,
) )
counter = forms.ModelChoiceField( counters = forms.ModelMultipleChoiceField(
Counter.objects.order_by("name").all(), label=_("Counter"), required=False Counter.objects.order_by("name").all(), label=_("Counter"), required=False
) )
def __init__(self, club, *args, **kwargs): def __init__(self, club, *args, **kwargs):
super(SellingsForm, self).__init__(*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(), club.products.order_by("name").filter(archived=False).all(),
label=_("Product"), label=_("Products"),
required=False, required=False,
) )
self.fields["archived_product"] = forms.ModelChoiceField( self.fields["archived_products"] = forms.ModelMultipleChoiceField(
club.products.order_by("name").filter(archived=True).all(), club.products.order_by("name").filter(archived=True).all(),
label=_("Archived product"), label=_("Archived products"),
required=False, required=False,
) )

View File

@ -355,21 +355,29 @@ class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailFormView):
qs = qs.filter(date__gte=form.cleaned_data["begin_date"]) qs = qs.filter(date__gte=form.cleaned_data["begin_date"])
if form.cleaned_data["end_date"]: if form.cleaned_data["end_date"]:
qs = qs.filter(date__lte=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 = [] selected_products = []
if form.cleaned_data["product"]: if form.cleaned_data["products"]:
selected_products.append(form.cleaned_data["product"].id) selected_products.extend(form.cleaned_data["products"])
if form.cleaned_data["archived_product"]: if form.cleaned_data["archived_products"]:
selected_products.append(form.cleaned_data["selected_products"].id) selected_products.extend(form.cleaned_data["selected_products"])
print(selected_products)
if len(selected_products) > 0: 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["result"] = qs.all().order_by("-id")
kwargs["total"] = sum([s.quantity * s.unit_price for s in qs.all()]) for selling in kwargs["result"]:
kwargs["total_quantity"] = sum([s.quantity for s in qs.all()]) kwargs["total"] += selling.quantity * selling.unit_price
kwargs["benefit"] = kwargs["total"] - sum( kwargs["total_quantity"] += selling.quantity
[s.product.purchase_price for s in qs.exclude(product=None)] if hasattr(selling, "product"):
) kwargs["benefit"] += selling.product.purchase_price
kwargs["benefit"] = kwargs["total"] - kwargs["benefit"]
return kwargs return kwargs