PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.1.3
Booking for Appointments and Events Calendar – Amelia v2.1.3
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 / Tax / TaxRepository.php
ameliabooking / src / Infrastructure / Repository / Tax Last commit date
TaxEntityRepository.php 5 months ago TaxRepository.php 5 months ago
TaxRepository.php
528 lines
1 <?php
2
3 /**
4 * @copyright © Melograno Ventures. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace AmeliaBooking\Infrastructure\Repository\Tax;
9
10 use AmeliaBooking\Domain\Collection\Collection;
11 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
12 use AmeliaBooking\Domain\Entity\Booking\Event\Event;
13 use AmeliaBooking\Domain\Entity\Tax\Tax;
14 use AmeliaBooking\Domain\Factory\Booking\Event\EventFactory;
15 use AmeliaBooking\Domain\Factory\Tax\TaxFactory;
16 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
17 use AmeliaBooking\Infrastructure\Connection;
18 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
19 use AmeliaBooking\Infrastructure\Repository\AbstractRepository;
20 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\PackagesTable;
21 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\ServicesTable;
22 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsTable;
23 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Tax\TaxesToEntitiesTable;
24
25 /**
26 * Class TaxRepository
27 *
28 * @package AmeliaBooking\Infrastructure\Repository\Tax
29 */
30 class TaxRepository extends AbstractRepository
31 {
32 public const FACTORY = TaxFactory::class;
33
34 /** @var string */
35 protected $taxesToEntitiesTable;
36
37 /**
38 * @param Connection $connection
39 * @param string $table
40 * @throws InvalidArgumentException
41 */
42 public function __construct(
43 Connection $connection,
44 $table
45 ) {
46 parent::__construct($connection, $table);
47
48 $this->taxesToEntitiesTable = TaxesToEntitiesTable::getTableName();
49 }
50
51 /**
52 * @param Tax $entity
53 *
54 * @return int
55 * @throws QueryExecutionException
56 */
57 public function add($entity)
58 {
59 $data = $entity->toArray();
60
61 $params = [
62 ':name' => $data['name'],
63 ':amount' => $data['amount'],
64 ':type' => $data['type'],
65 ':status' => $data['status'],
66 ':allServices' => !empty($data['allServices']) ? 1 : 0,
67 ':allEvents' => !empty($data['allEvents']) ? 1 : 0,
68 ':allPackages' => !empty($data['allPackages']) ? 1 : 0,
69 ':allExtras' => !empty($data['allExtras']) ? 1 : 0,
70 ];
71
72 try {
73 $statement = $this->connection->prepare(
74 "INSERT INTO
75 {$this->table}
76 (
77 `name`, `amount`, `type`, `status`, `allServices`, `allEvents`, `allPackages`, `allExtras`
78 ) VALUES (
79 :name, :amount, :type, :status, :allServices, :allEvents, :allPackages, :allExtras
80 )"
81 );
82
83
84 $statement->execute($params);
85 } catch (\Exception $e) {
86 throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
87 }
88
89 return $this->connection->lastInsertId();
90 }
91
92 /**
93 * @param int $id
94 * @param Tax $entity
95 *
96 * @return bool
97 * @throws QueryExecutionException
98 */
99 public function update($id, $entity)
100 {
101 $data = $entity->toArray();
102
103 $params = [
104 ':name' => $data['name'],
105 ':amount' => $data['amount'],
106 ':type' => $data['type'],
107 ':status' => $data['status'],
108 ':allServices' => !empty($data['allServices']) ? 1 : 0,
109 ':allEvents' => !empty($data['allEvents']) ? 1 : 0,
110 ':allPackages' => !empty($data['allPackages']) ? 1 : 0,
111 ':allExtras' => !empty($data['allExtras']) ? 1 : 0,
112 ':id' => $id,
113 ];
114
115 try {
116 $statement = $this->connection->prepare(
117 "UPDATE {$this->table}
118 SET
119 `name` = :name,
120 `amount` = :amount,
121 `type` = :type,
122 `status` = :status,
123 `allServices` = :allServices,
124 `allEvents` = :allEvents,
125 `allPackages` = :allPackages,
126 `allExtras` = :allExtras
127 WHERE
128 id = :id"
129 );
130
131 $statement->execute($params);
132 } catch (\Exception $e) {
133 throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
134 }
135
136 return true;
137 }
138
139 /**
140 * @param int $id
141 *
142 * @return Tax
143 * @throws QueryExecutionException
144 * @throws NotFoundException
145 * @throws InvalidArgumentException
146 */
147 public function getById($id)
148 {
149 try {
150 $statement = $this->connection->prepare(
151 "SELECT
152 t.id AS tax_id,
153 t.name AS tax_name,
154 t.amount AS tax_amount,
155 t.type AS tax_type,
156 t.status AS tax_status,
157 t.allServices AS tax_allServices,
158 t.allEvents AS tax_allEvents,
159 t.allPackages AS tax_allPackages,
160 t.allExtras AS tax_allExtras,
161 te.entityId AS tax_entityId,
162 te.entityType AS tax_entityType
163 FROM {$this->table} t
164 LEFT JOIN {$this->taxesToEntitiesTable} te ON te.taxId = t.id AND te.entityType != 'event'
165 WHERE t.id = :taxId"
166 );
167
168 $statement->bindParam(':taxId', $id);
169
170 $statement->execute();
171
172 $rows = $statement->fetchAll();
173 } catch (\Exception $e) {
174 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
175 }
176
177 if (!$rows) {
178 throw new NotFoundException('Data not found in ' . __CLASS__);
179 }
180
181 $eventsTable = EventsTable::getTableName();
182
183 /** @var Tax $tax */
184 $tax = call_user_func([static::FACTORY, 'createCollection'], $rows)->getItem($id);
185
186 $statement = $this->connection->prepare(
187 "SELECT
188 e.id AS id,
189 e.name AS name
190 FROM {$eventsTable} e
191 INNER JOIN {$this->taxesToEntitiesTable} te ON te.entityId = e.id AND te.entityType = 'event'
192 WHERE te.taxId = :taxId"
193 );
194
195 $statement->bindParam(':taxId', $id);
196
197 $statement->execute();
198
199 $rows = $statement->fetchAll();
200
201 $tax->setEventList(new Collection());
202
203 foreach ($rows as $row) {
204 $tax->getEventList()->addItem(EventFactory::create($row));
205 }
206
207 return $tax;
208 }
209
210 /**
211 * @param array $criteria
212 *
213 * @return Collection
214 * @throws QueryExecutionException
215 * @throws InvalidArgumentException
216 */
217 public function getWithEntities($criteria)
218 {
219 $where = !empty($criteria['ids']) ? "WHERE t.id IN (" . implode(', ', $criteria['ids']) . ")" : '';
220
221 try {
222 $statement = $this->connection->prepare(
223 "SELECT
224 t.id AS tax_id,
225 t.name AS tax_name,
226 t.amount AS tax_amount,
227 t.type AS tax_type,
228 t.status AS tax_status,
229 t.allServices AS tax_allServices,
230 t.allEvents AS tax_allEvents,
231 t.allPackages AS tax_allPackages,
232 t.allExtras AS tax_allExtras,
233 te.entityId AS tax_entityId,
234 te.entityType AS tax_entityType
235 FROM {$this->table} t
236 LEFT JOIN {$this->taxesToEntitiesTable} te ON te.taxId = t.id
237 {$where}
238 ORDER BY t.id"
239 );
240
241 $statement->execute();
242
243 $rows = $statement->fetchAll();
244 } catch (\Exception $e) {
245 throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
246 }
247
248 /** @var Collection $taxes */
249 $taxes = call_user_func([static::FACTORY, 'createCollection'], $rows);
250
251 $taxesIds = array_column($taxes->toArray(), 'id');
252
253 if ($taxesIds && !empty($criteria['events'])) {
254 $eventsTable = EventsTable::getTableName();
255
256 $statement = $this->connection->prepare(
257 "SELECT
258 e.id AS id,
259 e.name AS name
260 FROM {$this->taxesToEntitiesTable} te
261 INNER JOIN {$eventsTable} e ON te.entityId = e.id AND te.entityType = 'event'
262 WHERE te.taxId IN (" . implode(', ', $taxesIds) . ")"
263 );
264
265 $statement->execute();
266
267 $rows = $statement->fetchAll();
268
269 /** @var Collection $events */
270 $events = new Collection();
271
272 foreach ($rows as $row) {
273 if (!$events->keyExists($row['id'])) {
274 $events->addItem(EventFactory::create($row), $row['id']);
275 }
276 }
277
278 /** @var Tax $tax */
279 foreach ($taxes->getItems() as $tax) {
280 /** @var Tax $taxEvent */
281 foreach ($tax->getEventList()->getItems() as $taxEvent) {
282 if ($events->keyExists($taxEvent->getId()->getValue())) {
283 /** @var Event $event */
284 $event = $events->getItem($taxEvent->getId()->getValue());
285
286 $taxEvent->setName($event->getName());
287 }
288 }
289 }
290 }
291
292 return $taxes;
293 }
294
295 /**
296 * @param array $criteria
297 * @param int $itemsPerPage
298 *
299 * @return Collection
300 * @throws QueryExecutionException
301 */
302 public function getFiltered($criteria, $itemsPerPage)
303 {
304 $params = [];
305
306 $where = [];
307
308 if (!empty($criteria['search'])) {
309 $params[':search'] = "%{$criteria['search']}%";
310
311 $where[] = 'UPPER(t.name) LIKE UPPER(:search)';
312 }
313
314 if (!empty($criteria['services'])) {
315 $queryServices = [];
316
317 foreach ($criteria['services'] as $index => $value) {
318 $param = ':service' . $index;
319
320 $queryServices[] = $param;
321
322 $params[$param] = $value;
323 }
324
325 $where[] = "(t.id IN (
326 SELECT taxId FROM {$this->taxesToEntitiesTable}
327 WHERE entityId IN (" . implode(', ', $queryServices) . ") AND entityType = 'service'
328 ) OR t.allServices = 1)";
329 }
330
331 if (!empty($criteria['extras'])) {
332 $queryExtras = [];
333
334 foreach ($criteria['extras'] as $index => $value) {
335 $param = ':extra' . $index;
336
337 $queryExtras[] = $param;
338
339 $params[$param] = $value;
340 }
341
342 $where[] = "(t.id IN (
343 SELECT taxId FROM {$this->taxesToEntitiesTable}
344 WHERE entityId IN (" . implode(', ', $queryExtras) . ") AND entityType = 'extra'
345 ) OR t.allExtras = 1)";
346 }
347
348 if (!empty($criteria['events'])) {
349 $queryEvents = [];
350
351 foreach ($criteria['events'] as $index => $value) {
352 $param = ':event' . $index;
353
354 $queryEvents[] = $param;
355
356 $params[$param] = $value;
357 }
358
359 $where[] = "(t.id IN (
360 SELECT taxId FROM {$this->taxesToEntitiesTable}
361 WHERE entityId IN (" . implode(', ', $queryEvents) . ") AND entityType = 'event'
362 ) OR t.allEvents = 1)";
363 }
364
365 if (!empty($criteria['packages'])) {
366 $queryPackages = [];
367
368 foreach ((array)$criteria['packages'] as $index => $value) {
369 $param = ':package' . $index;
370
371 $queryPackages[] = $param;
372
373 $params[$param] = $value;
374 }
375
376 $where[] = "(t.id IN (
377 SELECT taxId FROM {$this->taxesToEntitiesTable}
378 WHERE entityId IN (" . implode(', ', $queryPackages) . ") AND entityType = 'package'
379 ) OR t.allPackages = 1)";
380 }
381
382
383 $where = $where ? ' WHERE ' . implode(' AND ', $where) : '';
384
385 $limit = $this->getLimit(
386 !empty($criteria['page']) ? (int)$criteria['page'] : 0,
387 (int)$itemsPerPage
388 );
389
390 $order = "ORDER BY id";
391 if (!empty($criteria['sort'])) {
392 $order = "ORDER BY {$criteria['sort']['field']} {$criteria['sort']['order']}";
393 }
394
395 try {
396 $statement = $this->connection->prepare(
397 "SELECT
398 t.id AS tax_id,
399 t.name AS tax_name,
400 t.amount AS tax_amount,
401 t.type AS tax_type,
402 t.status AS tax_status,
403 t.allServices AS tax_allServices,
404 t.allEvents AS tax_allEvents,
405 t.allPackages AS tax_allPackages,
406 t.allExtras AS tax_allExtras
407 FROM {$this->table} t
408 {$where}
409 {$order}
410 {$limit}"
411 );
412
413 $statement->execute($params);
414
415 $rows = $statement->fetchAll();
416 } catch (\Exception $e) {
417 throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
418 }
419
420 return call_user_func([static::FACTORY, 'createCollection'], $rows);
421 }
422
423 /**
424 * @param array $criteria
425 *
426 * @return mixed
427 * @throws QueryExecutionException
428 */
429 public function getCount($criteria)
430 {
431 $params = [];
432
433 $where = [];
434
435 if (!empty($criteria['search'])) {
436 $params[':search'] = "%{$criteria['search']}%";
437
438 $where[] = 't.name LIKE :search';
439 }
440
441 if (!empty($criteria['services'])) {
442 $queryServices = [];
443
444 foreach ((array)$criteria['services'] as $index => $value) {
445 $param = ':service' . $index;
446
447 $queryServices[] = $param;
448
449 $params[$param] = $value;
450 }
451
452 $where[] = "(t.id IN (
453 SELECT taxId FROM {$this->taxesToEntitiesTable}
454 WHERE entityId IN (" . implode(', ', $queryServices) . ") AND entityType = 'service'
455 ) OR t.allServices = 1)";
456 }
457
458 if (!empty($criteria['extras'])) {
459 $queryExtras = [];
460
461 foreach ($criteria['extras'] as $index => $value) {
462 $param = ':extra' . $index;
463
464 $queryExtras[] = $param;
465
466 $params[$param] = $value;
467 }
468
469 $where[] = "(t.id IN (
470 SELECT taxId FROM {$this->taxesToEntitiesTable}
471 WHERE entityId IN (" . implode(', ', $queryExtras) . ") AND entityType = 'extra'
472 ) OR t.allExtras = 1)";
473 }
474
475 if (!empty($criteria['events'])) {
476 $queryEvents = [];
477
478 foreach ((array)$criteria['events'] as $index => $value) {
479 $param = ':event' . $index;
480
481 $queryEvents[] = $param;
482
483 $params[$param] = $value;
484 }
485
486 $where[] = "(t.id IN (
487 SELECT taxId FROM {$this->taxesToEntitiesTable}
488 WHERE entityId IN (" . implode(', ', $queryEvents) . ") AND entityType = 'event'
489 ) OR t.allEvents = 1)";
490 }
491
492 if (!empty($criteria['packages'])) {
493 $queryPackages = [];
494
495 foreach ((array)$criteria['packages'] as $index => $value) {
496 $param = ':package' . $index;
497
498 $queryPackages[] = $param;
499
500 $params[$param] = $value;
501 }
502
503 $where[] = "(t.id IN (
504 SELECT taxId FROM {$this->taxesToEntitiesTable}
505 WHERE entityId IN (" . implode(', ', $queryPackages) . ") AND entityType = 'package'
506 ) OR t.allPackages = 1)";
507 }
508
509 $where = $where ? ' WHERE ' . implode(' AND ', $where) : '';
510
511 try {
512 $statement = $this->connection->prepare(
513 "SELECT COUNT(*) AS count
514 FROM {$this->table} t
515 {$where}"
516 );
517
518 $statement->execute($params);
519
520 $row = $statement->fetch()['count'];
521 } catch (\Exception $e) {
522 throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
523 }
524
525 return $row;
526 }
527 }
528