ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
UpdateAppointmentTimeCommandHandler.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
UpdateAppointmentTimeCommandHandler.php
236 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\Payment\PaymentApplicationService; |
| 12 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 13 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 14 | use AmeliaBooking\Domain\Common\Exceptions\BookingCancellationException; |
| 15 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 16 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 17 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 18 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 19 | use AmeliaBooking\Domain\Entity\Entities; |
| 20 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 21 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 22 | use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface; |
| 23 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 24 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 25 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 26 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 27 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 28 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 29 | use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings; |
| 30 | use Interop\Container\Exception\ContainerException; |
| 31 | |
| 32 | /** |
| 33 | * Class UpdateAppointmentTimeCommandHandler |
| 34 | * |
| 35 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 36 | */ |
| 37 | class UpdateAppointmentTimeCommandHandler extends CommandHandler |
| 38 | { |
| 39 | /** |
| 40 | * @var array |
| 41 | */ |
| 42 | public $mandatoryFields = [ |
| 43 | 'bookingStart' |
| 44 | ]; |
| 45 | |
| 46 | /** |
| 47 | * @param UpdateAppointmentTimeCommand $command |
| 48 | * |
| 49 | * @return CommandResult |
| 50 | * |
| 51 | * @throws AccessDeniedException |
| 52 | * @throws InvalidArgumentException |
| 53 | * @throws QueryExecutionException |
| 54 | * @throws NotFoundException |
| 55 | * @throws ContainerException |
| 56 | */ |
| 57 | public function handle(UpdateAppointmentTimeCommand $command) |
| 58 | { |
| 59 | $this->checkMandatoryFields($command); |
| 60 | |
| 61 | $result = new CommandResult(); |
| 62 | |
| 63 | /** @var UserApplicationService $userAS */ |
| 64 | $userAS = $this->container->get('application.user.service'); |
| 65 | /** @var SettingsService $settingsDS */ |
| 66 | $settingsDS = $this->container->get('domain.settings.service'); |
| 67 | /** @var AppointmentRepository $appointmentRepo */ |
| 68 | $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); |
| 69 | /** @var AppointmentApplicationService $appointmentAS */ |
| 70 | $appointmentAS = $this->container->get('application.booking.appointment.service'); |
| 71 | /** @var BookableApplicationService $bookableAS */ |
| 72 | $bookableAS = $this->container->get('application.bookable.service'); |
| 73 | /** @var BookingApplicationService $bookingAS */ |
| 74 | $bookingAS = $this->container->get('application.booking.booking.service'); |
| 75 | /** @var ReservationServiceInterface $reservationService */ |
| 76 | $reservationService = $this->container->get('application.reservation.service')->get(Entities::APPOINTMENT); |
| 77 | /** @var PaymentApplicationService $paymentAS */ |
| 78 | $paymentAS = $this->container->get('application.payment.service'); |
| 79 | |
| 80 | try { |
| 81 | /** @var AbstractUser $user */ |
| 82 | $user = $command->getUserApplicationService()->authorization( |
| 83 | $command->getPage() === 'cabinet' ? $command->getToken() : null, |
| 84 | $command->getCabinetType() |
| 85 | ); |
| 86 | } catch (AuthorizationException $e) { |
| 87 | $result->setResult(CommandResult::RESULT_ERROR); |
| 88 | $result->setData( |
| 89 | [ |
| 90 | 'reauthorize' => true |
| 91 | ] |
| 92 | ); |
| 93 | |
| 94 | return $result; |
| 95 | } |
| 96 | |
| 97 | if ($userAS->isCustomer($user) && !$settingsDS->getSetting('roles', 'allowCustomerReschedule')) { |
| 98 | throw new AccessDeniedException('You are not allowed to update appointment'); |
| 99 | } |
| 100 | |
| 101 | /** @var Appointment $appointment */ |
| 102 | $appointment = $appointmentRepo->getById((int)$command->getArg('id')); |
| 103 | |
| 104 | $initialBookingStart = $appointment->getBookingStart()->getValue(); |
| 105 | $initialBookingEnd = $appointment->getBookingEnd()->getValue(); |
| 106 | |
| 107 | /** @var Service $service */ |
| 108 | $service = $bookableAS->getAppointmentService( |
| 109 | $appointment->getServiceId()->getValue(), |
| 110 | $appointment->getProviderId()->getValue() |
| 111 | ); |
| 112 | |
| 113 | /** @var CustomerBooking $booking */ |
| 114 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 115 | if ($userAS->isAmeliaUser($user) && |
| 116 | $userAS->isCustomer($user) && |
| 117 | $bookingAS->isBookingApprovedOrPending($booking->getStatus()->getValue()) && |
| 118 | ($service->getMinCapacity()->getValue() !== 1 || $service->getMaxCapacity()->getValue() !== 1) && |
| 119 | ($user->getId() && $booking->getCustomerId()->getValue() !== $user->getId()->getValue()) |
| 120 | ) { |
| 121 | throw new AccessDeniedException('You are not allowed to update appointment'); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | $minimumRescheduleTimeInSeconds = $settingsDS |
| 126 | ->getEntitySettings($service->getSettings()) |
| 127 | ->getGeneralSettings() |
| 128 | ->getMinimumTimeRequirementPriorToRescheduling(); |
| 129 | |
| 130 | try { |
| 131 | $reservationService->inspectMinimumCancellationTime( |
| 132 | $appointment->getBookingStart()->getValue(), |
| 133 | $minimumRescheduleTimeInSeconds |
| 134 | ); |
| 135 | } catch (BookingCancellationException $e) { |
| 136 | $result->setResult(CommandResult::RESULT_ERROR); |
| 137 | $result->setMessage('You are not allowed to update booking'); |
| 138 | $result->setData( |
| 139 | [ |
| 140 | 'rescheduleBookingUnavailable' => true |
| 141 | ] |
| 142 | ); |
| 143 | |
| 144 | return $result; |
| 145 | } |
| 146 | |
| 147 | $bookingStart = $command->getField('bookingStart'); |
| 148 | |
| 149 | if ($command->getField('timeZone')) { |
| 150 | $bookingStart = DateTimeService::getDateTimeObjectInTimeZone( |
| 151 | $bookingStart, |
| 152 | $command->getField('timeZone') |
| 153 | )->setTimezone(DateTimeService::getTimeZone())->format('Y-m-d H:i:s'); |
| 154 | } |
| 155 | |
| 156 | $bookingStartInUtc = DateTimeService::getCustomDateTimeObject( |
| 157 | $bookingStart |
| 158 | )->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i'); |
| 159 | |
| 160 | // Convert UTC slot to slot in TimeZone based on Settings |
| 161 | if ($command->getField('utcOffset') !== null && $settingsDS->getSetting('general', 'showClientTimeZone')) { |
| 162 | $bookingStart = DateTimeService::getCustomDateTimeFromUtc( |
| 163 | $bookingStart |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | $appointment->setBookingStart( |
| 168 | new DateTimeValue( |
| 169 | DateTimeService::getCustomDateTimeObject( |
| 170 | $bookingStart |
| 171 | ) |
| 172 | ) |
| 173 | ); |
| 174 | |
| 175 | $appointment->setBookingEnd( |
| 176 | new DateTimeValue( |
| 177 | DateTimeService::getCustomDateTimeObject($bookingStart) |
| 178 | ->modify('+' . $appointmentAS->getAppointmentLengthTime($appointment, $service) . ' second') |
| 179 | ) |
| 180 | ); |
| 181 | |
| 182 | if (!$appointmentAS->canBeBooked($appointment, $userAS->isCustomer($user), null, null)) { |
| 183 | $result->setResult(CommandResult::RESULT_ERROR); |
| 184 | $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']); |
| 185 | $result->setData( |
| 186 | [ |
| 187 | 'timeSlotUnavailable' => true |
| 188 | ] |
| 189 | ); |
| 190 | |
| 191 | return $result; |
| 192 | } |
| 193 | |
| 194 | do_action('amelia_before_booking_rescheduled', $appointment->toArray()); |
| 195 | |
| 196 | $appointmentRepo->update((int)$command->getArg('id'), $appointment); |
| 197 | |
| 198 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 199 | $paymentAS->updateBookingPaymentDate($booking, $bookingStartInUtc); |
| 200 | } |
| 201 | |
| 202 | $appointment->setRescheduled(new BooleanValueObject(true)); |
| 203 | |
| 204 | $bookingAS->bookingRescheduled( |
| 205 | $appointment->getId()->getValue(), |
| 206 | Entities::APPOINTMENT, |
| 207 | null, |
| 208 | Entities::CUSTOMER |
| 209 | ); |
| 210 | |
| 211 | $bookingAS->bookingRescheduled( |
| 212 | $appointment->getId()->getValue(), |
| 213 | Entities::APPOINTMENT, |
| 214 | $appointment->getProviderId()->getValue(), |
| 215 | Entities::PROVIDER |
| 216 | ); |
| 217 | |
| 218 | |
| 219 | do_action('amelia_after_booking_rescheduled', $appointment->toArray()); |
| 220 | |
| 221 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 222 | $result->setMessage('Successfully updated appointment time'); |
| 223 | $result->setData( |
| 224 | [ |
| 225 | Entities::APPOINTMENT => $appointment->toArray(), |
| 226 | 'initialAppointmentDateTime' => [ |
| 227 | 'bookingStart' => $initialBookingStart->format('Y-m-d H:i:s'), |
| 228 | 'bookingEnd' => $initialBookingEnd->format('Y-m-d H:i:s'), |
| 229 | ], |
| 230 | ] |
| 231 | ); |
| 232 | |
| 233 | return $result; |
| 234 | } |
| 235 | } |
| 236 |