EntityService.php
243 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Domain\Services\Entity; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Collection\Collection; |
| 6 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 7 | use AmeliaBooking\Domain\Entity\Bookable\Service\Extra; |
| 8 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 9 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 10 | use AmeliaBooking\Domain\Entity\Booking\SlotsEntities; |
| 11 | use AmeliaBooking\Domain\Entity\User\Provider; |
| 12 | use AmeliaBooking\Domain\Factory\Booking\SlotsEntitiesFactory; |
| 13 | use AmeliaBooking\Domain\Services\Resource\AbstractResourceService; |
| 14 | use AmeliaBooking\Domain\Services\User\ProviderService; |
| 15 | use AmeliaBooking\Domain\ValueObjects\Duration; |
| 16 | |
| 17 | /** |
| 18 | * Class EntityService |
| 19 | * |
| 20 | * @package AmeliaBooking\Domain\Services\Entity |
| 21 | */ |
| 22 | class EntityService |
| 23 | { |
| 24 | /** @var ProviderService */ |
| 25 | private $providerService; |
| 26 | |
| 27 | /** @var AbstractResourceService */ |
| 28 | private $resourceService; |
| 29 | |
| 30 | /** |
| 31 | * EntityService constructor. |
| 32 | * |
| 33 | * @param ProviderService $providerService |
| 34 | * @param AbstractResourceService $resourceService |
| 35 | */ |
| 36 | public function __construct( |
| 37 | ProviderService $providerService, |
| 38 | AbstractResourceService $resourceService |
| 39 | ) { |
| 40 | $this->providerService = $providerService; |
| 41 | |
| 42 | $this->resourceService = $resourceService; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * get filtered entities needed for slots calculation. |
| 47 | * |
| 48 | * @param array $settings |
| 49 | * @param array $props |
| 50 | * @param SlotsEntities $slotsEntities |
| 51 | * |
| 52 | * @return SlotsEntities |
| 53 | * @throws InvalidArgumentException |
| 54 | */ |
| 55 | public function getFilteredSlotsEntities($settings, $props, $slotsEntities) |
| 56 | { |
| 57 | /** @var Collection $services */ |
| 58 | $services = $slotsEntities->getServices() ?: new Collection(); |
| 59 | |
| 60 | /** @var Collection $providers */ |
| 61 | $providers = $slotsEntities->getProviders() ?: new Collection(); |
| 62 | |
| 63 | /** @var Collection $locations */ |
| 64 | $locations = $slotsEntities->getLocations() ?: new Collection(); |
| 65 | |
| 66 | if (!empty($props['locationIds'])) { |
| 67 | /** @var Collection $filteredLocations */ |
| 68 | $filteredLocations = new Collection(); |
| 69 | foreach ($props['locationIds'] as $locationId) { |
| 70 | if ($locations->keyExists($locationId)) { |
| 71 | $filteredLocations->addItem( |
| 72 | $locations->getItem($locationId), |
| 73 | $locationId |
| 74 | ); |
| 75 | } |
| 76 | } |
| 77 | $locations = $filteredLocations; |
| 78 | } |
| 79 | |
| 80 | /** @var Collection $filteredProviders */ |
| 81 | $filteredProviders = new Collection(); |
| 82 | |
| 83 | /** @var Provider $provider */ |
| 84 | foreach ($providers->getItems() as $provider) { |
| 85 | if ($settings['timezonesFeatureEnabled'] === false) { |
| 86 | $provider->setTimeZone(null); |
| 87 | } |
| 88 | |
| 89 | if ($provider->getServiceList()->keyExists($props['serviceId'])) { |
| 90 | if ($settings['allowAdminBookAtAnyTime']) { |
| 91 | $this->providerService->setProvidersAlwaysAvailable( |
| 92 | $providers |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | $this->providerService->setProviderServices( |
| 97 | $provider, |
| 98 | $services, |
| 99 | false |
| 100 | ); |
| 101 | |
| 102 | /** @var Service $service */ |
| 103 | foreach ($provider->getServiceList()->getItems() as $service) { |
| 104 | $this->checkServiceTimes($service); |
| 105 | } |
| 106 | |
| 107 | $filteredProviders->addItem($provider, $provider->getId()->getValue()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** @var Service $service */ |
| 112 | foreach ($services->getItems() as $service) { |
| 113 | $this->checkServiceTimes($service); |
| 114 | } |
| 115 | |
| 116 | /** @var Collection $serviceResources */ |
| 117 | $serviceResources = $slotsEntities->getResources() ? $this->resourceService->getServiceResources( |
| 118 | $slotsEntities->getResources(), |
| 119 | $props['serviceId'] |
| 120 | ) : new Collection(); |
| 121 | |
| 122 | $this->resourceService->setNonSharedResources( |
| 123 | $serviceResources, |
| 124 | [ |
| 125 | 'service' => $services->keys(), |
| 126 | 'location' => $locations->keys(), |
| 127 | ], |
| 128 | $props['serviceId'] |
| 129 | ); |
| 130 | |
| 131 | /** @var SlotsEntities $filteredSlotsEntities */ |
| 132 | $filteredSlotsEntities = SlotsEntitiesFactory::create(); |
| 133 | |
| 134 | $filteredSlotsEntities->setServices($services); |
| 135 | |
| 136 | $filteredSlotsEntities->setProviders($filteredProviders); |
| 137 | |
| 138 | $filteredSlotsEntities->setLocations($locations); |
| 139 | |
| 140 | $filteredSlotsEntities->setResources($serviceResources); |
| 141 | |
| 142 | return $filteredSlotsEntities; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Add 0 as duration for service time before or time after if it is null |
| 147 | * |
| 148 | * @param Service $service |
| 149 | * |
| 150 | * @throws InvalidArgumentException |
| 151 | */ |
| 152 | private function checkServiceTimes($service) |
| 153 | { |
| 154 | if (!$service->getTimeBefore()) { |
| 155 | $service->setTimeBefore(new Duration(0)); |
| 156 | } |
| 157 | |
| 158 | if (!$service->getTimeAfter()) { |
| 159 | $service->setTimeAfter(new Duration(0)); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * filter appointments required for slots calculation |
| 165 | * |
| 166 | * @param SlotsEntities $slotsEntities |
| 167 | * @param Collection $appointments |
| 168 | * @param array $props |
| 169 | * |
| 170 | * @return void |
| 171 | * @throws InvalidArgumentException |
| 172 | */ |
| 173 | public function filterSlotsAppointments($slotsEntities, $appointments, $props) |
| 174 | { |
| 175 | /** @var Collection $services */ |
| 176 | $services = $slotsEntities->getServices(); |
| 177 | |
| 178 | /** @var Collection $providers */ |
| 179 | $providers = $slotsEntities->getProviders(); |
| 180 | |
| 181 | $providersIds = $providers->keys(); |
| 182 | |
| 183 | /** @var Appointment $appointment */ |
| 184 | foreach ($appointments->getItems() as $index => $appointment) { |
| 185 | if ( |
| 186 | !in_array($appointment->getProviderId()->getValue(), $providersIds) || |
| 187 | ( |
| 188 | $props['excludeAppointmentId'] && $index === $props['excludeAppointmentId'] |
| 189 | ) |
| 190 | ) { |
| 191 | $appointments->deleteItem($index); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** @var Appointment $appointment */ |
| 196 | foreach ($appointments->getItems() as $index => $appointment) { |
| 197 | /** @var Provider $provider */ |
| 198 | $provider = $providers->getItem($appointment->getProviderId()->getValue()); |
| 199 | |
| 200 | /** @var Service $providerService */ |
| 201 | $providerService = $provider->getServiceList()->keyExists($appointment->getServiceId()->getValue()) ? |
| 202 | $provider->getServiceList()->getItem($appointment->getServiceId()->getValue()) : |
| 203 | $services->getItem($appointment->getServiceId()->getValue()); |
| 204 | |
| 205 | $appointment->setService($providerService); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Return required time for the appointment in seconds by summing service duration, service time before and after |
| 211 | * and each passed extra. |
| 212 | * |
| 213 | * @param Service $service |
| 214 | * @param array $selectedExtras |
| 215 | * |
| 216 | * @return mixed |
| 217 | * @throws InvalidArgumentException |
| 218 | */ |
| 219 | public function getAppointmentRequiredTime($service, $selectedExtras) |
| 220 | { |
| 221 | $requiredTime = |
| 222 | $service->getTimeBefore()->getValue() + |
| 223 | $service->getDuration()->getValue() + |
| 224 | $service->getTimeAfter()->getValue(); |
| 225 | |
| 226 | $extraIds = array_column($selectedExtras, 'id'); |
| 227 | |
| 228 | /** @var Extra $extra */ |
| 229 | foreach ($service->getExtras()->getItems() as $extra) { |
| 230 | if (in_array($extra->getId()->getValue(), $extraIds, false)) { |
| 231 | if (!$extra->getDuration()) { |
| 232 | $extra->setDuration(new Duration(0)); |
| 233 | } |
| 234 | |
| 235 | $requiredTime += ($extra->getDuration()->getValue() * |
| 236 | array_column($selectedExtras, 'quantity', 'id')[$extra->getId()->getValue()]); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return $requiredTime; |
| 241 | } |
| 242 | } |
| 243 |