PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.0.2
Booking for Appointments and Events Calendar – Amelia v2.0.2
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 / UpdateAppointmentStatusCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Appointment Last commit date
AddAppointmentCommand.php 1 year ago AddAppointmentCommandHandler.php 6 months ago AddBookingCommand.php 1 year ago AddBookingCommandHandler.php 1 year ago ApproveBookingRemotelyCommand.php 1 year ago ApproveBookingRemotelyCommandHandler.php 6 months ago CancelBookingCommand.php 1 year ago CancelBookingCommandHandler.php 6 months ago CancelBookingRemotelyCommand.php 1 year ago CancelBookingRemotelyCommandHandler.php 6 months ago DeleteAppointmentCommand.php 1 year ago DeleteAppointmentCommandHandler.php 1 year ago DeleteBookingCommand.php 1 year ago DeleteBookingCommandHandler.php 10 months ago DeleteBookingRemotelyCommand.php 10 months ago DeleteBookingRemotelyCommandHandler.php 6 months ago GetAppointmentBookingsCommand.php 6 months ago GetAppointmentBookingsCommandHandler.php 6 months ago GetAppointmentCommand.php 7 years ago GetAppointmentCommandHandler.php 5 months ago GetAppointmentsCommand.php 1 year ago GetAppointmentsCommandHandler.php 6 months ago GetIcsCommand.php 6 months ago GetIcsCommandHandler.php 4 years ago GetPackageAppointmentsCommand.php 1 year ago GetPackageAppointmentsCommandHandler.php 6 months ago GetTimeSlotsCommand.php 1 year ago GetTimeSlotsCommandHandler.php 10 months ago ReassignBookingCommand.php 1 year ago ReassignBookingCommandHandler.php 10 months ago RejectBookingRemotelyCommand.php 1 year ago RejectBookingRemotelyCommandHandler.php 6 months ago SuccessfulBookingCommand.php 1 year ago SuccessfulBookingCommandHandler.php 1 year ago UpdateAppointmentCommand.php 1 year ago UpdateAppointmentCommandHandler.php 6 months ago UpdateAppointmentStatusCommand.php 1 year ago UpdateAppointmentStatusCommandHandler.php 5 months ago UpdateAppointmentTimeCommand.php 1 year ago UpdateAppointmentTimeCommandHandler.php 10 months ago UpdateBookingStatusCommand.php 1 year ago UpdateBookingStatusCommandHandler.php 5 months ago
UpdateAppointmentStatusCommandHandler.php
248 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\User\UserApplicationService;
12 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
13 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
14 use AmeliaBooking\Domain\Entity\Bookable\Service\Service;
15 use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment;
16 use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking;
17 use AmeliaBooking\Domain\Entity\Entities;
18 use AmeliaBooking\Domain\Entity\User\AbstractUser;
19 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
20 use AmeliaBooking\Domain\ValueObjects\BooleanValueObject;
21 use AmeliaBooking\Domain\ValueObjects\String\BookingStatus;
22 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
23 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
24 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository;
25 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
26 use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository;
27 use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings;
28 use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings;
29 use Interop\Container\Exception\ContainerException;
30
31 /**
32 * Class UpdateAppointmentStatusCommandHandler
33 *
34 * @package AmeliaBooking\Application\Commands\Booking\Appointment
35 */
36 class UpdateAppointmentStatusCommandHandler extends CommandHandler
37 {
38 /**
39 * @var array
40 */
41 public $mandatoryFields = [
42 'status'
43 ];
44
45 /**
46 * @param UpdateAppointmentStatusCommand $command
47 *
48 * @return CommandResult
49 *
50 * @throws AccessDeniedException
51 * @throws InvalidArgumentException
52 * @throws QueryExecutionException
53 * @throws ContainerException
54 * @throws NotFoundException
55 */
56 public function handle(UpdateAppointmentStatusCommand $command)
57 {
58 $result = new CommandResult();
59
60 if (!$command->getPermissionService()->currentUserCanWriteStatus(Entities::APPOINTMENTS)) {
61 try {
62 /** @var AbstractUser $user */
63 $user = $command->getUserApplicationService()->authorization(
64 $command->getPage() === 'cabinet' ? $command->getToken() : null,
65 $command->getCabinetType()
66 );
67 } catch (AuthorizationException $e) {
68 $result->setResult(CommandResult::RESULT_ERROR);
69 $result->setData(
70 [
71 'reauthorize' => true
72 ]
73 );
74
75 return $result;
76 }
77 } else {
78 /** @var AbstractUser $user */
79 $user = $this->container->get('logged.in.user');
80 }
81
82 $this->checkMandatoryFields($command);
83
84 /** @var CustomerBookingRepository $bookingRepository */
85 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
86 /** @var AppointmentRepository $appointmentRepo */
87 $appointmentRepo = $this->container->get('domain.booking.appointment.repository');
88 /** @var BookingApplicationService $bookingAS */
89 $bookingAS = $this->container->get('application.booking.booking.service');
90 /** @var UserApplicationService $userAS */
91 $userAS = $command->getUserApplicationService();
92 /** @var AppointmentApplicationService $appointmentAS */
93 $appointmentAS = $this->container->get('application.booking.appointment.service');
94 /** @var BookableApplicationService $bookableAS */
95 $bookableAS = $this->container->get('application.bookable.service');
96 /** @var ProviderRepository $providerRepo */
97 $providerRepo = $this->container->get('domain.users.providers.repository');
98
99 $appointmentId = (int)$command->getArg('id');
100 $requestedStatus = $command->getField('status');
101
102 /** @var Appointment $appointment */
103 $appointment = $appointmentRepo->getById($appointmentId);
104
105 if ($userAS->isCustomer($user)) {
106 /** @var CustomerBooking $booking */
107 foreach ($appointment->getBookings()->getItems() as $booking) {
108 if (
109 $booking->getCustomerId()->getValue() !== $user->getId()->getValue() &&
110 !$bookingAS->isBookingCanceledOrRejectedOrNoShow($booking->getStatus()->getValue())
111 ) {
112 throw new AccessDeniedException('You are not allowed to update appointment');
113 }
114 }
115 }
116
117 $oldStatus = $appointment->getStatus()->getValue();
118
119 if (
120 $bookingAS->isBookingApprovedOrPending($requestedStatus) &&
121 $bookingAS->isBookingCanceledOrRejectedOrNoShow($appointment->getStatus()->getValue()) &&
122 !$appointmentAS->canBeBooked($appointment, $userAS->isCustomer($user), null, null)
123 ) {
124 $result->setResult(CommandResult::RESULT_ERROR);
125 $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']);
126 $result->setData(
127 [
128 'timeSlotUnavailable' => true,
129 'status' => $appointment->getStatus()->getValue()
130 ]
131 );
132
133 return $result;
134 }
135
136 $oldAppointmentArray = $appointment->toArray();
137
138 $capacity = 0;
139
140 /** @var CustomerBooking $booking */
141 foreach ($appointment->getBookings()->getItems() as $booking) {
142 $booking->setStatus(new BookingStatus($requestedStatus));
143
144 $capacity += $booking->getPersons()->getValue();
145 }
146
147 /** @var Service $service */
148 $service = $bookableAS->getAppointmentService(
149 $appointment->getServiceId()->getValue(),
150 $appointment->getProviderId()->getValue()
151 );
152
153 if (
154 $requestedStatus === BookingStatus::APPROVED &&
155 (
156 (
157 $service->getMaxCapacity()->getValue() === 1 &&
158 $capacity > 1
159 ) || (
160 $service->getMaxCapacity()->getValue() > 1 &&
161 $capacity > $service->getMaxCapacity()->getValue()
162 )
163 )
164 ) {
165 $result->setResult(CommandResult::RESULT_ERROR);
166 $result->setMessage('Appointment status not updated');
167 $result->setData(
168 [
169 Entities::APPOINTMENT => $appointment->toArray(),
170 'bookingsWithChangedStatus' => [],
171 'status' => $appointment->getStatus()->getValue(),
172 'oldStatus' => $appointment->getStatus()->getValue(),
173 'message' => BackendStrings::get('maximum_capacity_reached'),
174 'maximumCapacityReached' => true,
175 ]
176 );
177
178 return $result;
179 }
180
181 $appointment->setStatus(new BookingStatus($requestedStatus));
182
183 $appointmentRepo->beginTransaction();
184
185 do_action('amelia_before_appointment_status_updated', $appointment->toArray(), $requestedStatus);
186
187 $appointmentAS->calculateAndSetAppointmentEnd($appointment, $service);
188
189 $appointmentRepo->updateFieldById(
190 $appointmentId,
191 DateTimeService::getCustomDateTimeObjectInUtc(
192 $appointment->getBookingEnd()->getValue()->format('Y-m-d H:i:s')
193 )->format('Y-m-d H:i:s'),
194 'bookingEnd'
195 );
196
197
198 $bookingRepository->updateFieldByColumn('status', $requestedStatus, 'appointmentId', $appointmentId);
199 $appointmentRepo->updateFieldById($appointmentId, $requestedStatus, 'status');
200
201 $appointmentRepo->commit();
202
203 do_action('amelia_after_appointment_status_updated', $appointment->toArray(), $requestedStatus);
204
205 /** @var CustomerBooking $booking */
206 foreach ($appointment->getBookings()->getItems() as $booking) {
207 if (
208 $booking->getStatus()->getValue() === BookingStatus::APPROVED &&
209 ($appointment->getStatus()->getValue() === BookingStatus::PENDING || $appointment->getStatus()->getValue() === BookingStatus::APPROVED)
210 ) {
211 $booking->setChangedStatus(new BooleanValueObject(true));
212 }
213 }
214
215 $appointmentArray = $appointment->toArray();
216 $bookingsWithChangedStatus = $bookingAS->getBookingsWithChangedStatus($appointmentArray, $oldAppointmentArray);
217
218 // Ensure provider's zoomUserId is included for Zoom integration
219 if (
220 $oldStatus === BookingStatus::PENDING && $requestedStatus === BookingStatus::APPROVED &&
221 $appointment->getProvider() && !$appointment->getProvider()->getZoomUserId()
222 ) {
223 $provider = $providerRepo->getById($appointment->getProvider()->getId()->getValue());
224 if ($provider && $provider->getZoomUserId()) {
225 if (!isset($appointmentArray['provider'])) {
226 $appointmentArray['provider'] = [];
227 }
228 $appointmentArray['provider']['zoomUserId'] = $provider->getZoomUserId()->getValue();
229 }
230 }
231
232 $result->setResult(CommandResult::RESULT_SUCCESS);
233 $result->setMessage('Successfully updated appointment status');
234 $result->setData(
235 [
236 Entities::APPOINTMENT => $appointmentArray,
237 'bookingsWithChangedStatus' => $bookingsWithChangedStatus,
238 'status' => $requestedStatus,
239 'oldStatus' => $oldStatus,
240 'message' =>
241 BackendStrings::get('appointment_status_changed') . strtolower(BackendStrings::get($requestedStatus))
242 ]
243 );
244
245 return $result;
246 }
247 }
248