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