GetEntitiesCommandHandler.php
732 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Entities; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Application\Services\Bookable\BookableApplicationService; |
| 9 | use AmeliaBooking\Application\Services\Bookable\AbstractPackageApplicationService; |
| 10 | use AmeliaBooking\Application\Services\Booking\EventApplicationService; |
| 11 | use AmeliaBooking\Application\Services\Coupon\AbstractCouponApplicationService; |
| 12 | use AmeliaBooking\Application\Services\CustomField\AbstractCustomFieldApplicationService; |
| 13 | use AmeliaBooking\Application\Services\Helper\HelperService; |
| 14 | use AmeliaBooking\Application\Services\Location\AbstractLocationApplicationService; |
| 15 | use AmeliaBooking\Application\Services\Resource\AbstractResourceApplicationService; |
| 16 | use AmeliaBooking\Application\Services\Tax\TaxApplicationService; |
| 17 | use AmeliaBooking\Application\Services\User\ProviderApplicationService; |
| 18 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 19 | use AmeliaBooking\Domain\Collection\Collection; |
| 20 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 21 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 22 | use AmeliaBooking\Domain\Entity\Bookable\Service\Package; |
| 23 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 24 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 25 | use AmeliaBooking\Domain\Entity\Coupon\Coupon; |
| 26 | use AmeliaBooking\Domain\Entity\CustomField\CustomField; |
| 27 | use AmeliaBooking\Domain\Entity\Entities; |
| 28 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 29 | use AmeliaBooking\Domain\Entity\User\Customer; |
| 30 | use AmeliaBooking\Domain\Entity\User\Provider; |
| 31 | use AmeliaBooking\Domain\Services\Booking\EventDomainService; |
| 32 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 33 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 34 | use AmeliaBooking\Domain\Services\User\ProviderService; |
| 35 | use AmeliaBooking\Domain\ValueObjects\String\Status; |
| 36 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 37 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 38 | use AmeliaBooking\Infrastructure\Licence\Licence as Licence; |
| 39 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\CategoryRepository; |
| 40 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\PackageRepository; |
| 41 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 42 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 43 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 44 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 45 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventTagsRepository; |
| 46 | use AmeliaBooking\Infrastructure\Repository\Coupon\CouponRepository; |
| 47 | use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository; |
| 48 | use AmeliaBooking\Infrastructure\Repository\User\UserRepository; |
| 49 | use AmeliaBooking\Infrastructure\Services\LessonSpace\AbstractLessonSpaceService; |
| 50 | use AmeliaBooking\Infrastructure\Services\Mailchimp\AbstractMailchimpService; |
| 51 | use AmeliaBooking\Infrastructure\Services\Payment\SquareService; |
| 52 | use AmeliaBooking\Infrastructure\WP\Integrations\IvyForms\IvyFormsService; |
| 53 | use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings; |
| 54 | |
| 55 | class GetEntitiesCommandHandler extends CommandHandler |
| 56 | { |
| 57 | /** |
| 58 | * @throws AccessDeniedException |
| 59 | * @throws InvalidArgumentException |
| 60 | * @throws NotFoundException |
| 61 | * @throws QueryExecutionException |
| 62 | */ |
| 63 | public function handle(GetEntitiesCommand $command): CommandResult |
| 64 | { |
| 65 | /** @var UserApplicationService $userAS */ |
| 66 | $userAS = $this->container->get('application.user.service'); |
| 67 | |
| 68 | /** @var EventDomainService $eventDS */ |
| 69 | $eventDS = $this->container->get('domain.booking.event.service'); |
| 70 | |
| 71 | /** @var ProviderApplicationService $providerAS */ |
| 72 | $providerAS = $this->container->get('application.user.provider.service'); |
| 73 | |
| 74 | /** @var AbstractCustomFieldApplicationService $customFieldAS */ |
| 75 | $customFieldAS = $this->container->get('application.customField.service'); |
| 76 | |
| 77 | /** @var AbstractLocationApplicationService $locationAS */ |
| 78 | $locationAS = $this->container->get('application.location.service'); |
| 79 | |
| 80 | /** @var AbstractCouponApplicationService $couponAS */ |
| 81 | $couponAS = $this->container->get('application.coupon.service'); |
| 82 | |
| 83 | /** @var ProviderService $providerService */ |
| 84 | $providerService = $this->container->get('domain.user.provider.service'); |
| 85 | |
| 86 | try { |
| 87 | /** @var AbstractUser $currentUser */ |
| 88 | $currentUser = $command->getUserApplicationService()->authorization( |
| 89 | $command->getPage() === 'cabinet' ? $command->getToken() : null, |
| 90 | $command->getCabinetType() |
| 91 | ); |
| 92 | } catch (AuthorizationException $e) { |
| 93 | $currentUser = null; |
| 94 | } |
| 95 | |
| 96 | $params = $command->getField('params'); |
| 97 | |
| 98 | $params['types'] = !empty($params['types']) ? $params['types'] : []; |
| 99 | |
| 100 | $result = new CommandResult(); |
| 101 | |
| 102 | $this->checkMandatoryFields($command); |
| 103 | |
| 104 | $allServices = new Collection(); |
| 105 | $services = new Collection(); |
| 106 | $locations = new Collection(); |
| 107 | $categories = new Collection(); |
| 108 | $events = new Collection(); |
| 109 | |
| 110 | $resultData = []; |
| 111 | |
| 112 | /** @var SettingsService $settingsDS */ |
| 113 | $settingsDS = $this->container->get('domain.settings.service'); |
| 114 | |
| 115 | $rolesSettings = $settingsDS->getCategorySettings('roles'); |
| 116 | |
| 117 | /** Events */ |
| 118 | if (in_array(Entities::EVENTS, $params['types'], true)) { |
| 119 | /** @var EventApplicationService $eventAS */ |
| 120 | $eventAS = $this->container->get('application.booking.event.service'); |
| 121 | |
| 122 | $events = $eventAS->getEventsByCriteria( |
| 123 | [ |
| 124 | 'dates' => [DateTimeService::getNowDateTime()], |
| 125 | 'page' => 1, |
| 126 | ], |
| 127 | [ |
| 128 | 'fetchEventsPeriods' => true, |
| 129 | ], |
| 130 | $settingsDS->getSetting('general', 'eventsFilterLimit') ?: 1000 |
| 131 | ); |
| 132 | |
| 133 | $resultData['events'] = $eventDS->getShortcodeForEventList($this->container, $events->toArray()); |
| 134 | } |
| 135 | |
| 136 | /** Event Tags */ |
| 137 | if (in_array(Entities::TAGS, $params['types'], true)) { |
| 138 | /** @var EventTagsRepository $eventTagsRepository */ |
| 139 | $eventTagsRepository = $this->container->get('domain.booking.event.tag.repository'); |
| 140 | |
| 141 | $eventsTags = $eventTagsRepository->getAllDistinctByCriteria( |
| 142 | $events->length() ? ['eventIds' => array_column($events->toArray(), 'id')] : [] |
| 143 | ); |
| 144 | |
| 145 | $resultData['tags'] = $eventsTags->toArray(); |
| 146 | } |
| 147 | |
| 148 | if ( |
| 149 | in_array(Entities::LOCATIONS, $params['types'], true) || |
| 150 | in_array(Entities::EMPLOYEES, $params['types'], true) |
| 151 | ) { |
| 152 | $locations = $locationAS->getAllOrderedByName(); |
| 153 | } |
| 154 | |
| 155 | /** Locations */ |
| 156 | if (in_array(Entities::LOCATIONS, $params['types'], true)) { |
| 157 | $resultData['locations'] = $locations->toArray(); |
| 158 | } |
| 159 | |
| 160 | if ( |
| 161 | in_array(Entities::CATEGORIES, $params['types'], true) || |
| 162 | in_array(Entities::EMPLOYEES, $params['types'], true) || |
| 163 | in_array(Entities::COUPONS, $params['types'], true) |
| 164 | ) { |
| 165 | /** @var ServiceRepository $serviceRepository */ |
| 166 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 167 | /** @var CategoryRepository $categoryRepository */ |
| 168 | $categoryRepository = $this->container->get('domain.bookable.category.repository'); |
| 169 | /** @var BookableApplicationService $bookableAS */ |
| 170 | $bookableAS = $this->container->get('application.bookable.service'); |
| 171 | |
| 172 | $allServices = $serviceRepository->getAllArrayIndexedById(); |
| 173 | |
| 174 | /** @var Service $service */ |
| 175 | foreach ($allServices->getItems() as $service) { |
| 176 | if ($settingsDS->isFeatureEnabled('customPricing') === false) { |
| 177 | $service->setCustomPricing(null); |
| 178 | } |
| 179 | |
| 180 | if ( |
| 181 | $service->getStatus()->getValue() === Status::VISIBLE || |
| 182 | Licence::isPremium() || |
| 183 | ($currentUser && $currentUser->getType() === AbstractUser::USER_ROLE_ADMIN) |
| 184 | ) { |
| 185 | $services->addItem($service, $service->getId()->getValue()); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | $categories = $categoryRepository->getAllIndexedById(); |
| 190 | |
| 191 | $bookableAS->addServicesToCategories($categories, $services); |
| 192 | } |
| 193 | |
| 194 | /** Categories */ |
| 195 | if (in_array(Entities::CATEGORIES, $params['types'], true)) { |
| 196 | $resultData['categories'] = $categories->toArray(); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | $resultData['customers'] = []; |
| 201 | |
| 202 | /** Customers */ |
| 203 | if (in_array(Entities::CUSTOMERS, $params['types'], true)) { |
| 204 | /** @var UserRepository $userRepo */ |
| 205 | $userRepo = $this->getContainer()->get('domain.users.repository'); |
| 206 | |
| 207 | $resultData['customers'] = []; |
| 208 | |
| 209 | if ($currentUser) { |
| 210 | switch ($currentUser->getType()) { |
| 211 | case (AbstractUser::USER_ROLE_CUSTOMER): |
| 212 | if ($currentUser->getId()) { |
| 213 | /** @var Customer $customer */ |
| 214 | $customer = $userRepo->getById($currentUser->getId()->getValue()); |
| 215 | |
| 216 | $resultData['customers'] = [$customer->toArray()]; |
| 217 | } |
| 218 | |
| 219 | break; |
| 220 | |
| 221 | case (AbstractUser::USER_ROLE_PROVIDER): |
| 222 | /** @var Collection $customers */ |
| 223 | $customers = empty($rolesSettings['allowReadAllCustomers']) |
| 224 | ? $userRepo->getProviderAllowedCustomers( |
| 225 | $currentUser->getId()->getValue() |
| 226 | ) |
| 227 | : $userRepo->getAllWithAllowedBooking(); |
| 228 | |
| 229 | $resultData['customers'] = $customers->toArray(); |
| 230 | |
| 231 | break; |
| 232 | |
| 233 | default: |
| 234 | /** @var Collection $customers */ |
| 235 | $customers = $userRepo->getAllWithAllowedBooking(); |
| 236 | |
| 237 | $resultData['customers'] = $customers->toArray(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | $noShowTagEnabled = $settingsDS->isFeatureEnabled('noShowTag'); |
| 242 | |
| 243 | if ($noShowTagEnabled && $resultData['customers']) { |
| 244 | /** @var CustomerBookingRepository $bookingRepository */ |
| 245 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 246 | |
| 247 | $usersIds = array_map( |
| 248 | function ($user) { |
| 249 | return $user['id']; |
| 250 | }, |
| 251 | $resultData['customers'] |
| 252 | ); |
| 253 | |
| 254 | $customersNoShowCount = $bookingRepository->countByNoShowStatus($usersIds); |
| 255 | |
| 256 | $customersNoShowCount = $customersNoShowCount ? array_values($customersNoShowCount) : []; |
| 257 | |
| 258 | foreach ($resultData['customers'] as $key => $customer) { |
| 259 | $resultData['customers'][$key]['noShowCount'] = $customersNoShowCount[$key]['count']; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** Providers */ |
| 265 | if (in_array(Entities::EMPLOYEES, $params['types'], true)) { |
| 266 | /** @var ProviderRepository $providerRepository */ |
| 267 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 268 | |
| 269 | $providers = $providerRepository->getWithSchedule( |
| 270 | [ |
| 271 | 'dates' => !empty($params['dates']) ? $params['dates'] : [ |
| 272 | DateTimeService::getNowDateTimeObject()->modify('-1 days')->format('Y-m-d H:i:s') |
| 273 | ], |
| 274 | 'fetchCalendars' => $currentUser && $currentUser->getType() === AbstractUser::USER_ROLE_ADMIN, |
| 275 | ] |
| 276 | ); |
| 277 | |
| 278 | /** @var Provider $provider */ |
| 279 | foreach ($providers->getItems() as $provider) { |
| 280 | $providerService->setProviderServices($provider, $services, true); |
| 281 | } |
| 282 | |
| 283 | if ( |
| 284 | array_key_exists('page', $params) && |
| 285 | in_array($params['page'], [Entities::CALENDAR, Entities::APPOINTMENTS]) && |
| 286 | $userAS->isAdminAndAllowedToBookAtAnyTime() |
| 287 | ) { |
| 288 | $providerService->setProvidersAlwaysAvailable($providers); |
| 289 | } |
| 290 | |
| 291 | $resultData['entitiesRelations'] = []; |
| 292 | |
| 293 | /** @var Provider $provider */ |
| 294 | foreach ($providers->getItems() as $providerId => $provider) { |
| 295 | if ($data = $providerAS->getProviderServiceLocations($provider, $locations, $services)) { |
| 296 | $resultData['entitiesRelations'][$providerId] = $data; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | $resultData['employees'] = $providerAS->removeAllExceptUser( |
| 301 | $providers->toArray(), |
| 302 | (array_key_exists('page', $params) && $params['page'] === Entities::BOOKING) ? |
| 303 | null : $currentUser |
| 304 | ); |
| 305 | |
| 306 | if (array_key_exists('page', $params) && $params['page'] === Entities::BOOKING) { |
| 307 | $resultData['employees'] = $providerAS->filterEmployeesByEntitiesRelations( |
| 308 | $resultData['employees'], |
| 309 | $resultData['entitiesRelations'] |
| 310 | ); |
| 311 | } |
| 312 | |
| 313 | // Add calendar list to each provider's Google Calendar data |
| 314 | $settingsDS->getSetting('general', 'googleCalendar'); |
| 315 | $googleCalendar = $settingsDS->getSetting('googleCalendar', 'accessToken'); |
| 316 | $calendarList = []; |
| 317 | |
| 318 | if ($googleCalendar) { |
| 319 | try { |
| 320 | $googleCalendarMiddlewareService = $this->container->get('infrastructure.google.calendar.middleware.service'); |
| 321 | |
| 322 | $accessToken = json_decode($googleCalendar, true); |
| 323 | if (is_array($accessToken)) { |
| 324 | $calendarList = $googleCalendarMiddlewareService->getCalendarList($accessToken); |
| 325 | } |
| 326 | } catch (\Throwable $e) { |
| 327 | $calendarList = []; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | foreach ($resultData['employees'] as &$employee) { |
| 332 | if ($employee['googleCalendar'] && $employee['googleCalendar']['token']) { |
| 333 | continue; |
| 334 | } |
| 335 | |
| 336 | $employee['googleCalendarList'] = $calendarList; |
| 337 | } |
| 338 | |
| 339 | $outlookCalendar = $settingsDS->getSetting('outlookCalendar', 'accessToken'); |
| 340 | $calendarList = []; |
| 341 | |
| 342 | if ($outlookCalendar) { |
| 343 | try { |
| 344 | $outlookCalendarMiddlewareService = $this->container->get('infrastructure.outlook.calendar.middleware.service'); |
| 345 | |
| 346 | $accessToken = json_decode($outlookCalendar, true); |
| 347 | if (is_array($accessToken)) { |
| 348 | $calendarList = $outlookCalendarMiddlewareService->getCalendarList($accessToken); |
| 349 | } |
| 350 | } catch (\Throwable $e) { |
| 351 | $calendarList = []; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | foreach ($resultData['employees'] as &$employee) { |
| 356 | if ($employee['outlookCalendar'] && $employee['outlookCalendar']['token']) { |
| 357 | continue; |
| 358 | } |
| 359 | |
| 360 | $employee['outlookCalendarList'] = $calendarList; |
| 361 | } |
| 362 | |
| 363 | if ( |
| 364 | $currentUser === null || |
| 365 | $currentUser->getType() === AbstractUser::USER_ROLE_CUSTOMER || |
| 366 | !$command->getPermissionService()->currentUserCanRead(Entities::EMPLOYEES) |
| 367 | ) { |
| 368 | foreach ($resultData['employees'] as &$employee) { |
| 369 | unset( |
| 370 | $employee['appleCalendarId'], |
| 371 | $employee['googleCalendarId'], |
| 372 | $employee['googleCalendar'], |
| 373 | $employee['outlookCalendar'], |
| 374 | $employee['outlookCalendarId'], |
| 375 | $employee['stripeConnect'], |
| 376 | $employee['birthday'], |
| 377 | $employee['email'], |
| 378 | $employee['externalId'], |
| 379 | $employee['phone'], |
| 380 | $employee['note'], |
| 381 | $employee['employeeAppleCalendar'], |
| 382 | ); |
| 383 | |
| 384 | if (isset($params['page']) && $params['page'] !== Entities::CALENDAR) { |
| 385 | unset( |
| 386 | $employee['weekDayList'], |
| 387 | $employee['specialDayList'], |
| 388 | $employee['dayOffList'] |
| 389 | ); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | $resultData[Entities::APPOINTMENTS] = [ |
| 396 | 'futureAppointments' => [], |
| 397 | ]; |
| 398 | |
| 399 | if (in_array(Entities::APPOINTMENTS, $params['types'], true)) { |
| 400 | $userParams = [ |
| 401 | 'dates' => [null, null] |
| 402 | ]; |
| 403 | |
| 404 | if (!$command->getPermissionService()->currentUserCanReadOthers(Entities::APPOINTMENTS)) { |
| 405 | if ($currentUser->getId() === null) { |
| 406 | $userParams[$currentUser->getType() . 'Id'] = 0; |
| 407 | } else { |
| 408 | $userParams[$currentUser->getType() . 'Id'] = |
| 409 | $currentUser->getId()->getValue(); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /** @var AppointmentRepository $appointmentRepo */ |
| 414 | $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); |
| 415 | |
| 416 | $appointments = $appointmentRepo->getFiltered($userParams); |
| 417 | |
| 418 | $resultData[Entities::APPOINTMENTS] = [ |
| 419 | 'futureAppointments' => $appointments->toArray(), |
| 420 | ]; |
| 421 | } |
| 422 | |
| 423 | /** Custom Fields */ |
| 424 | if ( |
| 425 | in_array(Entities::CUSTOM_FIELDS, $params['types'], true) || |
| 426 | in_array('customFields', $params['types'], true) |
| 427 | ) { |
| 428 | $customFields = $customFieldAS->getAll(); |
| 429 | |
| 430 | if (!empty($params['lite'])) { |
| 431 | $resultData['customFields'] = []; |
| 432 | |
| 433 | /** @var CustomField $customField */ |
| 434 | foreach ($customFields->getItems() as $customField) { |
| 435 | $item = array_merge( |
| 436 | $customField->toArray(), |
| 437 | [ |
| 438 | 'services' => [], |
| 439 | 'events' => [], |
| 440 | ] |
| 441 | ); |
| 442 | |
| 443 | /** @var Service $service */ |
| 444 | foreach ($customField->getServices()->getItems() as $service) { |
| 445 | $item['services'][] = [ |
| 446 | 'id' => $service->getId()->getValue() |
| 447 | ]; |
| 448 | } |
| 449 | |
| 450 | /** @var Event $event */ |
| 451 | foreach ($customField->getEvents()->getItems() as $event) { |
| 452 | $item['events'][] = [ |
| 453 | 'id' => $event->getId()->getValue() |
| 454 | ]; |
| 455 | } |
| 456 | |
| 457 | $resultData['customFields'][] = $item; |
| 458 | } |
| 459 | } else { |
| 460 | $resultData['customFields'] = $customFields->toArray(); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /** Coupons */ |
| 465 | // Deprecated for backend use; replaced by `/coupons` endpoint. |
| 466 | // Retained for public `/entities` route and API access. |
| 467 | if ( |
| 468 | in_array(Entities::COUPONS, $params['types'], true) && |
| 469 | $this->getContainer()->getPermissionsService()->currentUserCanRead(Entities::COUPONS) |
| 470 | ) { |
| 471 | /** @var CouponRepository $couponRepository */ |
| 472 | $couponRepository = $this->container->get('domain.coupon.repository'); |
| 473 | |
| 474 | /** @var EventRepository $eventRepository */ |
| 475 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 476 | |
| 477 | /** @var PackageRepository $packageRepository */ |
| 478 | $packageRepository = $this->container->get('domain.bookable.package.repository'); |
| 479 | |
| 480 | $coupons = $couponRepository->getFiltered( |
| 481 | ['page' => 1], |
| 482 | 100 |
| 483 | ); |
| 484 | |
| 485 | if ($coupons->length()) { |
| 486 | foreach ($couponRepository->getCouponsServicesIds($coupons->keys()) as $ids) { |
| 487 | /** @var Coupon $coupon */ |
| 488 | $coupon = $coupons->getItem($ids['couponId']); |
| 489 | |
| 490 | if ($coupon->getAllServices() && $coupon->getAllServices()->getValue()) { |
| 491 | $coupon->setServiceList(new Collection($allServices->getItems())); |
| 492 | continue; |
| 493 | } |
| 494 | |
| 495 | $coupon->getServiceList()->addItem( |
| 496 | $allServices->getItem($ids['serviceId']), |
| 497 | $ids['serviceId'] |
| 498 | ); |
| 499 | } |
| 500 | |
| 501 | $allEvents = $eventRepository->getAllIndexedById(); |
| 502 | |
| 503 | foreach ($couponRepository->getCouponsEventsIds($coupons->keys()) as $ids) { |
| 504 | /** @var Coupon $coupon */ |
| 505 | $coupon = $coupons->getItem($ids['couponId']); |
| 506 | |
| 507 | if ($coupon->getAllEvents() && $coupon->getAllEvents()->getValue()) { |
| 508 | $coupon->setEventList(new Collection($allEvents->getItems())); |
| 509 | continue; |
| 510 | } |
| 511 | |
| 512 | if ($allEvents->keyExists($ids['eventId'])) { |
| 513 | $coupon->getEventList()->addItem( |
| 514 | $allEvents->getItem($ids['eventId']), |
| 515 | $ids['eventId'] |
| 516 | ); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | $allPackages = $packageRepository->getAllIndexedById(); |
| 521 | |
| 522 | foreach ($couponRepository->getCouponsPackagesIds($coupons->keys()) as $ids) { |
| 523 | /** @var Coupon $coupon */ |
| 524 | $coupon = $coupons->getItem($ids['couponId']); |
| 525 | |
| 526 | if ($coupon->getAllPackages() && $coupon->getAllPackages()->getValue()) { |
| 527 | $coupon->setPackageList(new Collection($allPackages->getItems())); |
| 528 | continue; |
| 529 | } |
| 530 | |
| 531 | if ($allPackages->keyExists($ids['packageId'])) { |
| 532 | $coupon->getPackageList()->addItem( |
| 533 | $allPackages->getItem($ids['packageId']), |
| 534 | $ids['packageId'] |
| 535 | ); |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | if (!empty($params['lite'])) { |
| 541 | $resultData['coupons'] = []; |
| 542 | |
| 543 | /** @var Coupon $coupon */ |
| 544 | foreach ($coupons->getItems() as $coupon) { |
| 545 | $item = array_merge( |
| 546 | $coupon->toArray(), |
| 547 | [ |
| 548 | 'serviceList' => [], |
| 549 | 'eventList' => [], |
| 550 | 'packageList' => [], |
| 551 | ] |
| 552 | ); |
| 553 | |
| 554 | /** @var Service $service */ |
| 555 | foreach ($coupon->getServiceList()->getItems() as $service) { |
| 556 | $item['serviceList'][] = [ |
| 557 | 'id' => $service->getId()->getValue() |
| 558 | ]; |
| 559 | } |
| 560 | |
| 561 | /** @var Event $event */ |
| 562 | foreach ($coupon->getEventList()->getItems() as $event) { |
| 563 | $item['eventList'][] = [ |
| 564 | 'id' => $event->getId()->getValue() |
| 565 | ]; |
| 566 | } |
| 567 | |
| 568 | /** @var Package $package */ |
| 569 | foreach ($coupon->getPackageList()->getItems() as $package) { |
| 570 | $item['packageList'][] = [ |
| 571 | 'id' => $package->getId()->getValue() |
| 572 | ]; |
| 573 | } |
| 574 | |
| 575 | $resultData['coupons'][] = $item; |
| 576 | } |
| 577 | } else { |
| 578 | $resultData['coupons'] = $coupons->toArray(); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /** Settings */ |
| 583 | if (in_array(Entities::SETTINGS, $params['types'], true)) { |
| 584 | /** @var HelperService $helperService */ |
| 585 | $helperService = $this->container->get('application.helper.service'); |
| 586 | |
| 587 | $languages = $helperService->getLanguages(); |
| 588 | |
| 589 | usort( |
| 590 | $languages, |
| 591 | function ($x, $y) { |
| 592 | return strcasecmp($x['name'], $y['name']); |
| 593 | } |
| 594 | ); |
| 595 | |
| 596 | $languagesSorted = []; |
| 597 | |
| 598 | foreach ($languages as $language) { |
| 599 | $languagesSorted[$language['wp_locale']] = $language; |
| 600 | } |
| 601 | |
| 602 | /** @var \AmeliaBooking\Application\Services\Settings\SettingsService $settingsAS*/ |
| 603 | $settingsAS = $this->container->get('application.settings.service'); |
| 604 | |
| 605 | $daysOff = $settingsAS->getDaysOff(); |
| 606 | |
| 607 | $squareLocations = []; |
| 608 | if ( |
| 609 | !empty($settingsDS->getSetting('payments', 'square')['accessToken']['access_token']) |
| 610 | && in_array('squareLocations', $params['types']) |
| 611 | ) { |
| 612 | /** @var SquareService $squareService */ |
| 613 | $squareService = $this->container->get('infrastructure.payment.square.service'); |
| 614 | |
| 615 | try { |
| 616 | $squareLocations = $squareService->getLocations(); |
| 617 | } catch (\Exception $e) { |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | $mailchimpLists = []; |
| 622 | if ( |
| 623 | $settingsDS->isFeatureEnabled('mailchimp') && |
| 624 | !empty($settingsDS->getSetting('mailchimp', 'accessToken')) && |
| 625 | in_array('mailchimpLists', $params['types']) |
| 626 | ) { |
| 627 | /** @var AbstractMailchimpService $mailchimpService */ |
| 628 | $mailchimpService = $this->container->get('infrastructure.mailchimp.service'); |
| 629 | |
| 630 | try { |
| 631 | $mailchimpLists = $mailchimpService->getLists(); |
| 632 | |
| 633 | $mailchimpSettings = $settingsDS->getCategorySettings('mailchimp'); |
| 634 | if (!empty($mailchimpLists) && !$mailchimpSettings['list']) { |
| 635 | $mailchimpSettings['list'] = $mailchimpLists[0]; |
| 636 | $settingsDS->setCategorySettings('mailchimp', $mailchimpSettings); |
| 637 | } |
| 638 | } catch (\Exception $e) { |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | $resultData['settings'] = [ |
| 643 | 'general' => [ |
| 644 | 'usedLanguages' => $settingsDS->getSetting('general', 'usedLanguages'), |
| 645 | ], |
| 646 | 'languages' => $languagesSorted, |
| 647 | 'daysOff' => $daysOff, |
| 648 | 'squareLocations' => $squareLocations, |
| 649 | 'mailchimpLists' => $mailchimpLists, |
| 650 | ]; |
| 651 | } |
| 652 | |
| 653 | /** Packages */ |
| 654 | if (in_array(Entities::PACKAGES, $params['types'], true)) { |
| 655 | /** @var AbstractPackageApplicationService $packageApplicationService */ |
| 656 | $packageApplicationService = $this->container->get('application.bookable.package'); |
| 657 | |
| 658 | $resultData['packages'] = $packageApplicationService->getPackagesArray(); |
| 659 | } |
| 660 | |
| 661 | /** Resources */ |
| 662 | if (in_array(Entities::RESOURCES, $params['types'], true)) { |
| 663 | /** @var AbstractResourceApplicationService $resourceApplicationService */ |
| 664 | $resourceApplicationService = $this->container->get('application.resource.service'); |
| 665 | |
| 666 | $resources = $resourceApplicationService->getAll([]); |
| 667 | |
| 668 | $resultData['resources'] = $resources->toArray(); |
| 669 | } |
| 670 | |
| 671 | /** Taxes */ |
| 672 | if (in_array(Entities::TAXES, $params['types'], true)) { |
| 673 | /** @var TaxApplicationService $taxApplicationService */ |
| 674 | $taxApplicationService = $this->container->get('application.tax.service'); |
| 675 | |
| 676 | $taxes = $taxApplicationService->getAll(); |
| 677 | |
| 678 | $resultData['taxes'] = $taxes->toArray(); |
| 679 | } |
| 680 | |
| 681 | /** Lesson Spaces */ |
| 682 | if ( |
| 683 | in_array('lessonSpace_spaces', $params['types'], true) || |
| 684 | in_array('spaces', $params['types'], true) |
| 685 | ) { |
| 686 | $lessonSpaceApiKey = $settingsDS->getSetting('lessonSpace', 'apiKey'); |
| 687 | $lessonSpaceEnabled = $settingsDS->getSetting('lessonSpace', 'enabled'); |
| 688 | $lessonSpaceCompanyId = $settingsDS->getSetting('lessonSpace', 'companyId'); |
| 689 | |
| 690 | if ($lessonSpaceEnabled && $lessonSpaceApiKey) { |
| 691 | /** @var AbstractLessonSpaceService $lessonSpaceService */ |
| 692 | $lessonSpaceService = $this->container->get('infrastructure.lesson.space.service'); |
| 693 | |
| 694 | if (empty($lessonSpaceCompanyId)) { |
| 695 | $companyDetails = $lessonSpaceService->getCompanyId($lessonSpaceApiKey); |
| 696 | $lessonSpaceCompanyId = !empty($companyDetails) && !empty($companyDetails['id']) ? $companyDetails['id'] : null; |
| 697 | } |
| 698 | |
| 699 | $resultData['spaces'] = $lessonSpaceService->getAllSpaces( |
| 700 | $lessonSpaceApiKey, |
| 701 | $lessonSpaceCompanyId, |
| 702 | !empty($params['lessonSpaceSearch']) ? $params['lessonSpaceSearch'] : null |
| 703 | ); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | /** IvyForms */ |
| 708 | if (in_array('ivy', $params['types'], true)) { |
| 709 | $forms = IvyFormsService::getForms(); |
| 710 | |
| 711 | $resultData['ivy'] = $forms |
| 712 | ? array_merge([['value' => '', 'label' => BackendStrings::get('ivy_select')]], $forms) |
| 713 | : []; |
| 714 | } |
| 715 | |
| 716 | if (!empty($params['ivyId'])) { |
| 717 | $resultData['ivyFields'] = IvyFormsService::getFormFields((int)$params['ivyId']); |
| 718 | } |
| 719 | |
| 720 | |
| 721 | $resultData = apply_filters('amelia_get_entities_filter', $resultData); |
| 722 | |
| 723 | do_action('amelia_get_entities', $resultData); |
| 724 | |
| 725 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 726 | $result->setMessage('Successfully retrieved entities'); |
| 727 | $result->setData($resultData); |
| 728 | |
| 729 | return $result; |
| 730 | } |
| 731 | } |
| 732 |