PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.9
Booking for Appointments and Events Calendar – Amelia v2.4.9
2.4.9 2.4.8 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 3 weeks ago AddBookingCommand.php 1 year ago AddBookingCommandHandler.php 1 week ago ApproveBookingRemotelyCommand.php 1 year ago ApproveBookingRemotelyCommandHandler.php 4 months ago CancelBookingCommand.php 1 year ago CancelBookingCommandHandler.php 3 weeks 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 1 year ago DeleteBookingRemotelyCommand.php 1 year ago DeleteBookingRemotelyCommandHandler.php 1 week ago GetAppointmentBookingsCommand.php 8 months ago GetAppointmentBookingsCommandHandler.php 3 weeks ago GetAppointmentCommand.php 7 years ago GetAppointmentCommandHandler.php 3 weeks ago GetAppointmentsCommand.php 1 year ago GetAppointmentsCommandHandler.php 3 weeks ago GetIcsCommand.php 8 months ago GetIcsCommandHandler.php 4 years ago GetTimeSlotsCommand.php 1 year ago GetTimeSlotsCommandHandler.php 1 week ago ReassignBookingCommand.php 1 year ago ReassignBookingCommandHandler.php 3 weeks ago RejectBookingRemotelyCommand.php 1 year ago RejectBookingRemotelyCommandHandler.php 6 months ago SuccessfulBookingCommand.php 1 year ago SuccessfulBookingCommandHandler.php 1 week ago UpdateAppointmentCommand.php 1 year ago UpdateAppointmentCommandHandler.php 1 week ago UpdateAppointmentNoteCommand.php 4 months ago UpdateAppointmentNoteCommandHandler.php 3 weeks ago UpdateAppointmentStatusCommand.php 1 year ago UpdateAppointmentStatusCommandHandler.php 1 week ago UpdateAppointmentTimeCommand.php 1 year ago UpdateAppointmentTimeCommandHandler.php 3 weeks ago UpdateBookingStatusCommand.php 1 year ago UpdateBookingStatusCommandHandler.php 7 months ago
UpdateAppointmentStatusCommandHandler.php
220 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->authorizeAppointmentStatusWrite();
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 $oldStatus = $appointment->getStatus()->getValue();
88
89 if (
90 $bookingAS->isBookingApprovedOrPending($requestedStatus) &&
91 $bookingAS->isBookingCanceledOrRejectedOrNoShow($appointment->getStatus()->getValue()) &&
92 !$appointmentAS->canBeBooked($appointment, $userAS->isCustomer($user), null, null)
93 ) {
94 $result->setResult(CommandResult::RESULT_ERROR);
95 $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']);
96 $result->setData(
97 [
98 'timeSlotUnavailable' => true,
99 'status' => $appointment->getStatus()->getValue()
100 ]
101 );
102
103 return $result;
104 }
105
106 $oldAppointmentArray = $appointment->toArray();
107
108 $capacity = 0;
109
110 /** @var CustomerBooking $booking */
111 foreach ($appointment->getBookings()->getItems() as $booking) {
112 $booking->setStatus(new BookingStatus($requestedStatus));
113
114 $capacity += $booking->getPersons()->getValue();
115 }
116
117 /** @var Service $service */
118 $service = $bookableAS->getAppointmentService(
119 $appointment->getServiceId()->getValue(),
120 $appointment->getProviderId()->getValue()
121 );
122
123 $appointment->setService($service);
124
125 if (
126 $requestedStatus === BookingStatus::APPROVED &&
127 (
128 (
129 $service->getMaxCapacity()->getValue() === 1 &&
130 $capacity > 1
131 ) || (
132 $service->getMaxCapacity()->getValue() > 1 &&
133 $capacity > $service->getMaxCapacity()->getValue()
134 )
135 )
136 ) {
137 $result->setResult(CommandResult::RESULT_ERROR);
138 $result->setMessage('Appointment status not updated');
139 $result->setData(
140 [
141 Entities::APPOINTMENT => $appointment->toArray(),
142 'bookingsWithChangedStatus' => [],
143 'status' => $appointment->getStatus()->getValue(),
144 'oldStatus' => $appointment->getStatus()->getValue(),
145 'message' => BackendStrings::get('maximum_capacity_reached'),
146 'maximumCapacityReached' => true,
147 ]
148 );
149
150 return $result;
151 }
152
153 $appointment->setStatus(new BookingStatus($requestedStatus));
154
155 $appointmentRepo->beginTransaction();
156
157 do_action('amelia_before_appointment_status_updated', $appointment->toArray(), $requestedStatus);
158
159 $appointmentAS->calculateAndSetAppointmentEnd($appointment, $service);
160
161 $appointmentRepo->updateFieldById(
162 $appointmentId,
163 DateTimeService::getCustomDateTimeObjectInUtc(
164 $appointment->getBookingEnd()->getValue()->format('Y-m-d H:i:s')
165 )->format('Y-m-d H:i:s'),
166 'bookingEnd'
167 );
168
169
170 $bookingRepository->updateFieldByColumn('status', $requestedStatus, 'appointmentId', $appointmentId);
171 $appointmentRepo->updateFieldById($appointmentId, $requestedStatus, 'status');
172
173 $appointmentRepo->commit();
174
175 do_action('amelia_after_appointment_status_updated', $appointment->toArray(), $requestedStatus);
176
177 /** @var CustomerBooking $booking */
178 foreach ($appointment->getBookings()->getItems() as $booking) {
179 if (
180 $booking->getStatus()->getValue() === BookingStatus::APPROVED &&
181 ($appointment->getStatus()->getValue() === BookingStatus::PENDING || $appointment->getStatus()->getValue() === BookingStatus::APPROVED)
182 ) {
183 $booking->setChangedStatus(new BooleanValueObject(true));
184 }
185 }
186
187 $appointmentArray = $appointment->toArray();
188 $bookingsWithChangedStatus = $bookingAS->getBookingsWithChangedStatus($appointmentArray, $oldAppointmentArray);
189
190 // Ensure provider's zoomUserId is included for Zoom integration
191 if (
192 $oldStatus === BookingStatus::PENDING && $requestedStatus === BookingStatus::APPROVED &&
193 $appointment->getProvider() && !$appointment->getProvider()->getZoomUserId()
194 ) {
195 $provider = $providerRepo->getById($appointment->getProvider()->getId()->getValue());
196 if ($provider && $provider->getZoomUserId()) {
197 if (!isset($appointmentArray['provider'])) {
198 $appointmentArray['provider'] = [];
199 }
200 $appointmentArray['provider']['zoomUserId'] = $provider->getZoomUserId()->getValue();
201 }
202 }
203
204 $result->setResult(CommandResult::RESULT_SUCCESS);
205 $result->setMessage('Successfully updated appointment status');
206 $result->setData(
207 [
208 Entities::APPOINTMENT => $appointmentArray,
209 'bookingsWithChangedStatus' => $bookingsWithChangedStatus,
210 'status' => $requestedStatus,
211 'oldStatus' => $oldStatus,
212 'message' =>
213 BackendStrings::get('appointment_status_changed') . strtolower(BackendStrings::get($requestedStatus))
214 ]
215 );
216
217 return $result;
218 }
219 }
220