PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.3
Booking for Appointments and Events Calendar – Amelia v2.3
2.4.5 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 / GetEventBookingCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Event Last commit date
Tag 2 months ago AddEventCommand.php 1 year ago AddEventCommandHandler.php 1 year 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 3 months 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 3 months 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
GetEventBookingCommandHandler.php
338 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\Domain\Collection\Collection;
13 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
14 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
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\Appointment\CustomerBookingFactory;
22 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
23 use AmeliaBooking\Domain\Services\Settings\SettingsService;
24 use AmeliaBooking\Domain\ValueObjects\String\BookableType;
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
31 /**
32 * Class GetEventBookingCommandHandler
33 *
34 * @package AmeliaBooking\Application\Commands\Booking\Event
35 */
36 class GetEventBookingCommandHandler extends CommandHandler
37 {
38 /**
39 * @param GetEventBookingCommand $command
40 *
41 * @return CommandResult
42 *
43 * @throws AccessDeniedException
44 * @throws InvalidArgumentException
45 * @throws QueryExecutionException
46 * @throws Exception
47 */
48 public function handle(GetEventBookingCommand $command)
49 {
50 $result = new CommandResult();
51
52 /** @var SettingsService $settingsDS */
53 $settingsDS = $this->container->get('domain.settings.service');
54 /** @var PaymentApplicationService $paymentAS */
55 $paymentAS = $this->container->get('application.payment.service');
56 /** @var CustomerBookingRepository $bookingRepository */
57 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
58 /** @var EventApplicationService $eventApplicationService */
59 $eventApplicationService = $this->container->get('application.booking.event.service');
60 /** @var EventRepository $eventRepository */
61 $eventRepository = $this->container->get('domain.booking.event.repository');
62 /** @var AbstractCustomFieldApplicationService $customFieldService */
63 $customFieldService = $this->container->get('application.customField.service');
64 /** @var EventReservationService $reservationService */
65 $reservationService = $this->container->get('application.reservation.service')->get(Entities::EVENT);
66 /** @var CustomFieldRepository $customFieldRepository */
67 $customFieldRepository = $this->container->get('domain.customField.repository');
68
69 try {
70 /** @var AbstractUser $user */
71 $user = $command->getUserApplicationService()->authorization(
72 null,
73 $command->getCabinetType()
74 );
75 } catch (AuthorizationException $e) {
76 $result->setResult(CommandResult::RESULT_ERROR);
77 $result->setData(
78 [
79 'reauthorize' => true
80 ]
81 );
82
83 return $result;
84 }
85
86 $providerTimeZoneSet = $user && $user instanceof Provider && $user->getTimeZone() && $user->getTimeZone()->getValue();
87
88 $bookings = $bookingRepository->getEventBookingsByIds(
89 [$command->getArg('id')],
90 array_merge(
91 [
92 'fetchBookingsPayments' => true,
93 'fetchBookingsCoupons' => true,
94 'fetchCustomers' => true
95 ]
96 )
97 );
98
99 $eventId = $command->getField('params')['eventId'];
100
101 /** @var Event $event */
102 $event = $eventApplicationService->getEventById(
103 $eventId,
104 [
105 'fetchEventsPeriods' => true,
106 'fetchEventsTickets' => true,
107 'fetchEventsTags' => false,
108 'fetchEventsProviders' => false,
109 'fetchEventsImages' => true,
110 'fetchBookings' => true,
111 'fetchBookingsTickets' => true,
112 'fetchBookingsUsers' => true,
113 'fetchBookingsPayments' => true,
114 'fetchBookingsCoupons' => true,
115 'fetchEventsOrganizer' => true,
116 'fetchEventsLocation' => true,
117 ]
118 );
119
120 if (empty($bookings) || !$event) {
121 $result->setResult(CommandResult::RESULT_ERROR);
122 $result->setMessage('Could not retrieve event booking');
123 $result->setData(
124 [
125 Entities::BOOKING => []
126 ]
127 );
128
129 return $result;
130 }
131
132 /** @var Collection $customFieldsCollection */
133 $customFieldsCollection = $customFieldRepository->getAll([], false);
134
135 $booking = array_values($bookings)[0];
136
137 if ($user && $user->getType() === Entities::CUSTOMER && $booking['customerId'] !== $user->getId()->getValue()) {
138 throw new AccessDeniedException('You are not allowed to read event booking');
139 }
140
141 $customersNoShowCountIds = [];
142
143 $noShowTagEnabled = $settingsDS->isFeatureEnabled('noShowTag');
144
145 if ($noShowTagEnabled) {
146 $customersNoShowCountIds[] = $booking['customer']['id'];
147 }
148
149 $eventInfo = $eventApplicationService->getEventInfo($event);
150
151 $eventArray = $event->toArray();
152
153 foreach ($eventArray['periods'] as &$period) {
154 $period['periodStart'] = DateTimeService::getCustomDateTimeFromUtc($period['periodStart']);
155 $period['periodEnd'] = DateTimeService::getCustomDateTimeFromUtc($period['periodEnd']);
156 if ($providerTimeZoneSet) {
157 $period['periodStart'] =
158 DateTimeService::getCustomDateTimeObjectInTimeZone($period['periodStart'], $user->getTimeZone()->getValue())->format('Y-m-d H:i:s');
159 $period['periodEnd'] =
160 DateTimeService::getCustomDateTimeObjectInTimeZone($period['periodEnd'], $user->getTimeZone()->getValue())->format('Y-m-d H:i:s');
161 }
162 }
163
164 $eventStartDateTime = array_values($event->getPeriods()->toArray())[0]['periodStart'];
165
166 $eventEndDateTime = array_values($event->getPeriods()->toArray())[$event->getPeriods()->length() - 1]['periodEnd'];
167
168 $recurringEvents = [];
169 if ($event->getRecurring()) {
170 $recurringEvents =
171 $eventRepository->getFilteredIds(['parentId' => $event->getParentId() ?
172 $event->getParentId()->getValue() :
173 $event->getId()->getValue(), 'dates' => [$eventStartDateTime]], null);
174 }
175
176 usort(
177 $eventArray['gallery'],
178 function ($picture1, $picture2) {
179 return $picture1['position'] <=> $picture2['position'];
180 }
181 );
182
183 $eventArray = array_merge(
184 $eventInfo,
185 [
186 'id' => $eventArray['id'],
187 'name' => $eventArray['name'],
188 'show' => $eventArray['show'],
189 'pictureThumbPath' => !empty($eventArray['gallery'][0]) ? $eventArray['gallery'][0]['pictureThumbPath'] : null,
190 'customPricing' => $eventArray['customPricing'],
191 'startDate' => explode(' ', $eventStartDateTime)[0],
192 'endDate' => explode(' ', $eventEndDateTime)[0],
193 'isZoom' => !empty($eventArray['periods'][0]['zoomMeeting']),
194 'isGoogleMeet' => !empty($eventArray['periods'][0]['googleMeetUrl']),
195 'isWaitingList' => !empty($eventArray['settings']) ? json_decode($eventArray['settings'], true)['waitingList']['enabled'] : false,
196 'recurringCount' => sizeof($recurringEvents) > 0 ? (sizeof($recurringEvents) - 1) : 0,
197 'location' =>
198 $event->getCustomLocation() ?
199 ['name' => $event->getCustomLocation()->getValue()] : ($event->getLocationId() ? $event->getLocation()->toArray() : null),
200 'periods' => array_map(function ($period) {
201 return [
202 'periodStart' => $period['periodStart'],
203 'periodEnd' => $period['periodEnd']
204 ];
205 }, $event->getPeriods()->toArray()),
206 ]
207 );
208
209 $eventArray['organizer'] = $event->getOrganizerId() && $event->getOrganizer() ? [
210 'id' => $event->getOrganizerId(),
211 'firstName' => $event->getOrganizer()->getFirstName()->getValue(),
212 'lastName' => $event->getOrganizer()->getLastName() ? $event->getOrganizer()->getLastName()->getValue() : null,
213 'picture' => $event->getOrganizer()->getPicture() ? $event->getOrganizer()->getPicture()->getThumbPath() : null,
214 ] : null;
215
216
217 $persons = $booking['persons'];
218 if (!empty($eventArray['customPricing']) && !empty($booking['ticketsData'])) {
219 /** @var CustomerBookingEventTicket $bookedTicket */
220 foreach ($booking['ticketsData'] as $bookedTicket) {
221 $persons += $bookedTicket['persons'];
222 }
223 }
224
225 $ticketsData = [];
226
227 if (!empty($booking['ticketsData'])) {
228 foreach ($booking['ticketsData'] as $ticket) {
229 /** @var EventTicket $eventTicket */
230 $eventTicket =
231 $event->getCustomTickets()->keyExists($ticket['eventTicketId']) ? $event->getCustomTickets()->getItem($ticket['eventTicketId']) : null;
232
233 $ticketsData[] = [
234 'id' => $ticket['id'],
235 'eventTicketId' => $eventTicket ? $eventTicket->getId()->getValue() : null,
236 'name' => $eventTicket ? $eventTicket->getName()->getValue() : null,
237 'price' => $ticket['price'],
238 'quantity' => $ticket['persons']
239 ];
240 }
241 }
242
243 $bookingPaymentAmount = $reservationService->getPaymentAmount(CustomerBookingFactory::create($booking), $event, true);
244
245 $wcTax = 0;
246 $wcDiscount = 0;
247
248 $bookingPaidPrice = 0;
249 $paymentMethods = [];
250 $wcOrderUrls = [];
251 foreach ($booking['payments'] as $payment) {
252 $paymentMethods[] = $payment['gateway'];
253
254 if ($payment['status'] === 'paid' || $payment['status'] === 'partiallyPaid') {
255 $bookingPaidPrice += $payment['amount'];
256 }
257
258 $paymentAS->addWcFields($payment);
259
260 $wcTax += !empty($payment['wcItemTaxValue']) ? $payment['wcItemTaxValue'] : 0;
261
262 $wcDiscount += !empty($payment['wcItemCouponValue']) ? $payment['wcItemCouponValue'] : 0;
263
264 if (!empty($payment['wcOrderId'])) {
265 $wcOrderUrls[$payment['wcOrderId']] = $payment['wcOrderUrl'];
266 }
267 }
268
269 $total = $bookingPaymentAmount['subtotal']
270 + $bookingPaymentAmount['total_tax']
271 + $wcTax
272 - $bookingPaymentAmount['discount']
273 - $bookingPaymentAmount['deduction']
274 - $wcDiscount;
275
276 $eventBooking = [
277 'id' => $booking['id'],
278 'status' => $eventArray['status'] === 'canceled' || $eventArray['status'] === 'rejected' ? 'canceled' : $booking['status'],
279 'persons' => $persons,
280 'checked' => false,
281 'tickets' => $ticketsData,
282 'customer' => [
283 'id' => $booking['customer']['id'],
284 'firstName' => $booking['customer']['firstName'],
285 'lastName' => $booking['customer']['lastName'],
286 'note' => $booking['customer']['note']
287 ],
288 'code' => !empty($booking['token']) ? substr($booking['token'], 0, 5) : '',
289 'event' => $eventArray,
290 'payment' => [
291 'paymentMethods' => $paymentMethods,
292 'wcOrderUrls' => $wcOrderUrls,
293 'status' => $paymentAS->getFullStatus($booking, BookableType::EVENT),
294 'total' => $total,
295 'tax' => $bookingPaymentAmount['total_tax'],
296 'wcTax' => $wcTax,
297 'discount' => $bookingPaymentAmount['discount'] + $bookingPaymentAmount['deduction'],
298 'wcDiscount' => $wcDiscount,
299 'eventPrice' => $bookingPaymentAmount['subtotal'],
300 'subtotal' => $bookingPaymentAmount['subtotal'],
301 'paid' => $bookingPaidPrice,
302 'due' => max($total - $bookingPaidPrice, 0),
303 'id' => !empty($booking['payments']) ? array_key_first($booking['payments']) : null,
304 ],
305 'customFields' =>
306 !empty($booking['customFields']) ?
307 $customFieldService->reformatCustomField(CustomerBookingFactory::create($booking), [], $customFieldsCollection) :
308 null,
309 'qrCodes' => !empty($booking['qrCodes']) ? $booking['qrCodes'] : null,
310 ];
311
312 if ($noShowTagEnabled && $customersNoShowCountIds) {
313 /** @var CustomerBookingRepository $bookingRepository */
314 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
315
316 $customersNoShowCount = $bookingRepository->countByNoShowStatus($customersNoShowCountIds);
317
318 if (!empty($customersNoShowCount[$eventBooking['customer']['id']])) {
319 $eventBooking['customer']['noShowCount'] = $customersNoShowCount[$eventBooking['customer']['id']]['count'];
320 }
321 }
322
323 $eventBooking = apply_filters('amelia_get_event_bookings_filter', $eventBooking);
324
325 do_action('amelia_get_event_bookings', $eventBooking);
326
327 $result->setResult(CommandResult::RESULT_SUCCESS);
328 $result->setMessage('Successfully retrieved event bookings');
329 $result->setData(
330 [
331 Entities::BOOKING => $eventBooking,
332 ]
333 );
334
335 return $result;
336 }
337 }
338