From 58cc8cb2f8078ae6ee8f0f8f756582a6c5a73772 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Fri, 8 Mar 2024 15:28:25 -0500 Subject: [PATCH] doorcontrol: Add EventTicketType method to group by member/non-member --- membershipworks/models.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/membershipworks/models.py b/membershipworks/models.py index 97fb04a..38e335f 100644 --- a/membershipworks/models.py +++ b/membershipworks/models.py @@ -15,6 +15,7 @@ from django.db.models import ( Q, Subquery, Sum, + Value, When, ) from django.db.models.functions import Coalesce @@ -570,6 +571,28 @@ class EventMeetingTime(models.Model): ] +class EventTicketTypeQuerySet(models.QuerySet["EventTicketType"]): + def group_by_ticket_type(self): + return self.values("is_members_ticket").annotate( + label=Case( + When(Q(is_members_ticket=True), Value("Members")), + default=Value("Non-Members"), + ), + actual_price=F("actual_price"), + **{ + field: Sum(field) + for field in [ + "quantity", + "materials", + "amount", + "amount_without_materials", + "instructor_revenue", + "instructor_amount", + ] + }, + ) + + class EventTicketTypeManager(models.Manager["EventTicketType"]): def get_queryset(self) -> models.QuerySet["EventTicketType"]: members_folder = Subquery( @@ -627,7 +650,7 @@ class EventTicketTypeManager(models.Manager["EventTicketType"]): class EventTicketType(DBView): - objects = EventTicketTypeManager() + objects = EventTicketTypeManager.from_queryset(EventTicketTypeQuerySet)() event = models.ForeignKey( EventExt, on_delete=models.CASCADE, related_name="ticket_types"