PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.0.2
Booking for Appointments and Events Calendar – Amelia v2.0.2
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 6 months ago DeleteEventsCommand.php 6 months ago DeleteEventsCommandHandler.php 6 months ago GetCalendarEventsCommand.php 1 year ago GetCalendarEventsCommandHandler.php 1 year ago GetEventBookingCommand.php 6 months ago GetEventBookingCommandHandler.php 5 months ago GetEventBookingsCommand.php 1 year ago GetEventBookingsCommandHandler.php 6 months ago GetEventCommand.php 7 years ago GetEventCommandHandler.php 6 months ago GetEventDeleteEffectCommand.php 1 year ago GetEventDeleteEffectCommandHandler.php 6 months ago GetEventsCommand.php 1 year ago GetEventsCommandHandler.php 6 months ago UpdateEventBookingCommand.php 7 years ago UpdateEventBookingCommandHandler.php 6 months ago UpdateEventCommand.php 1 year ago UpdateEventCommandHandler.php 6 months ago UpdateEventStatusCommand.php 1 year ago UpdateEventStatusCommandHandler.php 6 months ago UpdateEventVisibilityCommand.php 6 months ago UpdateEventVisibilityCommandHandler.php 6 months ago
GetEventBookingsCommandHandler.php
382 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\ProviderApplicationService;
11 use AmeliaBooking\Application\Services\User\UserApplicationService;
12 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
13 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
14 use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking;
15 use AmeliaBooking\Domain\Entity\Booking\Event\CustomerBookingEventTicket;
16 use AmeliaBooking\Domain\Entity\Booking\Event\Event;
17 use AmeliaBooking\Domain\Entity\Booking\Event\EventTicket;
18 use AmeliaBooking\Domain\Entity\Entities;
19 use AmeliaBooking\Domain\Entity\User\AbstractUser;
20 use AmeliaBooking\Domain\Entity\User\Provider;
21 use AmeliaBooking\Domain\Factory\Booking\Event\EventFactory;
22 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
23 use AmeliaBooking\Domain\Services\Settings\SettingsService;
24 use AmeliaBooking\Domain\ValueObjects\String\BookableType;
25 use AmeliaBooking\Domain\ValueObjects\String\BookingStatus;
26 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
27 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
28 use Exception;
29
30 /**
31 * Class GetEventBookingsCommandHandler
32 *
33 * @package AmeliaBooking\Application\Commands\Booking\Event
34 */
35 class GetEventBookingsCommandHandler extends CommandHandler
36 {
37 /**
38 * @param GetEventBookingsCommand $command
39 *
40 * @return CommandResult
41 *
42 * @throws AccessDeniedException
43 * @throws InvalidArgumentException
44 * @throws QueryExecutionException
45 * @throws Exception
46 */
47 public function handle(GetEventBookingsCommand $command)
48 {
49 $result = new CommandResult();
50
51 /** @var SettingsService $settingsDS */
52 $settingsDS = $this->container->get('domain.settings.service');
53 /** @var UserApplicationService $userAS */
54 $userAS = $this->container->get('application.user.service');
55 /** @var PaymentApplicationService $paymentAS */
56 $paymentAS = $this->container->get('application.payment.service');
57 /** @var EventApplicationService $eventApplicationService */
58 $eventApplicationService = $this->container->get('application.booking.event.service');
59 /** @var CustomerBookingRepository $bookingRepository */
60 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
61 /** @var ProviderApplicationService $providerAS */
62 $providerAS = $this->container->get('application.user.provider.service');
63
64 $params = $command->getField('params');
65
66 if (isset($params['dates']) && empty($params['dates'][0]) && empty($params['dates'][1])) {
67 unset($params['dates']);
68 }
69
70 $isCabinetPage = $command->getPage() === 'cabinet';
71
72 try {
73 /** @var AbstractUser $user */
74 $user = $command->getUserApplicationService()->authorization(
75 $isCabinetPage ? $command->getToken() : null,
76 $command->getCabinetType()
77 );
78 } catch (AuthorizationException $e) {
79 $result->setResult(CommandResult::RESULT_ERROR);
80 $result->setData(
81 [
82 'reauthorize' => true
83 ]
84 );
85
86 return $result;
87 }
88
89 if ($user && $userAS->isAmeliaUser($user) && $userAS->isCustomer($user)) {
90 $params['customers'] = [$user->getId()->getValue()];
91 }
92
93 if ($user && $user->getType() === AbstractUser::USER_ROLE_PROVIDER) {
94 $params['providers'] = [$user->getId()->getValue()];
95 }
96
97 $providerTimeZoneSet = ($user instanceof Provider) && $user->getTimeZone() && $user->getTimeZone()->getValue();
98
99 if (!empty($params['dates'])) {
100 if (!empty($params['dates'][0])) {
101 $params['dates'][0] .= ' 00:00:00';
102 }
103 if (!empty($params['dates'][1])) {
104 $params['dates'][1] .= ' 23:59:59';
105 }
106 }
107
108 $attendeeCount = 0;
109
110 $waitingCount = 0;
111
112 $maxCapacity = 0;
113
114 $waitingCapacity = 0;
115
116 if ($isCabinetPage) {
117 /** @var Event $event */
118 $event = $eventApplicationService->getEventById(
119 (int)$params['events'][0],
120 [
121 'fetchEventsTickets' => true,
122 'fetchBookings' => true,
123 'fetchBookingsTickets' => true,
124 ]
125 );
126
127
128 if ($event->getCustomPricing()->getValue()) {
129 /** @var CustomerBooking $customerBooking */
130 foreach ($event->getBookings()->getItems() as $customerBooking) {
131 if (
132 $customerBooking->getStatus()->getValue() === BookingStatus::APPROVED ||
133 $customerBooking->getStatus()->getValue() === BookingStatus::PENDING
134 ) {
135 /** @var CustomerBookingEventTicket $bookingToEventTicket */
136 foreach ($customerBooking->getTicketsBooking()->getItems() as $bookingToEventTicket) {
137 $attendeeCount += $bookingToEventTicket->getPersons()->getValue();
138 }
139 }
140
141 if ($customerBooking->getStatus()->getValue() === BookingStatus::WAITING) {
142 /** @var CustomerBookingEventTicket $bookingToEventTicket */
143 foreach ($customerBooking->getTicketsBooking()->getItems() as $bookingToEventTicket) {
144 $waitingCount += $bookingToEventTicket->getPersons()->getValue();
145 }
146 }
147 }
148
149 /** @var EventTicket $ticket */
150 foreach ($event->getCustomTickets()->getItems() as $ticket) {
151 $maxCapacity += $ticket->getSpots()->getValue();
152 }
153 } else {
154 $maxCapacity = $event->getMaxCapacity()->getValue();
155
156 /** @var CustomerBooking $customerBooking */
157 foreach ($event->getBookings()->getItems() as $customerBooking) {
158 if (
159 $customerBooking->getStatus()->getValue() === BookingStatus::APPROVED ||
160 $customerBooking->getStatus()->getValue() === BookingStatus::PENDING
161 ) {
162 $attendeeCount += $customerBooking->getPersons()->getValue();
163 }
164 }
165
166 /** @var CustomerBooking $customerBooking */
167 foreach ($event->getBookings()->getItems() as $customerBooking) {
168 if ($customerBooking->getStatus()->getValue() === BookingStatus::WAITING) {
169 $waitingCount += $customerBooking->getPersons()->getValue();
170 }
171 }
172 }
173
174 $eventSettings = $settingsDS->isFeatureEnabled('waitingList') && $event->getSettings() && $event->getSettings()->getValue()
175 ? json_decode($event->getSettings()->getValue(), true)
176 : null;
177
178
179 if ($eventSettings && !empty($eventSettings['waitingList']['enabled'])) {
180 if ($event->getCustomPricing()->getValue()) {
181 /** @var EventTicket $ticket */
182 foreach ($event->getCustomTickets()->getItems() as $ticket) {
183 $waitingCapacity += $ticket->getWaitingListSpots()->getValue();
184 }
185 } else {
186 $waitingCapacity = $eventSettings['waitingList']['maxCapacity'];
187 }
188 }
189 }
190
191 $bookingIds = $bookingRepository->getEventBookingIdsByCriteria($params, !empty($params['limit']) ? $params['limit'] : 10);
192
193 if (!$bookingIds && $params['page'] && (int)$params['page'] > 1) {
194 $params['page'] = 1;
195
196 $bookingIds = $bookingRepository->getEventBookingIdsByCriteria($params, !empty($params['limit']) ? $params['limit'] : 10);
197 }
198
199 if (empty($bookingIds)) {
200 $result->setResult(CommandResult::RESULT_SUCCESS);
201 $result->setMessage('Successfully retrieved event bookings');
202 $result->setData(
203 [
204 Entities::BOOKINGS => [],
205 'totalCount' => sizeof($bookingRepository->getEventBookingIdsByCriteria()),
206 'filteredCount' => 0,
207 'attendeeCount' => 0,
208 'waitingCount' => 0,
209 'waitingCapacity' => $waitingCapacity,
210 'maxCapacity' => $maxCapacity,
211 ]
212 );
213
214 return $result;
215 }
216
217 $bookings = $bookingRepository->getEventBookingsByIds(
218 $bookingIds,
219 array_merge(
220 !empty($params['dates']) ? ['dates' => $params['dates']] : [],
221 [
222 'fetchBookingsPayments' => true,
223 'fetchBookingsCoupons' => true,
224 'fetchProviders' => true,
225 'fetchCustomers' => true,
226 'fetchEvent' => true,
227 ]
228 )
229 );
230
231
232 $customersNoShowCountIds = [];
233
234 $noShowTagEnabled = $settingsDS->isFeatureEnabled('noShowTag');
235
236 $eventBookings = [];
237
238 foreach ($bookings as &$booking) {
239 ksort($booking['payments']);
240
241 if ($noShowTagEnabled) {
242 $customersNoShowCountIds[] = $booking['customer']['id'];
243 }
244
245 foreach ($booking['event']['periods'] as &$period) {
246 $period['periodStart'] = DateTimeService::getCustomDateTimeFromUtc($period['periodStart']);
247 $period['periodEnd'] = DateTimeService::getCustomDateTimeFromUtc($period['periodEnd']);
248 if ($providerTimeZoneSet) {
249 $period['periodStart'] =
250 DateTimeService::getCustomDateTimeObjectInTimeZone($period['periodStart'], $user->getTimeZone()->getValue())->format('Y-m-d H:i:s');
251 $period['periodEnd'] =
252 DateTimeService::getCustomDateTimeObjectInTimeZone($period['periodEnd'], $user->getTimeZone()->getValue())->format('Y-m-d H:i:s');
253 }
254 }
255
256 $persons = $booking['persons'];
257 if (!empty($booking['event']['customPricing']) && !empty($booking['ticketsData'])) {
258 /** @var CustomerBookingEventTicket $bookedTicket */
259 foreach ($booking['ticketsData'] as $bookedTicket) {
260 $persons += $bookedTicket['persons'];
261 }
262 }
263
264 if ($booking['tax']) {
265 $booking['tax'] = json_decode($booking['tax'], true);
266 }
267
268 $booking['ticketsData'] = !empty($booking['ticketsData']) ? $booking['ticketsData'] : [];
269
270 if (!empty($booking['event']['providers'])) {
271 foreach ($booking['event']['providers'] as &$provider) {
272 if (!empty($provider['badgeId'])) {
273 $provider['badge'] = $providerAS->getBadge($provider['badgeId']);
274 }
275 }
276 }
277
278 if (!empty($booking['event']['organizer']) && !empty($booking['event']['organizer']['badgeId'])) {
279 $booking['event']['organizer']['badge'] = $providerAS->getBadge($booking['event']['organizer']['badgeId']);
280 }
281
282 $wcTax = 0;
283 $wcDiscount = 0;
284
285 foreach ($booking['payments'] as $payment) {
286 $paymentAS->addWcFields($payment);
287
288 $wcTax += !empty($payment['wcItemTaxValue']) ? $payment['wcItemTaxValue'] : 0;
289
290 $wcDiscount += !empty($payment['wcItemCouponValue']) ? $payment['wcItemCouponValue'] : 0;
291 }
292
293 $eventBooking = [
294 'id' => $booking['id'],
295 'bookedSpots' => $persons,
296 'status' => $booking['event']['status'] === 'canceled' || $booking['event']['status'] === 'rejected' ? 'canceled' : $booking['status'],
297 'persons' => $persons,
298 'checked' => false,
299 'customer' => [
300 'id' => $booking['customer']['id'],
301 'firstName' => $booking['customer']['firstName'],
302 'lastName' => $booking['customer']['lastName'],
303 'phone' => $booking['customer']['phone'],
304 'email' => $booking['customer']['email'],
305 'note' => $booking['customer']['note']
306 ],
307 'code' => !empty($booking['token']) ? substr($booking['token'], 0, 5) : '',
308 'event' => [
309 'id' => (int)$booking['event']['id'],
310 'name' => $booking['event']['name'],
311 'startDate' => explode(' ', array_values($booking['event']['periods'])[0]['periodStart'])[0],
312 'endDate' => explode(' ', array_values($booking['event']['periods'])[sizeof($booking['event']['periods']) - 1]['periodEnd'])[0],
313 'startTime' => explode(' ', array_values($booking['event']['periods'])[0]['periodStart'])[1],
314 'organizer' => !empty($booking['event']['organizer']) ? $booking['event']['organizer'] : null,
315 'staff' => !empty($booking['event']['providers']) ? array_values($booking['event']['providers']) : [],
316 'isZoom' => !empty(array_values($booking['event']['periods'])[0]['zoomMeeting']),
317 'isGoogleMeet' => !empty(array_values($booking['event']['periods'])[0]['googleMeetUrl']),
318 'isMicrosoftTeams' => !empty(array_values($booking['event']['periods'])[0]['microsoftTeamsUrl']),
319 'isLessonSpace' => !empty(array_values($booking['event']['periods'])[0]['lessonSpace']),
320 'isWaitingList' => !empty($booking['event']['settings']) ?
321 json_decode($booking['event']['settings'], true)['waitingList']['enabled'] :
322 false,
323 ],
324 'ticketsData' => $booking['ticketsData'],
325 'tax' => $booking['tax'],
326 'price' => $booking['price'],
327 'aggregatedPrice' => $booking['aggregatedPrice'],
328 'coupon' => !empty($booking['coupon']) ? $booking['coupon'] : null,
329 'payment' => [
330 'status' => $paymentAS->getFullStatus($booking, BookableType::EVENT),
331 'total' => $paymentAS->calculateAppointmentPrice($booking, BookableType::EVENT) + $wcTax - $wcDiscount,
332 ],
333 'payments' => array_values($booking['payments']),
334 'qrCodes' => !empty($booking['qrCodes']) ? $booking['qrCodes'] : null,
335 'cancelable' => $eventApplicationService->isCancelable(EventFactory::create($booking['event']), $user),
336 ];
337
338 if ($isCabinetPage) {
339 $eventBooking['customFields'] = $booking['customFields'];
340 }
341
342 $eventBookings[] = $eventBooking;
343 }
344
345
346 if ($noShowTagEnabled && !empty($customersNoShowCountIds)) {
347 /** @var CustomerBookingRepository $bookingRepository */
348 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
349
350 $customersNoShowCount = $bookingRepository->countByNoShowStatus($customersNoShowCountIds);
351
352 foreach ($eventBookings as &$eventBooking) {
353 if (!empty($customersNoShowCount[$eventBooking['customer']['id']])) {
354 $eventBooking['customer']['noShowCount'] = $customersNoShowCount[$eventBooking['customer']['id']]['count'];
355 }
356 }
357 }
358
359 $eventBookings = apply_filters('amelia_get_event_bookings_filter', $eventBookings);
360
361 do_action('amelia_get_event_bookings', $eventBookings);
362
363 $result->setResult(CommandResult::RESULT_SUCCESS);
364 $result->setMessage('Successfully retrieved event bookings');
365 $result->setData(
366 [
367 Entities::BOOKINGS => $eventBookings,
368 'totalCount' => sizeof($bookingRepository->getEventBookingIdsByCriteria()),
369 'filteredCount' => sizeof(
370 $bookingRepository->getEventBookingIdsByCriteria($params)
371 ),
372 'attendeeCount' => $attendeeCount,
373 'waitingCount' => $waitingCount,
374 'waitingCapacity' => $waitingCapacity,
375 'maxCapacity' => $maxCapacity,
376 ]
377 );
378
379 return $result;
380 }
381 }
382