Add annotations that provide a per ticket type sales information.

This commit is contained in:
Víðir Valberg Guðmundsson 2021-07-29 15:08:20 +02:00
parent 517f29940a
commit 1006b926c5
2 changed files with 23 additions and 2 deletions

View file

@ -16,6 +16,10 @@
<th>Ticket Type</th> <th>Ticket Type</th>
<th class="text-center">Tickets Generated</th> <th class="text-center">Tickets Generated</th>
<th class="text-center">Products</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> </tr>
</thead> </thead>
<tbody> <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><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.shopticket_count }}</td>
<td class="text-center">{{ tt.product_set.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> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>

View file

@ -6,6 +6,7 @@ import logging
import qrcode import qrcode
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
from django.db.models import Sum, Count, F
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -17,8 +18,20 @@ logger = logging.getLogger("bornhack.%s" % __name__)
class TicketTypeManager(models.Manager): class TicketTypeManager(models.Manager):
def with_price_stats(self): def with_price_stats(self):
return self.annotate(shopticket_count=models.Count("shopticket")).exclude( total_units_sold = Sum("shopticket__opr__quantity", distinct=True)
shopticket_count=0 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)
) )