PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.5
Booking for Appointments and Events Calendar – Amelia v1.2.5
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 / Coupon / CouponRepository.php
ameliabooking / src / Infrastructure / Repository / Coupon Last commit date
CouponEventRepository.php 1 year ago CouponPackageRepository.php 1 year ago CouponRepository.php 1 year ago CouponServiceRepository.php 1 year ago
CouponRepository.php
684 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['services'])) {
267 $queryServices = [];
268
269 foreach ((array)$criteria['services'] as $index => $value) {
270 $param = ':service' . $index;
271 $queryServices[] = $param;
272 $params[$param] = $value;
273 }
274
275 $where[] = "c.id IN (
276 SELECT couponId FROM {$this->couponToServicesTable}
277 WHERE serviceId IN (" . implode(', ', $queryServices) . ')
278 )';
279 }
280
281 if (!empty($criteria['events'])) {
282 $queryEvents = [];
283
284 foreach ((array)$criteria['events'] as $index => $value) {
285 $param = ':event' . $index;
286 $queryEvents[] = $param;
287 $params[$param] = $value;
288 }
289
290 $where[] = "c.id IN (
291 SELECT couponId FROM {$this->couponToEventsTable}
292 WHERE eventId IN (" . implode(', ', $queryEvents) . ')
293 )';
294 }
295
296 if (!empty($criteria['packages'])) {
297 $queryPackages = [];
298
299 foreach ((array)$criteria['packages'] as $index => $value) {
300 $param = ':package' . $index;
301 $queryPackages[] = $param;
302 $params[$param] = $value;
303 }
304
305 $where[] = "c.id IN (
306 SELECT couponId FROM {$this->couponToPackagesTable}
307 WHERE packageId IN (" . implode(', ', $queryPackages) . ')
308 )';
309 }
310
311
312 $where = $where ? ' WHERE ' . implode(' AND ', $where) : '';
313
314 $limit = $this->getLimit(
315 !empty($criteria['page']) ? (int)$criteria['page'] : 0,
316 (int)$itemsPerPage
317 );
318
319 $statement = $this->connection->prepare(
320 "SELECT
321 c.id AS coupon_id,
322 c.code AS coupon_code,
323 c.discount AS coupon_discount,
324 c.deduction AS coupon_deduction,
325 c.limit AS coupon_limit,
326 c.customerLimit AS coupon_customerLimit,
327 c.notificationInterval AS coupon_notificationInterval,
328 c.notificationRecurring AS coupon_notificationRecurring,
329 c.status AS coupon_status,
330 c.expirationDate AS coupon_expirationDate
331 FROM {$this->table} c
332 {$where}
333 {$limit}"
334 );
335
336 $statement->execute($params);
337
338 $rows = $statement->fetchAll();
339 } catch (\Exception $e) {
340 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
341 }
342
343 return call_user_func([static::FACTORY, 'createCollection'], $rows);
344 }
345
346 /**
347 * @param array $criteria
348 *
349 * @return mixed
350 * @throws QueryExecutionException
351 */
352 public function getCount($criteria)
353 {
354 try {
355 $params = [];
356
357 $where = [];
358
359 if (!empty($criteria['search'])) {
360 $params[':search'] = "%{$criteria['search']}%";
361
362 $where[] = 'c.code LIKE :search';
363 }
364
365 if (!empty($criteria['services'])) {
366 $queryServices = [];
367
368 foreach ((array)$criteria['services'] as $index => $value) {
369 $param = ':service' . $index;
370 $queryServices[] = $param;
371 $params[$param] = $value;
372 }
373
374 $where[] = "c.id IN (SELECT couponId FROM {$this->couponToServicesTable}
375 WHERE serviceId IN (" . implode(', ', $queryServices) . '))';
376 }
377
378 $where = $where ? ' WHERE ' . implode(' AND ', $where) : '';
379
380 $statement = $this->connection->prepare(
381 "SELECT COUNT(*) AS count
382 FROM {$this->table} c
383 $where"
384 );
385
386 $statement->execute($params);
387
388 $row = $statement->fetch()['count'];
389 } catch (\Exception $e) {
390 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
391 }
392
393 return $row;
394 }
395
396 /**
397 * @param int $id
398 * @param string $status
399 *
400 * @return mixed
401 * @throws QueryExecutionException
402 */
403 public function updateStatusById($id, $status)
404 {
405 $params = [
406 ':id' => $id,
407 ':status' => $status
408 ];
409
410 try {
411 $statement = $this->connection->prepare(
412 "UPDATE {$this->table}
413 SET
414 `status` = :status
415 WHERE id = :id"
416 );
417
418 $res = $statement->execute($params);
419
420 if (!$res) {
421 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
422 }
423
424 return $res;
425 } catch (\Exception $e) {
426 throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e);
427 }
428 }
429
430 /**
431 * @param array $criteria
432 *
433 * @return Collection
434 * @throws QueryExecutionException
435 */
436 public function getAllByCriteria($criteria)
437 {
438 try {
439 $params = [];
440
441 $where = [];
442
443 if (!empty($criteria['code'])) {
444 $where[] = $criteria['couponsCaseInsensitive'] ? 'LOWER(c.code) = LOWER(:code)' : 'c.code = :code';
445
446 $params[':code'] = $criteria['code'];
447 }
448
449 if (!empty($criteria['couponIds'])) {
450 $couponIdsParams = [];
451
452 foreach ((array)$criteria['couponIds'] as $key => $id) {
453 $couponIdsParams[":id$key"] = $id;
454 }
455
456 if ($couponIdsParams) {
457 $where[] = '(c.id IN ( ' . implode(', ', array_keys($couponIdsParams)) . '))';
458
459 $params = array_merge($params, $couponIdsParams);
460 }
461 }
462
463 $entitiesFields = '';
464
465 $entitiesJoin = '';
466
467 if (!empty($criteria['entityType']) && $criteria['entityType'] === Entities::SERVICE) {
468 $entitiesFields = '
469 s.id AS service_id,
470 s.price AS service_price,
471 s.minCapacity AS service_minCapacity,
472 s.maxCapacity AS service_maxCapacity,
473 s.name AS service_name,
474 s.description AS service_description,
475 s.color AS service_color,
476 s.status AS service_status,
477 s.categoryId AS service_categoryId,
478 s.duration AS service_duration,
479 ';
480
481 $entitiesJoin = "
482 LEFT JOIN {$this->couponToServicesTable} cs ON cs.couponId = c.id
483 LEFT JOIN {$this->servicesTable} s ON cs.serviceId = s.id
484 ";
485
486 if (!empty($criteria['entityIds'])) {
487 $queryIds = [];
488
489 foreach ($criteria['entityIds'] as $index => $value) {
490 $param = ':serviceId' . $index;
491
492 $queryIds[] = $param;
493
494 $params[$param] = $value;
495 }
496
497 $where[] = '(cs.serviceId IN (' . implode(', ', $queryIds) . '))';
498 }
499 } else if (!empty($criteria['entityType']) && $criteria['entityType'] === Entities::EVENT) {
500 $entitiesFields = '
501 e.id AS event_id,
502 e.price AS event_price,
503 e.name AS event_name,
504 ';
505
506 $entitiesJoin = "
507 LEFT JOIN {$this->couponToEventsTable} ce ON ce.couponId = c.id
508 LEFT JOIN {$this->eventsTable} e ON ce.eventId = e.id
509 ";
510
511 if (!empty($criteria['entityIds'])) {
512 $queryIds = [];
513
514 foreach ($criteria['entityIds'] as $index => $value) {
515 $param = ':eventId' . $index;
516
517 $queryIds[] = $param;
518
519 $params[$param] = $value;
520 }
521
522 $where[] = '(ce.eventId IN (' . implode(', ', $queryIds) . '))';
523 }
524 } else if (!empty($criteria['entityType']) && $criteria['entityType'] === Entities::PACKAGE) {
525 $entitiesFields = '
526 p.id AS package_id,
527 p.price AS package_price,
528 p.name AS package_name,
529 ';
530
531 $entitiesJoin = "
532 LEFT JOIN {$this->couponToPackagesTable} cp ON cp.couponId = c.id
533 LEFT JOIN {$this->packagesTable} p ON cp.packageId = p.id
534 ";
535
536 if (!empty($criteria['entityIds'])) {
537 $queryIds = [];
538
539 foreach ($criteria['entityIds'] as $index => $value) {
540 $param = ':packageId' . $index;
541
542 $queryIds[] = $param;
543
544 $params[$param] = $value;
545 }
546
547 $where[] = '(cp.packageId IN (' . implode(', ', $queryIds) . '))';
548 }
549 }
550
551 $where = $where ? 'WHERE ' . implode(' AND ', $where) : '';
552
553 $statement = $this->connection->prepare(
554 "SELECT
555 c.id AS coupon_id,
556 c.code AS coupon_code,
557 c.discount AS coupon_discount,
558 c.deduction AS coupon_deduction,
559 c.limit AS coupon_limit,
560 c.customerLimit AS coupon_customerLimit,
561 c.notificationInterval AS coupon_notificationInterval,
562 c.notificationRecurring AS coupon_notificationRecurring,
563 c.status AS coupon_status,
564 c.expirationDate AS coupon_expirationDate,
565
566 {$entitiesFields}
567
568 cb.id AS booking_id
569 FROM {$this->table} c
570 LEFT JOIN {$this->bookingsTable} cb ON cb.couponId = c.id
571 {$entitiesJoin}
572 $where"
573 );
574
575 $statement->execute($params);
576
577 $rows = $statement->fetchAll();
578 } catch (\Exception $e) {
579 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e);
580 }
581
582 return call_user_func([static::FACTORY, 'createCollection'], $rows);
583 }
584
585 /**
586 * @param array $couponIds
587 *
588 * @return array
589 *
590 * @throws QueryExecutionException
591 */
592 public function getCouponsServicesIds($couponIds)
593 {
594 $params = [];
595 $where = '';
596
597 if ($couponIds) {
598 foreach ($couponIds as $key => $couponId) {
599 $params[":id$key"] = $couponId;
600 }
601
602 $where = 'WHERE couponId IN (' . implode(', ', array_keys($params)) . ')';
603 }
604
605 try {
606 $statement = $this->connection->prepare(
607 "SELECT serviceId, couponId FROM {$this->couponToServicesTable} $where GROUP BY serviceId, couponId"
608 );
609
610 $statement->execute($params);
611
612 return $statement->fetchAll();
613 } catch (\Exception $e) {
614 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
615 }
616 }
617
618 /**
619 * @param array $couponIds
620 *
621 * @return array
622 *
623 * @throws QueryExecutionException
624 */
625 public function getCouponsEventsIds($couponIds)
626 {
627 $params = [];
628 $where = '';
629
630 if ($couponIds) {
631 foreach ($couponIds as $key => $couponId) {
632 $params[":id$key"] = $couponId;
633 }
634
635 $where = 'WHERE couponId IN (' . implode(', ', array_keys($params)) . ')';
636 }
637
638 try {
639 $statement = $this->connection->prepare(
640 "SELECT eventId, couponId FROM {$this->couponToEventsTable} $where GROUP BY eventId, couponId"
641 );
642
643 $statement->execute($params);
644
645 return $statement->fetchAll();
646 } catch (\Exception $e) {
647 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
648 }
649 }
650
651 /**
652 * @param array $couponIds
653 *
654 * @return array
655 *
656 * @throws QueryExecutionException
657 */
658 public function getCouponsPackagesIds($couponIds)
659 {
660 $params = [];
661 $where = '';
662
663 if ($couponIds) {
664 foreach ($couponIds as $key => $couponId) {
665 $params[":id$key"] = $couponId;
666 }
667
668 $where = 'WHERE couponId IN (' . implode(', ', array_keys($params)) . ')';
669 }
670
671 try {
672 $statement = $this->connection->prepare(
673 "SELECT packageId, couponId FROM {$this->couponToPackagesTable} $where GROUP BY packageId, couponId"
674 );
675
676 $statement->execute($params);
677
678 return $statement->fetchAll();
679 } catch (\Exception $e) {
680 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
681 }
682 }
683 }
684