StashApplicationService.php
490 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Services\Stash; |
| 4 | |
| 5 | use AmeliaBooking\Application\Services\Booking\EventApplicationService; |
| 6 | use AmeliaBooking\Application\Services\Location\AbstractLocationApplicationService; |
| 7 | use AmeliaBooking\Application\Services\Tax\TaxApplicationService; |
| 8 | use AmeliaBooking\Application\Services\User\ProviderApplicationService; |
| 9 | use AmeliaBooking\Domain\Collection\Collection; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 11 | use AmeliaBooking\Domain\Entity\Bookable\Service\Category; |
| 12 | use AmeliaBooking\Domain\Entity\Bookable\Service\Extra; |
| 13 | use AmeliaBooking\Domain\Entity\Bookable\Service\Package; |
| 14 | use AmeliaBooking\Domain\Entity\Bookable\Service\PackageService; |
| 15 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 16 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 17 | use AmeliaBooking\Domain\Entity\CustomField\CustomField; |
| 18 | use AmeliaBooking\Domain\Entity\Location\Location; |
| 19 | use AmeliaBooking\Domain\Entity\Tax\Tax; |
| 20 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 21 | use AmeliaBooking\Domain\Entity\User\Provider; |
| 22 | use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory; |
| 23 | use AmeliaBooking\Domain\Factory\Location\LocationFactory; |
| 24 | use AmeliaBooking\Domain\Factory\User\ProviderFactory; |
| 25 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 26 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 27 | use AmeliaBooking\Domain\Services\User\ProviderService; |
| 28 | use AmeliaBooking\Infrastructure\Common\Container; |
| 29 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 30 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\CategoryRepository; |
| 31 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\PackageRepository; |
| 32 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 33 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventTagsRepository; |
| 34 | use AmeliaBooking\Infrastructure\Repository\CustomField\CustomFieldRepository; |
| 35 | use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository; |
| 36 | use Interop\Container\Exception\ContainerException; |
| 37 | |
| 38 | /** |
| 39 | * Class StashApplicationService |
| 40 | * |
| 41 | * @package AmeliaBooking\Application\Stash |
| 42 | */ |
| 43 | class StashApplicationService |
| 44 | { |
| 45 | private $container; |
| 46 | |
| 47 | /** |
| 48 | * StashApplicationService constructor. |
| 49 | * |
| 50 | * @param Container $container |
| 51 | * |
| 52 | */ |
| 53 | public function __construct(Container $container) |
| 54 | { |
| 55 | $this->container = $container; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param array $daysOffNew |
| 60 | * |
| 61 | * @return void |
| 62 | * @throws QueryExecutionException |
| 63 | * @throws InvalidArgumentException |
| 64 | * @throws ContainerException |
| 65 | */ |
| 66 | public function setStash($daysOffNew = null) |
| 67 | { |
| 68 | /** @var SettingsService $settingsDomainService */ |
| 69 | $settingsDomainService = $this->container->get('domain.settings.service'); |
| 70 | |
| 71 | /** @var ProviderApplicationService $providerAS */ |
| 72 | $providerAS = $this->container->get('application.user.provider.service'); |
| 73 | |
| 74 | /** @var EventApplicationService $eventAS */ |
| 75 | $eventAS = $this->container->get('application.booking.event.service'); |
| 76 | |
| 77 | /** @var ProviderService $providerService */ |
| 78 | $providerService = $this->container->get('domain.user.provider.service'); |
| 79 | |
| 80 | /** @var AbstractLocationApplicationService $locationAS */ |
| 81 | $locationAS = $this->container->get('application.location.service'); |
| 82 | |
| 83 | /** @var ServiceRepository $serviceRepository */ |
| 84 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 85 | |
| 86 | /** @var CategoryRepository $categoryRepository */ |
| 87 | $categoryRepository = $this->container->get('domain.bookable.category.repository'); |
| 88 | |
| 89 | /** @var ProviderRepository $providerRepository */ |
| 90 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 91 | |
| 92 | /** @var CustomFieldRepository $customFieldRepository */ |
| 93 | $customFieldRepository = $this->container->get('domain.customField.repository'); |
| 94 | |
| 95 | /** @var EventTagsRepository $eventTagsRepository */ |
| 96 | $eventTagsRepository = $this->container->get('domain.booking.event.tag.repository'); |
| 97 | |
| 98 | /** @var Collection $events */ |
| 99 | $events = $eventAS->getEventsByCriteria( |
| 100 | [ |
| 101 | 'dates' => [DateTimeService::getNowDateTime()], |
| 102 | 'show' => 1 |
| 103 | ], |
| 104 | [ |
| 105 | ], |
| 106 | 0 |
| 107 | ); |
| 108 | |
| 109 | /** @var TaxApplicationService $taxApplicationService */ |
| 110 | $taxApplicationService = $this->container->get('application.tax.service'); |
| 111 | |
| 112 | /** @var Collection $taxes */ |
| 113 | $taxes = $taxApplicationService->getAll(); |
| 114 | |
| 115 | /** @var Collection $services */ |
| 116 | $services = $serviceRepository->getAllArrayIndexedById(); |
| 117 | |
| 118 | /** @var Collection $locations */ |
| 119 | $locations = $locationAS->getAllOrderedByName(); |
| 120 | |
| 121 | /** @var Collection $providers */ |
| 122 | $providers = $providerRepository->getWithSchedule( |
| 123 | ['dates' => [DateTimeService::getNowDateTimeObject()->modify('-1 days')->format('Y-m-d H:i:s')]] |
| 124 | ); |
| 125 | |
| 126 | |
| 127 | /** @var Collection $availableLocations */ |
| 128 | $availableLocations = new Collection(); |
| 129 | |
| 130 | /** @var Collection $availableServices */ |
| 131 | $availableServices = new Collection(); |
| 132 | |
| 133 | /** @var Collection $availableProviders */ |
| 134 | $availableProviders = new Collection(); |
| 135 | |
| 136 | $entitiesRelations = []; |
| 137 | |
| 138 | /** @var Provider $provider */ |
| 139 | foreach ($providers->getItems() as $providerId => $provider) { |
| 140 | if ($provider->getLocationId() && !$availableLocations->keyExists($provider->getLocationId()->getValue())) { |
| 141 | $availableLocations->addItem( |
| 142 | $locations->getItem($provider->getLocationId()->getValue()), |
| 143 | $provider->getLocationId()->getValue() |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | $providerService->setProviderServices($provider, $services, true); |
| 148 | |
| 149 | if ($data = $providerAS->getProviderServiceLocations($provider, $locations, $services, true)) { |
| 150 | $entitiesRelations[$providerId] = $data; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | foreach ($entitiesRelations as $providerId => $providerServiceRelations) { |
| 155 | foreach ($providerServiceRelations as $serviceId => $serviceLocationRelations) { |
| 156 | foreach ($serviceLocationRelations as $locationId) { |
| 157 | if ($locationId && !$availableLocations->keyExists($locationId)) { |
| 158 | $availableLocations->addItem($locations->getItem($locationId), $locationId); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (!$availableServices->keyExists($serviceId)) { |
| 163 | $availableServices->addItem($services->getItem($serviceId), $serviceId); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | $availableProviders->addItem($providers->getItem($providerId), $providerId); |
| 168 | } |
| 169 | |
| 170 | /** @var \AmeliaBooking\Application\Services\Settings\SettingsService $settingsAS*/ |
| 171 | $settingsAS = $this->container->get('application.settings.service'); |
| 172 | $daysOff = $settingsAS->getDaysOff($daysOffNew); |
| 173 | |
| 174 | $resultData = [ |
| 175 | 'categories' => [], |
| 176 | 'employees' => [], |
| 177 | 'locations' => [], |
| 178 | 'customFields' => [], |
| 179 | 'tags' => [], |
| 180 | 'packages' => [], |
| 181 | 'settings' => [ |
| 182 | 'daysOff' => $daysOff |
| 183 | ], |
| 184 | ]; |
| 185 | |
| 186 | /** @var Event $event */ |
| 187 | foreach ($events->getItems() as $event) { |
| 188 | if ($event->getLocationId() && !$availableLocations->keyExists($event->getLocationId()->getValue())) { |
| 189 | $availableLocations->addItem( |
| 190 | $locations->getItem($event->getLocationId()->getValue()), |
| 191 | $event->getLocationId()->getValue() |
| 192 | ); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** @var Collection $eventsTags */ |
| 197 | $eventsTags = $eventTagsRepository->getAllDistinctByCriteria([]); |
| 198 | |
| 199 | $resultData['tags'] = $eventsTags->toArray(); |
| 200 | |
| 201 | if ($locations->length() && !$availableLocations->length()) { |
| 202 | $settingsDomainService->setStashEntities($resultData); |
| 203 | |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /** @var Location $location */ |
| 209 | foreach ($availableLocations->getItems() as $location) { |
| 210 | $resultData['locations'][] = [ |
| 211 | 'id' => $location->getId()->getValue(), |
| 212 | 'name' => $location->getName()->getValue(), |
| 213 | 'status' => $location->getStatus()->getValue(), |
| 214 | 'address' => $location->getAddress()->getValue(), |
| 215 | 'description' => $location->getDescription() ? $location->getDescription()->getValue() : null, |
| 216 | 'translations' => $location->getTranslations() ? $location->getTranslations()->getValue() : null |
| 217 | ]; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /** @var Collection $categories */ |
| 222 | $categories = $categoryRepository->getAllIndexedById(); |
| 223 | |
| 224 | $availableCategories = new Collection(); |
| 225 | |
| 226 | /** @var Service $service */ |
| 227 | foreach ($availableServices->getItems() as $service) { |
| 228 | if (!$availableCategories->keyExists($service->getCategoryId()->getValue())) { |
| 229 | /** @var Category $category */ |
| 230 | $category = $categories->getItem($service->getCategoryId()->getValue()); |
| 231 | |
| 232 | $category->setServiceList(new Collection()); |
| 233 | |
| 234 | $availableCategories->addItem( |
| 235 | $category, |
| 236 | $service->getCategoryId()->getValue() |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** @var Category $category */ |
| 241 | $category = $availableCategories->getItem($service->getCategoryId()->getValue()); |
| 242 | |
| 243 | $category->getServiceList()->addItem($service, $service->getId()->getValue()); |
| 244 | } |
| 245 | |
| 246 | $resultData['categories'] = $availableCategories->toArray(); |
| 247 | |
| 248 | |
| 249 | /** @var Provider $provider */ |
| 250 | foreach ($availableProviders->getItems() as $provider) { |
| 251 | $providerData = [ |
| 252 | 'id' => $provider->getId()->getValue(), |
| 253 | 'firstName' => $provider->getFirstName()->getValue(), |
| 254 | 'lastName' => $provider->getLastName()->getValue(), |
| 255 | 'email' => $provider->getEmail()->getValue(), |
| 256 | 'status' => $provider->getStatus()->getValue(), |
| 257 | 'show' => $provider->getShow()->getValue(), |
| 258 | 'pictureFullPath' => $provider->getPicture() ? $provider->getPicture()->getFullPath() : null, |
| 259 | 'pictureThumbPath' => $provider->getPicture() ? $provider->getPicture()->getThumbPath() : null, |
| 260 | 'locationId' => $provider->getLocationId() ? $provider->getLocationId()->getValue() : null, |
| 261 | 'serviceList' => [], |
| 262 | 'weekDayList' => $provider->getWeekDayList()->toArray(), |
| 263 | 'dayOffList' => $provider->getDayOffList()->toArray(), |
| 264 | 'specialDayList' => $provider->getSpecialDayList()->toArray(), |
| 265 | 'translations' => $provider->getTranslations() ? $provider->getTranslations()->getValue() : null, |
| 266 | 'description' => $provider->getDescription() ? $provider->getDescription()->getValue() : null, |
| 267 | 'badgeId' => $provider->getBadgeId() ? $provider->getBadgeId()->getValue() : null, |
| 268 | ]; |
| 269 | |
| 270 | /** @var Service $service */ |
| 271 | foreach ($provider->getServiceList()->getItems() as $service) { |
| 272 | if ($availableServices->keyExists($service->getId()->getValue())) { |
| 273 | $providerData['serviceList'][] = [ |
| 274 | 'id' => $service->getId()->getValue(), |
| 275 | 'price' => $service->getPrice()->getValue(), |
| 276 | 'duration' => $service->getDuration()->getValue(), |
| 277 | 'customPricing' => $service->getCustomPricing() |
| 278 | ? $service->getCustomPricing()->getValue() : null, |
| 279 | 'minCapacity' => $service->getMinCapacity()->getValue(), |
| 280 | 'maxCapacity' => $service->getMaxCapacity()->getValue(), |
| 281 | 'status' => $service->getStatus()->getValue(), |
| 282 | 'categoryId' => $service->getCategoryId()->getValue() |
| 283 | ]; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | $resultData['employees'][] = $providerData; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /** @var Collection $customFields */ |
| 292 | $customFields = $customFieldRepository->getAll(); |
| 293 | |
| 294 | /** @var CustomField $customField */ |
| 295 | foreach ($customFields->getItems() as $customField) { |
| 296 | $customFieldData = array_merge( |
| 297 | $customField->toArray(), |
| 298 | [ |
| 299 | 'services' => [], |
| 300 | 'events' => [] |
| 301 | ] |
| 302 | ); |
| 303 | |
| 304 | /** @var Service $service */ |
| 305 | foreach ($customField->getServices()->getItems() as $service) { |
| 306 | $customFieldData['services'][] = [ |
| 307 | 'id' => $service->getId()->getValue() |
| 308 | ]; |
| 309 | } |
| 310 | |
| 311 | /** @var Event $event */ |
| 312 | foreach ($customField->getEvents()->getItems() as $event) { |
| 313 | $customFieldData['events'][] = [ |
| 314 | 'id' => $event->getId()->getValue() |
| 315 | ]; |
| 316 | } |
| 317 | |
| 318 | $resultData['customFields'][] = $customFieldData; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /** @var PackageRepository $packageRepository */ |
| 323 | $packageRepository = $this->container->get('domain.bookable.package.repository'); |
| 324 | |
| 325 | /** @var Collection $packages */ |
| 326 | $packages = $packageRepository->getByCriteria([]); |
| 327 | |
| 328 | /** @var Package $package */ |
| 329 | foreach ($packages->getItems() as $package) { |
| 330 | $packageData = array_merge( |
| 331 | $package->toArray(), |
| 332 | ['bookable' => []] |
| 333 | ); |
| 334 | |
| 335 | /** @var PackageService $bookable */ |
| 336 | foreach ($package->getBookable()->getItems() as $bookable) { |
| 337 | $bookableData = array_merge( |
| 338 | $bookable->toArray(), |
| 339 | [ |
| 340 | 'service' => ['id' => $bookable->getService()->getId()->getValue()], |
| 341 | 'providers' => [], |
| 342 | 'locations' => [], |
| 343 | ] |
| 344 | ); |
| 345 | |
| 346 | /** @var Provider $provider */ |
| 347 | foreach ($bookable->getProviders()->getItems() as $provider) { |
| 348 | $bookableData['providers'][] = [ |
| 349 | 'id' => $provider->getId()->getValue() |
| 350 | ]; |
| 351 | } |
| 352 | |
| 353 | /** @var Location $location */ |
| 354 | foreach ($bookable->getLocations()->getItems() as $location) { |
| 355 | $bookableData['locations'][] = [ |
| 356 | 'id' => $location->getId()->getValue() |
| 357 | ]; |
| 358 | } |
| 359 | |
| 360 | $packageData['bookable'][] = $bookableData; |
| 361 | } |
| 362 | |
| 363 | $resultData['packages'][] = $packageData; |
| 364 | } |
| 365 | |
| 366 | /** @var Tax $tax */ |
| 367 | foreach ($taxes->getItems() as $tax) { |
| 368 | $taxData = [ |
| 369 | 'id' => $tax->getId()->getValue(), |
| 370 | 'name' => $tax->getName()->getValue(), |
| 371 | 'type' => $tax->getType()->getValue(), |
| 372 | 'amount' => $tax->getAmount()->getValue(), |
| 373 | 'status' => $tax->getStatus()->getValue(), |
| 374 | 'serviceList' => [], |
| 375 | 'extraList' => [], |
| 376 | 'packageList' => [], |
| 377 | 'eventList' => [], |
| 378 | 'allServices' => $tax->getAllServices() ? $tax->getAllServices()->getValue() : null, |
| 379 | 'allExtras' => $tax->getAllExtras() ? $tax->getAllExtras()->getValue() : null, |
| 380 | 'allPackages' => $tax->getAllPackages() ? $tax->getAllPackages()->getValue() : null, |
| 381 | 'allEvents' => $tax->getAllEvents() ? $tax->getAllEvents()->getValue() : null, |
| 382 | ]; |
| 383 | |
| 384 | /** @var Service $service */ |
| 385 | foreach ($tax->getServiceList()->getItems() as $service) { |
| 386 | $taxData['serviceList'][] = [ |
| 387 | 'id' => $service->getId()->getValue(), |
| 388 | ]; |
| 389 | } |
| 390 | |
| 391 | /** @var Extra $extra */ |
| 392 | foreach ($tax->getExtraList()->getItems() as $extra) { |
| 393 | $taxData['extraList'][] = [ |
| 394 | 'id' => $extra->getId()->getValue(), |
| 395 | ]; |
| 396 | } |
| 397 | |
| 398 | /** @var Package $package */ |
| 399 | foreach ($tax->getPackageList()->getItems() as $package) { |
| 400 | $taxData['packageList'][] = [ |
| 401 | 'id' => $package->getId()->getValue(), |
| 402 | ]; |
| 403 | } |
| 404 | |
| 405 | /** @var Event $event */ |
| 406 | foreach ($tax->getEventList()->getItems() as $event) { |
| 407 | $taxData['eventList'][] = [ |
| 408 | 'id' => $event->getId()->getValue(), |
| 409 | ]; |
| 410 | } |
| 411 | |
| 412 | $resultData['taxes'][] = $taxData; |
| 413 | } |
| 414 | |
| 415 | $settingsDomainService->setStashEntities($resultData); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * @return array |
| 420 | * @throws InvalidArgumentException |
| 421 | */ |
| 422 | public function getStash() |
| 423 | { |
| 424 | /** @var ProviderApplicationService $providerAS */ |
| 425 | $providerAS = $this->container->get('application.user.provider.service'); |
| 426 | |
| 427 | /** @var SettingsService $settingsDomainService */ |
| 428 | $settingsDomainService = $this->container->get('domain.settings.service'); |
| 429 | |
| 430 | $entitiesData = $settingsDomainService->getStashEntities(); |
| 431 | |
| 432 | if ($entitiesData) { |
| 433 | /** @var Collection $locations */ |
| 434 | $locations = new Collection(); |
| 435 | |
| 436 | foreach ($entitiesData['locations'] as $locationData) { |
| 437 | $locations->addItem(LocationFactory::create($locationData), $locationData['id']); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | /** @var Collection $services */ |
| 442 | $services = new Collection(); |
| 443 | |
| 444 | foreach ($entitiesData['categories'] as $categoryData) { |
| 445 | foreach ($categoryData['serviceList'] as $serviceData) { |
| 446 | $services->addItem(ServiceFactory::create($serviceData), $serviceData['id']); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | |
| 451 | /** @var Collection $providers */ |
| 452 | $providers = new Collection(); |
| 453 | |
| 454 | foreach ($entitiesData['employees'] as &$employeeData) { |
| 455 | $employeeData['type'] = AbstractUser::USER_ROLE_PROVIDER; |
| 456 | |
| 457 | $providers->addItem(ProviderFactory::create($employeeData), $employeeData['id']); |
| 458 | |
| 459 | unset( |
| 460 | $employeeData['email'], |
| 461 | $employeeData['weekDayList'], |
| 462 | $employeeData['specialDayList'], |
| 463 | $employeeData['dayOffList'] |
| 464 | ); |
| 465 | } |
| 466 | |
| 467 | $entitiesRelations = []; |
| 468 | |
| 469 | /** @var Provider $provider */ |
| 470 | foreach ($providers->getItems() as $providerId => $provider) { |
| 471 | if ($data = $providerAS->getProviderServiceLocations($provider, $locations, $services)) { |
| 472 | $entitiesRelations[$providerId] = $data; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | $currentDateTime = DateTimeService::getNowDateTimeObject(); |
| 477 | |
| 478 | foreach ($entitiesData['packages'] as &$packageData) { |
| 479 | $packageData['available'] = |
| 480 | !$packageData['endDate'] || |
| 481 | DateTimeService::getCustomDateTimeObject($packageData['endDate']) > $currentDateTime; |
| 482 | } |
| 483 | |
| 484 | $entitiesData['entitiesRelations'] = $entitiesRelations; |
| 485 | } |
| 486 | |
| 487 | return $entitiesData; |
| 488 | } |
| 489 | } |
| 490 |