ameliabooking
/
src
/
Infrastructure
/
Repository
/
Booking
/
Appointment
/
CustomerBookingRepository.php
AppointmentRepository.php
8 months ago
CustomerBookingExtraRepository.php
1 year ago
CustomerBookingRepository.php
8 months ago
CustomerBookingRepository.php
933 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Booking\Appointment; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Collection\Collection; |
| 6 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 7 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 8 | use AmeliaBooking\Domain\Factory\Booking\Appointment\CustomerBookingFactory; |
| 9 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 10 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 11 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 12 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\CustomerBookingsToEventsPeriodsTable; |
| 13 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\CustomerBookingToEventsTicketsTable; |
| 14 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsPeriodsTable; |
| 15 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsProvidersTable; |
| 16 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsTable; |
| 17 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Coupon\CouponsTable; |
| 18 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Payment\PaymentsTable; |
| 19 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\UsersTable; |
| 20 | use Exception; |
| 21 | |
| 22 | /** |
| 23 | * Class CustomerBookingRepository |
| 24 | * |
| 25 | * @package AmeliaBooking\Infrastructure\Repository\Booking\Appointment |
| 26 | */ |
| 27 | class CustomerBookingRepository extends AbstractRepository |
| 28 | { |
| 29 | public const FACTORY = CustomerBookingFactory::class; |
| 30 | |
| 31 | /** |
| 32 | * @param CustomerBooking $entity |
| 33 | * |
| 34 | * @return mixed |
| 35 | * @throws QueryExecutionException |
| 36 | */ |
| 37 | public function add($entity) |
| 38 | { |
| 39 | $data = $entity->toArray(); |
| 40 | |
| 41 | $couponId = !empty($data['coupon']) ? $data['coupon']['id'] : null; |
| 42 | if (!$couponId && !empty($data['couponId'])) { |
| 43 | $couponId = $data['couponId']; |
| 44 | } |
| 45 | |
| 46 | $params = [ |
| 47 | ':appointmentId' => $data['appointmentId'], |
| 48 | ':customerId' => $data['customerId'], |
| 49 | ':status' => $data['status'], |
| 50 | ':price' => $data['price'], |
| 51 | ':tax' => !empty($data['tax']) ? json_encode($data['tax']) : null, |
| 52 | ':persons' => $data['persons'], |
| 53 | ':couponId' => $couponId, |
| 54 | ':token' => $data['token'], |
| 55 | ':customFields' => $data['customFields'] && json_decode($data['customFields']) !== false ? |
| 56 | $data['customFields'] : null, |
| 57 | ':info' => $data['info'], |
| 58 | ':aggregatedPrice' => $data['aggregatedPrice'] ? 1 : 0, |
| 59 | ':utcOffset' => $data['utcOffset'], |
| 60 | ':packageCustomerServiceId' => !empty($data['packageCustomerService']['id']) ? |
| 61 | $data['packageCustomerService']['id'] : null, |
| 62 | ':duration' => !empty($data['duration']) ? $data['duration'] : null, |
| 63 | ':created' => !empty($data['created']) ? |
| 64 | DateTimeService::getCustomDateTimeInUtc($data['created']) : DateTimeService::getNowDateTimeInUtc(), |
| 65 | ':actionsCompleted' => $data['actionsCompleted'] ? 1 : 0, |
| 66 | ':qrCodes' => $data['qrCodes'] && json_decode($data['qrCodes']) !== false ? $data['qrCodes'] : null, |
| 67 | ]; |
| 68 | |
| 69 | try { |
| 70 | $statement = $this->connection->prepare( |
| 71 | "INSERT INTO {$this->table} |
| 72 | ( |
| 73 | `appointmentId`, |
| 74 | `customerId`, |
| 75 | `status`, |
| 76 | `price`, |
| 77 | `tax`, |
| 78 | `persons`, |
| 79 | `couponId`, |
| 80 | `token`, |
| 81 | `customFields`, |
| 82 | `info`, |
| 83 | `aggregatedPrice`, |
| 84 | `utcOffset`, |
| 85 | `packageCustomerServiceId`, |
| 86 | `duration`, |
| 87 | `created`, |
| 88 | `actionsCompleted`, |
| 89 | `qrCodes` |
| 90 | ) |
| 91 | VALUES ( |
| 92 | :appointmentId, |
| 93 | :customerId, |
| 94 | :status, |
| 95 | :price, |
| 96 | :tax, |
| 97 | :persons, |
| 98 | :couponId, |
| 99 | :token, |
| 100 | :customFields, |
| 101 | :info, |
| 102 | :aggregatedPrice, |
| 103 | :utcOffset, |
| 104 | :packageCustomerServiceId, |
| 105 | :duration, |
| 106 | :created, |
| 107 | :actionsCompleted, |
| 108 | :qrCodes |
| 109 | )" |
| 110 | ); |
| 111 | |
| 112 | $res = $statement->execute($params); |
| 113 | |
| 114 | if (!$res) { |
| 115 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 116 | } |
| 117 | |
| 118 | return $this->connection->lastInsertId(); |
| 119 | } catch (Exception $e) { |
| 120 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @param int $id |
| 126 | * @param CustomerBooking $entity |
| 127 | * |
| 128 | * @return mixed |
| 129 | * @throws QueryExecutionException |
| 130 | */ |
| 131 | public function update($id, $entity) |
| 132 | { |
| 133 | $data = $entity->toArray(); |
| 134 | |
| 135 | $params = [ |
| 136 | ':id' => $id, |
| 137 | ':customerId' => $data['customerId'], |
| 138 | ':status' => $data['status'], |
| 139 | ':duration' => !empty($data['duration']) ? $data['duration'] : null, |
| 140 | ':persons' => $data['persons'], |
| 141 | ':couponId' => !empty($data['coupon']) ? $data['coupon']['id'] : null, |
| 142 | ':customFields' => $data['customFields'], |
| 143 | ':qrCodes' => $data['qrCodes'] && json_decode($data['qrCodes']) !== false ? $data['qrCodes'] : null, |
| 144 | ]; |
| 145 | |
| 146 | try { |
| 147 | $statement = $this->connection->prepare( |
| 148 | "UPDATE {$this->table} SET |
| 149 | `customerId` = :customerId, |
| 150 | `status` = :status, |
| 151 | `duration` = :duration, |
| 152 | `persons` = :persons, |
| 153 | `couponId` = :couponId, |
| 154 | `customFields` = :customFields, |
| 155 | `qrCodes` = :qrCodes |
| 156 | WHERE id = :id" |
| 157 | ); |
| 158 | |
| 159 | $res = $statement->execute($params); |
| 160 | |
| 161 | if (!$res) { |
| 162 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 163 | } |
| 164 | |
| 165 | return $res; |
| 166 | } catch (Exception $e) { |
| 167 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @param int $id |
| 173 | * @param CustomerBooking $entity |
| 174 | * |
| 175 | * @return mixed |
| 176 | * @throws QueryExecutionException |
| 177 | */ |
| 178 | public function updatePrice($id, $entity) |
| 179 | { |
| 180 | $data = $entity->toArray(); |
| 181 | |
| 182 | $params = [ |
| 183 | ':id' => $id, |
| 184 | ':price' => $data['price'], |
| 185 | ]; |
| 186 | |
| 187 | try { |
| 188 | $statement = $this->connection->prepare( |
| 189 | "UPDATE {$this->table} SET |
| 190 | `price` = :price |
| 191 | WHERE id = :id" |
| 192 | ); |
| 193 | |
| 194 | $res = $statement->execute($params); |
| 195 | |
| 196 | if (!$res) { |
| 197 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 198 | } |
| 199 | |
| 200 | return $res; |
| 201 | } catch (Exception $e) { |
| 202 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @param int $id |
| 208 | * @param CustomerBooking $entity |
| 209 | * |
| 210 | * @return bool |
| 211 | * @throws QueryExecutionException |
| 212 | */ |
| 213 | public function updateTax($id, $entity) |
| 214 | { |
| 215 | $data = $entity->toArray(); |
| 216 | |
| 217 | $params = [ |
| 218 | ':id' => $id, |
| 219 | ':tax' => !empty($data['tax']) ? (is_array($data['tax']) ? json_encode($data['tax']) : $data['tax']) : null, |
| 220 | ]; |
| 221 | |
| 222 | try { |
| 223 | $statement = $this->connection->prepare( |
| 224 | "UPDATE {$this->table} SET |
| 225 | `tax` = :tax |
| 226 | WHERE id = :id" |
| 227 | ); |
| 228 | |
| 229 | $res = $statement->execute($params); |
| 230 | |
| 231 | if (!$res) { |
| 232 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 233 | } |
| 234 | |
| 235 | return $res; |
| 236 | } catch (Exception $e) { |
| 237 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Returns token for given id |
| 243 | * |
| 244 | * @param $id |
| 245 | * |
| 246 | * @return array |
| 247 | * @throws QueryExecutionException |
| 248 | */ |
| 249 | public function getToken($id) |
| 250 | { |
| 251 | try { |
| 252 | $statement = $this->connection->prepare( |
| 253 | "SELECT cb.token |
| 254 | FROM {$this->table} cb |
| 255 | WHERE cb.id = :id" |
| 256 | ); |
| 257 | |
| 258 | $statement->execute([':id' => $id]); |
| 259 | |
| 260 | $row = $statement->fetch(); |
| 261 | } catch (Exception $e) { |
| 262 | throw new QueryExecutionException('Unable to return customer booking from' . __CLASS__, $e->getCode(), $e); |
| 263 | } |
| 264 | |
| 265 | return $row; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Returns tokens for given event id |
| 270 | * |
| 271 | * @param $id |
| 272 | * |
| 273 | * @return array |
| 274 | * @throws QueryExecutionException |
| 275 | * @throws InvalidArgumentException |
| 276 | */ |
| 277 | public function getTokensByEventId($id) |
| 278 | { |
| 279 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 280 | |
| 281 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 282 | |
| 283 | try { |
| 284 | $statement = $this->connection->prepare( |
| 285 | "SELECT |
| 286 | cb.id, cb.token |
| 287 | FROM {$this->table} cb |
| 288 | INNER JOIN {$customerBookingsEventsPeriods} cbep ON cbep.customerBookingId = cb.id |
| 289 | INNER JOIN {$eventsPeriodsTable} ep ON ep.id = cbep.eventPeriodId |
| 290 | WHERE ep.eventId = :id" |
| 291 | ); |
| 292 | |
| 293 | $statement->execute([':id' => $id]); |
| 294 | |
| 295 | $rows = $statement->fetchAll(); |
| 296 | } catch (Exception $e) { |
| 297 | throw new QueryExecutionException('Unable to return customer booking from' . __CLASS__, $e->getCode(), $e); |
| 298 | } |
| 299 | |
| 300 | return $rows; |
| 301 | } |
| 302 | |
| 303 | public function getQrTicketNumber($ticketNo) |
| 304 | { |
| 305 | try { |
| 306 | $params[':ticketNo'] = '%' . $ticketNo . '%'; |
| 307 | |
| 308 | $statement = $this->connection->prepare( |
| 309 | "SELECT |
| 310 | id, |
| 311 | qrCodes |
| 312 | FROM {$this->table} cb |
| 313 | WHERE qrCodes LIKE :ticketNo" |
| 314 | ); |
| 315 | |
| 316 | $statement->execute($params); |
| 317 | |
| 318 | $row = $statement->fetch(); |
| 319 | |
| 320 | return $row ?: null; |
| 321 | } catch (Exception $e) { |
| 322 | throw new QueryExecutionException('Unable to return customer bookings from ' . __CLASS__, $e->getCode(), $e); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * @param int $id |
| 328 | * |
| 329 | * @return mixed |
| 330 | * @throws QueryExecutionException |
| 331 | * @throws InvalidArgumentException |
| 332 | */ |
| 333 | public function getById($id) |
| 334 | { |
| 335 | $params = [ |
| 336 | ':id' => $id, |
| 337 | ]; |
| 338 | |
| 339 | $paymentsTable = PaymentsTable::getTableName(); |
| 340 | |
| 341 | $usersTable = UsersTable::getTableName(); |
| 342 | |
| 343 | $couponsTable = CouponsTable::getTableName(); |
| 344 | |
| 345 | try { |
| 346 | $statement = $this->connection->prepare( |
| 347 | "SELECT |
| 348 | cb.id AS booking_id, |
| 349 | cb.appointmentId AS booking_appointmentId, |
| 350 | cb.customerId AS booking_customerId, |
| 351 | cb.status AS booking_status, |
| 352 | cb.price AS booking_price, |
| 353 | cb.persons AS booking_persons, |
| 354 | cb.couponId AS booking_couponId, |
| 355 | cb.customFields AS booking_customFields, |
| 356 | cb.info AS booking_info, |
| 357 | cb.utcOffset AS booking_utcOffset, |
| 358 | cb.aggregatedPrice AS booking_aggregatedPrice, |
| 359 | cb.duration AS booking_duration, |
| 360 | cb.created AS booking_created, |
| 361 | cb.token AS booking_token, |
| 362 | |
| 363 | cb.qrCodes AS booking_qrCodes, |
| 364 | cu.id AS customer_id, |
| 365 | cu.firstName AS customer_firstName, |
| 366 | cu.lastName AS customer_lastName, |
| 367 | cu.email AS customer_email, |
| 368 | cu.note AS customer_note, |
| 369 | cu.phone AS customer_phone, |
| 370 | cu.gender AS customer_gender, |
| 371 | cu.birthday AS customer_birthday, |
| 372 | |
| 373 | p.id AS payment_id, |
| 374 | p.amount AS payment_amount, |
| 375 | p.dateTime AS payment_dateTime, |
| 376 | p.status AS payment_status, |
| 377 | p.gateway AS payment_gateway, |
| 378 | p.gatewayTitle AS payment_gatewayTitle, |
| 379 | p.transactionId AS payment_transactionId, |
| 380 | p.data AS payment_data, |
| 381 | p.created AS payment_created, |
| 382 | |
| 383 | c.id AS coupon_id, |
| 384 | c.code AS coupon_code, |
| 385 | c.discount AS coupon_discount, |
| 386 | c.deduction AS coupon_deduction, |
| 387 | c.expirationDate AS coupon_expirationDate, |
| 388 | c.startDate AS coupon_startDate, |
| 389 | c.limit AS coupon_limit, |
| 390 | c.customerLimit AS coupon_customerLimit, |
| 391 | c.status AS coupon_status |
| 392 | FROM {$this->table} cb |
| 393 | INNER JOIN {$usersTable} cu ON cu.id = cb.customerId |
| 394 | LEFT JOIN {$couponsTable} c ON c.id = cb.couponId |
| 395 | LEFT JOIN {$paymentsTable} p ON p.customerBookingId = cb.id |
| 396 | WHERE cb.id = :id" |
| 397 | ); |
| 398 | |
| 399 | $statement->execute($params); |
| 400 | |
| 401 | $rows = $statement->fetchAll(); |
| 402 | } catch (Exception $e) { |
| 403 | throw new QueryExecutionException('Unable to find booking by id in ' . __CLASS__, $e->getCode(), $e); |
| 404 | } |
| 405 | |
| 406 | $reformattedData = call_user_func([static::FACTORY, 'reformat'], $rows); |
| 407 | |
| 408 | return !empty($reformattedData[$id]) ? |
| 409 | call_user_func([static::FACTORY, 'create'], $reformattedData[$id]) : null; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Returns a collection of bookings where actions on booking are not completed |
| 414 | * |
| 415 | * @return Collection |
| 416 | * @throws InvalidArgumentException |
| 417 | * @throws QueryExecutionException |
| 418 | * @throws \Exception |
| 419 | */ |
| 420 | public function getUncompletedActionsForBookings() |
| 421 | { |
| 422 | $params = []; |
| 423 | |
| 424 | $currentDateTime = "STR_TO_DATE('" . DateTimeService::getNowDateTimeInUtc() . "', '%Y-%m-%d %H:%i:%s')"; |
| 425 | |
| 426 | $pastDateTime = |
| 427 | "STR_TO_DATE('" . |
| 428 | DateTimeService::getNowDateTimeObjectInUtc()->modify('-1 day')->format('Y-m-d H:i:s') . |
| 429 | "', '%Y-%m-%d %H:%i:%s')"; |
| 430 | |
| 431 | try { |
| 432 | $statement = $this->connection->prepare( |
| 433 | "SELECT * FROM {$this->table} |
| 434 | WHERE |
| 435 | actionsCompleted = 0 AND |
| 436 | {$currentDateTime} > DATE_ADD(created, INTERVAL 300 SECOND) AND |
| 437 | {$pastDateTime} < created" |
| 438 | ); |
| 439 | |
| 440 | $statement->execute($params); |
| 441 | |
| 442 | $rows = $statement->fetchAll(); |
| 443 | } catch (\Exception $e) { |
| 444 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e); |
| 445 | } |
| 446 | |
| 447 | $items = []; |
| 448 | |
| 449 | foreach ($rows as $row) { |
| 450 | $items[] = call_user_func([static::FACTORY, 'create'], $row); |
| 451 | } |
| 452 | |
| 453 | return new Collection($items); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * @param array $ids |
| 458 | * |
| 459 | * @return array |
| 460 | * @throws QueryExecutionException |
| 461 | */ |
| 462 | public function countByNoShowStatus($ids) |
| 463 | { |
| 464 | $idsString = implode(', ', $ids); |
| 465 | |
| 466 | try { |
| 467 | $statement = $this->connection->prepare( |
| 468 | "SELECT customerId, COUNT(*) AS count |
| 469 | FROM {$this->table} cb |
| 470 | WHERE customerId IN ($idsString) AND status = 'no-show' |
| 471 | GROUP BY customerId" |
| 472 | ); |
| 473 | |
| 474 | $statement->execute(); |
| 475 | |
| 476 | $rows = $statement->fetchAll(); |
| 477 | |
| 478 | $result = []; |
| 479 | foreach ($ids as $id) { |
| 480 | $count = 0; |
| 481 | foreach ($rows as $row) { |
| 482 | if ($row['customerId'] == $id) { |
| 483 | $count = $row['count']; |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | $result[$id] = [ |
| 488 | 'id' => $id, |
| 489 | 'count' => (int)$count, |
| 490 | ]; |
| 491 | } |
| 492 | } catch (Exception $e) { |
| 493 | throw new QueryExecutionException('Unable to find booking by id in ' . __CLASS__, $e->getCode(), $e); |
| 494 | } |
| 495 | |
| 496 | return $result; |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * @param array $criteria |
| 501 | * |
| 502 | * @return Collection |
| 503 | * @throws QueryExecutionException |
| 504 | * @throws InvalidArgumentException |
| 505 | */ |
| 506 | public function getByCriteria($criteria) |
| 507 | { |
| 508 | try { |
| 509 | $params = []; |
| 510 | |
| 511 | $where = []; |
| 512 | |
| 513 | if (!empty($criteria['appointmentIds'])) { |
| 514 | $queryAppointments = []; |
| 515 | |
| 516 | foreach ($criteria['appointmentIds'] as $index => $value) { |
| 517 | $param = ':appointmentId' . $index; |
| 518 | |
| 519 | $queryAppointments[] = $param; |
| 520 | |
| 521 | $params[$param] = $value; |
| 522 | } |
| 523 | |
| 524 | $where[] = 'cb.appointmentId IN (' . implode(', ', $queryAppointments) . ')'; |
| 525 | } |
| 526 | |
| 527 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 528 | |
| 529 | $statement = $this->connection->prepare( |
| 530 | "SELECT |
| 531 | cb.id AS id, |
| 532 | cb.appointmentId AS appointmentId, |
| 533 | cb.customerId AS customerId, |
| 534 | cb.status AS status, |
| 535 | cb.price AS price, |
| 536 | cb.tax AS tax, |
| 537 | cb.persons AS persons, |
| 538 | cb.customFields AS customFields, |
| 539 | cb.info AS info, |
| 540 | cb.aggregatedPrice AS aggregatedPrice, |
| 541 | cb.packageCustomerServiceId AS packageCustomerServiceId, |
| 542 | cb.duration AS duration, |
| 543 | cb.created AS created, |
| 544 | cb.qrCodes AS qrCodes, |
| 545 | cb.tax AS tax |
| 546 | FROM {$this->table} cb |
| 547 | {$where}" |
| 548 | ); |
| 549 | |
| 550 | $statement->execute($params); |
| 551 | |
| 552 | $rows = $statement->fetchAll(); |
| 553 | } catch (\Exception $e) { |
| 554 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 555 | } |
| 556 | |
| 557 | $result = new Collection(); |
| 558 | |
| 559 | foreach ($rows as $row) { |
| 560 | $result->addItem( |
| 561 | call_user_func([static::FACTORY, 'create'], $row), |
| 562 | $row['id'] |
| 563 | ); |
| 564 | } |
| 565 | |
| 566 | return $result; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * @param array $criteria |
| 571 | * @param int $limitPerPage |
| 572 | * |
| 573 | * @return array |
| 574 | * @throws QueryExecutionException |
| 575 | * @throws InvalidArgumentException |
| 576 | */ |
| 577 | public function getEventBookingIdsByCriteria($criteria = [], $limitPerPage = null) |
| 578 | { |
| 579 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 580 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 581 | $eventsTable = EventsTable::getTableName(); |
| 582 | $eventProvidersTable = EventsProvidersTable::getTableName(); |
| 583 | $customersTable = UsersTable::getTableName(); |
| 584 | |
| 585 | $params = []; |
| 586 | |
| 587 | $where = []; |
| 588 | |
| 589 | $joins = []; |
| 590 | |
| 591 | if (!empty($criteria['customers'])) { |
| 592 | $queryIds = []; |
| 593 | |
| 594 | foreach ($criteria['customers'] as $index => $value) { |
| 595 | $param = ':customerId' . $index; |
| 596 | |
| 597 | $queryIds[] = $param; |
| 598 | |
| 599 | $params[$param] = $value; |
| 600 | } |
| 601 | |
| 602 | $where[] = '(cb.customerId IN (' . implode(', ', $queryIds) . '))'; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | if (!empty($criteria['dates'])) { |
| 607 | if (isset($criteria['dates'][0], $criteria['dates'][1])) { |
| 608 | $where[] = "(ep.periodStart BETWEEN :eventFrom AND :eventTo)"; |
| 609 | $params[':eventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 610 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | if (!empty($criteria['search'])) { |
| 615 | $params[':search1'] = $params[':search2'] = "%{$criteria['search']}%"; |
| 616 | |
| 617 | $where[] = '(e.name LIKE :search1 OR SUBSTR(cb.token, 1, 5) LIKE :search2)'; |
| 618 | } |
| 619 | |
| 620 | if (!empty($criteria['providers'])) { |
| 621 | $queryIds1 = []; |
| 622 | $queryIds2 = []; |
| 623 | |
| 624 | foreach ($criteria['providers'] as $index => $value) { |
| 625 | $param1 = ':providerId' . $index; |
| 626 | $param2 = ':organizerId' . $index; |
| 627 | |
| 628 | $queryIds1[] = $param1; |
| 629 | $queryIds2[] = $param2; |
| 630 | |
| 631 | $params[$param1] = $value; |
| 632 | $params[$param2] = $value; |
| 633 | } |
| 634 | |
| 635 | $where[] = '(epr.userId IN (' . implode(', ', $queryIds1) . ') OR e.organizerId IN (' . implode(', ', $queryIds2) . '))'; |
| 636 | |
| 637 | $joins[] = "LEFT JOIN {$eventProvidersTable} epr ON epr.eventId = e.id"; |
| 638 | } |
| 639 | |
| 640 | if (!empty($criteria['statuses'])) { |
| 641 | $queryIds = []; |
| 642 | |
| 643 | foreach ($criteria['statuses'] as $index => $value) { |
| 644 | $param = ':status' . $index; |
| 645 | |
| 646 | $queryIds[] = $param; |
| 647 | |
| 648 | $params[$param] = $value; |
| 649 | } |
| 650 | |
| 651 | $where[] = '(cb.status IN (' . implode(', ', $queryIds) . '))'; |
| 652 | } |
| 653 | |
| 654 | if (!empty($criteria['status'])) { |
| 655 | $whereOr = []; |
| 656 | foreach ($criteria['status'] as $index => $value) { |
| 657 | switch ($value) { |
| 658 | case 'approved': |
| 659 | $whereOr[] = "(cb.status = 'approved' AND e.status = 'approved')"; |
| 660 | break; |
| 661 | case 'canceled': |
| 662 | $whereOr[] = "(cb.status = 'canceled' OR e.status = 'rejected' OR e.status = 'canceled')"; |
| 663 | break; |
| 664 | case 'rejected': |
| 665 | $whereOr[] = "(cb.status = 'rejected')"; |
| 666 | break; |
| 667 | case 'no-show': |
| 668 | $whereOr[] = "(cb.status = 'no-show' and e.status = 'approved')"; |
| 669 | break; |
| 670 | case 'waiting': |
| 671 | $whereOr[] = "(cb.status = 'waiting' and e.status = 'approved')"; |
| 672 | break; |
| 673 | } |
| 674 | } |
| 675 | $where[] = '(' . implode(' OR ', $whereOr) . ')'; |
| 676 | } |
| 677 | |
| 678 | if (!empty($criteria['events'])) { |
| 679 | $queryIds = []; |
| 680 | |
| 681 | foreach ($criteria['events'] as $index => $value) { |
| 682 | $param = ':eventId' . $index; |
| 683 | |
| 684 | $queryIds[] = $param; |
| 685 | |
| 686 | $params[$param] = $value; |
| 687 | } |
| 688 | |
| 689 | $where[] = '(e.id IN (' . implode(', ', $queryIds) . '))'; |
| 690 | } |
| 691 | |
| 692 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 693 | |
| 694 | $groupBy = 'GROUP BY cb.id'; |
| 695 | $limit = $this->getLimit( |
| 696 | !empty($criteria['page']) ? (int)$criteria['page'] : 0, |
| 697 | $limitPerPage |
| 698 | ); |
| 699 | |
| 700 | $orderBy = 'ORDER BY MIN(ep.periodStart), cb.id'; |
| 701 | |
| 702 | if (!empty($criteria['sort'])) { |
| 703 | $column = $criteria['sort'][0] === '-' ? substr($criteria['sort'], 1) : $criteria['sort']; |
| 704 | $orderColumn = ''; |
| 705 | if ($column === 'attendee') { |
| 706 | $joins[] = "INNER JOIN {$customersTable} cu ON cu.id = cb.customerId "; |
| 707 | |
| 708 | $orderColumn = ', CONCAT(cu.firstName, " ", cu.lastName)'; |
| 709 | } elseif ($column === 'event') { |
| 710 | $orderColumn = ', e.name'; |
| 711 | } |
| 712 | $orderDir = $orderColumn ? ($criteria['sort'][0] === '-' ? 'DESC' : 'ASC') : ''; |
| 713 | $orderBy = "ORDER BY MIN(DATE(ep.periodStart)) {$orderColumn} {$orderDir}, cb.id"; |
| 714 | } |
| 715 | |
| 716 | $joins = $joins ? implode(' ', $joins) : ''; |
| 717 | |
| 718 | try { |
| 719 | $statement = $this->connection->prepare( |
| 720 | "SELECT cb.id |
| 721 | FROM {$this->table} cb |
| 722 | INNER JOIN {$customerBookingsEventsPeriods} cbe ON cbe.customerBookingId = cb.id |
| 723 | LEFT JOIN {$eventsPeriodsTable} ep ON ep.id = cbe.eventPeriodId |
| 724 | LEFT JOIN {$eventsTable} e ON e.id = ep.eventId |
| 725 | |
| 726 | {$joins} |
| 727 | {$where} |
| 728 | {$groupBy} |
| 729 | {$orderBy} |
| 730 | {$limit}" |
| 731 | ); |
| 732 | |
| 733 | $statement->execute($params); |
| 734 | |
| 735 | $rows = $statement->fetchAll(\PDO::FETCH_COLUMN); |
| 736 | } catch (\Exception $e) { |
| 737 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 738 | } |
| 739 | |
| 740 | return $rows; |
| 741 | } |
| 742 | |
| 743 | |
| 744 | /** |
| 745 | * @param array $criteria |
| 746 | * |
| 747 | * @return array |
| 748 | * @throws QueryExecutionException |
| 749 | * @throws InvalidArgumentException |
| 750 | */ |
| 751 | public function getEventBookingsByIds($ids, $criteria) |
| 752 | { |
| 753 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 754 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 755 | $usersTable = UsersTable::getTableName(); |
| 756 | $eventsTable = EventsTable::getTableName(); |
| 757 | $eventProvidersTable = EventsProvidersTable::getTableName(); |
| 758 | $bookingsTicketsTable = CustomerBookingToEventsTicketsTable::getTableName(); |
| 759 | |
| 760 | $params = []; |
| 761 | |
| 762 | $where = []; |
| 763 | |
| 764 | $fields = ''; |
| 765 | |
| 766 | $joins = ''; |
| 767 | |
| 768 | if (!empty($criteria['dates'])) { |
| 769 | if (isset($criteria['dates'][0], $criteria['dates'][1])) { |
| 770 | $where[] = "(ep.periodStart BETWEEN :eventFrom AND :eventTo)"; |
| 771 | $params[':eventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 772 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | if (!empty($ids)) { |
| 777 | $queryIds = []; |
| 778 | |
| 779 | foreach ($ids as $index => $value) { |
| 780 | $param = ':id' . $index; |
| 781 | |
| 782 | $queryIds[] = $param; |
| 783 | |
| 784 | $params[$param] = $value; |
| 785 | } |
| 786 | |
| 787 | $where[] = '(cb.id IN (' . implode(', ', $queryIds) . '))'; |
| 788 | } |
| 789 | |
| 790 | if (!empty($criteria['fetchBookingsCoupons'])) { |
| 791 | $couponsTable = CouponsTable::getTableName(); |
| 792 | |
| 793 | $fields .= ' |
| 794 | c.id AS coupon_id, |
| 795 | c.code AS coupon_code, |
| 796 | c.discount AS coupon_discount, |
| 797 | c.deduction AS coupon_deduction, |
| 798 | c.limit AS coupon_limit, |
| 799 | c.customerLimit AS coupon_customerLimit, |
| 800 | c.status AS coupon_status, |
| 801 | '; |
| 802 | |
| 803 | $joins .= " |
| 804 | LEFT JOIN {$couponsTable} c ON c.id = cb.couponId |
| 805 | "; |
| 806 | } |
| 807 | |
| 808 | if (!empty($criteria['fetchBookingsPayments'])) { |
| 809 | $paymentsTable = PaymentsTable::getTableName(); |
| 810 | |
| 811 | $fields .= ' |
| 812 | p.id AS payment_id, |
| 813 | p.amount AS payment_amount, |
| 814 | p.dateTime AS payment_dateTime, |
| 815 | p.status AS payment_status, |
| 816 | p.gateway AS payment_gateway, |
| 817 | p.gatewayTitle AS payment_gatewayTitle, |
| 818 | p.transactionId AS payment_transactionId, |
| 819 | p.data AS payment_data, |
| 820 | p.wcOrderId AS payment_wcOrderId, |
| 821 | p.wcOrderItemId AS payment_wcOrderItemId, |
| 822 | '; |
| 823 | |
| 824 | $joins .= " |
| 825 | LEFT JOIN {$paymentsTable} p ON p.customerBookingId = cb.id |
| 826 | "; |
| 827 | } |
| 828 | |
| 829 | if (!empty($criteria['fetchEvent'])) { |
| 830 | $fields .= ' |
| 831 | ep.id as event_periodId, |
| 832 | ep.periodStart as event_periodStart, |
| 833 | ep.periodEnd as event_periodEnd, |
| 834 | ep.zoomMeeting as event_zoomMeeting, |
| 835 | ep.googleMeetUrl as event_googleMeetUrl, |
| 836 | ep.microsoftTeamsUrl as event_microsoftTeamsUrl, |
| 837 | ep.lessonSpace as event_lessonSpace, |
| 838 | |
| 839 | e.id AS event_id, |
| 840 | e.name AS event_name, |
| 841 | e.customPricing AS event_customPricing, |
| 842 | e.status AS event_status, |
| 843 | e.organizerId AS event_organizerId, |
| 844 | e.settings AS event_settings, |
| 845 | '; |
| 846 | |
| 847 | $joins .= " |
| 848 | INNER JOIN {$customerBookingsEventsPeriods} cbe ON cbe.customerBookingId = cb.id |
| 849 | LEFT JOIN {$eventsPeriodsTable} ep ON ep.id = cbe.eventPeriodId |
| 850 | LEFT JOIN {$eventsTable} e ON e.id = ep.eventId |
| 851 | "; |
| 852 | } |
| 853 | |
| 854 | if (!empty($criteria['fetchProviders'])) { |
| 855 | $fields .= ' |
| 856 | pu.id AS provider_id, |
| 857 | pu.firstName AS provider_firstName, |
| 858 | pu.lastName AS provider_lastName, |
| 859 | pu.email AS provider_email, |
| 860 | pu.pictureThumbPath AS provider_pictureThumbPath, |
| 861 | pu.badgeId AS provider_badgeId, |
| 862 | '; |
| 863 | $joins .= " |
| 864 | LEFT JOIN {$eventProvidersTable} epr ON epr.eventId = e.id |
| 865 | LEFT JOIN {$usersTable} pu ON epr.userId = pu.id or pu.id = e.organizerId |
| 866 | "; |
| 867 | } |
| 868 | |
| 869 | if (!empty($criteria['fetchCustomers'])) { |
| 870 | $fields .= ' |
| 871 | cu.id AS customer_id, |
| 872 | cu.type AS customer_type, |
| 873 | cu.firstName AS customer_firstName, |
| 874 | cu.lastName AS customer_lastName, |
| 875 | cu.email AS customer_email, |
| 876 | cu.note AS customer_note, |
| 877 | cu.phone AS customer_phone, |
| 878 | cu.gender AS customer_gender, |
| 879 | cu.birthday AS customer_birthday, |
| 880 | '; |
| 881 | |
| 882 | $joins .= " |
| 883 | INNER JOIN {$usersTable} cu ON cu.id = cb.customerId |
| 884 | "; |
| 885 | } |
| 886 | |
| 887 | $fields .= ' |
| 888 | cb.id AS booking_id, |
| 889 | cb.appointmentId AS booking_appointmentId, |
| 890 | cb.customerId AS booking_customerId, |
| 891 | cb.status AS booking_status, |
| 892 | cb.price AS booking_price, |
| 893 | cb.tax AS booking_tax, |
| 894 | cb.persons AS booking_persons, |
| 895 | cb.couponId AS booking_couponId, |
| 896 | cb.customFields AS booking_customFields, |
| 897 | cb.info AS booking_info, |
| 898 | cb.utcOffset AS booking_utcOffset, |
| 899 | cb.token AS booking_token, |
| 900 | cb.aggregatedPrice AS booking_aggregatedPrice, |
| 901 | cb.qrCodes AS booking_qrCodes, |
| 902 | |
| 903 | cbt.id AS booking_ticket_id, |
| 904 | cbt.eventTicketId AS booking_ticket_eventTicketId, |
| 905 | cbt.price AS booking_ticket_price, |
| 906 | cbt.persons AS booking_ticket_persons |
| 907 | '; |
| 908 | |
| 909 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 910 | |
| 911 | try { |
| 912 | $statement = $this->connection->prepare( |
| 913 | "SELECT |
| 914 | {$fields} |
| 915 | FROM {$this->table} cb |
| 916 | LEFT JOIN {$bookingsTicketsTable} cbt ON cbt.customerBookingId = cb.id |
| 917 | |
| 918 | {$joins} |
| 919 | {$where} |
| 920 | " |
| 921 | ); |
| 922 | |
| 923 | $statement->execute($params); |
| 924 | |
| 925 | $rows = $statement->fetchAll(); |
| 926 | } catch (\Exception $e) { |
| 927 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 928 | } |
| 929 | |
| 930 | return CustomerBookingFactory::reformat($rows); |
| 931 | } |
| 932 | } |
| 933 |