PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.4
Booking for Appointments and Events Calendar – Amelia v1.2.4
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Entities / GetEntitiesCommandHandler.php
ameliabooking / src / Application / Commands / Entities Last commit date
GetEntitiesCommand.php 1 year ago GetEntitiesCommandHandler.php 1 year ago
GetEntitiesCommandHandler.php
596 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\Coupon\AbstractCouponApplicationService;
11 use AmeliaBooking\Application\Services\CustomField\AbstractCustomFieldApplicationService;
12 use AmeliaBooking\Application\Services\Helper\HelperService;
13 use AmeliaBooking\Application\Services\Location\AbstractLocationApplicationService;
14 use AmeliaBooking\Application\Services\Resource\AbstractResourceApplicationService;
15 use AmeliaBooking\Application\Services\Tax\TaxApplicationService;
16 use AmeliaBooking\Application\Services\User\ProviderApplicationService;
17 use AmeliaBooking\Application\Services\User\UserApplicationService;
18 use AmeliaBooking\Domain\Collection\Collection;
19 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
20 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
21 use AmeliaBooking\Domain\Entity\Bookable\Service\Service;
22 use AmeliaBooking\Domain\Entity\Booking\Event\Event;
23 use AmeliaBooking\Domain\Entity\Coupon\Coupon;
24 use AmeliaBooking\Domain\Entity\Entities;
25 use AmeliaBooking\Domain\Entity\User\AbstractUser;
26 use AmeliaBooking\Domain\Entity\User\Customer;
27 use AmeliaBooking\Domain\Entity\User\Provider;
28 use AmeliaBooking\Domain\Services\Booking\EventDomainService;
29 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
30 use AmeliaBooking\Domain\Services\Settings\SettingsService;
31 use AmeliaBooking\Domain\Services\User\ProviderService;
32 use AmeliaBooking\Domain\ValueObjects\String\Status;
33 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
34 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
35 use AmeliaBooking\Infrastructure\Licence\Licence as Licence;
36 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\CategoryRepository;
37 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\PackageRepository;
38 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository;
39 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository;
40 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
41 use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository;
42 use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventTagsRepository;
43 use AmeliaBooking\Infrastructure\Repository\Coupon\CouponRepository;
44 use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository;
45 use AmeliaBooking\Infrastructure\Repository\User\UserRepository;
46 use AmeliaBooking\Infrastructure\Services\LessonSpace\AbstractLessonSpaceService;
47 use AmeliaBooking\Infrastructure\Services\Payment\SquareService;
48 use Interop\Container\Exception\ContainerException;
49
50 /**
51 * Class GetEntitiesCommandHandler
52 *
53 * @package AmeliaBooking\Application\Commands\Entities
54 */
55 class GetEntitiesCommandHandler extends CommandHandler
56 {
57 /**
58 * @param GetEntitiesCommand $command
59 *
60 * @return CommandResult
61 * @throws AccessDeniedException
62 * @throws ContainerException
63 * @throws InvalidArgumentException
64 * @throws NotFoundException
65 * @throws QueryExecutionException
66 */
67 public function handle(GetEntitiesCommand $command)
68 {
69 /** @var UserApplicationService $userAS */
70 $userAS = $this->container->get('application.user.service');
71
72 /** @var EventDomainService $eventDS */
73 $eventDS = $this->container->get('domain.booking.event.service');
74
75 /** @var ProviderApplicationService $providerAS */
76 $providerAS = $this->container->get('application.user.provider.service');
77
78 /** @var AbstractCustomFieldApplicationService $customFieldAS */
79 $customFieldAS = $this->container->get('application.customField.service');
80
81 /** @var AbstractLocationApplicationService $locationAS */
82 $locationAS = $this->container->get('application.location.service');
83
84 /** @var AbstractCouponApplicationService $couponAS */
85 $couponAS = $this->container->get('application.coupon.service');
86
87 /** @var ProviderService $providerService */
88 $providerService = $this->container->get('domain.user.provider.service');
89
90 try {
91 /** @var AbstractUser $currentUser */
92 $currentUser = $command->getUserApplicationService()->authorization(
93 $command->getPage() === 'cabinet' ? $command->getToken() : null,
94 $command->getCabinetType()
95 );
96 } catch (AuthorizationException $e) {
97 $currentUser = null;
98 }
99
100 $params = $command->getField('params');
101
102 $result = new CommandResult();
103
104 $this->checkMandatoryFields($command);
105
106 /** @var Collection $allServices */
107 $allServices = new Collection();
108
109 /** @var Collection $services */
110 $services = new Collection();
111
112 /** @var Collection $locations */
113 $locations = new Collection();
114
115 /** @var Collection $categories */
116 $categories = new Collection();
117
118 /** @var Collection $events */
119 $events = new Collection();
120
121 $resultData = [];
122
123
124
125 /** Events */
126 if (in_array(Entities::EVENTS, $params['types'], true)) {
127 /** @var EventRepository $eventRepository */
128 $eventRepository = $this->container->get('domain.booking.event.repository');
129
130 $dateFilter = ['dates' => [DateTimeService::getNowDateTime()], 'itemsPerPage' => 10000, 'page' => 1];
131
132 /** @var Collection $events */
133 $events = $eventRepository->getFiltered($dateFilter);
134
135 /** @var Event $event */
136 foreach ($events->getItems() as $event) {
137 $event->setBookings(new Collection());
138 }
139
140 $resultData['events'] = $events->toArray();
141
142 $resultData['events'] = $eventDS->getShortcodeForEventList($this->container, $resultData['events']);
143 }
144
145 /** Event Tags */
146 if (in_array(Entities::TAGS, $params['types'], true)) {
147 /** @var EventTagsRepository $eventTagsRepository */
148 $eventTagsRepository = $this->container->get('domain.booking.event.tag.repository');
149
150 /** @var Collection $eventsTags */
151 $eventsTags = $eventTagsRepository->getAllDistinctByCriteria(
152 $events->length() ? ['eventIds' => array_column($events->toArray(), 'id')] : []
153 );
154
155 $resultData['tags'] = $eventsTags->toArray();
156 }
157
158 if (in_array(Entities::LOCATIONS, $params['types'], true) ||
159 in_array(Entities::EMPLOYEES, $params['types'], true)
160 ) {
161 /** @var Collection $locations */
162 $locations = $locationAS->getAllOrderedByName();
163 }
164
165 /** Locations */
166 if (in_array(Entities::LOCATIONS, $params['types'], true)) {
167 $resultData['locations'] = $locations->toArray();
168 }
169
170 if (in_array(Entities::CATEGORIES, $params['types'], true) ||
171 in_array(Entities::EMPLOYEES, $params['types'], true) ||
172 in_array(Entities::COUPONS, $params['types'], true)
173 ) {
174 /** @var ServiceRepository $serviceRepository */
175 $serviceRepository = $this->container->get('domain.bookable.service.repository');
176 /** @var CategoryRepository $categoryRepository */
177 $categoryRepository = $this->container->get('domain.bookable.category.repository');
178 /** @var BookableApplicationService $bookableAS */
179 $bookableAS = $this->container->get('application.bookable.service');
180
181 /** @var Collection $allServices */
182 $allServices = $serviceRepository->getAllArrayIndexedById();
183
184 /** @var Service $service */
185 foreach ($allServices->getItems() as $service) {
186 if ($service->getStatus()->getValue() === Status::VISIBLE ||
187 Licence::$premium ||
188 ($currentUser && $currentUser->getType() === AbstractUser::USER_ROLE_ADMIN)
189 ) {
190 $services->addItem($service, $service->getId()->getValue());
191 }
192 }
193
194 /** @var Collection $categories */
195 $categories = $categoryRepository->getAllIndexedById();
196
197 $bookableAS->addServicesToCategories($categories, $services);
198 }
199
200 /** Categories */
201 if (in_array(Entities::CATEGORIES, $params['types'], true)) {
202 $resultData['categories'] = $categories->toArray();
203 }
204
205 /** @var SettingsService $settingsDS */
206 $settingsDS = $this->container->get('domain.settings.service');
207
208 $resultData['customers'] = [];
209
210 /** Customers */
211 if (in_array(Entities::CUSTOMERS, $params['types'], true)) {
212 /** @var UserRepository $userRepo */
213 $userRepo = $this->getContainer()->get('domain.users.repository');
214
215 $resultData['customers'] = [];
216
217 if ($currentUser) {
218 switch ($currentUser->getType()) {
219 case (AbstractUser::USER_ROLE_CUSTOMER):
220 if ($currentUser->getId()) {
221 /** @var Customer $customer */
222 $customer = $userRepo->getById($currentUser->getId()->getValue());
223
224 $resultData['customers'] = [$customer->toArray()];
225 }
226
227 break;
228
229 case (AbstractUser::USER_ROLE_PROVIDER):
230 $resultData['customers'] = $providerAS->getAllowedCustomers($currentUser)->toArray();
231
232 break;
233
234 default:
235 /** @var Collection $customers */
236 $customers = $userRepo->getAllWithAllowedBooking();
237
238 $resultData['customers'] = $customers->toArray();
239 }
240 }
241
242 $noShowTagEnabled = $settingsDS->getSetting('roles', 'enableNoShowTag');
243
244 if ($noShowTagEnabled && $resultData['customers']) {
245 /** @var CustomerBookingRepository $bookingRepository */
246 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
247
248 $usersIds = array_map(function ($user) { return $user['id']; }, $resultData['customers']);
249
250 $customersNoShowCount = $bookingRepository->countByNoShowStatus($usersIds);
251
252 foreach ($resultData['customers'] as $key => $customer) {
253 $resultData['customers'][$key]['noShowCount'] = $customersNoShowCount[$key]['count'];
254 }
255 }
256 }
257
258 /** Providers */
259 if (in_array(Entities::EMPLOYEES, $params['types'], true)) {
260 /** @var ProviderRepository $providerRepository */
261 $providerRepository = $this->container->get('domain.users.providers.repository');
262
263 /** @var Collection $testProviders */
264 $providers = $providerRepository->getWithSchedule([]);
265
266 /** @var Provider $provider */
267 foreach ($providers->getItems() as $provider) {
268 $providerService->setProviderServices($provider, $services, true);
269 }
270
271 if (array_key_exists('page', $params) &&
272 in_array($params['page'], [Entities::CALENDAR, Entities::APPOINTMENTS]) &&
273 $userAS->isAdminAndAllowedToBookAtAnyTime()
274 ) {
275 $providerService->setProvidersAlwaysAvailable($providers);
276 }
277
278 $resultData['entitiesRelations'] = [];
279
280 /** @var Provider $provider */
281 foreach ($providers->getItems() as $providerId => $provider) {
282 if ($data = $providerAS->getProviderServiceLocations($provider, $locations, $services)) {
283 $resultData['entitiesRelations'][$providerId] = $data;
284 }
285 }
286
287
288 $resultData['employees'] = $providerAS->removeAllExceptUser(
289 $providers->toArray(),
290 (array_key_exists('page', $params) && $params['page'] === Entities::BOOKING) ?
291 null : $currentUser
292 );
293
294 if ($currentUser === null || $currentUser->getType() === AbstractUser::USER_ROLE_CUSTOMER) {
295 foreach ($resultData['employees'] as &$employee) {
296 unset(
297 $employee['googleCalendar'],
298 $employee['outlookCalendar'],
299 $employee['stripeConnect'],
300 $employee['birthday'],
301 $employee['email'],
302 $employee['externalId'],
303 $employee['phone'],
304 $employee['note']
305 );
306
307 if (isset($params['page']) && $params['page'] !== Entities::CALENDAR) {
308 unset(
309 $employee['weekDayList'],
310 $employee['specialDayList'],
311 $employee['dayOffList']
312 );
313 }
314 }
315 }
316 }
317
318 $resultData[Entities::APPOINTMENTS] = [
319 'futureAppointments' => [],
320 ];
321
322 if (in_array(Entities::APPOINTMENTS, $params['types'], true)) {
323 $userParams = [
324 'dates' => [null, null]
325 ];
326
327 if (!$command->getPermissionService()->currentUserCanReadOthers(Entities::APPOINTMENTS)) {
328 if ($currentUser->getId() === null) {
329 $userParams[$currentUser->getType() . 'Id'] = 0;
330 } else {
331 $userParams[$currentUser->getType() . 'Id'] =
332 $currentUser->getId()->getValue();
333 }
334 }
335
336 /** @var AppointmentRepository $appointmentRepo */
337 $appointmentRepo = $this->container->get('domain.booking.appointment.repository');
338
339 /** @var Collection $appointments */
340 $appointments = $appointmentRepo->getFiltered($userParams);
341
342 $resultData[Entities::APPOINTMENTS] = [
343 'futureAppointments' => $appointments->toArray(),
344 ];
345 }
346
347 /** Custom Fields */
348 if (in_array(Entities::CUSTOM_FIELDS, $params['types'], true) ||
349 in_array('customFields', $params['types'], true)
350 ) {
351 /** @var Collection $customFields */
352 $customFields = $customFieldAS->getAll();
353
354 if (!empty($params['lite'])) {
355 $resultData['customFields'] = [];
356
357 /** @var CustomField $customField */
358 foreach ($customFields->getItems() as $customField) {
359 $item = array_merge(
360 $customField->toArray(),
361 [
362 'services' => [],
363 'events' => [],
364 ]
365 );
366
367 /** @var Service $service */
368 foreach ($customField->getServices()->getItems() as $service) {
369 $item['services'][] = [
370 'id' => $service->getId()->getValue()
371 ];
372 }
373
374 /** @var Event $event */
375 foreach ($customField->getEvents()->getItems() as $event) {
376 $item['events'][] = [
377 'id' => $event->getId()->getValue()
378 ];
379 }
380
381 $resultData['customFields'][] = $item;
382 }
383 } else {
384 $resultData['customFields'] = $customFields->toArray();
385 }
386 }
387
388 /** Coupons */
389 if (in_array(Entities::COUPONS, $params['types'], true) &&
390 $this->getContainer()->getPermissionsService()->currentUserCanRead(Entities::COUPONS)
391 ) {
392 /** @var Collection $coupons */
393 $coupons = $couponAS->getAll();
394
395 /** @var CouponRepository $couponRepository */
396 $couponRepository = $this->container->get('domain.coupon.repository');
397
398 /** @var EventRepository $eventRepository */
399 $eventRepository = $this->container->get('domain.booking.event.repository');
400
401 /** @var PackageRepository $packageRepository */
402 $packageRepository = $this->container->get('domain.bookable.package.repository');
403
404 if ($coupons->length()) {
405 foreach ($couponRepository->getCouponsServicesIds($coupons->keys()) as $ids) {
406 /** @var Coupon $coupon */
407 $coupon = $coupons->getItem($ids['couponId']);
408
409 $coupon->getServiceList()->addItem(
410 $allServices->getItem($ids['serviceId']),
411 $ids['serviceId']
412 );
413 }
414
415 /** @var Collection $allEvents */
416 $allEvents = $eventRepository->getAllIndexedById();
417
418 foreach ($couponRepository->getCouponsEventsIds($coupons->keys()) as $ids) {
419 /** @var Coupon $coupon */
420 $coupon = $coupons->getItem($ids['couponId']);
421
422 $coupon->getEventList()->addItem(
423 $allEvents->getItem($ids['eventId']),
424 $ids['eventId']
425 );
426 }
427
428 /** @var Collection $allPackages */
429 $allPackages = $packageRepository->getAllIndexedById();
430
431 foreach ($couponRepository->getCouponsPackagesIds($coupons->keys()) as $ids) {
432 /** @var Coupon $coupon */
433 $coupon = $coupons->getItem($ids['couponId']);
434
435 $coupon->getPackageList()->addItem(
436 $allPackages->getItem($ids['packageId']),
437 $ids['packageId']
438 );
439 }
440 }
441
442 if (!empty($params['lite'])) {
443 $resultData['coupons'] = [];
444
445 /** @var Coupon $coupon */
446 foreach ($coupons->getItems() as $coupon) {
447 $item = array_merge(
448 $coupon->toArray(),
449 [
450 'serviceList' => [],
451 'eventList' => [],
452 'packageList' => [],
453 ]
454 );
455
456 /** @var Service $service */
457 foreach ($coupon->getServiceList()->getItems() as $service) {
458 $item['serviceList'][] = [
459 'id' => $service->getId()->getValue()
460 ];
461 }
462
463 /** @var Event $event */
464 foreach ($coupon->getEventList()->getItems() as $event) {
465 $item['eventList'][] = [
466 'id' => $event->getId()->getValue()
467 ];
468 }
469
470 /** @var Package $package */
471 foreach ($coupon->getPackageList()->getItems() as $package) {
472 $item['packageList'][] = [
473 'id' => $package->getId()->getValue()
474 ];
475 }
476
477 $resultData['coupons'][] = $item;
478 }
479 } else {
480 $resultData['coupons'] = $coupons->toArray();
481 }
482 }
483
484 /** Settings */
485 if (in_array(Entities::SETTINGS, $params['types'], true)) {
486 /** @var HelperService $helperService */
487 $helperService = $this->container->get('application.helper.service');
488
489 $languages = $helperService->getLanguages();
490
491 usort(
492 $languages,
493 function ($x, $y) {
494 return strcasecmp($x['name'], $y['name']);
495 }
496 );
497
498 $languagesSorted = [];
499
500 foreach ($languages as $language) {
501 $languagesSorted[$language['wp_locale']] = $language;
502 }
503
504 /** @var \AmeliaBooking\Application\Services\Settings\SettingsService $settingsAS*/
505 $settingsAS = $this->container->get('application.settings.service');
506
507 $daysOff = $settingsAS->getDaysOff();
508
509 $squareLocations = [];
510 if (!empty($settingsDS->getSetting('payments', 'square')['accessToken']['access_token'])
511 && in_array('squareLocations', $params['types'])) {
512 /** @var SquareService $squareService */
513 $squareService = $this->container->get('infrastructure.payment.square.service');
514
515 try {
516 $squareLocations = $squareService->getLocations();
517 } catch (\Exception $e) {
518 }
519 }
520
521 $resultData['settings'] = [
522 'general' => [
523 'usedLanguages' => $settingsDS->getSetting('general', 'usedLanguages'),
524 ],
525 'languages' => $languagesSorted,
526 'daysOff' => $daysOff,
527 'squareLocations' => $squareLocations
528 ];
529 }
530
531 /** Packages */
532 if (in_array(Entities::PACKAGES, $params['types'], true)) {
533 /** @var AbstractPackageApplicationService $packageApplicationService */
534 $packageApplicationService = $this->container->get('application.bookable.package');
535
536 $resultData['packages'] = $packageApplicationService->getPackagesArray();
537 }
538
539 /** Resources */
540 if (in_array(Entities::RESOURCES, $params['types'], true)) {
541 /** @var AbstractResourceApplicationService $resourceApplicationService */
542 $resourceApplicationService = $this->container->get('application.resource.service');
543
544 /** @var Collection $resources */
545 $resources = $resourceApplicationService->getAll([]);
546
547 $resultData['resources'] = $resources->toArray();
548 }
549
550 /** Taxes */
551 if (in_array(Entities::TAXES, $params['types'], true)) {
552 /** @var TaxApplicationService $taxApplicationService */
553 $taxApplicationService = $this->container->get('application.tax.service');
554
555 /** @var Collection $taxes */
556 $taxes = $taxApplicationService->getAll();
557
558 $resultData['taxes'] = $taxes->toArray();
559 }
560
561 /** Lesson Spaces */
562 if (in_array('lessonSpace_spaces', $params['types'], true)) {
563 $lessonSpaceApiKey = $settingsDS->getSetting('lessonSpace', 'apiKey');
564 $lessonSpaceEnabled = $settingsDS->getSetting('lessonSpace', 'enabled');
565 $lessonSpaceCompanyId = $settingsDS->getSetting('lessonSpace', 'companyId');
566
567 if ($lessonSpaceEnabled && $lessonSpaceApiKey) {
568 /** @var AbstractLessonSpaceService $lessonSpaceService */
569 $lessonSpaceService = $this->container->get('infrastructure.lesson.space.service');
570
571 if (empty($lessonSpaceCompanyId)) {
572 $companyDetails = $lessonSpaceService->getCompanyId($lessonSpaceApiKey);
573 $lessonSpaceCompanyId = !empty($companyDetails) && !empty($companyDetails['id']) ? $companyDetails['id'] : null;
574 }
575
576 $resultData['spaces'] = $lessonSpaceService->getAllSpaces(
577 $lessonSpaceApiKey,
578 $lessonSpaceCompanyId,
579 !empty($params['lessonSpaceSearch']) ? $params['lessonSpaceSearch'] : null
580 );
581 }
582 }
583
584
585 $resultData = apply_filters('amelia_get_entities_filter', $resultData);
586
587 do_action('amelia_get_entities', $resultData);
588
589 $result->setResult(CommandResult::RESULT_SUCCESS);
590 $result->setMessage('Successfully retrieved entities');
591 $result->setData($resultData);
592
593 return $result;
594 }
595 }
596