PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
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 / Infrastructure / Repository / Bookable / Service / ProviderServiceRepository.php
ameliabooking / src / Infrastructure / Repository / Bookable / Service Last commit date
CategoryRepository.php 2 years ago ExtraRepository.php 5 years ago PackageCustomerRepository.php 1 year ago PackageCustomerServiceRepository.php 1 year ago PackageRepository.php 2 years ago PackageServiceLocationRepository.php 5 years ago PackageServiceProviderRepository.php 5 years ago PackageServiceRepository.php 2 years ago ProviderServiceRepository.php 1 year ago ResourceEntitiesRepository.php 2 years ago ResourceRepository.php 2 years ago ServiceRepository.php 2 years ago
ProviderServiceRepository.php
416 lines
1 <?php
2
3 namespace AmeliaBooking\Infrastructure\Repository\Bookable\Service;
4
5 use AmeliaBooking\Domain\Entity\Bookable\Service\Service;
6 use AmeliaBooking\Domain\Entity\Entities;
7 use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory;
8 use AmeliaBooking\Infrastructure\Licence;
9 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
10 use AmeliaBooking\Infrastructure\Repository\AbstractRepository;
11
12 /**
13 * Class ProviderServiceRepository
14 *
15 * @package AmeliaBooking\Infrastructure\Repository\Bookable\Service
16 */
17 class ProviderServiceRepository extends AbstractRepository
18 {
19 const FACTORY = ServiceFactory::class;
20
21 /**
22 * @param Service $entity
23 * @param int $userId
24 *
25 * @return int
26 * @throws QueryExecutionException
27 */
28 public function add($entity, $userId)
29 {
30 $data = $entity->toArray();
31
32 $params = [
33 ':userId' => $userId,
34 ':serviceId' => $data['id'],
35 ':minCapacity' => $data['minCapacity'],
36 ':maxCapacity' => $data['maxCapacity'],
37 ':price' => $data['price'],
38 ];
39
40 $additionalData = Licence\DataModifier::getProviderServiceRepositoryData($data);
41
42 $params = array_merge($params, $additionalData['values']);
43
44 try {
45 $statement = $this->connection->prepare(
46 "INSERT INTO {$this->table}
47 (
48 {$additionalData['columns']}
49 `userId`,
50 `serviceId`,
51 `minCapacity`,
52 `maxCapacity`,
53 `price`
54 )
55 VALUES
56 (
57 {$additionalData['placeholders']}
58 :userId,
59 :serviceId,
60 :minCapacity,
61 :maxCapacity,
62 :price
63 )"
64 );
65
66 $res = $statement->execute($params);
67 if (!$res) {
68 throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
69 }
70 } catch (\Exception $e) {
71 throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e);
72 }
73
74 return $this->connection->lastInsertId();
75 }
76
77 /**
78 * @param Service $entity
79 * @param int $id
80 *
81 * @return int
82 * @throws QueryExecutionException
83 */
84 public function update($entity, $id)
85 {
86 $data = $entity->toArray();
87
88 $params = [
89 ':id' => $id,
90 ':minCapacity' => $data['minCapacity'],
91 ':maxCapacity' => $data['maxCapacity'],
92 ':price' => $data['price'],
93 ];
94
95 $additionalData = Licence\DataModifier::getProviderServiceRepositoryData($data);
96
97 $params = array_merge($params, $additionalData['values']);
98
99 try {
100 $statement = $this->connection->prepare(
101 "UPDATE {$this->table}
102 SET
103 {$additionalData['columnsPlaceholders']}
104 `minCapacity` = :minCapacity,
105 `maxCapacity` = :maxCapacity,
106 `price` = :price
107 WHERE id = :id"
108 );
109
110 $res = $statement->execute($params);
111 if (!$res) {
112 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
113 }
114 } catch (\Exception $e) {
115 throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e);
116 }
117
118 return $this->connection->lastInsertId();
119 }
120
121 /**
122 * @param int $id
123 * @param string $type
124 *
125 * @return array
126 * @throws QueryExecutionException
127 */
128 public function getAllForEntity($id, $type)
129 {
130 $columnName = '';
131
132 switch ($type) {
133 case (Entities::EMPLOYEE):
134 $columnName = 'userId';
135
136 break;
137
138 case (Entities::SERVICE):
139 $columnName = 'serviceId';
140
141 break;
142 }
143
144 try {
145 $statement = $this->connection->prepare(
146 "SELECT
147 ps.id,
148 ps.userId,
149 ps.serviceId,
150 ps.minCapacity,
151 ps.maxCapacity,
152 ps.price,
153 ps.customPricing
154 FROM {$this->table} ps
155 WHERE ps.{$columnName} = :entityId"
156 );
157
158 $params = array(
159 ':entityId' => $id
160 );
161
162 $statement->execute($params);
163
164 $rows = $statement->fetchAll();
165
166 foreach ($rows as &$row) {
167 $row['id'] = (int)$row['id'];
168 $row['userId'] = (int)$row['userId'];
169 $row['serviceId'] = (int)$row['serviceId'];
170 $row['minCapacity'] = (int)$row['minCapacity'];
171 $row['maxCapacity'] = (int)$row['maxCapacity'];
172 }
173
174 return $rows;
175 } catch (\Exception $e) {
176 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e);
177 }
178 }
179
180 /**
181 *
182 * It will delete all relations for one service except ones that are sent in providers array
183 *
184 * @param array $providersIds
185 * @param int $serviceId
186 *
187 * @return bool
188 * @throws QueryExecutionException
189 */
190 public function deleteAllNotInProvidersArrayForService($providersIds, $serviceId)
191 {
192 $providers = ' ';
193
194 if (!empty($providersIds)) {
195 foreach ($providersIds as $index => $value) {
196 ++$index;
197 $providers .= ':providerId' . $index . ', ';
198 $params[':providerId' . $index] = (int)$value;
199 }
200 $providers = 'AND `userId` NOT IN (' . rtrim($providers, ', ') . ')';
201 }
202
203 $params[':serviceId'] = $serviceId;
204
205 try {
206 $statement = $this->connection->prepare(
207 "DELETE FROM {$this->table} WHERE 1 = 1 $providers AND serviceId = :serviceId"
208 );
209
210 return $statement->execute($params);
211 } catch (\Exception $e) {
212 throw new QueryExecutionException('Unable to delete data from ' . __CLASS__, $e->getCode(), $e);
213 }
214 }
215
216 /**
217 *
218 * It will delete all relations for one service except ones that are sent in providers array
219 *
220 * @param array $servicesIds
221 * @param int $providerId
222 *
223 * @return bool
224 * @throws QueryExecutionException
225 */
226 public function deleteAllNotInServicesArrayForProvider($servicesIds, $providerId)
227 {
228 $services = ' ';
229
230 if (!empty($servicesIds)) {
231 foreach ($servicesIds as $index => $value) {
232 ++$index;
233 $services .= ':serviceId' . $index . ', ';
234 $params[':serviceId' . $index] = $value;
235 }
236 $services = 'AND `serviceId` NOT IN (' . rtrim($services, ', ') . ')';
237 }
238
239 $params[':providerId'] = $providerId;
240
241 try {
242 $statement = $this->connection->prepare(
243 "DELETE FROM {$this->table} WHERE 1 = 1 $services AND userId = :providerId"
244 );
245
246 return $statement->execute($params);
247 } catch (\Exception $e) {
248 throw new QueryExecutionException('Unable to delete data from ' . __CLASS__, $e->getCode(), $e);
249 }
250 }
251
252 /**
253 * @param int $entityId
254 * @param int $entityType
255 *
256 * @return bool
257 * @throws QueryExecutionException
258 */
259 public function deleteDuplicated($entityId, $entityType)
260 {
261 $matchColumnName = '';
262
263 $entityColumnName = '';
264
265 switch ($entityType) {
266 case (Entities::EMPLOYEE):
267 $matchColumnName = 'serviceId';
268
269 $entityColumnName = 'userId';
270
271 break;
272
273 case (Entities::SERVICE):
274 $matchColumnName = 'userId';
275
276 $entityColumnName = 'serviceId';
277
278 break;
279 }
280
281 $params = [
282 ':entityId1' => $entityId,
283 ':entityId2' => $entityId,
284 ];
285
286 try {
287 $statement = $this->connection->prepare(
288 "DELETE t1 FROM {$this->table} t1, {$this->table} t2 WHERE
289 t1.{$entityColumnName} = :entityId1 AND
290 t2.{$entityColumnName} = :entityId2 AND
291 t1.id < t2.id AND
292 t1.{$matchColumnName} = t2.{$matchColumnName}"
293 );
294
295 return $statement->execute($params);
296 } catch (\Exception $e) {
297 throw new QueryExecutionException('Unable to delete data from ' . __CLASS__, $e->getCode(), $e);
298 }
299 }
300
301 /**
302 * @param Service $entity
303 * @param int $serviceId
304 *
305 * @return boolean
306 * @throws QueryExecutionException
307 */
308 public function updateServiceForAllProviders($entity, $serviceId)
309 {
310 $data = $entity->toArray();
311
312 $params = [
313 ':serviceId' => $serviceId,
314 ':minCapacity' => $data['minCapacity'],
315 ':maxCapacity' => $data['maxCapacity'],
316 ':price' => $data['price'],
317 ':customPricing' => $data['customPricing'],
318 ];
319
320 try {
321 $statement = $this->connection->prepare(
322 "UPDATE {$this->table}
323 SET `minCapacity` = :minCapacity, `maxCapacity` = :maxCapacity, `price` = :price, `customPricing` = :customPricing
324 WHERE serviceId = :serviceId"
325 );
326
327 $res = $statement->execute($params);
328 if (!$res) {
329 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
330 }
331 } catch (\Exception $e) {
332 throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e);
333 }
334
335 return true;
336 }
337
338 /**
339 * @param Service $entity
340 * @param int $serviceId
341 * @param int $providerId
342 *
343 * @return boolean
344 * @throws QueryExecutionException
345 */
346 public function updateServiceForProvider($entity, $serviceId, $providerId)
347 {
348 $data = $entity->toArray();
349
350 $params = [
351 ':serviceId' => $serviceId,
352 ':providerId' => $providerId,
353 ':minCapacity' => $data['minCapacity'],
354 ':maxCapacity' => $data['maxCapacity'],
355 ':price' => $data['price'],
356 ':customPricing' => $data['customPricing'],
357 ];
358
359 try {
360 $statement = $this->connection->prepare(
361 "UPDATE {$this->table}
362 SET `minCapacity` = :minCapacity, `maxCapacity` = :maxCapacity, `price` = :price, `customPricing` = :customPricing
363 WHERE serviceId = :serviceId AND userId = :providerId"
364 );
365
366 $res = $statement->execute($params);
367
368 if (!$res) {
369 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
370 }
371 } catch (\Exception $e) {
372 throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e);
373 }
374
375 return true;
376 }
377
378 /**
379 * @param int $providerId
380 *
381 * @return array
382 * @throws QueryExecutionException
383 */
384 public function getMandatoryServicesIdsForProvider($providerId)
385 {
386
387 try {
388 $statement = $this->connection->prepare(
389 "SELECT
390 ps.serviceId, ps.userId
391 FROM {$this->table} ps
392 GROUP BY ps.serviceId
393 HAVING COUNT(*) = 1"
394 );
395
396 $statement->execute();
397
398 $rows = $statement->fetchAll();
399
400
401 } catch (\Exception $e) {
402 throw new QueryExecutionException('Unable to find data from ' . __CLASS__, $e->getCode(), $e);
403 }
404
405 $items = [];
406
407 foreach ($rows as $row) {
408 if ($row['userId'] == $providerId) {
409 $items[] = $row['serviceId'];
410 }
411 }
412
413 return $items;
414 }
415 }
416