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