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