PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.32
Booking for Appointments and Events Calendar – Amelia v1.2.32
2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Infrastructure / Repository / Bookable / Service / PackageCustomerServiceRepository.php
ameliabooking / src / Infrastructure / Repository / Bookable / Service Last commit date
CategoryRepository.php 1 year ago ExtraRepository.php 1 year ago PackageCustomerRepository.php 10 months ago PackageCustomerServiceRepository.php 1 year ago PackageRepository.php 1 year ago PackageServiceLocationRepository.php 1 year ago PackageServiceProviderRepository.php 1 year ago PackageServiceRepository.php 1 year ago ProviderServiceRepository.php 1 year ago ResourceEntitiesRepository.php 1 year ago ResourceRepository.php 1 year ago ServiceRepository.php 1 year ago
PackageCustomerServiceRepository.php
261 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 public 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[] = "(pc.purchased 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 >= pc.start AND :from2 <= pc.end) OR (:from3 <= pc.start AND :to1 >= pc.start)) ";
140
141 $params[':from1'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]);
142 $params[':from2'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]);
143 $params[':from3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]);
144
145 $params[':to1'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]);
146 }
147
148 if (!empty($criteria['customerId'])) {
149 $params[':customerId'] = $criteria['customerId'];
150
151 $where[] = 'pc.customerId = :customerId';
152 }
153
154 if (!empty($criteria['services'])) {
155 $queryServices = [];
156
157 foreach ($criteria['services'] as $index => $value) {
158 $param = ':service' . $index;
159
160 $queryServices[] = $param;
161
162 $params[$param] = $value;
163 }
164
165 $where[] = 'pcs.serviceId IN (' . implode(', ', $queryServices) . ')';
166 }
167
168 if (!empty($criteria['packages'])) {
169 $queryServices = [];
170
171 foreach ($criteria['packages'] as $index => $value) {
172 $param = ':package' . $index;
173
174 $queryServices[] = $param;
175
176 $params[$param] = $value;
177 }
178
179 $where[] = 'pc.packageId IN (' . implode(', ', $queryServices) . ')';
180 }
181
182 if (!empty($criteria['packagesCustomers'])) {
183 $queryServices = [];
184
185 foreach ($criteria['packagesCustomers'] as $index => $value) {
186 $param = ':packageCustomerId' . $index;
187
188 $queryServices[] = $param;
189
190 $params[$param] = $value;
191 }
192
193 $where[] = 'pc.id IN (' . implode(', ', $queryServices) . ')';
194 }
195
196 if ($empty) {
197 $where[] = 'pcs.id NOT IN (SELECT packageCustomerServiceId FROM ' . $bookingsTable . ' WHERE packageCustomerServiceId IS NOT NULL)';
198 }
199
200 $where = $where ? 'WHERE ' . implode(' AND ', $where) : '';
201
202 $usersTable = UsersTable::getTableName();
203
204 try {
205 $statement = $this->connection->prepare(
206 "SELECT
207 pc.id AS package_customer_id,
208 pc.packageId AS package_customer_packageId,
209 pc.customerId AS package_customer_customerId,
210 pc.tax AS package_customer_tax,
211 pc.price AS package_customer_price,
212 pc.end AS package_customer_end,
213 pc.start AS package_customer_start,
214 pc.purchased AS package_customer_purchased,
215 pc.status AS package_customer_status,
216 pc.bookingsCount AS package_customer_bookingsCount,
217 pc.couponId AS package_customer_couponId,
218
219 pcs.id AS package_customer_service_id,
220 pcs.serviceId AS package_customer_service_serviceId,
221 pcs.providerId AS package_customer_service_providerId,
222 pcs.locationId AS package_customer_service_locationId,
223 pcs.bookingsCount AS package_customer_service_bookingsCount,
224
225 p.id AS payment_id,
226 p.packageCustomerId AS payment_packageCustomerId,
227 p.amount AS payment_amount,
228 p.dateTime AS payment_dateTime,
229 p.status AS payment_status,
230 p.gateway AS payment_gateway,
231 p.gatewayTitle AS payment_gatewayTitle,
232 p.transactionId AS payment_transactionId,
233 p.data AS payment_data,
234 p.wcOrderId AS payment_wcOrderId,
235 p.wcOrderItemId AS payment_wcOrderItemId,
236 p.invoiceNumber AS payment_invoiceNumber,
237 p.created AS payment_created,
238
239 cu.firstName AS customer_firstName,
240 cu.lastName AS customer_lastName,
241 cu.email AS customer_email,
242 cu.phone AS customer_phone,
243 cu.status AS customer_status
244 FROM {$this->table} pcs
245 INNER JOIN {$this->packagesCustomersTable} pc ON pcs.packageCustomerId = pc.id
246 INNER JOIN {$usersTable} cu ON cu.id = pc.customerId
247 LEFT JOIN {$this->paymentsTable} p ON p.packageCustomerId = pc.id
248 {$where}"
249 );
250
251 $statement->execute($params);
252
253 $rows = $statement->fetchAll();
254 } catch (\Exception $e) {
255 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e);
256 }
257
258 return call_user_func([static::FACTORY, 'createCollection'], $rows);
259 }
260 }
261