PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
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
273 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 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED) {
182 $isBookingStatusChanged = true;
183 }
184 }
185 }
186 }
187 } else if (!empty($bookingData['persons'])) {
188 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
189 $customerBooking->getPersons()->getValue() < $bookingData['persons']
190 ) {
191 $isBookingStatusChanged = true;
192 $customerBooking->setPersons(new IntegerValue($bookingData['persons']));
193 } elseif ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
194 $customerBooking->getPersons()->getValue() > $bookingData['persons']
195 ) {
196 $customerBooking->setPersons(new IntegerValue($bookingData['persons']));
197 }
198 }
199
200 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
201 $customerBooking->getCustomFields() &&
202 !empty($bookingData['customFields']) &&
203 $customerBooking->getCustomFields()->getValue() !== json_encode($bookingData['customFields'])
204 ) {
205 $isBookingStatusChanged = true;
206 }
207
208
209
210 $customerBookingRepository->update($customerBooking->getId()->getValue(), $customerBooking);
211
212 /** @var Payment $payment */
213 foreach ($customerBooking->getPayments()->getItems() as $payment) {
214 if ($payment->getWcOrderId() &&
215 $payment->getWcOrderId()->getValue()
216 ) {
217 $eventArray = $event->toArray();
218
219 $eventArray['bookings'] = [$customerBooking->toArray()];
220
221 $eventArray['recurring'] = [];
222
223 $eventArray['eventId'] = $eventArray['id'];
224
225 $dateTimeValues = [];
226
227 /** @var EventPeriod $period */
228 foreach ($event->getPeriods()->getItems() as $period) {
229 $dateTimeValues[] = [
230 'start' => $period->getPeriodStart()->getValue()->format('Y-m-d H:i'),
231 'end' => $period->getPeriodEnd()->getValue()->format('Y-m-d H:i')
232 ];
233 }
234
235 $eventArray['dateTimeValues'] = $dateTimeValues;
236
237 foreach ($eventArray['bookings'] as &$booking) {
238 if (!empty($booking['customFields'])) {
239 $customFields = json_decode($booking['customFields'], true);
240
241 $booking['customFields'] = $customFields;
242 }
243 }
244
245 if (StarterWooCommerceService::isEnabled()) {
246 StarterWooCommerceService::updateItemMetaData(
247 $payment->getWcOrderId()->getValue(),
248 $eventArray
249 );
250 }
251 }
252 }
253
254 do_action('amelia_after_event_booking_updated', $customerBooking->toArray(), $bookingData);
255
256 $result->setResult(CommandResult::RESULT_SUCCESS);
257 $result->setMessage('Successfully updated booking');
258 $result->setData(
259 [
260 'type' => Entities::EVENT,
261 Entities::EVENT => $event->toArray(),
262 Entities::BOOKING => $customerBooking->toArray(),
263 'appointmentStatusChanged' => false,
264 'bookingStatusChanged' => $isBookingStatusChanged,
265 'paymentId' => $customerBooking->getPayments()->length() > 0 ? $customerBooking->getPayments()->getItem($customerBooking->getPayments()->keys()[0])->getId()->getValue() : null,
266 'createPaymentLinks' => $command->getField('createPaymentLinks')
267 ]
268 );
269
270 return $result;
271 }
272 }
273