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