CustomerBookingEventPeriodRepository.php
6 years ago
CustomerBookingEventTicketRepository.php
1 year ago
EventPeriodsRepository.php
4 years ago
EventProvidersRepository.php
6 years ago
EventRepository.php
1 year ago
EventTagsRepository.php
6 years ago
EventTicketRepository.php
1 year ago
EventRepository.php
1881 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Booking\Event; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Collection\Collection; |
| 6 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 7 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 8 | use AmeliaBooking\Domain\Factory\Booking\Appointment\CustomerBookingFactory; |
| 9 | use AmeliaBooking\Domain\Factory\Booking\Event\EventFactory; |
| 10 | use AmeliaBooking\Domain\Repository\Booking\Event\EventRepositoryInterface; |
| 11 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 12 | use AmeliaBooking\Domain\ValueObjects\String\Status; |
| 13 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 14 | use AmeliaBooking\Infrastructure\Licence; |
| 15 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 16 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\CustomerBookingsTable; |
| 17 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\CustomerBookingsToEventsPeriodsTable; |
| 18 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\CustomerBookingToEventsTicketsTable; |
| 19 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsPeriodsTable; |
| 20 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsProvidersTable; |
| 21 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsTagsTable; |
| 22 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsTicketsTable; |
| 23 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Coupon\CouponsTable; |
| 24 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Coupon\CouponsToEventsTable; |
| 25 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Gallery\GalleriesTable; |
| 26 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Payment\PaymentsTable; |
| 27 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\Provider\ProvidersGoogleCalendarTable; |
| 28 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\Provider\ProvidersOutlookCalendarTable; |
| 29 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\UsersTable; |
| 30 | |
| 31 | /** |
| 32 | * Class EventRepository |
| 33 | * |
| 34 | * @package AmeliaBooking\Infrastructure\Repository\Booking\Event |
| 35 | */ |
| 36 | class EventRepository extends AbstractRepository implements EventRepositoryInterface |
| 37 | { |
| 38 | |
| 39 | const FACTORY = EventFactory::class; |
| 40 | |
| 41 | /** |
| 42 | * @param Event $entity |
| 43 | * |
| 44 | * @return bool |
| 45 | * @throws QueryExecutionException |
| 46 | */ |
| 47 | public function add($entity) |
| 48 | { |
| 49 | $data = $entity->toArray(); |
| 50 | |
| 51 | $params = [ |
| 52 | ':bookingOpens' => $data['bookingOpens'] ? DateTimeService::getCustomDateTimeInUtc($data['bookingOpens']) : null, |
| 53 | ':bookingCloses' => $data['bookingCloses'] ? DateTimeService::getCustomDateTimeInUtc($data['bookingCloses']) : null, |
| 54 | ':bookingOpensRec' => $data['bookingOpensRec'], |
| 55 | ':bookingClosesRec' => $data['bookingClosesRec'], |
| 56 | ':status' => $data['status'], |
| 57 | ':name' => $data['name'], |
| 58 | ':description' => $data['description'], |
| 59 | ':color' => $data['color'], |
| 60 | ':price' => $data['price'], |
| 61 | ':bringingAnyone' => $data['bringingAnyone'] ? 1 : 0, |
| 62 | ':bookMultipleTimes' => $data['bookMultipleTimes'] ? 1 : 0, |
| 63 | ':maxCapacity' => $data['maxCapacity'], |
| 64 | ':maxCustomCapacity' => $data['maxCustomCapacity'], |
| 65 | ':maxExtraPeople' => $data['maxExtraPeople'], |
| 66 | ':show' => $data['show'] ? 1 : 0, |
| 67 | ':notifyParticipants' => $data['notifyParticipants'], |
| 68 | ':customLocation' => $data['customLocation'], |
| 69 | ':parentId' => $data['parentId'], |
| 70 | ':created' => $data['created'], |
| 71 | ':closeAfterMin' => $data['closeAfterMin'], |
| 72 | ':closeAfterMinBookings' => $data['closeAfterMinBookings'] ? 1 : 0, |
| 73 | ':aggregatedPrice' => $data['aggregatedPrice'] ? 1 : 0, |
| 74 | ':error' => '', |
| 75 | ]; |
| 76 | |
| 77 | $additionalData = Licence\DataModifier::getEventRepositoryData($data); |
| 78 | |
| 79 | $params = array_merge($params, $additionalData['values'], $additionalData['addValues']); |
| 80 | |
| 81 | try { |
| 82 | $statement = $this->connection->prepare( |
| 83 | "INSERT INTO {$this->table} |
| 84 | ( |
| 85 | {$additionalData['columns']} |
| 86 | `bookingOpens`, |
| 87 | `bookingCloses`, |
| 88 | `bookingOpensRec`, |
| 89 | `bookingClosesRec`, |
| 90 | `status`, |
| 91 | `name`, |
| 92 | `description`, |
| 93 | `color`, |
| 94 | `price`, |
| 95 | `bringingAnyone`, |
| 96 | `bookMultipleTimes`, |
| 97 | `maxCapacity`, |
| 98 | `maxCustomCapacity`, |
| 99 | `maxExtraPeople`, |
| 100 | `show`, |
| 101 | `notifyParticipants`, |
| 102 | `customLocation`, |
| 103 | `parentId`, |
| 104 | `created`, |
| 105 | `closeAfterMin`, |
| 106 | `closeAfterMinBookings`, |
| 107 | `aggregatedPrice`, |
| 108 | `error` |
| 109 | ) |
| 110 | VALUES ( |
| 111 | {$additionalData['placeholders']} |
| 112 | :bookingOpens, |
| 113 | :bookingCloses, |
| 114 | :bookingOpensRec, |
| 115 | :bookingClosesRec, |
| 116 | :status, |
| 117 | :name, |
| 118 | :description, |
| 119 | :color, |
| 120 | :price, |
| 121 | :bringingAnyone, |
| 122 | :bookMultipleTimes, |
| 123 | :maxCapacity, |
| 124 | :maxCustomCapacity, |
| 125 | :maxExtraPeople, |
| 126 | :show, |
| 127 | :notifyParticipants, |
| 128 | :customLocation, |
| 129 | :parentId, |
| 130 | :created, |
| 131 | :closeAfterMin, |
| 132 | :closeAfterMinBookings, |
| 133 | :aggregatedPrice, |
| 134 | :error |
| 135 | )" |
| 136 | ); |
| 137 | |
| 138 | $res = $statement->execute($params); |
| 139 | |
| 140 | if (!$res) { |
| 141 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 142 | } |
| 143 | |
| 144 | return $this->connection->lastInsertId(); |
| 145 | } catch (\Exception $e) { |
| 146 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @param int $id |
| 152 | * @param Event $entity |
| 153 | * |
| 154 | * @return mixed |
| 155 | * @throws QueryExecutionException |
| 156 | */ |
| 157 | public function update($id, $entity) |
| 158 | { |
| 159 | $data = $entity->toArray(); |
| 160 | |
| 161 | $params = [ |
| 162 | ':id' => $id, |
| 163 | ':bookingOpens' => $data['bookingOpens'] ? DateTimeService::getCustomDateTimeInUtc($data['bookingOpens']) : null, |
| 164 | ':bookingCloses' => $data['bookingCloses'] ? DateTimeService::getCustomDateTimeInUtc($data['bookingCloses']) : null, |
| 165 | ':bookingOpensRec' => $data['bookingOpensRec'], |
| 166 | ':bookingClosesRec' => $data['bookingClosesRec'], |
| 167 | ':status' => $data['status'], |
| 168 | ':name' => $data['name'], |
| 169 | ':description' => $data['description'], |
| 170 | ':color' => $data['color'], |
| 171 | ':price' => $data['price'], |
| 172 | ':bringingAnyone' => $data['bringingAnyone'] ? 1 : 0, |
| 173 | ':bookMultipleTimes' => $data['bookMultipleTimes'] ? 1 : 0, |
| 174 | ':maxCapacity' => $data['maxCapacity'], |
| 175 | ':maxCustomCapacity' => $data['maxCustomCapacity'], |
| 176 | ':maxExtraPeople' => $data['maxExtraPeople'], |
| 177 | ':show' => $data['show'] ? 1 : 0, |
| 178 | ':notifyParticipants' => $data['notifyParticipants'] ? 1 : 0, |
| 179 | ':customLocation' => $data['customLocation'], |
| 180 | ':parentId' => $data['parentId'], |
| 181 | ':closeAfterMin' => $data['closeAfterMin'], |
| 182 | ':closeAfterMinBookings' => $data['closeAfterMinBookings'] ? 1 : 0, |
| 183 | ':aggregatedPrice' => $data['aggregatedPrice'] ? 1 : 0 |
| 184 | ]; |
| 185 | |
| 186 | $additionalData = Licence\DataModifier::getEventRepositoryData($data); |
| 187 | |
| 188 | $params = array_merge($params, $additionalData['values']); |
| 189 | |
| 190 | try { |
| 191 | $statement = $this->connection->prepare( |
| 192 | "UPDATE {$this->table} |
| 193 | SET |
| 194 | {$additionalData['columnsPlaceholders']} |
| 195 | `bookingOpens` = :bookingOpens, |
| 196 | `bookingCloses` = :bookingCloses, |
| 197 | `bookingOpensRec` = :bookingOpensRec, |
| 198 | `bookingClosesRec` = :bookingClosesRec, |
| 199 | `status` = :status, |
| 200 | `name` = :name, |
| 201 | `description` = :description, |
| 202 | `color` = :color, |
| 203 | `price` = :price, |
| 204 | `bringingAnyone` = :bringingAnyone, |
| 205 | `bookMultipleTimes` = :bookMultipleTimes, |
| 206 | `maxCapacity` = :maxCapacity, |
| 207 | `maxCustomCapacity` = :maxCustomCapacity, |
| 208 | `maxExtraPeople` = :maxExtraPeople, |
| 209 | `show` = :show, |
| 210 | `notifyParticipants` = :notifyParticipants, |
| 211 | `customLocation` = :customLocation, |
| 212 | `parentId` = :parentId, |
| 213 | `closeAfterMin` = :closeAfterMin, |
| 214 | `closeAfterMinBookings` = :closeAfterMinBookings, |
| 215 | `aggregatedPrice` = :aggregatedPrice |
| 216 | WHERE id = :id" |
| 217 | ); |
| 218 | |
| 219 | $res = $statement->execute($params); |
| 220 | |
| 221 | if (!$res) { |
| 222 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 223 | } |
| 224 | |
| 225 | return $res; |
| 226 | } catch (\Exception $e) { |
| 227 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * @param int $id |
| 233 | * @param int $status |
| 234 | * |
| 235 | * @return mixed |
| 236 | * @throws QueryExecutionException |
| 237 | */ |
| 238 | public function updateStatusById($id, $status) |
| 239 | { |
| 240 | $params = [ |
| 241 | ':id' => $id, |
| 242 | ':status' => $status |
| 243 | ]; |
| 244 | |
| 245 | try { |
| 246 | $statement = $this->connection->prepare( |
| 247 | "UPDATE {$this->table} |
| 248 | SET |
| 249 | `status` = :status |
| 250 | WHERE id = :id" |
| 251 | ); |
| 252 | |
| 253 | $res = $statement->execute($params); |
| 254 | |
| 255 | if (!$res) { |
| 256 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 257 | } |
| 258 | |
| 259 | return $res; |
| 260 | } catch (\Exception $e) { |
| 261 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @param int $id |
| 267 | * @param int $parentId |
| 268 | * |
| 269 | * @return mixed |
| 270 | * @throws QueryExecutionException |
| 271 | */ |
| 272 | public function updateParentId($id, $parentId) |
| 273 | { |
| 274 | $params = [ |
| 275 | ':id' => $id, |
| 276 | ':parentId' => $parentId, |
| 277 | ]; |
| 278 | |
| 279 | try { |
| 280 | $statement = $this->connection->prepare( |
| 281 | "UPDATE {$this->table} |
| 282 | SET |
| 283 | `parentId` = :parentId |
| 284 | WHERE id = :id" |
| 285 | ); |
| 286 | |
| 287 | $res = $statement->execute($params); |
| 288 | |
| 289 | if (!$res) { |
| 290 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 291 | } |
| 292 | |
| 293 | return $res; |
| 294 | } catch (\Exception $e) { |
| 295 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * @param array $criteria |
| 301 | * |
| 302 | * @return Collection |
| 303 | * @throws QueryExecutionException |
| 304 | * @throws InvalidArgumentException |
| 305 | */ |
| 306 | public function getProvidersEvents($criteria) |
| 307 | { |
| 308 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 309 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 310 | $usersTable = UsersTable::getTableName(); |
| 311 | |
| 312 | $params = []; |
| 313 | $where = []; |
| 314 | |
| 315 | if (!empty($criteria['dates'])) { |
| 316 | if (isset($criteria['dates'][0], $criteria['dates'][1])) { |
| 317 | $whereStart = "(DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') BETWEEN :eventFrom AND :eventTo)"; |
| 318 | $params[':eventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 319 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 320 | |
| 321 | $whereEnd = "(DATE_FORMAT(ep.periodEnd, '%Y-%m-%d %H:%i:%s') BETWEEN :bookingFrom2 AND :bookingTo2)"; |
| 322 | $params[':bookingFrom2'] = $params[':eventFrom']; |
| 323 | $params[':bookingTo2'] = $params[':eventTo']; |
| 324 | |
| 325 | $where[] = "({$whereStart} OR {$whereEnd})"; |
| 326 | } elseif (isset($criteria['dates'][0])) { |
| 327 | $where[] = "(DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') >= :eventFrom)"; |
| 328 | $params[':eventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 329 | } elseif (isset($criteria['dates'][1])) { |
| 330 | $where[] = "(DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') <= :eventTo)"; |
| 331 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 332 | } else { |
| 333 | $where[] = "(DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') > :eventFrom)"; |
| 334 | $params[':eventFrom'] = DateTimeService::getNowDateTimeInUtc(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if (!empty($criteria['providers'])) { |
| 339 | $queryProviders = []; |
| 340 | |
| 341 | foreach ((array)$criteria['providers'] as $index => $value) { |
| 342 | $param = ':provider' . $index; |
| 343 | $queryProviders[] = $param; |
| 344 | $params[$param] = $value; |
| 345 | } |
| 346 | |
| 347 | $where[] = 'epr.userId IN (' . implode(', ', $queryProviders) . ')'; |
| 348 | } |
| 349 | |
| 350 | if (!empty($criteria['status'])) { |
| 351 | $params[':status'] = $criteria['status']; |
| 352 | |
| 353 | $where[] = 'e.status = :status'; |
| 354 | } |
| 355 | |
| 356 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 357 | |
| 358 | try { |
| 359 | $statement = $this->connection->prepare( |
| 360 | "SELECT |
| 361 | e.id AS event_id, |
| 362 | e.name AS event_name, |
| 363 | e.status AS event_status, |
| 364 | e.bookingOpens AS event_bookingOpens, |
| 365 | e.bookingCloses AS event_bookingCloses, |
| 366 | e.recurringCycle AS event_recurringCycle, |
| 367 | e.recurringOrder AS event_recurringOrder, |
| 368 | e.recurringInterval AS event_recurringInterval, |
| 369 | e.recurringUntil AS event_recurringUntil, |
| 370 | e.recurringMonthly AS event_recurringMonthly, |
| 371 | e.monthlyDate AS event_monthlyDate, |
| 372 | e.monthlyOnRepeat AS event_monthlyOnRepeat, |
| 373 | e.monthlyOnDay AS event_monthlyOnDay, |
| 374 | e.bringingAnyone AS event_bringingAnyone, |
| 375 | e.bookMultipleTimes AS event_bookMultipleTimes, |
| 376 | e.maxCapacity AS event_maxCapacity, |
| 377 | e.maxCustomCapacity AS event_maxCustomCapacity, |
| 378 | e.maxExtraPeople AS event_maxExtraPeople, |
| 379 | e.price AS event_price, |
| 380 | e.description AS event_description, |
| 381 | e.color AS event_color, |
| 382 | e.show AS event_show, |
| 383 | e.locationId AS event_locationId, |
| 384 | e.customLocation AS event_customLocation, |
| 385 | e.parentId AS event_parentId, |
| 386 | e.created AS event_created, |
| 387 | e.notifyParticipants AS event_notifyParticipants, |
| 388 | e.translations AS event_translations, |
| 389 | e.deposit AS event_deposit, |
| 390 | e.depositPayment AS event_depositPayment, |
| 391 | e.depositPerPerson AS event_depositPerPerson, |
| 392 | e.fullPayment AS event_fullPayment, |
| 393 | e.customPricing AS event_customPricing, |
| 394 | e.aggregatedPrice AS event_aggregatedPrice, |
| 395 | |
| 396 | ep.id AS event_periodId, |
| 397 | ep.periodStart AS event_periodStart, |
| 398 | ep.periodEnd AS event_periodEnd, |
| 399 | |
| 400 | pu.id AS provider_id, |
| 401 | pu.firstName AS provider_firstName, |
| 402 | pu.lastName AS provider_lastName, |
| 403 | pu.email AS provider_email, |
| 404 | pu.note AS provider_note, |
| 405 | pu.description AS provider_description, |
| 406 | pu.phone AS provider_phone, |
| 407 | pu.gender AS provider_gender, |
| 408 | pu.pictureFullPath AS provider_pictureFullPath, |
| 409 | pu.pictureThumbPath AS provider_pictureThumbPath, |
| 410 | pu.translations AS provider_translations |
| 411 | FROM {$this->table} e |
| 412 | INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 413 | INNER JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 414 | INNER JOIN {$usersTable} pu ON pu.id = epr.userId |
| 415 | {$where} |
| 416 | ORDER BY ep.periodStart" |
| 417 | ); |
| 418 | |
| 419 | $statement->execute($params); |
| 420 | |
| 421 | $rows = $statement->fetchAll(); |
| 422 | } catch (\Exception $e) { |
| 423 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 424 | } |
| 425 | |
| 426 | return call_user_func([static::FACTORY, 'createCollection'], $rows); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * @param array $criteria |
| 431 | * @param int $itemsPerPage |
| 432 | * |
| 433 | * @return array |
| 434 | * @throws QueryExecutionException |
| 435 | * @throws InvalidArgumentException |
| 436 | */ |
| 437 | public function getFilteredIds($criteria, $itemsPerPage) |
| 438 | { |
| 439 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 440 | $eventsTagsTable = EventsTagsTable::getTableName(); |
| 441 | $customerBookingsTable = CustomerBookingsTable::getTableName(); |
| 442 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 443 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 444 | $usersTable = UsersTable::getTableName(); |
| 445 | |
| 446 | $params = []; |
| 447 | |
| 448 | $where = []; |
| 449 | |
| 450 | if (isset($criteria['parentId'])) { |
| 451 | $params[':parentId'] = $criteria['parentId']; |
| 452 | |
| 453 | $params[':originParentId'] = $criteria['parentId']; |
| 454 | |
| 455 | $where[] = 'e.parentId = :parentId OR e.id = :originParentId'; |
| 456 | } |
| 457 | |
| 458 | if (!empty($criteria['search'])) { |
| 459 | $where[] = "(e.name LIKE '%" . $criteria['search'] . "%' |
| 460 | OR e.translations LIKE '{\"name\":{%" . $criteria['search'] . "%\"description\":{%' |
| 461 | OR e.translations LIKE '{\"description\":{%\"name\":{%" . $criteria['search'] . "%' |
| 462 | OR (e.translations LIKE '{\"name\":{%" . $criteria['search'] . "%' AND e.translations NOT LIKE '%\"description\":{%'))"; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | if (isset($criteria['show'])) { |
| 467 | $where[] = 'e.show = 1'; |
| 468 | } |
| 469 | |
| 470 | if (!empty($criteria['dates'])) { |
| 471 | if (isset($criteria['dates'][0], $criteria['dates'][1])) { |
| 472 | $where[] = "((DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') BETWEEN :eventFrom1 AND :eventTo1) |
| 473 | OR (DATE_FORMAT(ep.periodEnd, '%Y-%m-%d %H:%i:%s') BETWEEN :eventFrom2 AND :eventTo2) |
| 474 | OR (:eventFrom3 BETWEEN DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') AND DATE_FORMAT(ep.periodEnd, '%Y-%m-%d %H:%i:%s')) |
| 475 | OR (:eventTo3 BETWEEN DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') AND DATE_FORMAT(ep.periodEnd, '%Y-%m-%d %H:%i:%s')))"; |
| 476 | |
| 477 | $params[':eventFrom1'] = $params[':eventFrom2'] = $params[':eventFrom3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 478 | $params[':eventTo1'] = $params[':eventTo2'] = $params[':eventTo3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 479 | } elseif (isset($criteria['dates'][0])) { |
| 480 | $where[] = "(DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') >= :eventFrom OR (DATE_FORMAT(ep.periodEnd, '%Y-%m-%d %H:%i:%s') >= :eventTo))"; |
| 481 | $params[':eventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 482 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 483 | } elseif (isset($criteria['dates'][1])) { |
| 484 | $where[] = "(DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') <= :eventTo)"; |
| 485 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 486 | } else { |
| 487 | $where[] = "(DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') > :eventFrom)"; |
| 488 | $params[':eventFrom'] = DateTimeService::getNowDateTimeInUtc(); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | $tagJoin = ''; |
| 493 | |
| 494 | if (isset($criteria['tag'])) { |
| 495 | $queryTags = []; |
| 496 | |
| 497 | $tags = $criteria['tag']; |
| 498 | foreach ((array)$tags as $index => $value) { |
| 499 | $param = ':tag' . $index; |
| 500 | |
| 501 | $queryTags[] = $param; |
| 502 | |
| 503 | $params[$param] = $value; |
| 504 | } |
| 505 | |
| 506 | $where[] = 'et.name IN (' . implode(', ', $queryTags) . ')'; |
| 507 | |
| 508 | $tagJoin = "INNER JOIN {$eventsTagsTable} et ON et.eventId = e.id"; |
| 509 | } |
| 510 | |
| 511 | if (!empty($criteria['id'])) { |
| 512 | if (!empty($criteria['recurring'])) { |
| 513 | $whereOr = []; |
| 514 | foreach ((array)$criteria['id'] as $index => $value) { |
| 515 | $param = 'id' . $index; |
| 516 | |
| 517 | $params[':rec1' . $param] = (int)$value; |
| 518 | $params[':rec2' . $param] = (int)$value; |
| 519 | $params[':rec3' . $param] = (int)$value; |
| 520 | $params[':rec4' . $param] = (int)$value; |
| 521 | |
| 522 | $whereOr[] = "((e.id = :rec1id" . $index . " AND e.parentId IS NULL) OR |
| 523 | (e.parentId IN (SELECT parentId FROM {$this->table} WHERE parentId = :rec2id" . $index . ")) OR |
| 524 | (e.id >= :rec3id" . $index . " AND e.parentId IN (SELECT parentId FROM {$this->table} WHERE id = :rec4id" . $index . ")))"; |
| 525 | } |
| 526 | $where[] = implode(' OR ', $whereOr); |
| 527 | } else { |
| 528 | $queryIds = []; |
| 529 | |
| 530 | foreach ((array)$criteria['id'] as $index => $value) { |
| 531 | $param = ':id' . $index; |
| 532 | |
| 533 | $queryIds[] = $param; |
| 534 | |
| 535 | $params[$param] = (int)$value; |
| 536 | } |
| 537 | |
| 538 | $where[] = 'e.id IN (' . implode(', ', $queryIds) . ')'; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | $customerJoin = ''; |
| 543 | |
| 544 | if (!empty($criteria['customerId']) || !empty($criteria['customerBookingsIds'])) { |
| 545 | $customerJoin = " |
| 546 | LEFT JOIN {$customerBookingsEventsPeriods} cbe ON cbe.eventPeriodId = ep.id |
| 547 | LEFT JOIN {$customerBookingsTable} cb ON cb.id = cbe.customerBookingId"; |
| 548 | |
| 549 | if (!empty($criteria['customerId'])) { |
| 550 | $params[':customerId'] = $criteria['customerId']; |
| 551 | |
| 552 | $where[] = 'cb.customerId = :customerId'; |
| 553 | } |
| 554 | |
| 555 | if (!empty($criteria['customerBookingsIds'])) { |
| 556 | $queryBookingsIds = []; |
| 557 | |
| 558 | foreach ($criteria['customerBookingsIds'] as $index => $value) { |
| 559 | $param = ':customerBookingId' . $index; |
| 560 | |
| 561 | $queryBookingsIds[] = $param; |
| 562 | |
| 563 | $params[$param] = $value; |
| 564 | } |
| 565 | |
| 566 | $where[] = 'cb.id IN (' . implode(', ', $queryBookingsIds) . ')'; |
| 567 | } |
| 568 | |
| 569 | if (!empty($criteria['customerBookingStatus'])) { |
| 570 | $params[':customerBookingStatus'] = $criteria['customerBookingStatus']; |
| 571 | |
| 572 | $where[] = 'cb.status = :customerBookingStatus'; |
| 573 | } |
| 574 | |
| 575 | if (!empty($criteria['customerBookingCouponId'])) { |
| 576 | $params[':customerBookingCouponId'] = $criteria['customerBookingCouponId']; |
| 577 | |
| 578 | $where[] = 'cb.couponId = :customerBookingCouponId'; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | if (!empty($criteria['locationId'])) { |
| 583 | $params[':locationId'] = $criteria['locationId']; |
| 584 | |
| 585 | $where[] = 'e.locationId = :locationId'; |
| 586 | } |
| 587 | |
| 588 | if (!empty($criteria['locations'])) { |
| 589 | foreach ((array)$criteria['locations'] as $index => $value) { |
| 590 | $param = ':location' . $index; |
| 591 | $queryLocations[] = $param; |
| 592 | $params[$param] = $value; |
| 593 | } |
| 594 | |
| 595 | $where3 = 'e.locationId IN (' . implode(', ', $queryLocations) . ')'; |
| 596 | |
| 597 | $where[] = '(' . $where3 . ')'; |
| 598 | } |
| 599 | |
| 600 | $providerJoin = ''; |
| 601 | |
| 602 | if (!empty($criteria['providers'])) { |
| 603 | $providerJoin = " |
| 604 | LEFT JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 605 | INNER JOIN {$usersTable} pu ON pu.id = epr.userId OR pu.id = e.organizerId"; |
| 606 | $queryProviders = []; |
| 607 | |
| 608 | foreach ((array)$criteria['providers'] as $index => $value) { |
| 609 | $param = ':provider' . $index; |
| 610 | $queryProviders[] = $param; |
| 611 | $params[$param] = $value; |
| 612 | } |
| 613 | |
| 614 | $where1 = 'epr.userId IN (' . implode(', ', $queryProviders) . ')'; |
| 615 | |
| 616 | $queryProviders = []; |
| 617 | foreach ((array)$criteria['providers'] as $index => $value) { |
| 618 | $param = ':organizer' . $index; |
| 619 | $queryProviders[] = $param; |
| 620 | $params[$param] = $value; |
| 621 | } |
| 622 | |
| 623 | $where2 = 'e.organizerId IN (' . implode(', ', $queryProviders) . ')'; |
| 624 | |
| 625 | $where[] = '(' . $where1 . ' OR ' . $where2 . ')'; |
| 626 | |
| 627 | } |
| 628 | |
| 629 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 630 | |
| 631 | $limit = $this->getLimit( |
| 632 | !empty($criteria['page']) ? (int)$criteria['page'] : 0, |
| 633 | (int)$itemsPerPage |
| 634 | ); |
| 635 | |
| 636 | try { |
| 637 | $statement = $this->connection->prepare( |
| 638 | "SELECT |
| 639 | e.id |
| 640 | FROM {$this->table} e |
| 641 | INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 642 | {$tagJoin} |
| 643 | {$providerJoin} |
| 644 | {$customerJoin} |
| 645 | {$where} |
| 646 | GROUP BY e.id |
| 647 | ORDER BY ep.periodStart, e.id |
| 648 | {$limit}" |
| 649 | ); |
| 650 | |
| 651 | $statement->execute($params); |
| 652 | |
| 653 | $rows = $statement->fetchAll(); |
| 654 | } catch (\Exception $e) { |
| 655 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 656 | } |
| 657 | |
| 658 | return array_column($rows, 'id'); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * @param array $criteria |
| 663 | * |
| 664 | * @return int |
| 665 | * @throws QueryExecutionException |
| 666 | * @throws InvalidArgumentException |
| 667 | */ |
| 668 | public function getFilteredIdsCount($criteria) |
| 669 | { |
| 670 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 671 | $eventsTagsTable = EventsTagsTable::getTableName(); |
| 672 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 673 | $usersTable = UsersTable::getTableName(); |
| 674 | |
| 675 | |
| 676 | $params = []; |
| 677 | $where = []; |
| 678 | |
| 679 | if (isset($criteria['parentId'])) { |
| 680 | $params[':parentId'] = $criteria['parentId']; |
| 681 | |
| 682 | $params[':originParentId'] = $criteria['parentId']; |
| 683 | |
| 684 | $where[] = 'e.parentId = :parentId OR e.id = :originParentId'; |
| 685 | } |
| 686 | |
| 687 | if (!empty($criteria['search'])) { |
| 688 | $where[] = "(e.name LIKE '%" . $criteria['search'] . "%' |
| 689 | OR e.translations LIKE '{\"name\":{%" . $criteria['search'] . "%\"description\":{%' |
| 690 | OR e.translations LIKE '{\"description\":{%\"name\":{%" . $criteria['search'] . "%' |
| 691 | OR (e.translations LIKE '{\"name\":{%" . $criteria['search'] . "%' AND e.translations NOT LIKE '%\"description\":{%'))"; |
| 692 | } |
| 693 | |
| 694 | if (isset($criteria['show'])) { |
| 695 | $where[] = 'e.show = 1'; |
| 696 | } |
| 697 | |
| 698 | if (!empty($criteria['dates'])) { |
| 699 | if (isset($criteria['dates'][0], $criteria['dates'][1])) { |
| 700 | $where[] = "((DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') BETWEEN :eventFrom1 AND :eventTo1) |
| 701 | OR (DATE_FORMAT(ep.periodEnd, '%Y-%m-%d %H:%i:%s') BETWEEN :eventFrom2 AND :eventTo2) |
| 702 | OR (:eventFrom3 BETWEEN DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') AND DATE_FORMAT(ep.periodEnd, '%Y-%m-%d %H:%i:%s')) |
| 703 | OR (:eventTo3 BETWEEN DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') AND DATE_FORMAT(ep.periodEnd, '%Y-%m-%d %H:%i:%s')))"; |
| 704 | |
| 705 | $params[':eventFrom1'] = $params[':eventFrom2'] = $params[':eventFrom3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 706 | $params[':eventTo1'] = $params[':eventTo2'] = $params[':eventTo3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 707 | } elseif (isset($criteria['dates'][0])) { |
| 708 | $where[] = "(DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') >= :eventFrom OR (DATE_FORMAT(ep.periodEnd, '%Y-%m-%d %H:%i:%s') >= :eventTo))"; |
| 709 | $params[':eventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 710 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 711 | } elseif (isset($criteria['dates'][1])) { |
| 712 | $where[] = "(DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') <= :eventTo)"; |
| 713 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 714 | } else { |
| 715 | $where[] = "(DATE_FORMAT(ep.periodStart, '%Y-%m-%d %H:%i:%s') > :eventFrom)"; |
| 716 | $params[':eventFrom'] = DateTimeService::getNowDateTimeInUtc(); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if (!empty($criteria['locationId'])) { |
| 721 | $params[':locationId'] = $criteria['locationId']; |
| 722 | |
| 723 | $where[] = 'e.locationId = :locationId'; |
| 724 | } |
| 725 | |
| 726 | if (!empty($criteria['locations'])) { |
| 727 | foreach ((array)$criteria['locations'] as $index => $value) { |
| 728 | $param = ':location' . $index; |
| 729 | $queryLocations[] = $param; |
| 730 | $params[$param] = $value; |
| 731 | } |
| 732 | |
| 733 | $where3 = 'e.locationId IN (' . implode(', ', $queryLocations) . ')'; |
| 734 | |
| 735 | $where[] = '(' . $where3 . ')'; |
| 736 | } |
| 737 | |
| 738 | |
| 739 | $tagJoin = ''; |
| 740 | |
| 741 | if (isset($criteria['tag'])) { |
| 742 | $queryTags = []; |
| 743 | |
| 744 | $tags = $criteria['tag'];//explode(',', $criteria['tag']); |
| 745 | foreach ((array)$tags as $index => $value) { |
| 746 | $param = ':tag' . $index; |
| 747 | |
| 748 | $queryTags[] = $param; |
| 749 | |
| 750 | $params[$param] = $value;//trim($value, '{}'); |
| 751 | } |
| 752 | |
| 753 | $where[] = 'et.name IN (' . implode(', ', $queryTags) . ')'; |
| 754 | |
| 755 | $tagJoin = "INNER JOIN {$eventsTagsTable} et ON et.eventId = e.id"; |
| 756 | } |
| 757 | |
| 758 | if (!empty($criteria['id'])) { |
| 759 | if (!empty($criteria['recurring'])) { |
| 760 | $whereOr = []; |
| 761 | foreach ((array)$criteria['id'] as $index => $value) { |
| 762 | $param = 'id' . $index; |
| 763 | |
| 764 | $params[':rec1' . $param] = (int)$value; |
| 765 | $params[':rec2' . $param] = (int)$value; |
| 766 | $params[':rec3' . $param] = (int)$value; |
| 767 | $params[':rec4' . $param] = (int)$value; |
| 768 | |
| 769 | $whereOr[] = "((e.id = :rec1id" . $index . " AND e.parentId IS NULL) OR |
| 770 | (e.parentId IN (SELECT parentId FROM {$this->table} WHERE parentId = :rec2id" . $index . ")) OR |
| 771 | (e.id >= :rec3id" . $index . " AND e.parentId IN (SELECT parentId FROM {$this->table} WHERE id = :rec4id" . $index . ")))"; |
| 772 | } |
| 773 | $where[] = implode(' OR ', $whereOr); |
| 774 | } else { |
| 775 | $queryIds = []; |
| 776 | |
| 777 | foreach ((array)$criteria['id'] as $index => $value) { |
| 778 | $param = ':id' . $index; |
| 779 | |
| 780 | $queryIds[] = $param; |
| 781 | |
| 782 | $params[$param] = (int)$value; |
| 783 | } |
| 784 | |
| 785 | $where[] = 'e.id IN (' . implode(', ', $queryIds) . ')'; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | $providerJoin = ''; |
| 790 | |
| 791 | if (!empty($criteria['providers'])) { |
| 792 | $providerJoin = " |
| 793 | LEFT JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 794 | INNER JOIN {$usersTable} pu ON pu.id = epr.userId OR pu.id = e.organizerId"; |
| 795 | |
| 796 | $queryProviders = []; |
| 797 | |
| 798 | foreach ((array)$criteria['providers'] as $index => $value) { |
| 799 | $param = ':provider' . $index; |
| 800 | $queryProviders[] = $param; |
| 801 | $params[$param] = $value; |
| 802 | } |
| 803 | $where1 = 'epr.userId IN (' . implode(', ', $queryProviders) . ')'; |
| 804 | |
| 805 | $queryProviders = []; |
| 806 | foreach ((array)$criteria['providers'] as $index => $value) { |
| 807 | $param = ':organizer' . $index; |
| 808 | $queryProviders[] = $param; |
| 809 | $params[$param] = $value; |
| 810 | } |
| 811 | $where2 = 'e.organizerId IN (' . implode(', ', $queryProviders) . ')'; |
| 812 | |
| 813 | $where[] = '(' . $where1 . ' OR ' . $where2 . ')'; |
| 814 | } |
| 815 | |
| 816 | $customerJoin = ''; |
| 817 | |
| 818 | $customerBookingsTable = CustomerBookingsTable::getTableName(); |
| 819 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 820 | |
| 821 | if (!empty($criteria['customerId']) || !empty($criteria['customerBookingsIds'])) { |
| 822 | $customerJoin = " |
| 823 | LEFT JOIN {$customerBookingsEventsPeriods} cbe ON cbe.eventPeriodId = ep.id |
| 824 | LEFT JOIN {$customerBookingsTable} cb ON cb.id = cbe.customerBookingId"; |
| 825 | |
| 826 | if (!empty($criteria['customerId'])) { |
| 827 | $params[':customerId'] = $criteria['customerId']; |
| 828 | |
| 829 | $where[] = 'cb.customerId = :customerId'; |
| 830 | } |
| 831 | |
| 832 | if (!empty($criteria['customerBookingsIds'])) { |
| 833 | $queryBookingsIds = []; |
| 834 | |
| 835 | foreach ($criteria['customerBookingsIds'] as $index => $value) { |
| 836 | $param = ':customerBookingId' . $index; |
| 837 | |
| 838 | $queryBookingsIds[] = $param; |
| 839 | |
| 840 | $params[$param] = $value; |
| 841 | } |
| 842 | |
| 843 | $where[] = 'cb.id IN (' . implode(', ', $queryBookingsIds) . ')'; |
| 844 | } |
| 845 | |
| 846 | if (!empty($criteria['customerBookingStatus'])) { |
| 847 | $params[':customerBookingStatus'] = $criteria['customerBookingStatus']; |
| 848 | |
| 849 | $where[] = 'cb.status = :customerBookingStatus'; |
| 850 | } |
| 851 | |
| 852 | if (!empty($criteria['customerBookingCouponId'])) { |
| 853 | $params[':customerBookingCouponId'] = $criteria['customerBookingCouponId']; |
| 854 | |
| 855 | $where[] = 'cb.couponId = :customerBookingCouponId'; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 860 | |
| 861 | try { |
| 862 | $statement = $this->connection->prepare( |
| 863 | "SELECT e.id |
| 864 | FROM {$this->table} e |
| 865 | INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 866 | {$tagJoin} |
| 867 | {$providerJoin} |
| 868 | {$customerJoin} |
| 869 | {$where} |
| 870 | GROUP BY e.id |
| 871 | ORDER BY ep.periodStart" |
| 872 | ); |
| 873 | |
| 874 | $statement->execute($params); |
| 875 | |
| 876 | $rows = $statement->fetchAll(); |
| 877 | } catch (\Exception $e) { |
| 878 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 879 | } |
| 880 | |
| 881 | return sizeOf($rows); |
| 882 | } |
| 883 | |
| 884 | /** |
| 885 | * @param int $id |
| 886 | * |
| 887 | * @return Event |
| 888 | * @throws QueryExecutionException |
| 889 | * @throws InvalidArgumentException |
| 890 | */ |
| 891 | public function getById($id) |
| 892 | { |
| 893 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 894 | $eventsTagsTable = EventsTagsTable::getTableName(); |
| 895 | $eventsTicketTable = EventsTicketsTable::getTableName(); |
| 896 | |
| 897 | $customerBookingsTable = CustomerBookingsTable::getTableName(); |
| 898 | $paymentsTable = PaymentsTable::getTableName(); |
| 899 | $usersTable = UsersTable::getTableName(); |
| 900 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 901 | $galleriesTable = GalleriesTable::getTableName(); |
| 902 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 903 | $couponsTable = CouponsTable::getTableName(); |
| 904 | |
| 905 | try { |
| 906 | $statement = $this->connection->prepare( |
| 907 | "SELECT |
| 908 | e.id AS event_id, |
| 909 | e.name AS event_name, |
| 910 | e.status AS event_status, |
| 911 | e.bookingOpens AS event_bookingOpens, |
| 912 | e.bookingCloses AS event_bookingCloses, |
| 913 | e.bookingOpensRec AS event_bookingOpensRec, |
| 914 | e.bookingClosesRec AS event_bookingClosesRec, |
| 915 | e.ticketRangeRec AS event_ticketRangeRec, |
| 916 | e.recurringCycle AS event_recurringCycle, |
| 917 | e.recurringOrder AS event_recurringOrder, |
| 918 | e.recurringInterval AS event_recurringInterval, |
| 919 | e.recurringMonthly AS event_recurringMonthly, |
| 920 | e.monthlyDate AS event_monthlyDate, |
| 921 | e.monthlyOnRepeat AS event_monthlyOnRepeat, |
| 922 | e.monthlyOnDay AS event_monthlyOnDay, |
| 923 | e.recurringUntil AS event_recurringUntil, |
| 924 | e.bringingAnyone AS event_bringingAnyone, |
| 925 | e.bookMultipleTimes AS event_bookMultipleTimes, |
| 926 | e.maxCapacity AS event_maxCapacity, |
| 927 | e.maxCustomCapacity AS event_maxCustomCapacity, |
| 928 | e.maxExtraPeople AS event_maxExtraPeople, |
| 929 | e.price AS event_price, |
| 930 | e.description AS event_description, |
| 931 | e.color AS event_color, |
| 932 | e.show AS event_show, |
| 933 | e.notifyParticipants AS event_notifyParticipants, |
| 934 | e.locationId AS event_locationId, |
| 935 | e.customLocation AS event_customLocation, |
| 936 | e.parentId AS event_parentId, |
| 937 | e.created AS event_created, |
| 938 | e.settings AS event_settings, |
| 939 | e.zoomUserId AS event_zoomUserId, |
| 940 | e.organizerId AS event_organizerId, |
| 941 | e.translations AS event_translations, |
| 942 | e.deposit AS event_deposit, |
| 943 | e.depositPayment AS event_depositPayment, |
| 944 | e.depositPerPerson AS event_depositPerPerson, |
| 945 | e.fullPayment AS event_fullPayment, |
| 946 | e.customPricing AS event_customPricing, |
| 947 | e.aggregatedPrice AS event_aggregatedPrice, |
| 948 | |
| 949 | ep.id AS event_periodId, |
| 950 | ep.periodStart AS event_periodStart, |
| 951 | ep.periodEnd AS event_periodEnd, |
| 952 | ep.zoomMeeting AS event_periodZoomMeeting, |
| 953 | ep.lessonSpace AS event_periodLessonSpace, |
| 954 | ep.googleCalendarEventId AS event_googleCalendarEventId, |
| 955 | ep.googleMeetUrl AS event_googleMeetUrl, |
| 956 | ep.outlookCalendarEventId AS event_outlookCalendarEventId, |
| 957 | ep.microsoftTeamsUrl AS event_microsoftTeamsUrl, |
| 958 | ep.appleCalendarEventId AS event_appleCalendarEventId, |
| 959 | |
| 960 | et.id AS event_tagId, |
| 961 | et.name AS event_tagName, |
| 962 | |
| 963 | cb.id AS booking_id, |
| 964 | cb.customerId AS booking_customerId, |
| 965 | cb.status AS booking_status, |
| 966 | cb.price AS booking_price, |
| 967 | cb.persons AS booking_persons, |
| 968 | cb.customFields AS booking_customFields, |
| 969 | cb.info AS booking_info, |
| 970 | cb.aggregatedPrice AS booking_aggregatedPrice, |
| 971 | cb.token AS booking_token, |
| 972 | cb.utcOffset AS booking_utcOffset, |
| 973 | cb.couponId AS booking_couponId, |
| 974 | |
| 975 | cu.id AS customer_id, |
| 976 | cu.firstName AS customer_firstName, |
| 977 | cu.lastName AS customer_lastName, |
| 978 | cu.email AS customer_email, |
| 979 | cu.note AS customer_note, |
| 980 | cu.phone AS customer_phone, |
| 981 | cu.gender AS customer_gender, |
| 982 | cu.birthday AS customer_birthday, |
| 983 | |
| 984 | p.id AS payment_id, |
| 985 | p.amount AS payment_amount, |
| 986 | p.dateTime AS payment_dateTime, |
| 987 | p.status AS payment_status, |
| 988 | p.gateway AS payment_gateway, |
| 989 | p.gatewayTitle AS payment_gatewayTitle, |
| 990 | p.transactionId AS payment_transactionId, |
| 991 | p.data AS payment_data, |
| 992 | p.wcOrderId AS payment_wcOrderId, |
| 993 | p.wcOrderItemId AS payment_wcOrderItemId, |
| 994 | p.invoiceNumber AS payment_invoiceNumber, |
| 995 | |
| 996 | pu.id AS provider_id, |
| 997 | pu.firstName AS provider_firstName, |
| 998 | pu.lastName AS provider_lastName, |
| 999 | pu.email AS provider_email, |
| 1000 | pu.note AS provider_note, |
| 1001 | pu.description AS provider_description, |
| 1002 | pu.phone AS provider_phone, |
| 1003 | pu.gender AS provider_gender, |
| 1004 | pu.translations AS provider_translations, |
| 1005 | pu.timeZone AS provider_timeZone, |
| 1006 | |
| 1007 | g.id AS gallery_id, |
| 1008 | g.pictureFullPath AS gallery_picture_full, |
| 1009 | g.pictureThumbPath AS gallery_picture_thumb, |
| 1010 | g.position AS gallery_position, |
| 1011 | |
| 1012 | c.id AS coupon_id, |
| 1013 | c.code AS coupon_code, |
| 1014 | c.discount AS coupon_discount, |
| 1015 | c.deduction AS coupon_deduction, |
| 1016 | c.limit AS coupon_limit, |
| 1017 | c.customerLimit AS coupon_customerLimit, |
| 1018 | c.status AS coupon_status, |
| 1019 | |
| 1020 | t.id AS ticket_id, |
| 1021 | t.name AS ticket_name, |
| 1022 | t.enabled AS ticket_enabled, |
| 1023 | t.price AS ticket_price, |
| 1024 | t.spots AS ticket_spots, |
| 1025 | t.waitingListSpots AS ticket_waiting_list_spots, |
| 1026 | t.dateRanges AS ticket_dateRanges, |
| 1027 | t.translations AS ticket_translations |
| 1028 | |
| 1029 | FROM {$this->table} e |
| 1030 | INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 1031 | LEFT JOIN {$eventsTagsTable} et ON et.eventId = e.id |
| 1032 | LEFT JOIN {$customerBookingsEventsPeriods} cbe ON cbe.eventPeriodId = ep.id |
| 1033 | LEFT JOIN {$customerBookingsTable} cb ON cb.id = cbe.customerBookingId |
| 1034 | LEFT JOIN {$usersTable} cu ON cu.id = cb.customerId |
| 1035 | LEFT JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 1036 | LEFT JOIN {$usersTable} pu ON pu.id = epr.userId |
| 1037 | LEFT JOIN {$paymentsTable} p ON p.customerBookingId = cb.id |
| 1038 | LEFT JOIN {$galleriesTable} g ON g.entityId = e.id AND g.entityType = 'event' |
| 1039 | LEFT JOIN {$couponsTable} c ON c.id = cb.couponId |
| 1040 | LEFT JOIN {$eventsTicketTable} t ON t.eventId = e.id |
| 1041 | |
| 1042 | WHERE e.id = :eventId" |
| 1043 | ); |
| 1044 | |
| 1045 | $statement->bindParam(':eventId', $id); |
| 1046 | |
| 1047 | $statement->execute(); |
| 1048 | |
| 1049 | $rows = $statement->fetchAll(); |
| 1050 | } catch (\Exception $e) { |
| 1051 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 1052 | } |
| 1053 | |
| 1054 | return call_user_func([static::FACTORY, 'createCollection'], $rows)->getItem($id); |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | /** |
| 1059 | * @param int $id |
| 1060 | * |
| 1061 | * @return mixed |
| 1062 | * @throws QueryExecutionException |
| 1063 | */ |
| 1064 | public function isRecurring($id) |
| 1065 | { |
| 1066 | try { |
| 1067 | $statement = $this->connection->prepare( |
| 1068 | "SELECT |
| 1069 | e.recurringOrder AS event_recurringOrder, |
| 1070 | e.parentId AS event_parentId |
| 1071 | FROM {$this->table} e |
| 1072 | WHERE e.id = :eventId" |
| 1073 | ); |
| 1074 | |
| 1075 | $statement->bindParam(':eventId', $id); |
| 1076 | |
| 1077 | $statement->execute(); |
| 1078 | |
| 1079 | return $statement->fetch(); |
| 1080 | } catch (\Exception $e) { |
| 1081 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | |
| 1086 | /** |
| 1087 | * @param int $id |
| 1088 | * @param int $parentId |
| 1089 | * |
| 1090 | * @return mixed |
| 1091 | * @throws QueryExecutionException |
| 1092 | */ |
| 1093 | public function getRecurringIds($id, $parentId) |
| 1094 | { |
| 1095 | $whereParent = empty($parentId) ? '' : ' OR e.parentId = :parentId'; |
| 1096 | try { |
| 1097 | $statement = $this->connection->prepare( |
| 1098 | "SELECT |
| 1099 | e.id AS eventId |
| 1100 | FROM {$this->table} e |
| 1101 | WHERE e.parentId = :eventId" . $whereParent |
| 1102 | ); |
| 1103 | |
| 1104 | $statement->bindParam(':eventId', $id); |
| 1105 | if ($parentId) { |
| 1106 | $statement->bindParam(':parentId', $parentId); |
| 1107 | } |
| 1108 | |
| 1109 | $statement->execute(); |
| 1110 | |
| 1111 | $events = $statement->fetchAll(); |
| 1112 | |
| 1113 | return array_column($events, 'eventId'); |
| 1114 | } catch (\Exception $e) { |
| 1115 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | /** |
| 1120 | * @param $criteria |
| 1121 | * |
| 1122 | * @return Collection |
| 1123 | * @throws InvalidArgumentException |
| 1124 | * @throws QueryExecutionException |
| 1125 | * @throws InvalidArgumentException |
| 1126 | */ |
| 1127 | public function getWithCoupons($criteria) |
| 1128 | { |
| 1129 | $couponToEventsTable = CouponsToEventsTable::getTableName(); |
| 1130 | $couponsTable = CouponsTable::getTableName(); |
| 1131 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 1132 | $usersTable = UsersTable::getTableName(); |
| 1133 | $eventsTicketTable = EventsTicketsTable::getTableName(); |
| 1134 | |
| 1135 | $params = []; |
| 1136 | |
| 1137 | $where = []; |
| 1138 | |
| 1139 | foreach ((array)$criteria as $index => $value) { |
| 1140 | $params[':event' . $index] = $value['eventId']; |
| 1141 | |
| 1142 | if ($value['couponId']) { |
| 1143 | $params[':coupon' . $index] = $value['couponId']; |
| 1144 | $params[':couponStatus' . $index] = Status::VISIBLE; |
| 1145 | } |
| 1146 | |
| 1147 | $where[] = "(e.id = :event$index" |
| 1148 | . ($value['couponId'] ? " AND c.id = :coupon$index AND c.status = :couponStatus$index" : '') . ')'; |
| 1149 | } |
| 1150 | |
| 1151 | $where = $where ? 'WHERE ' . implode(' OR ', $where) : ''; |
| 1152 | |
| 1153 | try { |
| 1154 | $statement = $this->connection->prepare( |
| 1155 | "SELECT |
| 1156 | e.id AS event_id, |
| 1157 | e.name AS event_name, |
| 1158 | e.status AS event_status, |
| 1159 | e.bookingOpens AS event_bookingOpens, |
| 1160 | e.bookingCloses AS event_bookingCloses, |
| 1161 | e.recurringCycle AS event_recurringCycle, |
| 1162 | e.recurringOrder AS event_recurringOrder, |
| 1163 | e.recurringInterval AS event_recurringInterval, |
| 1164 | e.recurringUntil AS event_recurringUntil, |
| 1165 | e.bringingAnyone AS event_bringingAnyone, |
| 1166 | e.bookMultipleTimes AS event_bookMultipleTimes, |
| 1167 | e.maxCapacity AS event_maxCapacity, |
| 1168 | e.maxCustomCapacity AS event_maxCustomCapacity, |
| 1169 | e.maxExtraPeople AS event_maxExtraPeople, |
| 1170 | e.price AS event_price, |
| 1171 | e.description AS event_description, |
| 1172 | e.color AS event_color, |
| 1173 | e.show AS event_show, |
| 1174 | e.notifyParticipants AS event_notifyParticipants, |
| 1175 | e.locationId AS event_locationId, |
| 1176 | e.customLocation AS event_customLocation, |
| 1177 | e.parentId AS event_parentId, |
| 1178 | e.created AS event_created, |
| 1179 | e.translations AS event_translations, |
| 1180 | e.deposit AS event_deposit, |
| 1181 | e.depositPayment AS event_depositPayment, |
| 1182 | e.depositPerPerson AS event_depositPerPerson, |
| 1183 | e.fullPayment AS event_fullPayment, |
| 1184 | e.customPricing AS event_customPricing, |
| 1185 | e.aggregatedPrice AS event_aggregatedPrice, |
| 1186 | |
| 1187 | pu.id AS provider_id, |
| 1188 | pu.firstName AS provider_firstName, |
| 1189 | pu.lastName AS provider_lastName, |
| 1190 | pu.email AS provider_email, |
| 1191 | pu.note AS provider_note, |
| 1192 | pu.description AS provider_description, |
| 1193 | pu.phone AS provider_phone, |
| 1194 | pu.gender AS provider_gender, |
| 1195 | pu.translations AS provider_translations, |
| 1196 | |
| 1197 | t.id AS ticket_id, |
| 1198 | t.name AS ticket_name, |
| 1199 | t.enabled AS ticket_enabled, |
| 1200 | t.price AS ticket_price, |
| 1201 | t.spots AS ticket_spots, |
| 1202 | t.waitingListSpots AS ticket_waiting_list_spots, |
| 1203 | t.dateRanges AS ticket_dateRanges, |
| 1204 | t.translations AS ticket_translations, |
| 1205 | |
| 1206 | c.id AS coupon_id, |
| 1207 | c.code AS coupon_code, |
| 1208 | c.discount AS coupon_discount, |
| 1209 | c.deduction AS coupon_deduction, |
| 1210 | c.limit AS coupon_limit, |
| 1211 | c.customerLimit AS coupon_customerLimit, |
| 1212 | c.status AS coupon_status |
| 1213 | FROM {$this->table} e |
| 1214 | LEFT JOIN {$couponToEventsTable} ce ON ce.eventId = e.id |
| 1215 | LEFT JOIN {$couponsTable} c ON c.id = ce.couponId |
| 1216 | LEFT JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 1217 | LEFT JOIN {$usersTable} pu ON pu.id = epr.userId |
| 1218 | LEFT JOIN {$eventsTicketTable} t ON t.eventId = e.id |
| 1219 | {$where}" |
| 1220 | ); |
| 1221 | |
| 1222 | $statement->execute($params); |
| 1223 | |
| 1224 | $rows = $statement->fetchAll(); |
| 1225 | } catch (\Exception $e) { |
| 1226 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 1227 | } |
| 1228 | |
| 1229 | return call_user_func([static::FACTORY, 'createCollection'], $rows); |
| 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * @param int $bookingId |
| 1234 | * @param array $criteria |
| 1235 | * |
| 1236 | * @return Event |
| 1237 | * @throws QueryExecutionException |
| 1238 | * @throws InvalidArgumentException |
| 1239 | */ |
| 1240 | public function getByBookingId($bookingId, $criteria = []) |
| 1241 | { |
| 1242 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 1243 | |
| 1244 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 1245 | |
| 1246 | $fields = ''; |
| 1247 | |
| 1248 | $joins = ''; |
| 1249 | |
| 1250 | if (!empty($criteria['fetchEventsCoupons'])) { |
| 1251 | $couponsTable = CouponsTable::getTableName(); |
| 1252 | |
| 1253 | $fields .= ' |
| 1254 | ec.id AS coupon_id, |
| 1255 | ec.code AS coupon_code, |
| 1256 | ec.discount AS coupon_discount, |
| 1257 | ec.deduction AS coupon_deduction, |
| 1258 | ec.limit AS coupon_limit, |
| 1259 | ec.customerLimit AS coupon_customerLimit, |
| 1260 | ec.status AS coupon_status, |
| 1261 | '; |
| 1262 | |
| 1263 | $joins .= " |
| 1264 | LEFT JOIN {$couponsTable} ec ON ec.id = cb.couponId |
| 1265 | "; |
| 1266 | } |
| 1267 | |
| 1268 | if (!empty($criteria['fetchEventsTickets'])) { |
| 1269 | $ticketsTable = EventsTicketsTable::getTableName(); |
| 1270 | |
| 1271 | $fields .= ' |
| 1272 | eti.id AS ticket_id, |
| 1273 | eti.name AS ticket_name, |
| 1274 | eti.enabled AS ticket_enabled, |
| 1275 | eti.price AS ticket_price, |
| 1276 | eti.spots AS ticket_spots, |
| 1277 | eti.waitingListSpots AS ticket_waiting_list_spots, |
| 1278 | eti.dateRanges AS ticket_dateRanges, |
| 1279 | eti.translations AS ticket_translations, |
| 1280 | '; |
| 1281 | |
| 1282 | $joins .= " |
| 1283 | LEFT JOIN {$ticketsTable} eti ON eti.eventId = e.id |
| 1284 | "; |
| 1285 | } |
| 1286 | |
| 1287 | if (!empty($criteria['fetchEventsTags'])) { |
| 1288 | $tagsTable = EventsTagsTable::getTableName(); |
| 1289 | |
| 1290 | $fields .= ' |
| 1291 | eta.id AS event_tagId, |
| 1292 | eta.name AS event_tagName, |
| 1293 | '; |
| 1294 | |
| 1295 | $joins .= " |
| 1296 | LEFT JOIN {$tagsTable} eta ON eta.eventId = e.id |
| 1297 | "; |
| 1298 | } |
| 1299 | |
| 1300 | if (!empty($criteria['fetchEventsImages'])) { |
| 1301 | $galleriesTable = GalleriesTable::getTableName(); |
| 1302 | |
| 1303 | $fields .= ' |
| 1304 | eg.id AS gallery_id, |
| 1305 | eg.pictureFullPath AS gallery_picture_full, |
| 1306 | eg.pictureThumbPath AS gallery_picture_thumb, |
| 1307 | eg.position AS gallery_position, |
| 1308 | '; |
| 1309 | |
| 1310 | $joins .= " |
| 1311 | LEFT JOIN {$galleriesTable} eg ON eg.entityId = e.id AND eg.entityType = 'event' |
| 1312 | "; |
| 1313 | } |
| 1314 | |
| 1315 | if (!empty($criteria['fetchEventsProviders'])) { |
| 1316 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 1317 | |
| 1318 | $usersTable = UsersTable::getTableName(); |
| 1319 | |
| 1320 | $joins .= " |
| 1321 | LEFT JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 1322 | LEFT JOIN {$usersTable} pu ON pu.id = epr.userId |
| 1323 | "; |
| 1324 | |
| 1325 | $fields .= ' |
| 1326 | pu.id AS provider_id, |
| 1327 | pu.firstName AS provider_firstName, |
| 1328 | pu.lastName AS provider_lastName, |
| 1329 | pu.email AS provider_email, |
| 1330 | pu.note AS provider_note, |
| 1331 | pu.description AS provider_description, |
| 1332 | pu.phone AS provider_phone, |
| 1333 | pu.gender AS provider_gender, |
| 1334 | pu.pictureFullPath AS provider_pictureFullPath, |
| 1335 | pu.pictureThumbPath AS provider_pictureThumbPath, |
| 1336 | pu.translations AS provider_translations, |
| 1337 | pu.timeZone AS provider_timeZone, |
| 1338 | '; |
| 1339 | } |
| 1340 | |
| 1341 | $fields .= " |
| 1342 | e.id AS event_id, |
| 1343 | e.name AS event_name, |
| 1344 | e.status AS event_status, |
| 1345 | e.bookingOpens AS event_bookingOpens, |
| 1346 | e.bookingCloses AS event_bookingCloses, |
| 1347 | e.recurringCycle AS event_recurringCycle, |
| 1348 | e.recurringOrder AS event_recurringOrder, |
| 1349 | e.recurringInterval AS event_recurringInterval, |
| 1350 | e.recurringUntil AS event_recurringUntil, |
| 1351 | e.bringingAnyone AS event_bringingAnyone, |
| 1352 | e.bookMultipleTimes AS event_bookMultipleTimes, |
| 1353 | e.maxCapacity AS event_maxCapacity, |
| 1354 | e.maxCustomCapacity AS event_maxCustomCapacity, |
| 1355 | e.maxExtraPeople AS event_maxExtraPeople, |
| 1356 | e.price AS event_price, |
| 1357 | e.description AS event_description, |
| 1358 | e.color AS event_color, |
| 1359 | e.show AS event_show, |
| 1360 | e.notifyParticipants AS event_notifyParticipants, |
| 1361 | e.locationId AS event_locationId, |
| 1362 | e.customLocation AS event_customLocation, |
| 1363 | e.customPricing AS event_customPricing, |
| 1364 | e.parentId AS event_parentId, |
| 1365 | e.created AS event_created, |
| 1366 | e.settings AS event_settings, |
| 1367 | e.zoomUserId AS event_zoomUserId, |
| 1368 | e.translations AS event_translations, |
| 1369 | e.deposit AS event_deposit, |
| 1370 | e.depositPayment AS event_depositPayment, |
| 1371 | e.depositPerPerson AS event_depositPerPerson, |
| 1372 | e.fullPayment AS event_fullPayment, |
| 1373 | e.organizerId AS event_organizerId, |
| 1374 | e.aggregatedPrice AS event_aggregatedPrice, |
| 1375 | |
| 1376 | ep.id AS event_periodId, |
| 1377 | ep.periodStart AS event_periodStart, |
| 1378 | ep.periodEnd AS event_periodEnd, |
| 1379 | ep.zoomMeeting AS event_periodZoomMeeting, |
| 1380 | ep.lessonSpace AS event_periodLessonSpace, |
| 1381 | ep.googleCalendarEventId AS event_googleCalendarEventId, |
| 1382 | ep.googleMeetUrl AS event_googleMeetUrl, |
| 1383 | ep.outlookCalendarEventId AS event_outlookCalendarEventId, |
| 1384 | ep.microsoftTeamsUrl AS event_microsoftTeamsUrl, |
| 1385 | ep.appleCalendarEventId AS event_appleCalendarEventId |
| 1386 | "; |
| 1387 | |
| 1388 | $params = [ |
| 1389 | ':customerBookingId' => $bookingId, |
| 1390 | ]; |
| 1391 | |
| 1392 | try { |
| 1393 | $statement = $this->connection->prepare( |
| 1394 | "SELECT |
| 1395 | {$fields} |
| 1396 | FROM {$customerBookingsEventsPeriods} cbe |
| 1397 | INNER JOIN {$eventsPeriodsTable} ep ON ep.id = cbe.eventPeriodId |
| 1398 | INNER JOIN {$this->table} e ON e.id = ep.eventId |
| 1399 | {$joins} |
| 1400 | WHERE cbe.customerBookingId = :customerBookingId" |
| 1401 | ); |
| 1402 | |
| 1403 | $statement->execute($params); |
| 1404 | |
| 1405 | $rows = $statement->fetchAll(); |
| 1406 | } catch (\Exception $e) { |
| 1407 | throw new QueryExecutionException('Unable to find event by booking id in ' . __CLASS__, $e->getCode(), $e); |
| 1408 | } |
| 1409 | |
| 1410 | /** @var Collection $events */ |
| 1411 | $events = call_user_func([static::FACTORY, 'createCollection'], $rows); |
| 1412 | |
| 1413 | return $events->length() ? $events->getItem($events->keys()[0]) : null; |
| 1414 | } |
| 1415 | |
| 1416 | /** |
| 1417 | * @param array $ids |
| 1418 | * @param array $criteria |
| 1419 | * |
| 1420 | * @return Collection |
| 1421 | * @throws QueryExecutionException |
| 1422 | * @throws InvalidArgumentException |
| 1423 | */ |
| 1424 | public function getByIdsWithEntities($ids, $criteria = []) |
| 1425 | { |
| 1426 | $params = []; |
| 1427 | |
| 1428 | $where = []; |
| 1429 | |
| 1430 | $fields = ''; |
| 1431 | |
| 1432 | $joins = ''; |
| 1433 | |
| 1434 | $orderBy = ''; |
| 1435 | |
| 1436 | if (!empty($criteria['fetchEventsPeriods'])) { |
| 1437 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 1438 | |
| 1439 | $fields .= ' |
| 1440 | ep.id AS event_periodId, |
| 1441 | ep.periodStart AS event_periodStart, |
| 1442 | ep.periodEnd AS event_periodEnd, |
| 1443 | ep.zoomMeeting AS event_periodZoomMeeting, |
| 1444 | ep.lessonSpace AS event_periodLessonSpace, |
| 1445 | ep.googleCalendarEventId AS event_googleCalendarEventId, |
| 1446 | ep.googleMeetUrl AS event_googleMeetUrl, |
| 1447 | ep.outlookCalendarEventId AS event_outlookCalendarEventId, |
| 1448 | ep.microsoftTeamsUrl AS event_microsoftTeamsUrl, |
| 1449 | ep.appleCalendarEventId AS event_appleCalendarEventId, |
| 1450 | '; |
| 1451 | |
| 1452 | $joins .= " |
| 1453 | INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 1454 | "; |
| 1455 | |
| 1456 | $orderBy = 'ORDER BY ep.periodStart'; |
| 1457 | } |
| 1458 | |
| 1459 | if (!empty($criteria['fetchEventsCoupons'])) { |
| 1460 | $couponsTable = CouponsTable::getTableName(); |
| 1461 | |
| 1462 | $fields .= ' |
| 1463 | ec.id AS coupon_id, |
| 1464 | ec.code AS coupon_code, |
| 1465 | ec.discount AS coupon_discount, |
| 1466 | ec.deduction AS coupon_deduction, |
| 1467 | ec.limit AS coupon_limit, |
| 1468 | ec.customerLimit AS coupon_customerLimit, |
| 1469 | ec.status AS coupon_status, |
| 1470 | '; |
| 1471 | |
| 1472 | $joins .= " |
| 1473 | LEFT JOIN {$couponsTable} ec ON ec.id = cb.couponId |
| 1474 | "; |
| 1475 | } |
| 1476 | |
| 1477 | if (!empty($criteria['fetchEventsTickets'])) { |
| 1478 | $ticketsTable = EventsTicketsTable::getTableName(); |
| 1479 | |
| 1480 | $fields .= ' |
| 1481 | eti.id AS ticket_id, |
| 1482 | eti.name AS ticket_name, |
| 1483 | eti.enabled AS ticket_enabled, |
| 1484 | eti.price AS ticket_price, |
| 1485 | eti.spots AS ticket_spots, |
| 1486 | eti.waitingListSpots AS ticket_waiting_list_spots, |
| 1487 | eti.dateRanges AS ticket_dateRanges, |
| 1488 | eti.translations AS ticket_translations, |
| 1489 | '; |
| 1490 | |
| 1491 | $joins .= " |
| 1492 | LEFT JOIN {$ticketsTable} eti ON eti.eventId = e.id |
| 1493 | "; |
| 1494 | } |
| 1495 | |
| 1496 | if (!empty($criteria['fetchEventsTags'])) { |
| 1497 | $tagsTable = EventsTagsTable::getTableName(); |
| 1498 | |
| 1499 | $fields .= ' |
| 1500 | eta.id AS event_tagId, |
| 1501 | eta.name AS event_tagName, |
| 1502 | '; |
| 1503 | |
| 1504 | $joins .= " |
| 1505 | LEFT JOIN {$tagsTable} eta ON eta.eventId = e.id |
| 1506 | "; |
| 1507 | } |
| 1508 | |
| 1509 | if (!empty($criteria['fetchEventsImages'])) { |
| 1510 | $galleriesTable = GalleriesTable::getTableName(); |
| 1511 | |
| 1512 | $fields .= ' |
| 1513 | eg.id AS gallery_id, |
| 1514 | eg.pictureFullPath AS gallery_picture_full, |
| 1515 | eg.pictureThumbPath AS gallery_picture_thumb, |
| 1516 | eg.position AS gallery_position, |
| 1517 | '; |
| 1518 | |
| 1519 | $joins .= " |
| 1520 | LEFT JOIN {$galleriesTable} eg ON eg.entityId = e.id AND eg.entityType = 'event' |
| 1521 | "; |
| 1522 | } |
| 1523 | |
| 1524 | if (!empty($criteria['fetchEventsProviders'])) { |
| 1525 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 1526 | |
| 1527 | $usersTable = UsersTable::getTableName(); |
| 1528 | |
| 1529 | $joins .= " |
| 1530 | LEFT JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 1531 | LEFT JOIN {$usersTable} pu ON pu.id = epr.userId |
| 1532 | "; |
| 1533 | |
| 1534 | $fields .= ' |
| 1535 | pu.id AS provider_id, |
| 1536 | pu.firstName AS provider_firstName, |
| 1537 | pu.lastName AS provider_lastName, |
| 1538 | pu.email AS provider_email, |
| 1539 | pu.note AS provider_note, |
| 1540 | pu.description AS provider_description, |
| 1541 | pu.phone AS provider_phone, |
| 1542 | pu.gender AS provider_gender, |
| 1543 | pu.pictureFullPath AS provider_pictureFullPath, |
| 1544 | pu.pictureThumbPath AS provider_pictureThumbPath, |
| 1545 | pu.translations AS provider_translations, |
| 1546 | pu.timeZone AS provider_timeZone, |
| 1547 | '; |
| 1548 | } |
| 1549 | |
| 1550 | $fields .= " |
| 1551 | e.id AS event_id, |
| 1552 | e.name AS event_name, |
| 1553 | e.status AS event_status, |
| 1554 | e.bookingOpens AS event_bookingOpens, |
| 1555 | e.bookingCloses AS event_bookingCloses, |
| 1556 | e.bookingOpensRec AS event_bookingOpensRec, |
| 1557 | e.bookingClosesRec AS event_bookingClosesRec, |
| 1558 | e.ticketRangeRec AS event_ticketRangeRec, |
| 1559 | e.recurringCycle AS event_recurringCycle, |
| 1560 | e.recurringOrder AS event_recurringOrder, |
| 1561 | e.recurringInterval AS event_recurringInterval, |
| 1562 | e.recurringMonthly AS event_recurringMonthly, |
| 1563 | e.monthlyDate AS event_monthlyDate, |
| 1564 | e.monthlyOnRepeat AS event_monthlyOnRepeat, |
| 1565 | e.monthlyOnDay AS event_monthlyOnDay, |
| 1566 | e.recurringUntil AS event_recurringUntil, |
| 1567 | e.bringingAnyone AS event_bringingAnyone, |
| 1568 | e.bookMultipleTimes AS event_bookMultipleTimes, |
| 1569 | e.maxCapacity AS event_maxCapacity, |
| 1570 | e.maxCustomCapacity AS event_maxCustomCapacity, |
| 1571 | e.maxExtraPeople AS event_maxExtraPeople, |
| 1572 | e.price AS event_price, |
| 1573 | e.description AS event_description, |
| 1574 | e.color AS event_color, |
| 1575 | e.show AS event_show, |
| 1576 | e.notifyParticipants AS event_notifyParticipants, |
| 1577 | e.locationId AS event_locationId, |
| 1578 | e.customLocation AS event_customLocation, |
| 1579 | e.parentId AS event_parentId, |
| 1580 | e.created AS event_created, |
| 1581 | e.settings AS event_settings, |
| 1582 | e.zoomUserId AS event_zoomUserId, |
| 1583 | e.organizerId AS event_organizerId, |
| 1584 | e.translations AS event_translations, |
| 1585 | e.deposit AS event_deposit, |
| 1586 | e.depositPayment AS event_depositPayment, |
| 1587 | e.depositPerPerson AS event_depositPerPerson, |
| 1588 | e.fullPayment AS event_fullPayment, |
| 1589 | e.customPricing AS event_customPricing, |
| 1590 | e.closeAfterMin AS event_closeAfterMin, |
| 1591 | e.closeAfterMinBookings AS event_closeAfterMinBookings, |
| 1592 | e.aggregatedPrice AS event_aggregatedPrice |
| 1593 | "; |
| 1594 | |
| 1595 | if (!empty($ids)) { |
| 1596 | $queryIds = []; |
| 1597 | |
| 1598 | foreach ($ids as $index => $value) { |
| 1599 | $param = ':id' . $index; |
| 1600 | |
| 1601 | $queryIds[] = $param; |
| 1602 | |
| 1603 | $params[$param] = $value; |
| 1604 | } |
| 1605 | |
| 1606 | $where[] = 'e.id IN (' . implode(', ', $queryIds) . ')'; |
| 1607 | } |
| 1608 | |
| 1609 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 1610 | |
| 1611 | try { |
| 1612 | $statement = $this->connection->prepare( |
| 1613 | "SELECT |
| 1614 | {$fields} |
| 1615 | FROM {$this->table} e |
| 1616 | {$joins} |
| 1617 | {$where} |
| 1618 | {$orderBy}" |
| 1619 | ); |
| 1620 | |
| 1621 | $statement->execute($params); |
| 1622 | |
| 1623 | $rows = $statement->fetchAll(); |
| 1624 | } catch (\Exception $e) { |
| 1625 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 1626 | } |
| 1627 | |
| 1628 | return call_user_func([static::FACTORY, 'createCollection'], $rows); |
| 1629 | } |
| 1630 | |
| 1631 | /** |
| 1632 | * @param array $criteria |
| 1633 | * |
| 1634 | * @return Collection |
| 1635 | * @throws QueryExecutionException |
| 1636 | * @throws InvalidArgumentException |
| 1637 | */ |
| 1638 | public function getBookingsByCriteria($criteria = []) |
| 1639 | { |
| 1640 | $params = []; |
| 1641 | |
| 1642 | $where = []; |
| 1643 | |
| 1644 | $fields = ''; |
| 1645 | |
| 1646 | $joins = ''; |
| 1647 | |
| 1648 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 1649 | |
| 1650 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 1651 | |
| 1652 | $customerBookingsTable = CustomerBookingsTable::getTableName(); |
| 1653 | |
| 1654 | if (!empty($criteria['fetchApprovedBookings'])) { |
| 1655 | $where[] = "cb.status = 'approved'"; |
| 1656 | } |
| 1657 | |
| 1658 | if (!empty($criteria['customerBookingId'])) { |
| 1659 | $params[':customerBookingId'] = $criteria['customerBookingId']; |
| 1660 | |
| 1661 | $where[] = 'cb.id = :customerBookingId'; |
| 1662 | } |
| 1663 | |
| 1664 | if (!empty($criteria['fetchBookingsPayments'])) { |
| 1665 | $paymentsTable = PaymentsTable::getTableName(); |
| 1666 | |
| 1667 | $fields .= ' |
| 1668 | p.id AS payment_id, |
| 1669 | p.amount AS payment_amount, |
| 1670 | p.dateTime AS payment_dateTime, |
| 1671 | p.created AS payment_created, |
| 1672 | p.status AS payment_status, |
| 1673 | p.gateway AS payment_gateway, |
| 1674 | p.gatewayTitle AS payment_gatewayTitle, |
| 1675 | p.transactionId AS payment_transactionId, |
| 1676 | p.data AS payment_data, |
| 1677 | p.wcOrderId AS payment_wcOrderId, |
| 1678 | p.wcOrderItemId AS payment_wcOrderItemId, |
| 1679 | p.invoiceNumber AS payment_invoiceNumber, |
| 1680 | '; |
| 1681 | |
| 1682 | $joins .= " |
| 1683 | LEFT JOIN {$paymentsTable} p ON p.customerBookingId = cb.id |
| 1684 | "; |
| 1685 | } |
| 1686 | |
| 1687 | if (!empty($criteria['fetchBookingsCoupons'])) { |
| 1688 | $couponsTable = CouponsTable::getTableName(); |
| 1689 | |
| 1690 | $fields .= ' |
| 1691 | c.id AS coupon_id, |
| 1692 | c.code AS coupon_code, |
| 1693 | c.discount AS coupon_discount, |
| 1694 | c.deduction AS coupon_deduction, |
| 1695 | c.limit AS coupon_limit, |
| 1696 | c.customerLimit AS coupon_customerLimit, |
| 1697 | c.status AS coupon_status, |
| 1698 | '; |
| 1699 | |
| 1700 | $joins .= " |
| 1701 | LEFT JOIN {$couponsTable} c ON c.id = cb.couponId |
| 1702 | "; |
| 1703 | } |
| 1704 | |
| 1705 | if (!empty($criteria['fetchBookingsUsers'])) { |
| 1706 | $usersTable = UsersTable::getTableName(); |
| 1707 | |
| 1708 | $fields .= ' |
| 1709 | cu.id AS customer_id, |
| 1710 | cu.type AS customer_type, |
| 1711 | cu.firstName AS customer_firstName, |
| 1712 | cu.lastName AS customer_lastName, |
| 1713 | cu.email AS customer_email, |
| 1714 | cu.note AS customer_note, |
| 1715 | cu.phone AS customer_phone, |
| 1716 | cu.gender AS customer_gender, |
| 1717 | cu.birthday AS customer_birthday, |
| 1718 | '; |
| 1719 | |
| 1720 | $joins .= " |
| 1721 | INNER JOIN {$usersTable} cu ON cu.id = cb.customerId |
| 1722 | "; |
| 1723 | } |
| 1724 | |
| 1725 | if (!empty($criteria['fetchBookingsTickets'])) { |
| 1726 | $bookingsTicketsTable = CustomerBookingToEventsTicketsTable::getTableName(); |
| 1727 | |
| 1728 | $fields .= ' |
| 1729 | cbt.id AS booking_ticket_id, |
| 1730 | cbt.eventTicketId AS booking_ticket_eventTicketId, |
| 1731 | cbt.price AS booking_ticket_price, |
| 1732 | cbt.persons AS booking_ticket_persons, |
| 1733 | '; |
| 1734 | |
| 1735 | $joins .= " |
| 1736 | LEFT JOIN {$bookingsTicketsTable} cbt ON cbt.customerBookingId = cb.id |
| 1737 | "; |
| 1738 | } |
| 1739 | |
| 1740 | $fields .= ' |
| 1741 | ep.eventId AS eventId, |
| 1742 | cb.id AS booking_id, |
| 1743 | cb.appointmentId AS booking_appointmentId, |
| 1744 | cb.customerId AS booking_customerId, |
| 1745 | cb.status AS booking_status, |
| 1746 | cb.price AS booking_price, |
| 1747 | cb.tax AS booking_tax, |
| 1748 | cb.persons AS booking_persons, |
| 1749 | cb.couponId AS booking_couponId, |
| 1750 | cb.customFields AS booking_customFields, |
| 1751 | cb.info AS booking_info, |
| 1752 | cb.utcOffset AS booking_utcOffset, |
| 1753 | cb.token AS booking_token, |
| 1754 | cb.aggregatedPrice AS booking_aggregatedPrice, |
| 1755 | cb.tax AS booking_tax |
| 1756 | '; |
| 1757 | |
| 1758 | if (!empty($criteria['ids'])) { |
| 1759 | $queryIds = []; |
| 1760 | |
| 1761 | foreach ($criteria['ids'] as $index => $value) { |
| 1762 | $param = ':id' . $index; |
| 1763 | |
| 1764 | $queryIds[] = $param; |
| 1765 | |
| 1766 | $params[$param] = $value; |
| 1767 | } |
| 1768 | |
| 1769 | $where[] = 'ep.eventId IN (' . implode(', ', $queryIds) . ')'; |
| 1770 | } |
| 1771 | |
| 1772 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 1773 | |
| 1774 | try { |
| 1775 | $statement = $this->connection->prepare( |
| 1776 | "SELECT |
| 1777 | {$fields} |
| 1778 | FROM {$eventsPeriodsTable} ep |
| 1779 | INNER JOIN {$customerBookingsEventsPeriods} cbe ON cbe.eventPeriodId = ep.id |
| 1780 | INNER JOIN {$customerBookingsTable} cb ON cb.id = cbe.customerBookingId |
| 1781 | {$joins} |
| 1782 | {$where} |
| 1783 | ORDER BY cb.id" |
| 1784 | ); |
| 1785 | |
| 1786 | $statement->execute($params); |
| 1787 | |
| 1788 | $rows = $statement->fetchAll(); |
| 1789 | } catch (\Exception $e) { |
| 1790 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 1791 | } |
| 1792 | |
| 1793 | $reformattedData = []; |
| 1794 | |
| 1795 | foreach ($rows as $row) { |
| 1796 | if (empty($reformattedData[$row['eventId']])) { |
| 1797 | $reformattedData[$row['eventId']] = []; |
| 1798 | } |
| 1799 | |
| 1800 | $reformattedData[$row['eventId']][] = $row; |
| 1801 | } |
| 1802 | |
| 1803 | $result = new Collection(); |
| 1804 | |
| 1805 | foreach ($reformattedData as $eventId => $bookingsData) { |
| 1806 | $reformattedBookingsData = CustomerBookingFactory::reformat($bookingsData); |
| 1807 | |
| 1808 | $eventBookings = new Collection(); |
| 1809 | |
| 1810 | foreach ($reformattedBookingsData as $bookingId => $data) { |
| 1811 | $eventBookings->addItem(CustomerBookingFactory::create($data), $bookingId); |
| 1812 | } |
| 1813 | |
| 1814 | $result->addItem($eventBookings, $eventId); |
| 1815 | } |
| 1816 | |
| 1817 | return $result; |
| 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | /** |
| 1822 | * @param Event $event |
| 1823 | * @param array $booking |
| 1824 | * @param array $limitPerCustomer |
| 1825 | * @return int |
| 1826 | * @throws QueryExecutionException |
| 1827 | * @throws InvalidArgumentException |
| 1828 | */ |
| 1829 | public function getRelevantBookingsCount($event, $booking, $limitPerCustomer) |
| 1830 | { |
| 1831 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 1832 | |
| 1833 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 1834 | |
| 1835 | $customerBookingsTable = CustomerBookingsTable::getTableName(); |
| 1836 | |
| 1837 | $params = [ |
| 1838 | ':customerId' => $booking['customerId'] |
| 1839 | ]; |
| 1840 | |
| 1841 | $paymentTableJoin = ''; |
| 1842 | $compareToDate = 'ep.periodStart'; |
| 1843 | |
| 1844 | if ($limitPerCustomer['from'] === 'bookingDate') { |
| 1845 | $eventStartDate = (clone $event->getPeriods()->getItems()[0]->getPeriodStart()->getValue())->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i'); |
| 1846 | } else { |
| 1847 | $paymentTableJoin = 'INNER JOIN ' . PaymentsTable::getTableName() . ' p ON p.customerBookingId = cb.id'; |
| 1848 | $eventStartDate = DateTimeService::getNowDateTimeObject()->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i'); |
| 1849 | $compareToDate = 'p.created'; |
| 1850 | } |
| 1851 | |
| 1852 | $intervalString = "interval " . $limitPerCustomer['period'] . " " . $limitPerCustomer['timeFrame']; |
| 1853 | |
| 1854 | $where = "(STR_TO_DATE('". $eventStartDate ."', '%Y-%m-%d %H:%i:%s') BETWEEN " . |
| 1855 | "(" . $compareToDate . " - " . $intervalString . " + interval 1 second)" . |
| 1856 | " AND (". |
| 1857 | $compareToDate . " + " . $intervalString . " - interval 1 second))"; |
| 1858 | |
| 1859 | try { |
| 1860 | $statement = $this->connection->prepare( |
| 1861 | "SELECT COUNT(DISTINCT cb.id) AS count FROM |
| 1862 | {$this->table} e |
| 1863 | INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 1864 | INNER JOIN {$customerBookingsEventsPeriods} cbep ON cbep.eventPeriodId = ep.id |
| 1865 | INNER JOIN {$customerBookingsTable} cb ON cb.id = cbep.customerBookingId |
| 1866 | {$paymentTableJoin} |
| 1867 | WHERE cb.customerId = :customerId AND {$where} AND e.status = 'approved' AND cb.status = 'approved' |
| 1868 | " |
| 1869 | ); |
| 1870 | |
| 1871 | $statement->execute($params); |
| 1872 | |
| 1873 | $rows = $statement->fetch()['count']; |
| 1874 | } catch (\Exception $e) { |
| 1875 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 1876 | } |
| 1877 | |
| 1878 | return $rows; |
| 1879 | } |
| 1880 | } |
| 1881 |