PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.7
Booking for Appointments and Events Calendar – Amelia v2.4.7
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Booking / Appointment / AddBookingCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Appointment Last commit date
AddAppointmentCommand.php 1 year ago AddAppointmentCommandHandler.php 3 weeks ago AddBookingCommand.php 1 year ago AddBookingCommandHandler.php 2 weeks ago ApproveBookingRemotelyCommand.php 1 year ago ApproveBookingRemotelyCommandHandler.php 4 months ago CancelBookingCommand.php 1 year ago CancelBookingCommandHandler.php 3 weeks 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 1 year ago DeleteBookingRemotelyCommand.php 1 year ago DeleteBookingRemotelyCommandHandler.php 8 months ago GetAppointmentBookingsCommand.php 8 months ago GetAppointmentBookingsCommandHandler.php 3 weeks ago GetAppointmentCommand.php 7 years ago GetAppointmentCommandHandler.php 3 weeks ago GetAppointmentsCommand.php 1 year ago GetAppointmentsCommandHandler.php 3 weeks ago GetIcsCommand.php 8 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 weeks ago RejectBookingRemotelyCommand.php 1 year ago RejectBookingRemotelyCommandHandler.php 6 months ago SuccessfulBookingCommand.php 1 year ago SuccessfulBookingCommandHandler.php 2 weeks ago UpdateAppointmentCommand.php 1 year ago UpdateAppointmentCommandHandler.php 3 weeks ago UpdateAppointmentNoteCommand.php 4 months ago UpdateAppointmentNoteCommandHandler.php 3 weeks ago UpdateAppointmentStatusCommand.php 1 year ago UpdateAppointmentStatusCommandHandler.php 3 weeks ago UpdateAppointmentTimeCommand.php 1 year ago UpdateAppointmentTimeCommandHandler.php 3 weeks ago UpdateBookingStatusCommand.php 1 year ago UpdateBookingStatusCommandHandler.php 7 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\Payment\PaymentApplicationService;
10 use AmeliaBooking\Application\Services\Reservation\AbstractReservationService;
11 use AmeliaBooking\Application\Services\User\UserApplicationService;
12 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
13 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
14 use AmeliaBooking\Domain\Entity\Booking\Reservation;
15 use AmeliaBooking\Domain\Entity\Entities;
16 use AmeliaBooking\Domain\Entity\User\AbstractUser;
17 use AmeliaBooking\Domain\Services\Settings\SettingsService;
18 use AmeliaBooking\Domain\ValueObjects\String\PaymentType;
19 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
20 use Exception;
21 use Interop\Container\Exception\ContainerException;
22 use Slim\Exception\ContainerValueNotFoundException;
23
24 /**
25 * Class AddBookingCommandHandler
26 *
27 * @package AmeliaBooking\Application\Commands\Booking\Appointment
28 */
29 class AddBookingCommandHandler extends CommandHandler
30 {
31 /**
32 * @var array
33 */
34 public $mandatoryFields = [
35 'bookings',
36 ];
37
38 /**
39 * @param AddBookingCommand $command
40 *
41 * @return CommandResult
42 * @throws ContainerValueNotFoundException
43 * @throws InvalidArgumentException
44 * @throws QueryExecutionException
45 * @throws ContainerException
46 * @throws Exception
47 */
48 public function handle(AddBookingCommand $command)
49 {
50 $this->checkMandatoryFields($command);
51
52 /** @var AbstractReservationService $reservationService */
53 $reservationService = $this->container->get('application.reservation.service')->get(
54 $command->getField('type') ?: Entities::APPOINTMENT
55 );
56
57 /** @var BookingApplicationService $bookingAS */
58 $bookingAS = $this->container->get('application.booking.booking.service');
59
60 $appointmentData = $bookingAS->getAppointmentData($command->getFields());
61
62 /** @var PaymentApplicationService $paymentAS */
63 $paymentAS = $this->container->get('application.payment.service');
64
65 // WooCommerce, Mollie and Barion bookings are added through their own command handlers
66 $allowedGateways = array_values(
67 array_diff(
68 $paymentAS->getAvailablePaymentGateways(),
69 [PaymentType::WC, PaymentType::MOLLIE, PaymentType::BARION]
70 )
71 );
72
73 if (isset($appointmentData['payment'])) {
74 $paymentData = $paymentAS->getValidatedRequestPaymentData(
75 $appointmentData['payment'],
76 $allowedGateways
77 );
78
79 if ($paymentData === null) {
80 $result = new CommandResult();
81
82 $result->setResult(CommandResult::RESULT_ERROR);
83
84 return $result;
85 }
86
87 $appointmentData['payment'] = $paymentData;
88 }
89
90 $appointmentData = apply_filters('amelia_before_booking_added_filter', $appointmentData);
91
92 do_action('amelia_before_booking_added', $appointmentData);
93
94 $validateCoupon = true;
95
96 if (
97 $command->getField('validateCoupon') === false &&
98 $this->getContainer()->getPermissionsService()->currentUserCanWrite(Entities::COUPONS)
99 ) {
100 $validateCoupon = false;
101 }
102
103 /** @var Reservation $reservation */
104 $reservation = $reservationService->getNew(
105 $validateCoupon,
106 true,
107 true
108 );
109
110 // package appointment booking is available only for logged in users (WP dashboard or panel)
111 if (!empty($appointmentData['bookings'][0]['packageCustomerService']['id'])) {
112 if ($command->getToken()) {
113 /** @var UserApplicationService $userAS */
114 $userAS = $this->container->get('application.user.service');
115
116 try {
117 /** @var AbstractUser $user */
118 $user = $userAS->authorization(
119 $command->getToken(),
120 Entities::CUSTOMER
121 );
122 } catch (AuthorizationException $e) {
123 $result = new CommandResult();
124
125 $result->setResult(CommandResult::RESULT_ERROR);
126 $result->setData(
127 [
128 'reauthorize' => true
129 ]
130 );
131
132 return $result;
133 }
134 } else {
135 /** @var AbstractUser $user */
136 $user = $this->container->get('logged.in.user');
137 }
138
139 // user must be admin, manager or customer (same as the customer in the booking)
140 if (
141 !$user ||
142 (
143 $user->getType() !== AbstractUser::USER_ROLE_ADMIN &&
144 $user->getType() !== AbstractUser::USER_ROLE_MANAGER &&
145 $user->getId()->getValue() !== (int)$appointmentData['bookings'][0]['customer']['id']
146 )
147 ) {
148 $result = new CommandResult();
149
150 $result->setResult(CommandResult::RESULT_ERROR);
151
152 return $result;
153 }
154
155 $appointmentData['payment'] = null;
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