| 18 | | class Process extends \BO\Zmsbackend\Base implements \BO\Zmsbackend\Interfaces\ResolveReferences |
| 19 | | { |
| 20 | | |
| 21 | | |
| 22 | | |
| 23 | | |
| 24 | | public function readEntity(int|string|null $processId = null, string|\BO\Zmsbackend\Helper\NoAuth|null $authKey = null, int $resolveReferences = 2): ?Entity |
| 25 | | { |
| 26 | | if (null === $processId || null === $authKey) { |
| 27 | | return null; |
| 28 | | } |
| 29 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 30 | | $query->addEntityMapping() |
| 31 | | ->addResolvedReferences($resolveReferences) |
| 32 | | ->addConditionProcessId($processId); |
| 33 | | if (!$authKey instanceof \BO\Zmsbackend\Helper\NoAuth) { |
| 34 | | $query->addConditionAuthKey($authKey); |
| 35 | | } |
| 36 | | $process = $this->fetchOne($query, new Entity()); |
| 37 | | $process = $this->readResolvedReferences($process, $resolveReferences); |
| 38 | | |
| 39 | | return $process; |
| 40 | | } |
| 41 | | |
| 42 | | public function readById($processId, int $resolveReferences = 1): Entity |
| 43 | | { |
| 44 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 45 | | $query->addEntityMapping() |
| 46 | | ->addResolvedReferences($resolveReferences) |
| 47 | | ->addConditionProcessId($processId); |
| 48 | | return $this->fetchOne($query, new Entity()); |
| 49 | | } |
| 50 | | |
| 51 | | |
| 52 | | |
| 53 | | |
| 54 | | |
| 55 | | #[\Override] |
| 56 | | public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $entity, int $resolveReferences) |
| 57 | | { |
| 58 | | if (0 <= $resolveReferences) { |
| 59 | | if ($entity->archiveId) { |
| 60 | | $entity->requests = (new \BO\Zmsbackend\Request\Service\Request()) |
| 61 | | ->readRequestByArchiveId($entity->archiveId, $resolveReferences - 1); |
| 62 | | } else { |
| 63 | | $entity->requests = (new \BO\Zmsbackend\Request\Service\Request()) |
| 64 | | ->readRequestByProcessId($entity->id, $resolveReferences - 1); |
| 65 | | } |
| 66 | | } |
| 67 | | return $entity; |
| 68 | | } |
| 69 | | |
| 70 | | |
| 71 | | |
| 72 | | |
| 73 | | |
| 74 | | |
| 75 | | |
| 76 | | |
| 77 | | public function updateEntity(\BO\Zmsentities\Process $process, \DateTimeInterface $now, int $resolveReferences = 0, string|null $previousStatus = null, ?\BO\Zmsentities\Useraccount $useraccount = null): Entity |
| 78 | | { |
| 79 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::UPDATE); |
| 80 | | $query->addConditionProcessId($process->getId()); |
| 81 | | $query->addConditionAuthKey($process->getAuthKey()); |
| 82 | | $query->addValuesUpdateProcess($process, $now, 0, $previousStatus); |
| 83 | | if ($this->perform($query->getLockProcessId(), ['processId' => $process->getId()])) { |
| 84 | | $this->writeItem($query); |
| 85 | | $this->writeRequestsToDb($process); |
| 86 | | } |
| 87 | | $process = $this->readEntity($process->getId(), $process->authKey, $resolveReferences); |
| 88 | | if (!$process->getId()) { |
| 89 | | throw new \BO\Zmsbackend\Process\Exception\ProcessUpdateFailed(); |
| 90 | | } |
| 91 | | $this->perform(\BO\Zmsbackend\Process\Repository\Process::QUERY_UPDATE_FOLLOWING_PROCESS, [ |
| 92 | | 'reserved' => ($process->status == 'reserved') ? 1 : 0, |
| 93 | | 'processID' => $process->getId(), |
| 94 | | ]); |
| 95 | | \BO\Zmsbackend\Log\Service\Log::writeProcessLog("UPDATE (\BO\Zmsbackend\Process\Service\Process::updateEntity) $process ", \BO\Zmsbackend\Log\Service\Log::ACTION_EDITED, $process, $useraccount); |
| 96 | | return $process; |
| 97 | | } |
| 98 | | |
| 99 | | public function updateEntityDisplayNumber(Entity $process): Entity |
| 100 | | { |
| 101 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::UPDATE); |
| 102 | | $query->addConditionProcessId($process->getId()); |
| 103 | | $query->addConditionAuthKey($process->getAuthKey()); |
| 104 | | |
| 105 | | $process->displayNumber = $query->getNewDisplayNumber($process); |
| 106 | | $query->addValueDisplayNumber($process); |
| 107 | | if ($this->perform($query->getLockProcessId(), ['processId' => $process->getId()])) { |
| 108 | | $this->writeItem($query); |
| 109 | | } |
| 110 | | |
| 111 | | \BO\Zmsbackend\Log\Service\Log::writeProcessLog("UPDATE (updateEntityDisplayNumber) $process ", \BO\Zmsbackend\Log\Service\Log::ACTION_EDITED, $process); |
| 112 | | return $process; |
| 113 | | } |
| 114 | | |
| 115 | | |
| 116 | | |
| 117 | | |
| 118 | | |
| 119 | | |
| 120 | | |
| 121 | | |
| 122 | | |
| 123 | | |
| 124 | | |
| 125 | | public function updateEntityWithSlots(\BO\Zmsentities\Process $process, \DateTimeInterface $now, $slotType = "intern", $slotsRequired = 0, int $resolveReferences = 0, $userAccount = null) |
| 126 | | { |
| 127 | | if ('intern' != $slotType) { |
| 128 | | return $this->updateEntity($process, $now, $resolveReferences, null, $userAccount); |
| 129 | | } |
| 130 | | $process = clone $process; |
| 131 | | $appointment = $process->getAppointments()->getFirst(); |
| 132 | | $slotList = (new \BO\Zmsbackend\Slot\Service\Slot())->readByAppointment($appointment, $slotsRequired, true); |
| 133 | | $processEntityList = $this->readEntityList($process->getId()); |
| 134 | | foreach ($processEntityList as $entity) { |
| 135 | | if ($process->getId() != $entity->getId()) { |
| 136 | | $this->writeDeletedEntity($entity->getId()); |
| 137 | | } |
| 138 | | } |
| 139 | | |
| 140 | | foreach ($slotList as $slot) { |
| 141 | | $newProcess = clone $process; |
| 142 | | $newProcess->getFirstAppointment()->setTime($slot->time); |
| 143 | | if (! $newProcess->getFirstAppointment()->isMatching($process->getFirstAppointment())) { |
| 144 | | $this->writeNewProcess($newProcess, $now, $process->id, 0, true, $userAccount); |
| 145 | | } |
| 146 | | } |
| 147 | | |
| 148 | | $appointment->addSlotCount($slotList->count()); |
| 149 | | |
| 150 | | $processEntity = $this->updateEntity($process, $now, $resolveReferences, null, $userAccount); |
| 151 | | $process = $this->readEntity($process->getId(), $process->authKey, $resolveReferences); |
| 152 | | |
| 153 | | \BO\Zmsbackend\Log\Service\Log::writeProcessLog("CREATE (\BO\Zmsbackend\Process\Service\Process::updateEntityWithSlots) $process ", \BO\Zmsbackend\Log\Service\Log::ACTION_EDITED, $process, $userAccount); |
| 154 | | return $processEntity; |
| 155 | | } |
| 156 | | |
| 157 | | public function redirectToScope(Entity $process, \BO\Zmsentities\Scope $scope, int $waitingNumber, ?\BO\Zmsentities\Useraccount $useraccount = null): Entity |
| 158 | | { |
| 159 | | $datetime = \App::$now; |
| 160 | | $process->setStatus('confirmed'); |
| 161 | | $appointment = $process->getFirstAppointment(); |
| 162 | | $date = (new \DateTimeImmutable())->setTimestamp($appointment->date); |
| 163 | | $date = $date->setTime(0, 0, 0); |
| 164 | | $appointment->date = $date->getTimestamp(); |
| 165 | | $process->appointments = new AppointmentList([$appointment]); |
| 166 | | $newQueueNumber = (new \BO\Zmsbackend\Scope\Service\Scope())->readWaitingNumberUpdated($scope->id, $datetime); |
| 167 | | $process->addQueue($newQueueNumber, $datetime); |
| 168 | | $process->queue['number'] = $waitingNumber; |
| 169 | | $process->priority = \BO\Zmsentities\Collection\QueueList::DEFAULT_PRIORITY_WITH_APPOINTMENT; |
| 170 | | $process = $this->writeNewProcess($process, $datetime); |
| 171 | | \BO\Zmsbackend\Log\Service\Log::writeProcessLog("CREATE (\BO\Zmsbackend\Process\Service\Process::redirectToScope) $process ", \BO\Zmsbackend\Log\Service\Log::ACTION_REDIRECTED, $process, $useraccount); |
| 172 | | $this->writeRequestsToDb($process); |
| 173 | | return $process; |
| 174 | | } |
| 175 | | |
| 176 | | public function readSlotCount(\BO\Zmsentities\Process $process): Entity |
| 177 | | { |
| 178 | | $scope = new \BO\Zmsentities\Scope($process->scope); |
| 179 | | $requestRelationList = (new \BO\Zmsbackend\RequestRelation\Service\RequestRelation()) |
| 180 | | ->readListByProviderId($scope->getProviderId(), $scope->getSource()); |
| 181 | | $appointment = $process->getAppointments()->getFirst(); |
| 182 | | $appointment->slotCount = 0; |
| 183 | | foreach ($process->requests as $request) { |
| 184 | | foreach ($requestRelationList as $requestRelation) { |
| 185 | | if ($requestRelation->getRequest()->getId() == $request->getId()) { |
| 186 | | $appointment->slotCount += $requestRelation->getSlotCount(); |
| 187 | | } |
| 188 | | } |
| 189 | | } |
| 190 | | $appointment->slotCount = round($appointment->slotCount, 0); |
| 191 | | return $process; |
| 192 | | } |
| 193 | | |
| 194 | | |
| 195 | | |
| 196 | | |
| 197 | | public function writeEntityWithNewAppointment(\BO\Zmsentities\Process $process, \BO\Zmsentities\Appointment $appointment, \DateTimeInterface $now, $slotType = 'public', int $slotsRequired = 0, int $resolveReferences = 0, bool $keepReserved = false): Entity|null |
| 198 | | { |
| 199 | | |
| 200 | | $processNew = clone $process; |
| 201 | | $processNew->id = 0; |
| 202 | | $processNew->scope = $appointment->scope; |
| 203 | | $processNew->queue['arrivalTime'] = 0; |
| 204 | | $processNew->queue['number'] = 0; |
| 205 | | $processNew->appointments = (new \BO\Zmsentities\Collection\AppointmentList())->addEntity($appointment); |
| 206 | | |
| 207 | | $this->writeDeletedEntity($process->getId()); |
| 208 | | |
| 209 | | $processNew = \BO\Zmsbackend\Process\Service\ProcessStatusFree::init() |
| 210 | | ->writeEntityReserved($processNew, $now, $slotType, $slotsRequired); |
| 211 | | $processTempNewId = $processNew->getId(); |
| 212 | | |
| 213 | | $processNew->withReassignedCredentials($process); |
| 214 | | |
| 215 | | $this->updateFollowingProcesses($processTempNewId, $processNew); |
| 216 | | $this->updateReassignedRequests($processTempNewId, $processNew->getId()); |
| 217 | | |
| 218 | | (new \BO\Zmsbackend\Slot\Service\Slot())->deleteSlotProcessMappingFor($processTempNewId); |
| 219 | | |
| 220 | | (new \BO\Zmsbackend\Slot\Service\Slot())->writeSlotProcessMappingFor($processNew->getId()); |
| 221 | | \BO\Zmsbackend\Log\Service\Log::writeProcessLog("UPDATE (\BO\Zmsbackend\Process\Service\Process::writeEntityWithNewAppointment) $process ", \BO\Zmsbackend\Log\Service\Log::ACTION_EDITED, $process); |
| 222 | | $status = ($keepReserved) ? Entity::STATUS_RESERVED : ENTITY::STATUS_CONFIRMED; |
| 223 | | return $this->updateProcessStatus($processNew, $status, $now, $resolveReferences); |
| 224 | | } |
| 225 | | |
| 226 | | |
| 227 | | |
| 228 | | |
| 229 | | public function updateFollowingProcesses($processId, \BO\Zmsentities\Process $processData): void |
| 230 | | { |
| 231 | | $this->perform(\BO\Zmsbackend\Process\Repository\Process::QUERY_REASSIGN_PROCESS_CREDENTIALS, [ |
| 232 | | 'newProcessId' => $processData->getId(), |
| 233 | | 'newAuthKey' => $processData->getAuthKey(), |
| 234 | | 'processId' => $processId |
| 235 | | ]); |
| 236 | | $processEntityList = $this->readEntityList($processId); |
| 237 | | if ($processEntityList->count()) { |
| 238 | | foreach ($processEntityList as $entity) { |
| 239 | | if ($entity->getId() != $processId) { |
| 240 | | $this->perform(\BO\Zmsbackend\Process\Repository\Process::QUERY_REASSIGN_FOLLWING_PROCESS, [ |
| 241 | | 'newProcessId' => $processData->getId(), |
| 242 | | 'processId' => $processId |
| 243 | | ]); |
| 244 | | } |
| 245 | | } |
| 246 | | } |
| 247 | | } |
| 248 | | |
| 249 | | |
| 250 | | |
| 251 | | |
| 252 | | public function updateReassignedRequests($processId, $newProcessId): void |
| 253 | | { |
| 254 | | $this->perform(\BO\Zmsbackend\Process\Repository\Process::QUERY_REASSIGN_PROCESS_REQUESTS, [ |
| 255 | | 'newProcessId' => $newProcessId, |
| 256 | | 'processId' => $processId |
| 257 | | ]); |
| 258 | | } |
| 259 | | |
| 260 | | |
| 261 | | |
| 262 | | |
| 263 | | |
| 264 | | protected function writeNewProcess(\BO\Zmsentities\Process $process, \DateTimeInterface $dateTime, int $parentProcess = 0, int $childProcessCount = 0, bool $retry = true, \BO\Zmsentities\Useraccount|null $userAccount = null) |
| 265 | | { |
| 266 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::INSERT); |
| 267 | | $process->id = $this->readNewProcessId(); |
| 268 | | $process->setRandomAuthKey(); |
| 269 | | $process->createTimestamp = $dateTime->getTimestamp(); |
| 270 | | if ($parentProcess !== 0) { |
| 271 | | $process->displayNumber = ''; |
| 272 | | } |
| 273 | | |
| 274 | | $query->addValuesNewProcess($process, $parentProcess, $childProcessCount); |
| 275 | | |
| 276 | | if ($parentProcess === 0 && !empty($process->getDisplayNumber())) { |
| 277 | | $query->addValueDisplayNumber($process); |
| 278 | | } |
| 279 | | |
| 280 | | $query->addValuesScopeData($process); |
| 281 | | $query->addValuesAppointmentData($process); |
| 282 | | $query->addValuesUpdateProcess($process, $dateTime, $parentProcess); |
| 283 | | try { |
| 284 | | $this->writeItem($query); |
| 285 | | } catch (\BO\Zmsbackend\Exception\Pdo\PDOFailed $exception) { |
| 286 | | if ($retry) { |
| 287 | | |
| 288 | | sleep(1); |
| 289 | | |
| 290 | | return $this->writeNewProcess($process, $dateTime, $parentProcess, $childProcessCount, false, $userAccount); |
| 291 | | } |
| 292 | | throw new \BO\Zmsbackend\Process\Exception\ProcessCreateFailed($exception->getMessage()); |
| 293 | | } |
| 294 | | (new \BO\Zmsbackend\Slot\Service\Slot())->writeSlotProcessMappingFor($process->id); |
| 295 | | $checksum = ($userAccount) ? sha1($process->id . '-' . $userAccount->getId()) : ''; |
| 296 | | |
| 297 | | \BO\Zmsbackend\Log\Service\Log::writeProcessLog("CREATE (\BO\Zmsbackend\Process\Service\Process::writeNewProcess) $process $checksum ", \BO\Zmsbackend\Log\Service\Log::ACTION_NEW, $process, $userAccount); |
| 298 | | return $process; |
| 299 | | } |
| 300 | | |
| 301 | | |
| 302 | | |
| 303 | | |
| 304 | | |
| 305 | | protected function readNewProcessId() |
| 306 | | { |
| 307 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 308 | | $newProcessId = $this->fetchValue($query->getQueryNewProcessId()); |
| 309 | | return $newProcessId; |
| 310 | | } |
| 311 | | |
| 312 | | |
| 313 | | |
| 314 | | |
| 315 | | |
| 316 | | |
| 317 | | public function readAuthKeyByProcessId($processId) |
| 318 | | { |
| 319 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 320 | | $query |
| 321 | | ->addEntityMapping() |
| 322 | | ->addResolvedReferences(0) |
| 323 | | ->addConditionProcessId($processId); |
| 324 | | $process = $this->fetchOne($query, new Entity()); |
| 325 | | return ($process->hasId()) ? array( |
| 326 | | 'authKey' => $process->authKey |
| 327 | | ) : null; |
| 328 | | } |
| 329 | | |
| 330 | | protected function readList($statement, int $resolveReferences): Collection |
| 331 | | { |
| 332 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 333 | | $processList = new Collection(); |
| 334 | | while ($processData = $statement->fetch(\PDO::FETCH_ASSOC)) { |
| 335 | | $entity = new Entity($query->postProcessJoins($processData)); |
| 336 | | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
| 337 | | $processList->addEntity($entity); |
| 338 | | } |
| 339 | | return $processList; |
| 340 | | } |
| 341 | | |
| 342 | | protected function readProcessesWithoutRequests($statement): Collection |
| 343 | | { |
| 344 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 345 | | $processList = new Collection(); |
| 346 | | while ($processData = $statement->fetch(\PDO::FETCH_ASSOC)) { |
| 347 | | $processList->addEntity(new Entity($query->postProcessJoins($processData))); |
| 348 | | } |
| 349 | | return $processList; |
| 350 | | } |
| 351 | | |
| 352 | | protected function attachAllRequestsInOneQuery(Collection $processList, int $resolveReferences): Collection |
| 353 | | { |
| 354 | | if ($processList->count() === 0 || $resolveReferences < 0) { |
| 355 | | return $processList; |
| 356 | | } |
| 357 | | |
| 358 | | $processIds = []; |
| 359 | | foreach ($processList as $process) { |
| 360 | | if ($process->hasId()) { |
| 361 | | $processIds[] = $process->getId(); |
| 362 | | } |
| 363 | | } |
| 364 | | |
| 365 | | $requestsByProcessId = (new \BO\Zmsbackend\Request\Service\Request()) |
| 366 | | ->readAllRequestsForProcessIds($processIds, $resolveReferences - 1); |
| 367 | | |
| 368 | | foreach ($processList as $process) { |
| 369 | | if (!$process->hasId()) { |
| 370 | | continue; |
| 371 | | } |
| 372 | | $process->requests = $requestsByProcessId[(int) $process->getId()] |
| 373 | | ?? new \BO\Zmsentities\Collection\RequestList(); |
| 374 | | } |
| 375 | | |
| 376 | | return $processList; |
| 377 | | } |
| 378 | | |
| 379 | | |
| 380 | | |
| 381 | | |
| 382 | | public function readEntityList($processId, int $resolveReferences = 0) |
| 383 | | { |
| 384 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 385 | | $query |
| 386 | | ->addResolvedReferences($resolveReferences) |
| 387 | | ->addEntityMapping() |
| 388 | | ->addConditionProcessIdFollow($processId); |
| 389 | | $statement = $this->fetchStatement($query); |
| 390 | | return $this->readList($statement, $resolveReferences)->sortByAppointmentDate(); |
| 391 | | } |
| 392 | | |
| 393 | | public function readByWorkstation(\BO\Zmsentities\Workstation $workstation, int $resolveReferences = 0) |
| 394 | | { |
| 395 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 396 | | $query |
| 397 | | ->addResolvedReferences($resolveReferences) |
| 398 | | ->addEntityMapping() |
| 399 | | ->addConditionWorkstationId($workstation->id); |
| 400 | | $process = $this->fetchOne($query, new Entity()); |
| 401 | | return ($process->hasId()) ? $this->readResolvedReferences($process, $resolveReferences) : null; |
| 402 | | } |
| 403 | | |
| 404 | | |
| 405 | | |
| 406 | | |
| 407 | | |
| 408 | | |
| 409 | | |
| 410 | | |
| 411 | | |
| 412 | | |
| 413 | | public function readProcessListByScopesAndTime( |
| 414 | | $scopeIds, |
| 415 | | \DateTimeInterface $dateTime, |
| 416 | | int $resolveReferences = 0, |
| 417 | | $withEntities = [] |
| 418 | | ) { |
| 419 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT, '', false, null, $withEntities); |
| 420 | | $query |
| 421 | | ->addResolvedReferences($resolveReferences) |
| 422 | | ->addEntityMapping() |
| 423 | | ->addConditionScopeIds($scopeIds) |
| 424 | | ->addConditionAssigned() |
| 425 | | ->addConditionIgnoreSlots() |
| 426 | | ->addConditionTime($dateTime) |
| 427 | | ->removeDuplicates(); |
| 428 | | $statement = $this->fetchStatement($query); |
| 429 | | return $this->readList($statement, $resolveReferences); |
| 430 | | } |
| 431 | | |
| 432 | | |
| 433 | | |
| 434 | | |
| 435 | | |
| 436 | | |
| 437 | | |
| 438 | | |
| 439 | | |
| 440 | | |
| 441 | | |
| 442 | | |
| 443 | | public function readConflictListByScopeAndTime(\BO\Zmsentities\Scope $scope, ?\DateTimeInterface $startDate = null, ?\DateTimeInterface $endDate = null, ?\DateTimeInterface $now = null, int $resolveReferences = 1) |
| 444 | | { |
| 445 | | $availabilityList = (new \BO\Zmsbackend\Availability\Service\Availability()) |
| 446 | | ->readAvailabilityListByScope($scope, 0, $startDate, $endDate) |
| 447 | | ->withScope($scope); |
| 448 | | if (! $endDate) { |
| 449 | | $availabilityList = $availabilityList->withDateTime($startDate); |
| 450 | | $endDate = $startDate; |
| 451 | | } |
| 452 | | $currentDate = ($startDate) ? $startDate : $now; |
| 453 | | $conflictList = $availabilityList->getConflicts($startDate, $endDate); |
| 454 | | while ($currentDate <= $endDate) { |
| 455 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 456 | | $query |
| 457 | | ->addResolvedReferences($resolveReferences) |
| 458 | | ->addEntityMapping() |
| 459 | | ->addConditionScopeId($scope->getId()) |
| 460 | | ->addConditionAssigned() |
| 461 | | ->addConditionIgnoreSlots(); |
| 462 | | $query->addConditionTime($currentDate); |
| 463 | | $statement = $this->fetchStatement($query); |
| 464 | | $processList = $this->readList($statement, $resolveReferences); |
| 465 | | $processList = $processList->toQueueList($currentDate)->withoutStatus(['queued'])->toProcessList(); |
| 466 | | $conflictList->addList($processList->withOutAvailability($availabilityList)); |
| 467 | | $currentDate = $currentDate->modify('+1 day'); |
| 468 | | } |
| 469 | | $conflictList = $conflictList->withoutExpiredAppointmentDate($now); |
| 470 | | return $conflictList; |
| 471 | | } |
| 472 | | |
| 473 | | |
| 474 | | |
| 475 | | |
| 476 | | |
| 477 | | |
| 478 | | |
| 479 | | |
| 480 | | public function readProcessListByScopeAndStatus(int $scopeId, string $status, int $resolveReferences = 0, int $limit = 1000, ?int $offset = null) |
| 481 | | { |
| 482 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 483 | | $query |
| 484 | | ->addResolvedReferences($resolveReferences) |
| 485 | | ->addEntityMapping() |
| 486 | | ->addConditionScopeId($scopeId) |
| 487 | | ->addConditionStatus($status) |
| 488 | | ->addLimit($limit, $offset); |
| 489 | | $statement = $this->fetchStatement($query); |
| 490 | | return $this->readList($statement, $resolveReferences); |
| 491 | | } |
| 492 | | |
| 493 | | |
| 494 | | |
| 495 | | |
| 496 | | |
| 497 | | |
| 498 | | |
| 499 | | |
| 500 | | |
| 501 | | public function readProcessListByClusterAndTime($clusterId, \DateTimeInterface $dateTime) |
| 502 | | { |
| 503 | | $cluster = (new \BO\Zmsbackend\Cluster\Service\Cluster())->readEntity($clusterId, 1); |
| 504 | | $scopeIds = []; |
| 505 | | if ($cluster->scopes->count()) { |
| 506 | | foreach ($cluster->scopes as $scope) { |
| 507 | | $scopeIds[] = $scope->id; |
| 508 | | } |
| 509 | | } |
| 510 | | |
| 511 | | return $this->readProcessListByScopesAndTime($scopeIds, $dateTime); |
| 512 | | } |
| 513 | | |
| 514 | | |
| 515 | | |
| 516 | | |
| 517 | | |
| 518 | | |
| 519 | | |
| 520 | | |
| 521 | | public function readProcessListCountByScope($scopeId) |
| 522 | | { |
| 523 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 524 | | $query |
| 525 | | ->addCountValue() |
| 526 | | ->addConditionAssigned() |
| 527 | | ->addConditionIgnoreSlots() |
| 528 | | ->addConditionScopeId($scopeId); |
| 529 | | $statement = $this->fetchStatement($query); |
| 530 | | $result = $statement->fetch(\PDO::FETCH_ASSOC); |
| 531 | | return $result['processCount']; |
| 532 | | } |
| 533 | | |
| 534 | | |
| 535 | | |
| 536 | | |
| 537 | | |
| 538 | | |
| 539 | | public function readProcessListByMailAddress(string $mailAddress, ?int $scopeId = null, int $resolveReferences = 0, int $limit = 2000): Collection |
| 540 | | { |
| 541 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 542 | | $query |
| 543 | | ->addResolvedReferences($resolveReferences) |
| 544 | | ->addEntityMapping() |
| 545 | | ->addConditionMail($mailAddress, true) |
| 546 | | ->addConditionIgnoreSlots() |
| 547 | | ->addLimit($limit); |
| 548 | | if ($scopeId) { |
| 549 | | $query->addConditionScopeId($scopeId); |
| 550 | | } |
| 551 | | |
| 552 | | $statement = $this->fetchStatement($query); |
| 553 | | return $this->readList($statement, $resolveReferences); |
| 554 | | } |
| 555 | | |
| 556 | | |
| 557 | | |
| 558 | | |
| 559 | | |
| 560 | | |
| 561 | | |
| 562 | | public function readListByMailAndStatusList(string $mailAddress, array $statusList, int $resolveReferences = 1, int $limit = 300): Collection |
| 563 | | { |
| 564 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 565 | | $query |
| 566 | | ->addResolvedReferences($resolveReferences) |
| 567 | | ->addEntityMapping() |
| 568 | | ->addConditionMail($mailAddress, true) |
| 569 | | ->addConditionIgnoreSlots() |
| 570 | | ->addLimit($limit); |
| 571 | | $statement = $this->fetchStatement($query); |
| 572 | | $collection = $this->readList($statement, $resolveReferences); |
| 573 | | return $collection->toProcessListByStatusList($statusList); |
| 574 | | } |
| 575 | | |
| 576 | | |
| 577 | | |
| 578 | | |
| 579 | | |
| 580 | | |
| 581 | | |
| 582 | | |
| 583 | | |
| 584 | | |
| 585 | | public function updateProcessStatus(Entity $process, string $status, \DateTimeInterface $dateTime, int $resolveReferences = 0, $userAccount = null) |
| 586 | | { |
| 587 | | $process = (new ProcessStatus()) |
| 588 | | ->writeUpdatedStatus($process, $status, $dateTime, $resolveReferences, $userAccount); |
| 589 | | |
| 590 | | |
| 591 | | if ($this->shouldUpdateDisplayNumber($process, $status)) { |
| 592 | | $this->updateEntityDisplayNumber($process); |
| 593 | | } |
| 594 | | |
| 595 | | return $process; |
| 596 | | } |
| 597 | | |
| 598 | | public function shouldUpdateDisplayNumber(Entity $process, $status): bool |
| 599 | | { |
| 600 | | if ($status !== 'preconfirmed' && $status !== 'confirmed') { |
| 601 | | return false; |
| 602 | | } |
| 603 | | $displayNumberPrefix = $process->scope->getPreference('queue', 'displayNumberPrefix'); |
| 604 | | if (empty($displayNumberPrefix)) { |
| 605 | | return false; |
| 606 | | } |
| 607 | | |
| 608 | | if (str_starts_with($process->getDisplayNumber(), $displayNumberPrefix)) { |
| 609 | | return false; |
| 610 | | } |
| 611 | | |
| 612 | | return true; |
| 613 | | } |
| 614 | | |
| 615 | | |
| 616 | | |
| 617 | | |
| 618 | | |
| 619 | | |
| 620 | | |
| 621 | | |
| 622 | | public function writeDeletedEntity($processId): bool |
| 623 | | { |
| 624 | | $processEntityList = $this->readEntityList($processId); |
| 625 | | $status = false; |
| 626 | | if ($processEntityList->count()) { |
| 627 | | foreach ($processEntityList as $entity) { |
| 628 | | $entityId = $entity->getId(); |
| 629 | | $query = \BO\Zmsbackend\Process\Repository\Process::QUERY_DELETE; |
| 630 | | $status = $this->perform($query, array( |
| 631 | | $entityId, |
| 632 | | $entityId |
| 633 | | )); |
| 634 | | if ($status) { |
| 635 | | $this->deleteRequestsForProcessId($entityId); |
| 636 | | (new \BO\Zmsbackend\Slot\Service\Slot())->deleteSlotProcessMappingFor($entityId); |
| 637 | | \BO\Zmsbackend\Log\Service\Log::writeProcessLog("DELETE (\BO\Zmsbackend\Process\Service\Process::writeDeletedEntity) $entityId ", \BO\Zmsbackend\Log\Service\Log::ACTION_DELETED, $entity); |
| 638 | | } |
| 639 | | } |
| 640 | | } |
| 641 | | |
| 642 | | return $status; |
| 643 | | } |
| 644 | | |
| 645 | | |
| 646 | | |
| 647 | | |
| 648 | | |
| 649 | | |
| 650 | | |
| 651 | | |
| 652 | | |
| 653 | | |
| 654 | | public function deleteEntity($processId, $authKey) |
| 655 | | { |
| 656 | | return $this->writeCanceledEntity($processId, $authKey); |
| 657 | | } |
| 658 | | |
| 659 | | |
| 660 | | |
| 661 | | |
| 662 | | |
| 663 | | |
| 664 | | |
| 665 | | |
| 666 | | |
| 667 | | public function writeCanceledEntity($processId, $authKey, $now = null, ?\BO\Zmsentities\Useraccount $useraccount = null) |
| 668 | | { |
| 669 | | $canceledTimestamp = ($now) ? $now->getTimestamp() : (new \DateTimeImmutable())->getTimestamp(); |
| 670 | | $newAuthKey = bin2hex(random_bytes(32)); |
| 671 | | $query = \BO\Zmsbackend\Process\Repository\Process::QUERY_CANCELED; |
| 672 | | $this->perform($query, [ |
| 673 | | 'processId' => $processId, |
| 674 | | 'authKey' => $authKey, |
| 675 | | 'canceledTimestamp' => $canceledTimestamp, |
| 676 | | 'newAuthKey' => $newAuthKey |
| 677 | | ]); |
| 678 | | $process = $this->readEntity($processId, new \BO\Zmsbackend\Helper\NoAuth(), 0); |
| 679 | | \BO\Zmsbackend\Log\Service\Log::writeProcessLog("DELETE (\BO\Zmsbackend\Process\Service\Process::writeCanceledEntity) $processId ", \BO\Zmsbackend\Log\Service\Log::ACTION_CANCELED, $process, $useraccount); |
| 680 | | return $process; |
| 681 | | } |
| 682 | | |
| 683 | | |
| 684 | | |
| 685 | | |
| 686 | | |
| 687 | | |
| 688 | | |
| 689 | | |
| 690 | | |
| 691 | | |
| 692 | | public function writeBlockedEntity(\BO\Zmsentities\Process $process, bool $releaseSlots = false, ?\BO\Zmsentities\Useraccount $useraccount = null) |
| 693 | | { |
| 694 | | $amendment = $process->toDerefencedAmendment(); |
| 695 | | $customTextfield = $process->toDerefencedcustomTextfield(); |
| 696 | | $customTextfield2 = $process->toDerefencedcustomTextfield2(); |
| 697 | | if (!isset($process->queue['status'])) { |
| 698 | | $process->queue['status'] = $process->status; |
| 699 | | } |
| 700 | | $process->status = 'blocked'; |
| 701 | | $query = \BO\Zmsbackend\Process\Repository\Process::QUERY_DEREFERENCED; |
| 702 | | $status = $this->perform($query, array( |
| 703 | | $amendment, |
| 704 | | $customTextfield, |
| 705 | | $customTextfield2, |
| 706 | | $process->id, |
| 707 | | $process->authKey, |
| 708 | | $process->id |
| 709 | | )); |
| 710 | | if ($status) { |
| 711 | | $processEntityList = $this->readEntityList($process->id); |
| 712 | | if ($processEntityList->count()) { |
| 713 | | foreach ($processEntityList as $entity) { |
| 714 | | $entityId = $entity->getId(); |
| 715 | | if ($releaseSlots) { |
| 716 | | (new \BO\Zmsbackend\Slot\Service\Slot())->deleteSlotProcessMappingFor($entityId); |
| 717 | | } |
| 718 | | \BO\Zmsbackend\Log\Service\Log::writeProcessLog("DELETE (\BO\Zmsbackend\Process\Service\Process::writeBlockedEntity) $entityId ", \BO\Zmsbackend\Log\Service\Log::ACTION_DELETED, $process, $useraccount); |
| 719 | | } |
| 720 | | } |
| 721 | | } |
| 722 | | return $status; |
| 723 | | } |
| 724 | | |
| 725 | | protected function writeRequestsToDb(\BO\Zmsentities\Process $process): void |
| 726 | | { |
| 727 | | |
| 728 | | $hasRequests = ($process->requests && count($process->requests)); |
| 729 | | if ($hasRequests || 'queued' == $process->status) { |
| 730 | | $this->deleteRequestsForProcessId($process->id); |
| 731 | | } |
| 732 | | if ($hasRequests) { |
| 733 | | $query = new \BO\Zmsbackend\Request\Repository\XRequest(\BO\Zmsbackend\Query\Base::INSERT); |
| 734 | | foreach ($process->requests as $request) { |
| 735 | | if ($request->id >= 0) { |
| 736 | | |
| 737 | | $query->addValues([ |
| 738 | | 'AnliegenID' => $request['id'], |
| 739 | | 'source' => $request['source'], |
| 740 | | 'BuergerID' => $process->id |
| 741 | | ]); |
| 742 | | $this->writeItem($query); |
| 743 | | } |
| 744 | | } |
| 745 | | } |
| 746 | | } |
| 747 | | |
| 748 | | protected function deleteRequestsForProcessId($processId): bool |
| 749 | | { |
| 750 | | $status = false; |
| 751 | | if (0 < $processId) { |
| 752 | | $query = new \BO\Zmsbackend\Request\Repository\XRequest(\BO\Zmsbackend\Query\Base::DELETE); |
| 753 | | $query->addConditionProcessId($processId); |
| 754 | | $status = $this->deleteItem($query); |
| 755 | | } |
| 756 | | return $status; |
| 757 | | } |
| 758 | | |
| 759 | | public function readExpiredProcessList(\DateTimeInterface $expirationDate, int $limit = 500, int $resolveReferences = 0, ?int $offset = null) |
| 760 | | { |
| 761 | | $selectQuery = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 762 | | $selectQuery |
| 763 | | ->addEntityMapping() |
| 764 | | ->addResolvedReferences($resolveReferences) |
| 765 | | ->addConditionProcessDeleteInterval($expirationDate) |
| 766 | | ->addConditionIgnoreSlots() |
| 767 | | ->addLimit($limit, $offset); |
| 768 | | $statement = $this->fetchStatement($selectQuery); |
| 769 | | return $this->readList($statement, $resolveReferences); |
| 770 | | } |
| 771 | | public function readUnconfirmedProcessList(\DateTimeInterface $expirationDate, $scopeId = 0, int $limit = 500, ?int $offset = null, int $resolveReferences = 0) |
| 772 | | { |
| 773 | | |
| 774 | | $selectQuery = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 775 | | $selectQuery |
| 776 | | ->addEntityMapping() |
| 777 | | ->addResolvedReferences($resolveReferences) |
| 778 | | ->addConditionScopeId($scopeId) |
| 779 | | ->addConditionProcessExpiredIPTimeStamp($expirationDate) |
| 780 | | ->addConditionStatus('preconfirmed') |
| 781 | | ->addConditionIgnoreSlots() |
| 782 | | ->addLimit($limit, $offset); |
| 783 | | $statement = $this->fetchStatement($selectQuery); |
| 784 | | return $this->readList($statement, $resolveReferences); |
| 785 | | } |
| 786 | | |
| 787 | | public function readExpiredProcessListByStatus(\DateTimeInterface $expirationDate, $status, int $limit = 500, ?int $offset = null, int $resolveReferences = 0) |
| 788 | | { |
| 789 | | $selectQuery = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 790 | | $selectQuery |
| 791 | | ->addEntityMapping() |
| 792 | | ->addResolvedReferences($resolveReferences) |
| 793 | | ->addConditionProcessDeleteBeforeDate($expirationDate) |
| 794 | | ->addConditionStatus($status) |
| 795 | | ->addConditionIgnoreSlots() |
| 796 | | ->addLimit($limit, $offset); |
| 797 | | $statement = $this->fetchStatement($selectQuery); |
| 798 | | return $this->readList($statement, $resolveReferences); |
| 799 | | } |
| 800 | | |
| 801 | | public function readExpiredReservationsList(\DateTimeInterface $expirationDate, $scopeId, int $limit = 500, ?int $offset = null, int $resolveReferences = 0) |
| 802 | | { |
| 803 | | $selectQuery = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 804 | | $selectQuery |
| 805 | | ->addEntityMapping() |
| 806 | | ->addResolvedReferences($resolveReferences) |
| 807 | | ->addConditionScopeId($scopeId) |
| 808 | | ->addConditionIsReserved() |
| 809 | | ->addConditionProcessExpiredIPTimeStamp($expirationDate) |
| 810 | | ->addConditionIgnoreSlots() |
| 811 | | ->addLimit($limit, $offset); |
| 812 | | $statement = $this->fetchStatement($selectQuery); |
| 813 | | return $this->readList($statement, $resolveReferences); |
| 814 | | } |
| 815 | | |
| 816 | | public function readEmailReminderProcessListByInterval(\DateTimeInterface $now, \DateTimeInterface $lastRun, int $defaultReminderInMinutes, int $limit = 500, ?int $offset = null, int $resolveReferences = 0) |
| 817 | | { |
| 818 | | $selectQuery = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 819 | | $selectQuery |
| 820 | | ->addEntityMapping() |
| 821 | | ->addResolvedReferences($resolveReferences) |
| 822 | | ->addConditionProcessMailReminder($now, $lastRun, $defaultReminderInMinutes) |
| 823 | | ->addConditionAssigned() |
| 824 | | ->addConditionIgnoreSlots() |
| 825 | | ->addConditionStatus('confirmed') |
| 826 | | ->addLimit($limit, $offset); |
| 827 | | $statement = $this->fetchStatement($selectQuery); |
| 828 | | return $this->readList($statement, $resolveReferences)->withDepartmentHasMailFrom(); |
| 829 | | } |
| 830 | | |
| 831 | | public function readDeallocateProcessList(\DateTimeInterface $now, int $limit = 500, ?int $offset = null, int $resolveReferences = 0) |
| 832 | | { |
| 833 | | $selectQuery = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 834 | | $selectQuery |
| 835 | | ->addEntityMapping() |
| 836 | | ->addResolvedReferences($resolveReferences) |
| 837 | | ->addConditionDeallocate($now) |
| 838 | | ->addConditionIgnoreSlots() |
| 839 | | ->addLimit($limit, $offset); |
| 840 | | $statement = $this->fetchStatement($selectQuery); |
| 841 | | return $this->readList($statement, $resolveReferences); |
| 842 | | } |
| 843 | | |
| 844 | | public function isAppointmentAllowedWithSameMail(Entity $entity): bool |
| 845 | | { |
| 846 | | if (empty($entity->getClients()) || empty($entity->getClients()->getFirst())) { |
| 847 | | return true; |
| 848 | | } |
| 849 | | |
| 850 | | $maxAppointmentsPerMail = $entity->scope->getAppointmentsPerMail(); |
| 851 | | $emailToCheck = $entity->getClients()->getFirst()->email; |
| 852 | | if ($maxAppointmentsPerMail < 1 || empty($emailToCheck)) { |
| 853 | | return true; |
| 854 | | } |
| 855 | | |
| 856 | | if ($this->isMailWhitelisted($emailToCheck, $entity->scope)) { |
| 857 | | return true; |
| 858 | | } |
| 859 | | |
| 860 | | $processes = $this->readProcessListByMailAddress($entity->getClients()->getFirst()->email, $entity->scope->id); |
| 861 | | $activeAppointments = 0; |
| 862 | | foreach ($processes as $process) { |
| 863 | | |
| 864 | | |
| 865 | | if ($entity->id == $process->id) { |
| 866 | | continue; |
| 867 | | } |
| 868 | | |
| 869 | | if (in_array($process->getStatus(), ['preconfirmed', 'confirmed'])) { |
| 870 | | $activeAppointments++; |
| 871 | | if ($activeAppointments >= $maxAppointmentsPerMail) { |
| 872 | | return false; |
| 873 | | } |
| 874 | | } |
| 875 | | } |
| 876 | | |
| 877 | | return true; |
| 878 | | } |
| 879 | | |
| 880 | | protected function isMailWhitelisted(string $email, ScopeEntity $scope): bool |
| 881 | | { |
| 882 | | $emailsWithNoLimit = explode(',', $scope->getWhitelistedMails()); |
| 883 | | if (empty($emailsWithNoLimit)) { |
| 884 | | return false; |
| 885 | | } |
| 886 | | |
| 887 | | foreach ($emailsWithNoLimit as $whitelistedMail) { |
| 888 | | $whitelistedMail = trim($whitelistedMail); |
| 889 | | if ($email === $whitelistedMail) { |
| 890 | | return true; |
| 891 | | } |
| 892 | | |
| 893 | | if (str_starts_with($whitelistedMail, '@') && str_contains($email, $whitelistedMail)) { |
| 894 | | return true; |
| 895 | | } |
| 896 | | } |
| 897 | | |
| 898 | | return false; |
| 899 | | } |
| 900 | | |
| 901 | | public function readProcessWithSameDayAndDisplayNumber($scopeId, $displayNumber, $date) |
| 902 | | { |
| 903 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 904 | | $query->addEntityMapping() |
| 905 | | ->addResolvedReferences(1) |
| 906 | | ->addConditionScopeId($scopeId) |
| 907 | | ->addConditionDate($date) |
| 908 | | ->addConditionDisplayNumber($displayNumber) |
| 909 | | ->addLimit(1); |
| 910 | | |
| 911 | | $process = $this->fetchOne($query, new Entity()); |
| 912 | | |
| 913 | | return $this->readResolvedReferences($process, 1); |
| 914 | | } |
| 915 | | |
| 916 | | public function readProcessListByExternalUserId(string $externalUserId, ?int $filterId = null, ?string $status = null, int $resolveReferences = 0, int $limit = 1000): Collection |
| 917 | | { |
| 918 | | $query = new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT); |
| 919 | | $query |
| 920 | | ->addResolvedReferences($resolveReferences) |
| 921 | | ->addEntityMapping() |
| 922 | | ->addConditionExternalUserId($externalUserId); |
| 923 | | if (!is_null($filterId)) { |
| 924 | | $query->addConditionProcessId($filterId); |
| 925 | | } |
| 926 | | if (!is_null($status)) { |
| 927 | | $query->addConditionStatus($status); |
| 928 | | } |
| 929 | | $query |
| 930 | | ->addLimit($limit); |
| 931 | | |
| 932 | | $statement = $this->fetchStatement($query); |
| 933 | | $processList = $this->readProcessesWithoutRequests($statement); |
| 934 | | return $this->attachAllRequestsInOneQuery($processList, (int) $resolveReferences); |
| 935 | | } |
| 936 | | |
| 937 | | public function readAssignedWorkstationIdForUpdate(int $processId): ?int |
| 938 | | { |
| 939 | | $query = (new \BO\Zmsbackend\Process\Repository\Process(\BO\Zmsbackend\Query\Base::SELECT))->getLockAssignedWorkstationId(); |
| 940 | | $value = $this->fetchValue($query, ['processId' => $processId]); |
| 941 | | |
| 942 | | if ($value === false || $value === null) { |
| 943 | | return null; |
| 944 | | } |
| 945 | | |
| 946 | | return (int) $value; |
| 947 | | } |
| 948 | | } |