PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.31
Booking for Appointments and Events Calendar – Amelia v1.2.31
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 / Domain / Services / Entity / EntityService.php
ameliabooking / src / Domain / Services / Entity Last commit date
EntityService.php 1 year ago
EntityService.php
224 lines
1 <?php
2
3 namespace AmeliaBooking\Domain\Services\Entity;
4
5 use AmeliaBooking\Domain\Collection\Collection;
6 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
7 use AmeliaBooking\Domain\Entity\Bookable\Service\Extra;
8 use AmeliaBooking\Domain\Entity\Bookable\Service\Service;
9 use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment;
10 use AmeliaBooking\Domain\Entity\Booking\SlotsEntities;
11 use AmeliaBooking\Domain\Entity\User\Provider;
12 use AmeliaBooking\Domain\Factory\Booking\SlotsEntitiesFactory;
13 use AmeliaBooking\Domain\Services\Resource\AbstractResourceService;
14 use AmeliaBooking\Domain\Services\User\ProviderService;
15 use AmeliaBooking\Domain\ValueObjects\Duration;
16
17 /**
18 * Class EntityService
19 *
20 * @package AmeliaBooking\Domain\Services\Entity
21 */
22 class EntityService
23 {
24 /** @var ProviderService */
25 private $providerService;
26
27 /** @var AbstractResourceService */
28 private $resourceService;
29
30 /**
31 * EntityService constructor.
32 *
33 * @param ProviderService $providerService
34 * @param AbstractResourceService $resourceService
35 */
36 public function __construct(
37 ProviderService $providerService,
38 AbstractResourceService $resourceService
39 ) {
40 $this->providerService = $providerService;
41
42 $this->resourceService = $resourceService;
43 }
44
45 /**
46 * get filtered entities needed for slots calculation.
47 *
48 * @param array $settings
49 * @param array $props
50 * @param SlotsEntities $slotsEntities
51 *
52 * @return SlotsEntities
53 * @throws InvalidArgumentException
54 */
55 public function getFilteredSlotsEntities($settings, $props, $slotsEntities)
56 {
57 /** @var Collection $services */
58 $services = $slotsEntities->getServices() ?: new Collection();
59
60 /** @var Collection $providers */
61 $providers = $slotsEntities->getProviders() ?: new Collection();
62
63 /** @var Collection $locations */
64 $locations = $slotsEntities->getLocations() ?: new Collection();
65
66 /** @var Collection $filteredProviders */
67 $filteredProviders = new Collection();
68
69 /** @var Provider $provider */
70 foreach ($providers->getItems() as $provider) {
71 if ($provider->getServiceList()->keyExists($props['serviceId'])) {
72 if ($settings['allowAdminBookAtAnyTime']) {
73 $this->providerService->setProvidersAlwaysAvailable(
74 $providers
75 );
76 }
77
78 $this->providerService->setProviderServices(
79 $provider,
80 $services,
81 false
82 );
83
84 /** @var Service $service */
85 foreach ($provider->getServiceList()->getItems() as $service) {
86 $this->checkServiceTimes($service);
87 }
88
89 $filteredProviders->addItem($provider, $provider->getId()->getValue());
90 }
91 }
92
93 /** @var Service $service */
94 foreach ($services->getItems() as $service) {
95 $this->checkServiceTimes($service);
96 }
97
98 /** @var Collection $serviceResources */
99 $serviceResources = $slotsEntities->getResources() ? $this->resourceService->getServiceResources(
100 $slotsEntities->getResources(),
101 $props['serviceId']
102 ) : new Collection();
103
104 $this->resourceService->setNonSharedResources(
105 $serviceResources,
106 [
107 'service' => $services->keys(),
108 'location' => $locations->keys(),
109 ]
110 );
111
112 /** @var SlotsEntities $filteredSlotsEntities */
113 $filteredSlotsEntities = SlotsEntitiesFactory::create();
114
115 $filteredSlotsEntities->setServices($services);
116
117 $filteredSlotsEntities->setProviders($filteredProviders);
118
119 $filteredSlotsEntities->setLocations($locations);
120
121 $filteredSlotsEntities->setResources($serviceResources);
122
123 return $filteredSlotsEntities;
124 }
125
126 /**
127 * Add 0 as duration for service time before or time after if it is null
128 *
129 * @param Service $service
130 *
131 * @throws InvalidArgumentException
132 */
133 private function checkServiceTimes($service)
134 {
135 if (!$service->getTimeBefore()) {
136 $service->setTimeBefore(new Duration(0));
137 }
138
139 if (!$service->getTimeAfter()) {
140 $service->setTimeAfter(new Duration(0));
141 }
142 }
143
144 /**
145 * filter appointments required for slots calculation
146 *
147 * @param SlotsEntities $slotsEntities
148 * @param Collection $appointments
149 * @param array $props
150 *
151 * @return void
152 * @throws InvalidArgumentException
153 */
154 public function filterSlotsAppointments($slotsEntities, $appointments, $props)
155 {
156 /** @var Collection $services */
157 $services = $slotsEntities->getServices();
158
159 /** @var Collection $providers */
160 $providers = $slotsEntities->getProviders();
161
162 $providersIds = $providers->keys();
163
164 /** @var Appointment $appointment */
165 foreach ($appointments->getItems() as $index => $appointment) {
166 if (
167 !in_array($appointment->getProviderId()->getValue(), $providersIds) ||
168 (
169 $props['excludeAppointmentId'] && $index === $props['excludeAppointmentId']
170 )
171 ) {
172 $appointments->deleteItem($index);
173 }
174 }
175
176 /** @var Appointment $appointment */
177 foreach ($appointments->getItems() as $index => $appointment) {
178 /** @var Provider $provider */
179 $provider = $providers->getItem($appointment->getProviderId()->getValue());
180
181 /** @var Service $providerService */
182 $providerService = $provider->getServiceList()->keyExists($appointment->getServiceId()->getValue()) ?
183 $provider->getServiceList()->getItem($appointment->getServiceId()->getValue()) :
184 $services->getItem($appointment->getServiceId()->getValue());
185
186 $appointment->setService($providerService);
187 }
188 }
189
190 /**
191 * Return required time for the appointment in seconds by summing service duration, service time before and after
192 * and each passed extra.
193 *
194 * @param Service $service
195 * @param array $selectedExtras
196 *
197 * @return mixed
198 * @throws InvalidArgumentException
199 */
200 public function getAppointmentRequiredTime($service, $selectedExtras)
201 {
202 $requiredTime =
203 $service->getTimeBefore()->getValue() +
204 $service->getDuration()->getValue() +
205 $service->getTimeAfter()->getValue();
206
207 $extraIds = array_column($selectedExtras, 'id');
208
209 /** @var Extra $extra */
210 foreach ($service->getExtras()->getItems() as $extra) {
211 if (in_array($extra->getId()->getValue(), $extraIds, false)) {
212 if (!$extra->getDuration()) {
213 $extra->setDuration(new Duration(0));
214 }
215
216 $requiredTime += ($extra->getDuration()->getValue() *
217 array_column($selectedExtras, 'quantity', 'id')[$extra->getId()->getValue()]);
218 }
219 }
220
221 return $requiredTime;
222 }
223 }
224