PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.8
Booking for Appointments and Events Calendar – Amelia v2.4.8
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 1 week 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 11 months ago DeleteBookingRemotelyCommand.php 11 months ago DeleteBookingRemotelyCommandHandler.php 1 week 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 week 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 1 week 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
173 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\Helper\HelperService;
8 use AmeliaBooking\Application\Services\Reservation\AbstractReservationService;
9 use AmeliaBooking\Application\Services\User\UserApplicationService;
10 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
11 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
12 use AmeliaBooking\Domain\Entity\Booking\Reservation;
13 use AmeliaBooking\Domain\Entity\Entities;
14 use AmeliaBooking\Domain\Entity\User\AbstractUser;
15 use AmeliaBooking\Domain\Services\Settings\SettingsService;
16 use AmeliaBooking\Domain\ValueObjects\String\PaymentType;
17 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
18 use Exception;
19
20 /**
21 * Class AddBookingCommandHandler
22 *
23 * @package AmeliaBooking\Application\Commands\Booking\Appointment
24 */
25 class AddBookingCommandHandler extends CommandHandler
26 {
27 /**
28 * @var array
29 */
30 public $mandatoryFields = [
31 'bookings',
32 ];
33
34 /**
35 * @param AddBookingCommand $command
36 *
37 * @return CommandResult
38 * @throws InvalidArgumentException
39 * @throws QueryExecutionException
40 * @throws Exception
41 */
42 public function handle(AddBookingCommand $command): CommandResult
43 {
44 $this->checkMandatoryFields($command);
45
46 $appointmentData = $this->getAppointmentData(
47 $command->getFields(),
48 empty($command->getFields()['bookings'][0]['packageCustomerService']['id'])
49 ? [
50 PaymentType::ON_SITE,
51 PaymentType::PAY_PAL,
52 PaymentType::STRIPE,
53 PaymentType::SQUARE,
54 PaymentType::RAZORPAY,
55 ]
56 : null
57 );
58
59 /** @var AbstractReservationService $reservationService */
60 $reservationService = $this->container->get('application.reservation.service')->get(
61 $command->getField('type') ?: Entities::APPOINTMENT
62 );
63
64 $appointmentData = apply_filters('amelia_before_booking_added_filter', $appointmentData);
65
66 do_action('amelia_before_booking_added', $appointmentData);
67
68 $validateCoupon = true;
69
70 if (
71 $command->getField('validateCoupon') === false &&
72 $this->getContainer()->getPermissionsService()->currentUserCanWrite(Entities::COUPONS)
73 ) {
74 $validateCoupon = false;
75 }
76
77 /** @var Reservation $reservation */
78 $reservation = $reservationService->getNew(
79 $validateCoupon,
80 true,
81 true
82 );
83
84 // package appointment booking is available only for logged in users (WP dashboard or panel)
85 if (!empty($appointmentData['bookings'][0]['packageCustomerService']['id'])) {
86 if ($command->getToken()) {
87 /** @var UserApplicationService $userAS */
88 $userAS = $this->container->get('application.user.service');
89
90 try {
91 /** @var AbstractUser $user */
92 $user = $userAS->authorization(
93 $command->getToken(),
94 Entities::CUSTOMER
95 );
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 } else {
109 /** @var AbstractUser $user */
110 $user = $this->container->get('logged.in.user');
111 }
112
113 // user must be admin, manager or customer (same as the customer in the booking)
114 if (
115 !$user ||
116 (
117 $user->getType() !== AbstractUser::USER_ROLE_ADMIN &&
118 $user->getType() !== AbstractUser::USER_ROLE_MANAGER &&
119 $user->getId()->getValue() !== (int)$appointmentData['bookings'][0]['customer']['id']
120 )
121 ) {
122 $result = new CommandResult();
123
124 $result->setResult(CommandResult::RESULT_ERROR);
125
126 return $result;
127 }
128 $appointmentData['payment'] = null;
129 }
130
131 $result = $reservationService->processRequest($appointmentData, $reservation, true);
132
133 /** @var SettingsService $settingsService */
134 $settingsService = $this->container->get('domain.settings.service');
135
136 if ($settingsService->getSetting('general', 'runInstantPostBookingActions') || !empty($command->getField('runInstantPostBookingActions'))) {
137 $reservationService->runPostBookingActions($result);
138 }
139
140 if ($result->getResult() === CommandResult::RESULT_SUCCESS && $reservation) {
141 $data = $result->getData();
142 $data['wpAmeliaNonce'] = wp_create_nonce('ajax-nonce');
143
144 /** @var HelperService $helperService */
145 $helperService = $this->container->get('application.helper.service');
146
147 /** @var AbstractUser $customer */
148 $customer = $reservation->getCustomer();
149
150 if ($customer && $customer->getEmail() && $customer->getEmail()->getValue()) {
151 $data['customerCabinetUrl'] = $helperService->getCustomerCabinetUrl(
152 $customer->getEmail()->getValue(),
153 $reservation->isNewUser()->getValue() ? 'email' : null,
154 null,
155 null,
156 $reservation->getLocale() ? $reservation->getLocale()->getValue() : '',
157 $reservation->isNewUser()->getValue()
158 );
159
160 if (!empty($appointmentData['packageBookingFromBackend'])) {
161 $data['packageBookingFromBackend'] = $appointmentData['packageBookingFromBackend'];
162 }
163 }
164
165 $result->setData($data);
166
167 do_action('amelia_after_booking_added', $result ? $result->getData() : null);
168 }
169
170 return $result;
171 }
172 }
173