From 6ca183ad553cbb34075d9192f36a6d508e1a8349 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 5 Apr 2017 14:23:51 +0200 Subject: [PATCH] Better sum writing --- counter/views.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/counter/views.py b/counter/views.py index 0a29a61e..e66d5988 100644 --- a/counter/views.py +++ b/counter/views.py @@ -1031,14 +1031,10 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView): start_date = start_date.replace(tzinfo=pytz.UTC) end_date = (start_date + timedelta(days=32)).replace(day=1, hour=0, minute=0, microsecond=0) from django.db.models import Sum, Case, When, F, DecimalField - sum_cb = 0 - for r in Refilling.objects.filter(payment_method='CARD', is_validated=True, - date__gte=start_date, date__lte=end_date): - sum_cb +=r.amount - for s in Selling.objects.filter(payment_method='CARD', is_validated=True, - date__gte=start_date, date__lte=end_date): - sum_cb +=s.quantity*s.unit_price - kwargs['sum_cb'] = sum_cb + 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)]) + 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,