From 1006b926c569db1e576737df000d295cfd14fee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Thu, 29 Jul 2021 15:08:20 +0200 Subject: [PATCH] Add annotations that provide a per ticket type sales information. --- src/backoffice/templates/ticket_stats.html | 8 ++++++++ src/tickets/models.py | 17 +++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/backoffice/templates/ticket_stats.html b/src/backoffice/templates/ticket_stats.html index ecf34f02..ba890026 100644 --- a/src/backoffice/templates/ticket_stats.html +++ b/src/backoffice/templates/ticket_stats.html @@ -16,6 +16,10 @@ Ticket Type Tickets Generated Products + Units Sold + Total Income + Total Cost + Total Profit @@ -24,6 +28,10 @@ {{ tt.name }} {{ tt.shopticket_count }} {{ tt.product_set.count }} + {{ tt.total_units_sold }} + {{ tt.total_income|floatformat:"2" }} DKK + {{ tt.total_cost|floatformat:"2" }} DKK + {{ tt.total_profit|floatformat:"2" }} DKK {% endfor %} diff --git a/src/tickets/models.py b/src/tickets/models.py index 92524c90..14aaf2ee 100644 --- a/src/tickets/models.py +++ b/src/tickets/models.py @@ -6,6 +6,7 @@ import logging import qrcode from django.conf import settings from django.db import models +from django.db.models import Sum, Count, F from django.urls import reverse_lazy from django.utils.translation import ugettext_lazy as _ @@ -17,8 +18,20 @@ logger = logging.getLogger("bornhack.%s" % __name__) class TicketTypeManager(models.Manager): def with_price_stats(self): - return self.annotate(shopticket_count=models.Count("shopticket")).exclude( - shopticket_count=0 + total_units_sold = Sum("shopticket__opr__quantity", distinct=True) + cost = F("shopticket__opr__quantity") * F("shopticket__opr__product__cost") + income = F("shopticket__opr__quantity") * F("shopticket__opr__product__price") + profit = income - cost + total_cost = Sum(cost, distinct=True) + total_profit = Sum(profit, distinct=True) + total_income = Sum(income, distinct=True) + + return ( + self.filter(shopticket__isnull=False) + .annotate(total_units_sold=total_units_sold) + .annotate(total_income=total_income) + .annotate(total_cost=total_cost) + .annotate(total_profit=total_profit) )