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 / Appointment / UpdateAppointmentTimeCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Appointment Last commit date
AddAppointmentCommand.php 1 year ago AddAppointmentCommandHandler.php 1 year ago AddBookingCommand.php 1 year ago AddBookingCommandHandler.php 1 year ago ApproveBookingRemotelyCommand.php 1 year ago ApproveBookingRemotelyCommandHandler.php 1 year ago CancelBookingCommand.php 1 year ago CancelBookingCommandHandler.php 1 year ago CancelBookingRemotelyCommand.php 1 year ago CancelBookingRemotelyCommandHandler.php 2 years ago DeleteAppointmentCommand.php 1 year ago DeleteAppointmentCommandHandler.php 1 year ago DeleteBookingCommand.php 1 year ago DeleteBookingCommandHandler.php 1 year ago GetAppointmentCommand.php 7 years ago GetAppointmentCommandHandler.php 1 year ago GetAppointmentsCommand.php 1 year ago GetAppointmentsCommandHandler.php 1 year ago GetIcsCommand.php 1 year ago GetIcsCommandHandler.php 4 years ago GetPackageAppointmentsCommand.php 1 year ago GetPackageAppointmentsCommandHandler.php 1 year ago GetTimeSlotsCommand.php 1 year ago GetTimeSlotsCommandHandler.php 1 year ago ReassignBookingCommand.php 1 year ago ReassignBookingCommandHandler.php 1 year ago RejectBookingRemotelyCommand.php 1 year ago RejectBookingRemotelyCommandHandler.php 1 year ago SuccessfulBookingCommand.php 1 year ago SuccessfulBookingCommandHandler.php 1 year ago UpdateAppointmentCommand.php 1 year ago UpdateAppointmentCommandHandler.php 1 year ago UpdateAppointmentStatusCommand.php 1 year ago UpdateAppointmentStatusCommandHandler.php 1 year ago UpdateAppointmentTimeCommand.php 1 year ago UpdateAppointmentTimeCommandHandler.php 1 year ago UpdateBookingStatusCommand.php 1 year ago UpdateBookingStatusCommandHandler.php 1 year ago
UpdateAppointmentTimeCommandHandler.php
237 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\Booking\Appointment;
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\Bookable\BookableApplicationService;
9 use AmeliaBooking\Application\Services\Booking\AppointmentApplicationService;
10 use AmeliaBooking\Application\Services\Booking\BookingApplicationService;
11 use AmeliaBooking\Application\Services\Payment\PaymentApplicationService;
12 use AmeliaBooking\Application\Services\User\UserApplicationService;
13 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
14 use AmeliaBooking\Domain\Common\Exceptions\BookingCancellationException;
15 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
16 use AmeliaBooking\Domain\Entity\Bookable\Service\Service;
17 use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment;
18 use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking;
19 use AmeliaBooking\Domain\Entity\Entities;
20 use AmeliaBooking\Domain\Entity\User\AbstractUser;
21 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
22 use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface;
23 use AmeliaBooking\Domain\Services\Settings\SettingsService;
24 use AmeliaBooking\Domain\ValueObjects\BooleanValueObject;
25 use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue;
26 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
27 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
28 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository;
29 use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings;
30 use Interop\Container\Exception\ContainerException;
31
32 /**
33 * Class UpdateAppointmentTimeCommandHandler
34 *
35 * @package AmeliaBooking\Application\Commands\Booking\Appointment
36 */
37 class UpdateAppointmentTimeCommandHandler extends CommandHandler
38 {
39 /**
40 * @var array
41 */
42 public $mandatoryFields = [
43 'bookingStart'
44 ];
45
46 /**
47 * @param UpdateAppointmentTimeCommand $command
48 *
49 * @return CommandResult
50 *
51 * @throws AccessDeniedException
52 * @throws InvalidArgumentException
53 * @throws QueryExecutionException
54 * @throws NotFoundException
55 * @throws ContainerException
56 */
57 public function handle(UpdateAppointmentTimeCommand $command)
58 {
59 $this->checkMandatoryFields($command);
60
61 $result = new CommandResult();
62
63 /** @var UserApplicationService $userAS */
64 $userAS = $this->container->get('application.user.service');
65 /** @var SettingsService $settingsDS */
66 $settingsDS = $this->container->get('domain.settings.service');
67 /** @var AppointmentRepository $appointmentRepo */
68 $appointmentRepo = $this->container->get('domain.booking.appointment.repository');
69 /** @var AppointmentApplicationService $appointmentAS */
70 $appointmentAS = $this->container->get('application.booking.appointment.service');
71 /** @var BookableApplicationService $bookableAS */
72 $bookableAS = $this->container->get('application.bookable.service');
73 /** @var BookingApplicationService $bookingAS */
74 $bookingAS = $this->container->get('application.booking.booking.service');
75 /** @var ReservationServiceInterface $reservationService */
76 $reservationService = $this->container->get('application.reservation.service')->get(Entities::APPOINTMENT);
77 /** @var PaymentApplicationService $paymentAS */
78 $paymentAS = $this->container->get('application.payment.service');
79
80 try {
81 /** @var AbstractUser $user */
82 $user = $command->getUserApplicationService()->authorization(
83 $command->getPage() === 'cabinet' ? $command->getToken() : null,
84 $command->getCabinetType()
85 );
86 } catch (AuthorizationException $e) {
87 $result->setResult(CommandResult::RESULT_ERROR);
88 $result->setData(
89 [
90 'reauthorize' => true
91 ]
92 );
93
94 return $result;
95 }
96
97 if ($userAS->isCustomer($user) && !$settingsDS->getSetting('roles', 'allowCustomerReschedule')) {
98 throw new AccessDeniedException('You are not allowed to update appointment');
99 }
100
101 /** @var Appointment $appointment */
102 $appointment = $appointmentRepo->getById((int)$command->getArg('id'));
103
104 $initialBookingStart = $appointment->getBookingStart()->getValue();
105 $initialBookingEnd = $appointment->getBookingEnd()->getValue();
106
107 /** @var Service $service */
108 $service = $bookableAS->getAppointmentService(
109 $appointment->getServiceId()->getValue(),
110 $appointment->getProviderId()->getValue()
111 );
112
113 /** @var CustomerBooking $booking */
114 foreach ($appointment->getBookings()->getItems() as $booking) {
115 if (
116 $userAS->isAmeliaUser($user) &&
117 $userAS->isCustomer($user) &&
118 $bookingAS->isBookingApprovedOrPending($booking->getStatus()->getValue()) &&
119 ($service->getMinCapacity()->getValue() !== 1 || $service->getMaxCapacity()->getValue() !== 1) &&
120 ($user->getId() && $booking->getCustomerId()->getValue() !== $user->getId()->getValue())
121 ) {
122 throw new AccessDeniedException('You are not allowed to update appointment');
123 }
124 }
125
126 $minimumRescheduleTimeInSeconds = $settingsDS
127 ->getEntitySettings($service->getSettings())
128 ->getGeneralSettings()
129 ->getMinimumTimeRequirementPriorToRescheduling();
130
131 try {
132 $reservationService->inspectMinimumCancellationTime(
133 $appointment->getBookingStart()->getValue(),
134 $minimumRescheduleTimeInSeconds
135 );
136 } catch (BookingCancellationException $e) {
137 $result->setResult(CommandResult::RESULT_ERROR);
138 $result->setMessage('You are not allowed to update booking');
139 $result->setData(
140 [
141 'rescheduleBookingUnavailable' => true
142 ]
143 );
144
145 return $result;
146 }
147
148 $bookingStart = $command->getField('bookingStart');
149
150 if ($command->getField('timeZone')) {
151 $bookingStart = DateTimeService::getDateTimeObjectInTimeZone(
152 $bookingStart,
153 $command->getField('timeZone')
154 )->setTimezone(DateTimeService::getTimeZone())->format('Y-m-d H:i:s');
155 }
156
157 $bookingStartInUtc = DateTimeService::getCustomDateTimeObject(
158 $bookingStart
159 )->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i');
160
161 // Convert UTC slot to slot in TimeZone based on Settings
162 if ($command->getField('utcOffset') !== null && $settingsDS->getSetting('general', 'showClientTimeZone')) {
163 $bookingStart = DateTimeService::getCustomDateTimeFromUtc(
164 $bookingStart
165 );
166 }
167
168 $appointment->setBookingStart(
169 new DateTimeValue(
170 DateTimeService::getCustomDateTimeObject(
171 $bookingStart
172 )
173 )
174 );
175
176 $appointment->setBookingEnd(
177 new DateTimeValue(
178 DateTimeService::getCustomDateTimeObject($bookingStart)
179 ->modify('+' . $appointmentAS->getAppointmentLengthTime($appointment, $service) . ' second')
180 )
181 );
182
183 if (!$appointmentAS->canBeBooked($appointment, $userAS->isCustomer($user), null, null)) {
184 $result->setResult(CommandResult::RESULT_ERROR);
185 $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']);
186 $result->setData(
187 [
188 'timeSlotUnavailable' => true
189 ]
190 );
191
192 return $result;
193 }
194
195 do_action('amelia_before_booking_rescheduled', $appointment->toArray());
196
197 $appointmentRepo->update((int)$command->getArg('id'), $appointment);
198
199 foreach ($appointment->getBookings()->getItems() as $booking) {
200 $paymentAS->updateBookingPaymentDate($booking, $bookingStartInUtc);
201 }
202
203 $appointment->setRescheduled(new BooleanValueObject(true));
204
205 $bookingAS->bookingRescheduled(
206 $appointment->getId()->getValue(),
207 Entities::APPOINTMENT,
208 null,
209 Entities::CUSTOMER
210 );
211
212 $bookingAS->bookingRescheduled(
213 $appointment->getId()->getValue(),
214 Entities::APPOINTMENT,
215 $appointment->getProviderId()->getValue(),
216 Entities::PROVIDER
217 );
218
219
220 do_action('amelia_after_booking_rescheduled', $appointment->toArray());
221
222 $result->setResult(CommandResult::RESULT_SUCCESS);
223 $result->setMessage('Successfully updated appointment time');
224 $result->setData(
225 [
226 Entities::APPOINTMENT => $appointment->toArray(),
227 'initialAppointmentDateTime' => [
228 'bookingStart' => $initialBookingStart->format('Y-m-d H:i:s'),
229 'bookingEnd' => $initialBookingEnd->format('Y-m-d H:i:s'),
230 ],
231 ]
232 );
233
234 return $result;
235 }
236 }
237