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