PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.11
Booking for Appointments and Events Calendar – Amelia v1.2.11
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 2 years 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 2 years ago UpdateEventCommand.php 7 years ago UpdateEventCommandHandler.php 1 year ago UpdateEventStatusCommand.php 7 years ago UpdateEventStatusCommandHandler.php 2 years ago
UpdateEventBookingCommandHandler.php
271 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->getCustomTickets() &&
135 $event->getCustomTickets()->length()
136 ) {
137 $event->setCustomTickets($eventAS->getTicketsPriceByDateRange($event->getCustomTickets()));
138
139 if (!empty($bookingData['ticketsData'])) {
140 foreach ($bookingData['ticketsData'] as $ticketBooking) {
141 if (!$ticketBooking['id'] && $ticketBooking['persons']) {
142 /** @var EventTicket $ticket */
143 $ticket = $event->getCustomTickets()->getItem($ticketBooking['eventTicketId']);
144
145 $ticketPrice = $ticket->getDateRangePrice() ?
146 $ticket->getDateRangePrice()->getValue() : $ticket->getPrice()->getValue();
147
148 /** @var CustomerBookingEventTicket $bookingEventTicket */
149 $bookingEventTicket = CustomerBookingEventTicketFactory::create(
150 [
151 'eventTicketId' => $ticketBooking['eventTicketId'],
152 'customerBookingId' => $customerBooking->getId()->getValue(),
153 'persons' => $ticketBooking['persons'],
154 'price' => $ticketPrice,
155 ]
156 );
157
158 $newTicketBookingId = $bookingEventTicketRepository->add($bookingEventTicket);
159 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED) {
160 $isBookingStatusChanged = true;
161 $bookingEventTicket->setId(new Id($newTicketBookingId));
162 $customerBooking->getTicketsBooking()->addItem($bookingEventTicket);
163 }
164 } else if ($ticketBooking['id'] && $ticketBooking['persons']) {
165 $bookingEventTicketRepository->update($ticketBooking['id'], $ticketBooking);
166
167 foreach ($customerBooking->getTicketsBooking()->getItems() as $item) {
168 if ($item->getEventTicketId()->getValue() === $ticketBooking['eventTicketId'] &&
169 $item->getPersons()->getValue() < $ticketBooking['persons'] &&
170 $customerBooking->getStatus()->getValue() === BookingStatus::APPROVED
171 ) {
172 $isBookingStatusChanged = true;
173 $item->setPersons(new IntegerValue($ticketBooking['persons']));
174 }
175 }
176 } else if ($ticketBooking['id'] && !$ticketBooking['persons']) {
177 $bookingEventTicketRepository->delete($ticketBooking['id']);
178
179 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED) {
180 $isBookingStatusChanged = true;
181 }
182 }
183 }
184 }
185 } else if (!empty($bookingData['persons'])) {
186 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
187 $customerBooking->getPersons()->getValue() < $bookingData['persons']
188 ) {
189 $isBookingStatusChanged = true;
190 $customerBooking->setPersons(new IntegerValue($bookingData['persons']));
191 } elseif ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
192 $customerBooking->getPersons()->getValue() > $bookingData['persons']
193 ) {
194 $customerBooking->setPersons(new IntegerValue($bookingData['persons']));
195 }
196 }
197
198 if ($customerBooking->getStatus()->getValue() === BookingStatus::APPROVED &&
199 $customerBooking->getCustomFields() &&
200 !empty($bookingData['customFields']) &&
201 $customerBooking->getCustomFields()->getValue() !== json_encode($bookingData['customFields'])
202 ) {
203 $isBookingStatusChanged = true;
204 }
205
206
207
208 $customerBookingRepository->update($customerBooking->getId()->getValue(), $customerBooking);
209
210 /** @var Payment $payment */
211 foreach ($customerBooking->getPayments()->getItems() as $payment) {
212 if ($payment->getWcOrderId() &&
213 $payment->getWcOrderId()->getValue()
214 ) {
215 $eventArray = $event->toArray();
216
217 $eventArray['bookings'] = [$customerBooking->toArray()];
218
219 $eventArray['recurring'] = [];
220
221 $eventArray['eventId'] = $eventArray['id'];
222
223 $dateTimeValues = [];
224
225 /** @var EventPeriod $period */
226 foreach ($event->getPeriods()->getItems() as $period) {
227 $dateTimeValues[] = [
228 'start' => $period->getPeriodStart()->getValue()->format('Y-m-d H:i'),
229 'end' => $period->getPeriodEnd()->getValue()->format('Y-m-d H:i')
230 ];
231 }
232
233 $eventArray['dateTimeValues'] = $dateTimeValues;
234
235 foreach ($eventArray['bookings'] as &$booking) {
236 if (!empty($booking['customFields'])) {
237 $customFields = json_decode($booking['customFields'], true);
238
239 $booking['customFields'] = $customFields;
240 }
241 }
242
243 if (StarterWooCommerceService::isEnabled()) {
244 StarterWooCommerceService::updateItemMetaData(
245 $payment->getWcOrderId()->getValue(),
246 $eventArray
247 );
248 }
249 }
250 }
251
252 do_action('amelia_after_event_booking_updated', $customerBooking->toArray(), $bookingData);
253
254 $result->setResult(CommandResult::RESULT_SUCCESS);
255 $result->setMessage('Successfully updated booking');
256 $result->setData(
257 [
258 'type' => Entities::EVENT,
259 Entities::EVENT => $event->toArray(),
260 Entities::BOOKING => $customerBooking->toArray(),
261 'appointmentStatusChanged' => false,
262 'bookingStatusChanged' => $isBookingStatusChanged,
263 'paymentId' => $customerBooking->getPayments()->length() > 0 ? $customerBooking->getPayments()->getItem($customerBooking->getPayments()->keys()[0])->getId()->getValue() : null,
264 'createPaymentLinks' => $command->getField('createPaymentLinks')
265 ]
266 );
267
268 return $result;
269 }
270 }
271