PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.31
Booking for Appointments and Events Calendar – Amelia v1.2.31
2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Booking / Event / GetEventBookingsCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Event Last commit date
AddEventCommand.php 1 year ago AddEventCommandHandler.php 1 year ago DeleteEventBookingCommand.php 7 years ago DeleteEventBookingCommandHandler.php 1 year ago DeleteEventCommand.php 1 year ago DeleteEventCommandHandler.php 2 years ago GetCalendarEventsCommand.php 1 year ago GetCalendarEventsCommandHandler.php 1 year ago GetEventBookingsCommand.php 1 year ago GetEventBookingsCommandHandler.php 1 year ago GetEventCommand.php 7 years ago GetEventCommandHandler.php 1 year ago GetEventDeleteEffectCommand.php 1 year ago GetEventDeleteEffectCommandHandler.php 1 year ago GetEventsCommand.php 1 year ago GetEventsCommandHandler.php 1 year ago UpdateEventBookingCommand.php 7 years ago UpdateEventBookingCommandHandler.php 1 year ago UpdateEventCommand.php 1 year ago UpdateEventCommandHandler.php 1 year ago UpdateEventStatusCommand.php 1 year ago UpdateEventStatusCommandHandler.php 1 year ago
GetEventBookingsCommandHandler.php
332 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\Booking\Event;
4
5 use AmeliaBooking\Application\Commands\CommandHandler;
6 use AmeliaBooking\Application\Commands\CommandResult;
7 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
8 use AmeliaBooking\Application\Services\Booking\EventApplicationService;
9 use AmeliaBooking\Application\Services\Payment\PaymentApplicationService;
10 use AmeliaBooking\Application\Services\User\UserApplicationService;
11 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
12 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
13 use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking;
14 use AmeliaBooking\Domain\Entity\Booking\Event\CustomerBookingEventTicket;
15 use AmeliaBooking\Domain\Entity\Booking\Event\Event;
16 use AmeliaBooking\Domain\Entity\Booking\Event\EventTicket;
17 use AmeliaBooking\Domain\Entity\Entities;
18 use AmeliaBooking\Domain\Entity\User\AbstractUser;
19 use AmeliaBooking\Domain\Entity\User\Provider;
20 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
21 use AmeliaBooking\Domain\Services\Settings\SettingsService;
22 use AmeliaBooking\Domain\ValueObjects\String\BookableType;
23 use AmeliaBooking\Domain\ValueObjects\String\BookingStatus;
24 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
25 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
26 use Exception;
27
28 /**
29 * Class GetEventBookingsCommandHandler
30 *
31 * @package AmeliaBooking\Application\Commands\Booking\Event
32 */
33 class GetEventBookingsCommandHandler extends CommandHandler
34 {
35 /**
36 * @param GetEventBookingsCommand $command
37 *
38 * @return CommandResult
39 *
40 * @throws AccessDeniedException
41 * @throws InvalidArgumentException
42 * @throws QueryExecutionException
43 * @throws Exception
44 */
45 public function handle(GetEventBookingsCommand $command)
46 {
47 $result = new CommandResult();
48
49 /** @var SettingsService $settingsDS */
50 $settingsDS = $this->container->get('domain.settings.service');
51 /** @var UserApplicationService $userAS */
52 $userAS = $this->container->get('application.user.service');
53 /** @var PaymentApplicationService $paymentAS */
54 $paymentAS = $this->container->get('application.payment.service');
55 /** @var EventApplicationService $eventApplicationService */
56 $eventApplicationService = $this->container->get('application.booking.event.service');
57 /** @var CustomerBookingRepository $bookingRepository */
58 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
59
60 $params = $command->getField('params');
61
62 try {
63 /** @var AbstractUser $user */
64 $user = $command->getUserApplicationService()->authorization(
65 $command->getToken(),
66 $command->getCabinetType()
67 );
68 } catch (AuthorizationException $e) {
69 $result->setResult(CommandResult::RESULT_ERROR);
70 $result->setData(
71 [
72 'reauthorize' => true
73 ]
74 );
75
76 return $result;
77 }
78
79 if ($user && $userAS->isAmeliaUser($user) && $userAS->isCustomer($user)) {
80 $params['customers'] = [$user->getId()->getValue()];
81 }
82
83 if ($user && $user->getType() === AbstractUser::USER_ROLE_PROVIDER) {
84 $params['providers'] = [$user->getId()->getValue()];
85 }
86
87 $providerTimeZoneSet = ($user instanceof Provider) && $user->getTimeZone() && $user->getTimeZone()->getValue();
88
89 if (isset($params['dates'][0])) {
90 $params['dates'][0] ? $params['dates'][0] .= ' 00:00:00' : null;
91 }
92
93 if (isset($params['dates'][1])) {
94 $params['dates'][1] ? $params['dates'][1] .= ' 23:59:59' : null;
95 }
96
97 $itemsPerPageBackEnd = $settingsDS->getSetting('general', 'itemsPerPageBackEnd');
98
99 /** @var Event $event */
100 $event = $eventApplicationService->getEventById(
101 (int)$params['events'][0],
102 [
103 'fetchEventsTickets' => true,
104 'fetchBookings' => true,
105 'fetchBookingsTickets' => true,
106 ]
107 );
108
109 $attendeeCount = 0;
110
111 $waitingCount = 0;
112
113 $maxCapacity = 0;
114
115 if ($event->getCustomPricing()->getValue()) {
116 /** @var CustomerBooking $customerBooking */
117 foreach ($event->getBookings()->getItems() as $customerBooking) {
118 if (
119 $customerBooking->getStatus()->getValue() === BookingStatus::APPROVED ||
120 $customerBooking->getStatus()->getValue() === BookingStatus::PENDING
121 ) {
122 /** @var CustomerBookingEventTicket $bookingToEventTicket */
123 foreach ($customerBooking->getTicketsBooking()->getItems() as $bookingToEventTicket) {
124 $attendeeCount += $bookingToEventTicket->getPersons()->getValue();
125 }
126 }
127
128 if ($customerBooking->getStatus()->getValue() === BookingStatus::WAITING) {
129 /** @var CustomerBookingEventTicket $bookingToEventTicket */
130 foreach ($customerBooking->getTicketsBooking()->getItems() as $bookingToEventTicket) {
131 $waitingCount += $bookingToEventTicket->getPersons()->getValue();
132 }
133 }
134 }
135
136 /** @var EventTicket $ticket */
137 foreach ($event->getCustomTickets()->getItems() as $ticket) {
138 $maxCapacity += $ticket->getSpots()->getValue();
139 }
140 } else {
141 $maxCapacity = $event->getMaxCapacity()->getValue();
142
143 /** @var CustomerBooking $customerBooking */
144 foreach ($event->getBookings()->getItems() as $customerBooking) {
145 if (
146 $customerBooking->getStatus()->getValue() === BookingStatus::APPROVED ||
147 $customerBooking->getStatus()->getValue() === BookingStatus::PENDING
148 ) {
149 $attendeeCount += $customerBooking->getPersons()->getValue();
150 }
151 }
152
153 /** @var CustomerBooking $customerBooking */
154 foreach ($event->getBookings()->getItems() as $customerBooking) {
155 if ($customerBooking->getStatus()->getValue() === BookingStatus::WAITING) {
156 $waitingCount += $customerBooking->getPersons()->getValue();
157 }
158 }
159 }
160
161 $waitingListSettings = $settingsDS->getSetting('appointments', 'waitingListEvents');
162
163 $eventSettings = $waitingListSettings['enabled'] && $event->getSettings() && $event->getSettings()->getValue()
164 ? json_decode($event->getSettings()->getValue(), true)
165 : null;
166
167 $waitingCapacity = 0;
168
169 if ($eventSettings && !empty($eventSettings['waitingList']['enabled'])) {
170 if ($event->getCustomPricing()->getValue()) {
171 /** @var EventTicket $ticket */
172 foreach ($event->getCustomTickets()->getItems() as $ticket) {
173 $waitingCapacity += $ticket->getWaitingListSpots()->getValue();
174 }
175 } else {
176 $waitingCapacity = $eventSettings['waitingList']['maxCapacity'];
177 }
178 }
179
180 $bookingIds = $bookingRepository->getEventBookingIdsByCriteria($params, $itemsPerPageBackEnd);
181
182 if (!$bookingIds && $params['page'] && (int)$params['page'] > 1) {
183 $params['page'] = 1;
184
185 $bookingIds = $bookingRepository->getEventBookingIdsByCriteria($params, $itemsPerPageBackEnd);
186 }
187
188 if (empty($bookingIds)) {
189 $result->setResult(CommandResult::RESULT_SUCCESS);
190 $result->setMessage('Successfully retrieved event bookings');
191 $result->setData(
192 [
193 Entities::BOOKINGS => [],
194 'totalCount' => sizeof($bookingRepository->getEventBookingIdsByCriteria()),
195 'filteredCount' => 0,
196 'attendeeCount' => 0,
197 'waitingCount' => 0,
198 'waitingCapacity' => $waitingCapacity,
199 'maxCapacity' => $maxCapacity,
200 ]
201 );
202
203 return $result;
204 }
205
206 $bookings = $bookingRepository->getEventBookingsByIds(
207 $bookingIds,
208 array_merge(
209 !empty($params['dates']) ? ['dates' => $params['dates']] : [],
210 [
211 'fetchBookingsPayments' => true,
212 'fetchBookingsCoupons' => true,
213 'fetchProviders' => true,
214 'fetchCustomers' => true
215 ]
216 )
217 );
218
219
220 $customersNoShowCountIds = [];
221
222 $noShowTagEnabled = $settingsDS->getSetting('roles', 'enableNoShowTag');
223
224 $eventBookings = [];
225
226 foreach ($bookings as $key => &$booking) {
227 ksort($booking['payments']);
228
229 if ($noShowTagEnabled) {
230 $customersNoShowCountIds[] = $booking['customer']['id'];
231 }
232
233 foreach ($booking['eventPeriods'] as &$period) {
234 $period['periodStart'] = DateTimeService::getCustomDateTimeFromUtc($period['periodStart']);
235 if ($providerTimeZoneSet) {
236 $period['periodStart'] =
237 DateTimeService::getCustomDateTimeObjectInTimeZone($period['periodStart'], $user->getTimeZone()->getValue())->format('Y-m-d H:i:s');
238 }
239 }
240
241 $persons = $booking['persons'];
242 if (!empty($booking['event']['customPricing']) && !empty($booking['ticketsData'])) {
243 /** @var CustomerBookingEventTicket $bookedTicket */
244 foreach ($booking['ticketsData'] as $bookedTicket) {
245 $persons += $bookedTicket['persons'];
246 }
247 }
248
249 if ($booking['tax']) {
250 $booking['tax'] = json_decode($booking['tax'], true);
251 }
252
253 $booking['ticketsData'] = !empty($booking['ticketsData']) ? $booking['ticketsData'] : [];
254
255 $eventBookings[] = [
256 'id' => $booking['id'],
257 'bookedSpots' => $persons,
258 'status' => $booking['event']['status'] === 'canceled' || $booking['event']['status'] === 'rejected' ? 'canceled' : $booking['status'],
259 'checked' => false,
260 'customer' => [
261 'id' => $booking['customer']['id'],
262 'firstName' => $booking['customer']['firstName'],
263 'lastName' => $booking['customer']['lastName'],
264 'phone' => $booking['customer']['phone'],
265 'email' => $booking['customer']['email'],
266 'note' => $booking['customer']['note']
267 ],
268 'code' => !empty($booking['token']) ? substr($booking['token'], 0, 5) : '',
269 'event' => [
270 'id' => $booking['event']['id'],
271 'name' => $booking['event']['name'],
272 'startDate' => explode(' ', array_values($booking['eventPeriods'])[0]['periodStart'])[0],
273 'startTime' => explode(' ', array_values($booking['eventPeriods'])[0]['periodStart'])[1],
274 'organizer' => !empty($booking['event']['organizer']) ? $booking['event']['organizer'] : null,
275 'staff' => !empty($booking['event']['providers']) ? array_values($booking['event']['providers']) : [],
276 'isZoom' => !empty(array_values($booking['eventPeriods'])[0]['zoomMeeting']),
277 'isGoogleMeet' => !empty(array_values($booking['eventPeriods'])[0]['googleMeetUrl']),
278 'isWaitingList' => $booking['event']['isWaitingList'],
279 ],
280 'persons' => $booking['persons'],
281 'customFields' => $booking['customFields'],
282 'ticketsData' => $booking['ticketsData'],
283 'tax' => $booking['tax'],
284 'price' => $booking['price'],
285 'aggregatedPrice' => $booking['aggregatedPrice'],
286 'coupon' => !empty($booking['coupon']) ? $booking['coupon'] : null,
287 'payment' => [
288 'status' => $paymentAS->getFullStatus($booking, BookableType::EVENT),
289 'total' => $paymentAS->calculateAppointmentPrice($booking, BookableType::EVENT),
290 ],
291 'payments' => array_values($booking['payments']),
292 ];
293 }
294
295
296 if ($noShowTagEnabled && !empty($customersNoShowCountIds)) {
297 /** @var CustomerBookingRepository $bookingRepository */
298 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
299
300 $customersNoShowCount = $bookingRepository->countByNoShowStatus($customersNoShowCountIds);
301
302 foreach ($eventBookings as &$eventBooking) {
303 if (!empty($customersNoShowCount[$eventBooking['customer']['id']])) {
304 $eventBooking['customer']['noShowCount'] = $customersNoShowCount[$eventBooking['customer']['id']]['count'];
305 }
306 }
307 }
308
309 $eventBookings = apply_filters('amelia_get_event_bookings_filter', $eventBookings);
310
311 do_action('amelia_get_event_bookings', $eventBookings);
312
313 $result->setResult(CommandResult::RESULT_SUCCESS);
314 $result->setMessage('Successfully retrieved event bookings');
315 $result->setData(
316 [
317 Entities::BOOKINGS => $eventBookings,
318 'totalCount' => sizeof($bookingRepository->getEventBookingIdsByCriteria()),
319 'filteredCount' => sizeof(
320 $bookingRepository->getEventBookingIdsByCriteria($params)
321 ),
322 'attendeeCount' => $attendeeCount,
323 'waitingCount' => $waitingCount,
324 'waitingCapacity' => $waitingCapacity,
325 'maxCapacity' => $maxCapacity,
326 ]
327 );
328
329 return $result;
330 }
331 }
332