diff --git a/counter/forms.py b/counter/forms.py
index c7df1d9d..0d5e1255 100644
--- a/counter/forms.py
+++ b/counter/forms.py
@@ -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}"
diff --git a/counter/models.py b/counter/models.py
index 744e3079..45272bc4 100644
--- a/counter/models.py
+++ b/counter/models.py
@@ -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"))
diff --git a/counter/templates/counter/invoices_call.jinja b/counter/templates/counter/invoices_call.jinja
index 6ca1004b..95e83727 100644
--- a/counter/templates/counter/invoices_call.jinja
+++ b/counter/templates/counter/invoices_call.jinja
@@ -29,12 +29,12 @@
{% trans %}Validated{% endtrans %} |
- {% for i in sums %}
+ {% for data in club_data %}
- {{ i['club__name'] }} |
- {{"%.2f"|format(i['selling_sum'])}} € |
+ {{ data.club.name }} |
+ {{"%.2f"|format(data.sum)}} € |
-
+ {{ form[form.get_club_name(data.club.id)] }}
|
{% endfor %}
diff --git a/counter/views/invoice.py b/counter/views/invoice.py
index 55c96b38..5e6923e7 100644
--- a/counter/views/invoice.py
+++ b/counter/views/invoice.py
@@ -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', '')}")