EntityApplicationService.php
665 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Services\Entity; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandResult; |
| 6 | use AmeliaBooking\Domain\Entity\Entities; |
| 7 | use AmeliaBooking\Infrastructure\Common\Container; |
| 8 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 9 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ExtraRepository; |
| 10 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 11 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 12 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventTicketRepository; |
| 13 | use AmeliaBooking\Infrastructure\Repository\Coupon\CouponRepository; |
| 14 | use AmeliaBooking\Infrastructure\Repository\Location\LocationRepository; |
| 15 | use AmeliaBooking\Infrastructure\Repository\User\UserRepository; |
| 16 | use InvalidArgumentException; |
| 17 | use Slim\Exception\ContainerValueNotFoundException; |
| 18 | |
| 19 | /** |
| 20 | * Class EntityApplicationService |
| 21 | * |
| 22 | * @package AmeliaBooking\Application\Services\Entities |
| 23 | */ |
| 24 | class EntityApplicationService |
| 25 | { |
| 26 | private $container; |
| 27 | |
| 28 | /** |
| 29 | * EntityApplicationService constructor. |
| 30 | * |
| 31 | * @param Container $container |
| 32 | * |
| 33 | * @throws InvalidArgumentException |
| 34 | */ |
| 35 | public function __construct(Container $container) |
| 36 | { |
| 37 | $this->container = $container; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param array $idsArrays |
| 42 | * |
| 43 | * @return array |
| 44 | */ |
| 45 | private function getUniqueIds(...$idsArrays) |
| 46 | { |
| 47 | $uniqueIds = []; |
| 48 | |
| 49 | foreach ($idsArrays as $idsArray) { |
| 50 | $uniqueIds = array_merge( |
| 51 | $uniqueIds, |
| 52 | array_map('intval', $idsArray ? $idsArray : []) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | return array_unique($uniqueIds); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @param array $data |
| 61 | * |
| 62 | * @return void |
| 63 | * |
| 64 | * @throws QueryExecutionException |
| 65 | */ |
| 66 | public function removeMissingEntitiesForEvent(&$data) |
| 67 | { |
| 68 | /** @var UserRepository $userRepository */ |
| 69 | $userRepository = $this->container->get('domain.users.repository'); |
| 70 | /** @var LocationRepository $locationRepository */ |
| 71 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 72 | /** @var EventTicketRepository $eventTicketRepository */ |
| 73 | $eventTicketRepository = $this->container->get('domain.booking.event.ticket.repository'); |
| 74 | |
| 75 | $providersIds = self::getUniqueIds( |
| 76 | !empty($data['providers']) ? array_column($data['providers'], 'id') : [], |
| 77 | !empty($data['organizerId']) ? [$data['organizerId']] : [] |
| 78 | ); |
| 79 | |
| 80 | $existingProvidersIds = $providersIds ? $userRepository->getIds(['id' => $providersIds]) : []; |
| 81 | |
| 82 | if (!empty($data['organizerId']) && !in_array((int)$data['organizerId'], $existingProvidersIds)) { |
| 83 | $data['organizerId'] = null; |
| 84 | } |
| 85 | |
| 86 | foreach ($data['providers'] as $index => $item) { |
| 87 | if (!in_array((int)$item['id'], $existingProvidersIds)) { |
| 88 | unset($data['providers'][$index]); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | $locationsIds = !empty($data['locationId']) ? [(int)$data['locationId']] : []; |
| 94 | |
| 95 | $existingLocationsIds = $locationsIds ? $locationRepository->getIds(['id' => $locationsIds]) : []; |
| 96 | |
| 97 | if (!empty($data['locationId']) && !in_array((int)$data['locationId'], $existingLocationsIds)) { |
| 98 | $data['locationId'] = null; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | $ticketsIds = []; |
| 103 | |
| 104 | foreach ($data['customTickets'] as $item) { |
| 105 | if (!empty($item['id'])) { |
| 106 | $ticketsIds[] = $item['id']; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | $ticketsIds = self::getUniqueIds($ticketsIds); |
| 111 | |
| 112 | $existingTicketsIds = $ticketsIds ? $eventTicketRepository->getIds(['id' => $ticketsIds]) : []; |
| 113 | |
| 114 | foreach ($data['customTickets'] as $index => $item) { |
| 115 | if (!empty($item['id']) && !in_array((int)$item['id'], $existingTicketsIds)) { |
| 116 | unset($data['customTickets'][$index]); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param array $data |
| 123 | * |
| 124 | * @return void |
| 125 | * |
| 126 | * @throws QueryExecutionException |
| 127 | */ |
| 128 | public function removeMissingEntitiesForCustomField(&$data) |
| 129 | { |
| 130 | /** @var ServiceRepository $serviceRepository */ |
| 131 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 132 | /** @var EventRepository $eventRepository */ |
| 133 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 134 | |
| 135 | |
| 136 | $servicesIds = self::getUniqueIds(array_column($data['services'], 'id')); |
| 137 | |
| 138 | $existingServicesIds = $servicesIds ? $serviceRepository->getIds(['id' => $servicesIds]) : []; |
| 139 | |
| 140 | foreach ($data['services'] as $index => $item) { |
| 141 | if (!in_array((int)$item['id'], $existingServicesIds)) { |
| 142 | unset($data['services'][$index]); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | $eventsIds = self::getUniqueIds(array_column($data['events'], 'id')); |
| 148 | |
| 149 | $existingEventsIds = $eventsIds ? $eventRepository->getIds(['id' => $eventsIds]) : []; |
| 150 | |
| 151 | foreach ($data['events'] as $index => $item) { |
| 152 | if (!in_array((int)$item['id'], $existingEventsIds)) { |
| 153 | unset($data['events'][$index]); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @param array $data |
| 160 | * |
| 161 | * @return void |
| 162 | * |
| 163 | * @throws QueryExecutionException |
| 164 | */ |
| 165 | public function removeMissingEntitiesForNotification(&$data) |
| 166 | { |
| 167 | /** @var ServiceRepository $serviceRepository */ |
| 168 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 169 | /** @var EventRepository $eventRepository */ |
| 170 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 171 | |
| 172 | |
| 173 | $servicesIds = $data['entity'] === 'appointment' ? self::getUniqueIds($data['entityIds']) : []; |
| 174 | |
| 175 | $existingServicesIds = $servicesIds ? $serviceRepository->getIds(['id' => $servicesIds]) : []; |
| 176 | |
| 177 | |
| 178 | $eventsIds = $data['entity'] === 'event' ? self::getUniqueIds($data['entityIds']) : []; |
| 179 | |
| 180 | $existingEventsIds = $eventsIds ? $eventRepository->getIds(['id' => $eventsIds]) : []; |
| 181 | |
| 182 | |
| 183 | foreach ($data['entityIds'] as $index => $id) { |
| 184 | if (($data['entity'] === 'appointment' && !in_array((int)$id, $existingServicesIds)) || |
| 185 | ($data['entity'] === 'event' && !in_array((int)$id, $existingEventsIds)) |
| 186 | ) { |
| 187 | unset($data['entityIds'][$index]); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @param array $data |
| 194 | * |
| 195 | * @return string |
| 196 | * |
| 197 | * @throws ContainerValueNotFoundException |
| 198 | * @throws QueryExecutionException |
| 199 | */ |
| 200 | public function getMissingEntityForNotification($data) |
| 201 | { |
| 202 | /** @var ServiceRepository $serviceRepository */ |
| 203 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 204 | /** @var EventRepository $eventRepository */ |
| 205 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 206 | |
| 207 | |
| 208 | $servicesIds = $data['entity'] === 'appointment' ? self::getUniqueIds($data['entityIds']) : []; |
| 209 | |
| 210 | $existingServicesIds = $servicesIds ? $serviceRepository->getIds(['id' => $servicesIds]) : []; |
| 211 | |
| 212 | |
| 213 | $eventsIds = $data['entity'] === 'event' ? self::getUniqueIds($data['entityIds']) : []; |
| 214 | |
| 215 | $existingEventsIds = $eventsIds ? $eventRepository->getIds(['id' => $eventsIds]) : []; |
| 216 | |
| 217 | |
| 218 | foreach ($data['entityIds'] as $id) { |
| 219 | if ($data['entity'] === 'appointment' && !in_array((int)$id, $existingServicesIds)) { |
| 220 | return Entities::SERVICE; |
| 221 | } |
| 222 | |
| 223 | if ($data['entity'] === 'event' && !in_array((int)$id, $existingEventsIds)) { |
| 224 | return Entities::EVENT; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return ''; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * @param array $data |
| 233 | * |
| 234 | * @return void |
| 235 | * |
| 236 | * @throws ContainerValueNotFoundException |
| 237 | * @throws QueryExecutionException |
| 238 | */ |
| 239 | public function removeMissingEntitiesForPackage(&$data) |
| 240 | { |
| 241 | /** @var ServiceRepository $serviceRepository */ |
| 242 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 243 | /** @var UserRepository $userRepository */ |
| 244 | $userRepository = $this->container->get('domain.users.repository'); |
| 245 | /** @var LocationRepository $locationRepository */ |
| 246 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 247 | |
| 248 | |
| 249 | $servicesIds = []; |
| 250 | |
| 251 | $providersIds = []; |
| 252 | |
| 253 | $locationsIds = []; |
| 254 | |
| 255 | foreach ($data['bookable'] as $item) { |
| 256 | $servicesIds[] = (int)$item['service']['id']; |
| 257 | |
| 258 | $providersIds = self::getUniqueIds($providersIds, array_column($item['providers'], 'id')); |
| 259 | |
| 260 | $locationsIds = self::getUniqueIds($locationsIds, array_column($item['locations'], 'id')); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | $existingServicesIds = $servicesIds ? $serviceRepository->getIds(['id' => $servicesIds]) : []; |
| 265 | |
| 266 | $existingProvidersIds = $providersIds ? $userRepository->getIds(['id' => $providersIds]) : []; |
| 267 | |
| 268 | $existingLocationsIds = $locationsIds ? $locationRepository->getIds(['id' => $locationsIds]) : []; |
| 269 | |
| 270 | |
| 271 | foreach ($data['bookable'] as $index => $item) { |
| 272 | if (!in_array((int)$item['service']['id'], $existingServicesIds)) { |
| 273 | unset($data['bookable'][$index]); |
| 274 | |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | foreach ($item['providers'] as $providerIndex => $provider) { |
| 279 | if (!in_array((int)$provider['id'], $existingProvidersIds)) { |
| 280 | unset($data['bookable'][$index]['providers'][$providerIndex]); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | foreach ($item['locations'] as $locationIndex => $location) { |
| 285 | if (!in_array((int)$location['id'], $existingLocationsIds)) { |
| 286 | unset($data['bookable'][$index]['locations'][$locationIndex]); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * @param array $data |
| 294 | * |
| 295 | * @return void |
| 296 | * |
| 297 | * @throws ContainerValueNotFoundException |
| 298 | * @throws QueryExecutionException |
| 299 | */ |
| 300 | public function removeMissingEntitiesForResource(&$data) |
| 301 | { |
| 302 | /** @var ServiceRepository $serviceRepository */ |
| 303 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 304 | /** @var UserRepository $userRepository */ |
| 305 | $userRepository = $this->container->get('domain.users.repository'); |
| 306 | /** @var LocationRepository $locationRepository */ |
| 307 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 308 | |
| 309 | |
| 310 | $servicesIds = []; |
| 311 | |
| 312 | $providersIds = []; |
| 313 | |
| 314 | $locationsIds = []; |
| 315 | |
| 316 | foreach ($data['entities'] as $item) { |
| 317 | switch ($item['entityType']) { |
| 318 | case 'service': |
| 319 | $servicesIds[] = $item['entityId']; |
| 320 | |
| 321 | break; |
| 322 | |
| 323 | case 'employee': |
| 324 | $providersIds[] = $item['entityId']; |
| 325 | |
| 326 | break; |
| 327 | |
| 328 | case 'location': |
| 329 | $locationsIds[] = $item['entityId']; |
| 330 | |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | $servicesIds = self::getUniqueIds($servicesIds); |
| 336 | |
| 337 | $existingServicesIds = $servicesIds ? $serviceRepository->getIds(['id' => $servicesIds]) : []; |
| 338 | |
| 339 | |
| 340 | $providersIds = self::getUniqueIds($providersIds); |
| 341 | |
| 342 | $existingProvidersIds = $providersIds ? $userRepository->getIds(['id' => $providersIds]) : []; |
| 343 | |
| 344 | |
| 345 | $locationsIds = self::getUniqueIds($locationsIds); |
| 346 | |
| 347 | $existingLocationsIds = $locationsIds ? $locationRepository->getIds(['id' => $locationsIds]) : []; |
| 348 | |
| 349 | |
| 350 | foreach ($data['entities'] as $index => $item) { |
| 351 | switch ($item['entityType']) { |
| 352 | case 'service': |
| 353 | if (!in_array((int)$item['entityId'], $existingServicesIds)) { |
| 354 | unset($data['entities'][$index]); |
| 355 | } |
| 356 | |
| 357 | break; |
| 358 | |
| 359 | case 'employee': |
| 360 | if (!in_array((int)$item['entityId'], $existingProvidersIds)) { |
| 361 | unset($data['entities'][$index]); |
| 362 | } |
| 363 | |
| 364 | break; |
| 365 | |
| 366 | case 'location': |
| 367 | if (!in_array((int)$item['entityId'], $existingLocationsIds)) { |
| 368 | unset($data['entities'][$index]); |
| 369 | } |
| 370 | |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * @param array $data |
| 378 | * |
| 379 | * @return void |
| 380 | * |
| 381 | * @throws QueryExecutionException |
| 382 | */ |
| 383 | public function removeMissingEntitiesForService(&$data) |
| 384 | { |
| 385 | /** @var UserRepository $userRepository */ |
| 386 | $userRepository = $this->container->get('domain.users.repository'); |
| 387 | |
| 388 | $providersIds = self::getUniqueIds($data['providers']); |
| 389 | |
| 390 | $existingProvidersIds = $data['providers'] ? $userRepository->getIds(['id' => $providersIds]) : []; |
| 391 | |
| 392 | foreach ($data['providers'] as $index => $id) { |
| 393 | if (!in_array((int)$id, $existingProvidersIds)) { |
| 394 | unset($data['providers'][$index]); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * @param array $data |
| 401 | * |
| 402 | * @return void |
| 403 | * |
| 404 | * @throws QueryExecutionException |
| 405 | */ |
| 406 | public function removeMissingEntitiesForProvider(&$data) |
| 407 | { |
| 408 | /** @var ServiceRepository $serviceRepository */ |
| 409 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 410 | /** @var LocationRepository $locationRepository */ |
| 411 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 412 | |
| 413 | if (isset($data['serviceList'])) { |
| 414 | $servicesIds = self::getUniqueIds(array_column($data['serviceList'], 'id')); |
| 415 | } |
| 416 | |
| 417 | $locationsIds = []; |
| 418 | |
| 419 | if (isset($data['locationId'])) { |
| 420 | $locationsIds[] = (int)$data['locationId']; |
| 421 | } |
| 422 | |
| 423 | foreach (['weekDayList', 'specialDayList'] as $type) { |
| 424 | if (isset($data[$type])) { |
| 425 | foreach ($data[$type] as $day) { |
| 426 | foreach ($day['periodList'] as $period) { |
| 427 | $locationsIds = self::getUniqueIds( |
| 428 | $locationsIds, |
| 429 | $period['locationId'] ? [$period['locationId']] : [], |
| 430 | array_column($period['periodLocationList'], 'locationId') |
| 431 | ); |
| 432 | |
| 433 | if (isset($servicesIds)) { |
| 434 | $servicesIds = self::getUniqueIds( |
| 435 | $servicesIds, |
| 436 | array_column($period['periodServiceList'], 'serviceId') |
| 437 | ); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | |
| 445 | $existingServicesIds = !empty($servicesIds) ? $serviceRepository->getIds(['id' => $servicesIds]) : []; |
| 446 | |
| 447 | $existingLocationsIds = !empty($locationsIds) ? $locationRepository->getIds(['id' => $locationsIds]) : []; |
| 448 | |
| 449 | |
| 450 | if (isset($data['locationId']) && !in_array((int)$data['locationId'], $existingLocationsIds)) { |
| 451 | $data['locationId'] = null; |
| 452 | } |
| 453 | |
| 454 | foreach (['weekDayList', 'specialDayList'] as $dayType) { |
| 455 | if (isset($data[$dayType])) { |
| 456 | foreach ($data[$dayType] as $dayIndex => $day) { |
| 457 | foreach ($day['periodList'] as $periodIndex => $period) { |
| 458 | if (!in_array((int)$period['locationId'], $existingLocationsIds)) { |
| 459 | $data[$dayType][$dayIndex]['periodList'][$periodIndex]['locationId'] = null; |
| 460 | } |
| 461 | |
| 462 | foreach ($period['periodLocationList'] as $index => $periodLocation) { |
| 463 | if (!in_array((int)$periodLocation['locationId'], $existingLocationsIds)) { |
| 464 | unset($data[$dayType][$dayIndex]['periodList'][$periodIndex]['periodLocationList'][$index]); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | foreach ($period['periodServiceList'] as $index => $periodService) { |
| 469 | if (!in_array((int)$periodService['serviceId'], $existingServicesIds)) { |
| 470 | unset($data[$dayType][$dayIndex]['periodList'][$periodIndex]['periodServiceList'][$index]); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if (isset($data['serviceList'])) { |
| 479 | foreach ($data['serviceList'] as $index => $item) { |
| 480 | if (!in_array((int)$item['id'], $existingServicesIds)) { |
| 481 | unset($data['serviceList'][$index]); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * @param array $data |
| 489 | * |
| 490 | * @return void |
| 491 | * |
| 492 | * @throws ContainerValueNotFoundException |
| 493 | * @throws QueryExecutionException |
| 494 | */ |
| 495 | public function removeMissingEntityForAppointment(&$data) |
| 496 | { |
| 497 | /** @var UserRepository $userRepository */ |
| 498 | $userRepository = $this->container->get('domain.users.repository'); |
| 499 | /** @var LocationRepository $locationRepository */ |
| 500 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 501 | /** @var ExtraRepository $extraRepository */ |
| 502 | $extraRepository = $this->container->get('domain.bookable.extra.repository'); |
| 503 | /** @var CouponRepository $couponRepository */ |
| 504 | $couponRepository = $this->container->get('domain.coupon.repository'); |
| 505 | |
| 506 | |
| 507 | $customersIds = self::getUniqueIds(array_column($data['bookings'], 'customerId')); |
| 508 | |
| 509 | if (!empty($customersIds)) { |
| 510 | $existingCustomersIds = $userRepository->getIds(['id' => $customersIds]); |
| 511 | |
| 512 | foreach ($data['bookings'] as $index => $item) { |
| 513 | if (!in_array((int)$item['customerId'], $existingCustomersIds)) { |
| 514 | unset($data['bookings'][$index]); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | |
| 520 | $extrasIds = []; |
| 521 | |
| 522 | foreach ($data['bookings'] as $item) { |
| 523 | $extrasIds = self::getUniqueIds( |
| 524 | $extrasIds, |
| 525 | array_column($item['extras'], 'extraId') |
| 526 | ); |
| 527 | } |
| 528 | |
| 529 | $existingExtrasIds = $extrasIds ? $extraRepository->getIds(['id' => $extrasIds]) : []; |
| 530 | |
| 531 | foreach ($data['bookings'] as $bookingIndex => $bookingItem) { |
| 532 | foreach ($bookingItem['extras'] as $extraIndex => $extraItem) { |
| 533 | if (!in_array((int)$extraItem['extraId'], $existingExtrasIds)) { |
| 534 | unset($data['bookings'][$bookingIndex]['extras'][$extraIndex]); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | |
| 540 | $couponsIds = self::getUniqueIds(array_column(array_column($data['bookings'], 'coupon'), 'id')); |
| 541 | |
| 542 | $existingCouponsIds = $couponsIds ? $couponRepository->getIds(['id' => $couponsIds]) : []; |
| 543 | |
| 544 | foreach ($data['bookings'] as $index => $item) { |
| 545 | if (!empty($item['coupon']) && !in_array((int)$item['coupon']['id'], $existingCouponsIds)) { |
| 546 | $data['bookings'][$index]['coupon'] = null; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | |
| 551 | $existingLocationsIds = !empty($data['locationId']) ? |
| 552 | $locationRepository->getIds(['id' => [(int)$data['locationId']]]) : []; |
| 553 | |
| 554 | if (!empty($data['locationId']) && !in_array((int)$data['locationId'], $existingLocationsIds)) { |
| 555 | $data['locationId'] = null; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * @param array $data |
| 561 | * |
| 562 | * @return string |
| 563 | * |
| 564 | * @throws ContainerValueNotFoundException |
| 565 | * @throws QueryExecutionException |
| 566 | */ |
| 567 | public function getMissingEntityForAppointment($data) |
| 568 | { |
| 569 | /** @var ServiceRepository $serviceRepository */ |
| 570 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 571 | /** @var UserRepository $userRepository */ |
| 572 | $userRepository = $this->container->get('domain.users.repository'); |
| 573 | /** @var LocationRepository $locationRepository */ |
| 574 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 575 | /** @var ExtraRepository $extraRepository */ |
| 576 | $extraRepository = $this->container->get('domain.bookable.extra.repository'); |
| 577 | /** @var CouponRepository $couponRepository */ |
| 578 | $couponRepository = $this->container->get('domain.coupon.repository'); |
| 579 | |
| 580 | |
| 581 | $customersIds = self::getUniqueIds(array_column($data['bookings'], 'customerId')); |
| 582 | |
| 583 | $existingCustomersIds = $userRepository->getIds(['id' => $customersIds]); |
| 584 | |
| 585 | foreach ($data['bookings'] as $item) { |
| 586 | if (!in_array((int)$item['customerId'], $existingCustomersIds)) { |
| 587 | return Entities::CUSTOMER; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | |
| 592 | $extrasIds = []; |
| 593 | |
| 594 | foreach ($data['bookings'] as $item) { |
| 595 | $extrasIds = self::getUniqueIds( |
| 596 | $extrasIds, |
| 597 | array_column($item['extras'], 'extraId') |
| 598 | ); |
| 599 | } |
| 600 | |
| 601 | $existingExtrasIds = $extrasIds ? $extraRepository->getIds(['id' => $extrasIds]) : []; |
| 602 | |
| 603 | foreach ($data['bookings'] as $bookingItem) { |
| 604 | foreach ($bookingItem['extras'] as $extraItem) { |
| 605 | if (!in_array((int)$extraItem['extraId'], $existingExtrasIds)) { |
| 606 | return Entities::EXTRA; |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | |
| 612 | $couponsIds = self::getUniqueIds(array_column(array_column($data['bookings'], 'coupon'), 'id')); |
| 613 | |
| 614 | $existingCouponsIds = $couponsIds ? $couponRepository->getIds(['id' => $couponsIds]) : []; |
| 615 | |
| 616 | foreach ($data['bookings'] as $item) { |
| 617 | if (!empty($item['coupon']) && !in_array((int)$item['coupon']['id'], $existingCouponsIds)) { |
| 618 | return Entities::COUPON; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | |
| 623 | $existingServicesIds = $serviceRepository->getIds(['id' => [(int)$data['serviceId']]]); |
| 624 | |
| 625 | if (!in_array((int)$data['serviceId'], $existingServicesIds)) { |
| 626 | return Entities::SERVICE; |
| 627 | } |
| 628 | |
| 629 | |
| 630 | $existingProvidersIds = $userRepository->getIds(['id' => [(int)$data['providerId']]]); |
| 631 | |
| 632 | if (!in_array((int)$data['providerId'], $existingProvidersIds)) { |
| 633 | return Entities::PROVIDER; |
| 634 | } |
| 635 | |
| 636 | |
| 637 | $existingLocationsIds = !empty($data['locationId']) ? |
| 638 | $locationRepository->getIds(['id' => [(int)$data['locationId']]]) : []; |
| 639 | |
| 640 | if (!empty($data['locationId']) && !in_array((int)$data['locationId'], $existingLocationsIds)) { |
| 641 | return Entities::LOCATION; |
| 642 | } |
| 643 | |
| 644 | return ''; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * @return CommandResult |
| 649 | */ |
| 650 | public function getMissingEntityResponse($type) |
| 651 | { |
| 652 | $result = new CommandResult(); |
| 653 | |
| 654 | $result->setResult(CommandResult::RESULT_ERROR); |
| 655 | $result->setMessage("Entity missing ($type). Please refresh page and try again."); |
| 656 | $result->setData( |
| 657 | [ |
| 658 | 'entityMissing' => true, |
| 659 | ] |
| 660 | ); |
| 661 | |
| 662 | return $result; |
| 663 | } |
| 664 | } |
| 665 |