Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.50% covered (warning)
85.50%
283 / 331
68.00% covered (warning)
68.00%
51 / 75
CRAP
0.00% covered (danger)
0.00%
0 / 1
Process
85.50% covered (warning)
85.50%
283 / 331
68.00% covered (warning)
68.00%
51 / 75
234.13
0.00% covered (danger)
0.00%
0 / 1
 getDefaults
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
1
 createFromScope
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 getRequests
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 getRequestIds
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDisplayNumber
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRequestCSV
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addScope
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addQueue
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 addRequests
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 updateRequests
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 hasScopeAdmin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sendAdminMailOnConfirmation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 sendAdminMailOnDeleted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 sendAdminMailOnUpdated
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 shouldSendAdminMailOnClerkMail
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 withUpdatedData
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 addAppointmentFromRequest
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 addClientFromForm
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 setStatus
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getStatus
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getReminderTimestamp
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 addReminderTimestamp
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getAppointments
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 getClients
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
4.07
 hasAppointment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isWithAppointment
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 hasProcessCredentials
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
4
 withReassignedCredentials
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 hasQueueNumber
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
 getQueueNumber
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addAppointment
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getScopeId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCurrentScope
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAmendment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getShowUpTime
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getWaitingTime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getProcessingTime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFinishTime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addAmendment
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 getCustomTextfield
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addCustomTextfield
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getCustomTextfield2
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addCustomTextfield2
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 addPriority
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 getAuthKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPriority
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setRandomAuthKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCallTime
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getCallTimeString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCallTime
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getFirstClient
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getFirstAppointment
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 setStatusBySettings
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 setClientsCount
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 withoutPersonalData
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 withLessData
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
20
 toCalendar
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 toQueue
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
6.02
 hasArrivalTime
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getArrivalTime
72.73% covered (warning)
72.73%
8 / 11
0.00% covered (danger)
0.00%
0 / 1
4.32
 setArrivalTime
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getWaitedSeconds
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getWaitedMinutes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getWaySeconds
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getWayMinutes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setWasMissed
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getWasMissed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 toDerefencedAmendment
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 toDerefencedCustomTextfield
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 toDerefencedCustomTextfield2
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 __toString
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
 getExternalUserId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setExternalUserId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getParkedBy
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setParkedBy
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BO\Zmsentities;
4
5use BO\Zmsentities\Helper\ProcessPlainText;
6use BO\Zmsentities\Helper\Property;
7
8/**
9 * @SuppressWarnings(Complexity)
10 * @SuppressWarnings(Coupling)
11 * @SuppressWarnings(Public)
12 *
13 */
14class Process extends Schema\Entity
15{
16    public const PRIMARY = 'id';
17    public const STATUS_FREE = 'free';
18    public const STATUS_RESERVED = 'reserved';
19    public const STATUS_CONFIRMED = 'confirmed';
20    public const STATUS_PRECONFIRMED = 'preconfirmed';
21    public const STATUS_QUEUED = 'queued';
22    public const STATUS_CALLED = 'called';
23    public const STATUS_PROCESSING = 'processing';
24    public const STATUS_PENDING = 'pending';
25    public const STATUS_FINISHED = 'finished';
26    public const STATUS_MISSED = 'missed';
27    public const STATUS_PARKED = 'parked';
28    public const STATUS_ARCHIVED = 'archived';
29    public const STATUS_DELETED = 'deleted';
30    public const STATUS_ANONYMIZED = 'anonymized';
31    public const STATUS_BLOCKED = 'blocked';
32    public const STATUS_CONFLICT = 'conflict';
33    public static $schema = "process.json";
34    #[\Override]
35    public function getDefaults()
36    {
37        return [
38            'amendment' => '',
39            'customTextfield' => '',
40            'customTextfield2' => '',
41            'appointments' => new Collection\AppointmentList(),
42            'apiclient' => new Apiclient(),
43            'authKey' => '',
44            'captchaToken' => '',
45            'clients' => new Collection\ClientList(),
46            'createIP' => '',
47            'createTimestamp' => time(),
48            'id' => 0,
49            'archiveId' => 0,
50            'queue' => new Queue(),
51            'reminderTimestamp' => 0,
52            'requests' => new Collection\RequestList(),
53            'scope' => new Scope(),
54            'status' => 'free',
55            'displayNumber' => '',
56            'dbstatus' => 'free',
57            'lastChange' => time(),
58            'wasMissed' => false,
59            'priority' => null,
60            'externalUserId' => null,
61            'parkedBy' => null,
62        ];
63    }
64
65    public static function createFromScope(Scope $scope, \DateTimeInterface $dateTime)
66    {
67        $appointment = new Appointment();
68        $appointment->addScope($scope->id);
69        $appointment->addSlotCount(0);
70        $appointment->addDate($dateTime->modify('00:00:00')->getTimestamp());
71        $process = new static();
72        $process->scope = $scope;
73        $process->setStatus('queued');
74        $process->addAppointment($appointment);
75        return $process;
76    }
77
78    /**
79     * @return Collection\RequestList
80     *
81     */
82    public function getRequests()
83    {
84        if (!$this->requests instanceof Collection\RequestList) {
85            $requestList = new Collection\RequestList();
86            foreach ($this->requests as $request) {
87                $request = ($request instanceof Request) ? $request : new Request($request);
88                $requestList->addEntity($request);
89            }
90            $this->requests = $requestList;
91        }
92        return $this->requests;
93    }
94
95    public function getRequestIds()
96    {
97        return $this->getRequests()->getIds();
98    }
99
100    public function getDisplayNumber()
101    {
102        return $this->displayNumber;
103    }
104
105    public function getRequestCSV()
106    {
107        return $this->getRequests()->getIdsCsv();
108    }
109
110    public function addScope($scopeId)
111    {
112        $this->scope = new Scope(array('id' => $scopeId));
113        return $this;
114    }
115
116    public function addQueue($number, \DateTimeInterface $dateTime)
117    {
118        $this->queue = new Queue(array(
119            'number' => $number,
120            'arrivalTime' => $dateTime->getTimestamp(),
121            'priority' => $this->getPriority()
122        ));
123        return $this;
124    }
125
126    public function addRequests($source, $requestCSV)
127    {
128        $requestList = $this->getRequests();
129        foreach (explode(',', $requestCSV) as $id) {
130            if (!$requestList->hasRequests($id)) {
131                $this->requests[] = new Request(array(
132                    'source' => $source,
133                    'id' => $id
134                ));
135            }
136        }
137        return $this;
138    }
139
140    public function updateRequests($source, $requestCSV = '')
141    {
142        $this->requests = new Collection\RequestList();
143        if ($requestCSV) {
144            foreach (explode(',', $requestCSV) as $id) {
145                $this->requests->addEntity(new Request(array(
146                            'source' => $source,
147                            'id' => $id
148                        )));
149            }
150        }
151        return $this;
152    }
153
154    public function hasScopeAdmin()
155    {
156        return ('' != $this->toProperty()->scope->contact->email->get());
157    }
158
159    public function sendAdminMailOnConfirmation()
160    {
161        return (bool) ((int) $this->toProperty()->scope->preferences->client->adminMailOnAppointment->get());
162    }
163
164    public function sendAdminMailOnDeleted()
165    {
166        return (bool) ((int) $this->toProperty()->scope->preferences->client->adminMailOnDeleted->get());
167    }
168
169    public function sendAdminMailOnUpdated()
170    {
171        return (bool) ((int) $this->toProperty()->scope->preferences->client->adminMailOnUpdated->get());
172    }
173
174    public function shouldSendAdminMailOnClerkMail()
175    {
176        return (bool) ((int) $this->toProperty()->scope->preferences->client->adminMailOnMailSent->get());
177    }
178
179    public function withUpdatedData($requestData, \DateTimeInterface $dateTime, $scope = null, $notice = '')
180    {
181        $this->scope = ($scope) ? $scope : $this->scope;
182        $this->addAppointmentFromRequest($requestData, $dateTime);
183        $requestCsv = isset($requestData['requests']) ? implode(',', $requestData['requests']) : 0;
184        $this->updateRequests($scope->getSource(), $requestCsv);
185        $this->addClientFromForm($requestData);
186        $this->addReminderTimestamp($requestData, $dateTime);
187        $this->addAmendment($requestData, $notice);
188        $this->addCustomTextfield($requestData);
189        $this->addCustomTextfield2($requestData);
190        $this->addPriority($requestData);
191        return $this;
192    }
193
194    public function addAppointmentFromRequest($requestData, \DateTimeInterface $dateTime)
195    {
196        $this->appointments = null;
197        if (isset($requestData['selecteddate'])) {
198            $dateTime = new \DateTime($requestData['selecteddate']);
199        }
200        if (isset($requestData['selectedtime'])) {
201            $time = explode('-', $requestData['selectedtime']);
202            $dateTime->setTime($time[0], $time[1]);
203        }
204
205        $appointment = (new Appointment())
206            ->addDate($dateTime->getTimestamp())
207            ->addScope($this->scope['id']);
208        if (isset($requestData['slotCount'])) {
209            $appointment->addSlotCount($requestData['slotCount']);
210        }
211        $this->addAppointment($appointment);
212        return $this;
213    }
214
215    public function addClientFromForm($requestData)
216    {
217        $client = new Client();
218        foreach ($requestData as $key => $value) {
219            if (null !== $value && $client->offsetExists($key)) {
220                $client[$key] = (isset($value['value'])) ? $value['value'] : $value;
221            }
222        }
223        $this->clients = array();
224        $this->clients[] = $client;
225        return $this;
226    }
227
228    public function setStatus($status)
229    {
230        $this->status = $status;
231        return $this;
232    }
233
234    public function getStatus()
235    {
236        return $this->status;
237    }
238
239    public function getReminderTimestamp()
240    {
241        $timestamp = $this->toProperty()->reminderTimestamp->get();
242        return ($timestamp) ? $timestamp : 0;
243    }
244
245    public function addReminderTimestamp($input, \DateTimeInterface $dateTime)
246    {
247        $this->reminderTimestamp = (
248            Property::__keyExists('headsUpTime', $input) &&
249            $input['headsUpTime'] > 0
250        ) ? $dateTime->getTimestamp() - $input['headsUpTime'] : 0;
251        return $this;
252    }
253
254    /**
255     * @return \BO\Zmsentities\Collection\AppointmentList
256     *
257     */
258    public function getAppointments()
259    {
260        if (!$this['appointments'] instanceof Collection\AppointmentList) {
261            $this['appointments'] = new Collection\AppointmentList($this['appointments']);
262            foreach ($this['appointments'] as $index => $appointment) {
263                if (!$appointment instanceof Appointment) {
264                    $this['appointments'][$index] = new Appointment($appointment);
265                }
266            }
267        }
268        return $this['appointments'];
269    }
270
271    /**
272     * @return \BO\Zmsentities\Collection\ClientList
273     *
274     */
275    public function getClients()
276    {
277        if (!$this['clients'] instanceof Collection\ClientList) {
278            $this['clients'] = new Collection\ClientList($this['clients']);
279            foreach ($this['clients'] as $index => $client) {
280                if (!$client instanceof Client) {
281                    $this['clients'][$index] = new Client($client);
282                }
283            }
284        }
285        return $this['clients'];
286    }
287
288    public function hasAppointment($date, $scopeId)
289    {
290        return $this->getAppointments()->hasDateScope($date, $scopeId);
291    }
292
293    /**
294     * check if process is with appointment and not only queued
295     * return Boolean
296     */
297    public function isWithAppointment()
298    {
299        $appointment = $this->getFirstAppointment();
300        if ($appointment->hasTime()) {
301            return true;
302        }
303        return (1 == $this->toProperty()->queue->withAppointment->get());
304    }
305
306    public function hasProcessCredentials()
307    {
308        return (isset($this['id']) && isset($this['authKey']) && $this['id'] && $this['authKey']);
309    }
310
311    public function withReassignedCredentials($process)
312    {
313        $this->id = $process->getId();
314        $this->authKey = $process->getAuthKey();
315        return $this;
316    }
317
318    public function hasQueueNumber()
319    {
320        return (isset($this['queue']) && isset($this['queue']['number']) && $this['queue']['number']);
321    }
322
323    public function getQueueNumber()
324    {
325        return $this['queue']['number'];
326    }
327
328    public function addAppointment(Appointment $newappointment)
329    {
330        $this->appointments[] = $newappointment;
331        return $this;
332    }
333
334    /**
335     * Reminder: A process might have multiple scopes. Each appointment can
336     * have his own scope. The scope in $this->scope is the current/next scope.
337     * This function returns the original scope ID and ignores internal scope
338     * which are used for processing like to pick up documents
339     *
340     */
341    public function getScopeId()
342    {
343        //as current scope - see zmsdb Query/Process EntityMapping
344        return $this->toProperty()->scope->id->get();
345    }
346
347    public function getCurrentScope(): Scope
348    {
349        return $this->getProperty('scope');
350    }
351
352    public function getAmendment()
353    {
354        return $this->toProperty()->amendment->get();
355    }
356
357    /*public function getShowUpTime()
358    {
359        return $this->toProperty()->showUpTime->get();
360    }*/
361
362    public function getShowUpTime($default = 'now', $timezone = null)
363    {
364        $showUpTime = $this->toProperty()->showUpTime->get();
365        $showDateTime = Helper\DateTime::create($default, $timezone);
366        if ($showUpTime) {
367            list($hours, $minutes, $seconds) = explode(':', $showUpTime);
368            $showDateTime = $showDateTime->setTime(intval($hours), intval($minutes), intval($seconds));
369        }
370        return $showDateTime;
371    }
372
373    public function getWaitingTime()
374    {
375        return $this->toProperty()->queue->waitingTime->get();
376    }
377
378    public function getProcessingTime()
379    {
380        return $this->toProperty()->processingTime->get();
381    }
382
383    public function getFinishTime()
384    {
385        return $this->toProperty()->finishTime->get();
386    }
387
388    public function addAmendment($input, $notice = '')
389    {
390        $this->amendment = $notice;
391        $this->amendment .= (isset($input['amendment']) && $input['amendment']) ? $input['amendment'] : '';
392        $this->amendment = trim(ProcessPlainText::normalize($this->amendment));
393        return $this;
394    }
395
396    public function getCustomTextfield()
397    {
398        return $this->toProperty()->customTextfield->get();
399    }
400
401    public function addCustomTextfield($input)
402    {
403        $this->customTextfield = (
404            isset($input['customTextfield']) && $input['customTextfield']
405        ) ? $input['customTextfield'] : '';
406        $this->customTextfield = trim(ProcessPlainText::normalize($this->customTextfield));
407        return $this;
408    }
409
410    public function getCustomTextfield2()
411    {
412        return $this->toProperty()->customTextfield2->get();
413    }
414
415    public function addCustomTextfield2($input)
416    {
417        $this->customTextfield2 = (
418            isset($input['customTextfield2']) && $input['customTextfield2']
419        ) ? $input['customTextfield2'] : '';
420        $this->customTextfield2 = trim(ProcessPlainText::normalize($this->customTextfield2));
421        return $this;
422    }
423
424    public function addPriority($input)
425    {
426        $this->priority = isset($input['priority']) && $input['priority'] ? $input['priority'] : null;
427
428        return $this;
429    }
430
431    public function getAuthKey()
432    {
433        return $this->toProperty()->authKey->get();
434    }
435
436    public function getPriority()
437    {
438        return (int) $this->toProperty()->priority->get();
439    }
440
441    public function setRandomAuthKey()
442    {
443        $this->authKey = bin2hex(random_bytes(32));
444    }
445
446    public function setCallTime($dateTime = null)
447    {
448        $this->queue['callTime'] = ($dateTime) ? $dateTime->getTimestamp() : 0;
449        return $this;
450    }
451
452    public function getCallTimeString()
453    {
454        return $this->getCallTime()->format('H:i:s');
455    }
456
457    public function getCallTime($default = 'now', $timezone = null)
458    {
459        $callTime = $this->toProperty()->queue->callTime->get();
460        $callDateTime = Helper\DateTime::create($default, $timezone);
461        if ($callTime) {
462            $callDateTime = $callDateTime->setTimestamp($callTime);
463        }
464        return $callDateTime;
465    }
466
467    public function getFirstClient()
468    {
469        $client = $this->getClients()->getFirst();
470        if (!$client) {
471            $client = new Client();
472            $this->clients->addEntity($client);
473        }
474        return $client;
475    }
476
477    public function getFirstAppointment(): Appointment
478    {
479        $appointment = $this->getAppointments()->getFirst();
480        if (!$appointment) {
481            $appointment = new Appointment();
482            $appointment->scope = $this->scope;
483            $this->appointments->addEntity($appointment);
484        }
485        return $appointment;
486    }
487
488    public function setStatusBySettings()
489    {
490        $scope = new Scope($this->scope);
491        if ('called' == $this->status && $this->queue['callCount'] > $scope->getPreference('queue', 'callCountMax')) {
492            $this->status = 'missed';
493        } elseif ('parked' == $this->status) {
494            $this->status = 'parked';
495        } else {
496            $this->status = 'confirmed';
497        }
498        return $this;
499    }
500
501    public function setClientsCount($count)
502    {
503        $clientList = $this->getClients();
504        while ($clientList->count() < $count) {
505            $clientList->addEntity(new Client());
506        }
507        return $this;
508    }
509
510
511    public function withoutPersonalData()
512    {
513        $entity = clone $this;
514        if ($this->toProperty()->clients->isAvailable()) {
515            $client = $entity->getFirstClient();
516            unset($client['familyName']);
517            unset($client['email']);
518        }
519        return $entity;
520    }
521
522    /**
523     * Reduce data of dereferenced entities to a required minimum
524     *
525     */
526    #[\Override]
527    public function withLessData(array $keepArray = [])
528    {
529        $entity = clone $this;
530        if (!in_array('availability', $keepArray)) {
531            foreach ($entity['appointments'] as $appointment) {
532                if ($appointment->toProperty()->scope->isAvailable()) {
533                    $scopeId = $appointment['scope']['id'];
534                    $appointment['scope'] = [
535                        'id' => $scopeId,
536                        'provider' => $appointment['scope']['provider'] ?? [],
537                        'shortName' => $appointment['scope']['shortName'] ?? ''
538                    ];
539                }
540                if ($appointment->toProperty()->availability->isAvailable()) {
541                    unset($appointment['availability']);
542                }
543            }
544        }
545
546        unset($entity['createTimestamp']);
547        unset($entity['createIP']);
548        if ($entity->toProperty()->scope->status->isAvailable()) {
549            unset($entity['scope']['status']);
550        }
551
552        if ($entity->status == 'free') {
553// delete keys
554            foreach (['authKey', 'queue', 'requests',] as $key) {
555                if (!in_array($key, $keepArray) && $entity->toProperty()->$key->isAvailable()) {
556                    unset($entity[$key]);
557                }
558            }
559            // delete if empty
560            foreach (['amendment', 'id', 'authKey', 'archiveId', 'reminderTimestamp',] as $key) {
561                if (!in_array($key, $keepArray) && $entity->toProperty()->$key->isAvailable() && !$entity[$key]) {
562                    unset($entity[$key]);
563                }
564            }
565            if (!in_array('provider', $keepArray) && $entity->toProperty()->scope->provider->data->isAvailable()) {
566                unset($entity['scope']['provider']['data']);
567            }
568        }
569
570        if (!in_array('dayoff', $keepArray) && $entity->toProperty()->scope->dayoff->isAvailable()) {
571            unset($entity['scope']['dayoff']);
572        }
573        if (!in_array('scope', $keepArray) && $entity->toProperty()->scope->preferences->isAvailable()) {
574            unset($entity['scope']['preferences']);
575        }
576        return $entity;
577    }
578
579    public function toCalendar()
580    {
581        $calendar = new Calendar();
582        $dateTime = $this->getFirstAppointment()->toDateTime();
583        $day = new Day();
584        $day->setDateTime($dateTime);
585        $calendar->firstDay = $day;
586        $calendar->lastDay = $day;
587        $calendar->requests = clone $this->getRequests();
588        $calendar->scopes = new Collection\ScopeList([$this->scope]);
589        return $calendar;
590    }
591
592    public function toQueue(\DateTimeInterface $dateTime)
593    {
594        $queue = new Queue($this->queue);
595        $queue->withAppointment = ($this->getFirstAppointment()->hasTime()) ? true : false;
596        $queue->waitingTime = ($queue->waitingTime) ? $queue->waitingTime : 0;
597        $queue->wayTime = ($queue->wayTime) ? $queue->wayTime : 0;
598        if ($queue->withAppointment) {
599            $queue->number = !empty($this->getDisplayNumber())
600                ? $this->getDisplayNumber()
601                : $this->id;
602        } else {
603            $queue->number = $this->toProperty()->queue->number->get();
604        }
605        $queue->displayNumber = $this->getDisplayNumber();
606        $queue->arrivalTime = $this->getArrivalTime($dateTime)->getTimestamp();
607        $queue->priority = $this->priority;
608        return $queue->setProcess($this);
609    }
610
611    public function hasArrivalTime()
612    {
613        $arrivalTime = 0;
614        if ($this->isWithAppointment()) {
615            $arrivalTime = $this->getFirstAppointment()->date;
616        } else {
617            $arrivalTime = $this->toProperty()->queue->arrivalTime->get();
618        }
619        return ($arrivalTime) ? true : false;
620    }
621
622    public function getArrivalTime($default = 'now', $timezone = null)
623    {
624        $queueArrivalTime = $this->toProperty()->queue->arrivalTime->get();
625
626        if ($queueArrivalTime) {
627            // Falls der Queue-Wert vorhanden ist â€“ auch wenn ein Termin existiert â€“ verwende diesen (dabei ist handelt es sich, um verpasste Termine)
628            $arrivalTime = $queueArrivalTime;
629        } elseif ($this->isWithAppointment()) {
630            $arrivalTime = $this->getFirstAppointment()->date;
631        } else {
632            $arrivalTime = 0;
633        }
634
635        $arrivalTime = (int)$arrivalTime;
636        $arrivalDateTime = Helper\DateTime::create($default, $timezone);
637        if ($arrivalTime) {
638            $arrivalDateTime = $arrivalDateTime->setTimestamp($arrivalTime);
639        }
640        return $arrivalDateTime;
641    }
642
643    public function setArrivalTime(\DateTimeInterface $dateTime = null)
644    {
645        $this->queue['arrivalTime'] = ($dateTime) ? $dateTime->getTimestamp() : 0;
646        return $this;
647    }
648
649    /**
650     * Calculate real waiting time, only available after called
651     */
652    public function getWaitedSeconds($defaultTime = 'now')
653    {
654        return $this->getCallTime($defaultTime)->getTimestamp() - $this->getArrivalTime($defaultTime)->getTimestamp();
655    }
656
657    public function getWaitedMinutes($defaultTime = 'now')
658    {
659        return $this->getWaitedSeconds($defaultTime) / 60;
660    }
661
662    public function getWaySeconds($defaultTime = 'now')
663    {
664        return $this->getShowUpTime($defaultTime)->getTimestamp() - $this->getCallTime($defaultTime)->getTimestamp();
665    }
666
667    public function getWayMinutes($defaultTime = 'now')
668    {
669        return $this->getWaySeconds($defaultTime) / 60;
670    }
671
672    public function setWasMissed(bool $bool)
673    {
674        $this->wasMissed = $bool;
675        $this->status = self::STATUS_MISSED;
676        return $this;
677    }
678
679    public function getWasMissed(): bool
680    {
681        return (bool) $this->wasMissed;
682    }
683
684    public function toDerefencedAmendment()
685    {
686        $lastChange = (new \DateTimeImmutable())->setTimestamp($this->createTimestamp)->format('c');
687        return var_export(array(
688                'BuergerID' => $this->id,
689                'StandortID' => $this->scope['id'],
690                'Anmerkung' => null,
691                'IPTimeStamp' => $this->createTimestamp,
692                'LastChange' => $lastChange,
693            ), 1);
694    }
695
696    public function toDerefencedCustomTextfield()
697    {
698        $lastChange = (new \DateTimeImmutable())->setTimestamp($this->createTimestamp)->format('c');
699        return var_export(array(
700                'BuergerID' => $this->id,
701                'StandortID' => $this->scope['id'],
702                'CustomTextfield' => null,
703                'IPTimeStamp' => $this->createTimestamp,
704                'LastChange' => $lastChange,
705            ), 1);
706    }
707
708    public function toDerefencedCustomTextfield2()
709    {
710        $lastChange = (new \DateTimeImmutable())->setTimestamp($this->createTimestamp)->format('c');
711        return var_export(array(
712                'BuergerID' => $this->id,
713                'StandortID' => $this->scope['id'],
714                'CustomTextfield2' => null,
715                'IPTimeStamp' => $this->createTimestamp,
716                'LastChange' => $lastChange,
717            ), 1);
718    }
719
720    public function __toString()
721    {
722        $string = "process#";
723        $string .= $this->getDisplayNumber() ?: $this->id ?: $this->archiveId;
724        $string .= ":" . $this->authKey;
725        $string .= " (" . $this->status . ")";
726        $string .= " " . $this->getFirstAppointment()->toDateTime()->format('c');
727        $string .= " " . ($this->isWithAppointment() ? "appoint" : "arrival:" . $this->getArrivalTime()->format('c'));
728        $string .= " " . $this->getFirstAppointment()->slotCount . "slots";
729        $string .= "*" . count($this->appointments);
730        foreach ($this->getRequests() as $request) {
731            $string .= " " . $request['source'] . "." . $request['id'];
732        }
733        $string .= " scope." . $this['scope']['id'];
734        $string .= " ~" . base_convert($this['lastChange'], 10, 35);
735        $string .= " client:" . $this['apiclient']['shortname'];
736        $string .= " token:" . ($this['captchaToken'] ?? '(none)');
737        return $string;
738    }
739
740    public function getExternalUserId()
741    {
742        return $this->toProperty()->externalUserId->get();
743    }
744
745    public function setExternalUserId($externalUserId)
746    {
747        $this->externalUserId = $externalUserId;
748        return $this;
749    }
750
751    public function getParkedBy()
752    {
753        return $this->toProperty()->parkedBy->get();
754    }
755
756    public function setParkedBy($name)
757    {
758        $this->parkedBy = $name;
759    }
760}