From ad93c361fdf723c97bbcb11dfe4d3000b7099818 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 25 Nov 2024 14:18:13 -0500 Subject: [PATCH] membershipworks: Group tickets by price group for event invoices This groups tickets by their actual price grouping, instead of always using member/non-member, which is not a useful distinction post 2024-07-01. --- membershipworks/models.py | 45 +++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/membershipworks/models.py b/membershipworks/models.py index eccdcb5..24b8342 100644 --- a/membershipworks/models.py +++ b/membershipworks/models.py @@ -704,11 +704,8 @@ class EventInvoice(models.Model): class EventTicketTypeQuerySet(models.QuerySet["EventTicketType"]): def group_by_ticket_type(self): - return self.values(is_members_ticket=Q(restrict_to__isnull=False)).annotate( - label=Case( - When(Q(is_members_ticket=True), Value("Members")), - default=Value("Non-Members"), - ), + return self.values("price_group").annotate( + label=F("price_group"), actual_price=F("actual_price"), **{ field: Sum(field) @@ -732,25 +729,31 @@ class EventTicketTypeManager(models.Manager["EventTicketType"]): # restricted ticket, but list price for unrestricted # (Non-Members) ticket. After, use Members ticket price # for all tickets except where members ticket is free. - actual_price=Case( + price_group=Case( When( - # member ticket - Q(restrict_to__has_key=settings.MW_MEMBERS_FOLDER_ID) - | ( - # non-member ticket - Q(restrict_to__isnull=True) - & ( - Q( - event__start__lt=datetime( - year=2024, - month=7, - day=1, - tzinfo=timezone.get_default_timezone(), - ) - ) - | Q(members_price=0) + Q(members_price=0) + | Q( + event__start__lt=datetime( + year=2024, + month=7, + day=1, + tzinfo=timezone.get_default_timezone(), ) ), + Case( + When(Q(restrict_to__isnull=True), Value("Non-Members")), + default=Value("Members"), + ), + ), + default=Value("Attendee"), + ), + actual_price=Case( + # Price group will be "Non-Members" iff we are using + # the list price for that ticket type. In all other + # cases (special program discounts or non-members + # tickets after 2024-07-01), use the members price + When( + Q(price_group=Value("Non-Members")), "list_price", ), default="members_price",