ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
UpdateAppointmentStatusCommandHandler.php
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
DeleteBookingRemotelyCommand.php
1 year ago
DeleteBookingRemotelyCommandHandler.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
UpdateAppointmentStatusCommandHandler.php
226 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\DateTime\DateTimeValue; |
| 22 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 23 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 24 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 25 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 26 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 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 | $command->getUserApplicationService()->authorization( |
| 63 | $command->getPage() === 'cabinet' ? $command->getToken() : null, |
| 64 | $command->getCabinetType() |
| 65 | ); |
| 66 | } catch (AuthorizationException $e) { |
| 67 | $result->setResult(CommandResult::RESULT_ERROR); |
| 68 | $result->setData( |
| 69 | [ |
| 70 | 'reauthorize' => true |
| 71 | ] |
| 72 | ); |
| 73 | |
| 74 | return $result; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | $this->checkMandatoryFields($command); |
| 79 | |
| 80 | /** @var CustomerBookingRepository $bookingRepository */ |
| 81 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 82 | /** @var AppointmentRepository $appointmentRepo */ |
| 83 | $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); |
| 84 | /** @var BookingApplicationService $bookingAS */ |
| 85 | $bookingAS = $this->container->get('application.booking.booking.service'); |
| 86 | /** @var UserApplicationService $userAS */ |
| 87 | $userAS = $command->getUserApplicationService(); |
| 88 | /** @var AppointmentApplicationService $appointmentAS */ |
| 89 | $appointmentAS = $this->container->get('application.booking.appointment.service'); |
| 90 | /** @var BookableApplicationService $bookableAS */ |
| 91 | $bookableAS = $this->container->get('application.bookable.service'); |
| 92 | |
| 93 | $appointmentId = (int)$command->getArg('id'); |
| 94 | $requestedStatus = $command->getField('status'); |
| 95 | |
| 96 | /** @var Appointment $appointment */ |
| 97 | $appointment = $appointmentRepo->getById($appointmentId); |
| 98 | |
| 99 | $oldStatus = $appointment->getStatus()->getValue(); |
| 100 | |
| 101 | if ( |
| 102 | $bookingAS->isBookingApprovedOrPending($requestedStatus) && |
| 103 | $bookingAS->isBookingCanceledOrRejectedOrNoShow($appointment->getStatus()->getValue()) |
| 104 | ) { |
| 105 | /** @var AbstractUser $user */ |
| 106 | $user = $this->container->get('logged.in.user'); |
| 107 | |
| 108 | if (!$appointmentAS->canBeBooked($appointment, $userAS->isCustomer($user), null, null)) { |
| 109 | $result->setResult(CommandResult::RESULT_ERROR); |
| 110 | $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']); |
| 111 | $result->setData( |
| 112 | [ |
| 113 | 'timeSlotUnavailable' => true, |
| 114 | 'status' => $appointment->getStatus()->getValue() |
| 115 | ] |
| 116 | ); |
| 117 | |
| 118 | return $result; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | $oldAppointmentArray = $appointment->toArray(); |
| 123 | |
| 124 | $capacity = 0; |
| 125 | |
| 126 | /** @var CustomerBooking $booking */ |
| 127 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 128 | $booking->setStatus(new BookingStatus($requestedStatus)); |
| 129 | |
| 130 | $capacity += $booking->getPersons()->getValue(); |
| 131 | } |
| 132 | |
| 133 | /** @var Service $service */ |
| 134 | $service = $bookableAS->getAppointmentService( |
| 135 | $appointment->getServiceId()->getValue(), |
| 136 | $appointment->getProviderId()->getValue() |
| 137 | ); |
| 138 | |
| 139 | if ( |
| 140 | $requestedStatus === BookingStatus::APPROVED && |
| 141 | ( |
| 142 | ( |
| 143 | $service->getMaxCapacity()->getValue() === 1 && |
| 144 | $capacity > 1 |
| 145 | ) || ( |
| 146 | $service->getMaxCapacity()->getValue() > 1 && |
| 147 | $capacity > $service->getMaxCapacity()->getValue() |
| 148 | ) |
| 149 | ) |
| 150 | ) { |
| 151 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 152 | $result->setMessage('Appointment status not updated'); |
| 153 | $result->setData( |
| 154 | [ |
| 155 | Entities::APPOINTMENT => $appointment->toArray(), |
| 156 | 'bookingsWithChangedStatus' => [], |
| 157 | 'status' => $appointment->getStatus()->getValue(), |
| 158 | 'oldStatus' => $appointment->getStatus()->getValue(), |
| 159 | 'message' => BackendStrings::getEventStrings()['maximum_capacity_reached'], |
| 160 | 'maximumCapacityReached' => true, |
| 161 | ] |
| 162 | ); |
| 163 | |
| 164 | return $result; |
| 165 | } |
| 166 | |
| 167 | $appointment->setStatus(new BookingStatus($requestedStatus)); |
| 168 | |
| 169 | $appointmentRepo->beginTransaction(); |
| 170 | |
| 171 | do_action('amelia_before_appointment_status_updated', $appointment->toArray(), $requestedStatus); |
| 172 | |
| 173 | $appointment->setBookingEnd( |
| 174 | new DateTimeValue( |
| 175 | DateTimeService::getCustomDateTimeObject( |
| 176 | $appointment->getBookingStart()->getValue()->format('Y-m-d H:i:s') |
| 177 | )->modify('+' . $appointmentAS->getAppointmentLengthTime($appointment, $service) . ' second') |
| 178 | ) |
| 179 | ); |
| 180 | |
| 181 | $appointmentRepo->updateFieldById( |
| 182 | $appointmentId, |
| 183 | DateTimeService::getCustomDateTimeObjectInUtc( |
| 184 | $appointment->getBookingEnd()->getValue()->format('Y-m-d H:i:s') |
| 185 | )->format('Y-m-d H:i:s'), |
| 186 | 'bookingEnd' |
| 187 | ); |
| 188 | |
| 189 | |
| 190 | $bookingRepository->updateStatusByAppointmentId($appointmentId, $requestedStatus); |
| 191 | $appointmentRepo->updateStatusById($appointmentId, $requestedStatus); |
| 192 | |
| 193 | $appointmentRepo->commit(); |
| 194 | |
| 195 | do_action('amelia_after_appointment_status_updated', $appointment->toArray(), $requestedStatus); |
| 196 | |
| 197 | /** @var CustomerBooking $booking */ |
| 198 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 199 | if ( |
| 200 | $booking->getStatus()->getValue() === BookingStatus::APPROVED && |
| 201 | ($appointment->getStatus()->getValue() === BookingStatus::PENDING || $appointment->getStatus()->getValue() === BookingStatus::APPROVED) |
| 202 | ) { |
| 203 | $booking->setChangedStatus(new BooleanValueObject(true)); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | $appointmentArray = $appointment->toArray(); |
| 208 | $bookingsWithChangedStatus = $bookingAS->getBookingsWithChangedStatus($appointmentArray, $oldAppointmentArray); |
| 209 | |
| 210 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 211 | $result->setMessage('Successfully updated appointment status'); |
| 212 | $result->setData( |
| 213 | [ |
| 214 | Entities::APPOINTMENT => $appointmentArray, |
| 215 | 'bookingsWithChangedStatus' => $bookingsWithChangedStatus, |
| 216 | 'status' => $requestedStatus, |
| 217 | 'oldStatus' => $oldStatus, |
| 218 | 'message' => |
| 219 | BackendStrings::getAppointmentStrings()['appointment_status_changed'] . strtolower(BackendStrings::getCommonStrings()[$requestedStatus]) |
| 220 | ] |
| 221 | ); |
| 222 | |
| 223 | return $result; |
| 224 | } |
| 225 | } |
| 226 |