From 6803294358cb069218513b61aa6f3c6fcd5f7517 Mon Sep 17 00:00:00 2001 From: Kenneth SOARES Date: Mon, 2 Jun 2025 16:50:46 +0200 Subject: [PATCH] refactored invoice view and template --- counter/templates/counter/invoices_call.jinja | 3 +- counter/views/invoice.py | 39 ++++++++----------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/counter/templates/counter/invoices_call.jinja b/counter/templates/counter/invoices_call.jinja index 973021fb..bdc43d31 100644 --- a/counter/templates/counter/invoices_call.jinja +++ b/counter/templates/counter/invoices_call.jinja @@ -27,7 +27,7 @@ {% for i in sums %} {{ i['club__name'] }} - {{ i['selling_sum'] }} € + {{"%.2f"|format(i['selling_sum'])}} € {% endfor %} @@ -35,4 +35,3 @@ {% endblock %} - diff --git a/counter/views/invoice.py b/counter/views/invoice.py index cabbccdb..34e97a40 100644 --- a/counter/views/invoice.py +++ b/counter/views/invoice.py @@ -46,30 +46,23 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView): ) from django.db.models import Case, Sum, When - 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["sum_cb"] = Refilling.objects.filter( + payment_method="CARD", + is_validated=True, + date__gte=start_date, + date__lte=end_date, + ).aggregate(amount=Sum(F("amount"), default=0))["amount"] + + kwargs["sum_cb"] += Selling.objects.filter( + payment_method="CARD", + is_validated=True, + date__gte=start_date, + date__lte=end_date, + ).aggregate(amount=Sum(F("quantity") * F("unit_price"), default=0))["amount"] + kwargs["start_date"] = start_date - kwargs["sums"] = ( + + kwargs["sums"] = list( Selling.objects.values("club__name") .annotate( selling_sum=Sum(