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