Merge pull request #858 from bornhack/add_ticket_stats_on_overview

Add annotations that provide a per ticket type sales information.
This commit is contained in:
Thomas Steen Rasmussen 2021-07-29 15:16:52 +02:00 committed by GitHub
commit e96b8a9e44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -16,6 +16,10 @@
<th>Ticket Type</th>
<th class="text-center">Tickets Generated</th>
<th class="text-center">Products</th>
<th class="text-center">Units Sold</th>
<th class="text-right">Total Income</th>
<th class="text-right">Total Cost</th>
<th class="text-right">Total Profit</th>
</tr>
</thead>
<tbody>
@ -24,6 +28,10 @@
<td><a href="{% url 'backoffice:shop_ticket_stats_detail' camp_slug=camp.slug pk=tt.pk %}">{{ tt.name }}</a></td>
<td class="text-center">{{ tt.shopticket_count }}</td>
<td class="text-center">{{ tt.product_set.count }}</td>
<td class="text-center">{{ tt.total_units_sold }}</td>
<td class="text-right">{{ tt.total_income|floatformat:"2" }}&nbsp;DKK</td>
<td class="text-right">{{ tt.total_cost|floatformat:"2" }}&nbsp;DKK</td>
<td class="text-right">{{ tt.total_profit|floatformat:"2" }}&nbsp;DKK</td>
</tr>
{% endfor %}
</tbody>

View File

@ -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)
)