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 / Infrastructure / Repository / Bookable / Service / PackageRepository.php
ameliabooking / src / Infrastructure / Repository / Bookable / Service Last commit date
CategoryRepository.php 1 year ago ExtraRepository.php 1 year ago PackageCustomerRepository.php 1 year ago PackageCustomerServiceRepository.php 1 year ago PackageRepository.php 1 year ago PackageServiceLocationRepository.php 1 year ago PackageServiceProviderRepository.php 1 year ago PackageServiceRepository.php 1 year ago ProviderServiceRepository.php 1 year ago ResourceEntitiesRepository.php 1 year ago ResourceRepository.php 1 year ago ServiceRepository.php 1 year ago
PackageRepository.php
529 lines
1 <?php
2
3 /**
4 * @copyright © TMS-Plugins. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace AmeliaBooking\Infrastructure\Repository\Bookable\Service;
9
10 use AmeliaBooking\Domain\Collection\Collection;
11 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
12 use AmeliaBooking\Domain\Entity\Bookable\Service\Package;
13 use AmeliaBooking\Domain\Factory\Bookable\Service\PackageFactory;
14 use AmeliaBooking\Infrastructure\Connection;
15 use AmeliaBooking\Infrastructure\Repository\AbstractRepository;
16 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
17 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\PackagesServicesLocationsTable;
18 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\PackagesServicesProvidersTable;
19 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\PackagesServicesTable;
20 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\ServicesTable;
21 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Gallery\GalleriesTable;
22 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Location\LocationsTable;
23 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\UsersTable;
24
25 /**
26 * Class PackageRepository
27 *
28 * @package AmeliaBooking\Infrastructure\Repository\Service
29 */
30 class PackageRepository extends AbstractRepository
31 {
32 public const FACTORY = PackageFactory::class;
33
34 /**
35 * @param Connection $connection
36 * @param string $table
37 */
38 public function __construct(
39 Connection $connection,
40 $table
41 ) {
42 parent::__construct($connection, $table);
43 }
44
45 /**
46 * @param Package $entity
47 *
48 * @return bool
49 * @throws QueryExecutionException
50 */
51 public function add($entity)
52 {
53 $data = $entity->toArray();
54
55 $params = [
56 ':name' => $data['name'],
57 ':description' => $data['description'],
58 ':color' => $data['color'],
59 ':price' => $data['price'],
60 ':status' => $data['status'],
61 ':pictureFullPath' => $data['pictureFullPath'],
62 ':pictureThumbPath' => $data['pictureThumbPath'],
63 ':position' => $data['position'],
64 ':calculatedPrice' => $data['calculatedPrice'] ? 1 : 0,
65 ':discount' => $data['discount'],
66 ':settings' => $data['settings'],
67 ':endDate' => $data['endDate'],
68 ':durationCount' => $data['durationCount'],
69 ':durationType' => $data['durationType'],
70 ':translations' => $data['translations'],
71 ':deposit' => $data['deposit'],
72 ':depositPayment' => $data['depositPayment'],
73 ':fullPayment' => $data['fullPayment'] ? 1 : 0,
74 ':sharedCapacity' => $data['sharedCapacity'] ? 1 : 0,
75 ':quantity' => $data['quantity'],
76 ':limitPerCustomer' => $data['limitPerCustomer']
77 ];
78
79 try {
80 $statement = $this->connection->prepare(
81 "INSERT INTO
82 {$this->table}
83 (
84 `name`,
85 `description`,
86 `color`,
87 `price`,
88 `status`,
89 `pictureFullPath`,
90 `pictureThumbPath`,
91 `calculatedPrice`,
92 `discount`,
93 `position`,
94 `settings`,
95 `endDate`,
96 `durationCount`,
97 `durationType`,
98 `translations`,
99 `deposit`,
100 `depositPayment`,
101 `fullPayment`,
102 `sharedCapacity`,
103 `quantity`,
104 `limitPerCustomer`
105 ) VALUES (
106 :name,
107 :description,
108 :color,
109 :price,
110 :status,
111 :pictureFullPath,
112 :pictureThumbPath,
113 :calculatedPrice,
114 :discount,
115 :position,
116 :settings,
117 :endDate,
118 :durationCount,
119 :durationType,
120 :translations,
121 :deposit,
122 :depositPayment,
123 :fullPayment,
124 :sharedCapacity,
125 :quantity,
126 :limitPerCustomer
127 )"
128 );
129
130 $result = $statement->execute($params);
131
132 if (!$result) {
133 throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
134 }
135
136 return $this->connection->lastInsertId();
137 } catch (\Exception $e) {
138 throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e);
139 }
140 }
141
142 /**
143 * @param int $packageId
144 * @param Package $entity
145 *
146 * @throws QueryExecutionException
147 */
148 public function update($packageId, $entity)
149 {
150 $data = $entity->toArray();
151
152 $params = [
153 ':name' => $data['name'],
154 ':description' => $data['description'],
155 ':color' => $data['color'],
156 ':price' => $data['price'],
157 ':status' => $data['status'],
158 ':pictureFullPath' => $data['pictureFullPath'],
159 ':pictureThumbPath' => $data['pictureThumbPath'],
160 ':position' => $data['position'],
161 ':calculatedPrice' => $data['calculatedPrice'] ? 1 : 0,
162 ':discount' => $data['discount'],
163 ':settings' => $data['settings'],
164 ':endDate' => $data['endDate'],
165 ':durationCount' => $data['durationCount'],
166 ':durationType' => $data['durationType'],
167 ':translations' => $data['translations'],
168 ':deposit' => $data['deposit'],
169 ':depositPayment' => $data['depositPayment'],
170 ':fullPayment' => $data['fullPayment'] ? 1 : 0,
171 ':sharedCapacity' => $data['sharedCapacity'] ? 1 : 0,
172 ':quantity' => $data['quantity'],
173 ':limitPerCustomer' => $data['limitPerCustomer'],
174 ':id' => $packageId
175 ];
176
177
178 try {
179 $statement = $this->connection->prepare(
180 "UPDATE {$this->table}
181 SET
182 `name` = :name,
183 `description` = :description,
184 `color` = :color,
185 `price` = :price,
186 `status` = :status,
187 `pictureFullPath` = :pictureFullPath,
188 `pictureThumbPath` = :pictureThumbPath,
189 `position` = :position,
190 `calculatedPrice` = :calculatedPrice,
191 `discount` = :discount,
192 `settings` = :settings,
193 `endDate` = :endDate,
194 `durationCount` = :durationCount,
195 `durationType` = :durationType,
196 `translations` = :translations,
197 `deposit` = :deposit,
198 `depositPayment` = :depositPayment,
199 `fullPayment` = :fullPayment,
200 `sharedCapacity` = :sharedCapacity,
201 `quantity` = :quantity,
202 `limitPerCustomer` = :limitPerCustomer
203 WHERE
204 id = :id"
205 );
206
207 $result = $statement->execute($params);
208
209 if (!$result) {
210 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
211 }
212 } catch (\Exception $e) {
213 throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e);
214 }
215 }
216
217 /**
218 * @param $criteria
219 *
220 * @return Collection
221 * @throws QueryExecutionException
222 * @throws InvalidArgumentException
223 */
224 public function getByCriteria($criteria)
225 {
226 $params = [];
227
228 $where = [];
229
230 $order = 'ORDER BY p.name, ps.position ASC, ps.id ASC';
231
232 if (isset($criteria['sort'])) {
233 if ($criteria['sort'] === '') {
234 $order = 'ORDER BY p.position';
235 } else {
236 $orderColumn = strpos($criteria['sort'], 'name') !== false ? 'p.name' : 'p.price';
237
238 $orderDirection = $criteria['sort'][0] === '-' ? 'DESC' : 'ASC';
239
240 $order = "ORDER BY {$orderColumn} {$orderDirection}";
241 }
242 }
243
244 if (!empty($criteria['search'])) {
245 $params[':search'] = "%{$criteria['search']}%";
246
247 $where[] = 'p.name LIKE :search';
248 }
249
250 if (!empty($criteria['services'])) {
251 $queryServices = [];
252
253 foreach ((array)$criteria['services'] as $index => $value) {
254 $param = ':service' . $index;
255
256 $queryServices[] = $param;
257
258 $params[$param] = $value;
259 }
260
261 $where[] = 's.id IN (' . implode(', ', $queryServices) . ')';
262 }
263
264 if (!empty($criteria['packages'])) {
265 $queryPackages = [];
266
267 foreach ((array)$criteria['packages'] as $index => $value) {
268 $param = ':package' . $index;
269 $queryPackages[] = $param;
270 $params[$param] = $value;
271 }
272
273 $where[] = 'p.id IN (' . implode(', ', $queryPackages) . ')';
274 }
275
276 if (!empty($criteria['status'])) {
277 $params[':status'] = $criteria['status'];
278
279 $where[] = 's.status = :status';
280 }
281
282 $where = $where ? ' AND ' . implode(' AND ', $where) : '';
283
284 $servicesTable = ServicesTable::getTableName();
285
286 $usersTable = UsersTable::getTableName();
287
288 $locationsTable = LocationsTable::getTableName();
289
290 $packageServicesTable = PackagesServicesTable::getTableName();
291
292 $packageServicesProvidersTable = PackagesServicesProvidersTable::getTableName();
293 $packageServicesLocationsTable = PackagesServicesLocationsTable::getTableName();
294 $galleriesTable = GalleriesTable::getTableName();
295
296 try {
297 $statement = $this->connection->prepare(
298 "SELECT
299 p.id AS package_id,
300 p.name AS package_name,
301 p.description AS package_description,
302 p.color AS package_color,
303 p.price AS package_price,
304 p.status AS package_status,
305 p.pictureFullPath AS package_picture_full,
306 p.pictureThumbPath AS package_picture_thumb,
307 p.calculatedPrice AS package_calculated_price,
308 p.discount AS package_discount,
309 p.position AS package_position,
310 p.settings AS package_settings,
311 p.endDate AS package_endDate,
312 p.durationCount AS package_durationCount,
313 p.durationType AS package_durationType,
314 p.translations AS package_translations,
315 p.deposit AS package_deposit,
316 p.depositPayment AS package_depositPayment,
317 p.fullPayment AS package_fullPayment,
318 p.sharedCapacity AS package_sharedCapacity,
319 p.quantity AS package_quantity,
320 p.limitPerCustomer AS package_limitPerCustomer,
321
322 ps.id AS package_service_id,
323 ps.quantity AS package_service_quantity,
324 ps.minimumScheduled AS package_service_minimumScheduled,
325 ps.maximumScheduled AS package_service_maximumScheduled,
326 ps.allowProviderSelection AS package_service_allowProviderSelection,
327 ps.position AS package_service_position,
328
329 s.id AS service_id,
330 s.price AS service_price,
331 s.minCapacity AS service_minCapacity,
332 s.maxCapacity AS service_maxCapacity,
333 s.name AS service_name,
334 s.description AS service_description,
335 s.status AS service_status,
336 s.categoryId AS service_categoryId,
337 s.duration AS service_duration,
338 s.timeBefore AS service_timeBefore,
339 s.timeAfter AS service_timeAfter,
340 s.pictureFullPath AS service_picture_full,
341 s.pictureThumbPath AS service_picture_thumb,
342 s.translations AS service_translations,
343 s.show AS service_show,
344
345 l.id AS location_id,
346 l.name AS location_name,
347 l.address AS location_address,
348 l.phone AS location_phone,
349 l.latitude AS location_latitude,
350 l.longitude AS location_longitude,
351
352 pu.id AS provider_id,
353 pu.firstName AS provider_firstName,
354 pu.lastName AS provider_lastName,
355 pu.email AS provider_email,
356 pu.status AS provider_status,
357 pu.translations AS provider_translations,
358
359 g.id AS gallery_id,
360 g.pictureFullPath AS gallery_picture_full,
361 g.pictureThumbPath AS gallery_picture_thumb,
362 g.position AS gallery_position
363
364 FROM {$this->table} p
365 LEFT JOIN {$packageServicesTable} ps ON ps.packageId = p.id
366 LEFT JOIN {$servicesTable} s ON ps.serviceId = s.id
367 LEFT JOIN {$packageServicesProvidersTable} psp ON psp.packageServiceId = ps.id
368 LEFT JOIN {$packageServicesLocationsTable} psl ON psl.packageServiceId = ps.id
369 LEFT JOIN {$usersTable} pu ON pu.id = psp.userId
370 LEFT JOIN {$locationsTable} l ON l.id = psl.locationId
371 LEFT JOIN {$galleriesTable} g ON g.entityId = p.id AND g.entityType = 'package'
372 WHERE 1 = 1 {$where}
373 {$order}"
374 );
375
376 $statement->execute($params);
377
378 $rows = $statement->fetchAll();
379 } catch (\Exception $e) {
380 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e);
381 }
382
383 return call_user_func([static::FACTORY, 'createCollection'], $rows);
384 }
385
386 /**
387 * @param $id
388 *
389 * @return Collection
390 * @throws QueryExecutionException
391 * @throws InvalidArgumentException
392 */
393 public function getById($id)
394 {
395 $params[':id'] = $id;
396
397 $servicesTable = ServicesTable::getTableName();
398
399 $usersTable = UsersTable::getTableName();
400
401 $locationsTable = LocationsTable::getTableName();
402
403 $packageServicesTable = PackagesServicesTable::getTableName();
404
405 $packageServicesProvidersTable = PackagesServicesProvidersTable::getTableName();
406
407 $packageServicesLocationsTable = PackagesServicesLocationsTable::getTableName();
408
409 $galleriesTable = GalleriesTable::getTableName();
410
411 try {
412 $statement = $this->connection->prepare(
413 "SELECT
414 p.id AS package_id,
415 p.name AS package_name,
416 p.description AS package_description,
417 p.color AS package_color,
418 p.price AS package_price,
419 p.status AS package_status,
420 p.pictureFullPath AS package_picture_full,
421 p.pictureThumbPath AS package_picture_thumb,
422 p.calculatedPrice AS package_calculated_price,
423 p.discount AS package_discount,
424 p.position AS package_position,
425 p.settings AS package_settings,
426 p.endDate AS package_endDate,
427 p.durationCount AS package_durationCount,
428 p.durationType AS package_durationType,
429 p.translations AS package_translations,
430 p.deposit AS package_deposit,
431 p.depositPayment AS package_depositPayment,
432 p.fullPayment AS package_fullPayment,
433 p.sharedCapacity AS package_sharedCapacity,
434 p.quantity AS package_quantity,
435 p.limitPerCustomer AS package_limitPerCustomer,
436
437 ps.id AS package_service_id,
438 ps.quantity AS package_service_quantity,
439 ps.minimumScheduled AS package_service_minimumScheduled,
440 ps.maximumScheduled AS package_service_maximumScheduled,
441 ps.allowProviderSelection AS package_service_allowProviderSelection,
442 ps.position AS package_service_position,
443
444 s.id AS service_id,
445 s.price AS service_price,
446 s.minCapacity AS service_minCapacity,
447 s.maxCapacity AS service_maxCapacity,
448 s.name AS service_name,
449 s.status AS service_status,
450 s.categoryId AS service_categoryId,
451 s.duration AS service_duration,
452 s.timeBefore AS service_timeBefore,
453 s.timeAfter AS service_timeAfter,
454 s.pictureFullPath AS service_picture_full,
455 s.pictureThumbPath AS service_picture_thumb,
456 s.show AS service_show,
457
458 l.id AS location_id,
459 l.name AS location_name,
460 l.address AS location_address,
461 l.phone AS location_phone,
462 l.latitude AS location_latitude,
463 l.longitude AS location_longitude,
464
465 pu.id AS provider_id,
466 pu.firstName AS provider_firstName,
467 pu.lastName AS provider_lastName,
468 pu.email AS provider_email,
469 pu.translations AS provider_translations,
470 pu.stripeConnect AS provider_stripeConnect,
471
472 g.id AS gallery_id,
473 g.pictureFullPath AS gallery_picture_full,
474 g.pictureThumbPath AS gallery_picture_thumb,
475 g.position AS gallery_position
476
477 FROM {$this->table} p
478 LEFT JOIN {$packageServicesTable} ps ON ps.packageId = p.id
479 LEFT JOIN {$servicesTable} s ON ps.serviceId = s.id
480 LEFT JOIN {$packageServicesProvidersTable} psp ON psp.packageServiceId = ps.id
481 LEFT JOIN {$packageServicesLocationsTable} psl ON psl.packageServiceId = ps.id
482 LEFT JOIN {$usersTable} pu ON pu.id = psp.userId
483 LEFT JOIN {$locationsTable} l ON l.id = psl.locationId
484 LEFT JOIN {$galleriesTable} g ON g.entityId = p.id AND g.entityType = 'package'
485 WHERE p.id = :id"
486 );
487
488 $statement->execute($params);
489
490 $rows = $statement->fetchAll();
491 } catch (\Exception $e) {
492 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e);
493 }
494
495 return call_user_func([static::FACTORY, 'createCollection'], $rows)->getItem($id);
496 }
497
498 /**
499 * @param $serviceId
500 * @param $status
501 *
502 * @throws QueryExecutionException
503 */
504 public function updateStatusById($serviceId, $status)
505 {
506 $params = [
507 ':id' => $serviceId,
508 ':status' => $status
509 ];
510
511 try {
512 $statement = $this->connection->prepare(
513 "UPDATE {$this->table}
514 SET
515 `status` = :status
516 WHERE id = :id"
517 );
518
519 $res = $statement->execute($params);
520
521 if (!$res) {
522 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
523 }
524 } catch (\Exception $e) {
525 throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e);
526 }
527 }
528 }
529