CalendarRescheduleEventCommand.php
6 months ago
CalendarRescheduleEventCommandHandler.php
6 months ago
GetCalendarEventsCommand.php
6 months ago
GetCalendarEventsCommandHandler.php
6 months ago
GetCalendarSlotAvailabilityCommand.php
6 months ago
GetCalendarSlotAvailabilityHandler.php
6 months ago
GetCalendarSlotEntitiesCommand.php
6 months ago
GetCalendarSlotEntitiesCommandHandler.php
6 months ago
GetCalendarSlotsCommand.php
6 months ago
GetCalendarSlotsCommandHandler.php
6 months ago
GetCalendarSlotEntitiesCommandHandler.php
167 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Application\Commands\Calendar; |
| 9 | |
| 10 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 11 | use AmeliaBooking\Application\Commands\CommandResult; |
| 12 | use AmeliaBooking\Application\Services\Bookable\BookableApplicationService; |
| 13 | use AmeliaBooking\Domain\Services\Entity\EntityService; |
| 14 | use AmeliaBooking\Domain\Services\TimeSlot\TimeSlotService; |
| 15 | use AmeliaBooking\Application\Services\TimeSlot\TimeSlotService as ApplicationTimeSlotService; |
| 16 | use AmeliaBooking\Domain\Collection\Collection; |
| 17 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 18 | use AmeliaBooking\Domain\Entity\Booking\SlotsEntities; |
| 19 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 20 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 21 | use Exception; |
| 22 | |
| 23 | class GetCalendarSlotEntitiesCommandHandler extends CommandHandler |
| 24 | { |
| 25 | public function handle(GetCalendarSlotEntitiesCommand $command) |
| 26 | { |
| 27 | $result = new CommandResult(); |
| 28 | |
| 29 | $params = $command->getFields(); |
| 30 | |
| 31 | /** @var EntityService $entityService */ |
| 32 | $entityService = $this->container->get('domain.entity.service'); |
| 33 | /** @var TimeSlotService $timeSlotService */ |
| 34 | $timeSlotService = $this->container->get('domain.timeSlot.service'); |
| 35 | /** @var ApplicationTimeSlotService $applicationTimeSlotService */ |
| 36 | $applicationTimeSlotService = $this->container->get('application.timeSlot.service'); |
| 37 | |
| 38 | $searchStartDateString = $params['date']; |
| 39 | $searchEndDateString = $params['date']; |
| 40 | $searchTime = $params['time'] ?? null; |
| 41 | |
| 42 | $slotsEntities = $applicationTimeSlotService->getSlotsEntities( |
| 43 | [ |
| 44 | 'isFrontEndBooking' => false, |
| 45 | 'providerIds' => !empty($params['providers']) ? $params['providers'] : [], |
| 46 | ] |
| 47 | ); |
| 48 | |
| 49 | $startDateTimeObject = DateTimeService::getCustomDateTimeObject($searchStartDateString . ' 00:00:00'); |
| 50 | $endDateTimeObject = DateTimeService::getCustomDateTimeObject($searchEndDateString . ' 23:59:00'); |
| 51 | |
| 52 | $props = [ |
| 53 | 'providerIds' => !empty($params['providers']) ? $params['providers'] : [], |
| 54 | 'locationId' => !empty($params['location']) ? (int)$params['location'] : null, |
| 55 | 'extras' => [], |
| 56 | 'excludeAppointmentId' => null, |
| 57 | 'personsCount' => 1, |
| 58 | 'isFrontEndBooking' => false, |
| 59 | 'startDateTime' => $startDateTimeObject->modify('-1 days'), |
| 60 | 'endDateTime' => $endDateTimeObject->modify('+1 days'), |
| 61 | ]; |
| 62 | |
| 63 | $settings = $applicationTimeSlotService->getSlotsSettings(false, $slotsEntities); |
| 64 | |
| 65 | $applicationTimeSlotService->setBlockerAppointments($slotsEntities->getProviders(), $props); |
| 66 | |
| 67 | $appointments = $applicationTimeSlotService->getBookedAppointments($slotsEntities, $props); |
| 68 | $servicesIds = !empty($params['services']) ? $params['services'] : $slotsEntities->getServices()->keys(); |
| 69 | |
| 70 | $resultServicesIds = []; |
| 71 | $resultProvidersIds = []; |
| 72 | $resultLocationIds = []; |
| 73 | foreach ($servicesIds as $serviceId) { |
| 74 | $filteredSlotEntities = $entityService->getFilteredSlotsEntities( |
| 75 | $settings, |
| 76 | array_merge($props, ['serviceId' => $serviceId]), |
| 77 | $slotsEntities |
| 78 | ); |
| 79 | |
| 80 | /** @var Service $service */ |
| 81 | $service = $filteredSlotEntities->getServices()->getItem($serviceId); |
| 82 | |
| 83 | if (!$service->getShow()->getValue()) { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | $startDateTime = DateTimeService::getCustomDateTimeObject($searchStartDateString); |
| 88 | |
| 89 | $endDateTime = DateTimeService::getCustomDateTimeObject($searchEndDateString); |
| 90 | |
| 91 | $freeSlots = $timeSlotService->getSlots( |
| 92 | $settings, |
| 93 | array_merge( |
| 94 | $props, |
| 95 | [ |
| 96 | 'serviceId' => $serviceId, |
| 97 | 'startDateTime' => $startDateTime, |
| 98 | 'endDateTime' => $endDateTime->modify('+1 day'), |
| 99 | ] |
| 100 | ), |
| 101 | $filteredSlotEntities, |
| 102 | $appointments |
| 103 | )['available']; |
| 104 | |
| 105 | if ($searchTime && array_key_exists($searchStartDateString, $freeSlots)) { |
| 106 | $freeSlots = $this->filterByTime($searchStartDateString, $searchTime, $freeSlots); |
| 107 | } |
| 108 | |
| 109 | if (!array_key_exists($searchStartDateString, $freeSlots)) { |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | foreach ($freeSlots as $dateSlot) { |
| 114 | foreach ($dateSlot as $timeSlot) { |
| 115 | foreach ($timeSlot as $infoSlot) { |
| 116 | $resultProvidersIds[] = $infoSlot[0]; |
| 117 | $resultLocationIds[] = $infoSlot[1]; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | $resultServicesIds[] = $serviceId; |
| 123 | } |
| 124 | |
| 125 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 126 | $result->setMessage('Successfully retrieved searched services.'); |
| 127 | $result->setData( |
| 128 | [ |
| 129 | 'services' => $resultServicesIds, |
| 130 | 'employees' => array_values(array_unique($resultProvidersIds)), |
| 131 | 'locations' => array_values(array_unique($resultLocationIds)), |
| 132 | ] |
| 133 | ); |
| 134 | |
| 135 | return $result; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @param $date |
| 140 | * @param $time |
| 141 | * @param $freeSlots |
| 142 | * |
| 143 | * @return mixed |
| 144 | * |
| 145 | * @throws Exception |
| 146 | */ |
| 147 | private function filterByTime($date, $time, $freeSlots) |
| 148 | { |
| 149 | foreach (array_keys($freeSlots[$date]) as $freeSlotKey) { |
| 150 | if ( |
| 151 | DateTimeService::getCustomDateTimeObject($date . ' ' . $freeSlotKey) >= |
| 152 | DateTimeService::getCustomDateTimeObject($date . ' ' . $time) |
| 153 | ) { |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | unset($freeSlots[$date][$freeSlotKey]); |
| 158 | |
| 159 | if (empty($freeSlots[$date])) { |
| 160 | unset($freeSlots[$date]); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return $freeSlots; |
| 165 | } |
| 166 | } |
| 167 |