PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.5
Booking for Appointments and Events Calendar – Amelia v1.2.5
2.4.9 2.4.8 2.4.7 2.4.6 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 1 year 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
248 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['purchased'])) {
117 $where[] = "(DATE_FORMAT(pc.purchased, '%Y-%m-%d %H:%i:%s') BETWEEN :purchasedFrom AND :purchasedTo)";
118
119 $params[':purchasedFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['purchased'][0]);
120
121 $params[':purchasedTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['purchased'][1]);
122 }
123
124 if (!empty($criteria['dates'])) {
125 $where[] = "((:from1 >= DATE_FORMAT(pc.start, '%Y-%m-%d %H:%i:%s') AND
126 :from2 <= DATE_FORMAT(pc.end, '%Y-%m-%d %H:%i:%s')
127 ) OR (
128 :from3 <= DATE_FORMAT(pc.start, '%Y-%m-%d %H:%i:%s') AND
129 :to1 >= DATE_FORMAT(pc.start, '%Y-%m-%d %H:%i:%s'))) ";
130
131 $params[':from1'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]);
132 $params[':from2'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]);
133 $params[':from3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]);
134
135 $params[':to1'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]);
136 }
137
138 if (!empty($criteria['customerId'])) {
139 $params[':customerId'] = $criteria['customerId'];
140
141 $where[] = 'pc.customerId = :customerId';
142 }
143
144 if (!empty($criteria['services'])) {
145 $queryServices = [];
146
147 foreach ($criteria['services'] as $index => $value) {
148 $param = ':service' . $index;
149
150 $queryServices[] = $param;
151
152 $params[$param] = $value;
153 }
154
155 $where[] = 'pcs.serviceId IN (' . implode(', ', $queryServices) . ')';
156 }
157
158 if (!empty($criteria['packages'])) {
159 $queryServices = [];
160
161 foreach ($criteria['packages'] as $index => $value) {
162 $param = ':package' . $index;
163
164 $queryServices[] = $param;
165
166 $params[$param] = $value;
167 }
168
169 $where[] = 'pc.packageId IN (' . implode(', ', $queryServices) . ')';
170 }
171
172 if (!empty($criteria['packagesCustomers'])) {
173 $queryServices = [];
174
175 foreach ($criteria['packagesCustomers'] as $index => $value) {
176 $param = ':packageCustomerId' . $index;
177
178 $queryServices[] = $param;
179
180 $params[$param] = $value;
181 }
182
183 $where[] = 'pc.id IN (' . implode(', ', $queryServices) . ')';
184 }
185
186 if ($empty) {
187 $where[] = 'pcs.id NOT IN (SELECT packageCustomerServiceId FROM ' . $bookingsTable . ' WHERE packageCustomerServiceId IS NOT NULL)';
188 }
189
190 $where = $where ? 'WHERE ' . implode(' AND ', $where) : '';
191
192 $usersTable = UsersTable::getTableName();
193
194 try {
195 $statement = $this->connection->prepare(
196 "SELECT
197 pc.id AS package_customer_id,
198 pc.packageId AS package_customer_packageId,
199 pc.customerId AS package_customer_customerId,
200 pc.tax AS package_customer_tax,
201 pc.price AS package_customer_price,
202 pc.end AS package_customer_end,
203 pc.start AS package_customer_start,
204 pc.purchased AS package_customer_purchased,
205 pc.status AS package_customer_status,
206 pc.bookingsCount AS package_customer_bookingsCount,
207 pc.couponId AS package_customer_couponId,
208
209 pcs.id AS package_customer_service_id,
210 pcs.serviceId AS package_customer_service_serviceId,
211 pcs.providerId AS package_customer_service_providerId,
212 pcs.locationId AS package_customer_service_locationId,
213 pcs.bookingsCount AS package_customer_service_bookingsCount,
214
215 p.id AS payment_id,
216 p.packageCustomerId AS payment_packageCustomerId,
217 p.amount AS payment_amount,
218 p.dateTime AS payment_dateTime,
219 p.status AS payment_status,
220 p.gateway AS payment_gateway,
221 p.gatewayTitle AS payment_gatewayTitle,
222 p.transactionId AS payment_transactionId,
223 p.data AS payment_data,
224 p.wcOrderId AS payment_wcOrderId,
225 p.wcOrderItemId AS payment_wcOrderItemId,
226
227 cu.firstName AS customer_firstName,
228 cu.lastName AS customer_lastName,
229 cu.email AS customer_email,
230 cu.phone AS customer_phone
231 FROM {$this->table} pcs
232 INNER JOIN {$this->packagesCustomersTable} pc ON pcs.packageCustomerId = pc.id
233 INNER JOIN {$usersTable} cu ON cu.id = pc.customerId
234 LEFT JOIN {$this->paymentsTable} p ON p.packageCustomerId = pc.id
235 {$where}"
236 );
237
238 $statement->execute($params);
239
240 $rows = $statement->fetchAll();
241 } catch (\Exception $e) {
242 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e);
243 }
244
245 return call_user_func([static::FACTORY, 'createCollection'], $rows);
246 }
247 }
248