AddAppointmentCommand.php
1 year ago
AddAppointmentCommandHandler.php
6 days ago
AddBookingCommand.php
1 year ago
AddBookingCommandHandler.php
6 days ago
ApproveBookingRemotelyCommand.php
1 year ago
ApproveBookingRemotelyCommandHandler.php
3 months ago
CancelBookingCommand.php
1 year ago
CancelBookingCommandHandler.php
6 days ago
CancelBookingRemotelyCommand.php
1 year ago
CancelBookingRemotelyCommandHandler.php
2 months ago
DeleteAppointmentCommand.php
1 year ago
DeleteAppointmentCommandHandler.php
1 year ago
DeleteBookingCommand.php
1 year ago
DeleteBookingCommandHandler.php
11 months ago
DeleteBookingRemotelyCommand.php
11 months ago
DeleteBookingRemotelyCommandHandler.php
7 months ago
GetAppointmentBookingsCommand.php
7 months ago
GetAppointmentBookingsCommandHandler.php
6 days ago
GetAppointmentCommand.php
7 years ago
GetAppointmentCommandHandler.php
6 days ago
GetAppointmentsCommand.php
1 year ago
GetAppointmentsCommandHandler.php
6 days ago
GetIcsCommand.php
7 months ago
GetIcsCommandHandler.php
4 years ago
GetTimeSlotsCommand.php
1 year ago
GetTimeSlotsCommandHandler.php
3 weeks ago
ReassignBookingCommand.php
1 year ago
ReassignBookingCommandHandler.php
6 days ago
RejectBookingRemotelyCommand.php
1 year ago
RejectBookingRemotelyCommandHandler.php
6 months ago
SuccessfulBookingCommand.php
1 year ago
SuccessfulBookingCommandHandler.php
1 year ago
UpdateAppointmentCommand.php
1 year ago
UpdateAppointmentCommandHandler.php
6 days ago
UpdateAppointmentNoteCommand.php
4 months ago
UpdateAppointmentNoteCommandHandler.php
6 days ago
UpdateAppointmentStatusCommand.php
1 year ago
UpdateAppointmentStatusCommandHandler.php
6 days ago
UpdateAppointmentTimeCommand.php
1 year ago
UpdateAppointmentTimeCommandHandler.php
6 days ago
UpdateBookingStatusCommand.php
1 year ago
UpdateBookingStatusCommandHandler.php
6 months ago
AddBookingCommandHandler.php
177 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\Domain\ValueObjects\String\PaymentType; |
| 18 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 19 | use Exception; |
| 20 | |
| 21 | /** |
| 22 | * Class AddBookingCommandHandler |
| 23 | * |
| 24 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 25 | */ |
| 26 | class AddBookingCommandHandler extends CommandHandler |
| 27 | { |
| 28 | /** |
| 29 | * @var array |
| 30 | */ |
| 31 | public $mandatoryFields = [ |
| 32 | 'bookings', |
| 33 | ]; |
| 34 | |
| 35 | /** |
| 36 | * @param AddBookingCommand $command |
| 37 | * |
| 38 | * @return CommandResult |
| 39 | * @throws InvalidArgumentException |
| 40 | * @throws QueryExecutionException |
| 41 | * @throws Exception |
| 42 | */ |
| 43 | public function handle(AddBookingCommand $command) |
| 44 | { |
| 45 | $this->checkMandatoryFields($command); |
| 46 | |
| 47 | /** @var AbstractReservationService $reservationService */ |
| 48 | $reservationService = $this->container->get('application.reservation.service')->get( |
| 49 | $command->getField('type') ?: Entities::APPOINTMENT |
| 50 | ); |
| 51 | |
| 52 | /** @var BookingApplicationService $bookingAS */ |
| 53 | $bookingAS = $this->container->get('application.booking.booking.service'); |
| 54 | |
| 55 | $appointmentData = $bookingAS->getAppointmentData($command->getFields()); |
| 56 | |
| 57 | if ( |
| 58 | !empty($appointmentData['payment']['gateway']) && |
| 59 | in_array($appointmentData['payment']['gateway'], [PaymentType::WC, PaymentType::MOLLIE, PaymentType::BARION]) |
| 60 | ) { |
| 61 | $result = new CommandResult(); |
| 62 | |
| 63 | $result->setResult(CommandResult::RESULT_ERROR); |
| 64 | |
| 65 | return $result; |
| 66 | } |
| 67 | |
| 68 | $appointmentData = apply_filters('amelia_before_booking_added_filter', $appointmentData); |
| 69 | |
| 70 | do_action('amelia_before_booking_added', $appointmentData); |
| 71 | |
| 72 | $validateCoupon = true; |
| 73 | |
| 74 | if ( |
| 75 | $command->getField('validateCoupon') === false && |
| 76 | $this->getContainer()->getPermissionsService()->currentUserCanWrite(Entities::COUPONS) |
| 77 | ) { |
| 78 | $validateCoupon = false; |
| 79 | } |
| 80 | |
| 81 | /** @var Reservation $reservation */ |
| 82 | $reservation = $reservationService->getNew( |
| 83 | $validateCoupon, |
| 84 | true, |
| 85 | true |
| 86 | ); |
| 87 | |
| 88 | // package appointment booking is available only for logged in users (WP dashboard or panel) |
| 89 | if (!empty($appointmentData['bookings'][0]['packageCustomerService']['id'])) { |
| 90 | if ($command->getToken()) { |
| 91 | /** @var UserApplicationService $userAS */ |
| 92 | $userAS = $this->container->get('application.user.service'); |
| 93 | |
| 94 | try { |
| 95 | /** @var AbstractUser $user */ |
| 96 | $user = $userAS->authorization( |
| 97 | $command->getToken(), |
| 98 | Entities::CUSTOMER |
| 99 | ); |
| 100 | } catch (AuthorizationException $e) { |
| 101 | $result = new CommandResult(); |
| 102 | |
| 103 | $result->setResult(CommandResult::RESULT_ERROR); |
| 104 | $result->setData( |
| 105 | [ |
| 106 | 'reauthorize' => true |
| 107 | ] |
| 108 | ); |
| 109 | |
| 110 | return $result; |
| 111 | } |
| 112 | } else { |
| 113 | /** @var AbstractUser $user */ |
| 114 | $user = $this->container->get('logged.in.user'); |
| 115 | } |
| 116 | |
| 117 | // user must be admin, manager or customer (same as the customer in the booking) |
| 118 | if ( |
| 119 | !$user || |
| 120 | ( |
| 121 | $user->getType() !== AbstractUser::USER_ROLE_ADMIN && |
| 122 | $user->getType() !== AbstractUser::USER_ROLE_MANAGER && |
| 123 | $user->getId()->getValue() !== (int)$appointmentData['bookings'][0]['customer']['id'] |
| 124 | ) |
| 125 | ) { |
| 126 | $result = new CommandResult(); |
| 127 | |
| 128 | $result->setResult(CommandResult::RESULT_ERROR); |
| 129 | |
| 130 | return $result; |
| 131 | } |
| 132 | |
| 133 | $appointmentData['payment'] = null; |
| 134 | } |
| 135 | |
| 136 | $result = $reservationService->processRequest($appointmentData, $reservation, true); |
| 137 | |
| 138 | /** @var SettingsService $settingsService */ |
| 139 | $settingsService = $this->container->get('domain.settings.service'); |
| 140 | |
| 141 | if ($settingsService->getSetting('general', 'runInstantPostBookingActions') || !empty($command->getField('runInstantPostBookingActions'))) { |
| 142 | $reservationService->runPostBookingActions($result); |
| 143 | } |
| 144 | |
| 145 | if ($result->getResult() === CommandResult::RESULT_SUCCESS && $reservation) { |
| 146 | /** @var HelperService $helperService */ |
| 147 | $helperService = $this->container->get('application.helper.service'); |
| 148 | |
| 149 | /** @var AbstractUser $customer */ |
| 150 | $customer = $reservation->getCustomer(); |
| 151 | |
| 152 | if ($customer && $customer->getEmail() && $customer->getEmail()->getValue()) { |
| 153 | $data = $result->getData(); |
| 154 | |
| 155 | $data['customerCabinetUrl'] = $helperService->getCustomerCabinetUrl( |
| 156 | $customer->getEmail()->getValue(), |
| 157 | $reservation->isNewUser()->getValue() ? 'email' : null, |
| 158 | null, |
| 159 | null, |
| 160 | $reservation->getLocale() ? $reservation->getLocale()->getValue() : '', |
| 161 | $reservation->isNewUser()->getValue() |
| 162 | ); |
| 163 | |
| 164 | if (!empty($appointmentData['packageBookingFromBackend'])) { |
| 165 | $data['packageBookingFromBackend'] = $appointmentData['packageBookingFromBackend']; |
| 166 | } |
| 167 | |
| 168 | $result->setData($data); |
| 169 | } |
| 170 | |
| 171 | do_action('amelia_after_booking_added', $result ? $result->getData() : null); |
| 172 | } |
| 173 | |
| 174 | return $result; |
| 175 | } |
| 176 | } |
| 177 |