DayOffRepository.php
1 month ago
PeriodLocationRepository.php
3 months ago
PeriodRepository.php
3 months ago
PeriodServiceRepository.php
3 months ago
SpecialDayPeriodLocationRepository.php
3 months ago
SpecialDayPeriodRepository.php
3 months ago
SpecialDayPeriodServiceRepository.php
3 months ago
SpecialDayRepository.php
3 months ago
TimeOutRepository.php
3 months ago
WeekDayRepository.php
3 months ago
DayOffRepository.php
208 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Schedule; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 6 | use AmeliaBooking\Domain\Entity\Schedule\BlockTime; |
| 7 | use AmeliaBooking\Domain\Entity\Schedule\DayOff; |
| 8 | use AmeliaBooking\Domain\Factory\Schedule\BlockTimeFactory; |
| 9 | use AmeliaBooking\Domain\Factory\Schedule\DayOffFactory; |
| 10 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 11 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 12 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 13 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\UsersTable; |
| 14 | |
| 15 | /** |
| 16 | * Class DayOffRepository |
| 17 | * |
| 18 | * @package AmeliaBooking\Infrastructure\Repository\Schedule |
| 19 | */ |
| 20 | class DayOffRepository extends AbstractRepository |
| 21 | { |
| 22 | public const FACTORY = DayOffFactory::class; |
| 23 | |
| 24 | /** |
| 25 | * @param DayOff | BlockTime $entity |
| 26 | * @param int | null $userId |
| 27 | * |
| 28 | * @return int |
| 29 | * @throws QueryExecutionException |
| 30 | */ |
| 31 | public function add($entity, $userId) |
| 32 | { |
| 33 | $data = $entity->toArray(); |
| 34 | |
| 35 | $params = [ |
| 36 | ':userId' => $userId, |
| 37 | ':name' => $data['name'], |
| 38 | ':startDate' => $data['startDate'], |
| 39 | ':endDate' => $data['endDate'], |
| 40 | ':type' => $data['type'], |
| 41 | ':repeat' => $data['repeat'], |
| 42 | ]; |
| 43 | |
| 44 | try { |
| 45 | $statement = $this->connection->prepare( |
| 46 | "INSERT INTO {$this->table} |
| 47 | (`userId`, `name`, `startDate`, `endDate`, `repeat`, `type`) |
| 48 | VALUES |
| 49 | (:userId, :name, :startDate, :endDate, :repeat, :type)" |
| 50 | ); |
| 51 | |
| 52 | $statement->execute($params); |
| 53 | } catch (\Exception $e) { |
| 54 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 55 | } |
| 56 | |
| 57 | return $this->connection->lastInsertId(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param DayOff $entity |
| 62 | * @param int $id |
| 63 | * |
| 64 | * @return void |
| 65 | * @throws QueryExecutionException |
| 66 | */ |
| 67 | public function update($entity, $id) |
| 68 | { |
| 69 | $data = $entity->toArray(); |
| 70 | |
| 71 | $params = [ |
| 72 | ':id' => $id, |
| 73 | ':name' => $data['name'], |
| 74 | ':startDate' => $data['startDate'], |
| 75 | ':endDate' => $data['endDate'], |
| 76 | ':repeat' => $data['repeat'], |
| 77 | ]; |
| 78 | |
| 79 | try { |
| 80 | $statement = $this->connection->prepare( |
| 81 | "UPDATE {$this->table} |
| 82 | SET `name` = :name, `startDate` = :startDate, `endDate` = :endDate, `repeat` = :repeat |
| 83 | WHERE id = :id" |
| 84 | ); |
| 85 | |
| 86 | $statement->execute($params); |
| 87 | } catch (\Exception $e) { |
| 88 | throw new QueryExecutionException('Unable to add save in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param $criteria |
| 94 | * @return mixed |
| 95 | * @throws QueryExecutionException |
| 96 | * @throws InvalidArgumentException |
| 97 | */ |
| 98 | public function getFiltered($criteria) |
| 99 | { |
| 100 | $userTable = UsersTable::getTableName(); |
| 101 | |
| 102 | try { |
| 103 | $params = []; |
| 104 | |
| 105 | $where = []; |
| 106 | |
| 107 | if (!empty($criteria['dates'][0]) && !empty($criteria['dates'][1])) { |
| 108 | $where[] = "((do.startDate BETWEEN :dayOffFrom1 AND :dayOffTo1) |
| 109 | OR (do.endDate BETWEEN :dayOffFrom2 AND :dayOffTo2) |
| 110 | OR (:dayOffFrom3 BETWEEN do.startDate AND do.endDate) |
| 111 | OR (:dayOffTo3 BETWEEN do.startDate AND do.endDate))"; |
| 112 | |
| 113 | $params[':dayOffFrom1'] = $params[':dayOffFrom2'] = $params[':dayOffFrom3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 114 | $params[':dayOffTo1'] = $params[':dayOffTo2'] = $params[':dayOffTo3'] = $params[':dateTo'] = |
| 115 | DateTimeService::getCustomDateTimeObjectInUtc($criteria['dates'][1])->modify('+1 day')->format('Y-m-d H:i:s'); |
| 116 | } |
| 117 | |
| 118 | if (isset($criteria['type'])) { |
| 119 | $where[] = 'do.type = :type'; |
| 120 | $params[':type'] = $criteria['type']; |
| 121 | } |
| 122 | |
| 123 | if (!empty($criteria['providers'])) { |
| 124 | $queryProviders = []; |
| 125 | |
| 126 | foreach ((array)$criteria['providers'] as $index => $value) { |
| 127 | $param = ':provider' . $index; |
| 128 | |
| 129 | $queryProviders[] = $param; |
| 130 | |
| 131 | $params[$param] = $value; |
| 132 | } |
| 133 | |
| 134 | $where[] = '(do.userId IN (' . implode(', ', $queryProviders) . ') OR do.userId IS NULL)'; |
| 135 | } |
| 136 | |
| 137 | if (isset($criteria['providerId'])) { |
| 138 | $where[] = '(do.userId = :providerId OR do.userId IS NULL)'; |
| 139 | $params[':providerId'] = $criteria['providerId']; |
| 140 | } |
| 141 | |
| 142 | $userFields = " |
| 143 | u.id AS user_id, |
| 144 | u.firstName AS user_firstName, |
| 145 | u.lastName AS user_lastName, |
| 146 | u.email AS user_email, |
| 147 | CONCAT(u.firstName, ' ', u.lastName) AS user_fullName |
| 148 | "; |
| 149 | |
| 150 | $userJoin = " LEFT JOIN {$userTable} u ON u.id = do.userId"; |
| 151 | |
| 152 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 153 | |
| 154 | $statement = $this->connection->prepare( |
| 155 | "SELECT |
| 156 | {$userFields}, |
| 157 | do.id, |
| 158 | do.name, |
| 159 | do.userId, |
| 160 | do.startDate, |
| 161 | do.endDate |
| 162 | FROM {$this->table} do |
| 163 | {$userJoin} |
| 164 | {$where}" |
| 165 | ); |
| 166 | |
| 167 | $statement->execute($params); |
| 168 | |
| 169 | $rows = $statement->fetchAll(); |
| 170 | } catch (\Exception $e) { |
| 171 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 172 | } |
| 173 | |
| 174 | return call_user_func([BlockTimeFactory::class, 'createCollection'], $rows); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @param $id |
| 179 | * @return mixed |
| 180 | * @throws QueryExecutionException |
| 181 | */ |
| 182 | public function getBlockTimeById($id) |
| 183 | { |
| 184 | $params[':id'] = $id; |
| 185 | |
| 186 | try { |
| 187 | $statement = $this->connection->prepare( |
| 188 | "SELECT |
| 189 | do.id, |
| 190 | do.name, |
| 191 | do.userId, |
| 192 | do.startDate, |
| 193 | do.endDate |
| 194 | FROM {$this->table} do |
| 195 | WHERE do.id = :id" |
| 196 | ); |
| 197 | |
| 198 | $statement->execute($params); |
| 199 | |
| 200 | $rows = $statement->fetchAll(); |
| 201 | } catch (\Exception $e) { |
| 202 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 203 | } |
| 204 | |
| 205 | return call_user_func([BlockTimeFactory::class, 'createCollection'], $rows)->getItem($id); |
| 206 | } |
| 207 | } |
| 208 |