Sith/counter/views/cash.py

355 lines
14 KiB
Python
Raw Normal View History

#
# Copyright 2023 © AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
#
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
#
# You can find the source code of the website at https://github.com/ae-utbm/sith
#
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
2024-09-23 08:25:27 +00:00
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
# OR WITHIN THE LOCAL FILE "LICENSE"
#
#
2024-11-27 17:46:24 +00:00
from datetime import datetime
2024-07-18 15:33:14 +00:00
from datetime import timezone as tz
2024-06-24 11:07:36 +00:00
from django import forms
from django.conf import settings
2024-11-27 17:46:24 +00:00
from django.http import HttpResponseRedirect
2024-06-24 11:07:36 +00:00
from django.urls import reverse, reverse_lazy
from django.utils.translation import gettext_lazy as _
2024-11-27 17:46:24 +00:00
from django.views.generic import DetailView, ListView
from django.views.generic.edit import UpdateView
2016-03-28 12:54:35 +00:00
2024-11-27 17:46:24 +00:00
from core.views import CanViewMixin
from counter.forms import CashSummaryFormBase
2018-10-04 19:29:19 +00:00
from counter.models import (
2024-06-24 11:07:36 +00:00
CashRegisterSummary,
CashRegisterSummaryItem,
2018-10-04 19:29:19 +00:00
Counter,
2024-06-24 11:07:36 +00:00
Refilling,
2018-10-04 19:29:19 +00:00
)
2024-08-05 08:46:15 +00:00
from counter.utils import is_logged_in_counter
2016-03-28 12:54:35 +00:00
2017-06-12 07:47:24 +00:00
2016-08-26 18:57:04 +00:00
class CashRegisterSummaryForm(forms.Form):
2024-07-12 07:34:16 +00:00
"""Provide the cash summary form."""
2018-10-04 19:29:19 +00:00
2017-03-01 09:21:57 +00:00
ten_cents = forms.IntegerField(label=_("10 cents"), required=False, min_value=0)
twenty_cents = forms.IntegerField(label=_("20 cents"), required=False, min_value=0)
fifty_cents = forms.IntegerField(label=_("50 cents"), required=False, min_value=0)
one_euro = forms.IntegerField(label=_("1 euro"), required=False, min_value=0)
two_euros = forms.IntegerField(label=_("2 euros"), required=False, min_value=0)
five_euros = forms.IntegerField(label=_("5 euros"), required=False, min_value=0)
ten_euros = forms.IntegerField(label=_("10 euros"), required=False, min_value=0)
twenty_euros = forms.IntegerField(label=_("20 euros"), required=False, min_value=0)
fifty_euros = forms.IntegerField(label=_("50 euros"), required=False, min_value=0)
2018-10-04 19:29:19 +00:00
hundred_euros = forms.IntegerField(
label=_("100 euros"), required=False, min_value=0
)
check_1_value = forms.DecimalField(
label=_("Check amount"), required=False, min_value=0
)
check_1_quantity = forms.IntegerField(
label=_("Check quantity"), required=False, min_value=0
)
check_2_value = forms.DecimalField(
label=_("Check amount"), required=False, min_value=0
)
check_2_quantity = forms.IntegerField(
label=_("Check quantity"), required=False, min_value=0
)
check_3_value = forms.DecimalField(
label=_("Check amount"), required=False, min_value=0
)
check_3_quantity = forms.IntegerField(
label=_("Check quantity"), required=False, min_value=0
)
check_4_value = forms.DecimalField(
label=_("Check amount"), required=False, min_value=0
)
check_4_quantity = forms.IntegerField(
label=_("Check quantity"), required=False, min_value=0
)
check_5_value = forms.DecimalField(
label=_("Check amount"), required=False, min_value=0
)
check_5_quantity = forms.IntegerField(
label=_("Check quantity"), required=False, min_value=0
)
2016-08-26 18:57:04 +00:00
comment = forms.CharField(label=_("Comment"), required=False)
emptied = forms.BooleanField(label=_("Emptied"), required=False)
def __init__(self, *args, **kwargs):
2018-10-04 19:29:19 +00:00
instance = kwargs.pop("instance", None)
2024-06-27 12:46:43 +00:00
super().__init__(*args, **kwargs)
if instance:
2018-10-04 19:29:19 +00:00
self.fields["ten_cents"].initial = (
instance.ten_cents.quantity if instance.ten_cents else 0
)
self.fields["twenty_cents"].initial = (
instance.twenty_cents.quantity if instance.twenty_cents else 0
)
self.fields["fifty_cents"].initial = (
instance.fifty_cents.quantity if instance.fifty_cents else 0
)
self.fields["one_euro"].initial = (
instance.one_euro.quantity if instance.one_euro else 0
)
self.fields["two_euros"].initial = (
instance.two_euros.quantity if instance.two_euros else 0
)
self.fields["five_euros"].initial = (
instance.five_euros.quantity if instance.five_euros else 0
)
self.fields["ten_euros"].initial = (
instance.ten_euros.quantity if instance.ten_euros else 0
)
self.fields["twenty_euros"].initial = (
instance.twenty_euros.quantity if instance.twenty_euros else 0
)
self.fields["fifty_euros"].initial = (
instance.fifty_euros.quantity if instance.fifty_euros else 0
)
self.fields["hundred_euros"].initial = (
instance.hundred_euros.quantity if instance.hundred_euros else 0
)
self.fields["check_1_quantity"].initial = (
instance.check_1.quantity if instance.check_1 else 0
)
self.fields["check_2_quantity"].initial = (
instance.check_2.quantity if instance.check_2 else 0
)
self.fields["check_3_quantity"].initial = (
instance.check_3.quantity if instance.check_3 else 0
)
self.fields["check_4_quantity"].initial = (
instance.check_4.quantity if instance.check_4 else 0
)
self.fields["check_5_quantity"].initial = (
instance.check_5.quantity if instance.check_5 else 0
)
self.fields["check_1_value"].initial = (
instance.check_1.value if instance.check_1 else 0
)
self.fields["check_2_value"].initial = (
instance.check_2.value if instance.check_2 else 0
)
self.fields["check_3_value"].initial = (
instance.check_3.value if instance.check_3 else 0
)
self.fields["check_4_value"].initial = (
instance.check_4.value if instance.check_4 else 0
)
self.fields["check_5_value"].initial = (
instance.check_5.value if instance.check_5 else 0
)
self.fields["comment"].initial = instance.comment
self.fields["emptied"].initial = instance.emptied
self.instance = instance
else:
self.instance = None
def save(self, counter=None):
2016-08-26 18:57:04 +00:00
cd = self.cleaned_data
summary = self.instance or CashRegisterSummary(
2018-10-04 19:29:19 +00:00
counter=counter, user=counter.get_random_barman()
2017-06-12 07:47:24 +00:00
)
2018-10-04 19:29:19 +00:00
summary.comment = cd["comment"]
summary.emptied = cd["emptied"]
2016-08-26 18:57:04 +00:00
summary.save()
summary.items.all().delete()
2016-08-26 18:57:04 +00:00
# Cash
2018-10-04 19:29:19 +00:00
if cd["ten_cents"]:
CashRegisterSummaryItem(
cash_summary=summary, value=0.1, quantity=cd["ten_cents"]
).save()
if cd["twenty_cents"]:
CashRegisterSummaryItem(
cash_summary=summary, value=0.2, quantity=cd["twenty_cents"]
).save()
if cd["fifty_cents"]:
CashRegisterSummaryItem(
cash_summary=summary, value=0.5, quantity=cd["fifty_cents"]
).save()
if cd["one_euro"]:
CashRegisterSummaryItem(
cash_summary=summary, value=1, quantity=cd["one_euro"]
).save()
if cd["two_euros"]:
CashRegisterSummaryItem(
cash_summary=summary, value=2, quantity=cd["two_euros"]
).save()
if cd["five_euros"]:
CashRegisterSummaryItem(
cash_summary=summary, value=5, quantity=cd["five_euros"]
).save()
if cd["ten_euros"]:
CashRegisterSummaryItem(
cash_summary=summary, value=10, quantity=cd["ten_euros"]
).save()
if cd["twenty_euros"]:
CashRegisterSummaryItem(
cash_summary=summary, value=20, quantity=cd["twenty_euros"]
).save()
if cd["fifty_euros"]:
CashRegisterSummaryItem(
cash_summary=summary, value=50, quantity=cd["fifty_euros"]
).save()
if cd["hundred_euros"]:
CashRegisterSummaryItem(
cash_summary=summary, value=100, quantity=cd["hundred_euros"]
).save()
2016-08-26 18:57:04 +00:00
# Checks
2018-10-04 19:29:19 +00:00
if cd["check_1_quantity"]:
CashRegisterSummaryItem(
cash_summary=summary,
value=cd["check_1_value"],
quantity=cd["check_1_quantity"],
is_check=True,
2018-10-04 19:29:19 +00:00
).save()
if cd["check_2_quantity"]:
CashRegisterSummaryItem(
cash_summary=summary,
value=cd["check_2_value"],
quantity=cd["check_2_quantity"],
is_check=True,
2018-10-04 19:29:19 +00:00
).save()
if cd["check_3_quantity"]:
CashRegisterSummaryItem(
cash_summary=summary,
value=cd["check_3_value"],
quantity=cd["check_3_quantity"],
is_check=True,
2018-10-04 19:29:19 +00:00
).save()
if cd["check_4_quantity"]:
CashRegisterSummaryItem(
cash_summary=summary,
value=cd["check_4_value"],
quantity=cd["check_4_quantity"],
is_check=True,
2018-10-04 19:29:19 +00:00
).save()
if cd["check_5_quantity"]:
CashRegisterSummaryItem(
cash_summary=summary,
value=cd["check_5_value"],
quantity=cd["check_5_quantity"],
is_check=True,
2018-10-04 19:29:19 +00:00
).save()
2016-08-26 18:57:04 +00:00
if summary.items.count() < 1:
summary.delete()
2017-06-12 07:47:24 +00:00
2016-09-28 09:07:32 +00:00
class CounterCashSummaryView(CounterTabsMixin, CanViewMixin, DetailView):
2024-07-12 07:34:16 +00:00
"""Provide the cash summary form."""
2018-10-04 19:29:19 +00:00
2016-08-26 18:57:04 +00:00
model = Counter
pk_url_kwarg = "counter_id"
2018-10-04 19:29:19 +00:00
template_name = "counter/cash_register_summary.jinja"
2016-09-28 09:07:32 +00:00
current_tab = "cash_summary"
def dispatch(self, request, *args, **kwargs):
2024-07-12 07:34:16 +00:00
"""We have here again a very particular right handling."""
2016-09-28 09:07:32 +00:00
self.object = self.get_object()
2024-08-05 08:46:15 +00:00
if is_logged_in_counter(request) and self.object.barmen_list:
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
2018-10-04 19:29:19 +00:00
return HttpResponseRedirect(
reverse("counter:details", kwargs={"counter_id": self.object.id})
+ "?bad_location"
)
2016-08-26 18:57:04 +00:00
def get(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = CashRegisterSummaryForm()
2024-06-27 12:46:43 +00:00
return super().get(request, *args, **kwargs)
2016-08-26 18:57:04 +00:00
def post(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = CashRegisterSummaryForm(request.POST)
if self.form.is_valid():
self.form.save(self.object)
return HttpResponseRedirect(self.get_success_url())
2024-06-27 12:46:43 +00:00
return super().get(request, *args, **kwargs)
2016-08-26 18:57:04 +00:00
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return reverse_lazy("counter:details", kwargs={"counter_id": self.object.id})
2016-08-26 18:57:04 +00:00
def get_context_data(self, **kwargs):
2024-07-12 07:34:16 +00:00
"""Add form to the context."""
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["form"] = self.form
2016-08-26 18:57:04 +00:00
return kwargs
2016-09-12 15:34:33 +00:00
2017-06-12 07:47:24 +00:00
class CashSummaryEditView(CounterAdminTabsMixin, CounterAdminMixin, UpdateView):
2024-07-12 07:34:16 +00:00
"""Edit cash summaries."""
2018-10-04 19:29:19 +00:00
model = CashRegisterSummary
2018-10-04 19:29:19 +00:00
template_name = "counter/cash_register_summary.jinja"
context_object_name = "cashsummary"
pk_url_kwarg = "cashsummary_id"
form_class = CashRegisterSummaryForm
current_tab = "cash_summary"
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return reverse("counter:cash_summary_list")
2016-09-15 09:07:03 +00:00
2017-06-12 07:47:24 +00:00
2017-04-04 13:45:02 +00:00
class CashSummaryListView(CounterAdminTabsMixin, CounterAdminMixin, ListView):
2024-07-12 07:34:16 +00:00
"""Display a list of cash summaries."""
2018-10-04 19:29:19 +00:00
2016-09-13 00:04:49 +00:00
model = CashRegisterSummary
2018-10-04 19:29:19 +00:00
template_name = "counter/cash_summary_list.jinja"
2016-09-13 00:04:49 +00:00
context_object_name = "cashsummary_list"
current_tab = "cash_summary"
2018-10-04 19:29:19 +00:00
queryset = CashRegisterSummary.objects.all().order_by("-date")
2017-03-13 22:32:06 +00:00
paginate_by = settings.SITH_COUNTER_CASH_SUMMARY_LENGTH
2016-09-13 00:04:49 +00:00
def get_context_data(self, **kwargs):
2024-07-12 07:34:16 +00:00
"""Add sums to the context."""
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
form = CashSummaryFormBase(self.request.GET)
2018-10-04 19:29:19 +00:00
kwargs["form"] = form
kwargs["summaries_sums"] = {}
kwargs["refilling_sums"] = {}
2016-09-13 00:04:49 +00:00
for c in Counter.objects.filter(type="BAR").all():
refillings = Refilling.objects.filter(counter=c)
cashredistersummaries = CashRegisterSummary.objects.filter(counter=c)
2018-10-04 19:29:19 +00:00
if form.is_valid() and form.cleaned_data["begin_date"]:
refillings = refillings.filter(
date__gte=form.cleaned_data["begin_date"]
)
cashredistersummaries = cashredistersummaries.filter(
date__gte=form.cleaned_data["begin_date"]
)
2016-09-13 00:04:49 +00:00
else:
2018-10-04 19:29:19 +00:00
last_summary = (
CashRegisterSummary.objects.filter(counter=c, emptied=True)
.order_by("-date")
.first()
)
if last_summary:
2016-10-18 12:48:47 +00:00
refillings = refillings.filter(date__gt=last_summary.date)
2018-10-04 19:29:19 +00:00
cashredistersummaries = cashredistersummaries.filter(
date__gt=last_summary.date
)
else:
2018-10-04 19:29:19 +00:00
refillings = refillings.filter(
2024-07-18 15:33:14 +00:00
date__gte=datetime(year=1994, month=5, day=17, tzinfo=tz.utc)
2018-10-04 19:29:19 +00:00
) # My birth date should be old enough
cashredistersummaries = cashredistersummaries.filter(
2024-07-18 15:33:14 +00:00
date__gte=datetime(year=1994, month=5, day=17, tzinfo=tz.utc)
2018-10-04 19:29:19 +00:00
)
if form.is_valid() and form.cleaned_data["end_date"]:
refillings = refillings.filter(date__lte=form.cleaned_data["end_date"])
cashredistersummaries = cashredistersummaries.filter(
date__lte=form.cleaned_data["end_date"]
)
kwargs["summaries_sums"][c.name] = sum(
[s.get_total() for s in cashredistersummaries.all()]
)
kwargs["refilling_sums"][c.name] = sum([s.amount for s in refillings.all()])
2016-09-13 00:04:49 +00:00
return kwargs