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