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