ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
UpdateAppointmentStatusCommandHandler.php
AddAppointmentCommand.php
1 year ago
AddAppointmentCommandHandler.php
2 months ago
AddBookingCommand.php
1 year ago
AddBookingCommandHandler.php
1 year ago
ApproveBookingRemotelyCommand.php
1 year ago
ApproveBookingRemotelyCommandHandler.php
2 months ago
CancelBookingCommand.php
1 year ago
CancelBookingCommandHandler.php
6 months ago
CancelBookingRemotelyCommand.php
1 year ago
CancelBookingRemotelyCommandHandler.php
1 month 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
2 months ago
GetAppointmentCommand.php
7 years ago
GetAppointmentCommandHandler.php
2 weeks ago
GetAppointmentsCommand.php
1 year ago
GetAppointmentsCommandHandler.php
2 weeks ago
GetIcsCommand.php
6 months ago
GetIcsCommandHandler.php
4 years ago
GetTimeSlotsCommand.php
1 year ago
GetTimeSlotsCommandHandler.php
1 month ago
ReassignBookingCommand.php
1 year ago
ReassignBookingCommandHandler.php
3 months ago
RejectBookingRemotelyCommand.php
1 year ago
RejectBookingRemotelyCommandHandler.php
4 months ago
SuccessfulBookingCommand.php
1 year ago
SuccessfulBookingCommandHandler.php
1 year ago
UpdateAppointmentCommand.php
1 year ago
UpdateAppointmentCommandHandler.php
3 months ago
UpdateAppointmentNoteCommand.php
3 months ago
UpdateAppointmentNoteCommandHandler.php
3 months ago
UpdateAppointmentStatusCommand.php
1 year ago
UpdateAppointmentStatusCommandHandler.php
2 months ago
UpdateAppointmentTimeCommand.php
1 year ago
UpdateAppointmentTimeCommandHandler.php
1 month ago
UpdateBookingStatusCommand.php
1 year ago
UpdateBookingStatusCommandHandler.php
5 months ago
UpdateAppointmentStatusCommandHandler.php
250 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 | $appointment->setService($service); |
| 154 | |
| 155 | if ( |
| 156 | $requestedStatus === BookingStatus::APPROVED && |
| 157 | ( |
| 158 | ( |
| 159 | $service->getMaxCapacity()->getValue() === 1 && |
| 160 | $capacity > 1 |
| 161 | ) || ( |
| 162 | $service->getMaxCapacity()->getValue() > 1 && |
| 163 | $capacity > $service->getMaxCapacity()->getValue() |
| 164 | ) |
| 165 | ) |
| 166 | ) { |
| 167 | $result->setResult(CommandResult::RESULT_ERROR); |
| 168 | $result->setMessage('Appointment status not updated'); |
| 169 | $result->setData( |
| 170 | [ |
| 171 | Entities::APPOINTMENT => $appointment->toArray(), |
| 172 | 'bookingsWithChangedStatus' => [], |
| 173 | 'status' => $appointment->getStatus()->getValue(), |
| 174 | 'oldStatus' => $appointment->getStatus()->getValue(), |
| 175 | 'message' => BackendStrings::get('maximum_capacity_reached'), |
| 176 | 'maximumCapacityReached' => true, |
| 177 | ] |
| 178 | ); |
| 179 | |
| 180 | return $result; |
| 181 | } |
| 182 | |
| 183 | $appointment->setStatus(new BookingStatus($requestedStatus)); |
| 184 | |
| 185 | $appointmentRepo->beginTransaction(); |
| 186 | |
| 187 | do_action('amelia_before_appointment_status_updated', $appointment->toArray(), $requestedStatus); |
| 188 | |
| 189 | $appointmentAS->calculateAndSetAppointmentEnd($appointment, $service); |
| 190 | |
| 191 | $appointmentRepo->updateFieldById( |
| 192 | $appointmentId, |
| 193 | DateTimeService::getCustomDateTimeObjectInUtc( |
| 194 | $appointment->getBookingEnd()->getValue()->format('Y-m-d H:i:s') |
| 195 | )->format('Y-m-d H:i:s'), |
| 196 | 'bookingEnd' |
| 197 | ); |
| 198 | |
| 199 | |
| 200 | $bookingRepository->updateFieldByColumn('status', $requestedStatus, 'appointmentId', $appointmentId); |
| 201 | $appointmentRepo->updateFieldById($appointmentId, $requestedStatus, 'status'); |
| 202 | |
| 203 | $appointmentRepo->commit(); |
| 204 | |
| 205 | do_action('amelia_after_appointment_status_updated', $appointment->toArray(), $requestedStatus); |
| 206 | |
| 207 | /** @var CustomerBooking $booking */ |
| 208 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 209 | if ( |
| 210 | $booking->getStatus()->getValue() === BookingStatus::APPROVED && |
| 211 | ($appointment->getStatus()->getValue() === BookingStatus::PENDING || $appointment->getStatus()->getValue() === BookingStatus::APPROVED) |
| 212 | ) { |
| 213 | $booking->setChangedStatus(new BooleanValueObject(true)); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | $appointmentArray = $appointment->toArray(); |
| 218 | $bookingsWithChangedStatus = $bookingAS->getBookingsWithChangedStatus($appointmentArray, $oldAppointmentArray); |
| 219 | |
| 220 | // Ensure provider's zoomUserId is included for Zoom integration |
| 221 | if ( |
| 222 | $oldStatus === BookingStatus::PENDING && $requestedStatus === BookingStatus::APPROVED && |
| 223 | $appointment->getProvider() && !$appointment->getProvider()->getZoomUserId() |
| 224 | ) { |
| 225 | $provider = $providerRepo->getById($appointment->getProvider()->getId()->getValue()); |
| 226 | if ($provider && $provider->getZoomUserId()) { |
| 227 | if (!isset($appointmentArray['provider'])) { |
| 228 | $appointmentArray['provider'] = []; |
| 229 | } |
| 230 | $appointmentArray['provider']['zoomUserId'] = $provider->getZoomUserId()->getValue(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 235 | $result->setMessage('Successfully updated appointment status'); |
| 236 | $result->setData( |
| 237 | [ |
| 238 | Entities::APPOINTMENT => $appointmentArray, |
| 239 | 'bookingsWithChangedStatus' => $bookingsWithChangedStatus, |
| 240 | 'status' => $requestedStatus, |
| 241 | 'oldStatus' => $oldStatus, |
| 242 | 'message' => |
| 243 | BackendStrings::get('appointment_status_changed') . strtolower(BackendStrings::get($requestedStatus)) |
| 244 | ] |
| 245 | ); |
| 246 | |
| 247 | return $result; |
| 248 | } |
| 249 | } |
| 250 |