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