PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.32
Booking for Appointments and Events Calendar – Amelia v1.2.32
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
238 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 if (!empty($props['locationIds'])) {
67 /** @var Collection $filteredLocations */
68 $filteredLocations = new Collection();
69 foreach ($props['locationIds'] as $locationId) {
70 if ($locations->keyExists($locationId)) {
71 $filteredLocations->addItem(
72 $locations->getItem($locationId),
73 $locationId
74 );
75 }
76 }
77 $locations = $filteredLocations;
78 }
79
80 /** @var Collection $filteredProviders */
81 $filteredProviders = new Collection();
82
83 /** @var Provider $provider */
84 foreach ($providers->getItems() as $provider) {
85 if ($provider->getServiceList()->keyExists($props['serviceId'])) {
86 if ($settings['allowAdminBookAtAnyTime']) {
87 $this->providerService->setProvidersAlwaysAvailable(
88 $providers
89 );
90 }
91
92 $this->providerService->setProviderServices(
93 $provider,
94 $services,
95 false
96 );
97
98 /** @var Service $service */
99 foreach ($provider->getServiceList()->getItems() as $service) {
100 $this->checkServiceTimes($service);
101 }
102
103 $filteredProviders->addItem($provider, $provider->getId()->getValue());
104 }
105 }
106
107 /** @var Service $service */
108 foreach ($services->getItems() as $service) {
109 $this->checkServiceTimes($service);
110 }
111
112 /** @var Collection $serviceResources */
113 $serviceResources = $slotsEntities->getResources() ? $this->resourceService->getServiceResources(
114 $slotsEntities->getResources(),
115 $props['serviceId']
116 ) : new Collection();
117
118 $this->resourceService->setNonSharedResources(
119 $serviceResources,
120 [
121 'service' => $services->keys(),
122 'location' => $locations->keys(),
123 ]
124 );
125
126 /** @var SlotsEntities $filteredSlotsEntities */
127 $filteredSlotsEntities = SlotsEntitiesFactory::create();
128
129 $filteredSlotsEntities->setServices($services);
130
131 $filteredSlotsEntities->setProviders($filteredProviders);
132
133 $filteredSlotsEntities->setLocations($locations);
134
135 $filteredSlotsEntities->setResources($serviceResources);
136
137 return $filteredSlotsEntities;
138 }
139
140 /**
141 * Add 0 as duration for service time before or time after if it is null
142 *
143 * @param Service $service
144 *
145 * @throws InvalidArgumentException
146 */
147 private function checkServiceTimes($service)
148 {
149 if (!$service->getTimeBefore()) {
150 $service->setTimeBefore(new Duration(0));
151 }
152
153 if (!$service->getTimeAfter()) {
154 $service->setTimeAfter(new Duration(0));
155 }
156 }
157
158 /**
159 * filter appointments required for slots calculation
160 *
161 * @param SlotsEntities $slotsEntities
162 * @param Collection $appointments
163 * @param array $props
164 *
165 * @return void
166 * @throws InvalidArgumentException
167 */
168 public function filterSlotsAppointments($slotsEntities, $appointments, $props)
169 {
170 /** @var Collection $services */
171 $services = $slotsEntities->getServices();
172
173 /** @var Collection $providers */
174 $providers = $slotsEntities->getProviders();
175
176 $providersIds = $providers->keys();
177
178 /** @var Appointment $appointment */
179 foreach ($appointments->getItems() as $index => $appointment) {
180 if (
181 !in_array($appointment->getProviderId()->getValue(), $providersIds) ||
182 (
183 $props['excludeAppointmentId'] && $index === $props['excludeAppointmentId']
184 )
185 ) {
186 $appointments->deleteItem($index);
187 }
188 }
189
190 /** @var Appointment $appointment */
191 foreach ($appointments->getItems() as $index => $appointment) {
192 /** @var Provider $provider */
193 $provider = $providers->getItem($appointment->getProviderId()->getValue());
194
195 /** @var Service $providerService */
196 $providerService = $provider->getServiceList()->keyExists($appointment->getServiceId()->getValue()) ?
197 $provider->getServiceList()->getItem($appointment->getServiceId()->getValue()) :
198 $services->getItem($appointment->getServiceId()->getValue());
199
200 $appointment->setService($providerService);
201 }
202 }
203
204 /**
205 * Return required time for the appointment in seconds by summing service duration, service time before and after
206 * and each passed extra.
207 *
208 * @param Service $service
209 * @param array $selectedExtras
210 *
211 * @return mixed
212 * @throws InvalidArgumentException
213 */
214 public function getAppointmentRequiredTime($service, $selectedExtras)
215 {
216 $requiredTime =
217 $service->getTimeBefore()->getValue() +
218 $service->getDuration()->getValue() +
219 $service->getTimeAfter()->getValue();
220
221 $extraIds = array_column($selectedExtras, 'id');
222
223 /** @var Extra $extra */
224 foreach ($service->getExtras()->getItems() as $extra) {
225 if (in_array($extra->getId()->getValue(), $extraIds, false)) {
226 if (!$extra->getDuration()) {
227 $extra->setDuration(new Duration(0));
228 }
229
230 $requiredTime += ($extra->getDuration()->getValue() *
231 array_column($selectedExtras, 'quantity', 'id')[$extra->getId()->getValue()]);
232 }
233 }
234
235 return $requiredTime;
236 }
237 }
238