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 / PackageCustomerRepository.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
PackageCustomerRepository.php
158 lines
1 <?php
2
3 namespace AmeliaBooking\Infrastructure\Repository\Bookable\Service;
4
5 use AmeliaBooking\Domain\Entity\Bookable\Service\Package;
6 use AmeliaBooking\Domain\Entity\Bookable\Service\PackageCustomer;
7 use AmeliaBooking\Domain\Factory\Bookable\Service\PackageCustomerFactory;
8 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
9 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
10 use AmeliaBooking\Infrastructure\Repository\AbstractRepository;
11
12 /**
13 * Class PackageCustomerRepository
14 *
15 * @package AmeliaBooking\Infrastructure\Repository\Bookable\Service
16 */
17 class PackageCustomerRepository extends AbstractRepository
18 {
19 const FACTORY = PackageCustomerFactory::class;
20
21 /**
22 * @param PackageCustomer $entity
23 *
24 * @return int
25 * @throws QueryExecutionException
26 */
27 public function add($entity)
28 {
29 $data = $entity->toArray();
30
31 $params = [
32 ':packageId' => $data['packageId'],
33 ':customerId' => $data['customerId'],
34 ':price' => $data['price'],
35 ':tax' => !empty($data['tax']) ? json_encode($data['tax']) : null,
36 ':start' => $data['start'],
37 ':end' => $data['end'],
38 ':purchased' => $data['purchased'],
39 ':bookingsCount' => $data['bookingsCount'],
40 ':couponId' => $data['couponId'],
41 ];
42
43 try {
44 $statement = $this->connection->prepare(
45 "INSERT INTO {$this->table}
46 (`packageId`, `customerId`, `price`, `tax`, `start`, `end`, `purchased`, `status`, `bookingsCount`, `couponId`)
47 VALUES
48 (:packageId, :customerId, :price, :tax, :start, :end, :purchased, 'approved', :bookingsCount, :couponId)"
49 );
50
51 $res = $statement->execute($params);
52
53 if (!$res) {
54 throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
55 }
56 } catch (\Exception $e) {
57 throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e);
58 }
59
60 return $this->connection->lastInsertId();
61 }
62
63
64 /**
65 * @param Package $package
66 * @param int $customerId
67 * @param array $limitPerCustomer
68 * @param boolean $packageSpecific
69 * @return int
70 * @throws QueryExecutionException
71 */
72 public function getUserPackageCount($package, $customerId, $limitPerCustomer, $packageSpecific)
73 {
74 $params = [
75 ':customerId' => $customerId
76 ];
77
78 $startDate = DateTimeService::getNowDateTimeObject()->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i');
79
80 $intervalString = "interval " . $limitPerCustomer['period'] . " " . $limitPerCustomer['timeFrame'];
81
82 $where = "(STR_TO_DATE('" . $startDate . "', '%Y-%m-%d %H:%i:%s') BETWEEN " .
83 "(pc.purchased - " . $intervalString . " + interval 1 second) AND " .
84 "(pc.purchased + " . $intervalString . " - interval 1 second))"; //+ interval 2 day
85
86 if ($packageSpecific) {
87 $where .= " AND pc.packageId = :packageId";
88 $params[':packageId'] = $package->getId()->getValue();
89 }
90
91 try {
92 $statement = $this->connection->prepare(
93 "SELECT COUNT(DISTINCT pc.id) AS count
94 FROM {$this->table} pc
95 WHERE pc.customerId = :customerId AND {$where} AND pc.status = 'approved'
96 "
97 );
98
99 $statement->execute($params);
100
101 $rows = $statement->fetch()['count'];
102 } catch (\Exception $e) {
103 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e);
104 }
105
106 return $rows;
107 }
108
109 /**
110 * @param array $criteria
111 *
112 * @return array
113 * @throws QueryExecutionException
114 * @throws InvalidArgumentException
115 */
116 public function getFiltered($criteria)
117 {
118 $params = [];
119
120 $where = [];
121
122 if (!empty($criteria['customerId'])) {
123 $params[':customerId'] = $criteria['customerId'];
124
125 $where[] = 'pc.customerId = :customerId';
126 }
127
128 if (array_key_exists('bookingStatus', $criteria)) {
129 $where[] = 'pc.status = :bookingStatus';
130 $params[':bookingStatus'] = $criteria['bookingStatus'];
131 }
132
133 if (isset($criteria['couponId'])) {
134 $where[] = "pc.couponId = {$criteria['couponId']}";
135 }
136
137 $where = $where ? 'WHERE ' . implode(' AND ', $where) : '';
138
139 try {
140 $statement = $this->connection->prepare(
141 "SELECT
142 pc.customerId
143 FROM {$this->table} pc
144 $where"
145 );
146
147 $statement->execute($params);
148
149 $rows = $statement->fetchAll();
150 } catch (Exception $e) {
151 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e);
152 }
153
154 return $rows;
155 }
156
157 }
158