club: separation between archived products and non archived ones

This commit is contained in:
Antoine Bartuccio 2019-11-27 16:23:14 +01:00
parent ad8bcc7282
commit af48553e35
Signed by: klmp200
GPG Key ID: E7245548C53F904B
3 changed files with 39 additions and 18 deletions

View File

@ -157,7 +157,7 @@ class MailingForm(forms.Form):
return cleaned_data
class SellingsFormBase(forms.Form):
class SellingsForm(forms.Form):
begin_date = forms.DateTimeField(
input_formats=["%Y-%m-%d %H:%M:%S"],
label=_("Begin date"),
@ -174,6 +174,20 @@ class SellingsFormBase(forms.Form):
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(
club.products.order_by("name").filter(archived=False).all(),
label=_("Product"),
required=False,
)
self.fields["archived_product"] = forms.ModelChoiceField(
club.products.order_by("name").filter(archived=True).all(),
label=_("Archived product"),
required=False,
)
class ClubMemberForm(forms.Form):
"""

View File

@ -3,7 +3,7 @@
{% block content %}
<h3>{% trans %}Sellings{% endtrans %}</h3>
<form action="" method="get">
<form action="" method="post">
{% csrf_token %}
{{ form }}
<p><input type="submit" value="{% trans %}Show{% endtrans %}" /></p>

View File

@ -60,7 +60,7 @@ from com.views import (
)
from club.models import Club, Membership, Mailing, MailingSubscription
from club.forms import MailingForm, ClubEditForm, ClubMemberForm, SellingsFormBase
from club.forms import MailingForm, ClubEditForm, ClubMemberForm, SellingsForm
class ClubTabsMixin(TabedViewMixin):
@ -319,7 +319,7 @@ class ClubOldMembersView(ClubTabsMixin, CanViewMixin, DetailView):
current_tab = "elderlies"
class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailView):
class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailFormView):
"""
Sellings of a club
"""
@ -328,21 +328,26 @@ class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailView):
pk_url_kwarg = "club_id"
template_name = "club/club_sellings.jinja"
current_tab = "sellings"
form_class = SellingsForm
def get_form_class(self):
kwargs = {
"product": forms.ModelChoiceField(
self.object.products.order_by("name").all(),
label=_("Product"),
required=False,
)
}
return type("SellingsForm", (SellingsFormBase,), kwargs)
def get_form_kwargs(self):
kwargs = super(ClubSellingView, self).get_form_kwargs()
kwargs["club"] = self.object
return kwargs
def post(self, request, *args, **kwargs):
return self.get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
kwargs = super(ClubSellingView, self).get_context_data(**kwargs)
form = self.get_form_class()(self.request.GET)
qs = Selling.objects.filter(club=self.object)
kwargs["result"] = qs[:0]
kwargs["total"] = 0
kwargs["total_quantity"] = 0
kwargs["benefit"] = 0
form = self.get_form()
if form.is_valid():
if not len([v for v in form.cleaned_data.values() if v is not None]):
qs = Selling.objects.filter(id=-1)
@ -352,17 +357,19 @@ class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailView):
qs = qs.filter(date__lte=form.cleaned_data["end_date"])
if form.cleaned_data["counter"]:
qs = qs.filter(counter=form.cleaned_data["counter"])
selected_products = []
if form.cleaned_data["product"]:
qs = qs.filter(product__id=form.cleaned_data["product"].id)
selected_products.append(form.cleaned_data["product"].id)
if form.cleaned_data["archived_product"]:
selected_products.append(form.cleaned_data["selected_products"].id)
if len(selected_products) > 0:
qs = qs.filter(product__id__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)]
)
else:
kwargs["result"] = qs[:0]
kwargs["form"] = form
return kwargs