PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.31
Booking for Appointments and Events Calendar – Amelia v1.2.31
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 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 2 years ago GetCalendarEventsCommand.php 1 year 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 1 year ago GetEventDeleteEffectCommandHandler.php 1 year ago GetEventsCommand.php 1 year ago GetEventsCommandHandler.php 1 year ago UpdateEventBookingCommand.php 7 years ago UpdateEventBookingCommandHandler.php 1 year ago UpdateEventCommand.php 1 year ago UpdateEventCommandHandler.php 1 year ago UpdateEventStatusCommand.php 1 year ago UpdateEventStatusCommandHandler.php 1 year ago
UpdateEventBookingCommandHandler.php
291 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 (
102 $user &&
103 $userAS->isProvider($user) &&
104 (
105 !$settingsDS->getSetting('roles', 'allowWriteEvents') ||
106 (!$event->getProviders()->keyExists($user->getId()->getValue()) &&
107 (!$event->getOrganizerId() || $event->getOrganizerId()->getValue() !== $user->getId()->getValue()))
108 )
109 ) {
110 throw new AccessDeniedException('You are not allowed to update booking');
111 }
112
113 $isBookingStatusChanged =
114 $bookingData &&
115 isset($bookingData['status']) &&
116 $customerBooking->getStatus()->getValue() !== $bookingData['status'];
117
118 if (isset($bookingData['customFields'])) {
119 $customerBooking->setCustomFields(new Json(json_encode($bookingData['customFields'])));
120 }
121
122 if (isset($bookingData['status'])) {
123 $customerBooking->setStatus(new BookingStatus($bookingData['status']));
124 }
125
126 if (isset($bookingData['coupon'])) {
127 $customerBooking->setCoupon(CouponFactory::create($bookingData['coupon']));
128 }
129
130
131 /** @var CustomerBookingEventTicketRepository $bookingEventTicketRepository */
132 $bookingEventTicketRepository =
133 $this->container->get('domain.booking.customerBookingEventTicket.repository');
134
135 if (
136 $event->getCustomPricing() &&
137 $event->getCustomPricing()->getValue() &&
138 $event->getCustomTickets() &&
139 $event->getCustomTickets()->length()
140 ) {
141 $event->setCustomTickets($eventAS->getTicketsPriceByDateRange($event->getCustomTickets()));
142
143 if (!empty($bookingData['ticketsData'])) {
144 foreach ($bookingData['ticketsData'] as $ticketBooking) {
145 if (empty($ticketBooking['id']) && $ticketBooking['persons']) {
146 /** @var EventTicket $ticket */
147 $ticket = $event->getCustomTickets()->getItem($ticketBooking['eventTicketId']);
148
149 $ticketPrice = $ticket->getDateRangePrice() ?
150 $ticket->getDateRangePrice()->getValue() : $ticket->getPrice()->getValue();
151
152 /** @var CustomerBookingEventTicket $bookingEventTicket */
153 $bookingEventTicket = CustomerBookingEventTicketFactory::create(
154 [
155 'eventTicketId' => $ticketBooking['eventTicketId'],
156 'customerBookingId' => $customerBooking->getId()->getValue(),
157 'persons' => $ticketBooking['persons'],
158 'price' => $ticketPrice,
159 ]
160 );
161
162 $newTicketBookingId = $bookingEventTicketRepository->add($bookingEventTicket);
163 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED) {
164 $isBookingStatusChanged = true;
165 $bookingEventTicket->setId(new Id($newTicketBookingId));
166 $customerBooking->getTicketsBooking()->addItem($bookingEventTicket);
167 }
168 } elseif ($ticketBooking['id'] && $ticketBooking['persons']) {
169 $bookingEventTicketRepository->update($ticketBooking['id'], $ticketBooking);
170
171 foreach ($customerBooking->getTicketsBooking()->getItems() as $item) {
172 if (
173 $item->getEventTicketId()->getValue() === $ticketBooking['eventTicketId'] &&
174 $item->getPersons()->getValue() < $ticketBooking['persons'] &&
175 $customerBooking->getStatus()->getValue() === BookingStatus::APPROVED
176 ) {
177 $isBookingStatusChanged = true;
178 $item->setPersons(new IntegerValue($ticketBooking['persons']));
179 }
180 }
181 } elseif ($ticketBooking['id'] && !$ticketBooking['persons']) {
182 $bookingEventTicketRepository->delete($ticketBooking['id']);
183
184 foreach ($customerBooking->getTicketsBooking()->getItems() as $key => $item) {
185 if ($item->getId()->getValue() === $ticketBooking['id']) {
186 $customerBooking->getTicketsBooking()->deleteItem($key);
187 break;
188 }
189 }
190
191 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED) {
192 $isBookingStatusChanged = true;
193 }
194 }
195 }
196 }
197 } elseif (!empty($bookingData['persons'])) {
198 if (
199 $customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
200 $customerBooking->getPersons()->getValue() < $bookingData['persons']
201 ) {
202 $isBookingStatusChanged = true;
203 $customerBooking->setPersons(new IntegerValue($bookingData['persons']));
204 } elseif (
205 $customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
206 $customerBooking->getPersons()->getValue() > $bookingData['persons']
207 ) {
208 $customerBooking->setPersons(new IntegerValue($bookingData['persons']));
209 }
210 }
211
212 if (
213 $customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
214 $customerBooking->getCustomFields() &&
215 !empty($bookingData['customFields']) &&
216 $customerBooking->getCustomFields()->getValue() !== json_encode($bookingData['customFields'])
217 ) {
218 $isBookingStatusChanged = true;
219 }
220
221
222
223 $customerBookingRepository->update($customerBooking->getId()->getValue(), $customerBooking);
224
225 /** @var Payment $payment */
226 foreach ($customerBooking->getPayments()->getItems() as $payment) {
227 if (
228 $payment->getWcOrderId() &&
229 $payment->getWcOrderId()->getValue()
230 ) {
231 $eventArray = $event->toArray();
232
233 $eventArray['bookings'] = [$customerBooking->toArray()];
234
235 $eventArray['recurring'] = [];
236
237 $eventArray['eventId'] = $eventArray['id'];
238
239 $dateTimeValues = [];
240
241 /** @var EventPeriod $period */
242 foreach ($event->getPeriods()->getItems() as $period) {
243 $dateTimeValues[] = [
244 'start' => $period->getPeriodStart()->getValue()->format('Y-m-d H:i'),
245 'end' => $period->getPeriodEnd()->getValue()->format('Y-m-d H:i')
246 ];
247 }
248
249 $eventArray['dateTimeValues'] = $dateTimeValues;
250
251 foreach ($eventArray['bookings'] as &$booking) {
252 if (!empty($booking['customFields'])) {
253 $customFields = json_decode($booking['customFields'], true);
254
255 $booking['customFields'] = $customFields;
256 }
257 }
258
259 if (StarterWooCommerceService::isEnabled()) {
260 StarterWooCommerceService::updateItemMetaData(
261 $payment->getWcOrderId()->getValue(),
262 $payment->getWcOrderItemId() ? $payment->getWcOrderItemId()->getValue() : null,
263 $eventArray
264 );
265 }
266 }
267 }
268
269 do_action('amelia_after_event_booking_updated', $customerBooking->toArray(), $bookingData);
270
271 $result->setResult(CommandResult::RESULT_SUCCESS);
272 $result->setMessage('Successfully updated booking');
273 $result->setData(
274 [
275 'type' => Entities::EVENT,
276 Entities::EVENT => $event->toArray(),
277 Entities::BOOKING => $customerBooking->toArray(),
278 'appointmentStatusChanged' => false,
279 'bookingStatusChanged' => $isBookingStatusChanged,
280 'paymentId' =>
281 $customerBooking->getPayments()->length() > 0 ?
282 $customerBooking->getPayments()->getItem($customerBooking->getPayments()->keys()[0])->getId()->getValue() :
283 null,
284 'createPaymentLinks' => $command->getField('createPaymentLinks')
285 ]
286 );
287
288 return $result;
289 }
290 }
291