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
GetEventCommandHandler.php
325 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\Reservation\EventReservationService; |
| 12 | use AmeliaBooking\Application\Services\User\CustomerApplicationService; |
| 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\EventPeriod; |
| 20 | use AmeliaBooking\Domain\Entity\Booking\Event\EventTicket; |
| 21 | use AmeliaBooking\Domain\Entity\Entities; |
| 22 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 23 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\IntegerValue; |
| 24 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 25 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 26 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 27 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 28 | use AmeliaBooking\Infrastructure\Repository\CustomField\CustomFieldRepository; |
| 29 | use Exception; |
| 30 | use Slim\Exception\ContainerValueNotFoundException; |
| 31 | |
| 32 | /** |
| 33 | * Class GetEventCommandHandler |
| 34 | * |
| 35 | * @package AmeliaBooking\Application\Commands\Booking\Event |
| 36 | */ |
| 37 | class GetEventCommandHandler extends CommandHandler |
| 38 | { |
| 39 | /** |
| 40 | * @param GetEventCommand $command |
| 41 | * |
| 42 | * @return CommandResult |
| 43 | * @throws ContainerValueNotFoundException |
| 44 | * @throws AccessDeniedException |
| 45 | * @throws QueryExecutionException |
| 46 | * @throws InvalidArgumentException |
| 47 | * @throws Exception |
| 48 | */ |
| 49 | public function handle(GetEventCommand $command) |
| 50 | { |
| 51 | $result = new CommandResult(); |
| 52 | |
| 53 | try { |
| 54 | /** @var AbstractUser $user */ |
| 55 | $user = $command->getUserApplicationService()->authorization( |
| 56 | $command->getPage() === 'cabinet' ? $command->getToken() : null, |
| 57 | $command->getCabinetType() |
| 58 | ); |
| 59 | } catch (AuthorizationException $e) { |
| 60 | $result->setResult(CommandResult::RESULT_ERROR); |
| 61 | $result->setData( |
| 62 | [ |
| 63 | 'reauthorize' => true |
| 64 | ] |
| 65 | ); |
| 66 | |
| 67 | return $result; |
| 68 | } |
| 69 | |
| 70 | if ($user === null) { |
| 71 | throw new AccessDeniedException('You are not allowed to read events'); |
| 72 | } |
| 73 | |
| 74 | /** @var EventApplicationService $eventApplicationService */ |
| 75 | $eventApplicationService = $this->container->get('application.booking.event.service'); |
| 76 | /** @var EventRepository $eventRepository */ |
| 77 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 78 | /** @var PaymentApplicationService $paymentAS */ |
| 79 | $paymentAS = $this->container->get('application.payment.service'); |
| 80 | /** @var AbstractCustomFieldApplicationService $customFieldService */ |
| 81 | $customFieldService = $this->container->get('application.customField.service'); |
| 82 | /** @var EventReservationService $reservationService */ |
| 83 | $reservationService = $this->container->get('application.reservation.service')->get(Entities::EVENT); |
| 84 | /** @var CustomerBookingRepository $bookingRepository */ |
| 85 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 86 | /** @var CustomFieldRepository $customFieldRepository */ |
| 87 | $customFieldRepository = $this->container->get('domain.customField.repository'); |
| 88 | |
| 89 | |
| 90 | /** @var Event $event */ |
| 91 | $event = $eventApplicationService->getEventById( |
| 92 | (int)$command->getField('id'), |
| 93 | [ |
| 94 | 'fetchEventsPeriods' => true, |
| 95 | 'fetchEventsTickets' => true, |
| 96 | 'fetchEventsTags' => empty($command->getFields()['params']['drawer']), |
| 97 | 'fetchEventsProviders' => empty($command->getFields()['params']['drawer']), |
| 98 | 'fetchEventsImages' => true, |
| 99 | 'fetchBookings' => true, |
| 100 | 'fetchBookingsTickets' => true, |
| 101 | 'fetchBookingsUsers' => true, |
| 102 | 'fetchBookingsPayments' => true, |
| 103 | 'fetchBookingsCoupons' => true, |
| 104 | 'fetchEventsOrganizer' => true, |
| 105 | 'fetchEventsLocation' => true, |
| 106 | ] |
| 107 | ); |
| 108 | |
| 109 | if (!$event) { |
| 110 | $result->setResult(CommandResult::RESULT_ERROR); |
| 111 | $result->setMessage('Could not retrieve event'); |
| 112 | $result->setData( |
| 113 | [ |
| 114 | Entities::EVENT => [] |
| 115 | ] |
| 116 | ); |
| 117 | |
| 118 | return $result; |
| 119 | } |
| 120 | |
| 121 | /** @var Collection $customFieldsCollection */ |
| 122 | $customFieldsCollection = $customFieldRepository->getAll([], false); |
| 123 | |
| 124 | // set tickets price by dateRange |
| 125 | if ($event->getCustomTickets()->getItems()) { |
| 126 | $event->setCustomTickets($eventApplicationService->getTicketsPriceByDateRange($event->getCustomTickets())); |
| 127 | } |
| 128 | |
| 129 | if (!empty($command->getField('params')['timeZone'])) { |
| 130 | /** @var EventPeriod $period */ |
| 131 | foreach ($event->getPeriods()->getItems() as $period) { |
| 132 | $period->getPeriodStart()->getValue()->setTimezone( |
| 133 | new \DateTimeZone($command->getField('params')['timeZone']) |
| 134 | ); |
| 135 | |
| 136 | $period->getPeriodEnd()->getValue()->setTimezone( |
| 137 | new \DateTimeZone($command->getField('params')['timeZone']) |
| 138 | ); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** @var CustomerApplicationService $customerAS */ |
| 143 | $customerAS = $this->container->get('application.user.customer.service'); |
| 144 | |
| 145 | $customerAS->removeBookingsForOtherCustomers($user, new Collection([$event])); |
| 146 | |
| 147 | $eventInfo = $eventApplicationService->getEventInfo($event); |
| 148 | |
| 149 | $eventStartDateTime = array_values($event->getPeriods()->toArray())[0]['periodStart']; |
| 150 | |
| 151 | $eventEndDateTime = array_values($event->getPeriods()->toArray())[$event->getPeriods()->length() - 1]['periodEnd']; |
| 152 | |
| 153 | $recurringEvents = []; |
| 154 | if ($event->getRecurring()) { |
| 155 | $recurringEvents = $eventRepository->getFilteredIds( |
| 156 | [ |
| 157 | 'parentId' => $event->getParentId() ? $event->getParentId()->getValue() : $event->getId()->getValue(), |
| 158 | 'dates' => [$eventStartDateTime] |
| 159 | ], |
| 160 | null |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | $eventBookings = []; |
| 165 | $bookingsPrice = 0; |
| 166 | $paidPrice = 0; |
| 167 | |
| 168 | /** @var CustomerBooking $booking */ |
| 169 | foreach ($event->getBookings()->getItems() as $booking) { |
| 170 | $customFields = []; |
| 171 | $bookingPrice = $paymentAS->calculateAppointmentPrice($booking->toArray(), 'event'); |
| 172 | $bookingsPrice += $bookingPrice; |
| 173 | $ticketsData = []; |
| 174 | |
| 175 | $persons = $booking->getPersons()->getValue(); |
| 176 | |
| 177 | /** @var CustomerBookingEventTicket $ticket */ |
| 178 | foreach ($booking->getTicketsBooking()->getItems() as $ticket) { |
| 179 | $persons += $ticket->getPersons()->getValue(); |
| 180 | |
| 181 | /** @var EventTicket $eventTicket */ |
| 182 | $eventTicket = $event->getCustomTickets()->keyExists($ticket->getEventTicketId()->getValue()) ? |
| 183 | $event->getCustomTickets()->getItem($ticket->getEventTicketId()->getValue()) : |
| 184 | null; |
| 185 | |
| 186 | $ticketsData[] = [ |
| 187 | 'id' => $ticket->getId()->getValue(), |
| 188 | 'eventTicketId' => $eventTicket ? $eventTicket->getId()->getValue() : null, |
| 189 | 'name' => $eventTicket ? $eventTicket->getName()->getValue() : null, |
| 190 | 'price' => $ticket->getPrice()->getValue(), |
| 191 | 'quantity' => $ticket->getPersons()->getValue() |
| 192 | ]; |
| 193 | } |
| 194 | $booking->setPersons(new IntegerValue($persons)); |
| 195 | |
| 196 | $bookingPaymentAmount = $reservationService->getPaymentAmount($booking, $event); |
| 197 | |
| 198 | $bookingPaidPrice = 0; |
| 199 | foreach ($booking->getPayments()->toArray() as $payment) { |
| 200 | if ($payment['status'] === 'paid' || $payment['status'] === 'partiallyPaid') { |
| 201 | $bookingPaidPrice += $payment['amount']; |
| 202 | } |
| 203 | } |
| 204 | $paidPrice += $bookingPaidPrice; |
| 205 | |
| 206 | $noShowCount = $bookingRepository->countByNoShowStatus([$booking->getCustomerId()->getValue()]); |
| 207 | if ($noShowCount && !empty($noShowCount[$booking->getCustomerId()->getValue()])) { |
| 208 | $noShowCount = $noShowCount[$booking->getCustomerId()->getValue()]['count']; |
| 209 | } |
| 210 | |
| 211 | $customFields = $customFieldService->reformatCustomField($booking, $customFields, $customFieldsCollection); |
| 212 | |
| 213 | $eventBookings[] = [ |
| 214 | 'persons' => $persons, |
| 215 | 'customer' => [ |
| 216 | 'id' => $booking->getCustomerId()->getValue(), |
| 217 | 'firstName' => $booking->getCustomer()->getFirstName()->getValue(), |
| 218 | 'lastName' => $booking->getCustomer()->getLastName() ? $booking->getCustomer()->getLastName()->getValue() : null, |
| 219 | 'email' => $booking->getCustomer()->getEmail() ? $booking->getCustomer()->getEmail()->getValue() : null, |
| 220 | 'noShowCount' => $noShowCount, |
| 221 | 'note' => $booking->getCustomer()->getNote() ? $booking->getCustomer()->getNote()->getValue() : null, |
| 222 | ], |
| 223 | 'tickets' => $ticketsData, |
| 224 | 'status' => in_array($event->getStatus()->getValue(), [BookingStatus::CANCELED, BookingStatus::REJECTED]) ? |
| 225 | 'canceled' : |
| 226 | $booking->getStatus()->getValue(), |
| 227 | 'id' => $booking->getId()->getValue(), |
| 228 | 'customFields' => $customFields, |
| 229 | 'payment' => [ |
| 230 | 'paymentMethods' => array_map( |
| 231 | function ($payment) { |
| 232 | return $payment['gateway']; |
| 233 | }, |
| 234 | $booking->getPayments()->toArray() |
| 235 | ), |
| 236 | 'status' => $paymentAS->getFullStatus($booking->toArray(), 'appointment'), |
| 237 | 'total' => $bookingPrice, |
| 238 | 'discount' => $bookingPaymentAmount['full_discount'], |
| 239 | 'eventPrice' => $bookingPaymentAmount['price'], |
| 240 | 'subtotal' => $bookingPaymentAmount['price'], |
| 241 | 'paid' => $bookingPaidPrice, |
| 242 | 'due' => max($bookingPrice - $bookingPaidPrice, 0), |
| 243 | ], |
| 244 | 'qrCodes' => $booking->getQrCodes() ? $booking->getQrCodes()->getValue() : null, |
| 245 | ]; |
| 246 | } |
| 247 | |
| 248 | $allEventFields = $event->toArray(); |
| 249 | |
| 250 | usort( |
| 251 | $allEventFields['gallery'], |
| 252 | function ($picture1, $picture2) { |
| 253 | return $picture1['position'] <=> $picture2['position']; |
| 254 | } |
| 255 | ); |
| 256 | |
| 257 | $firstGalleryImage = !empty($allEventFields['gallery']) ? $allEventFields['gallery'][0]['pictureThumbPath'] : null; |
| 258 | |
| 259 | $eventArray = array_merge( |
| 260 | [ |
| 261 | 'id' => $event->getId()->getValue(), |
| 262 | 'name' => $event->getName()->getValue(), |
| 263 | 'bookings' => $eventBookings, |
| 264 | 'periods' => $event->getPeriods()->toArray(), |
| 265 | 'color' => $event->getColor() ? $event->getColor()->getValue() : null, |
| 266 | 'customTickets' => $event->getCustomPricing() && $event->getCustomPricing()->getValue() ? $event->getCustomTickets()->toArray() : null, |
| 267 | 'organizer' => $event->getOrganizerId() && $event->getOrganizer() ? $event->getOrganizer()->toArray() : null, |
| 268 | 'price' => $event->getPrice() ? $event->getPrice()->getValue() : null, |
| 269 | 'show' => $event->getShow() ? $event->getShow()->getValue() : true, |
| 270 | 'pictureThumbPath' => $event->getPicture() ? $event->getPicture()->getThumbPath() : $firstGalleryImage, |
| 271 | 'maxCapacity' => $event->getMaxCapacity() ? $event->getMaxCapacity()->getValue() : null, |
| 272 | 'recurringCount' => sizeof($recurringEvents) > 0 ? (sizeof($recurringEvents) - 1) : 0, |
| 273 | 'totalPrice' => $bookingsPrice, |
| 274 | 'paidPrice' => $paidPrice, |
| 275 | 'startDate' => explode(' ', $eventStartDateTime)[0], |
| 276 | 'startTime' => explode(' ', $eventStartDateTime)[1], |
| 277 | 'endDate' => explode(' ', $eventEndDateTime)[0], |
| 278 | 'location' => $event->getCustomLocation() ? |
| 279 | ['name' => $event->getCustomLocation()->getValue()] : |
| 280 | ($event->getLocationId() ? $event->getLocation()->toArray() : null), |
| 281 | ], |
| 282 | $eventInfo |
| 283 | ); |
| 284 | |
| 285 | $eventArray['staff'] = array_map( |
| 286 | function ($provider) { |
| 287 | return [ |
| 288 | 'id' => $provider['id'], |
| 289 | 'firstName' => $provider['firstName'], |
| 290 | 'lastName' => $provider['lastName'], |
| 291 | 'picture' => $provider['pictureThumbPath'] |
| 292 | ]; |
| 293 | }, |
| 294 | $event->getProviders()->toArray() |
| 295 | ); |
| 296 | |
| 297 | $eventArray['organizer'] = $event->getOrganizerId() && $event->getOrganizer() ? [ |
| 298 | 'id' => $eventArray['organizer']['id'], |
| 299 | 'firstName' => $eventArray['organizer']['firstName'], |
| 300 | 'lastName' => $eventArray['organizer']['lastName'], |
| 301 | 'picture' => $eventArray['organizer']['pictureThumbPath'] |
| 302 | ] : null; |
| 303 | |
| 304 | |
| 305 | $eventArray = apply_filters('amelia_get_event_filter', $eventArray); |
| 306 | |
| 307 | do_action('amelia_get_event', $eventArray); |
| 308 | |
| 309 | |
| 310 | |
| 311 | // TODO: Redesign - remove 'drawer' condition, used for old design compatibility |
| 312 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 313 | $result->setMessage('Successfully retrieved event'); |
| 314 | $result->setData( |
| 315 | [ |
| 316 | Entities::EVENT => !empty($command->getFields()['params']['drawer']) |
| 317 | ? $eventArray |
| 318 | : $allEventFields, |
| 319 | ] |
| 320 | ); |
| 321 | |
| 322 | return $result; |
| 323 | } |
| 324 | } |
| 325 |