PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.31
Booking for Appointments and Events Calendar – Amelia v1.2.31
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 1 year ago AddBookingCommand.php 1 year ago AddBookingCommandHandler.php 1 year ago ApproveBookingRemotelyCommand.php 1 year ago ApproveBookingRemotelyCommandHandler.php 1 year ago CancelBookingCommand.php 1 year ago CancelBookingCommandHandler.php 1 year ago CancelBookingRemotelyCommand.php 1 year ago CancelBookingRemotelyCommandHandler.php 2 years ago DeleteAppointmentCommand.php 1 year ago DeleteAppointmentCommandHandler.php 1 year ago DeleteBookingCommand.php 1 year ago DeleteBookingCommandHandler.php 1 year ago GetAppointmentCommand.php 7 years ago GetAppointmentCommandHandler.php 1 year ago GetAppointmentsCommand.php 1 year ago GetAppointmentsCommandHandler.php 1 year ago GetIcsCommand.php 1 year ago GetIcsCommandHandler.php 4 years ago GetPackageAppointmentsCommand.php 1 year ago GetPackageAppointmentsCommandHandler.php 1 year ago GetTimeSlotsCommand.php 1 year ago GetTimeSlotsCommandHandler.php 1 year ago ReassignBookingCommand.php 1 year ago ReassignBookingCommandHandler.php 1 year ago RejectBookingRemotelyCommand.php 1 year ago RejectBookingRemotelyCommandHandler.php 1 year ago SuccessfulBookingCommand.php 1 year ago SuccessfulBookingCommandHandler.php 1 year ago UpdateAppointmentCommand.php 1 year ago UpdateAppointmentCommandHandler.php 1 year ago UpdateAppointmentStatusCommand.php 1 year ago UpdateAppointmentStatusCommandHandler.php 1 year ago UpdateAppointmentTimeCommand.php 1 year ago UpdateAppointmentTimeCommandHandler.php 1 year ago UpdateBookingStatusCommand.php 1 year ago UpdateBookingStatusCommandHandler.php 1 year 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