PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.25
Booking for Appointments and Events Calendar – Amelia v1.2.25
2.4.9 2.4.8 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 / UpdateEventBookingCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Event Last commit date
AddEventCommand.php 7 years ago AddEventCommandHandler.php 2 years ago DeleteEventBookingCommand.php 7 years ago DeleteEventBookingCommandHandler.php 1 year ago DeleteEventCommand.php 7 years ago DeleteEventCommandHandler.php 2 years ago GetCalendarEventsCommand.php 4 years ago GetCalendarEventsCommandHandler.php 1 year ago GetEventBookingsCommand.php 1 year ago GetEventBookingsCommandHandler.php 1 year ago GetEventCommand.php 7 years ago GetEventCommandHandler.php 1 year ago GetEventDeleteEffectCommand.php 7 years ago GetEventDeleteEffectCommandHandler.php 2 years ago GetEventsCommand.php 7 years ago GetEventsCommandHandler.php 1 year ago UpdateEventBookingCommand.php 7 years ago UpdateEventBookingCommandHandler.php 1 year ago UpdateEventCommand.php 7 years ago UpdateEventCommandHandler.php 1 year ago UpdateEventStatusCommand.php 7 years ago UpdateEventStatusCommandHandler.php 2 years ago
UpdateEventBookingCommandHandler.php
281 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\User\UserApplicationService;
10 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
11 use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking;
12 use AmeliaBooking\Domain\Entity\Booking\Event\CustomerBookingEventTicket;
13 use AmeliaBooking\Domain\Entity\Booking\Event\Event;
14 use AmeliaBooking\Domain\Entity\Booking\Event\EventPeriod;
15 use AmeliaBooking\Domain\Entity\Booking\Event\EventTicket;
16 use AmeliaBooking\Domain\Entity\Entities;
17 use AmeliaBooking\Domain\Entity\Payment\Payment;
18 use AmeliaBooking\Domain\Entity\User\AbstractUser;
19 use AmeliaBooking\Domain\Factory\Booking\Event\CustomerBookingEventTicketFactory;
20 use AmeliaBooking\Domain\Factory\Coupon\CouponFactory;
21 use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface;
22 use AmeliaBooking\Domain\Services\Settings\SettingsService;
23 use AmeliaBooking\Domain\ValueObjects\Json;
24 use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id;
25 use AmeliaBooking\Domain\ValueObjects\Number\Integer\IntegerValue;
26 use AmeliaBooking\Domain\ValueObjects\String\BookingStatus;
27 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
28 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
29 use AmeliaBooking\Infrastructure\Repository\Booking\Event\CustomerBookingEventTicketRepository;
30 use AmeliaBooking\Infrastructure\WP\Integrations\WooCommerce\StarterWooCommerceService;
31 use Interop\Container\Exception\ContainerException;
32 use Slim\Exception\ContainerValueNotFoundException;
33
34 /**
35 * Class UpdateEventBookingCommandHandler
36 *
37 * @package AmeliaBooking\Application\Commands\Booking\Event
38 */
39 class UpdateEventBookingCommandHandler extends CommandHandler
40 {
41 /**
42 * @param UpdateEventBookingCommand $command
43 *
44 * @return CommandResult
45 * @throws ContainerValueNotFoundException
46 * @throws AccessDeniedException
47 * @throws QueryExecutionException
48 * @throws ContainerException
49 * @throws InvalidArgumentException
50 */
51 public function handle(UpdateEventBookingCommand $command)
52 {
53 $result = new CommandResult();
54
55 /** @var UserApplicationService $userAS */
56 $userAS = $this->getContainer()->get('application.user.service');
57
58 /** @var SettingsService $settingsDS */
59 $settingsDS = $this->container->get('domain.settings.service');
60
61 /** @var AbstractUser $user */
62 $user = $this->container->get('logged.in.user');
63
64 if (!$command->getPermissionService()->currentUserCanWrite(Entities::EVENTS)) {
65 $user = $userAS->getAuthenticatedUser($command->getToken(), false, 'providerCabinet');
66
67 if ($user === null) {
68 $result->setResult(CommandResult::RESULT_ERROR);
69 $result->setMessage('Could not retrieve user');
70 $result->setData(
71 [
72 'reauthorize' => true
73 ]
74 );
75
76 return $result;
77 }
78 }
79
80 $this->checkMandatoryFields($command);
81
82 $bookingData = $command->getField('bookings') ? $command->getField('bookings')[0] : null;
83
84 /** @var CustomerBookingRepository $customerBookingRepository */
85 $customerBookingRepository = $this->container->get('domain.booking.customerBooking.repository');
86
87 /** @var EventApplicationService $eventAS */
88 $eventAS = $this->container->get('application.booking.event.service');
89
90 /** @var ReservationServiceInterface $reservationService */
91 $reservationService = $this->container->get('application.reservation.service')->get(Entities::EVENT);
92
93 /** @var Event $event */
94 $event = $reservationService->getReservationByBookingId((int)$command->getField('id'));
95
96 /** @var CustomerBooking $customerBooking */
97 $customerBooking = $event->getBookings()->getItem((int)$command->getField('id'));
98
99 do_action('amelia_before_event_booking_updated', $customerBooking ? $customerBooking->toArray() : null, $bookingData);
100
101 if ($user &&
102 $userAS->isProvider($user) &&
103 (
104 !$settingsDS->getSetting('roles', 'allowWriteEvents') ||
105 (!$event->getProviders()->keyExists($user->getId()->getValue()) &&
106 (!$event->getOrganizerId() || $event->getOrganizerId()->getValue() !== $user->getId()->getValue()))
107 )
108 ) {
109 throw new AccessDeniedException('You are not allowed to update booking');
110 }
111
112 $isBookingStatusChanged =
113 $bookingData &&
114 isset($bookingData['status']) &&
115 $customerBooking->getStatus()->getValue() !== $bookingData['status'];
116
117 if (isset($bookingData['customFields'])) {
118 $customerBooking->setCustomFields(new Json(json_encode($bookingData['customFields'])));
119 }
120
121 if (isset($bookingData['status'])) {
122 $customerBooking->setStatus(new BookingStatus($bookingData['status']));
123 }
124
125 if (isset($bookingData['coupon'])) {
126 $customerBooking->setCoupon(CouponFactory::create($bookingData['coupon']));
127 }
128
129
130 /** @var CustomerBookingEventTicketRepository $bookingEventTicketRepository */
131 $bookingEventTicketRepository =
132 $this->container->get('domain.booking.customerBookingEventTicket.repository');
133
134 if ($event->getCustomPricing() &&
135 $event->getCustomPricing()->getValue() &&
136 $event->getCustomTickets() &&
137 $event->getCustomTickets()->length()
138 ) {
139 $event->setCustomTickets($eventAS->getTicketsPriceByDateRange($event->getCustomTickets()));
140
141 if (!empty($bookingData['ticketsData'])) {
142 foreach ($bookingData['ticketsData'] as $ticketBooking) {
143 if (empty($ticketBooking['id']) && $ticketBooking['persons']) {
144 /** @var EventTicket $ticket */
145 $ticket = $event->getCustomTickets()->getItem($ticketBooking['eventTicketId']);
146
147 $ticketPrice = $ticket->getDateRangePrice() ?
148 $ticket->getDateRangePrice()->getValue() : $ticket->getPrice()->getValue();
149
150 /** @var CustomerBookingEventTicket $bookingEventTicket */
151 $bookingEventTicket = CustomerBookingEventTicketFactory::create(
152 [
153 'eventTicketId' => $ticketBooking['eventTicketId'],
154 'customerBookingId' => $customerBooking->getId()->getValue(),
155 'persons' => $ticketBooking['persons'],
156 'price' => $ticketPrice,
157 ]
158 );
159
160 $newTicketBookingId = $bookingEventTicketRepository->add($bookingEventTicket);
161 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED) {
162 $isBookingStatusChanged = true;
163 $bookingEventTicket->setId(new Id($newTicketBookingId));
164 $customerBooking->getTicketsBooking()->addItem($bookingEventTicket);
165 }
166 } else if ($ticketBooking['id'] && $ticketBooking['persons']) {
167 $bookingEventTicketRepository->update($ticketBooking['id'], $ticketBooking);
168
169 foreach ($customerBooking->getTicketsBooking()->getItems() as $item) {
170 if ($item->getEventTicketId()->getValue() === $ticketBooking['eventTicketId'] &&
171 $item->getPersons()->getValue() < $ticketBooking['persons'] &&
172 $customerBooking->getStatus()->getValue() === BookingStatus::APPROVED
173 ) {
174 $isBookingStatusChanged = true;
175 $item->setPersons(new IntegerValue($ticketBooking['persons']));
176 }
177 }
178 } else if ($ticketBooking['id'] && !$ticketBooking['persons']) {
179 $bookingEventTicketRepository->delete($ticketBooking['id']);
180
181 foreach ($customerBooking->getTicketsBooking()->getItems() as $key => $item) {
182 if ($item->getId()->getValue() === $ticketBooking['id']) {
183 $customerBooking->getTicketsBooking()->deleteItem($key);
184 break;
185 }
186 }
187
188 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED) {
189 $isBookingStatusChanged = true;
190 }
191 }
192 }
193 }
194 } else if (!empty($bookingData['persons'])) {
195 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
196 $customerBooking->getPersons()->getValue() < $bookingData['persons']
197 ) {
198 $isBookingStatusChanged = true;
199 $customerBooking->setPersons(new IntegerValue($bookingData['persons']));
200 } elseif ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
201 $customerBooking->getPersons()->getValue() > $bookingData['persons']
202 ) {
203 $customerBooking->setPersons(new IntegerValue($bookingData['persons']));
204 }
205 }
206
207 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
208 $customerBooking->getCustomFields() &&
209 !empty($bookingData['customFields']) &&
210 $customerBooking->getCustomFields()->getValue() !== json_encode($bookingData['customFields'])
211 ) {
212 $isBookingStatusChanged = true;
213 }
214
215
216
217 $customerBookingRepository->update($customerBooking->getId()->getValue(), $customerBooking);
218
219 /** @var Payment $payment */
220 foreach ($customerBooking->getPayments()->getItems() as $payment) {
221 if ($payment->getWcOrderId() &&
222 $payment->getWcOrderId()->getValue()
223 ) {
224 $eventArray = $event->toArray();
225
226 $eventArray['bookings'] = [$customerBooking->toArray()];
227
228 $eventArray['recurring'] = [];
229
230 $eventArray['eventId'] = $eventArray['id'];
231
232 $dateTimeValues = [];
233
234 /** @var EventPeriod $period */
235 foreach ($event->getPeriods()->getItems() as $period) {
236 $dateTimeValues[] = [
237 'start' => $period->getPeriodStart()->getValue()->format('Y-m-d H:i'),
238 'end' => $period->getPeriodEnd()->getValue()->format('Y-m-d H:i')
239 ];
240 }
241
242 $eventArray['dateTimeValues'] = $dateTimeValues;
243
244 foreach ($eventArray['bookings'] as &$booking) {
245 if (!empty($booking['customFields'])) {
246 $customFields = json_decode($booking['customFields'], true);
247
248 $booking['customFields'] = $customFields;
249 }
250 }
251
252 if (StarterWooCommerceService::isEnabled()) {
253 StarterWooCommerceService::updateItemMetaData(
254 $payment->getWcOrderId()->getValue(),
255 $payment->getWcOrderItemId() ? $payment->getWcOrderItemId()->getValue() : null,
256 $eventArray
257 );
258 }
259 }
260 }
261
262 do_action('amelia_after_event_booking_updated', $customerBooking->toArray(), $bookingData);
263
264 $result->setResult(CommandResult::RESULT_SUCCESS);
265 $result->setMessage('Successfully updated booking');
266 $result->setData(
267 [
268 'type' => Entities::EVENT,
269 Entities::EVENT => $event->toArray(),
270 Entities::BOOKING => $customerBooking->toArray(),
271 'appointmentStatusChanged' => false,
272 'bookingStatusChanged' => $isBookingStatusChanged,
273 'paymentId' => $customerBooking->getPayments()->length() > 0 ? $customerBooking->getPayments()->getItem($customerBooking->getPayments()->keys()[0])->getId()->getValue() : null,
274 'createPaymentLinks' => $command->getField('createPaymentLinks')
275 ]
276 );
277
278 return $result;
279 }
280 }
281