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