ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
UpdateAppointmentStatusCommandHandler.php
AddAppointmentCommand.php
7 years ago
AddAppointmentCommandHandler.php
1 year ago
AddBookingCommand.php
7 years ago
AddBookingCommandHandler.php
1 year ago
ApproveBookingRemotelyCommand.php
2 years ago
ApproveBookingRemotelyCommandHandler.php
1 year ago
CancelBookingCommand.php
7 years ago
CancelBookingCommandHandler.php
2 years ago
CancelBookingRemotelyCommand.php
7 years ago
CancelBookingRemotelyCommandHandler.php
2 years ago
DeleteAppointmentCommand.php
7 years ago
DeleteAppointmentCommandHandler.php
2 years ago
DeleteBookingCommand.php
4 years ago
DeleteBookingCommandHandler.php
2 years ago
GetAppointmentCommand.php
7 years ago
GetAppointmentCommandHandler.php
1 year ago
GetAppointmentsCommand.php
7 years ago
GetAppointmentsCommandHandler.php
1 year ago
GetIcsCommand.php
5 years ago
GetIcsCommandHandler.php
4 years ago
GetPackageAppointmentsCommand.php
1 year ago
GetPackageAppointmentsCommandHandler.php
1 year ago
GetTimeSlotsCommand.php
7 years ago
GetTimeSlotsCommandHandler.php
1 year ago
ReassignBookingCommand.php
5 years ago
ReassignBookingCommandHandler.php
1 year ago
RejectBookingRemotelyCommand.php
2 years ago
RejectBookingRemotelyCommandHandler.php
1 year ago
SuccessfulBookingCommand.php
7 years ago
SuccessfulBookingCommandHandler.php
1 year ago
UpdateAppointmentCommand.php
7 years ago
UpdateAppointmentCommandHandler.php
1 year ago
UpdateAppointmentStatusCommand.php
7 years ago
UpdateAppointmentStatusCommandHandler.php
1 year ago
UpdateAppointmentTimeCommand.php
7 years ago
UpdateAppointmentTimeCommandHandler.php
1 year ago
UpdateBookingStatusCommand.php
1 year ago
UpdateBookingStatusCommandHandler.php
1 year ago
UpdateAppointmentStatusCommandHandler.php
227 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\Collection\Collection; |
| 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\Number\Integer\Id; |
| 23 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 24 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 25 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 26 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 27 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 28 | use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings; |
| 29 | use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings; |
| 30 | use Interop\Container\Exception\ContainerException; |
| 31 | |
| 32 | /** |
| 33 | * Class UpdateAppointmentStatusCommandHandler |
| 34 | * |
| 35 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 36 | */ |
| 37 | class UpdateAppointmentStatusCommandHandler extends CommandHandler |
| 38 | { |
| 39 | /** |
| 40 | * @var array |
| 41 | */ |
| 42 | public $mandatoryFields = [ |
| 43 | 'status' |
| 44 | ]; |
| 45 | |
| 46 | /** |
| 47 | * @param UpdateAppointmentStatusCommand $command |
| 48 | * |
| 49 | * @return CommandResult |
| 50 | * |
| 51 | * @throws AccessDeniedException |
| 52 | * @throws InvalidArgumentException |
| 53 | * @throws QueryExecutionException |
| 54 | * @throws ContainerException |
| 55 | * @throws NotFoundException |
| 56 | */ |
| 57 | public function handle(UpdateAppointmentStatusCommand $command) |
| 58 | { |
| 59 | if (!$command->getPermissionService()->currentUserCanWriteStatus(Entities::APPOINTMENTS)) { |
| 60 | throw new AccessDeniedException('You are not allowed to update appointment status'); |
| 61 | } |
| 62 | |
| 63 | $result = new CommandResult(); |
| 64 | |
| 65 | $this->checkMandatoryFields($command); |
| 66 | |
| 67 | /** @var CustomerBookingRepository $bookingRepository */ |
| 68 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 69 | /** @var AppointmentRepository $appointmentRepo */ |
| 70 | $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); |
| 71 | /** @var BookingApplicationService $bookingAS */ |
| 72 | $bookingAS = $this->container->get('application.booking.booking.service'); |
| 73 | /** @var UserApplicationService $userAS */ |
| 74 | $userAS = $command->getUserApplicationService(); |
| 75 | /** @var AppointmentApplicationService $appointmentAS */ |
| 76 | $appointmentAS = $this->container->get('application.booking.appointment.service'); |
| 77 | /** @var BookableApplicationService $bookableAS */ |
| 78 | $bookableAS = $this->container->get('application.bookable.service'); |
| 79 | |
| 80 | $appointmentId = (int)$command->getArg('id'); |
| 81 | $requestedStatus = $command->getField('status'); |
| 82 | |
| 83 | /** @var Appointment $appointment */ |
| 84 | $appointment = $appointmentRepo->getById($appointmentId); |
| 85 | |
| 86 | $packageCustomerId = $command->getField('packageCustomerId'); |
| 87 | |
| 88 | if ($packageCustomerId) { |
| 89 | $appArray = array_filter( |
| 90 | $appointment->getBookings()->getItems(), |
| 91 | function ($booking) use ($packageCustomerId) { |
| 92 | /** @var Id $pcId */ |
| 93 | $pcId = $booking->getPackageCustomerService() ? |
| 94 | $booking->getPackageCustomerService()->getPackageCustomer()->getId() : null; |
| 95 | |
| 96 | return isset($pcId) && $pcId->getValue() === $packageCustomerId; |
| 97 | } |
| 98 | ); |
| 99 | |
| 100 | $appointment->setBookings(new Collection($appArray)); |
| 101 | } |
| 102 | |
| 103 | $oldStatus = $appointment->getStatus()->getValue(); |
| 104 | |
| 105 | if ($bookingAS->isBookingApprovedOrPending($requestedStatus) && |
| 106 | $bookingAS->isBookingCanceledOrRejectedOrNoShow($appointment->getStatus()->getValue()) |
| 107 | ) { |
| 108 | /** @var AbstractUser $user */ |
| 109 | $user = $this->container->get('logged.in.user'); |
| 110 | |
| 111 | if (!$appointmentAS->canBeBooked($appointment, $userAS->isCustomer($user), null, null)) { |
| 112 | $result->setResult(CommandResult::RESULT_ERROR); |
| 113 | $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']); |
| 114 | $result->setData( |
| 115 | [ |
| 116 | 'timeSlotUnavailable' => true, |
| 117 | 'status' => $appointment->getStatus()->getValue() |
| 118 | ] |
| 119 | ); |
| 120 | |
| 121 | return $result; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | $oldAppointmentArray = $appointment->toArray(); |
| 126 | |
| 127 | /** @var CustomerBooking $booking */ |
| 128 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 129 | $booking->setStatus(new BookingStatus($requestedStatus)); |
| 130 | } |
| 131 | |
| 132 | /** @var Service $service */ |
| 133 | $service = $bookableAS->getAppointmentService( |
| 134 | $appointment->getServiceId()->getValue(), |
| 135 | $appointment->getProviderId()->getValue() |
| 136 | ); |
| 137 | |
| 138 | if ($requestedStatus === BookingStatus::APPROVED && |
| 139 | ( |
| 140 | ( |
| 141 | $service->getMaxCapacity()->getValue() === 1 && |
| 142 | $appointment->getBookings()->length() > 1 |
| 143 | ) || ( |
| 144 | $service->getMaxCapacity()->getValue() > 1 && |
| 145 | $appointment->getBookings()->length() > $service->getMaxCapacity()->getValue() |
| 146 | ) |
| 147 | ) |
| 148 | ) { |
| 149 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 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::getEventStrings()['maximum_capacity_reached'], |
| 158 | ] |
| 159 | ); |
| 160 | |
| 161 | return $result; |
| 162 | } |
| 163 | |
| 164 | $appointment->setStatus(new BookingStatus($requestedStatus)); |
| 165 | |
| 166 | $appointmentRepo->beginTransaction(); |
| 167 | |
| 168 | do_action('amelia_before_appointment_status_updated', $appointment->toArray(), $requestedStatus); |
| 169 | |
| 170 | $appointment->setBookingEnd( |
| 171 | new DateTimeValue( |
| 172 | DateTimeService::getCustomDateTimeObject( |
| 173 | $appointment->getBookingStart()->getValue()->format('Y-m-d H:i:s') |
| 174 | )->modify('+' . $appointmentAS->getAppointmentLengthTime($appointment, $service) . ' second') |
| 175 | ) |
| 176 | ); |
| 177 | |
| 178 | $appointmentRepo->updateFieldById($appointmentId, DateTimeService::getCustomDateTimeObjectInUtc($appointment->getBookingEnd()->getValue()->format('Y-m-d H:i:s'))->format('Y-m-d H:i:s'), 'bookingEnd'); |
| 179 | |
| 180 | |
| 181 | if ($packageCustomerId) { |
| 182 | /** @var CustomerBooking $booking */ |
| 183 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 184 | $bookingRepository->updateStatusById($booking->getId()->getValue(), $requestedStatus); |
| 185 | } |
| 186 | |
| 187 | if ($appointment->getBookings()->length() === 1) { |
| 188 | $appointmentRepo->updateStatusById($appointmentId, $requestedStatus); |
| 189 | } |
| 190 | } else { |
| 191 | $bookingRepository->updateStatusByAppointmentId($appointmentId, $requestedStatus); |
| 192 | $appointmentRepo->updateStatusById($appointmentId, $requestedStatus); |
| 193 | } |
| 194 | |
| 195 | $appointmentRepo->commit(); |
| 196 | |
| 197 | do_action('amelia_after_appointment_status_updated', $appointment->toArray(), $requestedStatus); |
| 198 | |
| 199 | /** @var CustomerBooking $booking */ |
| 200 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 201 | if ($booking->getStatus()->getValue() === BookingStatus::APPROVED && |
| 202 | ($appointment->getStatus()->getValue() === BookingStatus::PENDING || $appointment->getStatus()->getValue() === BookingStatus::APPROVED) |
| 203 | ) { |
| 204 | $booking->setChangedStatus(new BooleanValueObject(true)); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | $appointmentArray = $appointment->toArray(); |
| 209 | $bookingsWithChangedStatus = $bookingAS->getBookingsWithChangedStatus($appointmentArray, $oldAppointmentArray); |
| 210 | |
| 211 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 212 | $result->setMessage('Successfully updated appointment status'); |
| 213 | $result->setData( |
| 214 | [ |
| 215 | Entities::APPOINTMENT => $appointmentArray, |
| 216 | 'bookingsWithChangedStatus' => $bookingsWithChangedStatus, |
| 217 | 'status' => $requestedStatus, |
| 218 | 'oldStatus' => $oldStatus, |
| 219 | 'message' => |
| 220 | BackendStrings::getAppointmentStrings()['appointment_status_changed'] . strtolower(BackendStrings::getCommonStrings()[$requestedStatus]) |
| 221 | ] |
| 222 | ); |
| 223 | |
| 224 | return $result; |
| 225 | } |
| 226 | } |
| 227 |