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