mirror of
https://github.com/ae-utbm/sith.git
synced 2025-06-26 13:15:18 +00:00
invoice call form
This commit is contained in:
parent
6608dac4d3
commit
481c46e367
@ -19,6 +19,7 @@ from counter.models import (
|
||||
Counter,
|
||||
Customer,
|
||||
Eticket,
|
||||
InvoiceCall,
|
||||
Product,
|
||||
Refilling,
|
||||
ReturnableProduct,
|
||||
@ -373,3 +374,35 @@ class BaseBasketForm(forms.BaseFormSet):
|
||||
BasketForm = forms.formset_factory(
|
||||
ProductForm, formset=BaseBasketForm, absolute_max=None, min_num=1
|
||||
)
|
||||
|
||||
|
||||
class InvoiceCallForm(forms.Form):
|
||||
def __init__(self, *args, month=None, clubs=None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.month = month
|
||||
self.clubs = clubs
|
||||
|
||||
for club in self.clubs:
|
||||
field_name = f"club_{club.id}"
|
||||
initial = (
|
||||
InvoiceCall.objects.filter(club=club, month=month)
|
||||
.values_list("is_validated", flat=True)
|
||||
.first()
|
||||
)
|
||||
|
||||
self.fields[field_name] = forms.BooleanField(
|
||||
required=False,
|
||||
initial=initial,
|
||||
)
|
||||
|
||||
def save(self):
|
||||
for club in self.clubs:
|
||||
field_name = f"club_{club.id}"
|
||||
is_validated = self.cleaned_data.get(field_name, False)
|
||||
|
||||
InvoiceCall.objects.update_or_create(
|
||||
month=self.month, club=club, defaults={"is_validated": is_validated}
|
||||
)
|
||||
|
||||
def get_club_name(self, club_id):
|
||||
return f"club_{club_id}"
|
||||
|
@ -1407,7 +1407,7 @@ class MonthField(models.CharField):
|
||||
|
||||
|
||||
class InvoiceCall(models.Model):
|
||||
is_validated = models.BooleanField(verbose_name=_("is validated"))
|
||||
is_validated = models.BooleanField(verbose_name=_("is validated"), default=False)
|
||||
club = models.ForeignKey(Club, on_delete=models.CASCADE)
|
||||
month = MonthField(verbose_name=_("invoice date"))
|
||||
|
||||
|
@ -29,12 +29,12 @@
|
||||
<td>{% trans %}Validated{% endtrans %}</td>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for i in sums %}
|
||||
{% for data in club_data %}
|
||||
<tr>
|
||||
<td>{{ i['club__name'] }}</td>
|
||||
<td>{{"%.2f"|format(i['selling_sum'])}} €</td>
|
||||
<td>{{ data.club.name }}</td>
|
||||
<td>{{"%.2f"|format(data.sum)}} €</td>
|
||||
<td>
|
||||
<input type="checkbox" name="validate_{{ i['club__name'] }}" {% if validated[i['club__name']] %}checked{% endif %}>
|
||||
{{ form[form.get_club_name(data.club.id)] }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
@ -21,6 +21,7 @@ from django.utils import timezone
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from counter.fields import CurrencyField
|
||||
from counter.forms import InvoiceCallForm
|
||||
from counter.models import Club, InvoiceCall, Refilling, Selling
|
||||
from counter.views.mixins import CounterAdminMixin, CounterAdminTabsMixin
|
||||
|
||||
@ -87,10 +88,28 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView):
|
||||
clubs = Club.objects.filter(name__in=club_names)
|
||||
|
||||
invoice_calls = InvoiceCall.objects.filter(month=month_str, club__in=clubs)
|
||||
|
||||
invoice_statuses = {ic.club.name: ic.is_validated for ic in invoice_calls}
|
||||
|
||||
kwargs["validated"] = invoice_statuses
|
||||
kwargs["form"] = InvoiceCallForm(clubs=clubs, month=month_str)
|
||||
|
||||
kwargs["club_data"] = []
|
||||
for club in clubs:
|
||||
selling_sum = next(
|
||||
(
|
||||
item["selling_sum"]
|
||||
for item in kwargs["sums"]
|
||||
if item["club__name"] == club.name
|
||||
),
|
||||
0,
|
||||
)
|
||||
kwargs["club_data"].append(
|
||||
{
|
||||
"club": club,
|
||||
"sum": selling_sum,
|
||||
"validated": invoice_statuses.get(club.name, False),
|
||||
}
|
||||
)
|
||||
|
||||
return kwargs
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
@ -113,10 +132,9 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView):
|
||||
has_selling=True
|
||||
)
|
||||
|
||||
for club in clubs:
|
||||
is_checked = f"validate_{club.name}" in request.POST
|
||||
form = InvoiceCallForm(request.POST, clubs=clubs, month=month_str)
|
||||
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
|
||||
InvoiceCall.objects.update_or_create(
|
||||
month=month_str, club=club, defaults={"is_validated": is_checked}
|
||||
)
|
||||
return redirect(f"{request.path}?month={request.POST.get('month', '')}")
|
||||
|
Loading…
x
Reference in New Issue
Block a user