Sith/counter/views/invoice.py

89 lines
2.9 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-06-24 11:07:36 +00:00
from datetime import datetime, timedelta
2024-07-18 15:33:14 +00:00
from datetime import timezone as tz
from django.db.models import F
2024-06-24 11:07:36 +00:00
from django.utils import timezone
2024-11-27 17:43:26 +00:00
from django.views.generic import TemplateView
2016-03-28 12:54:35 +00:00
2024-06-24 11:07:36 +00:00
from accounting.models import CurrencyField
2024-11-27 17:43:26 +00:00
from counter.models import Refilling, Selling
2017-06-12 07:47:24 +00:00
2017-04-04 13:45:02 +00:00
class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView):
2018-10-04 19:29:19 +00:00
template_name = "counter/invoices_call.jinja"
current_tab = "invoices_call"
2016-09-29 16:17:44 +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)
2018-10-04 19:29:19 +00:00
kwargs["months"] = Selling.objects.datetimes("date", "month", order="DESC")
2024-07-18 15:33:14 +00:00
if "month" in self.request.GET:
2018-10-04 19:29:19 +00:00
start_date = datetime.strptime(self.request.GET["month"], "%Y-%m")
2024-07-18 15:33:14 +00:00
else:
2018-10-04 19:29:19 +00:00
start_date = datetime(
year=timezone.now().year,
month=(timezone.now().month + 10) % 12 + 1,
day=1,
)
2024-07-18 15:33:14 +00:00
start_date = start_date.replace(tzinfo=tz.utc)
2018-10-04 19:29:19 +00:00
end_date = (start_date + timedelta(days=32)).replace(
day=1, hour=0, minute=0, microsecond=0
)
2024-11-27 17:43:26 +00:00
from django.db.models import Case, Sum, When
2018-10-04 19:29:19 +00:00
kwargs["sum_cb"] = sum(
[
r.amount
for r in Refilling.objects.filter(
payment_method="CARD",
is_validated=True,
date__gte=start_date,
date__lte=end_date,
)
]
)
kwargs["sum_cb"] += sum(
[
s.quantity * s.unit_price
for s in Selling.objects.filter(
payment_method="CARD",
is_validated=True,
date__gte=start_date,
date__lte=end_date,
)
]
)
kwargs["start_date"] = start_date
kwargs["sums"] = (
Selling.objects.values("club__name")
.annotate(
selling_sum=Sum(
Case(
When(
date__gte=start_date,
date__lt=end_date,
then=F("unit_price") * F("quantity"),
),
output_field=CurrencyField(),
)
)
)
.exclude(selling_sum=None)
.order_by("-selling_sum")
)
2016-09-29 16:17:44 +00:00
return kwargs