ameliabooking
/
src
/
Infrastructure
/
Repository
/
Bookable
/
Service
/
PackageCustomerServiceRepository.php
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
PackageCustomerServiceRepository.php
265 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Bookable\Service; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Collection\Collection; |
| 6 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 7 | use AmeliaBooking\Domain\Entity\Bookable\Service\PackageCustomerService; |
| 8 | use AmeliaBooking\Domain\Factory\Bookable\Service\PackageCustomerServiceFactory; |
| 9 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 10 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 11 | use AmeliaBooking\Infrastructure\Connection; |
| 12 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 13 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\PackagesCustomersTable; |
| 14 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\CustomerBookingsTable; |
| 15 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Payment\PaymentsTable; |
| 16 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\UsersTable; |
| 17 | |
| 18 | /** |
| 19 | * Class PackageCustomerServiceRepository |
| 20 | * |
| 21 | * @package AmeliaBooking\Infrastructure\Repository\Bookable\Service |
| 22 | */ |
| 23 | class PackageCustomerServiceRepository extends AbstractRepository |
| 24 | { |
| 25 | const FACTORY = PackageCustomerServiceFactory::class; |
| 26 | |
| 27 | /** @var string */ |
| 28 | protected $packagesCustomersTable; |
| 29 | |
| 30 | /** @var string */ |
| 31 | protected $paymentsTable; |
| 32 | |
| 33 | /** |
| 34 | * @param Connection $connection |
| 35 | * @param string $table |
| 36 | * |
| 37 | * @throws InvalidArgumentException |
| 38 | */ |
| 39 | public function __construct( |
| 40 | Connection $connection, |
| 41 | $table |
| 42 | ) { |
| 43 | parent::__construct($connection, $table); |
| 44 | |
| 45 | $this->packagesCustomersTable = PackagesCustomersTable::getTableName(); |
| 46 | |
| 47 | $this->paymentsTable = PaymentsTable::getTableName(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param PackageCustomerService $entity |
| 52 | * |
| 53 | * @return int |
| 54 | * @throws QueryExecutionException |
| 55 | */ |
| 56 | public function add($entity) |
| 57 | { |
| 58 | $data = $entity->toArray(); |
| 59 | |
| 60 | $params = [ |
| 61 | ':packageCustomerId' => $data['packageCustomer']['id'], |
| 62 | ':serviceId' => $data['serviceId'], |
| 63 | ':providerId' => $data['providerId'], |
| 64 | ':locationId' => $data['locationId'], |
| 65 | ':bookingsCount' => $data['bookingsCount'], |
| 66 | ]; |
| 67 | |
| 68 | try { |
| 69 | $statement = $this->connection->prepare( |
| 70 | "INSERT INTO {$this->table} |
| 71 | (`packageCustomerId`, `serviceId`, `providerId`, `locationId`, `bookingsCount`) |
| 72 | VALUES |
| 73 | (:packageCustomerId, :serviceId, :providerId, :locationId, :bookingsCount)" |
| 74 | ); |
| 75 | |
| 76 | $res = $statement->execute($params); |
| 77 | if (!$res) { |
| 78 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 79 | } |
| 80 | } catch (\Exception $e) { |
| 81 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 82 | } |
| 83 | |
| 84 | return $this->connection->lastInsertId(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param array $criteria |
| 89 | * @param bool $empty |
| 90 | * @return Collection |
| 91 | * @throws InvalidArgumentException |
| 92 | * @throws QueryExecutionException |
| 93 | */ |
| 94 | public function getByCriteria($criteria, $empty = false) |
| 95 | { |
| 96 | $bookingsTable = CustomerBookingsTable::getTableName(); |
| 97 | |
| 98 | $params = []; |
| 99 | |
| 100 | $where = []; |
| 101 | |
| 102 | if (!empty($criteria['ids'])) { |
| 103 | $queryIds = []; |
| 104 | |
| 105 | foreach ($criteria['ids'] as $index => $value) { |
| 106 | $param = ':id' . $index; |
| 107 | |
| 108 | $queryIds[] = $param; |
| 109 | |
| 110 | $params[$param] = $value; |
| 111 | } |
| 112 | |
| 113 | $where[] = 'pcs.id IN (' . implode(', ', $queryIds) . ')'; |
| 114 | } |
| 115 | |
| 116 | if (!empty($criteria['packageCustomerIds'])) { |
| 117 | $queryIds = []; |
| 118 | |
| 119 | foreach ($criteria['packageCustomerIds'] as $index => $value) { |
| 120 | $param = ':id' . $index; |
| 121 | |
| 122 | $queryIds[] = $param; |
| 123 | |
| 124 | $params[$param] = $value; |
| 125 | } |
| 126 | |
| 127 | $where[] = 'pc.id IN (' . implode(', ', $queryIds) . ')'; |
| 128 | } |
| 129 | |
| 130 | if (!empty($criteria['purchased'])) { |
| 131 | $where[] = "(DATE_FORMAT(pc.purchased, '%Y-%m-%d %H:%i:%s') BETWEEN :purchasedFrom AND :purchasedTo)"; |
| 132 | |
| 133 | $params[':purchasedFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['purchased'][0]); |
| 134 | |
| 135 | $params[':purchasedTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['purchased'][1]); |
| 136 | } |
| 137 | |
| 138 | if (!empty($criteria['dates'])) { |
| 139 | $where[] = "((:from1 >= DATE_FORMAT(pc.start, '%Y-%m-%d %H:%i:%s') AND |
| 140 | :from2 <= DATE_FORMAT(pc.end, '%Y-%m-%d %H:%i:%s') |
| 141 | ) OR ( |
| 142 | :from3 <= DATE_FORMAT(pc.start, '%Y-%m-%d %H:%i:%s') AND |
| 143 | :to1 >= DATE_FORMAT(pc.start, '%Y-%m-%d %H:%i:%s'))) "; |
| 144 | |
| 145 | $params[':from1'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 146 | $params[':from2'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 147 | $params[':from3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 148 | |
| 149 | $params[':to1'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 150 | } |
| 151 | |
| 152 | if (!empty($criteria['customerId'])) { |
| 153 | $params[':customerId'] = $criteria['customerId']; |
| 154 | |
| 155 | $where[] = 'pc.customerId = :customerId'; |
| 156 | } |
| 157 | |
| 158 | if (!empty($criteria['services'])) { |
| 159 | $queryServices = []; |
| 160 | |
| 161 | foreach ($criteria['services'] as $index => $value) { |
| 162 | $param = ':service' . $index; |
| 163 | |
| 164 | $queryServices[] = $param; |
| 165 | |
| 166 | $params[$param] = $value; |
| 167 | } |
| 168 | |
| 169 | $where[] = 'pcs.serviceId IN (' . implode(', ', $queryServices) . ')'; |
| 170 | } |
| 171 | |
| 172 | if (!empty($criteria['packages'])) { |
| 173 | $queryServices = []; |
| 174 | |
| 175 | foreach ($criteria['packages'] as $index => $value) { |
| 176 | $param = ':package' . $index; |
| 177 | |
| 178 | $queryServices[] = $param; |
| 179 | |
| 180 | $params[$param] = $value; |
| 181 | } |
| 182 | |
| 183 | $where[] = 'pc.packageId IN (' . implode(', ', $queryServices) . ')'; |
| 184 | } |
| 185 | |
| 186 | if (!empty($criteria['packagesCustomers'])) { |
| 187 | $queryServices = []; |
| 188 | |
| 189 | foreach ($criteria['packagesCustomers'] as $index => $value) { |
| 190 | $param = ':packageCustomerId' . $index; |
| 191 | |
| 192 | $queryServices[] = $param; |
| 193 | |
| 194 | $params[$param] = $value; |
| 195 | } |
| 196 | |
| 197 | $where[] = 'pc.id IN (' . implode(', ', $queryServices) . ')'; |
| 198 | } |
| 199 | |
| 200 | if ($empty) { |
| 201 | $where[] = 'pcs.id NOT IN (SELECT packageCustomerServiceId FROM ' . $bookingsTable . ' WHERE packageCustomerServiceId IS NOT NULL)'; |
| 202 | } |
| 203 | |
| 204 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 205 | |
| 206 | $usersTable = UsersTable::getTableName(); |
| 207 | |
| 208 | try { |
| 209 | $statement = $this->connection->prepare( |
| 210 | "SELECT |
| 211 | pc.id AS package_customer_id, |
| 212 | pc.packageId AS package_customer_packageId, |
| 213 | pc.customerId AS package_customer_customerId, |
| 214 | pc.tax AS package_customer_tax, |
| 215 | pc.price AS package_customer_price, |
| 216 | pc.end AS package_customer_end, |
| 217 | pc.start AS package_customer_start, |
| 218 | pc.purchased AS package_customer_purchased, |
| 219 | pc.status AS package_customer_status, |
| 220 | pc.bookingsCount AS package_customer_bookingsCount, |
| 221 | pc.couponId AS package_customer_couponId, |
| 222 | |
| 223 | pcs.id AS package_customer_service_id, |
| 224 | pcs.serviceId AS package_customer_service_serviceId, |
| 225 | pcs.providerId AS package_customer_service_providerId, |
| 226 | pcs.locationId AS package_customer_service_locationId, |
| 227 | pcs.bookingsCount AS package_customer_service_bookingsCount, |
| 228 | |
| 229 | p.id AS payment_id, |
| 230 | p.packageCustomerId AS payment_packageCustomerId, |
| 231 | p.amount AS payment_amount, |
| 232 | p.dateTime AS payment_dateTime, |
| 233 | p.status AS payment_status, |
| 234 | p.gateway AS payment_gateway, |
| 235 | p.gatewayTitle AS payment_gatewayTitle, |
| 236 | p.transactionId AS payment_transactionId, |
| 237 | p.data AS payment_data, |
| 238 | p.wcOrderId AS payment_wcOrderId, |
| 239 | p.wcOrderItemId AS payment_wcOrderItemId, |
| 240 | p.invoiceNumber AS payment_invoiceNumber, |
| 241 | p.created AS payment_created, |
| 242 | |
| 243 | cu.firstName AS customer_firstName, |
| 244 | cu.lastName AS customer_lastName, |
| 245 | cu.email AS customer_email, |
| 246 | cu.phone AS customer_phone, |
| 247 | cu.status AS customer_status |
| 248 | FROM {$this->table} pcs |
| 249 | INNER JOIN {$this->packagesCustomersTable} pc ON pcs.packageCustomerId = pc.id |
| 250 | INNER JOIN {$usersTable} cu ON cu.id = pc.customerId |
| 251 | LEFT JOIN {$this->paymentsTable} p ON p.packageCustomerId = pc.id |
| 252 | {$where}" |
| 253 | ); |
| 254 | |
| 255 | $statement->execute($params); |
| 256 | |
| 257 | $rows = $statement->fetchAll(); |
| 258 | } catch (\Exception $e) { |
| 259 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 260 | } |
| 261 | |
| 262 | return call_user_func([static::FACTORY, 'createCollection'], $rows); |
| 263 | } |
| 264 | } |
| 265 |