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 / Infrastructure / Repository / Coupon / CouponRepository.php
ameliabooking / src / Infrastructure / Repository / Coupon Last commit date
CouponEventRepository.php 6 years ago CouponPackageRepository.php 3 years ago CouponRepository.php 1 year ago CouponServiceRepository.php 6 years ago
CouponRepository.php
696 lines
1 <?php
2 /**
3 * @copyright © TMS-Plugins. All rights reserved.
4 * @licence See LICENCE.md for license details.
5 */
6
7 namespace AmeliaBooking\Infrastructure\Repository\Coupon;
8
9 use AmeliaBooking\Domain\Collection\Collection;
10 use AmeliaBooking\Domain\Entity\Coupon\Coupon;
11 use AmeliaBooking\Domain\Entity\Entities;
12 use AmeliaBooking\Domain\Factory\Coupon\CouponFactory;
13 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
14 use AmeliaBooking\Infrastructure\Connection;
15 use AmeliaBooking\Infrastructure\Repository\AbstractRepository;
16 use AmeliaBooking\Domain\Repository\Coupon\CouponRepositoryInterface;
17 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
18
19 /**
20 * Class CouponRepository
21 *
22 * @package AmeliaBooking\Infrastructure\Repository\Coupon
23 */
24 class CouponRepository extends AbstractRepository implements CouponRepositoryInterface
25 {
26
27 const FACTORY = CouponFactory::class;
28
29 /** @var string */
30 protected $servicesTable;
31
32 /** @var string */
33 protected $couponToServicesTable;
34
35 /** @var string */
36 protected $eventsTable;
37
38 /** @var string */
39 protected $couponToEventsTable;
40
41 /** @var string */
42 protected $packagesTable;
43
44 /** @var string */
45 protected $couponToPackagesTable;
46
47 /** @var string */
48 protected $bookingsTable;
49
50 /**
51 * @param Connection $connection
52 * @param string $table
53 * @param string $servicesTable
54 * @param string $couponToServicesTable
55 * @param string $eventsTable
56 * @param string $couponToEventsTable
57 * @param string $packagesTable
58 * @param string $couponToPackagesTable
59 * @param string $bookingsTable
60 */
61 public function __construct(
62 Connection $connection,
63 $table,
64 $servicesTable,
65 $couponToServicesTable,
66 $eventsTable,
67 $couponToEventsTable,
68 $packagesTable,
69 $couponToPackagesTable,
70 $bookingsTable
71 ) {
72 parent::__construct($connection, $table);
73
74 $this->servicesTable = $servicesTable;
75 $this->couponToServicesTable = $couponToServicesTable;
76 $this->eventsTable = $eventsTable;
77 $this->couponToEventsTable = $couponToEventsTable;
78 $this->packagesTable = $packagesTable;
79 $this->couponToPackagesTable = $couponToPackagesTable;
80 $this->bookingsTable = $bookingsTable;
81 }
82
83 /**
84 * @param Coupon $entity
85 *
86 * @return bool
87 * @throws QueryExecutionException
88 */
89 public function add($entity)
90 {
91 $data = $entity->toArray();
92
93 $params = [
94 ':code' => $data['code'],
95 ':discount' => $data['discount'],
96 ':deduction' => $data['deduction'],
97 ':limit' => (int)$data['limit'],
98 ':customerLimit' => (int)$data['customerLimit'],
99 ':status' => $data['status'],
100 ':notificationInterval' => $data['notificationInterval'],
101 ':notificationRecurring' => $data['notificationRecurring'] ? 1 : 0,
102 ':expirationDate' => $data['expirationDate']
103 ];
104
105 try {
106 $statement = $this->connection->prepare(
107 "INSERT INTO
108 {$this->table}
109 (
110 `code`, `discount`, `deduction`, `limit`, `customerLimit`, `status`, `notificationInterval`, `notificationRecurring`, `expirationDate`
111 ) VALUES (
112 :code, :discount, :deduction, :limit, :customerLimit, :status, :notificationInterval, :notificationRecurring, :expirationDate
113 )"
114 );
115
116
117 $response = $statement->execute($params);
118 } catch (\Exception $e) {
119 throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e);
120 }
121
122 if (!$response) {
123 throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
124 }
125
126 return $this->connection->lastInsertId();
127 }
128
129 /**
130 * @param int $id
131 * @param Coupon $entity
132 *
133 * @return bool
134 * @throws QueryExecutionException
135 */
136 public function update($id, $entity)
137 {
138 $data = $entity->toArray();
139
140 $params = [
141 ':code' => $data['code'],
142 ':discount' => $data['discount'],
143 ':deduction' => $data['deduction'],
144 ':limit' => (int)$data['limit'],
145 ':customerLimit' => (int)$data['customerLimit'],
146 ':status' => $data['status'],
147 ':notificationInterval' => $data['notificationInterval'],
148 ':notificationRecurring' => $data['notificationRecurring'] ? 1 : 0,
149 ':id' => $id,
150 ':expirationDate' => $data['expirationDate']
151 ];
152
153 try {
154 $statement = $this->connection->prepare(
155 "UPDATE {$this->table}
156 SET
157 `code` = :code,
158 `discount` = :discount,
159 `deduction` = :deduction,
160 `limit` = :limit,
161 `customerLimit` = :customerLimit,
162 `status` = :status,
163 `notificationInterval` = :notificationInterval,
164 `notificationRecurring` = :notificationRecurring,
165 `expirationDate` = :expirationDate
166 WHERE
167 id = :id"
168 );
169
170 $response = $statement->execute($params);
171 } catch (\Exception $e) {
172 throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . $e->getMessage());
173 }
174
175 if (!$response) {
176 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
177 }
178
179 return $response;
180 }
181
182 /**
183 * @param int $id
184 *
185 * @return Coupon
186 * @throws QueryExecutionException
187 * @throws NotFoundException
188 */
189 public function getById($id)
190 {
191 try {
192 $statement = $this->connection->prepare(
193 "SELECT
194 c.id AS coupon_id,
195 c.code AS coupon_code,
196 c.discount AS coupon_discount,
197 c.deduction AS coupon_deduction,
198 c.limit AS coupon_limit,
199 c.customerLimit AS coupon_customerLimit,
200 c.notificationInterval AS coupon_notificationInterval,
201 c.notificationRecurring AS coupon_notificationRecurring,
202 c.status AS coupon_status,
203 c.expirationDate AS coupon_expirationDate,
204 s.id AS service_id,
205 s.price AS service_price,
206 s.minCapacity AS service_minCapacity,
207 s.maxCapacity AS service_maxCapacity,
208 s.name AS service_name,
209 s.description AS service_description,
210 s.color AS service_color,
211 s.status AS service_status,
212 s.categoryId AS service_categoryId,
213 s.duration AS service_duration,
214 e.id AS event_id,
215 e.price AS event_price,
216 e.name AS event_name,
217 p.id AS package_id,
218 p.price AS package_price,
219 p.name AS package_name
220 FROM {$this->table} c
221 LEFT JOIN {$this->couponToServicesTable} cs ON cs.couponId = c.id
222 LEFT JOIN {$this->couponToEventsTable} ce ON ce.couponId = c.id
223 LEFT JOIN {$this->couponToPackagesTable} cp ON cp.couponId = c.id
224 LEFT JOIN {$this->servicesTable} s ON cs.serviceId = s.id
225 LEFT JOIN {$this->eventsTable} e ON ce.eventId = e.id
226 LEFT JOIN {$this->packagesTable} p ON cp.packageId = p.id
227 WHERE c.id = :couponId"
228 );
229
230 $statement->bindParam(':couponId', $id);
231
232 $statement->execute();
233
234 $rows = $statement->fetchAll();
235 } catch (\Exception $e) {
236 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e);
237 }
238
239 if (!$rows) {
240 throw new NotFoundException('Data not found in ' . __CLASS__);
241 }
242
243 return call_user_func([static::FACTORY, 'createCollection'], $rows)->getItem($id);
244 }
245
246 /**
247 * @param array $criteria
248 * @param int $itemsPerPage
249 *
250 * @return Collection
251 * @throws QueryExecutionException
252 */
253 public function getFiltered($criteria, $itemsPerPage)
254 {
255 try {
256 $params = [];
257
258 $where = [];
259
260 if (!empty($criteria['search'])) {
261 $params[':search'] = "%{$criteria['search']}%";
262
263 $where[] = 'UPPER(c.code) LIKE UPPER(:search)';
264 }
265
266 if (!empty($criteria['ids'])) {
267 $queryIds = [];
268
269 foreach ((array)$criteria['ids'] as $index => $value) {
270 $param = ':id' . $index;
271 $queryIds[] = $param;
272 $params[$param] = $value;
273 }
274
275 $where[] = "c.id IN (" . implode(', ', $queryIds) . ')';
276 }
277
278 if (!empty($criteria['services'])) {
279 $queryServices = [];
280
281 foreach ((array)$criteria['services'] as $index => $value) {
282 $param = ':service' . $index;
283 $queryServices[] = $param;
284 $params[$param] = $value;
285 }
286
287 $where[] = "c.id IN (
288 SELECT couponId FROM {$this->couponToServicesTable}
289 WHERE serviceId IN (" . implode(', ', $queryServices) . ')
290 )';
291 }
292
293 if (!empty($criteria['events'])) {
294 $queryEvents = [];
295
296 foreach ((array)$criteria['events'] as $index => $value) {
297 $param = ':event' . $index;
298 $queryEvents[] = $param;
299 $params[$param] = $value;
300 }
301
302 $where[] = "c.id IN (
303 SELECT couponId FROM {$this->couponToEventsTable}
304 WHERE eventId IN (" . implode(', ', $queryEvents) . ')
305 )';
306 }
307
308 if (!empty($criteria['packages'])) {
309 $queryPackages = [];
310
311 foreach ((array)$criteria['packages'] as $index => $value) {
312 $param = ':package' . $index;
313 $queryPackages[] = $param;
314 $params[$param] = $value;
315 }
316
317 $where[] = "c.id IN (
318 SELECT couponId FROM {$this->couponToPackagesTable}
319 WHERE packageId IN (" . implode(', ', $queryPackages) . ')
320 )';
321 }
322
323
324 $where = $where ? ' WHERE ' . implode(' AND ', $where) : '';
325
326 $limit = $this->getLimit(
327 !empty($criteria['page']) ? (int)$criteria['page'] : 0,
328 (int)$itemsPerPage
329 );
330
331 $statement = $this->connection->prepare(
332 "SELECT
333 c.id AS coupon_id,
334 c.code AS coupon_code,
335 c.discount AS coupon_discount,
336 c.deduction AS coupon_deduction,
337 c.limit AS coupon_limit,
338 c.customerLimit AS coupon_customerLimit,
339 c.notificationInterval AS coupon_notificationInterval,
340 c.notificationRecurring AS coupon_notificationRecurring,
341 c.status AS coupon_status,
342 c.expirationDate AS coupon_expirationDate
343 FROM {$this->table} c
344 {$where}
345 {$limit}"
346 );
347
348 $statement->execute($params);
349
350 $rows = $statement->fetchAll();
351 } catch (\Exception $e) {
352 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
353 }
354
355 return call_user_func([static::FACTORY, 'createCollection'], $rows);
356 }
357
358 /**
359 * @param array $criteria
360 *
361 * @return mixed
362 * @throws QueryExecutionException
363 */
364 public function getCount($criteria)
365 {
366 try {
367 $params = [];
368
369 $where = [];
370
371 if (!empty($criteria['search'])) {
372 $params[':search'] = "%{$criteria['search']}%";
373
374 $where[] = 'c.code LIKE :search';
375 }
376
377 if (!empty($criteria['services'])) {
378 $queryServices = [];
379
380 foreach ((array)$criteria['services'] as $index => $value) {
381 $param = ':service' . $index;
382 $queryServices[] = $param;
383 $params[$param] = $value;
384 }
385
386 $where[] = "c.id IN (SELECT couponId FROM {$this->couponToServicesTable}
387 WHERE serviceId IN (" . implode(', ', $queryServices) . '))';
388 }
389
390 $where = $where ? ' WHERE ' . implode(' AND ', $where) : '';
391
392 $statement = $this->connection->prepare(
393 "SELECT COUNT(*) AS count
394 FROM {$this->table} c
395 $where"
396 );
397
398 $statement->execute($params);
399
400 $row = $statement->fetch()['count'];
401 } catch (\Exception $e) {
402 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
403 }
404
405 return $row;
406 }
407
408 /**
409 * @param int $id
410 * @param string $status
411 *
412 * @return mixed
413 * @throws QueryExecutionException
414 */
415 public function updateStatusById($id, $status)
416 {
417 $params = [
418 ':id' => $id,
419 ':status' => $status
420 ];
421
422 try {
423 $statement = $this->connection->prepare(
424 "UPDATE {$this->table}
425 SET
426 `status` = :status
427 WHERE id = :id"
428 );
429
430 $res = $statement->execute($params);
431
432 if (!$res) {
433 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
434 }
435
436 return $res;
437 } catch (\Exception $e) {
438 throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e);
439 }
440 }
441
442 /**
443 * @param array $criteria
444 *
445 * @return Collection
446 * @throws QueryExecutionException
447 */
448 public function getAllByCriteria($criteria)
449 {
450 try {
451 $params = [];
452
453 $where = [];
454
455 if (!empty($criteria['code'])) {
456 $where[] = $criteria['couponsCaseInsensitive'] ? 'LOWER(c.code) = LOWER(:code)' : 'c.code = :code';
457
458 $params[':code'] = $criteria['code'];
459 }
460
461 if (!empty($criteria['couponIds'])) {
462 $couponIdsParams = [];
463
464 foreach ((array)$criteria['couponIds'] as $key => $id) {
465 $couponIdsParams[":id$key"] = $id;
466 }
467
468 if ($couponIdsParams) {
469 $where[] = '(c.id IN ( ' . implode(', ', array_keys($couponIdsParams)) . '))';
470
471 $params = array_merge($params, $couponIdsParams);
472 }
473 }
474
475 $entitiesFields = '';
476
477 $entitiesJoin = '';
478
479 if (!empty($criteria['entityType']) && $criteria['entityType'] === Entities::SERVICE) {
480 $entitiesFields = '
481 s.id AS service_id,
482 s.price AS service_price,
483 s.minCapacity AS service_minCapacity,
484 s.maxCapacity AS service_maxCapacity,
485 s.name AS service_name,
486 s.description AS service_description,
487 s.color AS service_color,
488 s.status AS service_status,
489 s.categoryId AS service_categoryId,
490 s.duration AS service_duration,
491 ';
492
493 $entitiesJoin = "
494 LEFT JOIN {$this->couponToServicesTable} cs ON cs.couponId = c.id
495 LEFT JOIN {$this->servicesTable} s ON cs.serviceId = s.id
496 ";
497
498 if (!empty($criteria['entityIds'])) {
499 $queryIds = [];
500
501 foreach ($criteria['entityIds'] as $index => $value) {
502 $param = ':serviceId' . $index;
503
504 $queryIds[] = $param;
505
506 $params[$param] = $value;
507 }
508
509 $where[] = '(cs.serviceId IN (' . implode(', ', $queryIds) . '))';
510 }
511 } else if (!empty($criteria['entityType']) && $criteria['entityType'] === Entities::EVENT) {
512 $entitiesFields = '
513 e.id AS event_id,
514 e.price AS event_price,
515 e.name AS event_name,
516 ';
517
518 $entitiesJoin = "
519 LEFT JOIN {$this->couponToEventsTable} ce ON ce.couponId = c.id
520 LEFT JOIN {$this->eventsTable} e ON ce.eventId = e.id
521 ";
522
523 if (!empty($criteria['entityIds'])) {
524 $queryIds = [];
525
526 foreach ($criteria['entityIds'] as $index => $value) {
527 $param = ':eventId' . $index;
528
529 $queryIds[] = $param;
530
531 $params[$param] = $value;
532 }
533
534 $where[] = '(ce.eventId IN (' . implode(', ', $queryIds) . '))';
535 }
536 } else if (!empty($criteria['entityType']) && $criteria['entityType'] === Entities::PACKAGE) {
537 $entitiesFields = '
538 p.id AS package_id,
539 p.price AS package_price,
540 p.name AS package_name,
541 ';
542
543 $entitiesJoin = "
544 LEFT JOIN {$this->couponToPackagesTable} cp ON cp.couponId = c.id
545 LEFT JOIN {$this->packagesTable} p ON cp.packageId = p.id
546 ";
547
548 if (!empty($criteria['entityIds'])) {
549 $queryIds = [];
550
551 foreach ($criteria['entityIds'] as $index => $value) {
552 $param = ':packageId' . $index;
553
554 $queryIds[] = $param;
555
556 $params[$param] = $value;
557 }
558
559 $where[] = '(cp.packageId IN (' . implode(', ', $queryIds) . '))';
560 }
561 }
562
563 $where = $where ? 'WHERE ' . implode(' AND ', $where) : '';
564
565 $statement = $this->connection->prepare(
566 "SELECT
567 c.id AS coupon_id,
568 c.code AS coupon_code,
569 c.discount AS coupon_discount,
570 c.deduction AS coupon_deduction,
571 c.limit AS coupon_limit,
572 c.customerLimit AS coupon_customerLimit,
573 c.notificationInterval AS coupon_notificationInterval,
574 c.notificationRecurring AS coupon_notificationRecurring,
575 c.status AS coupon_status,
576 c.expirationDate AS coupon_expirationDate,
577
578 {$entitiesFields}
579
580 cb.id AS booking_id
581 FROM {$this->table} c
582 LEFT JOIN {$this->bookingsTable} cb ON cb.couponId = c.id
583 {$entitiesJoin}
584 $where"
585 );
586
587 $statement->execute($params);
588
589 $rows = $statement->fetchAll();
590 } catch (\Exception $e) {
591 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e);
592 }
593
594 return call_user_func([static::FACTORY, 'createCollection'], $rows);
595 }
596
597 /**
598 * @param array $couponIds
599 *
600 * @return array
601 *
602 * @throws QueryExecutionException
603 */
604 public function getCouponsServicesIds($couponIds)
605 {
606 $params = [];
607 $where = '';
608
609 if ($couponIds) {
610 foreach ($couponIds as $key => $couponId) {
611 $params[":id$key"] = $couponId;
612 }
613
614 $where = 'WHERE couponId IN (' . implode(', ', array_keys($params)) . ')';
615 }
616
617 try {
618 $statement = $this->connection->prepare(
619 "SELECT serviceId, couponId FROM {$this->couponToServicesTable} $where GROUP BY serviceId, couponId"
620 );
621
622 $statement->execute($params);
623
624 return $statement->fetchAll();
625 } catch (\Exception $e) {
626 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
627 }
628 }
629
630 /**
631 * @param array $couponIds
632 *
633 * @return array
634 *
635 * @throws QueryExecutionException
636 */
637 public function getCouponsEventsIds($couponIds)
638 {
639 $params = [];
640 $where = '';
641
642 if ($couponIds) {
643 foreach ($couponIds as $key => $couponId) {
644 $params[":id$key"] = $couponId;
645 }
646
647 $where = 'WHERE couponId IN (' . implode(', ', array_keys($params)) . ')';
648 }
649
650 try {
651 $statement = $this->connection->prepare(
652 "SELECT eventId, couponId FROM {$this->couponToEventsTable} $where GROUP BY eventId, couponId"
653 );
654
655 $statement->execute($params);
656
657 return $statement->fetchAll();
658 } catch (\Exception $e) {
659 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
660 }
661 }
662
663 /**
664 * @param array $couponIds
665 *
666 * @return array
667 *
668 * @throws QueryExecutionException
669 */
670 public function getCouponsPackagesIds($couponIds)
671 {
672 $params = [];
673 $where = '';
674
675 if ($couponIds) {
676 foreach ($couponIds as $key => $couponId) {
677 $params[":id$key"] = $couponId;
678 }
679
680 $where = 'WHERE couponId IN (' . implode(', ', array_keys($params)) . ')';
681 }
682
683 try {
684 $statement = $this->connection->prepare(
685 "SELECT packageId, couponId FROM {$this->couponToPackagesTable} $where GROUP BY packageId, couponId"
686 );
687
688 $statement->execute($params);
689
690 return $statement->fetchAll();
691 } catch (\Exception $e) {
692 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
693 }
694 }
695 }
696