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