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
AddBookingCommandHandler.php
199 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\Services\Booking\BookingApplicationService; |
| 8 | use AmeliaBooking\Application\Services\Helper\HelperService; |
| 9 | use AmeliaBooking\Application\Services\Reservation\AbstractReservationService; |
| 10 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 11 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 12 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 13 | use AmeliaBooking\Domain\Entity\Booking\Reservation; |
| 14 | use AmeliaBooking\Domain\Entity\Entities; |
| 15 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 16 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 17 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 18 | use Exception; |
| 19 | use Interop\Container\Exception\ContainerException; |
| 20 | use Slim\Exception\ContainerValueNotFoundException; |
| 21 | |
| 22 | /** |
| 23 | * Class AddBookingCommandHandler |
| 24 | * |
| 25 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 26 | */ |
| 27 | class AddBookingCommandHandler extends CommandHandler |
| 28 | { |
| 29 | /** |
| 30 | * @var array |
| 31 | */ |
| 32 | public $mandatoryFields = [ |
| 33 | 'bookings', |
| 34 | ]; |
| 35 | |
| 36 | /** |
| 37 | * @param AddBookingCommand $command |
| 38 | * |
| 39 | * @return CommandResult |
| 40 | * @throws ContainerValueNotFoundException |
| 41 | * @throws InvalidArgumentException |
| 42 | * @throws QueryExecutionException |
| 43 | * @throws ContainerException |
| 44 | * @throws Exception |
| 45 | */ |
| 46 | public function handle(AddBookingCommand $command) |
| 47 | { |
| 48 | $this->checkMandatoryFields($command); |
| 49 | |
| 50 | /** @var AbstractReservationService $reservationService */ |
| 51 | $reservationService = $this->container->get('application.reservation.service')->get( |
| 52 | $command->getField('type') ?: Entities::APPOINTMENT |
| 53 | ); |
| 54 | |
| 55 | /** @var BookingApplicationService $bookingAS */ |
| 56 | $bookingAS = $this->container->get('application.booking.booking.service'); |
| 57 | |
| 58 | $appointmentData = $bookingAS->getAppointmentData($command->getFields()); |
| 59 | |
| 60 | $appointmentData = apply_filters('amelia_before_booking_added_filter', $appointmentData); |
| 61 | |
| 62 | do_action('amelia_before_booking_added', $appointmentData); |
| 63 | |
| 64 | $validateCoupon = true; |
| 65 | |
| 66 | if ( |
| 67 | $command->getField('validateCoupon') === false && |
| 68 | $this->getContainer()->getPermissionsService()->currentUserCanWrite(Entities::COUPONS) |
| 69 | ) { |
| 70 | $validateCoupon = false; |
| 71 | } |
| 72 | |
| 73 | /** @var Reservation $reservation */ |
| 74 | $reservation = $reservationService->getNew( |
| 75 | $validateCoupon, |
| 76 | true, |
| 77 | true |
| 78 | ); |
| 79 | |
| 80 | /** @var UserApplicationService $userAS */ |
| 81 | $userAS = $this->container->get('application.user.service'); |
| 82 | |
| 83 | if ( |
| 84 | $command->getToken() && |
| 85 | $command->getPage() === 'cabinet' && |
| 86 | $command->getCabinetType() === Entities::PROVIDER |
| 87 | ) { |
| 88 | try { |
| 89 | /** @var AbstractUser $user */ |
| 90 | $user = $userAS->authorization( |
| 91 | $command->getToken(), |
| 92 | Entities::PROVIDER |
| 93 | ); |
| 94 | |
| 95 | $reservation->setLoggedInUser($user); |
| 96 | } catch (AuthorizationException $e) { |
| 97 | $result = new CommandResult(); |
| 98 | |
| 99 | $result->setResult(CommandResult::RESULT_ERROR); |
| 100 | $result->setData( |
| 101 | [ |
| 102 | 'reauthorize' => true |
| 103 | ] |
| 104 | ); |
| 105 | |
| 106 | return $result; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if ($command->getToken() && !empty($appointmentData['bookings'][0]['packageCustomerService']['id'])) { |
| 111 | try { |
| 112 | /** @var AbstractUser $user */ |
| 113 | $user = $userAS->authorization( |
| 114 | $command->getToken(), |
| 115 | Entities::CUSTOMER |
| 116 | ); |
| 117 | |
| 118 | $reservation->setLoggedInUser($user); |
| 119 | } catch (AuthorizationException $e) { |
| 120 | $result = new CommandResult(); |
| 121 | |
| 122 | $result->setResult(CommandResult::RESULT_ERROR); |
| 123 | $result->setData( |
| 124 | [ |
| 125 | 'reauthorize' => true |
| 126 | ] |
| 127 | ); |
| 128 | |
| 129 | return $result; |
| 130 | } |
| 131 | |
| 132 | if ($user->getId()->getValue() !== (int)$appointmentData['bookings'][0]['customer']['id']) { |
| 133 | $result = new CommandResult(); |
| 134 | |
| 135 | $result->setResult(CommandResult::RESULT_ERROR); |
| 136 | |
| 137 | return $result; |
| 138 | } |
| 139 | |
| 140 | $appointmentData['payment'] = null; |
| 141 | |
| 142 | $appointmentData['isCabinetBooking'] = true; |
| 143 | } elseif (!empty($appointmentData['bookings'][0]['packageCustomerService']['id'])) { |
| 144 | /** @var AbstractUser $user */ |
| 145 | $user = $this->container->get('logged.in.user'); |
| 146 | |
| 147 | if ($user && $user->getType() === AbstractUser::USER_ROLE_ADMIN) { |
| 148 | $appointmentData['payment'] = null; |
| 149 | |
| 150 | $appointmentData['isCabinetBooking'] = true; |
| 151 | } |
| 152 | } else { |
| 153 | $appointmentData['isCabinetBooking'] = false; |
| 154 | |
| 155 | unset($appointmentData['bookings'][0]['packageCustomerService']['id']); |
| 156 | } |
| 157 | |
| 158 | $result = $reservationService->processRequest($appointmentData, $reservation, true); |
| 159 | |
| 160 | /** @var SettingsService $settingsService */ |
| 161 | $settingsService = $this->container->get('domain.settings.service'); |
| 162 | |
| 163 | if ($settingsService->getSetting('general', 'runInstantPostBookingActions') || !empty($command->getField('runInstantPostBookingActions'))) { |
| 164 | $reservationService->runPostBookingActions($result); |
| 165 | } |
| 166 | |
| 167 | if ($result->getResult() === CommandResult::RESULT_SUCCESS && $reservation) { |
| 168 | /** @var HelperService $helperService */ |
| 169 | $helperService = $this->container->get('application.helper.service'); |
| 170 | |
| 171 | /** @var AbstractUser $customer */ |
| 172 | $customer = $reservation->getCustomer(); |
| 173 | |
| 174 | if ($customer && $customer->getEmail() && $customer->getEmail()->getValue()) { |
| 175 | $data = $result->getData(); |
| 176 | |
| 177 | $data['customerCabinetUrl'] = $helperService->getCustomerCabinetUrl( |
| 178 | $customer->getEmail()->getValue(), |
| 179 | $reservation->isNewUser()->getValue() ? 'email' : null, |
| 180 | null, |
| 181 | null, |
| 182 | $reservation->getLocale() ? $reservation->getLocale()->getValue() : '', |
| 183 | $reservation->isNewUser()->getValue() |
| 184 | ); |
| 185 | |
| 186 | if (!empty($appointmentData['packageBookingFromBackend'])) { |
| 187 | $data['packageBookingFromBackend'] = $appointmentData['packageBookingFromBackend']; |
| 188 | } |
| 189 | |
| 190 | $result->setData($data); |
| 191 | } |
| 192 | |
| 193 | do_action('amelia_after_booking_added', $result ? $result->getData() : null); |
| 194 | } |
| 195 | |
| 196 | return $result; |
| 197 | } |
| 198 | } |
| 199 |