PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.25
Booking for Appointments and Events Calendar – Amelia v1.2.25
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 / AddAppointmentCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Appointment Last commit date
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 6 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
AddAppointmentCommandHandler.php
394 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\Common\Exceptions\AccessDeniedException;
8 use AmeliaBooking\Application\Services\Bookable\BookableApplicationService;
9 use AmeliaBooking\Application\Services\Booking\AppointmentApplicationService;
10 use AmeliaBooking\Application\Services\Entity\EntityApplicationService;
11 use AmeliaBooking\Application\Services\User\UserApplicationService;
12 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
13 use AmeliaBooking\Domain\Common\Exceptions\BookingUnavailableException;
14 use AmeliaBooking\Domain\Common\Exceptions\CustomerBookedException;
15 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
16 use AmeliaBooking\Domain\Entity\Bookable\Service\Service;
17 use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment;
18 use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking;
19 use AmeliaBooking\Domain\Entity\Entities;
20 use AmeliaBooking\Domain\Entity\Payment\Payment;
21 use AmeliaBooking\Domain\Entity\User\AbstractUser;
22 use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface;
23 use AmeliaBooking\Domain\Services\Settings\SettingsService;
24 use AmeliaBooking\Domain\ValueObjects\PositiveDuration;
25 use AmeliaBooking\Domain\ValueObjects\String\BookingStatus;
26 use AmeliaBooking\Domain\ValueObjects\String\Description;
27 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
28 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
29 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository;
30 use Exception;
31 use Interop\Container\Exception\ContainerException;
32 use Slim\Exception\ContainerValueNotFoundException;
33
34 /**
35 * Class AddAppointmentCommandHandler
36 *
37 * @package AmeliaBooking\Application\Commands\Booking\Appointment
38 */
39 class AddAppointmentCommandHandler extends CommandHandler
40 {
41 /**
42 * @var array
43 */
44 public $mandatoryFields = [
45 'bookings',
46 'bookingStart',
47 'notifyParticipants',
48 'serviceId',
49 'providerId'
50 ];
51
52 /**
53 * @param AddAppointmentCommand $command
54 *
55 * @return CommandResult
56 * @throws NotFoundException
57 * @throws ContainerValueNotFoundException
58 * @throws InvalidArgumentException
59 * @throws QueryExecutionException
60 * @throws ContainerException
61 * @throws Exception
62 */
63 public function handle(AddAppointmentCommand $command)
64 {
65 $result = new CommandResult();
66
67 $this->checkMandatoryFields($command);
68
69 /** @var AppointmentRepository $appointmentRepo */
70 $appointmentRepo = $this->container->get('domain.booking.appointment.repository');
71 /** @var AppointmentApplicationService $appointmentAS */
72 $appointmentAS = $this->container->get('application.booking.appointment.service');
73 /** @var BookableApplicationService $bookableAS */
74 $bookableAS = $this->container->get('application.bookable.service');
75 /** @var UserApplicationService $userAS */
76 $userAS = $this->getContainer()->get('application.user.service');
77 /** @var SettingsService $settingsDS */
78 $settingsDS = $this->container->get('domain.settings.service');
79 /** @var EntityApplicationService $entityService */
80 $entityService = $this->container->get('application.entity.service');
81 /** @var ReservationServiceInterface $reservationService */
82 $reservationService = $this->container->get('application.reservation.service')->get(Entities::APPOINTMENT);
83
84 if ($missingEntity = $entityService->getMissingEntityForAppointment($command->getFields())) {
85 return $entityService->getMissingEntityResponse($missingEntity);
86 }
87
88 try {
89 /** @var AbstractUser $user */
90 $user = $command->getUserApplicationService()->authorization(
91 $command->getPage() === 'cabinet' ? $command->getToken() : null,
92 $command->getCabinetType()
93 );
94 } catch (AuthorizationException $e) {
95 $result->setResult(CommandResult::RESULT_ERROR);
96 $result->setData(
97 [
98 'reauthorize' => true
99 ]
100 );
101
102 return $result;
103 }
104
105 if ($userAS->isCustomer($user)) {
106 throw new AccessDeniedException('You are not allowed to update appointment');
107 }
108
109 if ($userAS->isProvider($user) && !$settingsDS->getSetting('roles', 'allowWriteAppointments')) {
110 throw new AccessDeniedException('You are not allowed to add an appointment');
111 }
112
113 $appointmentData = $command->getFields();
114
115 $paymentData = !empty($command->getField('payment')) ? array_merge($command->getField('payment'), ['isBackendBooking' => true]) :
116 ['amount' => 0, 'gateway' => 'onSite', 'isBackendBooking' => true];
117
118 /** @var Service $service */
119 $service = $bookableAS->getAppointmentService($appointmentData['serviceId'], $appointmentData['providerId']);
120
121 $appointmentData = apply_filters('amelia_before_appointment_added_filter', $appointmentData, $service ? $service->toArray() : null, $paymentData);
122
123 do_action('amelia_before_appointment_added', $appointmentData, $service ? $service->toArray() : null, $paymentData);
124
125 $maxDuration = 0;
126
127 foreach ($appointmentData['bookings'] as $booking) {
128 if ($booking['duration'] > $maxDuration && ($booking['status'] === BookingStatus::APPROVED || BookingStatus::PENDING)) {
129 $maxDuration = $booking['duration'];
130 }
131 }
132
133 if ($maxDuration) {
134 $service->setDuration(new PositiveDuration($maxDuration));
135 }
136
137 $appointmentAS->convertTime($appointmentData);
138
139 $reservationService->manageTaxes($appointmentData);
140
141 $appointmentRepo->beginTransaction();
142
143 $ignoredData = [];
144
145 /** @var Appointment $existingAppointment */
146 $existingAppointment = $appointmentAS->getAlreadyBookedAppointment($appointmentData, false, $service);
147
148 /** @var Appointment $appointment */
149 $appointment = $appointmentAS->build(
150 $existingAppointment ? $existingAppointment->toArray() : $appointmentData,
151 $service
152 );
153
154 if ($existingAppointment && !empty($appointmentData['internalNotes'])) {
155 if ($existingAppointment->getInternalNotes() &&
156 $existingAppointment->getInternalNotes()->getValue()
157 ) {
158 $appointment->setInternalNotes(
159 new Description(
160 $existingAppointment->getInternalNotes()->getValue() .
161 PHP_EOL .
162 PHP_EOL .
163 $appointmentData['internalNotes']
164 )
165 );
166 } else {
167 $appointment->setInternalNotes(
168 new Description(
169 $appointmentData['internalNotes']
170 )
171 );
172 }
173 }
174
175 try {
176 $appointmentAS->addOrEditAppointment(
177 $appointment,
178 $existingAppointment,
179 $service,
180 $appointmentData,
181 $paymentData
182 );
183 } catch (CustomerBookedException $e) {
184 $appointmentRepo->rollback();
185
186 $result->setResult(CommandResult::RESULT_ERROR);
187 $result->setMessage($e->getMessage());
188 $result->setData(
189 [
190 'customerAlreadyBooked' => true
191 ]
192 );
193
194 return $result;
195 } catch (BookingUnavailableException $e) {
196 $appointmentRepo->rollback();
197
198 $result->setResult(CommandResult::RESULT_ERROR);
199 $result->setMessage($e->getMessage());
200 $result->setData(
201 [
202 'timeSlotUnavailable' => true
203 ]
204 );
205
206 return $result;
207 }
208
209 foreach ($appointmentData['bookings'] as $bookingData) {
210 $paymentData['customerPaymentParentId'][(int)$bookingData['customerId']] = null;
211 }
212
213 /** @var CustomerBooking $booking */
214 foreach ($appointment->getBookings()->getItems() as $booking) {
215 if ($booking->getCustomerId() &&
216 $booking->getCustomerId()->getValue() &&
217 array_key_exists($booking->getCustomerId()->getValue(), $paymentData['customerPaymentParentId']) &&
218 $booking->getPayments() &&
219 $booking->getPayments()->keyExists(0)
220 ) {
221 /** @var Payment $payment */
222 $payment = $booking->getPayments()->getItem(0);
223
224 $paymentData['customerPaymentParentId'][$booking->getCustomerId()->getValue()]
225 = $payment->getId()->getValue();
226 }
227 }
228
229 if ($existingAppointment !== null) {
230 $existingAppointmentId = $existingAppointment->getId()->getValue();
231
232 $ignoredData[$existingAppointmentId] = [
233 'status' => $existingAppointment->getStatus()->getValue(),
234 'bookingsIds' => [],
235 ];
236
237 /** @var CustomerBooking $booking */
238 foreach ($existingAppointment->getBookings()->getItems() as $booking) {
239 $ignoredData[$existingAppointmentId]['bookingsIds'][$booking->getId()->getValue()] = true;
240 }
241 }
242
243 $error = false;
244
245 $recurringAppointments = [];
246
247 foreach ($command->getField('recurring') as $recurringData) {
248 $recurringAppointmentData = array_merge(
249 $appointmentData,
250 [
251 'bookingStart' => $recurringData['bookingStart'],
252 'locationId' => $recurringData['locationId'],
253 'parentId' => $appointment->getId()->getValue()
254 ]
255 );
256
257 $appointmentAS->convertTime($recurringAppointmentData);
258
259 /** @var Appointment $existingRecurringAppointment */
260 $existingRecurringAppointment = $appointmentAS->getAlreadyBookedAppointment(
261 $recurringAppointmentData,
262 false,
263 $service
264 );
265
266 /** @var Appointment $recurringAppointment */
267 $recurringAppointment = $appointmentAS->build(
268 $existingRecurringAppointment ? $existingRecurringAppointment->toArray() : $recurringAppointmentData,
269 $service
270 );
271
272 if ($existingRecurringAppointment && $recurringAppointmentData['internalNotes']) {
273 if ($existingRecurringAppointment->getInternalNotes() &&
274 $existingRecurringAppointment->getInternalNotes()->getValue()
275 ) {
276 $recurringAppointment->setInternalNotes(
277 new Description(
278 $existingRecurringAppointment->getInternalNotes()->getValue() .
279 PHP_EOL .
280 PHP_EOL .
281 $recurringAppointmentData['internalNotes']
282 )
283 );
284 } else {
285 $recurringAppointment->setInternalNotes(
286 new Description(
287 $recurringAppointmentData['internalNotes']
288 )
289 );
290 }
291 }
292
293 try {
294 $appointmentAS->addOrEditAppointment(
295 $recurringAppointment,
296 $existingRecurringAppointment,
297 $service,
298 $recurringAppointmentData,
299 $paymentData
300 );
301 } catch (CustomerBookedException $e) {
302 $appointmentRepo->rollback();
303
304 $result->setResult(CommandResult::RESULT_ERROR);
305 $result->setMessage($e->getMessage());
306 $result->setData(
307 [
308 'customerAlreadyBooked' => true
309 ]
310 );
311
312 $error = true;
313 } catch (BookingUnavailableException $e) {
314 $appointmentRepo->rollback();
315
316 $result->setResult(CommandResult::RESULT_ERROR);
317 $result->setMessage($e->getMessage());
318 $result->setData(
319 [
320 'timeSlotUnavailable' => true
321 ]
322 );
323
324 $error = true;
325 }
326
327 if ($error) {
328 $appointmentAS->delete($appointment, $ignoredData);
329
330 if ($appointment->getId() &&
331 $appointment->getId()->getValue() &&
332 !empty($ignoredData[$appointment->getId()->getValue()])
333 ) {
334 $appointmentRepo->updateFieldById(
335 $appointment->getId()->getValue(),
336 $ignoredData[$appointment->getId()->getValue()]['status'],
337 'status'
338 );
339 }
340
341 foreach ($recurringAppointments as $savedRecurringAppointment) {
342 $appointmentAS->delete(
343 $appointmentAS->build($savedRecurringAppointment[Entities::APPOINTMENT], $service),
344 $ignoredData
345 );
346
347 if (!empty($ignoredData[$savedRecurringAppointment[Entities::APPOINTMENT]['id']])) {
348 $appointmentRepo->updateFieldById(
349 $savedRecurringAppointment[Entities::APPOINTMENT]['id'],
350 $ignoredData[$savedRecurringAppointment[Entities::APPOINTMENT]['id']]['status'],
351 'status'
352 );
353 }
354 }
355
356 return $result;
357 }
358
359 if ($existingRecurringAppointment !== null) {
360 $existingAppointmentId = $existingRecurringAppointment->getId()->getValue();
361
362 $ignoredData[$existingAppointmentId] = [
363 'status' => $existingRecurringAppointment->getStatus()->getValue(),
364 'bookingsIds' => [],
365 ];
366
367 /** @var CustomerBooking $booking */
368 foreach ($existingRecurringAppointment->getBookings()->getItems() as $booking) {
369 $ignoredData[$existingAppointmentId]['bookingsIds'][$booking->getId()->getValue()] = true;
370 }
371 }
372
373 $recurringAppointments[] = [
374 Entities::APPOINTMENT => $recurringAppointment->toArray()
375 ];
376 }
377
378 $result->setResult(CommandResult::RESULT_SUCCESS);
379 $result->setMessage('Successfully added new appointment');
380 $result->setData(
381 [
382 Entities::APPOINTMENT => $appointment->toArray(),
383 'recurring' => $recurringAppointments
384 ]
385 );
386
387 $appointmentRepo->commit();
388
389 do_action('amelia_after_appointment_added', $appointment ? $appointment->toArray() : null, $service ? $service->toArray() : null, $paymentData);
390
391 return $result;
392 }
393 }
394