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