mirror of
https://github.com/ae-utbm/sith.git
synced 2025-06-26 21:25:21 +00:00
MonthField for InvoiceCall
This commit is contained in:
parent
3dcdb6ff46
commit
1f77f154ba
@ -1,4 +1,4 @@
|
||||
# Generated by Django 5.2 on 2025-06-11 12:53
|
||||
# Generated by Django 5.2 on 2025-06-14 13:36
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
@ -1364,7 +1364,7 @@ class ReturnableProductBalance(models.Model):
|
||||
)
|
||||
|
||||
|
||||
class MonthField(models.DateField):
|
||||
class MonthField(models.CharField):
|
||||
description = _("Year + month field")
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
@ -1377,21 +1377,33 @@ class MonthField(models.DateField):
|
||||
def from_db_value(self, value, expression, connection):
|
||||
if value is None:
|
||||
return value
|
||||
year, month = map(value.split("-"))
|
||||
return date(year, month, 1)
|
||||
try:
|
||||
year, month = map(int, value.split("-"))
|
||||
return date(year, month, 1)
|
||||
except (ValueError, TypeError):
|
||||
return value
|
||||
|
||||
def to_python(self, value):
|
||||
if isinstance(value, date):
|
||||
return value
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
year, month = map(int, value.split("-"))
|
||||
return date(year, month, 1)
|
||||
except ValueError:
|
||||
pass
|
||||
return value
|
||||
|
||||
def get_prep_value(self, value):
|
||||
if isinstance(value, date):
|
||||
return value.strftime("%Y-%m")
|
||||
elif isinstance(value, str):
|
||||
try:
|
||||
datetime.strptime(value, "%Y-%m")
|
||||
except ValueError:
|
||||
raise ValueError("invalid format for date (use YYYY-mm)") from None
|
||||
elif value is None:
|
||||
if isinstance(value, str) and len(value) == 7 and value[4] == "-":
|
||||
return value
|
||||
else:
|
||||
raise TypeError("Invalid type for MonthField")
|
||||
return value
|
||||
|
||||
def value_to_string(self, obj):
|
||||
value = self.value_from_object(obj)
|
||||
return self.get_prep_value(value)
|
||||
|
||||
|
||||
class InvoiceCall(models.Model):
|
||||
@ -1404,4 +1416,4 @@ class InvoiceCall(models.Model):
|
||||
verbose_name_plural = _("Invoice calls")
|
||||
|
||||
def __str__(self):
|
||||
return f"invoice call of {self.month}/{self.year} made by {self.club}"
|
||||
return f"invoice call of {self.month} made by {self.club}"
|
||||
|
@ -32,7 +32,9 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView):
|
||||
"""Add sums to the context."""
|
||||
kwargs = super().get_context_data(**kwargs)
|
||||
kwargs["months"] = Selling.objects.datetimes("date", "month", order="DESC")
|
||||
if "month" in self.request.GET:
|
||||
month_str = self.request.GET.get("month")
|
||||
|
||||
if month_str:
|
||||
start_date = datetime.strptime(self.request.GET["month"], "%Y-%m")
|
||||
else:
|
||||
start_date = datetime(
|
||||
@ -85,9 +87,7 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView):
|
||||
clubs = Club.objects.filter(name__in=club_names)
|
||||
|
||||
# et une query pour les factures
|
||||
invoice_calls = InvoiceCall.objects.filter(
|
||||
month=date(start_date.year, start_date.month, 1), club__in=clubs
|
||||
)
|
||||
invoice_calls = InvoiceCall.objects.filter(month=month_str, club__in=clubs)
|
||||
|
||||
invoice_statuses = {ic.club.name: ic.validated for ic in invoice_calls}
|
||||
|
||||
@ -95,14 +95,22 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView):
|
||||
return kwargs
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
if request.POST["month"]:
|
||||
start_date = datetime.strptime(request.POST["month"], "%Y-%m")
|
||||
month_str = request.POST.get("month")
|
||||
if not month_str:
|
||||
return self.get(request, *args, **kwargs)
|
||||
|
||||
year = start_date.year
|
||||
month = start_date.month
|
||||
try:
|
||||
start_date = datetime.strptime(month_str, "%Y-%m")
|
||||
start_date = date(start_date.year, start_date.month, 1)
|
||||
except ValueError:
|
||||
from django.shortcuts import redirect
|
||||
|
||||
return redirect(request.path)
|
||||
|
||||
club_names = list(
|
||||
Selling.objects.filter(date__year=year, date__month=month)
|
||||
Selling.objects.filter(
|
||||
date__year=start_date.year, date__month=start_date.month
|
||||
)
|
||||
.values_list("club__name", flat=True)
|
||||
.distinct()
|
||||
)
|
||||
@ -110,9 +118,7 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView):
|
||||
clubs = Club.objects.filter(name__in=club_names)
|
||||
club_map = {club.name: club for club in clubs}
|
||||
|
||||
invoice_calls = InvoiceCall.objects.filter(
|
||||
month=date(year, month, 1), club__in=clubs
|
||||
)
|
||||
invoice_calls = InvoiceCall.objects.filter(month=month_str, club__in=clubs)
|
||||
invoice_statuses = {ic.club.name: ic for ic in invoice_calls}
|
||||
|
||||
for club_name in club_names:
|
||||
@ -125,7 +131,7 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView):
|
||||
invoice_call.save()
|
||||
else:
|
||||
InvoiceCall.objects.create(
|
||||
month=date(year, month, 1),
|
||||
month=month_str,
|
||||
club=club_map[club_name],
|
||||
validated=is_checked,
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user