ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
GetTimeSlotsCommandHandler.php
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
5 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
GetTimeSlotsCommandHandler.php
434 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 ($endDateTimeCopy->format('Y-m-d H:i') === $maximumDateTimeCopy->format('Y-m-d H:i') || |
| 292 | ($endDateTimeCopy->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i') === |
| 293 | $maximumDateTimeCopy->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i')) || |
| 294 | ($endDateTimeCopy > $maximumDateTimeCopy) |
| 295 | ) { |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // search once more if first available date is in 11 days added to endDateTime (days outside calendar on frontend form) |
| 301 | foreach (array_slice($freeSlots['available'], 0, 1, true) as $slotDate => $slotTimes) { |
| 302 | if (substr($slotDate, 0, 7) === $endDateTime->format('Y-m')) { |
| 303 | $endDateTime->modify('last day of this month'); |
| 304 | |
| 305 | $endDateTime->modify('+12days'); |
| 306 | |
| 307 | $endDateTime->setTime(23, 59, 59); |
| 308 | |
| 309 | if ($isFrontEndBooking) { |
| 310 | $endDateTime = $endDateTime > $maximumDateTime ? |
| 311 | DateTimeService::getDateTimeObjectInTimeZone( |
| 312 | $maximumDateTime->format('Y-m-d H:i'), |
| 313 | $timeZone |
| 314 | ) : $endDateTime; |
| 315 | } |
| 316 | |
| 317 | AbstractGoogleCalendarService::$providersGoogleEvents = []; |
| 318 | |
| 319 | AbstractOutlookCalendarService::$providersOutlookEvents = []; |
| 320 | |
| 321 | AbstractAppleCalendarService::$providersAppleEvents = []; |
| 322 | |
| 323 | $freeSlots = $applicationTimeSlotService->getSlotsByProps( |
| 324 | $settings, |
| 325 | array_merge( |
| 326 | $props, |
| 327 | [ |
| 328 | 'startDateTime' => $startDateTime, |
| 329 | 'endDateTime' => $endDateTime, |
| 330 | ] |
| 331 | ), |
| 332 | $filteredSlotEntities |
| 333 | ); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | $busyness = []; |
| 339 | |
| 340 | foreach ($freeSlots['available'] as $slotDate => $slotTimes) { |
| 341 | if (!empty($freeSlots['continuousAppointments'][$slotDate]) && !empty($freeSlots['occupied'][$slotDate])) { |
| 342 | $freeSlots['occupied'][$slotDate] = |
| 343 | array_merge( |
| 344 | $freeSlots['occupied'][$slotDate], |
| 345 | $freeSlots['continuousAppointments'][$slotDate] |
| 346 | ); |
| 347 | |
| 348 | foreach ($freeSlots['occupied'][$slotDate] as $key => $timeKey) { |
| 349 | $freeSlots['occupied'][$slotDate][$key] = |
| 350 | [0 => reset($freeSlots['occupied'][$slotDate])[0]]; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | $busyness[$slotDate] = round( |
| 355 | count(!empty($freeSlots['occupied'][$slotDate]) ? $freeSlots['occupied'][$slotDate] : []) / |
| 356 | (count(!empty($freeSlots['available'][$slotDate]) ? $freeSlots['available'][$slotDate] : []) + |
| 357 | count(!empty($freeSlots['occupied'][$slotDate]) ? $freeSlots['occupied'][$slotDate] : [])) |
| 358 | * 100); |
| 359 | } |
| 360 | |
| 361 | $converted = ['available' => [], 'occupied' => []]; |
| 362 | |
| 363 | $isUtcResponse = ($settingsDS->getSetting('general', 'showClientTimeZone') && $isFrontEndBooking) || |
| 364 | $props['timeZone']; |
| 365 | |
| 366 | if ($isUtcResponse) { |
| 367 | foreach (['available', 'occupied'] as $type) { |
| 368 | foreach ($freeSlots[$type] as $slotDate => $slotTimes) { |
| 369 | foreach ($freeSlots[$type][$slotDate] as $slotTime => $slotTimesProviders) { |
| 370 | $convertedSlotParts = explode( |
| 371 | ' ', |
| 372 | $props['timeZone'] ? |
| 373 | DateTimeService::getCustomDateTimeObjectInTimeZone( |
| 374 | $slotDate . ' ' . $slotTime, |
| 375 | $props['timeZone'] |
| 376 | )->format('Y-m-d H:i') : |
| 377 | DateTimeService::getCustomDateTimeObjectInUtc( |
| 378 | $slotDate . ' ' . $slotTime |
| 379 | )->format('Y-m-d H:i') |
| 380 | ); |
| 381 | |
| 382 | $converted[$type][$convertedSlotParts[0]][$convertedSlotParts[1]] = $slotTimesProviders; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if (count($props['providerIds']) !== 1 && $settingsDS->getSetting('appointments', 'employeeSelection') === 'roundRobin') { |
| 389 | /** @var AppointmentRepository $appointmentRepository */ |
| 390 | $appointmentRepository = $this->container->get('domain.booking.appointment.repository'); |
| 391 | |
| 392 | $lastBookedProviderId = $appointmentRepository->getLastBookedEmployee($props['providerIds']); |
| 393 | } |
| 394 | |
| 395 | $resultData = [ |
| 396 | 'slots' => $converted['available'] ?: $freeSlots['available'], |
| 397 | 'occupied' => $converted['occupied'] ?: $freeSlots['occupied'], |
| 398 | 'minimum' => $isUtcResponse ? |
| 399 | $minimumDateTime->setTimezone( |
| 400 | new DateTimeZone('UTC') |
| 401 | )->format('Y-m-d H:i') : $minimumDateTime->format('Y-m-d H:i'), |
| 402 | 'maximum' => $isUtcResponse ? |
| 403 | $maximumDateTime->setTimezone( |
| 404 | new DateTimeZone('UTC') |
| 405 | )->format('Y-m-d H:i') : $maximumDateTime->format('Y-m-d H:i'), |
| 406 | 'busyness' => $busyness, |
| 407 | 'lastBookedProviderId' => $lastBookedProviderId, |
| 408 | 'appCount' => $freeSlots['appCount'] |
| 409 | ]; |
| 410 | |
| 411 | |
| 412 | $resultData = apply_filters('amelia_get_timeslots_filter', $resultData, $props); |
| 413 | |
| 414 | do_action('amelia_get_timeslots', $resultData, $props); |
| 415 | |
| 416 | |
| 417 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 418 | $result->setMessage('Successfully retrieved free slots'); |
| 419 | $result->setData( |
| 420 | [ |
| 421 | 'minimum' => $resultData['minimum'], |
| 422 | 'maximum' => $resultData['maximum'], |
| 423 | 'slots' => $resultData['slots'], |
| 424 | 'occupied' => $resultData['occupied'], |
| 425 | 'busyness' => $resultData['busyness'], |
| 426 | 'lastProvider' => $resultData['lastBookedProviderId'], |
| 427 | 'appCount' => $resultData['appCount'] |
| 428 | ] |
| 429 | ); |
| 430 | |
| 431 | return $result; |
| 432 | } |
| 433 | } |
| 434 |