PaymentRepository.php
1363 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\Payment; |
| 9 | |
| 10 | use AmeliaBooking\Domain\Collection\Collection; |
| 11 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 12 | use AmeliaBooking\Domain\Entity\Payment\Payment; |
| 13 | use AmeliaBooking\Domain\Factory\Payment\PaymentFactory; |
| 14 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 15 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 16 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 17 | use AmeliaBooking\Infrastructure\Connection; |
| 18 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\PackagesCustomersServicesTable; |
| 19 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\CustomerBookingsToExtrasTable; |
| 20 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Coupon\CouponsTable; |
| 21 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Location\LocationsTable; |
| 22 | |
| 23 | /** |
| 24 | * Class PaymentRepository |
| 25 | * |
| 26 | * @package AmeliaBooking\Infrastructure\Repository\Payment |
| 27 | */ |
| 28 | class PaymentRepository extends AbstractRepository |
| 29 | { |
| 30 | /** @var string */ |
| 31 | protected $appointmentsTable; |
| 32 | |
| 33 | /** @var string */ |
| 34 | protected $bookingsTable; |
| 35 | |
| 36 | /** @var string */ |
| 37 | protected $servicesTable; |
| 38 | |
| 39 | /** @var string */ |
| 40 | protected $usersTable; |
| 41 | |
| 42 | /** @var string */ |
| 43 | protected $eventsTable; |
| 44 | |
| 45 | /** @var string */ |
| 46 | protected $eventsProvidersTable; |
| 47 | |
| 48 | /** @var string */ |
| 49 | protected $eventsPeriodsTable; |
| 50 | |
| 51 | /** @var string */ |
| 52 | protected $customerBookingsToEventsPeriodsTable; |
| 53 | |
| 54 | /** @var string */ |
| 55 | protected $packagesTable; |
| 56 | |
| 57 | /** @var string */ |
| 58 | protected $packagesCustomersTable; |
| 59 | |
| 60 | /** @var string */ |
| 61 | protected $packagesCustomersServiceTable; |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * @param Connection $connection |
| 66 | * @param string $table |
| 67 | * @param string $appointmentsTable |
| 68 | * @param string $bookingsTable |
| 69 | * @param string $servicesTable |
| 70 | * @param string $usersTable |
| 71 | * @param string $eventsTable |
| 72 | * @param string $eventsProvidersTable |
| 73 | * @param string $eventsPeriodsTable |
| 74 | * @param string $customerBookingsToEventsPeriodsTable |
| 75 | * @param string $packagesTable |
| 76 | * @param string $packagesCustomersTable |
| 77 | */ |
| 78 | public function __construct( |
| 79 | Connection $connection, |
| 80 | $table, |
| 81 | $appointmentsTable, |
| 82 | $bookingsTable, |
| 83 | $servicesTable, |
| 84 | $usersTable, |
| 85 | $eventsTable, |
| 86 | $eventsProvidersTable, |
| 87 | $eventsPeriodsTable, |
| 88 | $customerBookingsToEventsPeriodsTable, |
| 89 | $packagesTable, |
| 90 | $packagesCustomersTable |
| 91 | ) { |
| 92 | parent::__construct($connection, $table); |
| 93 | |
| 94 | $this->appointmentsTable = $appointmentsTable; |
| 95 | $this->bookingsTable = $bookingsTable; |
| 96 | $this->servicesTable = $servicesTable; |
| 97 | $this->usersTable = $usersTable; |
| 98 | $this->eventsTable = $eventsTable; |
| 99 | $this->eventsProvidersTable = $eventsProvidersTable; |
| 100 | $this->eventsPeriodsTable = $eventsPeriodsTable; |
| 101 | $this->customerBookingsToEventsPeriodsTable = $customerBookingsToEventsPeriodsTable; |
| 102 | $this->packagesTable = $packagesTable; |
| 103 | $this->packagesCustomersTable = $packagesCustomersTable; |
| 104 | $this->packagesCustomersServiceTable = PackagesCustomersServicesTable::getTableName(); |
| 105 | } |
| 106 | |
| 107 | public const FACTORY = PaymentFactory::class; |
| 108 | |
| 109 | /** |
| 110 | * @param Payment $entity |
| 111 | * |
| 112 | * @return bool |
| 113 | * @throws QueryExecutionException |
| 114 | */ |
| 115 | public function add($entity) |
| 116 | { |
| 117 | $data = $entity->toArray(); |
| 118 | |
| 119 | $params = [ |
| 120 | ':customerBookingId' => $data['customerBookingId'] ? $data['customerBookingId'] : null, |
| 121 | ':packageCustomerId' => $data['packageCustomerId'] ? $data['packageCustomerId'] : null, |
| 122 | ':parentId' => $data['parentId'] ? $data['parentId'] : null, |
| 123 | ':amount' => $data['amount'], |
| 124 | ':dateTime' => DateTimeService::getCustomDateTimeInUtc($data['dateTime']), |
| 125 | ':status' => $data['status'], |
| 126 | ':gateway' => $data['gateway'], |
| 127 | ':gatewayTitle' => $data['gatewayTitle'], |
| 128 | ':data' => $data['data'], |
| 129 | ':entity' => $data['entity'], |
| 130 | ':created' => DateTimeService::getNowDateTimeInUtc(), |
| 131 | ':wcOrderId' => !empty($data['wcOrderId']) ? $data['wcOrderId'] : null, |
| 132 | ':transactionId' => !empty($data['transactionId']) ? $data['transactionId'] : null, |
| 133 | ':wcOrderItemId' => !empty($data['wcOrderItemId']) ? $data['wcOrderItemId'] : null, |
| 134 | ]; |
| 135 | |
| 136 | if (!empty($data['invoiceNumber'])) { |
| 137 | $params[':invoiceNumber'] = $data['invoiceNumber']; |
| 138 | |
| 139 | $invoiceNumberText = ":invoiceNumber"; |
| 140 | } else { |
| 141 | $invoiceNumberText = "(SELECT MAX(CASE WHEN invoiceNumber IS NULL THEN 0 ELSE invoiceNumber END)+1 FROM {$this->table} p)"; |
| 142 | } |
| 143 | |
| 144 | if ($data['parentId']) { |
| 145 | $params[':actionsCompleted'] = null; |
| 146 | } else { |
| 147 | $params[':actionsCompleted'] = !empty($data['actionsCompleted']) ? 1 : 0; |
| 148 | } |
| 149 | |
| 150 | try { |
| 151 | $statement = $this->connection->prepare( |
| 152 | "INSERT INTO |
| 153 | {$this->table} |
| 154 | ( |
| 155 | `customerBookingId`, |
| 156 | `packageCustomerId`, |
| 157 | `parentId`, |
| 158 | `amount`, |
| 159 | `dateTime`, |
| 160 | `status`, |
| 161 | `gateway`, |
| 162 | `gatewayTitle`, |
| 163 | `data`, `entity`, |
| 164 | `actionsCompleted`, |
| 165 | `created`, |
| 166 | `wcOrderId`, |
| 167 | `wcOrderItemId`, |
| 168 | `transactionId`, |
| 169 | `invoiceNumber` |
| 170 | ) VALUES ( |
| 171 | :customerBookingId, |
| 172 | :packageCustomerId, |
| 173 | :parentId, |
| 174 | :amount, |
| 175 | :dateTime, |
| 176 | :status, |
| 177 | :gateway, |
| 178 | :gatewayTitle, |
| 179 | :data, |
| 180 | :entity, |
| 181 | :actionsCompleted, |
| 182 | :created, |
| 183 | :wcOrderId, |
| 184 | :wcOrderItemId, |
| 185 | :transactionId, |
| 186 | {$invoiceNumberText} |
| 187 | )" |
| 188 | ); |
| 189 | |
| 190 | $response = $statement->execute($params); |
| 191 | } catch (\Exception $e) { |
| 192 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 193 | } |
| 194 | |
| 195 | if (!$response) { |
| 196 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 197 | } |
| 198 | |
| 199 | return $this->connection->lastInsertId(); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @param int $id |
| 204 | * @param Payment $entity |
| 205 | * |
| 206 | * @return bool |
| 207 | * @throws QueryExecutionException |
| 208 | */ |
| 209 | public function update($id, $entity) |
| 210 | { |
| 211 | $data = $entity->toArray(); |
| 212 | |
| 213 | $params = [ |
| 214 | ':customerBookingId' => $data['customerBookingId'] ? $data['customerBookingId'] : null, |
| 215 | ':packageCustomerId' => $data['packageCustomerId'] ? $data['packageCustomerId'] : null, |
| 216 | ':parentId' => $data['parentId'] ? $data['parentId'] : null, |
| 217 | ':amount' => $data['amount'], |
| 218 | ':dateTime' => DateTimeService::getCustomDateTimeInUtc($data['dateTime']), |
| 219 | ':status' => $data['status'], |
| 220 | ':gateway' => $data['gateway'], |
| 221 | ':gatewayTitle' => $data['gatewayTitle'], |
| 222 | ':data' => $data['data'], |
| 223 | ':transactionId' => $data['transactionId'], |
| 224 | ':id' => $id, |
| 225 | ]; |
| 226 | |
| 227 | try { |
| 228 | $statement = $this->connection->prepare( |
| 229 | "UPDATE {$this->table} |
| 230 | SET |
| 231 | `customerBookingId` = :customerBookingId, |
| 232 | `packageCustomerId` = :packageCustomerId, |
| 233 | `parentId` = :parentId, |
| 234 | `amount` = :amount, |
| 235 | `dateTime` = :dateTime, |
| 236 | `status` = :status, |
| 237 | `gateway` = :gateway, |
| 238 | `gatewayTitle` = :gatewayTitle, |
| 239 | `data` = :data, |
| 240 | `transactionId` = :transactionId |
| 241 | WHERE |
| 242 | id = :id" |
| 243 | ); |
| 244 | |
| 245 | $response = $statement->execute($params); |
| 246 | } catch (\Exception $e) { |
| 247 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 248 | } |
| 249 | |
| 250 | if (!$response) { |
| 251 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 252 | } |
| 253 | |
| 254 | return $response; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @param array $criteria |
| 259 | * |
| 260 | * @return Collection |
| 261 | * @throws QueryExecutionException |
| 262 | */ |
| 263 | public function getByCriteria($criteria) |
| 264 | { |
| 265 | $result = new Collection(); |
| 266 | |
| 267 | $params = []; |
| 268 | |
| 269 | $where = []; |
| 270 | |
| 271 | if (!empty($criteria['bookingIds'])) { |
| 272 | $queryBookings = []; |
| 273 | |
| 274 | foreach ($criteria['bookingIds'] as $index => $value) { |
| 275 | $param = ':id' . $index; |
| 276 | |
| 277 | $queryBookings[] = $param; |
| 278 | |
| 279 | $params[$param] = $value; |
| 280 | } |
| 281 | |
| 282 | $where[] = 'customerBookingId IN (' . implode(', ', $queryBookings) . ')'; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | if (!empty($criteria['packageCustomerId'])) { |
| 287 | $params[':packageCustomerId'] = $criteria['packageCustomerId']; |
| 288 | $where[] = 'packageCustomerId = :packageCustomerId'; |
| 289 | } |
| 290 | |
| 291 | if (!empty($criteria['ids'])) { |
| 292 | $queryIds = []; |
| 293 | |
| 294 | foreach ($criteria['ids'] as $index => $value) { |
| 295 | $param = ':id' . $index; |
| 296 | |
| 297 | $queryIds[] = $param; |
| 298 | |
| 299 | $params[$param] = $value; |
| 300 | } |
| 301 | |
| 302 | $where[] = 'id IN (' . implode(', ', $queryIds) . ')'; |
| 303 | } |
| 304 | |
| 305 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 306 | |
| 307 | try { |
| 308 | $statement = $this->connection->prepare( |
| 309 | "SELECT |
| 310 | id AS id, |
| 311 | customerBookingId AS customerBookingId, |
| 312 | packageCustomerId AS packageCustomerId, |
| 313 | parentId AS parentId, |
| 314 | invoiceNumber AS invoiceNumber, |
| 315 | amount AS amount, |
| 316 | dateTime AS dateTime, |
| 317 | status AS status, |
| 318 | gateway AS gateway, |
| 319 | gatewayTitle AS gatewayTitle, |
| 320 | data AS data |
| 321 | FROM {$this->table} |
| 322 | {$where}" |
| 323 | ); |
| 324 | |
| 325 | $statement->execute($params); |
| 326 | |
| 327 | while ($row = $statement->fetch()) { |
| 328 | $result->addItem(call_user_func([static::FACTORY, 'create'], $row), $row['id']); |
| 329 | } |
| 330 | } catch (\Exception $e) { |
| 331 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 332 | } |
| 333 | |
| 334 | return $result; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * @param array $criteria |
| 339 | * @param int $itemsPerPage |
| 340 | * @param boolean $invoice |
| 341 | * |
| 342 | * @return array |
| 343 | * @throws QueryExecutionException |
| 344 | */ |
| 345 | public function getFilteredIds($criteria, $itemsPerPage = null, $invoice = false) |
| 346 | { |
| 347 | $params = []; |
| 348 | $appointmentParams1 = []; |
| 349 | $appointmentParams2 = []; |
| 350 | $eventParams = []; |
| 351 | $whereAppointment1 = []; |
| 352 | $whereAppointment2 = []; |
| 353 | $whereEvent = []; |
| 354 | |
| 355 | $basedOnDate = $invoice ? 'created' : 'dateTime'; |
| 356 | |
| 357 | if (!empty($criteria['dates'])) { |
| 358 | $whereAppointment1[] = "(p.{$basedOnDate} BETWEEN :paymentAppointmentFrom1 AND :paymentAppointmentTo1)"; |
| 359 | $whereAppointment2[] = "(p.{$basedOnDate} BETWEEN :paymentAppointmentFrom2 AND :paymentAppointmentTo2)"; |
| 360 | $appointmentParams1[':paymentAppointmentFrom1'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 361 | $appointmentParams2[':paymentAppointmentFrom2'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 362 | $appointmentParams1[':paymentAppointmentTo1'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 363 | $appointmentParams2[':paymentAppointmentTo2'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 364 | |
| 365 | $whereEvent[] = "(p.{$basedOnDate} BETWEEN :paymentEventFrom AND :paymentEventTo)"; |
| 366 | $eventParams[':paymentEventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 367 | $eventParams[':paymentEventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 368 | } |
| 369 | |
| 370 | if (!empty($criteria['customerId'])) { |
| 371 | $appointmentParams1[':customerAppointmentId1'] = $criteria['customerId']; |
| 372 | $appointmentParams2[':customerAppointmentId2'] = $criteria['customerId']; |
| 373 | $whereAppointment1[] = 'cb.customerId = :customerAppointmentId1'; |
| 374 | $whereAppointment2[] = 'pc.customerId = :customerAppointmentId2'; |
| 375 | |
| 376 | $eventParams[':customerEventId'] = $criteria['customerId']; |
| 377 | $whereEvent[] = 'cb.customerId = :customerEventId'; |
| 378 | } |
| 379 | |
| 380 | $eventsProvidersJoin = ''; |
| 381 | |
| 382 | if (!empty($criteria['providerId'])) { |
| 383 | $appointmentParams1[':providerAppointmentId1'] = $criteria['providerId']; |
| 384 | $appointmentParams1[':providerAppointmentId2'] = $criteria['providerId']; |
| 385 | $whereAppointment1[] = 'a.providerId = :providerAppointmentId1'; |
| 386 | $whereAppointment2[] = 'a.providerId = :providerAppointmentId2'; |
| 387 | |
| 388 | $eventParams[':providerEventId'] = $criteria['providerId']; |
| 389 | $whereEvent[] = 'epu.userId = :providerEventId'; |
| 390 | |
| 391 | $eventsProvidersJoin = " |
| 392 | INNER JOIN {$this->eventsPeriodsTable} ep ON ep.id = cbe.eventPeriodId |
| 393 | INNER JOIN {$this->eventsProvidersTable} epu ON epu.eventId = ep.eventId |
| 394 | "; |
| 395 | } |
| 396 | |
| 397 | if (!empty($criteria['services'])) { |
| 398 | $queryServices1 = []; |
| 399 | $queryServices2 = []; |
| 400 | |
| 401 | foreach ((array)$criteria['services'] as $index => $value) { |
| 402 | $param1 = ':service0' . $index; |
| 403 | $param2 = ':service1' . $index; |
| 404 | $queryServices1[] = $param1; |
| 405 | $queryServices2[] = $param2; |
| 406 | $appointmentParams1[$param1] = $value; |
| 407 | $appointmentParams2[$param2] = $value; |
| 408 | } |
| 409 | |
| 410 | $whereAppointment1[] = 'a.serviceId IN (' . implode(', ', $queryServices1) . ')'; |
| 411 | $whereAppointment2[] = 'a.serviceId IN (' . implode(', ', $queryServices2) . ')'; |
| 412 | } |
| 413 | |
| 414 | $appointments2ProvidersServicesJoin = ''; |
| 415 | |
| 416 | if (!empty($criteria['providerId']) || !empty($criteria['services'])) { |
| 417 | $appointments2ProvidersServicesJoin = " |
| 418 | INNER JOIN {$this->packagesCustomersServiceTable} pcs ON pc.id = pcs.packageCustomerId |
| 419 | INNER JOIN {$this->bookingsTable} cb ON cb.packageCustomerServiceId = pcs.id |
| 420 | INNER JOIN {$this->appointmentsTable} a ON a.id = cb.appointmentId |
| 421 | "; |
| 422 | } |
| 423 | |
| 424 | if (!empty($criteria['status'])) { |
| 425 | $appointmentParams1[':statusAppointment1'] = $criteria['status']; |
| 426 | $appointmentParams2[':statusAppointment2'] = $criteria['status']; |
| 427 | $whereAppointment1[] = 'p.status = :statusAppointment1'; |
| 428 | $whereAppointment2[] = 'p.status = :statusAppointment2'; |
| 429 | |
| 430 | $eventParams[':statusEvent'] = $criteria['status']; |
| 431 | $whereEvent[] = 'p.status = :statusEvent'; |
| 432 | } |
| 433 | |
| 434 | if (!empty($criteria['packages'])) { |
| 435 | $queryPackages = []; |
| 436 | |
| 437 | foreach ((array)$criteria['packages'] as $index => $value) { |
| 438 | $param = ':package' . $index; |
| 439 | $queryPackages[] = $param; |
| 440 | $appointmentParams2[$param] = $value; |
| 441 | } |
| 442 | |
| 443 | $whereAppointment2[] = "p.packageCustomerId IN (SELECT pc.id |
| 444 | FROM {$this->packagesCustomersTable} pc |
| 445 | WHERE pc.packageId IN (" . implode(', ', $queryPackages) . '))'; |
| 446 | } |
| 447 | |
| 448 | if (!empty($criteria['events'])) { |
| 449 | $queryEvents = []; |
| 450 | |
| 451 | foreach ((array)$criteria['events'] as $index => $value) { |
| 452 | $param = ':event' . $index; |
| 453 | $queryEvents[] = $param; |
| 454 | $eventParams[$param] = $value; |
| 455 | } |
| 456 | |
| 457 | $whereEvent[] = "p.customerBookingId IN (SELECT cbe.customerBookingId |
| 458 | FROM {$this->eventsTable} e |
| 459 | INNER JOIN {$this->eventsPeriodsTable} ep ON ep.eventId = e.id |
| 460 | INNER JOIN {$this->customerBookingsToEventsPeriodsTable} cbe ON cbe.eventPeriodId = ep.id |
| 461 | WHERE e.id IN (" . implode(', ', $queryEvents) . '))'; |
| 462 | } |
| 463 | |
| 464 | $whereAppointment1 = $whereAppointment1 ? ' AND ' . implode(' AND ', $whereAppointment1) : ''; |
| 465 | $whereAppointment2 = $whereAppointment2 ? ' AND ' . implode(' AND ', $whereAppointment2) : ''; |
| 466 | $whereEvent = $whereEvent ? ' AND ' . implode(' AND ', $whereEvent) : ''; |
| 467 | |
| 468 | $groupBy = ''; |
| 469 | if ($invoice) { |
| 470 | $groupBy = 'GROUP BY IFNULL(invoiceNumber, id)'; |
| 471 | } |
| 472 | |
| 473 | $appointmentQuery1 = "SELECT |
| 474 | p.id AS id, |
| 475 | p.dateTime AS dateTime, |
| 476 | p.created AS created, |
| 477 | p.invoiceNumber AS invoiceNumber, |
| 478 | 'appointment' AS type |
| 479 | FROM {$this->table} p |
| 480 | INNER JOIN {$this->bookingsTable} cb ON cb.id = p.customerBookingId |
| 481 | INNER JOIN {$this->appointmentsTable} a ON a.id = cb.appointmentId |
| 482 | WHERE 1=1 {$whereAppointment1} GROUP BY p.customerBookingId ORDER BY p.id ASC"; |
| 483 | |
| 484 | $appointmentQuery2 = "SELECT |
| 485 | p.id AS id, |
| 486 | p.dateTime AS dateTime, |
| 487 | p.created AS created, |
| 488 | p.invoiceNumber AS invoiceNumber, |
| 489 | 'package' AS type |
| 490 | FROM {$this->table} p |
| 491 | INNER JOIN {$this->packagesCustomersTable} pc ON p.packageCustomerId = pc.id |
| 492 | {$appointments2ProvidersServicesJoin} |
| 493 | WHERE 1=1 {$whereAppointment2} GROUP BY p.packageCustomerId ORDER BY p.id ASC"; |
| 494 | |
| 495 | $eventQuery = "SELECT |
| 496 | p.id AS id, |
| 497 | p.dateTime AS dateTime, |
| 498 | p.created AS created, |
| 499 | p.invoiceNumber AS invoiceNumber, |
| 500 | 'event' AS type |
| 501 | FROM {$this->table} p |
| 502 | INNER JOIN {$this->bookingsTable} cb ON cb.id = p.customerBookingId |
| 503 | INNER JOIN {$this->customerBookingsToEventsPeriodsTable} cbe ON cbe.customerBookingId = cb.id |
| 504 | {$eventsProvidersJoin} |
| 505 | WHERE 1=1 {$whereEvent} GROUP BY p.customerBookingId ORDER BY p.id ASC"; |
| 506 | |
| 507 | $result = []; |
| 508 | |
| 509 | if (isset($criteria['events'], $criteria['services'])) { |
| 510 | return $result; |
| 511 | } elseif (isset($criteria['services'])) { |
| 512 | $paymentQuery = "({$appointmentQuery1}) UNION ALL ({$appointmentQuery2})"; |
| 513 | $params = array_merge($params, $appointmentParams1, $appointmentParams2); |
| 514 | } elseif (isset($criteria['events'])) { |
| 515 | $paymentQuery = "({$eventQuery})"; |
| 516 | $params = array_merge($params, $eventParams); |
| 517 | } elseif (isset($criteria['packages'])) { |
| 518 | $paymentQuery = "({$appointmentQuery2})"; |
| 519 | $params = array_merge($params, $appointmentParams2); |
| 520 | } else { |
| 521 | $paymentQuery = "({$appointmentQuery1}) UNION ALL ({$appointmentQuery2}) UNION ALL ({$eventQuery})"; |
| 522 | $params = array_merge($params, $appointmentParams1, $appointmentParams2, $eventParams); |
| 523 | } |
| 524 | |
| 525 | $limit = $this->getLimit( |
| 526 | !empty($criteria['page']) ? (int)$criteria['page'] : 0, |
| 527 | (int)$itemsPerPage |
| 528 | ); |
| 529 | |
| 530 | try { |
| 531 | $statement = $this->connection->prepare( |
| 532 | "SELECT * FROM ({$paymentQuery}) payments |
| 533 | {$groupBy} |
| 534 | ORDER BY {$basedOnDate}, id |
| 535 | {$limit}" |
| 536 | ); |
| 537 | |
| 538 | $statement->execute($params); |
| 539 | |
| 540 | $rows = $statement->fetchAll(); |
| 541 | } catch (\Exception $e) { |
| 542 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e); |
| 543 | } |
| 544 | |
| 545 | foreach ($rows as $row) { |
| 546 | $result[(int)$row['id']] = $row['type']; |
| 547 | } |
| 548 | |
| 549 | return $result; |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * @param array $criteria |
| 554 | * @param boolean $invoice |
| 555 | * |
| 556 | * @return array |
| 557 | * @throws QueryExecutionException |
| 558 | */ |
| 559 | public function getFilteredIdsCount($criteria, $invoice = false) |
| 560 | { |
| 561 | $params = []; |
| 562 | $appointmentParams1 = []; |
| 563 | $appointmentParams2 = []; |
| 564 | $eventParams = []; |
| 565 | $whereAppointment1 = []; |
| 566 | $whereAppointment2 = []; |
| 567 | $whereEvent = []; |
| 568 | |
| 569 | $basedOnDate = $invoice ? 'created' : 'dateTime'; |
| 570 | |
| 571 | if (!empty($criteria['dates'])) { |
| 572 | $whereAppointment1[] = "(p.{$basedOnDate} BETWEEN :paymentAppointmentFrom1 AND :paymentAppointmentTo1)"; |
| 573 | $whereAppointment2[] = "(p.{$basedOnDate} BETWEEN :paymentAppointmentFrom2 AND :paymentAppointmentTo2)"; |
| 574 | $appointmentParams1[':paymentAppointmentFrom1'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 575 | $appointmentParams2[':paymentAppointmentFrom2'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 576 | $appointmentParams1[':paymentAppointmentTo1'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 577 | $appointmentParams2[':paymentAppointmentTo2'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 578 | |
| 579 | $whereEvent[] = "(p.{$basedOnDate} BETWEEN :paymentEventFrom AND :paymentEventTo)"; |
| 580 | $eventParams[':paymentEventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 581 | $eventParams[':paymentEventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 582 | } |
| 583 | |
| 584 | if (!empty($criteria['customerId'])) { |
| 585 | $appointmentParams1[':customerAppointmentId1'] = $criteria['customerId']; |
| 586 | $appointmentParams2[':customerAppointmentId2'] = $criteria['customerId']; |
| 587 | $whereAppointment1[] = 'cb.customerId = :customerAppointmentId1'; |
| 588 | $whereAppointment2[] = 'pc.customerId = :customerAppointmentId2'; |
| 589 | |
| 590 | $eventParams[':customerEventId'] = $criteria['customerId']; |
| 591 | $whereEvent[] = 'cb.customerId = :customerEventId'; |
| 592 | } |
| 593 | |
| 594 | $eventsProvidersJoin = ''; |
| 595 | |
| 596 | if (!empty($criteria['providerId'])) { |
| 597 | $appointmentParams1[':providerAppointmentId1'] = $criteria['providerId']; |
| 598 | $appointmentParams1[':providerAppointmentId2'] = $criteria['providerId']; |
| 599 | $whereAppointment1[] = 'a.providerId = :providerAppointmentId1'; |
| 600 | $whereAppointment2[] = 'a.providerId = :providerAppointmentId2'; |
| 601 | |
| 602 | $eventParams[':providerEventId'] = $criteria['providerId']; |
| 603 | $whereEvent[] = 'epu.userId = :providerEventId'; |
| 604 | |
| 605 | $eventsProvidersJoin = " |
| 606 | INNER JOIN {$this->eventsPeriodsTable} ep ON ep.id = cbe.eventPeriodId |
| 607 | INNER JOIN {$this->eventsProvidersTable} epu ON epu.eventId = ep.eventId |
| 608 | "; |
| 609 | } |
| 610 | |
| 611 | if (!empty($criteria['services'])) { |
| 612 | $queryServices1 = []; |
| 613 | $queryServices2 = []; |
| 614 | |
| 615 | foreach ((array)$criteria['services'] as $index => $value) { |
| 616 | $param1 = ':service0' . $index; |
| 617 | $param2 = ':service1' . $index; |
| 618 | $queryServices1[] = $param1; |
| 619 | $queryServices2[] = $param2; |
| 620 | $appointmentParams1[$param1] = $value; |
| 621 | $appointmentParams2[$param2] = $value; |
| 622 | } |
| 623 | |
| 624 | $whereAppointment1[] = 'a.serviceId IN (' . implode(', ', $queryServices1) . ')'; |
| 625 | $whereAppointment2[] = 'a.serviceId IN (' . implode(', ', $queryServices2) . ')'; |
| 626 | } |
| 627 | |
| 628 | $appointments2ProvidersServicesJoin = ''; |
| 629 | |
| 630 | if (!empty($criteria['providerId']) || !empty($criteria['services'])) { |
| 631 | $appointments2ProvidersServicesJoin = " |
| 632 | INNER JOIN {$this->packagesCustomersServiceTable} pcs ON pc.id = pcs.packageCustomerId |
| 633 | INNER JOIN {$this->bookingsTable} cb ON cb.packageCustomerServiceId = pcs.id |
| 634 | INNER JOIN {$this->appointmentsTable} a ON a.id = cb.appointmentId |
| 635 | "; |
| 636 | } |
| 637 | |
| 638 | if (!empty($criteria['status'])) { |
| 639 | $appointmentParams1[':statusAppointment1'] = $criteria['status']; |
| 640 | $appointmentParams2[':statusAppointment2'] = $criteria['status']; |
| 641 | $whereAppointment1[] = 'p.status = :statusAppointment1'; |
| 642 | $whereAppointment2[] = 'p.status = :statusAppointment2'; |
| 643 | |
| 644 | $eventParams[':statusEvent'] = $criteria['status']; |
| 645 | $whereEvent[] = 'p.status = :statusEvent'; |
| 646 | } |
| 647 | |
| 648 | if (!empty($criteria['events'])) { |
| 649 | $queryEvents = []; |
| 650 | |
| 651 | foreach ((array)$criteria['events'] as $index => $value) { |
| 652 | $param = ':event' . $index; |
| 653 | $queryEvents[] = $param; |
| 654 | $eventParams[$param] = $value; |
| 655 | } |
| 656 | |
| 657 | $whereEvent[] = "p.customerBookingId IN (SELECT cbe.customerBookingId |
| 658 | FROM {$this->eventsTable} e |
| 659 | INNER JOIN {$this->eventsPeriodsTable} ep ON ep.eventId = e.id |
| 660 | INNER JOIN {$this->customerBookingsToEventsPeriodsTable} cbe ON cbe.eventPeriodId = ep.id |
| 661 | WHERE e.id IN (" . implode(', ', $queryEvents) . '))'; |
| 662 | } |
| 663 | |
| 664 | if (!empty($criteria['packages'])) { |
| 665 | $queryPackages = []; |
| 666 | |
| 667 | foreach ((array)$criteria['packages'] as $index => $value) { |
| 668 | $param = ':package' . $index; |
| 669 | $queryPackages[] = $param; |
| 670 | $appointmentParams2[$param] = $value; |
| 671 | } |
| 672 | |
| 673 | $whereAppointment2[] = "p.packageCustomerId IN (SELECT pc.id |
| 674 | FROM {$this->packagesCustomersTable} pc |
| 675 | WHERE pc.packageId IN (" . implode(', ', $queryPackages) . '))'; |
| 676 | } |
| 677 | |
| 678 | $whereAppointment1 = $whereAppointment1 ? ' AND ' . implode(' AND ', $whereAppointment1) : ''; |
| 679 | $whereAppointment2 = $whereAppointment2 ? ' AND ' . implode(' AND ', $whereAppointment2) : ''; |
| 680 | $whereEvent = $whereEvent ? ' AND ' . implode(' AND ', $whereEvent) : ''; |
| 681 | |
| 682 | $groupBy = ''; |
| 683 | if ($invoice) { |
| 684 | $groupBy = 'GROUP BY IFNULL(invoiceNumber, id)'; |
| 685 | } |
| 686 | |
| 687 | |
| 688 | $appointmentQuery1 = "SELECT |
| 689 | COUNT(DISTINCT(p.customerBookingId)) AS appointmentsCount1, |
| 690 | 0 AS appointmentsCount2, |
| 691 | 0 AS eventsCount, |
| 692 | p.id AS id, |
| 693 | p.invoiceNumber AS invoiceNumber, |
| 694 | p.created AS created, |
| 695 | p.dateTime AS dateTime |
| 696 | FROM {$this->table} p |
| 697 | INNER JOIN {$this->bookingsTable} cb ON cb.id = p.customerBookingId |
| 698 | INNER JOIN {$this->appointmentsTable} a ON a.id = cb.appointmentId |
| 699 | WHERE 1=1 {$whereAppointment1} GROUP BY p.customerBookingId ORDER BY p.id ASC"; |
| 700 | |
| 701 | $appointmentQuery2 = "SELECT |
| 702 | 0 AS appointmentsCount1, |
| 703 | COUNT(DISTINCT(p.packageCustomerId)) AS appointmentsCount2, |
| 704 | 0 AS eventsCount, |
| 705 | p.id AS id, |
| 706 | p.invoiceNumber AS invoiceNumber, |
| 707 | p.created AS created, |
| 708 | p.dateTime AS dateTime |
| 709 | FROM {$this->table} p |
| 710 | INNER JOIN {$this->packagesCustomersTable} pc ON p.packageCustomerId = pc.id |
| 711 | {$appointments2ProvidersServicesJoin} |
| 712 | WHERE 1=1 {$whereAppointment2} GROUP BY p.packageCustomerId ORDER BY p.id ASC"; |
| 713 | |
| 714 | $eventQuery = "SELECT |
| 715 | 0 AS appointmentsCount1, |
| 716 | 0 AS appointmentsCount2, |
| 717 | COUNT(DISTINCT(p.customerBookingId)) AS eventsCount, |
| 718 | p.id AS id, |
| 719 | p.invoiceNumber AS invoiceNumber, |
| 720 | p.created AS created, |
| 721 | p.dateTime AS dateTime |
| 722 | FROM {$this->table} p |
| 723 | INNER JOIN {$this->bookingsTable} cb ON cb.id = p.customerBookingId |
| 724 | INNER JOIN {$this->customerBookingsToEventsPeriodsTable} cbe ON cbe.customerBookingId = cb.id |
| 725 | {$eventsProvidersJoin} |
| 726 | WHERE 1=1 {$whereEvent} GROUP BY p.customerBookingId ORDER BY p.id ASC"; |
| 727 | |
| 728 | if (isset($criteria['events'], $criteria['services'])) { |
| 729 | return []; |
| 730 | } elseif (isset($criteria['services'])) { |
| 731 | $paymentQuery = "({$appointmentQuery1}) UNION ALL ({$appointmentQuery2})"; |
| 732 | $params = array_merge($params, $appointmentParams1, $appointmentParams2); |
| 733 | } elseif (isset($criteria['events'])) { |
| 734 | $paymentQuery = "({$eventQuery})"; |
| 735 | $params = array_merge($params, $eventParams); |
| 736 | } elseif (isset($criteria['packages'])) { |
| 737 | $paymentQuery = "({$appointmentQuery2})"; |
| 738 | $params = array_merge($params, $appointmentParams2); |
| 739 | } else { |
| 740 | $paymentQuery = "({$appointmentQuery1}) UNION ALL ({$appointmentQuery2}) UNION ALL ({$eventQuery})"; |
| 741 | $params = array_merge($params, $appointmentParams1, $appointmentParams2, $eventParams); |
| 742 | } |
| 743 | |
| 744 | try { |
| 745 | $statement = $this->connection->prepare( |
| 746 | "SELECT * FROM ({$paymentQuery}) payments |
| 747 | {$groupBy} |
| 748 | ORDER BY {$basedOnDate}, id" |
| 749 | ); |
| 750 | |
| 751 | $statement->execute($params); |
| 752 | |
| 753 | $statements = $statement->fetchAll(); |
| 754 | |
| 755 | $appointmentsCount1 = 0; |
| 756 | $appointmentsCount2 = 0; |
| 757 | $eventsCount = 0; |
| 758 | |
| 759 | foreach ($statements as $st) { |
| 760 | $appointmentsCount1 += !empty($st['appointmentsCount1']) ? $st['appointmentsCount1'] : 0; |
| 761 | $appointmentsCount2 += !empty($st['appointmentsCount2']) ? $st['appointmentsCount2'] : 0; |
| 762 | $eventsCount += !empty($st['eventsCount']) ? $st['eventsCount'] : 0; |
| 763 | } |
| 764 | } catch (\Exception $e) { |
| 765 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e); |
| 766 | } |
| 767 | |
| 768 | return $appointmentsCount1 + $appointmentsCount2 + $eventsCount; |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * Returns a collection of customers that have birthday on today's date and where notification is not sent |
| 773 | * |
| 774 | * @return Collection |
| 775 | * @throws InvalidArgumentException |
| 776 | * @throws QueryExecutionException |
| 777 | * @throws \Exception |
| 778 | */ |
| 779 | public function getUncompletedActionsForPayments() |
| 780 | { |
| 781 | $params = []; |
| 782 | |
| 783 | $currentDateTime = "STR_TO_DATE('" . DateTimeService::getNowDateTimeInUtc() . "', '%Y-%m-%d %H:%i:%s')"; |
| 784 | |
| 785 | $pastDateTime = |
| 786 | "STR_TO_DATE('" . |
| 787 | DateTimeService::getNowDateTimeObjectInUtc()->modify('-1 day')->format('Y-m-d H:i:s') . |
| 788 | "', '%Y-%m-%d %H:%i:%s')"; |
| 789 | |
| 790 | try { |
| 791 | $statement = $this->connection->prepare( |
| 792 | "SELECT * FROM {$this->table} |
| 793 | WHERE |
| 794 | actionsCompleted = 0 AND |
| 795 | {$currentDateTime} > DATE_ADD(created, INTERVAL 300 SECOND) AND |
| 796 | {$pastDateTime} < created AND |
| 797 | entity IS NOT NULL" |
| 798 | ); |
| 799 | |
| 800 | $statement->execute($params); |
| 801 | |
| 802 | $rows = $statement->fetchAll(); |
| 803 | } catch (\Exception $e) { |
| 804 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e); |
| 805 | } |
| 806 | |
| 807 | $items = []; |
| 808 | |
| 809 | foreach ($rows as $row) { |
| 810 | $items[] = call_user_func([static::FACTORY, 'create'], $row); |
| 811 | } |
| 812 | |
| 813 | return new Collection($items); |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * @param array $data |
| 818 | * @param boolean $invoice |
| 819 | * |
| 820 | * @return array |
| 821 | * @throws QueryExecutionException |
| 822 | */ |
| 823 | public function getSecondaryPaymentIds($data, $invoice) |
| 824 | { |
| 825 | $params = []; |
| 826 | |
| 827 | $where = []; |
| 828 | |
| 829 | $parentIdParam1 = null; |
| 830 | $parentIdParam2 = null; |
| 831 | $paymentIdParam2 = null; |
| 832 | |
| 833 | foreach ($data as $index => $item) { |
| 834 | $paymentIdParam1 = ':paymentId1' . $index; |
| 835 | $params[$paymentIdParam1] = $item['paymentId']; |
| 836 | |
| 837 | if ($invoice) { |
| 838 | if (!empty($item['parentId'])) { |
| 839 | $parentIdParam1 = ':parentId1' . $index; |
| 840 | $params[$parentIdParam1] = $item['parentId']; |
| 841 | $parentIdParam2 = ':parentId2' . $index; |
| 842 | $params[$parentIdParam2] = $item['parentId']; |
| 843 | } |
| 844 | $paymentIdParam2 = ':paymentId2' . $index; |
| 845 | $params[$paymentIdParam2] = $item['paymentId']; |
| 846 | } |
| 847 | |
| 848 | $relationParam = ':' . $item['columnName'] . $index; |
| 849 | |
| 850 | $params[$relationParam] = $item['columnId']; |
| 851 | |
| 852 | // change in the future to simply retrieve by invoiceNumber when on invoice page since all the related payments will have the same invoiceNumber |
| 853 | // cannot be done immediately since invoiceNumber is NULL for existing payments |
| 854 | if ($invoice) { |
| 855 | $where[] = |
| 856 | "((id <> $paymentIdParam1 AND " . |
| 857 | $item['columnName'] . " = $relationParam) OR parentId = $paymentIdParam2" . |
| 858 | (!empty($item['parentId']) ? " OR id = $parentIdParam1 OR parentId = $parentIdParam2)" : ")"); |
| 859 | } else { |
| 860 | $where[] = "(id <> $paymentIdParam1 AND " . $item['columnName'] . " = $relationParam)"; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | $where = $where ? 'WHERE ' . implode(' OR ', $where) : ''; |
| 865 | |
| 866 | try { |
| 867 | $statement = $this->connection->prepare( |
| 868 | "SELECT |
| 869 | p.id AS id, |
| 870 | p.entity AS entity |
| 871 | FROM {$this->table} p |
| 872 | {$where}" |
| 873 | ); |
| 874 | |
| 875 | $statement->execute($params); |
| 876 | |
| 877 | $rows = $statement->fetchAll(); |
| 878 | } catch (\Exception $e) { |
| 879 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 880 | } |
| 881 | |
| 882 | $result = []; |
| 883 | |
| 884 | foreach ($rows as $row) { |
| 885 | $result[(int)$row['id']] = $row['entity']; |
| 886 | } |
| 887 | |
| 888 | return $result; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * @param int $paymentId |
| 893 | * @param string $transactionId |
| 894 | * |
| 895 | * @throws QueryExecutionException |
| 896 | */ |
| 897 | public function updateTransactionId($paymentId, $transactionId) |
| 898 | { |
| 899 | $params = [ |
| 900 | ':transactionId' => $transactionId, |
| 901 | ':paymentId1' => $paymentId, |
| 902 | ':paymentId2' => $paymentId |
| 903 | ]; |
| 904 | |
| 905 | try { |
| 906 | $statement = $this->connection->prepare( |
| 907 | "UPDATE {$this->table} SET `transactionId` = :transactionId WHERE id = :paymentId1 OR parentId = :paymentId2" |
| 908 | ); |
| 909 | |
| 910 | $response = $statement->execute($params); |
| 911 | } catch (\Exception $e) { |
| 912 | throw new QueryExecutionException('Unable to update data in ' . __CLASS__, $e->getCode(), $e); |
| 913 | } |
| 914 | |
| 915 | if (!$response) { |
| 916 | throw new QueryExecutionException('Unable to update data in ' . __CLASS__); |
| 917 | } |
| 918 | |
| 919 | return $response; |
| 920 | } |
| 921 | |
| 922 | |
| 923 | |
| 924 | /** |
| 925 | * @param array $data |
| 926 | * |
| 927 | * @return bool |
| 928 | * @throws QueryExecutionException |
| 929 | */ |
| 930 | public function setInvoiceNumber($data) |
| 931 | { |
| 932 | $params = [ |
| 933 | ':id1' => $data['id'], |
| 934 | ':id2' => $data['id'], |
| 935 | ":{$data['columnName']}" => $data['columnValue'] |
| 936 | ]; |
| 937 | |
| 938 | $where = "WHERE id = :id1 OR parentId = :id2 OR {$data['columnName']} = :{$data['columnName']}"; |
| 939 | |
| 940 | if (!empty($data['parentId'])) { |
| 941 | $params[':parentId1'] = $params[':parentId2'] = $data['parentId']; |
| 942 | $where = ' OR id = :parentId1 OR parentId = :parentId2'; |
| 943 | } |
| 944 | |
| 945 | try { |
| 946 | $statement = $this->connection->prepare( |
| 947 | "UPDATE {$this->table} |
| 948 | SET `invoiceNumber` = (SELECT MAX(CASE WHEN invoiceNumber IS NULL THEN 0 ELSE invoiceNumber END)+1 FROM (SELECT * FROM {$this->table}) AS p) |
| 949 | {$where}" |
| 950 | ); |
| 951 | |
| 952 | $response = $statement->execute($params); |
| 953 | } catch (\Exception $e) { |
| 954 | throw new QueryExecutionException('Unable to save invoice number in ' . __CLASS__, $e->getCode(), $e); |
| 955 | } |
| 956 | |
| 957 | if (!$response) { |
| 958 | throw new QueryExecutionException('Unable to save invoice number in ' . __CLASS__); |
| 959 | } |
| 960 | |
| 961 | return $response; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * @param array $rows |
| 966 | * |
| 967 | * @return array |
| 968 | */ |
| 969 | private function getEntitiesPaymentsResult($rows) |
| 970 | { |
| 971 | $result = []; |
| 972 | |
| 973 | foreach ($rows as &$row) { |
| 974 | $customerInfo = $row['info'] ? json_decode($row['info'], true) : null; |
| 975 | |
| 976 | if (empty($result[(int)$row['id']])) { |
| 977 | $result[(int)$row['id']] = [ |
| 978 | 'id' => (int)$row['id'], |
| 979 | 'dateTime' => DateTimeService::getCustomDateTimeFromUtc($row['dateTime']), |
| 980 | 'created' => DateTimeService::getCustomDateTimeFromUtc($row['created']), |
| 981 | 'bookingStart' => $row['bookingStart'] ? |
| 982 | DateTimeService::getCustomDateTimeFromUtc($row['bookingStart']) : null, |
| 983 | 'status' => $row['status'], |
| 984 | 'parentId' => $row['parentId'], |
| 985 | 'wcOrderId' => $row['wcOrderId'], |
| 986 | 'wcOrderItemId' => $row['wcOrderItemId'], |
| 987 | 'gateway' => $row['gateway'], |
| 988 | 'gatewayTitle' => $row['gatewayTitle'], |
| 989 | 'transactionId' => $row['transactionId'], |
| 990 | 'type' => $row['type'], |
| 991 | 'name' => $row['bookableName'], |
| 992 | 'customerBookingId' => (int)$row['customerBookingId'] ?: null, |
| 993 | 'packageCustomerId' => (int)$row['packageCustomerId'] ?: null, |
| 994 | 'amount' => (float)$row['amount'], |
| 995 | 'invoiceNumber' => $row['invoiceNumber'], |
| 996 | 'providers' => (int)$row['providerId'] ? [ |
| 997 | [ |
| 998 | 'id' => (int)$row['providerId'], |
| 999 | 'fullName' => $row['providerFirstName'] . ' ' . $row['providerLastName'], |
| 1000 | 'email' => $row['providerEmail'], |
| 1001 | ] |
| 1002 | ] : [], |
| 1003 | 'location' => (int)$row['locationId'] || $row['location_address'] ? |
| 1004 | [ |
| 1005 | 'id' => (int)$row['locationId'], |
| 1006 | 'location_name' => $row['location_name'], |
| 1007 | 'location_address' => $row['location_address'], |
| 1008 | ] |
| 1009 | : null, |
| 1010 | 'customerId' => (int)$row['customerId'], |
| 1011 | 'serviceId' => (int)$row['serviceId'] ?: null, |
| 1012 | 'appointmentId' => (int)$row['appointmentId'] ?: null, |
| 1013 | 'packageId' => (int)$row['packageId'] ?: null, |
| 1014 | 'bookedPrice' => (float)$row['bookedPrice'] ?: null, |
| 1015 | 'bookedTax' => $row['bookedTax'] ? : null, |
| 1016 | 'bookingId' => !empty($row['bookingId']) ? (int)$row['bookingId'] : null, |
| 1017 | 'bookableName' => $row['bookableName'], |
| 1018 | 'customerFirstName' => $customerInfo ? $customerInfo['firstName'] : $row['customerFirstName'], |
| 1019 | 'customerLastName' => $customerInfo ? $customerInfo['lastName'] : $row['customerLastName'], |
| 1020 | 'info' => $row['info'], |
| 1021 | 'customerEmail' => $row['customerEmail'], |
| 1022 | 'customerStatus' => $row['customerStatus'], |
| 1023 | 'coupon' => !empty($row['coupon_id']) ? [ |
| 1024 | 'id' => (int)$row['coupon_id'], |
| 1025 | 'discount' => (float)$row['coupon_discount'], |
| 1026 | 'deduction' => (float)$row['coupon_deduction'], |
| 1027 | 'code' => $row['coupon_code'] |
| 1028 | ] : null, |
| 1029 | 'persons' => (int)$row['persons'], |
| 1030 | 'aggregatedPrice' => (bool)$row['aggregatedPrice'], |
| 1031 | 'extras' => [], |
| 1032 | ]; |
| 1033 | } |
| 1034 | |
| 1035 | if ($result[(int)$row['id']] && $row['bookingExtra_id']) { |
| 1036 | $result[(int)$row['id']]['extras'][] = [ |
| 1037 | 'id' => (int)$row['bookingExtra_id'], |
| 1038 | 'quantity' => (int)$row['bookingExtra_quantity'], |
| 1039 | 'price' => (float)$row['bookingExtra_price'], |
| 1040 | 'aggregatedPrice' => (bool)$row['bookingExtra_aggregatedPrice'], |
| 1041 | 'tax' => $row['bookingExtra_tax'] ?: null |
| 1042 | ]; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | return $result; |
| 1047 | } |
| 1048 | |
| 1049 | /** |
| 1050 | * @param array $ids |
| 1051 | * |
| 1052 | * @return array |
| 1053 | * @throws QueryExecutionException |
| 1054 | * @throws InvalidArgumentException |
| 1055 | */ |
| 1056 | public function getAppointmentsPaymentsByIds($ids) |
| 1057 | { |
| 1058 | $params = []; |
| 1059 | |
| 1060 | $where = []; |
| 1061 | |
| 1062 | if (!empty($ids)) { |
| 1063 | foreach ($ids as $index => $value) { |
| 1064 | $param = ':sId' . $index; |
| 1065 | |
| 1066 | $params[$param] = $value; |
| 1067 | } |
| 1068 | |
| 1069 | $where[] = 'p.id IN (' . implode(', ', array_keys($params)) . ')'; |
| 1070 | } |
| 1071 | |
| 1072 | $where = $where ? ' AND ' . implode(' AND ', $where) : ''; |
| 1073 | |
| 1074 | $customerBookingsExtrasTable = CustomerBookingsToExtrasTable::getTableName(); |
| 1075 | |
| 1076 | $couponsTable = CouponsTable::getTableName(); |
| 1077 | |
| 1078 | $locationsTable = LocationsTable::getTableName(); |
| 1079 | |
| 1080 | try { |
| 1081 | $statement = $this->connection->prepare( |
| 1082 | "SELECT |
| 1083 | p.id AS id, |
| 1084 | p.customerBookingId AS customerBookingId, |
| 1085 | NULL AS packageCustomerId, |
| 1086 | p.amount AS amount, |
| 1087 | p.invoiceNumber AS invoiceNumber, |
| 1088 | p.dateTime AS dateTime, |
| 1089 | p.created AS created, |
| 1090 | p.status AS status, |
| 1091 | p.wcOrderId AS wcOrderId, |
| 1092 | p.wcOrderItemId AS wcOrderItemId, |
| 1093 | p.gateway AS gateway, |
| 1094 | p.gatewayTitle AS gatewayTitle, |
| 1095 | p.transactionId AS transactionId, |
| 1096 | p.parentId AS parentId, |
| 1097 | p.entity AS type, |
| 1098 | |
| 1099 | NULL AS packageId, |
| 1100 | cb.id AS bookingId, |
| 1101 | cb.price AS bookedPrice, |
| 1102 | cb.tax AS bookedTax, |
| 1103 | a.providerId AS providerId, |
| 1104 | cb.customerId AS customerId, |
| 1105 | cb.persons AS persons, |
| 1106 | cb.aggregatedPrice AS aggregatedPrice, |
| 1107 | cb.info AS info, |
| 1108 | |
| 1109 | cbe.id AS bookingExtra_id, |
| 1110 | cbe.quantity AS bookingExtra_quantity, |
| 1111 | cbe.price AS bookingExtra_price, |
| 1112 | cbe.aggregatedPrice AS bookingExtra_aggregatedPrice, |
| 1113 | cbe.tax AS bookingExtra_tax, |
| 1114 | |
| 1115 | c.id AS coupon_id, |
| 1116 | c.discount AS coupon_discount, |
| 1117 | c.deduction AS coupon_deduction, |
| 1118 | c.code AS coupon_code, |
| 1119 | |
| 1120 | a.serviceId AS serviceId, |
| 1121 | a.id AS appointmentId, |
| 1122 | a.bookingStart AS bookingStart, |
| 1123 | s.name AS bookableName, |
| 1124 | cu.firstName AS customerFirstName, |
| 1125 | cu.lastName AS customerLastName, |
| 1126 | cu.email AS customerEmail, |
| 1127 | cu.status AS customerStatus, |
| 1128 | pu.firstName AS providerFirstName, |
| 1129 | pu.lastName AS providerLastName, |
| 1130 | pu.email AS providerEmail, |
| 1131 | l.id AS locationId, |
| 1132 | l.name AS location_name, |
| 1133 | l.address AS location_address |
| 1134 | FROM {$this->table} p |
| 1135 | INNER JOIN {$this->bookingsTable} cb ON cb.id = p.customerBookingId |
| 1136 | LEFT JOIN {$customerBookingsExtrasTable} cbe ON cbe.customerBookingId = cb.id |
| 1137 | LEFT JOIN {$couponsTable} c ON c.id = cb.couponId |
| 1138 | INNER JOIN {$this->appointmentsTable} a ON a.id = cb.appointmentId |
| 1139 | INNER JOIN {$this->servicesTable} s ON s.id = a.serviceId |
| 1140 | INNER JOIN {$this->usersTable} cu ON cu.id = cb.customerId |
| 1141 | INNER JOIN {$this->usersTable} pu ON pu.id = a.providerId |
| 1142 | LEFT JOIN {$locationsTable} l ON l.id = a.locationId |
| 1143 | WHERE 1=1 {$where}" |
| 1144 | ); |
| 1145 | |
| 1146 | $statement->execute($params); |
| 1147 | |
| 1148 | $rows = $statement->fetchAll(); |
| 1149 | } catch (\Exception $e) { |
| 1150 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e); |
| 1151 | } |
| 1152 | |
| 1153 | return $this->getEntitiesPaymentsResult($rows); |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * @param array $ids |
| 1158 | * |
| 1159 | * @return array |
| 1160 | * @throws QueryExecutionException |
| 1161 | * @throws InvalidArgumentException |
| 1162 | */ |
| 1163 | public function getEventsPaymentsByIds($ids) |
| 1164 | { |
| 1165 | $params = []; |
| 1166 | |
| 1167 | $where = []; |
| 1168 | |
| 1169 | if (!empty($ids)) { |
| 1170 | foreach ($ids as $index => $value) { |
| 1171 | $param = ':eId' . $index; |
| 1172 | |
| 1173 | $params[$param] = $value; |
| 1174 | } |
| 1175 | |
| 1176 | $where[] = 'p.id IN (' . implode(', ', array_keys($params)) . ')'; |
| 1177 | } |
| 1178 | |
| 1179 | $where = $where ? ' AND ' . implode(' AND ', $where) : ''; |
| 1180 | |
| 1181 | $couponsTable = CouponsTable::getTableName(); |
| 1182 | |
| 1183 | $locationsTable = LocationsTable::getTableName(); |
| 1184 | |
| 1185 | try { |
| 1186 | $statement = $this->connection->prepare( |
| 1187 | "SELECT |
| 1188 | p.id AS id, |
| 1189 | p.customerBookingId AS customerBookingId, |
| 1190 | NULL AS packageCustomerId, |
| 1191 | p.amount AS amount, |
| 1192 | p.invoiceNumber AS invoiceNumber, |
| 1193 | p.dateTime AS dateTime, |
| 1194 | p.created AS created, |
| 1195 | p.status AS status, |
| 1196 | p.wcOrderId AS wcOrderId, |
| 1197 | p.wcOrderItemId AS wcOrderItemId, |
| 1198 | p.gateway AS gateway, |
| 1199 | p.gatewayTitle AS gatewayTitle, |
| 1200 | p.transactionId AS transactionId, |
| 1201 | p.parentId AS parentId, |
| 1202 | p.entity AS type, |
| 1203 | |
| 1204 | NULL AS packageId, |
| 1205 | cb.id AS bookingId, |
| 1206 | cb.price AS bookedPrice, |
| 1207 | cb.tax AS bookedTax, |
| 1208 | NULL AS providerId, |
| 1209 | cb.customerId AS customerId, |
| 1210 | cb.persons AS persons, |
| 1211 | cb.aggregatedPrice AS aggregatedPrice, |
| 1212 | cb.info AS info, |
| 1213 | |
| 1214 | NULL AS bookingExtra_id, |
| 1215 | NULL AS bookingExtra_quantity, |
| 1216 | NULL AS bookingExtra_price, |
| 1217 | NULL AS bookingExtra_aggregatedPrice, |
| 1218 | NULL AS bookingExtra_tax, |
| 1219 | |
| 1220 | c.id AS coupon_id, |
| 1221 | c.discount AS coupon_discount, |
| 1222 | c.deduction AS coupon_deduction, |
| 1223 | c.code AS coupon_code, |
| 1224 | |
| 1225 | NULL AS serviceId, |
| 1226 | NULL AS appointmentId, |
| 1227 | NULL AS bookingStart, |
| 1228 | NULL AS bookableName, |
| 1229 | cu.firstName AS customerFirstName, |
| 1230 | cu.lastName AS customerLastName, |
| 1231 | cu.email AS customerEmail, |
| 1232 | cu.status AS customerStatus, |
| 1233 | NULL AS providerFirstName, |
| 1234 | NULL AS providerLastName, |
| 1235 | NULL AS providerEmail, |
| 1236 | l.id AS locationId, |
| 1237 | l.name AS location_name, |
| 1238 | (CASE WHEN e.customlocation IS NOT NULL THEN e.customlocation ELSE l.address END) AS location_address |
| 1239 | FROM {$this->table} p |
| 1240 | INNER JOIN {$this->bookingsTable} cb ON cb.id = p.customerBookingId |
| 1241 | LEFT JOIN {$couponsTable} c ON c.id = cb.couponId |
| 1242 | INNER JOIN {$this->usersTable} cu ON cu.id = cb.customerId |
| 1243 | INNER JOIN {$this->customerBookingsToEventsPeriodsTable} cbe ON cbe.customerBookingId = cb.id |
| 1244 | INNER JOIN {$this->eventsPeriodsTable} ep ON ep.id = cbe.eventPeriodId |
| 1245 | INNER JOIN {$this->eventsTable} e ON e.id = ep.eventId |
| 1246 | LEFT JOIN {$this->eventsProvidersTable} epu ON epu.eventId = ep.eventId |
| 1247 | LEFT JOIN {$locationsTable} l ON l.id = e.locationId |
| 1248 | WHERE 1=1 {$where}" |
| 1249 | ); |
| 1250 | |
| 1251 | $statement->execute($params); |
| 1252 | |
| 1253 | $rows = $statement->fetchAll(); |
| 1254 | } catch (\Exception $e) { |
| 1255 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e); |
| 1256 | } |
| 1257 | |
| 1258 | return $this->getEntitiesPaymentsResult($rows); |
| 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * @param array $ids |
| 1263 | * |
| 1264 | * @return array |
| 1265 | * @throws QueryExecutionException |
| 1266 | * @throws InvalidArgumentException |
| 1267 | */ |
| 1268 | public function getPackagesPaymentsByIds($ids) |
| 1269 | { |
| 1270 | $params = []; |
| 1271 | |
| 1272 | $where = []; |
| 1273 | |
| 1274 | if (!empty($ids)) { |
| 1275 | foreach ($ids as $index => $value) { |
| 1276 | $param = ':pId' . $index; |
| 1277 | |
| 1278 | $params[$param] = $value; |
| 1279 | } |
| 1280 | |
| 1281 | $where[] = 'p.id IN (' . implode(', ', array_keys($params)) . ')'; |
| 1282 | } |
| 1283 | |
| 1284 | $where = $where ? ' AND ' . implode(' AND ', $where) : ''; |
| 1285 | |
| 1286 | $couponsTable = CouponsTable::getTableName(); |
| 1287 | |
| 1288 | try { |
| 1289 | $statement = $this->connection->prepare( |
| 1290 | "SELECT |
| 1291 | p.id AS id, |
| 1292 | NULL AS customerBookingId, |
| 1293 | p.packageCustomerId AS packageCustomerId, |
| 1294 | p.amount AS amount, |
| 1295 | p.invoiceNumber AS invoiceNumber, |
| 1296 | p.dateTime AS dateTime, |
| 1297 | p.created AS created, |
| 1298 | p.status AS status, |
| 1299 | p.wcOrderId AS wcOrderId, |
| 1300 | p.wcOrderItemId AS wcOrderItemId, |
| 1301 | p.gateway AS gateway, |
| 1302 | p.gatewayTitle AS gatewayTitle, |
| 1303 | p.transactionId AS transactionId, |
| 1304 | p.parentId AS parentId, |
| 1305 | p.entity AS type, |
| 1306 | |
| 1307 | pc.packageId AS packageId, |
| 1308 | pc.id AS bookingId, |
| 1309 | pc.price AS bookedPrice, |
| 1310 | pc.tax AS bookedTax, |
| 1311 | NULL AS providerId, |
| 1312 | pc.customerId AS customerId, |
| 1313 | NULL AS persons, |
| 1314 | NULL AS aggregatedPrice, |
| 1315 | cb.info AS info, |
| 1316 | |
| 1317 | NULL AS bookingExtra_id, |
| 1318 | NULL AS bookingExtra_quantity, |
| 1319 | NULL AS bookingExtra_price, |
| 1320 | NULL AS bookingExtra_aggregatedPrice, |
| 1321 | NULL AS bookingExtra_tax, |
| 1322 | |
| 1323 | c.id AS coupon_id, |
| 1324 | c.discount AS coupon_discount, |
| 1325 | c.deduction AS coupon_deduction, |
| 1326 | c.code AS coupon_code, |
| 1327 | |
| 1328 | NULL AS serviceId, |
| 1329 | NULL AS appointmentId, |
| 1330 | NULL AS bookingStart, |
| 1331 | pa.name AS bookableName, |
| 1332 | cu.firstName AS customerFirstName, |
| 1333 | cu.lastName AS customerLastName, |
| 1334 | cu.email AS customerEmail, |
| 1335 | cu.status AS customerStatus, |
| 1336 | '' AS providerFirstName, |
| 1337 | '' AS providerLastName, |
| 1338 | '' AS providerEmail, |
| 1339 | '' AS locationId, |
| 1340 | '' AS location_name, |
| 1341 | '' AS location_address |
| 1342 | FROM {$this->table} p |
| 1343 | INNER JOIN {$this->packagesCustomersTable} pc ON p.packageCustomerId = pc.id |
| 1344 | INNER JOIN {$this->usersTable} cu ON cu.id = pc.customerId |
| 1345 | LEFT JOIN {$couponsTable} c ON c.id = pc.couponId |
| 1346 | INNER JOIN {$this->packagesTable} pa ON pa.id = pc.packageId |
| 1347 | INNER JOIN {$this->packagesCustomersServiceTable} pcs ON pc.id = pcs.packageCustomerId |
| 1348 | LEFT JOIN {$this->bookingsTable} cb ON cb.packageCustomerServiceId = pcs.id |
| 1349 | LEFT JOIN {$this->appointmentsTable} a ON a.id = cb.appointmentId |
| 1350 | WHERE 1=1 {$where} ORDER BY p.id ASC" |
| 1351 | ); |
| 1352 | |
| 1353 | $statement->execute($params); |
| 1354 | |
| 1355 | $rows = $statement->fetchAll(); |
| 1356 | } catch (\Exception $e) { |
| 1357 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e); |
| 1358 | } |
| 1359 | |
| 1360 | return $this->getEntitiesPaymentsResult($rows); |
| 1361 | } |
| 1362 | } |
| 1363 |