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