CategoryRepository.php
2 years ago
ExtraRepository.php
5 years ago
PackageCustomerRepository.php
1 year ago
PackageCustomerServiceRepository.php
1 year ago
PackageRepository.php
2 years ago
PackageServiceLocationRepository.php
5 years ago
PackageServiceProviderRepository.php
5 years ago
PackageServiceRepository.php
2 years ago
ProviderServiceRepository.php
1 year ago
ResourceEntitiesRepository.php
2 years ago
ResourceRepository.php
2 years ago
ServiceRepository.php
2 years ago
PackageRepository.php
528 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\Bookable\Service; |
| 8 | |
| 9 | use AmeliaBooking\Domain\Collection\Collection; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 11 | use AmeliaBooking\Domain\Entity\Bookable\Service\Package; |
| 12 | use AmeliaBooking\Domain\Factory\Bookable\Service\PackageFactory; |
| 13 | use AmeliaBooking\Infrastructure\Connection; |
| 14 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 15 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 16 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\PackagesServicesLocationsTable; |
| 17 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\PackagesServicesProvidersTable; |
| 18 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\PackagesServicesTable; |
| 19 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\ServicesTable; |
| 20 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Gallery\GalleriesTable; |
| 21 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Location\LocationsTable; |
| 22 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\UsersTable; |
| 23 | |
| 24 | /** |
| 25 | * Class PackageRepository |
| 26 | * |
| 27 | * @package AmeliaBooking\Infrastructure\Repository\Service |
| 28 | */ |
| 29 | class PackageRepository extends AbstractRepository |
| 30 | { |
| 31 | const FACTORY = PackageFactory::class; |
| 32 | |
| 33 | /** |
| 34 | * @param Connection $connection |
| 35 | * @param string $table |
| 36 | */ |
| 37 | public function __construct( |
| 38 | Connection $connection, |
| 39 | $table |
| 40 | ) { |
| 41 | parent::__construct($connection, $table); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param Package $entity |
| 46 | * |
| 47 | * @return bool |
| 48 | * @throws QueryExecutionException |
| 49 | */ |
| 50 | public function add($entity) |
| 51 | { |
| 52 | $data = $entity->toArray(); |
| 53 | |
| 54 | $params = [ |
| 55 | ':name' => $data['name'], |
| 56 | ':description' => $data['description'], |
| 57 | ':color' => $data['color'], |
| 58 | ':price' => $data['price'], |
| 59 | ':status' => $data['status'], |
| 60 | ':pictureFullPath' => $data['pictureFullPath'], |
| 61 | ':pictureThumbPath' => $data['pictureThumbPath'], |
| 62 | ':position' => $data['position'], |
| 63 | ':calculatedPrice' => $data['calculatedPrice'] ? 1 : 0, |
| 64 | ':discount' => $data['discount'], |
| 65 | ':settings' => $data['settings'], |
| 66 | ':endDate' => $data['endDate'], |
| 67 | ':durationCount' => $data['durationCount'], |
| 68 | ':durationType' => $data['durationType'], |
| 69 | ':translations' => $data['translations'], |
| 70 | ':deposit' => $data['deposit'], |
| 71 | ':depositPayment' => $data['depositPayment'], |
| 72 | ':fullPayment' => $data['fullPayment'] ? 1 : 0, |
| 73 | ':sharedCapacity' => $data['sharedCapacity'] ? 1 : 0, |
| 74 | ':quantity' => $data['quantity'], |
| 75 | ':limitPerCustomer' => $data['limitPerCustomer'] |
| 76 | ]; |
| 77 | |
| 78 | try { |
| 79 | $statement = $this->connection->prepare( |
| 80 | "INSERT INTO |
| 81 | {$this->table} |
| 82 | ( |
| 83 | `name`, |
| 84 | `description`, |
| 85 | `color`, |
| 86 | `price`, |
| 87 | `status`, |
| 88 | `pictureFullPath`, |
| 89 | `pictureThumbPath`, |
| 90 | `calculatedPrice`, |
| 91 | `discount`, |
| 92 | `position`, |
| 93 | `settings`, |
| 94 | `endDate`, |
| 95 | `durationCount`, |
| 96 | `durationType`, |
| 97 | `translations`, |
| 98 | `deposit`, |
| 99 | `depositPayment`, |
| 100 | `fullPayment`, |
| 101 | `sharedCapacity`, |
| 102 | `quantity`, |
| 103 | `limitPerCustomer` |
| 104 | ) VALUES ( |
| 105 | :name, |
| 106 | :description, |
| 107 | :color, |
| 108 | :price, |
| 109 | :status, |
| 110 | :pictureFullPath, |
| 111 | :pictureThumbPath, |
| 112 | :calculatedPrice, |
| 113 | :discount, |
| 114 | :position, |
| 115 | :settings, |
| 116 | :endDate, |
| 117 | :durationCount, |
| 118 | :durationType, |
| 119 | :translations, |
| 120 | :deposit, |
| 121 | :depositPayment, |
| 122 | :fullPayment, |
| 123 | :sharedCapacity, |
| 124 | :quantity, |
| 125 | :limitPerCustomer |
| 126 | )" |
| 127 | ); |
| 128 | |
| 129 | $result = $statement->execute($params); |
| 130 | |
| 131 | if (!$result) { |
| 132 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 133 | } |
| 134 | |
| 135 | return $this->connection->lastInsertId(); |
| 136 | } catch (\Exception $e) { |
| 137 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @param int $packageId |
| 143 | * @param Package $entity |
| 144 | * |
| 145 | * @throws QueryExecutionException |
| 146 | */ |
| 147 | public function update($packageId, $entity) |
| 148 | { |
| 149 | $data = $entity->toArray(); |
| 150 | |
| 151 | $params = [ |
| 152 | ':name' => $data['name'], |
| 153 | ':description' => $data['description'], |
| 154 | ':color' => $data['color'], |
| 155 | ':price' => $data['price'], |
| 156 | ':status' => $data['status'], |
| 157 | ':pictureFullPath' => $data['pictureFullPath'], |
| 158 | ':pictureThumbPath' => $data['pictureThumbPath'], |
| 159 | ':position' => $data['position'], |
| 160 | ':calculatedPrice' => $data['calculatedPrice'] ? 1 : 0, |
| 161 | ':discount' => $data['discount'], |
| 162 | ':settings' => $data['settings'], |
| 163 | ':endDate' => $data['endDate'], |
| 164 | ':durationCount' => $data['durationCount'], |
| 165 | ':durationType' => $data['durationType'], |
| 166 | ':translations' => $data['translations'], |
| 167 | ':deposit' => $data['deposit'], |
| 168 | ':depositPayment' => $data['depositPayment'], |
| 169 | ':fullPayment' => $data['fullPayment'] ? 1 : 0, |
| 170 | ':sharedCapacity' => $data['sharedCapacity'] ? 1 : 0, |
| 171 | ':quantity' => $data['quantity'], |
| 172 | ':limitPerCustomer' => $data['limitPerCustomer'], |
| 173 | ':id' => $packageId |
| 174 | ]; |
| 175 | |
| 176 | |
| 177 | try { |
| 178 | $statement = $this->connection->prepare( |
| 179 | "UPDATE {$this->table} |
| 180 | SET |
| 181 | `name` = :name, |
| 182 | `description` = :description, |
| 183 | `color` = :color, |
| 184 | `price` = :price, |
| 185 | `status` = :status, |
| 186 | `pictureFullPath` = :pictureFullPath, |
| 187 | `pictureThumbPath` = :pictureThumbPath, |
| 188 | `position` = :position, |
| 189 | `calculatedPrice` = :calculatedPrice, |
| 190 | `discount` = :discount, |
| 191 | `settings` = :settings, |
| 192 | `endDate` = :endDate, |
| 193 | `durationCount` = :durationCount, |
| 194 | `durationType` = :durationType, |
| 195 | `translations` = :translations, |
| 196 | `deposit` = :deposit, |
| 197 | `depositPayment` = :depositPayment, |
| 198 | `fullPayment` = :fullPayment, |
| 199 | `sharedCapacity` = :sharedCapacity, |
| 200 | `quantity` = :quantity, |
| 201 | `limitPerCustomer` = :limitPerCustomer |
| 202 | WHERE |
| 203 | id = :id" |
| 204 | ); |
| 205 | |
| 206 | $result = $statement->execute($params); |
| 207 | |
| 208 | if (!$result) { |
| 209 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 210 | } |
| 211 | } catch (\Exception $e) { |
| 212 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @param $criteria |
| 218 | * |
| 219 | * @return Collection |
| 220 | * @throws QueryExecutionException |
| 221 | * @throws InvalidArgumentException |
| 222 | */ |
| 223 | public function getByCriteria($criteria) |
| 224 | { |
| 225 | $params = []; |
| 226 | |
| 227 | $where = []; |
| 228 | |
| 229 | $order = 'ORDER BY p.name, ps.position ASC, ps.id ASC'; |
| 230 | |
| 231 | if (isset($criteria['sort'])) { |
| 232 | if ($criteria['sort'] === '') { |
| 233 | $order = 'ORDER BY p.position'; |
| 234 | } else { |
| 235 | $orderColumn = strpos($criteria['sort'], 'name') !== false ? 'p.name' : 'p.price'; |
| 236 | |
| 237 | $orderDirection = $criteria['sort'][0] === '-' ? 'DESC' : 'ASC'; |
| 238 | |
| 239 | $order = "ORDER BY {$orderColumn} {$orderDirection}"; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (!empty($criteria['search'])) { |
| 244 | $params[':search'] = "%{$criteria['search']}%"; |
| 245 | |
| 246 | $where[] = 'p.name LIKE :search'; |
| 247 | } |
| 248 | |
| 249 | if (!empty($criteria['services'])) { |
| 250 | $queryServices = []; |
| 251 | |
| 252 | foreach ((array)$criteria['services'] as $index => $value) { |
| 253 | $param = ':service' . $index; |
| 254 | |
| 255 | $queryServices[] = $param; |
| 256 | |
| 257 | $params[$param] = $value; |
| 258 | } |
| 259 | |
| 260 | $where[] = 's.id IN (' . implode(', ', $queryServices) . ')'; |
| 261 | } |
| 262 | |
| 263 | if (!empty($criteria['packages'])) { |
| 264 | $queryPackages = []; |
| 265 | |
| 266 | foreach ((array)$criteria['packages'] as $index => $value) { |
| 267 | $param = ':package' . $index; |
| 268 | $queryPackages[] = $param; |
| 269 | $params[$param] = $value; |
| 270 | } |
| 271 | |
| 272 | $where[] = 'p.id IN (' . implode(', ', $queryPackages) . ')'; |
| 273 | } |
| 274 | |
| 275 | if (!empty($criteria['status'])) { |
| 276 | $params[':status'] = $criteria['status']; |
| 277 | |
| 278 | $where[] = 's.status = :status'; |
| 279 | } |
| 280 | |
| 281 | $where = $where ? ' AND ' . implode(' AND ', $where) : ''; |
| 282 | |
| 283 | $servicesTable = ServicesTable::getTableName(); |
| 284 | |
| 285 | $usersTable = UsersTable::getTableName(); |
| 286 | |
| 287 | $locationsTable = LocationsTable::getTableName(); |
| 288 | |
| 289 | $packageServicesTable = PackagesServicesTable::getTableName(); |
| 290 | |
| 291 | $packageServicesProvidersTable = PackagesServicesProvidersTable::getTableName(); |
| 292 | $packageServicesLocationsTable = PackagesServicesLocationsTable::getTableName(); |
| 293 | $galleriesTable = GalleriesTable::getTableName(); |
| 294 | |
| 295 | try { |
| 296 | $statement = $this->connection->prepare( |
| 297 | "SELECT |
| 298 | p.id AS package_id, |
| 299 | p.name AS package_name, |
| 300 | p.description AS package_description, |
| 301 | p.color AS package_color, |
| 302 | p.price AS package_price, |
| 303 | p.status AS package_status, |
| 304 | p.pictureFullPath AS package_picture_full, |
| 305 | p.pictureThumbPath AS package_picture_thumb, |
| 306 | p.calculatedPrice AS package_calculated_price, |
| 307 | p.discount AS package_discount, |
| 308 | p.position AS package_position, |
| 309 | p.settings AS package_settings, |
| 310 | p.endDate AS package_endDate, |
| 311 | p.durationCount AS package_durationCount, |
| 312 | p.durationType AS package_durationType, |
| 313 | p.translations AS package_translations, |
| 314 | p.deposit AS package_deposit, |
| 315 | p.depositPayment AS package_depositPayment, |
| 316 | p.fullPayment AS package_fullPayment, |
| 317 | p.sharedCapacity AS package_sharedCapacity, |
| 318 | p.quantity AS package_quantity, |
| 319 | p.limitPerCustomer AS package_limitPerCustomer, |
| 320 | |
| 321 | ps.id AS package_service_id, |
| 322 | ps.quantity AS package_service_quantity, |
| 323 | ps.minimumScheduled AS package_service_minimumScheduled, |
| 324 | ps.maximumScheduled AS package_service_maximumScheduled, |
| 325 | ps.allowProviderSelection AS package_service_allowProviderSelection, |
| 326 | ps.position AS package_service_position, |
| 327 | |
| 328 | s.id AS service_id, |
| 329 | s.price AS service_price, |
| 330 | s.minCapacity AS service_minCapacity, |
| 331 | s.maxCapacity AS service_maxCapacity, |
| 332 | s.name AS service_name, |
| 333 | s.description AS service_description, |
| 334 | s.status AS service_status, |
| 335 | s.categoryId AS service_categoryId, |
| 336 | s.duration AS service_duration, |
| 337 | s.timeBefore AS service_timeBefore, |
| 338 | s.timeAfter AS service_timeAfter, |
| 339 | s.pictureFullPath AS service_picture_full, |
| 340 | s.pictureThumbPath AS service_picture_thumb, |
| 341 | s.translations AS service_translations, |
| 342 | s.show AS service_show, |
| 343 | |
| 344 | l.id AS location_id, |
| 345 | l.name AS location_name, |
| 346 | l.address AS location_address, |
| 347 | l.phone AS location_phone, |
| 348 | l.latitude AS location_latitude, |
| 349 | l.longitude AS location_longitude, |
| 350 | |
| 351 | pu.id AS provider_id, |
| 352 | pu.firstName AS provider_firstName, |
| 353 | pu.lastName AS provider_lastName, |
| 354 | pu.email AS provider_email, |
| 355 | pu.status AS provider_status, |
| 356 | pu.translations AS provider_translations, |
| 357 | |
| 358 | g.id AS gallery_id, |
| 359 | g.pictureFullPath AS gallery_picture_full, |
| 360 | g.pictureThumbPath AS gallery_picture_thumb, |
| 361 | g.position AS gallery_position |
| 362 | |
| 363 | FROM {$this->table} p |
| 364 | LEFT JOIN {$packageServicesTable} ps ON ps.packageId = p.id |
| 365 | LEFT JOIN {$servicesTable} s ON ps.serviceId = s.id |
| 366 | LEFT JOIN {$packageServicesProvidersTable} psp ON psp.packageServiceId = ps.id |
| 367 | LEFT JOIN {$packageServicesLocationsTable} psl ON psl.packageServiceId = ps.id |
| 368 | LEFT JOIN {$usersTable} pu ON pu.id = psp.userId |
| 369 | LEFT JOIN {$locationsTable} l ON l.id = psl.locationId |
| 370 | LEFT JOIN {$galleriesTable} g ON g.entityId = p.id AND g.entityType = 'package' |
| 371 | WHERE 1 = 1 {$where} |
| 372 | {$order}" |
| 373 | ); |
| 374 | |
| 375 | $statement->execute($params); |
| 376 | |
| 377 | $rows = $statement->fetchAll(); |
| 378 | } catch (\Exception $e) { |
| 379 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 380 | } |
| 381 | |
| 382 | return call_user_func([static::FACTORY, 'createCollection'], $rows); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * @param $id |
| 387 | * |
| 388 | * @return Collection |
| 389 | * @throws QueryExecutionException |
| 390 | * @throws InvalidArgumentException |
| 391 | */ |
| 392 | public function getById($id) |
| 393 | { |
| 394 | $params[':id'] = $id; |
| 395 | |
| 396 | $servicesTable = ServicesTable::getTableName(); |
| 397 | |
| 398 | $usersTable = UsersTable::getTableName(); |
| 399 | |
| 400 | $locationsTable = LocationsTable::getTableName(); |
| 401 | |
| 402 | $packageServicesTable = PackagesServicesTable::getTableName(); |
| 403 | |
| 404 | $packageServicesProvidersTable = PackagesServicesProvidersTable::getTableName(); |
| 405 | |
| 406 | $packageServicesLocationsTable = PackagesServicesLocationsTable::getTableName(); |
| 407 | |
| 408 | $galleriesTable = GalleriesTable::getTableName(); |
| 409 | |
| 410 | try { |
| 411 | $statement = $this->connection->prepare( |
| 412 | "SELECT |
| 413 | p.id AS package_id, |
| 414 | p.name AS package_name, |
| 415 | p.description AS package_description, |
| 416 | p.color AS package_color, |
| 417 | p.price AS package_price, |
| 418 | p.status AS package_status, |
| 419 | p.pictureFullPath AS package_picture_full, |
| 420 | p.pictureThumbPath AS package_picture_thumb, |
| 421 | p.calculatedPrice AS package_calculated_price, |
| 422 | p.discount AS package_discount, |
| 423 | p.position AS package_position, |
| 424 | p.settings AS package_settings, |
| 425 | p.endDate AS package_endDate, |
| 426 | p.durationCount AS package_durationCount, |
| 427 | p.durationType AS package_durationType, |
| 428 | p.translations AS package_translations, |
| 429 | p.deposit AS package_deposit, |
| 430 | p.depositPayment AS package_depositPayment, |
| 431 | p.fullPayment AS package_fullPayment, |
| 432 | p.sharedCapacity AS package_sharedCapacity, |
| 433 | p.quantity AS package_quantity, |
| 434 | p.limitPerCustomer AS package_limitPerCustomer, |
| 435 | |
| 436 | ps.id AS package_service_id, |
| 437 | ps.quantity AS package_service_quantity, |
| 438 | ps.minimumScheduled AS package_service_minimumScheduled, |
| 439 | ps.maximumScheduled AS package_service_maximumScheduled, |
| 440 | ps.allowProviderSelection AS package_service_allowProviderSelection, |
| 441 | ps.position AS package_service_position, |
| 442 | |
| 443 | s.id AS service_id, |
| 444 | s.price AS service_price, |
| 445 | s.minCapacity AS service_minCapacity, |
| 446 | s.maxCapacity AS service_maxCapacity, |
| 447 | s.name AS service_name, |
| 448 | s.status AS service_status, |
| 449 | s.categoryId AS service_categoryId, |
| 450 | s.duration AS service_duration, |
| 451 | s.timeBefore AS service_timeBefore, |
| 452 | s.timeAfter AS service_timeAfter, |
| 453 | s.pictureFullPath AS service_picture_full, |
| 454 | s.pictureThumbPath AS service_picture_thumb, |
| 455 | s.show AS service_show, |
| 456 | |
| 457 | l.id AS location_id, |
| 458 | l.name AS location_name, |
| 459 | l.address AS location_address, |
| 460 | l.phone AS location_phone, |
| 461 | l.latitude AS location_latitude, |
| 462 | l.longitude AS location_longitude, |
| 463 | |
| 464 | pu.id AS provider_id, |
| 465 | pu.firstName AS provider_firstName, |
| 466 | pu.lastName AS provider_lastName, |
| 467 | pu.email AS provider_email, |
| 468 | pu.translations AS provider_translations, |
| 469 | pu.stripeConnect AS provider_stripeConnect, |
| 470 | |
| 471 | g.id AS gallery_id, |
| 472 | g.pictureFullPath AS gallery_picture_full, |
| 473 | g.pictureThumbPath AS gallery_picture_thumb, |
| 474 | g.position AS gallery_position |
| 475 | |
| 476 | FROM {$this->table} p |
| 477 | LEFT JOIN {$packageServicesTable} ps ON ps.packageId = p.id |
| 478 | LEFT JOIN {$servicesTable} s ON ps.serviceId = s.id |
| 479 | LEFT JOIN {$packageServicesProvidersTable} psp ON psp.packageServiceId = ps.id |
| 480 | LEFT JOIN {$packageServicesLocationsTable} psl ON psl.packageServiceId = ps.id |
| 481 | LEFT JOIN {$usersTable} pu ON pu.id = psp.userId |
| 482 | LEFT JOIN {$locationsTable} l ON l.id = psl.locationId |
| 483 | LEFT JOIN {$galleriesTable} g ON g.entityId = p.id AND g.entityType = 'package' |
| 484 | WHERE p.id = :id" |
| 485 | ); |
| 486 | |
| 487 | $statement->execute($params); |
| 488 | |
| 489 | $rows = $statement->fetchAll(); |
| 490 | } catch (\Exception $e) { |
| 491 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 492 | } |
| 493 | |
| 494 | return call_user_func([static::FACTORY, 'createCollection'], $rows)->getItem($id); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * @param $serviceId |
| 499 | * @param $status |
| 500 | * |
| 501 | * @throws QueryExecutionException |
| 502 | */ |
| 503 | public function updateStatusById($serviceId, $status) |
| 504 | { |
| 505 | $params = [ |
| 506 | ':id' => $serviceId, |
| 507 | ':status' => $status |
| 508 | ]; |
| 509 | |
| 510 | try { |
| 511 | $statement = $this->connection->prepare( |
| 512 | "UPDATE {$this->table} |
| 513 | SET |
| 514 | `status` = :status |
| 515 | WHERE id = :id" |
| 516 | ); |
| 517 | |
| 518 | $res = $statement->execute($params); |
| 519 | |
| 520 | if (!$res) { |
| 521 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 522 | } |
| 523 | } catch (\Exception $e) { |
| 524 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 |