PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.0.2
Booking for Appointments and Events Calendar – Amelia v2.0.2
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
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
GetEventBookingCommandHandler.php
332 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 ]
201 );
202
203 $eventArray['organizer'] = $event->getOrganizerId() && $event->getOrganizer() ? [
204 'id' => $event->getOrganizerId(),
205 'firstName' => $event->getOrganizer()->getFirstName()->getValue(),
206 'lastName' => $event->getOrganizer()->getLastName() ? $event->getOrganizer()->getLastName()->getValue() : null,
207 'picture' => $event->getOrganizer()->getPicture() ? $event->getOrganizer()->getPicture()->getThumbPath() : null,
208 ] : null;
209
210
211 $persons = $booking['persons'];
212 if (!empty($eventArray['customPricing']) && !empty($booking['ticketsData'])) {
213 /** @var CustomerBookingEventTicket $bookedTicket */
214 foreach ($booking['ticketsData'] as $bookedTicket) {
215 $persons += $bookedTicket['persons'];
216 }
217 }
218
219 $ticketsData = [];
220
221 if (!empty($booking['ticketsData'])) {
222 foreach ($booking['ticketsData'] as $ticket) {
223 /** @var EventTicket $eventTicket */
224 $eventTicket =
225 $event->getCustomTickets()->keyExists($ticket['eventTicketId']) ? $event->getCustomTickets()->getItem($ticket['eventTicketId']) : null;
226
227 $ticketsData[] = [
228 'id' => $ticket['id'],
229 'eventTicketId' => $eventTicket ? $eventTicket->getId()->getValue() : null,
230 'name' => $eventTicket ? $eventTicket->getName()->getValue() : null,
231 'price' => $ticket['price'],
232 'quantity' => $ticket['persons']
233 ];
234 }
235 }
236
237 $bookingPaymentAmount = $reservationService->getPaymentAmount(CustomerBookingFactory::create($booking), $event, true);
238
239 $wcTax = 0;
240 $wcDiscount = 0;
241
242 $bookingPaidPrice = 0;
243 $paymentMethods = [];
244 $wcOrderUrls = [];
245 foreach ($booking['payments'] as $payment) {
246 $paymentMethods[] = $payment['gateway'];
247
248 if ($payment['status'] === 'paid' || $payment['status'] === 'partiallyPaid') {
249 $bookingPaidPrice += $payment['amount'];
250 }
251
252 $paymentAS->addWcFields($payment);
253
254 $wcTax += !empty($payment['wcItemTaxValue']) ? $payment['wcItemTaxValue'] : 0;
255
256 $wcDiscount += !empty($payment['wcItemCouponValue']) ? $payment['wcItemCouponValue'] : 0;
257
258 if (!empty($payment['wcOrderId'])) {
259 $wcOrderUrls[$payment['wcOrderId']] = $payment['wcOrderUrl'];
260 }
261 }
262
263 $total = $bookingPaymentAmount['subtotal']
264 + $bookingPaymentAmount['total_tax']
265 + $wcTax
266 - $bookingPaymentAmount['discount']
267 - $bookingPaymentAmount['deduction']
268 - $wcDiscount;
269
270 $eventBooking = [
271 'id' => $booking['id'],
272 'status' => $eventArray['status'] === 'canceled' || $eventArray['status'] === 'rejected' ? 'canceled' : $booking['status'],
273 'persons' => $persons,
274 'checked' => false,
275 'tickets' => $ticketsData,
276 'customer' => [
277 'id' => $booking['customer']['id'],
278 'firstName' => $booking['customer']['firstName'],
279 'lastName' => $booking['customer']['lastName'],
280 'note' => $booking['customer']['note']
281 ],
282 'code' => !empty($booking['token']) ? substr($booking['token'], 0, 5) : '',
283 'event' => $eventArray,
284 'payment' => [
285 'paymentMethods' => $paymentMethods,
286 'wcOrderUrls' => $wcOrderUrls,
287 'status' => $paymentAS->getFullStatus($booking, BookableType::EVENT),
288 'total' => $total,
289 'tax' => $bookingPaymentAmount['total_tax'],
290 'wcTax' => $wcTax,
291 'discount' => $bookingPaymentAmount['discount'] + $bookingPaymentAmount['deduction'],
292 'wcDiscount' => $wcDiscount,
293 'eventPrice' => $bookingPaymentAmount['subtotal'],
294 'subtotal' => $bookingPaymentAmount['subtotal'],
295 'paid' => $bookingPaidPrice,
296 'due' => max($total - $bookingPaidPrice, 0),
297 'id' => !empty($booking['payments']) ? array_key_first($booking['payments']) : null,
298 ],
299 'customFields' =>
300 !empty($booking['customFields']) ?
301 $customFieldService->reformatCustomField(CustomerBookingFactory::create($booking), [], $customFieldsCollection) :
302 null,
303 'qrCodes' => !empty($booking['qrCodes']) ? $booking['qrCodes'] : null,
304 ];
305
306 if ($noShowTagEnabled && $customersNoShowCountIds) {
307 /** @var CustomerBookingRepository $bookingRepository */
308 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
309
310 $customersNoShowCount = $bookingRepository->countByNoShowStatus($customersNoShowCountIds);
311
312 if (!empty($customersNoShowCount[$eventBooking['customer']['id']])) {
313 $eventBooking['customer']['noShowCount'] = $customersNoShowCount[$eventBooking['customer']['id']]['count'];
314 }
315 }
316
317 $eventBooking = apply_filters('amelia_get_event_bookings_filter', $eventBooking);
318
319 do_action('amelia_get_event_bookings', $eventBooking);
320
321 $result->setResult(CommandResult::RESULT_SUCCESS);
322 $result->setMessage('Successfully retrieved event bookings');
323 $result->setData(
324 [
325 Entities::BOOKING => $eventBooking,
326 ]
327 );
328
329 return $result;
330 }
331 }
332