EntityApplicationService.php
678 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 (!empty($data['providers']) ? $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 (!empty($data['customTickets']) ? $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 (!empty($data['customTickets']) ? $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 ( |
| 185 | ($data['entity'] === 'appointment' && !in_array((int)$id, $existingServicesIds)) || |
| 186 | ($data['entity'] === 'event' && !in_array((int)$id, $existingEventsIds)) |
| 187 | ) { |
| 188 | unset($data['entityIds'][$index]); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * @param array $data |
| 195 | * |
| 196 | * @return string |
| 197 | * |
| 198 | * @throws ContainerValueNotFoundException |
| 199 | * @throws QueryExecutionException |
| 200 | */ |
| 201 | public function getMissingEntityForNotification($data) |
| 202 | { |
| 203 | /** @var ServiceRepository $serviceRepository */ |
| 204 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 205 | /** @var EventRepository $eventRepository */ |
| 206 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 207 | |
| 208 | |
| 209 | $servicesIds = $data['entity'] === 'appointment' ? self::getUniqueIds($data['entityIds']) : []; |
| 210 | |
| 211 | $existingServicesIds = $servicesIds ? $serviceRepository->getIds(['id' => $servicesIds]) : []; |
| 212 | |
| 213 | |
| 214 | $eventsIds = $data['entity'] === 'event' ? self::getUniqueIds($data['entityIds']) : []; |
| 215 | |
| 216 | $existingEventsIds = $eventsIds ? $eventRepository->getIds(['id' => $eventsIds]) : []; |
| 217 | |
| 218 | |
| 219 | foreach ($data['entityIds'] as $id) { |
| 220 | if ($data['entity'] === 'appointment' && !in_array((int)$id, $existingServicesIds)) { |
| 221 | return Entities::SERVICE; |
| 222 | } |
| 223 | |
| 224 | if ($data['entity'] === 'event' && !in_array((int)$id, $existingEventsIds)) { |
| 225 | return Entities::EVENT; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return ''; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * @param array $data |
| 234 | * |
| 235 | * @return void |
| 236 | * |
| 237 | * @throws ContainerValueNotFoundException |
| 238 | * @throws QueryExecutionException |
| 239 | */ |
| 240 | public function removeMissingEntitiesForPackage(&$data) |
| 241 | { |
| 242 | /** @var ServiceRepository $serviceRepository */ |
| 243 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 244 | /** @var UserRepository $userRepository */ |
| 245 | $userRepository = $this->container->get('domain.users.repository'); |
| 246 | /** @var LocationRepository $locationRepository */ |
| 247 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 248 | |
| 249 | |
| 250 | $servicesIds = []; |
| 251 | |
| 252 | $providersIds = []; |
| 253 | |
| 254 | $locationsIds = []; |
| 255 | |
| 256 | foreach ($data['bookable'] as $item) { |
| 257 | $servicesIds[] = (int)$item['service']['id']; |
| 258 | |
| 259 | $providersIds = self::getUniqueIds($providersIds, array_column($item['providers'], 'id')); |
| 260 | |
| 261 | $locationsIds = self::getUniqueIds($locationsIds, array_column($item['locations'], 'id')); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | $existingServicesIds = $servicesIds ? $serviceRepository->getIds(['id' => $servicesIds]) : []; |
| 266 | |
| 267 | $existingProvidersIds = $providersIds ? $userRepository->getIds(['id' => $providersIds]) : []; |
| 268 | |
| 269 | $existingLocationsIds = $locationsIds ? $locationRepository->getIds(['id' => $locationsIds]) : []; |
| 270 | |
| 271 | |
| 272 | foreach ($data['bookable'] as $index => $item) { |
| 273 | if (!in_array((int)$item['service']['id'], $existingServicesIds)) { |
| 274 | unset($data['bookable'][$index]); |
| 275 | |
| 276 | continue; |
| 277 | } |
| 278 | |
| 279 | foreach ($item['providers'] as $providerIndex => $provider) { |
| 280 | if (!in_array((int)$provider['id'], $existingProvidersIds)) { |
| 281 | unset($data['bookable'][$index]['providers'][$providerIndex]); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | foreach ($item['locations'] as $locationIndex => $location) { |
| 286 | if (!in_array((int)$location['id'], $existingLocationsIds)) { |
| 287 | unset($data['bookable'][$index]['locations'][$locationIndex]); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @param array $data |
| 295 | * |
| 296 | * @return void |
| 297 | * |
| 298 | * @throws ContainerValueNotFoundException |
| 299 | * @throws QueryExecutionException |
| 300 | */ |
| 301 | public function removeMissingEntitiesForResource(&$data) |
| 302 | { |
| 303 | /** @var ServiceRepository $serviceRepository */ |
| 304 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 305 | /** @var UserRepository $userRepository */ |
| 306 | $userRepository = $this->container->get('domain.users.repository'); |
| 307 | /** @var LocationRepository $locationRepository */ |
| 308 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 309 | |
| 310 | |
| 311 | $servicesIds = []; |
| 312 | |
| 313 | $providersIds = []; |
| 314 | |
| 315 | $locationsIds = []; |
| 316 | |
| 317 | foreach ($data['entities'] as $item) { |
| 318 | switch ($item['entityType']) { |
| 319 | case 'service': |
| 320 | $servicesIds[] = $item['entityId']; |
| 321 | |
| 322 | break; |
| 323 | |
| 324 | case 'employee': |
| 325 | $providersIds[] = $item['entityId']; |
| 326 | |
| 327 | break; |
| 328 | |
| 329 | case 'location': |
| 330 | $locationsIds[] = $item['entityId']; |
| 331 | |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | $servicesIds = self::getUniqueIds($servicesIds); |
| 337 | |
| 338 | $existingServicesIds = $servicesIds ? $serviceRepository->getIds(['id' => $servicesIds]) : []; |
| 339 | |
| 340 | |
| 341 | $providersIds = self::getUniqueIds($providersIds); |
| 342 | |
| 343 | $existingProvidersIds = $providersIds ? $userRepository->getIds(['id' => $providersIds]) : []; |
| 344 | |
| 345 | |
| 346 | $locationsIds = self::getUniqueIds($locationsIds); |
| 347 | |
| 348 | $existingLocationsIds = $locationsIds ? $locationRepository->getIds(['id' => $locationsIds]) : []; |
| 349 | |
| 350 | |
| 351 | foreach ($data['entities'] as $index => $item) { |
| 352 | switch ($item['entityType']) { |
| 353 | case 'service': |
| 354 | if (!in_array((int)$item['entityId'], $existingServicesIds)) { |
| 355 | unset($data['entities'][$index]); |
| 356 | } |
| 357 | |
| 358 | break; |
| 359 | |
| 360 | case 'employee': |
| 361 | if (!in_array((int)$item['entityId'], $existingProvidersIds)) { |
| 362 | unset($data['entities'][$index]); |
| 363 | } |
| 364 | |
| 365 | break; |
| 366 | |
| 367 | case 'location': |
| 368 | if (!in_array((int)$item['entityId'], $existingLocationsIds)) { |
| 369 | unset($data['entities'][$index]); |
| 370 | } |
| 371 | |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * @param array $data |
| 379 | * |
| 380 | * @return void |
| 381 | * |
| 382 | * @throws QueryExecutionException |
| 383 | */ |
| 384 | public function removeMissingEntitiesForService(&$data) |
| 385 | { |
| 386 | /** @var UserRepository $userRepository */ |
| 387 | $userRepository = $this->container->get('domain.users.repository'); |
| 388 | |
| 389 | $providersIds = self::getUniqueIds($data['providers']); |
| 390 | |
| 391 | $existingProvidersIds = $data['providers'] ? $userRepository->getIds(['id' => $providersIds]) : []; |
| 392 | |
| 393 | foreach ($data['providers'] as $index => $id) { |
| 394 | if (!in_array((int)$id, $existingProvidersIds)) { |
| 395 | unset($data['providers'][$index]); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * @param array $data |
| 402 | * |
| 403 | * @return void |
| 404 | * |
| 405 | * @throws QueryExecutionException |
| 406 | */ |
| 407 | public function removeMissingEntitiesForProvider(&$data) |
| 408 | { |
| 409 | /** @var ServiceRepository $serviceRepository */ |
| 410 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 411 | /** @var LocationRepository $locationRepository */ |
| 412 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 413 | |
| 414 | if (isset($data['serviceList'])) { |
| 415 | $servicesIds = self::getUniqueIds(array_column($data['serviceList'], 'id')); |
| 416 | } |
| 417 | |
| 418 | $locationsIds = []; |
| 419 | |
| 420 | if (isset($data['locationId'])) { |
| 421 | $locationsIds[] = (int)$data['locationId']; |
| 422 | } |
| 423 | |
| 424 | foreach (['weekDayList', 'specialDayList'] as $type) { |
| 425 | if (isset($data[$type])) { |
| 426 | foreach ($data[$type] as $day) { |
| 427 | foreach ($day['periodList'] as $period) { |
| 428 | $locationsIds = self::getUniqueIds( |
| 429 | $locationsIds, |
| 430 | $period['locationId'] ? [$period['locationId']] : [], |
| 431 | array_column($period['periodLocationList'], 'locationId') |
| 432 | ); |
| 433 | |
| 434 | if (isset($servicesIds)) { |
| 435 | $servicesIds = self::getUniqueIds( |
| 436 | $servicesIds, |
| 437 | array_column($period['periodServiceList'], 'serviceId') |
| 438 | ); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | |
| 446 | $existingServicesIds = !empty($servicesIds) ? $serviceRepository->getIds(['id' => $servicesIds]) : []; |
| 447 | |
| 448 | $existingLocationsIds = !empty($locationsIds) ? $locationRepository->getIds(['id' => $locationsIds]) : []; |
| 449 | |
| 450 | |
| 451 | if (isset($data['locationId']) && !in_array((int)$data['locationId'], $existingLocationsIds)) { |
| 452 | $data['locationId'] = null; |
| 453 | } |
| 454 | |
| 455 | foreach (['weekDayList', 'specialDayList'] as $dayType) { |
| 456 | if (isset($data[$dayType])) { |
| 457 | foreach ($data[$dayType] as $dayIndex => $day) { |
| 458 | foreach ($day['periodList'] as $periodIndex => $period) { |
| 459 | if (!in_array((int)$period['locationId'], $existingLocationsIds)) { |
| 460 | $data[$dayType][$dayIndex]['periodList'][$periodIndex]['locationId'] = null; |
| 461 | } |
| 462 | |
| 463 | foreach ($period['periodLocationList'] as $index => $periodLocation) { |
| 464 | if (!in_array((int)$periodLocation['locationId'], $existingLocationsIds)) { |
| 465 | unset($data[$dayType][$dayIndex]['periodList'][$periodIndex]['periodLocationList'][$index]); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | foreach ($period['periodServiceList'] as $index => $periodService) { |
| 470 | if (!in_array((int)$periodService['serviceId'], $existingServicesIds)) { |
| 471 | unset($data[$dayType][$dayIndex]['periodList'][$periodIndex]['periodServiceList'][$index]); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if (isset($data['serviceList'])) { |
| 480 | foreach ($data['serviceList'] as $index => $item) { |
| 481 | if (!in_array((int)$item['id'], $existingServicesIds)) { |
| 482 | unset($data['serviceList'][$index]); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * @param array $data |
| 490 | * |
| 491 | * @return void |
| 492 | * |
| 493 | * @throws ContainerValueNotFoundException |
| 494 | * @throws QueryExecutionException |
| 495 | */ |
| 496 | public function removeMissingEntityForAppointment(&$data) |
| 497 | { |
| 498 | /** @var UserRepository $userRepository */ |
| 499 | $userRepository = $this->container->get('domain.users.repository'); |
| 500 | /** @var LocationRepository $locationRepository */ |
| 501 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 502 | /** @var ExtraRepository $extraRepository */ |
| 503 | $extraRepository = $this->container->get('domain.bookable.extra.repository'); |
| 504 | /** @var CouponRepository $couponRepository */ |
| 505 | $couponRepository = $this->container->get('domain.coupon.repository'); |
| 506 | |
| 507 | |
| 508 | $customersIds = self::getUniqueIds(array_column($data['bookings'], 'customerId')); |
| 509 | |
| 510 | if (!empty($customersIds)) { |
| 511 | $existingCustomersIds = $userRepository->getIds(['id' => $customersIds]); |
| 512 | |
| 513 | foreach ($data['bookings'] as $index => $item) { |
| 514 | if (!in_array((int)$item['customerId'], $existingCustomersIds)) { |
| 515 | unset($data['bookings'][$index]); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | |
| 521 | $extrasIds = []; |
| 522 | |
| 523 | foreach ($data['bookings'] as $item) { |
| 524 | if (empty($item['extras'])) { |
| 525 | continue; |
| 526 | } |
| 527 | $extrasIds = self::getUniqueIds( |
| 528 | $extrasIds, |
| 529 | array_column($item['extras'], 'extraId') |
| 530 | ); |
| 531 | } |
| 532 | |
| 533 | $existingExtrasIds = $extrasIds ? $extraRepository->getIds(['id' => $extrasIds]) : []; |
| 534 | |
| 535 | foreach ($data['bookings'] as $bookingIndex => $bookingItem) { |
| 536 | if (empty($bookingItem['extras'])) { |
| 537 | continue; |
| 538 | } |
| 539 | foreach ($bookingItem['extras'] as $extraIndex => $extraItem) { |
| 540 | if (!in_array((int)$extraItem['extraId'], $existingExtrasIds)) { |
| 541 | unset($data['bookings'][$bookingIndex]['extras'][$extraIndex]); |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | |
| 547 | $couponsIds = self::getUniqueIds(array_column(array_column($data['bookings'], 'coupon'), 'id')); |
| 548 | |
| 549 | $existingCouponsIds = $couponsIds ? $couponRepository->getIds(['id' => $couponsIds]) : []; |
| 550 | |
| 551 | foreach ($data['bookings'] as $index => $item) { |
| 552 | if (!empty($item['coupon']) && !empty($item['coupon']['id']) && !in_array((int)$item['coupon']['id'], $existingCouponsIds)) { |
| 553 | $data['bookings'][$index]['coupon'] = null; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | |
| 558 | $existingLocationsIds = !empty($data['locationId']) ? |
| 559 | $locationRepository->getIds(['id' => [(int)$data['locationId']]]) : []; |
| 560 | |
| 561 | if (!empty($data['locationId']) && !in_array((int)$data['locationId'], $existingLocationsIds)) { |
| 562 | $data['locationId'] = null; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * @param array $data |
| 568 | * |
| 569 | * @return string |
| 570 | * |
| 571 | * @throws ContainerValueNotFoundException |
| 572 | * @throws QueryExecutionException |
| 573 | */ |
| 574 | public function getMissingEntityForAppointment($data) |
| 575 | { |
| 576 | /** @var ServiceRepository $serviceRepository */ |
| 577 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 578 | /** @var UserRepository $userRepository */ |
| 579 | $userRepository = $this->container->get('domain.users.repository'); |
| 580 | /** @var LocationRepository $locationRepository */ |
| 581 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 582 | /** @var ExtraRepository $extraRepository */ |
| 583 | $extraRepository = $this->container->get('domain.bookable.extra.repository'); |
| 584 | /** @var CouponRepository $couponRepository */ |
| 585 | $couponRepository = $this->container->get('domain.coupon.repository'); |
| 586 | |
| 587 | |
| 588 | $customersIds = self::getUniqueIds(array_column($data['bookings'], 'customerId')); |
| 589 | |
| 590 | $existingCustomersIds = $userRepository->getIds(['id' => $customersIds]); |
| 591 | |
| 592 | foreach ($data['bookings'] as $item) { |
| 593 | if (!in_array((int)$item['customerId'], $existingCustomersIds)) { |
| 594 | return Entities::CUSTOMER; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | |
| 599 | $extrasIds = []; |
| 600 | |
| 601 | foreach ($data['bookings'] as $item) { |
| 602 | if (empty($item['extras'])) { |
| 603 | continue; |
| 604 | } |
| 605 | $extrasIds = self::getUniqueIds( |
| 606 | $extrasIds, |
| 607 | array_column($item['extras'], 'extraId') |
| 608 | ); |
| 609 | } |
| 610 | |
| 611 | $existingExtrasIds = $extrasIds ? $extraRepository->getIds(['id' => $extrasIds]) : []; |
| 612 | |
| 613 | foreach ($data['bookings'] as $bookingItem) { |
| 614 | if (empty($bookingItem['extras'])) { |
| 615 | continue; |
| 616 | } |
| 617 | foreach ($bookingItem['extras'] as $extraItem) { |
| 618 | if (!in_array((int)$extraItem['extraId'], $existingExtrasIds)) { |
| 619 | return Entities::EXTRA; |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | |
| 625 | $couponsIds = self::getUniqueIds(array_column(array_column($data['bookings'], 'coupon'), 'id')); |
| 626 | |
| 627 | $existingCouponsIds = $couponsIds ? $couponRepository->getIds(['id' => $couponsIds]) : []; |
| 628 | |
| 629 | foreach ($data['bookings'] as $item) { |
| 630 | if (!empty($item['coupon']) && !empty($item['coupon']['id']) && !in_array((int)$item['coupon']['id'], $existingCouponsIds)) { |
| 631 | return Entities::COUPON; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | |
| 636 | $existingServicesIds = $serviceRepository->getIds(['id' => [(int)$data['serviceId']]]); |
| 637 | |
| 638 | if (!in_array((int)$data['serviceId'], $existingServicesIds)) { |
| 639 | return Entities::SERVICE; |
| 640 | } |
| 641 | |
| 642 | |
| 643 | $existingProvidersIds = $userRepository->getIds(['id' => [(int)$data['providerId']]]); |
| 644 | |
| 645 | if (!in_array((int)$data['providerId'], $existingProvidersIds)) { |
| 646 | return Entities::PROVIDER; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | $existingLocationsIds = !empty($data['locationId']) ? |
| 651 | $locationRepository->getIds(['id' => [(int)$data['locationId']]]) : []; |
| 652 | |
| 653 | if (!empty($data['locationId']) && !in_array((int)$data['locationId'], $existingLocationsIds)) { |
| 654 | return Entities::LOCATION; |
| 655 | } |
| 656 | |
| 657 | return ''; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * @return CommandResult |
| 662 | */ |
| 663 | public function getMissingEntityResponse($type) |
| 664 | { |
| 665 | $result = new CommandResult(); |
| 666 | |
| 667 | $result->setResult(CommandResult::RESULT_ERROR); |
| 668 | $result->setMessage("Entity missing ($type). Please refresh page and try again."); |
| 669 | $result->setData( |
| 670 | [ |
| 671 | 'entityMissing' => true, |
| 672 | ] |
| 673 | ); |
| 674 | |
| 675 | return $result; |
| 676 | } |
| 677 | } |
| 678 |