CalendarRescheduleEventCommand.php
6 months ago
CalendarRescheduleEventCommandHandler.php
6 months ago
GetCalendarEventsCommand.php
6 months ago
GetCalendarEventsCommandHandler.php
6 months ago
GetCalendarSlotAvailabilityCommand.php
6 months ago
GetCalendarSlotAvailabilityHandler.php
6 months ago
GetCalendarSlotEntitiesCommand.php
6 months ago
GetCalendarSlotEntitiesCommandHandler.php
6 months ago
GetCalendarSlotsCommand.php
6 months ago
GetCalendarSlotsCommandHandler.php
6 months ago
CalendarRescheduleEventCommandHandler.php
174 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Application\Commands\Calendar; |
| 9 | |
| 10 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 11 | use AmeliaBooking\Application\Commands\CommandResult; |
| 12 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 13 | use AmeliaBooking\Application\Services\Bookable\BookableApplicationService; |
| 14 | use AmeliaBooking\Application\Services\Booking\AppointmentApplicationService; |
| 15 | use AmeliaBooking\Application\Services\Booking\BookingApplicationService; |
| 16 | use AmeliaBooking\Application\Services\Payment\PaymentApplicationService; |
| 17 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 18 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 19 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 20 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 21 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 22 | use AmeliaBooking\Domain\Entity\Entities; |
| 23 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 24 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 25 | use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface; |
| 26 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 27 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 28 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 29 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 30 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 31 | use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings; |
| 32 | use Interop\Container\Exception\ContainerException; |
| 33 | use Psr\Container\ContainerExceptionInterface; |
| 34 | |
| 35 | class CalendarRescheduleEventCommandHandler extends CommandHandler |
| 36 | { |
| 37 | /** |
| 38 | * @var array |
| 39 | */ |
| 40 | public $mandatoryFields = [ |
| 41 | 'bookingStart', |
| 42 | 'appointmentId' |
| 43 | ]; |
| 44 | |
| 45 | /** |
| 46 | * @throws ContainerException |
| 47 | * @throws InvalidArgumentException |
| 48 | * @throws ContainerExceptionInterface |
| 49 | * @throws QueryExecutionException |
| 50 | */ |
| 51 | public function handle(CalendarRescheduleEventCommand $command): CommandResult |
| 52 | { |
| 53 | $this->checkMandatoryFields($command); |
| 54 | |
| 55 | $result = new CommandResult(); |
| 56 | |
| 57 | /** @var UserApplicationService $userAS */ |
| 58 | $userAS = $this->container->get('application.user.service'); |
| 59 | /** @var AppointmentRepository $appointmentRepo */ |
| 60 | $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); |
| 61 | /** @var AppointmentApplicationService $appointmentAS */ |
| 62 | $appointmentAS = $this->container->get('application.booking.appointment.service'); |
| 63 | /** @var BookableApplicationService $bookableAS */ |
| 64 | $bookableAS = $this->container->get('application.bookable.service'); |
| 65 | /** @var BookingApplicationService $bookingAS */ |
| 66 | $bookingAS = $this->container->get('application.booking.booking.service'); |
| 67 | /** @var PaymentApplicationService $paymentAS */ |
| 68 | $paymentAS = $this->container->get('application.payment.service'); |
| 69 | |
| 70 | try { |
| 71 | /** @var AbstractUser $user */ |
| 72 | $user = $command->getUserApplicationService()->authorization( |
| 73 | $command->getPage() === 'cabinet' ? $command->getToken() : null, |
| 74 | $command->getCabinetType() |
| 75 | ); |
| 76 | } catch (AuthorizationException $e) { |
| 77 | $result->setResult(CommandResult::RESULT_ERROR); |
| 78 | $result->setData( |
| 79 | [ |
| 80 | 'reauthorize' => true |
| 81 | ] |
| 82 | ); |
| 83 | |
| 84 | return $result; |
| 85 | } |
| 86 | |
| 87 | /** @var Appointment $appointment */ |
| 88 | $appointment = $appointmentRepo->getById($command->getField('appointmentId')); |
| 89 | |
| 90 | $initialBookingStart = $appointment->getBookingStart()->getValue(); |
| 91 | $initialBookingEnd = $appointment->getBookingEnd()->getValue(); |
| 92 | |
| 93 | /** @var Service $service */ |
| 94 | $service = $bookableAS->getAppointmentService( |
| 95 | $appointment->getServiceId()->getValue(), |
| 96 | $appointment->getProviderId()->getValue() |
| 97 | ); |
| 98 | |
| 99 | $bookingStart = $command->getField('bookingStart'); |
| 100 | |
| 101 | $bookingStartInUtc = DateTimeService::getCustomDateTimeObject( |
| 102 | $bookingStart |
| 103 | )->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i'); |
| 104 | |
| 105 | $appointment->setBookingStart( |
| 106 | new DateTimeValue( |
| 107 | DateTimeService::getCustomDateTimeObject( |
| 108 | $bookingStart |
| 109 | ) |
| 110 | ) |
| 111 | ); |
| 112 | |
| 113 | $appointment->setBookingEnd( |
| 114 | new DateTimeValue( |
| 115 | DateTimeService::getCustomDateTimeObject($bookingStart) |
| 116 | ->modify('+' . $appointmentAS->getAppointmentLengthTime($appointment, $service) . ' second') |
| 117 | ) |
| 118 | ); |
| 119 | |
| 120 | if (!$appointmentAS->canBeBooked($appointment, $userAS->isCustomer($user), null, null)) { |
| 121 | $result->setResult(CommandResult::RESULT_ERROR); |
| 122 | $result->setMessage(FrontendStrings::getCommonStrings()['time_slot_unavailable']); |
| 123 | $result->setData( |
| 124 | [ |
| 125 | 'timeSlotUnavailable' => true |
| 126 | ] |
| 127 | ); |
| 128 | |
| 129 | return $result; |
| 130 | } |
| 131 | |
| 132 | do_action('amelia_before_booking_rescheduled', $appointment->toArray()); |
| 133 | |
| 134 | $appointmentRepo->update($command->getField('appointmentId'), $appointment); |
| 135 | |
| 136 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 137 | $paymentAS->updateBookingPaymentDate($booking, $bookingStartInUtc); |
| 138 | } |
| 139 | |
| 140 | $appointment->setRescheduled(new BooleanValueObject(true)); |
| 141 | |
| 142 | $bookingAS->bookingRescheduled( |
| 143 | $appointment->getId()->getValue(), |
| 144 | Entities::APPOINTMENT, |
| 145 | null, |
| 146 | Entities::CUSTOMER |
| 147 | ); |
| 148 | |
| 149 | $bookingAS->bookingRescheduled( |
| 150 | $appointment->getId()->getValue(), |
| 151 | Entities::APPOINTMENT, |
| 152 | $appointment->getProviderId()->getValue(), |
| 153 | Entities::PROVIDER |
| 154 | ); |
| 155 | |
| 156 | |
| 157 | do_action('amelia_after_booking_rescheduled', $appointment->toArray()); |
| 158 | |
| 159 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 160 | $result->setMessage('Successfully updated appointment time'); |
| 161 | $result->setData( |
| 162 | [ |
| 163 | Entities::APPOINTMENT => $appointment->toArray(), |
| 164 | 'initialAppointmentDateTime' => [ |
| 165 | 'bookingStart' => $initialBookingStart->format('Y-m-d H:i:s'), |
| 166 | 'bookingEnd' => $initialBookingEnd->format('Y-m-d H:i:s'), |
| 167 | ], |
| 168 | ] |
| 169 | ); |
| 170 | |
| 171 | return $result; |
| 172 | } |
| 173 | } |
| 174 |