1+ from __future__ import annotations
2+
13import datetime
2- from decimal import Decimal
4+ from typing import TYPE_CHECKING
35
4- from django .db import models
6+ from django .db import models , transaction
57from django .utils import timezone
68
9+ from reservations .enums import CustomerTypeChoice
10+
11+ if TYPE_CHECKING :
12+ from decimal import Decimal
13+
14+ from reservations .models import Reservation
15+
16+
717__all__ = [
818 "ReservationStatistic" ,
919 "ReservationStatisticsReservationUnit" ,
@@ -137,6 +147,80 @@ class Meta:
137147 def __str__ (self ) -> str :
138148 return f"{ self .reservee_uuid } - { self .begin } - { self .end } "
139149
150+ @classmethod
151+ def for_reservation (cls , reservation : Reservation , * , save : bool = True ) -> ReservationStatistic : # noqa: PLR0915
152+ recurring_reservation = getattr (reservation , "recurring_reservation" , None )
153+ ability_group = getattr (recurring_reservation , "ability_group" , None )
154+ allocated_time_slot = getattr (recurring_reservation , "allocated_time_slot" , None )
155+
156+ requires_org_name = reservation .reservee_type != CustomerTypeChoice .INDIVIDUAL
157+ requires_org_id = not reservation .reservee_is_unregistered_association and requires_org_name
158+ by_profile_user = bool (getattr (reservation .user , "profile_id" , "" ))
159+
160+ statistic = ( #
161+ ReservationStatistic .objects .filter (reservation = reservation ).first ()
162+ or ReservationStatistic (reservation = reservation )
163+ )
164+
165+ statistic .ability_group = ability_group
166+ statistic .age_group = reservation .age_group
167+ statistic .age_group_name = str (reservation .age_group )
168+ statistic .applying_for_free_of_charge = reservation .applying_for_free_of_charge
169+ statistic .begin = reservation .begin
170+ statistic .buffer_time_after = reservation .buffer_time_after
171+ statistic .buffer_time_before = reservation .buffer_time_before
172+ statistic .cancel_reason = reservation .cancel_reason
173+ statistic .cancel_reason_text = getattr (reservation .cancel_reason , "reason" , "" )
174+ statistic .deny_reason = reservation .deny_reason
175+ statistic .deny_reason_text = getattr (reservation .deny_reason , "reason" , "" )
176+ statistic .duration_minutes = (reservation .end - reservation .begin ).total_seconds () / 60
177+ statistic .end = reservation .end
178+ statistic .home_city = reservation .home_city
179+ statistic .home_city_municipality_code = getattr (reservation .home_city , "municipality_code" , "" )
180+ statistic .home_city_name = reservation .home_city .name if reservation .home_city else ""
181+ statistic .is_applied = allocated_time_slot is not None
182+ statistic .is_recurring = recurring_reservation is not None
183+ statistic .is_subsidised = reservation .price < reservation .non_subsidised_price
184+ statistic .non_subsidised_price = reservation .non_subsidised_price
185+ statistic .non_subsidised_price_net = reservation .non_subsidised_price_net
186+ statistic .num_persons = reservation .num_persons
187+ statistic .price = reservation .price
188+ statistic .price_net = reservation .price_net
189+ statistic .purpose = reservation .purpose
190+ statistic .purpose_name = reservation .purpose .name if reservation .purpose else ""
191+ statistic .recurrence_begin_date = getattr (recurring_reservation , "begin_date" , None )
192+ statistic .recurrence_end_date = getattr (recurring_reservation , "end_date" , None )
193+ statistic .recurrence_uuid = getattr (recurring_reservation , "uuid" , "" )
194+ statistic .reservation = reservation
195+ statistic .reservation_confirmed_at = reservation .confirmed_at
196+ statistic .reservation_created_at = reservation .created_at
197+ statistic .reservation_handled_at = reservation .handled_at
198+ statistic .reservation_type = reservation .type
199+ statistic .reservee_address_zip = reservation .reservee_address_zip if by_profile_user else ""
200+ statistic .reservee_id = reservation .reservee_id if requires_org_id else ""
201+ statistic .reservee_is_unregistered_association = reservation .reservee_is_unregistered_association
202+ statistic .reservee_language = reservation .reservee_language
203+ statistic .reservee_organisation_name = reservation .reservee_organisation_name if requires_org_name else ""
204+ statistic .reservee_type = reservation .reservee_type
205+ statistic .reservee_uuid = str (reservation .user .tvp_uuid ) if reservation .user else ""
206+ statistic .state = reservation .state
207+ statistic .tax_percentage_value = reservation .tax_percentage_value
208+
209+ for res_unit in reservation .reservation_unit .all ():
210+ statistic .primary_reservation_unit = res_unit
211+ statistic .primary_reservation_unit_name = res_unit .name
212+ statistic .primary_unit_name = getattr (res_unit .unit , "name" , "" )
213+ statistic .primary_unit_tprek_id = getattr (res_unit .unit , "tprek_id" , "" )
214+ break
215+
216+ if statistic .is_applied and ability_group :
217+ statistic .ability_group_name = ability_group .name
218+
219+ if save :
220+ statistic .save ()
221+
222+ return statistic
223+
140224
141225class ReservationStatisticsReservationUnit (models .Model ):
142226 name = models .CharField (max_length = 255 )
@@ -163,3 +247,32 @@ class Meta:
163247
164248 def __str__ (self ) -> str :
165249 return f"{ self .reservation_statistics } - { self .reservation_unit } "
250+
251+ @classmethod
252+ def for_statistic (
253+ cls ,
254+ statistic : ReservationStatistic ,
255+ * ,
256+ save : bool = True ,
257+ ) -> list [ReservationStatisticsReservationUnit ]:
258+ to_save : list [ReservationStatisticsReservationUnit ] = []
259+ for reservation_unit in statistic .reservation .reservation_unit .all ():
260+ stat_unit = ReservationStatisticsReservationUnit (
261+ reservation_statistics = statistic ,
262+ reservation_unit = reservation_unit ,
263+ )
264+
265+ stat_unit .name = reservation_unit .name
266+ stat_unit .reservation_statistics = statistic
267+ stat_unit .reservation_unit = reservation_unit
268+ stat_unit .unit_name = getattr (reservation_unit .unit , "name" , "" )
269+ stat_unit .unit_tprek_id = getattr (reservation_unit .unit , "tprek_id" , "" )
270+
271+ to_save .append (stat_unit )
272+
273+ if save :
274+ with transaction .atomic ():
275+ ReservationStatisticsReservationUnit .objects .filter (reservation_statistics = statistic ).delete ()
276+ ReservationStatisticsReservationUnit .objects .bulk_create (to_save )
277+
278+ return to_save
0 commit comments