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
AddBookingCommandHandler.php
197 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 ($command->getField('validateCoupon') === false && |
| 67 | $this->getContainer()->getPermissionsService()->currentUserCanWrite(Entities::COUPONS) |
| 68 | ) { |
| 69 | $validateCoupon = false; |
| 70 | } |
| 71 | |
| 72 | /** @var Reservation $reservation */ |
| 73 | $reservation = $reservationService->getNew( |
| 74 | $validateCoupon, |
| 75 | true, |
| 76 | true |
| 77 | ); |
| 78 | |
| 79 | /** @var UserApplicationService $userAS */ |
| 80 | $userAS = $this->container->get('application.user.service'); |
| 81 | |
| 82 | if ($command->getToken() && |
| 83 | $command->getPage() === 'cabinet' && |
| 84 | $command->getCabinetType() === Entities::PROVIDER |
| 85 | ) { |
| 86 | try { |
| 87 | /** @var AbstractUser $user */ |
| 88 | $user = $userAS->authorization( |
| 89 | $command->getToken(), |
| 90 | Entities::PROVIDER |
| 91 | ); |
| 92 | |
| 93 | $reservation->setLoggedInUser($user); |
| 94 | } catch (AuthorizationException $e) { |
| 95 | $result = new CommandResult(); |
| 96 | |
| 97 | $result->setResult(CommandResult::RESULT_ERROR); |
| 98 | $result->setData( |
| 99 | [ |
| 100 | 'reauthorize' => true |
| 101 | ] |
| 102 | ); |
| 103 | |
| 104 | return $result; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if ($command->getToken() && !empty($appointmentData['bookings'][0]['packageCustomerService']['id'])) { |
| 109 | try { |
| 110 | /** @var AbstractUser $user */ |
| 111 | $user = $userAS->authorization( |
| 112 | $command->getToken(), |
| 113 | Entities::CUSTOMER |
| 114 | ); |
| 115 | |
| 116 | $reservation->setLoggedInUser($user); |
| 117 | } catch (AuthorizationException $e) { |
| 118 | $result = new CommandResult(); |
| 119 | |
| 120 | $result->setResult(CommandResult::RESULT_ERROR); |
| 121 | $result->setData( |
| 122 | [ |
| 123 | 'reauthorize' => true |
| 124 | ] |
| 125 | ); |
| 126 | |
| 127 | return $result; |
| 128 | } |
| 129 | |
| 130 | if ($user->getId()->getValue() !== (int)$appointmentData['bookings'][0]['customer']['id']) { |
| 131 | $result = new CommandResult(); |
| 132 | |
| 133 | $result->setResult(CommandResult::RESULT_ERROR); |
| 134 | |
| 135 | return $result; |
| 136 | } |
| 137 | |
| 138 | $appointmentData['payment'] = null; |
| 139 | |
| 140 | $appointmentData['isCabinetBooking'] = true; |
| 141 | } elseif (!empty($appointmentData['bookings'][0]['packageCustomerService']['id'])) { |
| 142 | /** @var AbstractUser $user */ |
| 143 | $user = $this->container->get('logged.in.user'); |
| 144 | |
| 145 | if ($user && $user->getType() === AbstractUser::USER_ROLE_ADMIN) { |
| 146 | $appointmentData['payment'] = null; |
| 147 | |
| 148 | $appointmentData['isCabinetBooking'] = true; |
| 149 | } |
| 150 | } else { |
| 151 | $appointmentData['isCabinetBooking'] = false; |
| 152 | |
| 153 | unset($appointmentData['bookings'][0]['packageCustomerService']['id']); |
| 154 | } |
| 155 | |
| 156 | $result = $reservationService->processRequest($appointmentData, $reservation, true); |
| 157 | |
| 158 | /** @var SettingsService $settingsService */ |
| 159 | $settingsService = $this->container->get('domain.settings.service'); |
| 160 | |
| 161 | if ($settingsService->getSetting('general', 'runInstantPostBookingActions') || !empty($command->getField('runInstantPostBookingActions'))) { |
| 162 | $reservationService->runPostBookingActions($result); |
| 163 | } |
| 164 | |
| 165 | if ($result->getResult() === CommandResult::RESULT_SUCCESS && $reservation) { |
| 166 | /** @var HelperService $helperService */ |
| 167 | $helperService = $this->container->get('application.helper.service'); |
| 168 | |
| 169 | /** @var AbstractUser $customer */ |
| 170 | $customer = $reservation->getCustomer(); |
| 171 | |
| 172 | if ($customer && $customer->getEmail() && $customer->getEmail()->getValue()) { |
| 173 | $data = $result->getData(); |
| 174 | |
| 175 | $data['customerCabinetUrl'] = $helperService->getCustomerCabinetUrl( |
| 176 | $customer->getEmail()->getValue(), |
| 177 | $reservation->isNewUser()->getValue() ? 'email' : null, |
| 178 | null, |
| 179 | null, |
| 180 | $reservation->getLocale() ? $reservation->getLocale()->getValue() : '', |
| 181 | $reservation->isNewUser()->getValue() |
| 182 | ); |
| 183 | |
| 184 | if (!empty($appointmentData['packageBookingFromBackend'])) { |
| 185 | $data['packageBookingFromBackend'] = $appointmentData['packageBookingFromBackend']; |
| 186 | } |
| 187 | |
| 188 | $result->setData($data); |
| 189 | } |
| 190 | |
| 191 | do_action('amelia_after_booking_added', $result ? $result->getData() : null); |
| 192 | } |
| 193 | |
| 194 | return $result; |
| 195 | } |
| 196 | } |
| 197 |