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