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 / GetTimeSlotsCommandHandler.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
GetTimeSlotsCommandHandler.php
425 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\TimeSlot\TimeSlotService as ApplicationTimeSlotService;
8 use AmeliaBooking\Domain\Entity\User\Provider;
9 use AmeliaBooking\Domain\Services\Entity\EntityService;
10 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
11 use AmeliaBooking\Domain\Entity\Bookable\Service\Service;
12 use AmeliaBooking\Domain\Entity\Booking\SlotsEntities;
13 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
14 use AmeliaBooking\Domain\Services\Settings\SettingsService;
15 use AmeliaBooking\Domain\ValueObjects\PositiveDuration;
16 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
17 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository;
18 use AmeliaBooking\Infrastructure\Services\Apple\AbstractAppleCalendarService;
19 use AmeliaBooking\Infrastructure\Services\Google\AbstractGoogleCalendarService;
20 use AmeliaBooking\Infrastructure\Services\Outlook\AbstractOutlookCalendarService;
21 use DateTimeZone;
22 use Exception;
23 use Interop\Container\Exception\ContainerException;
24 use Slim\Exception\ContainerValueNotFoundException;
25
26 /**
27 * Class GetTimeSlotsCommandHandler
28 *
29 * @package AmeliaBooking\Application\Commands\Booking\Appointment
30 */
31 class GetTimeSlotsCommandHandler extends CommandHandler
32 {
33 /**
34 * @var array
35 */
36 public $mandatoryFields = [
37 'serviceId'
38 ];
39
40 /**
41 * @param GetTimeSlotsCommand $command
42 *
43 * @return CommandResult
44 * @throws ContainerValueNotFoundException
45 * @throws ContainerException
46 * @throws Exception
47 * @throws InvalidArgumentException
48 * @throws QueryExecutionException
49 */
50 public function handle(GetTimeSlotsCommand $command)
51 {
52 $result = new CommandResult();
53
54 $this->checkMandatoryFields($command);
55
56 /** @var EntityService $entityService */
57 $entityService = $this->container->get('domain.entity.service');
58
59 /** @var ApplicationTimeSlotService $applicationTimeSlotService */
60 $applicationTimeSlotService = $this->container->get('application.timeSlot.service');
61
62 /** @var SettingsService $settingsDS */
63 $settingsDS = $this->container->get('domain.settings.service');
64
65 $props = [
66 'serviceId' => $command->getField('serviceId'),
67 'providerIds' => $command->getField('providerIds'),
68 'locationId' => $command->getField('locationId'),
69 'extras' => $command->getField('extras'),
70 'excludeAppointmentId' => $command->getField('excludeAppointmentId'),
71 'personsCount' => $command->getField('group') ? $command->getField('persons') : null,
72 'isFrontEndBooking' => $command->getField('page') === 'booking' || $command->getField('page') === 'cabinet',
73 'totalPersons' => $command->getField('persons'),
74 'serviceDuration' => $command->getField('serviceDuration'),
75 'monthsLoad' => $command->getField('monthsLoad'),
76 'startDateTime' => $command->getField('startDateTime'),
77 'endDateTime' => $command->getField('endDateTime'),
78 'queryTimeZone' => $command->getField('queryTimeZone'),
79 'timeZone' => $command->getField('timeZone')
80
81 ];
82
83 $props = apply_filters('amelia_before_get_timeslots_filter', $props);
84
85 do_action('amelia_before_get_timeslots', $props);
86
87 $isFrontEndBooking = $props['isFrontEndBooking'];
88
89 /** @var SlotsEntities $slotsEntities */
90 $slotsEntities = $applicationTimeSlotService->getSlotsEntities(
91 [
92 'isFrontEndBooking' => $isFrontEndBooking,
93 'providerIds' => $props['providerIds'],
94 ]
95 );
96
97 $settings = $applicationTimeSlotService->getSlotsSettings($isFrontEndBooking, $slotsEntities, $props);
98
99 $lastBookedProviderId = null;
100
101 /** @var SlotsEntities $filteredSlotEntities */
102 $filteredSlotEntities = $entityService->getFilteredSlotsEntities(
103 $settings,
104 $props,
105 $slotsEntities
106 );
107
108 /** @var Service $service */
109 $service = $filteredSlotEntities->getServices()->getItem($props['serviceId']);
110
111 if ($props['serviceDuration']) {
112 $service->setDuration(new PositiveDuration($props['serviceDuration']));
113 }
114
115 $minimumBookingTimeInSeconds = $settingsDS
116 ->getEntitySettings($service->getSettings())
117 ->getGeneralSettings()
118 ->getMinimumTimeRequirementPriorToBooking();
119
120 $maximumBookingTimeInDays = $settingsDS
121 ->getEntitySettings($service->getSettings())
122 ->getGeneralSettings()
123 ->getNumberOfDaysAvailableForBooking();
124
125 $monthsLoad = $props['monthsLoad'];
126
127 $loadGeneratedPeriod = $monthsLoad &&
128 !$props['endDateTime'];
129
130 $timeZone = $props['queryTimeZone'] ?: DateTimeService::getTimeZone()->getName();
131
132 $queryStartDateTime = $props['startDateTime'] ?
133 DateTimeService::getDateTimeObjectInTimeZone(
134 $props['startDateTime'],
135 $timeZone
136 )->setTimezone(DateTimeService::getTimeZone()) : null;
137
138 $queryEndDateTime = $props['endDateTime'] ?
139 DateTimeService::getDateTimeObjectInTimeZone(
140 $props['endDateTime'],
141 $timeZone
142 )->setTimezone(DateTimeService::getTimeZone()) : null;
143
144 $minimumDateTime = $applicationTimeSlotService->getMinimumDateTimeForBooking(
145 null,
146 $isFrontEndBooking,
147 $minimumBookingTimeInSeconds
148 );
149
150 $startDateTime = $queryStartDateTime ?:
151 $applicationTimeSlotService->getMinimumDateTimeForBooking(
152 null,
153 $isFrontEndBooking,
154 $minimumBookingTimeInSeconds
155 );
156
157 $endDateTime = $queryEndDateTime ?:
158 $applicationTimeSlotService->getMaximumDateTimeForBooking(
159 null,
160 $isFrontEndBooking,
161 $maximumBookingTimeInDays
162 );
163
164 $maximumDateTime = $applicationTimeSlotService->getMaximumDateTimeForBooking(
165 null,
166 $isFrontEndBooking,
167 $maximumBookingTimeInDays
168 );
169
170 if ($isFrontEndBooking) {
171 $startDateTime = $startDateTime < $minimumDateTime ? $minimumDateTime : $startDateTime;
172
173 $endDateTime = $endDateTime > $maximumDateTime ? $maximumDateTime : $endDateTime;
174 }
175
176 // set initial search period if query dates are not set
177 if ($loadGeneratedPeriod) {
178 $endDateTime = DateTimeService::getCustomDateTimeObject(
179 $startDateTime->format('Y-m-d H:i:s')
180 )->setTimezone(
181 new DateTimeZone($timeZone)
182 );
183
184 $endDateTime->modify('first day of this month');
185
186 $endDateTime->modify('+' . ($monthsLoad - 1) . 'months');
187
188 $endDateTime->modify('last day of this month');
189
190 $endDateTime->modify('+12days');
191
192 $endDateTime->setTime(23, 59, 59);
193
194 if ($isFrontEndBooking) {
195 $endDateTime = $endDateTime > $maximumDateTime ?
196 DateTimeService::getDateTimeObjectInTimeZone(
197 $maximumDateTime->format('Y-m-d H:i'),
198 $timeZone
199 ) : $endDateTime;
200 }
201
202 $endDateTime->setTimezone(DateTimeService::getTimeZone());
203 }
204
205 /** @var Service $filteredSlotEntitiesService */
206 foreach ($filteredSlotEntities->getServices()->getItems() as $filteredSlotEntitiesService) {
207 if ($filteredSlotEntitiesService->getId()->getValue() === $service->getId()->getValue()) {
208 $filteredSlotEntitiesService->setDuration($service->getDuration());
209
210 break;
211 }
212 }
213
214 /** @var Provider $filteredSlotEntitiesProvider */
215 foreach ($filteredSlotEntities->getProviders()->getItems() as $filteredSlotEntitiesProvider) {
216 /** @var Service $providerService */
217 foreach ($filteredSlotEntitiesProvider->getServiceList()->getItems() as $providerService) {
218 if ($providerService->getId()->getValue() === $service->getId()->getValue()) {
219 $providerService->setDuration($service->getDuration());
220
221 break;
222 }
223 }
224 }
225
226 $freeSlots = $applicationTimeSlotService->getSlotsByProps(
227 $settings,
228 array_merge(
229 $props,
230 [
231 'startDateTime' => $startDateTime,
232 'endDateTime' => $endDateTime,
233 ]
234 ),
235 $filteredSlotEntities
236 );
237
238 if ($loadGeneratedPeriod) {
239 // search with new period until slots are not found
240 while (!$freeSlots['available'] && $endDateTime && $endDateTime <= $maximumDateTime) {
241 $startDateTime = DateTimeService::getCustomDateTimeObject(
242 $endDateTime->format('Y-m-d H:i:s')
243 )->setTimezone(
244 new DateTimeZone($timeZone)
245 );
246
247 $startDateTime->setTime(0, 0, 0);
248
249 $endDateTime->modify('first day of this month');
250
251 $endDateTime->modify('+' . ($monthsLoad - 1) . 'months');
252
253 $endDateTime->modify('last day of this month');
254
255 $endDateTime->modify('+12days');
256
257 $endDateTime->setTime(23, 59, 59);
258
259 if ($isFrontEndBooking) {
260 $endDateTime = $endDateTime > $maximumDateTime ?
261 DateTimeService::getDateTimeObjectInTimeZone(
262 $maximumDateTime->format('Y-m-d H:i'),
263 $timeZone
264 ) : $endDateTime;
265 }
266
267 $endDateTime->setTimezone(DateTimeService::getTimeZone());
268
269 AbstractGoogleCalendarService::$providersGoogleEvents = [];
270
271 AbstractOutlookCalendarService::$providersOutlookEvents = [];
272
273 AbstractAppleCalendarService::$providersAppleEvents = [];
274
275 $freeSlots = $applicationTimeSlotService->getSlotsByProps(
276 $settings,
277 array_merge(
278 $props,
279 [
280 'startDateTime' => $startDateTime,
281 'endDateTime' => $endDateTime,
282 ]
283 ),
284 $filteredSlotEntities
285 );
286
287 $endDateTimeCopy = clone $endDateTime;
288
289 $maximumDateTimeCopy = clone $maximumDateTime;
290
291 if (
292 $endDateTimeCopy->format('Y-m-d H:i') === $maximumDateTimeCopy->format('Y-m-d H:i') ||
293 ($endDateTimeCopy->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i') ===
294 $maximumDateTimeCopy->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i')) ||
295 ($endDateTimeCopy > $maximumDateTimeCopy)
296 ) {
297 break;
298 }
299 }
300
301 // search once more if first available date is in 11 days added to endDateTime (days outside calendar on frontend form)
302 foreach (array_slice($freeSlots['available'], 0, 1, true) as $slotDate => $slotTimes) {
303 if (substr($slotDate, 0, 7) === $endDateTime->format('Y-m')) {
304 $endDateTime->modify('last day of this month');
305
306 $endDateTime->modify('+12days');
307
308 $endDateTime->setTime(23, 59, 59);
309
310 if ($isFrontEndBooking) {
311 $endDateTime = $endDateTime > $maximumDateTime ?
312 DateTimeService::getDateTimeObjectInTimeZone(
313 $maximumDateTime->format('Y-m-d H:i'),
314 $timeZone
315 ) : $endDateTime;
316 }
317
318 AbstractGoogleCalendarService::$providersGoogleEvents = [];
319
320 AbstractOutlookCalendarService::$providersOutlookEvents = [];
321
322 AbstractAppleCalendarService::$providersAppleEvents = [];
323
324 $freeSlots = $applicationTimeSlotService->getSlotsByProps(
325 $settings,
326 array_merge(
327 $props,
328 [
329 'startDateTime' => $startDateTime,
330 'endDateTime' => $endDateTime,
331 ]
332 ),
333 $filteredSlotEntities
334 );
335 }
336 }
337 }
338
339 $busyness = [];
340
341 foreach ($freeSlots['available'] as $slotDate => $slotTimes) {
342 $busyness[$slotDate] = round(
343 count(!empty($freeSlots['occupied'][$slotDate]) ? $freeSlots['occupied'][$slotDate] : []) /
344 (count(!empty($freeSlots['available'][$slotDate]) ? $freeSlots['available'][$slotDate] : []) +
345 count(!empty($freeSlots['occupied'][$slotDate]) ? $freeSlots['occupied'][$slotDate] : []))
346 * 100
347 );
348 }
349
350 $converted = ['available' => [], 'occupied' => []];
351
352 $isUtcResponse = ($settingsDS->getSetting('general', 'showClientTimeZone') && $isFrontEndBooking) ||
353 $props['timeZone'];
354
355 if ($isUtcResponse) {
356 foreach (['available', 'occupied'] as $type) {
357 foreach ($freeSlots[$type] as $slotDate => $slotTimes) {
358 foreach ($freeSlots[$type][$slotDate] as $slotTime => $slotTimesProviders) {
359 $convertedSlotParts = explode(
360 ' ',
361 $props['timeZone'] ?
362 DateTimeService::getCustomDateTimeObjectInTimeZone(
363 $slotDate . ' ' . $slotTime,
364 $props['timeZone']
365 )->format('Y-m-d H:i') :
366 DateTimeService::getCustomDateTimeObjectInUtc(
367 $slotDate . ' ' . $slotTime
368 )->format('Y-m-d H:i')
369 );
370
371 $converted[$type][$convertedSlotParts[0]][$convertedSlotParts[1]] = $slotTimesProviders;
372 }
373 }
374 }
375 }
376
377 if (count($props['providerIds']) !== 1 && $settingsDS->getSetting('appointments', 'employeeSelection') === 'roundRobin') {
378 /** @var AppointmentRepository $appointmentRepository */
379 $appointmentRepository = $this->container->get('domain.booking.appointment.repository');
380
381 $lastBookedProviderId = $appointmentRepository->getLastBookedEmployee($props['providerIds']);
382 }
383
384 $resultData = [
385 'slots' => $converted['available'] ?: $freeSlots['available'],
386 'occupied' => $converted['occupied'] ?: $freeSlots['occupied'],
387 'minimum' => $isUtcResponse ?
388 $minimumDateTime->setTimezone(
389 new DateTimeZone('UTC')
390 )->format('Y-m-d H:i') : $minimumDateTime->format('Y-m-d H:i'),
391 'maximum' => $isUtcResponse ?
392 $maximumDateTime->setTimezone(
393 new DateTimeZone('UTC')
394 )->format('Y-m-d H:i') : $maximumDateTime->format('Y-m-d H:i'),
395 'busyness' => $busyness,
396 'lastBookedProviderId' => $lastBookedProviderId,
397 'appCount' => $freeSlots['appCount'],
398 'duration' => $freeSlots['duration'],
399 ];
400
401
402 $resultData = apply_filters('amelia_get_timeslots_filter', $resultData, $props);
403
404 do_action('amelia_get_timeslots', $resultData, $props);
405
406
407 $result->setResult(CommandResult::RESULT_SUCCESS);
408 $result->setMessage('Successfully retrieved free slots');
409 $result->setData(
410 [
411 'minimum' => $resultData['minimum'],
412 'maximum' => $resultData['maximum'],
413 'slots' => $resultData['slots'],
414 'occupied' => $resultData['occupied'],
415 'busyness' => $resultData['busyness'],
416 'lastProvider' => $resultData['lastBookedProviderId'],
417 'appCount' => $resultData['appCount'],
418 'duration' => $resultData['duration'],
419 ]
420 );
421
422 return $result;
423 }
424 }
425