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