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