Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.06% covered (warning)
86.06%
284 / 330
69.86% covered (warning)
69.86%
51 / 73
CRAP
0.00% covered (danger)
0.00%
0 / 1
Process
86.06% covered (warning)
86.06%
284 / 330
69.86% covered (warning)
69.86%
51 / 73
223.76
0.00% covered (danger)
0.00%
0 / 1
 getDefaults
100.00% covered (success)
100.00%
25 / 25
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
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
5.03
 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
1<?php
2
3namespace BO\Zmsentities;
4
5use BO\Zmsentities\Helper\Property;
6
7/**
8 * @SuppressWarnings(Complexity)
9 * @SuppressWarnings(Coupling)
10 * @SuppressWarnings(Public)
11 *
12 */
13class Process extends Schema\Entity
14{
15    public const PRIMARY = 'id';
16    public const STATUS_FREE = 'free';
17    public const STATUS_RESERVED = 'reserved';
18    public const STATUS_CONFIRMED = 'confirmed';
19    public const STATUS_PRECONFIRMED = 'preconfirmed';
20    public const STATUS_QUEUED = 'queued';
21    public const STATUS_CALLED = 'called';
22    public const STATUS_PROCESSING = 'processing';
23    public const STATUS_PENDING = 'pending';
24    public const STATUS_PICKUP = 'pickup';
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    public function getDefaults()
35    {
36        return [
37            'amendment' => '',
38            'customTextfield' => '',
39            'customTextfield2' => '',
40            'appointments' => new Collection\AppointmentList(),
41            'apiclient' => new Apiclient(),
42            'authKey' => '',
43            'captchaToken' => '',
44            'clients' => new Collection\ClientList(),
45            'createIP' => '',
46            'createTimestamp' => time(),
47            'id' => 0,
48            'archiveId' => 0,
49            'queue' => new Queue(),
50            'reminderTimestamp' => 0,
51            'requests' => new Collection\RequestList(),
52            'scope' => new Scope(),
53            'status' => 'free',
54            'displayNumber' => '',
55            'dbstatus' => 'free',
56            'lastChange' => time(),
57            'wasMissed' => false,
58            'priority' => null,
59            'external_user_id' => null,
60        ];
61    }
62
63    public static function createFromScope(Scope $scope, \DateTimeInterface $dateTime)
64    {
65        $appointment = new Appointment();
66        $appointment->addScope($scope->id);
67        $appointment->addSlotCount(0);
68        $appointment->addDate($dateTime->modify('00:00:00')->getTimestamp());
69        $process = new static();
70        $process->scope = $scope;
71        $process->setStatus('queued');
72        $process->addAppointment($appointment);
73        return $process;
74    }
75
76    /**
77     * @return Collection\RequestList
78     *
79     */
80    public function getRequests()
81    {
82        if (!$this->requests instanceof Collection\RequestList) {
83            $requestList = new Collection\RequestList();
84            foreach ($this->requests as $request) {
85                $request = ($request instanceof Request) ? $request : new Request($request);
86                $requestList->addEntity($request);
87            }
88            $this->requests = $requestList;
89        }
90        return $this->requests;
91    }
92
93    public function getRequestIds()
94    {
95        return $this->getRequests()->getIds();
96    }
97
98    public function getDisplayNumber()
99    {
100        return $this->displayNumber;
101    }
102
103    public function getRequestCSV()
104    {
105        return $this->getRequests()->getIdsCsv();
106    }
107
108    public function addScope($scopeId)
109    {
110        $this->scope = new Scope(array('id' => $scopeId));
111        return $this;
112    }
113
114    public function addQueue($number, \DateTimeInterface $dateTime)
115    {
116        $this->queue = new Queue(array(
117            'number' => $number,
118            'arrivalTime' => $dateTime->getTimestamp(),
119            'priority' => $this->getPriority()
120        ));
121        return $this;
122    }
123
124    public function addRequests($source, $requestCSV)
125    {
126        $requestList = $this->getRequests();
127        foreach (explode(',', $requestCSV) as $id) {
128            if (!$requestList->hasRequests($id)) {
129                $this->requests[] = new Request(array(
130                    'source' => $source,
131                    'id' => $id
132                ));
133            }
134        }
135        return $this;
136    }
137
138    public function updateRequests($source, $requestCSV = '')
139    {
140        $this->requests = new Collection\RequestList();
141        if ($requestCSV) {
142            foreach (explode(',', $requestCSV) as $id) {
143                $this->requests->addEntity(new Request(array(
144                            'source' => $source,
145                            'id' => $id
146                        )));
147            }
148        }
149        return $this;
150    }
151
152    public function hasScopeAdmin()
153    {
154        return ('' != $this->toProperty()->scope->contact->email->get());
155    }
156
157    public function sendAdminMailOnConfirmation()
158    {
159        return (bool) ((int) $this->toProperty()->scope->preferences->client->adminMailOnAppointment->get());
160    }
161
162    public function sendAdminMailOnDeleted()
163    {
164        return (bool) ((int) $this->toProperty()->scope->preferences->client->adminMailOnDeleted->get());
165    }
166
167    public function sendAdminMailOnUpdated()
168    {
169        return (bool) ((int) $this->toProperty()->scope->preferences->client->adminMailOnUpdated->get());
170    }
171
172    public function shouldSendAdminMailOnClerkMail()
173    {
174        return (bool) ((int) $this->toProperty()->scope->preferences->client->adminMailOnMailSent->get());
175    }
176
177    public function withUpdatedData($requestData, \DateTimeInterface $dateTime, $scope = null, $notice = '')
178    {
179        $this->scope = ($scope) ? $scope : $this->scope;
180        $this->addAppointmentFromRequest($requestData, $dateTime);
181        $requestCsv = isset($requestData['requests']) ? implode(',', $requestData['requests']) : 0;
182        $this->updateRequests($scope->getSource(), $requestCsv);
183        $this->addClientFromForm($requestData);
184        $this->addReminderTimestamp($requestData, $dateTime);
185        $this->addAmendment($requestData, $notice);
186        $this->addCustomTextfield($requestData);
187        $this->addCustomTextfield2($requestData);
188        $this->addPriority($requestData);
189        return $this;
190    }
191
192    public function addAppointmentFromRequest($requestData, \DateTimeInterface $dateTime)
193    {
194        $this->appointments = null;
195        if (isset($requestData['selecteddate'])) {
196            $dateTime = new \DateTime($requestData['selecteddate']);
197        }
198        if (isset($requestData['selectedtime'])) {
199            $time = explode('-', $requestData['selectedtime']);
200            $dateTime->setTime($time[0], $time[1]);
201        }
202
203        $appointment = (new Appointment())
204            ->addDate($dateTime->getTimestamp())
205            ->addScope($this->scope['id']);
206        if (isset($requestData['slotCount'])) {
207            $appointment->addSlotCount($requestData['slotCount']);
208        }
209        $this->addAppointment($appointment);
210        return $this;
211    }
212
213    public function addClientFromForm($requestData)
214    {
215        $client = new Client();
216        foreach ($requestData as $key => $value) {
217            if (null !== $value && $client->offsetExists($key)) {
218                $client[$key] = (isset($value['value'])) ? $value['value'] : $value;
219            }
220        }
221        $this->clients = array();
222        $this->clients[] = $client;
223        return $this;
224    }
225
226    public function setStatus($status)
227    {
228        $this->status = $status;
229        return $this;
230    }
231
232    public function getStatus()
233    {
234        return $this->status;
235    }
236
237    public function getReminderTimestamp()
238    {
239        $timestamp = $this->toProperty()->reminderTimestamp->get();
240        return ($timestamp) ? $timestamp : 0;
241    }
242
243    public function addReminderTimestamp($input, \DateTimeInterface $dateTime)
244    {
245        $this->reminderTimestamp = (
246            Property::__keyExists('headsUpTime', $input) &&
247            $input['headsUpTime'] > 0
248        ) ? $dateTime->getTimestamp() - $input['headsUpTime'] : 0;
249        return $this;
250    }
251
252    /**
253     * @return \BO\Zmsentities\Collection\AppointmentList
254     *
255     */
256    public function getAppointments()
257    {
258        if (!$this['appointments'] instanceof Collection\AppointmentList) {
259            $this['appointments'] = new Collection\AppointmentList($this['appointments']);
260            foreach ($this['appointments'] as $index => $appointment) {
261                if (!$appointment instanceof Appointment) {
262                    $this['appointments'][$index] = new Appointment($appointment);
263                }
264            }
265        }
266        return $this['appointments'];
267    }
268
269    /**
270     * @return \BO\Zmsentities\Collection\ClientList
271     *
272     */
273    public function getClients()
274    {
275        if (!$this['clients'] instanceof Collection\ClientList) {
276            $this['clients'] = new Collection\ClientList($this['clients']);
277            foreach ($this['clients'] as $index => $client) {
278                if (!$client instanceof Client) {
279                    $this['clients'][$index] = new Client($client);
280                }
281            }
282        }
283        return $this['clients'];
284    }
285
286    public function hasAppointment($date, $scopeId)
287    {
288        return $this->getAppointments()->hasDateScope($date, $scopeId);
289    }
290
291    /**
292     * check if process is with appointment and not only queued
293     * return Boolean
294     */
295    public function isWithAppointment()
296    {
297        $appointment = $this->getFirstAppointment();
298        if ($appointment->hasTime()) {
299            return true;
300        }
301        return (1 == $this->toProperty()->queue->withAppointment->get());
302    }
303
304    public function hasProcessCredentials()
305    {
306        return (isset($this['id']) && isset($this['authKey']) && $this['id'] && $this['authKey']);
307    }
308
309    public function withReassignedCredentials($process)
310    {
311        $this->id = $process->getId();
312        $this->authKey = $process->getAuthKey();
313        return $this;
314    }
315
316    public function hasQueueNumber()
317    {
318        return (isset($this['queue']) && isset($this['queue']['number']) && $this['queue']['number']);
319    }
320
321    public function getQueueNumber()
322    {
323        return $this['queue']['number'];
324    }
325
326    public function addAppointment(Appointment $newappointment)
327    {
328        $this->appointments[] = $newappointment;
329        return $this;
330    }
331
332    /**
333     * Reminder: A process might have multiple scopes. Each appointment can
334     * have his own scope. The scope in $this->scope is the current/next scope.
335     * This function returns the original scope ID and ignores internal scope
336     * which are used for processing like to pick up documents
337     *
338     */
339    public function getScopeId()
340    {
341        //TK 2020-09-28 changed because pickup and pending processes have assigned pickup scope
342        //as current scope - see zmsdb Query/Process EntityMapping
343        return $this->toProperty()->scope->id->get();
344    }
345
346    public function getCurrentScope(): Scope
347    {
348        return $this->getProperty('scope');
349    }
350
351    public function getAmendment()
352    {
353        return $this->toProperty()->amendment->get();
354    }
355
356    /*public function getShowUpTime()
357    {
358        return $this->toProperty()->showUpTime->get();
359    }*/
360
361    public function getShowUpTime($default = 'now', $timezone = null)
362    {
363        $showUpTime = $this->toProperty()->showUpTime->get();
364        $showDateTime = Helper\DateTime::create($default, $timezone);
365        if ($showUpTime) {
366            list($hours, $minutes, $seconds) = explode(':', $showUpTime);
367            $showDateTime = $showDateTime->setTime(intval($hours), intval($minutes), intval($seconds));
368        }
369        return $showDateTime;
370    }
371
372    public function getWaitingTime()
373    {
374        return $this->toProperty()->queue->waitingTime->get();
375    }
376
377    public function getProcessingTime()
378    {
379        return $this->toProperty()->processingTime->get();
380    }
381
382    public function getFinishTime()
383    {
384        return $this->toProperty()->finishTime->get();
385    }
386
387    public function addAmendment($input, $notice = '')
388    {
389        $this->amendment = $notice;
390        $this->amendment .= (isset($input['amendment']) && $input['amendment']) ? $input['amendment'] : '';
391        trim($this->amendment);
392        return $this;
393    }
394
395    public function getCustomTextfield()
396    {
397        return $this->toProperty()->customTextfield->get();
398    }
399
400    public function addCustomTextfield($input)
401    {
402        $this->customTextfield .= (
403            isset($input['customTextfield']) && $input['customTextfield']
404        ) ? $input['customTextfield'] : '';
405        trim($this->customTextfield);
406        return $this;
407    }
408
409    public function getCustomTextfield2()
410    {
411        return $this->toProperty()->customTextfield2->get();
412    }
413
414    public function addCustomTextfield2($input)
415    {
416        $this->customTextfield2 .= (
417            isset($input['customTextfield2']) && $input['customTextfield2']
418        ) ? $input['customTextfield2'] : '';
419        trim($this->customTextfield2);
420        return $this;
421    }
422
423    public function addPriority($input)
424    {
425        $this->priority = isset($input['priority']) && $input['priority'] ? $input['priority'] : null;
426
427        return $this;
428    }
429
430    public function getAuthKey()
431    {
432        return $this->toProperty()->authKey->get();
433    }
434
435    public function getPriority()
436    {
437        return (int) $this->toProperty()->priority->get();
438    }
439
440    public function setRandomAuthKey()
441    {
442        $this->authKey = substr(md5(rand()), 0, 4);
443    }
444
445    public function setCallTime($dateTime = null)
446    {
447        $this->queue['callTime'] = ($dateTime) ? $dateTime->getTimestamp() : 0;
448        return $this;
449    }
450
451    public function getCallTimeString()
452    {
453        return $this->getCallTime()->format('H:i:s');
454    }
455
456    public function getCallTime($default = 'now', $timezone = null)
457    {
458        $callTime = $this->toProperty()->queue->callTime->get();
459        $callDateTime = Helper\DateTime::create($default, $timezone);
460        if ($callTime) {
461            $callDateTime = $callDateTime->setTimestamp($callTime);
462        }
463        return $callDateTime;
464    }
465
466    public function getFirstClient()
467    {
468        $client = $this->getClients()->getFirst();
469        if (!$client) {
470            $client = new Client();
471            $this->clients->addEntity($client);
472        }
473        return $client;
474    }
475
476    public function getFirstAppointment(): Appointment
477    {
478        $appointment = $this->getAppointments()->getFirst();
479        if (!$appointment) {
480            $appointment = new Appointment();
481            $appointment->scope = $this->scope;
482            $this->appointments->addEntity($appointment);
483        }
484        return $appointment;
485    }
486
487    public function setStatusBySettings()
488    {
489        $scope = new Scope($this->scope);
490        if ('called' == $this->status && $this->queue['callCount'] > $scope->getPreference('queue', 'callCountMax')) {
491            $this->status = 'missed';
492        } elseif ('parked' == $this->status) {
493            $this->status = 'parked';
494        } elseif ('pickup' == $this->status) {
495            $this->status = 'queued';
496        } else {
497            $this->status = 'confirmed';
498        }
499        return $this;
500    }
501
502    public function setClientsCount($count)
503    {
504        $clientList = $this->getClients();
505        while ($clientList->count() < $count) {
506            $clientList->addEntity(new Client());
507        }
508        return $this;
509    }
510
511
512    public function withoutPersonalData()
513    {
514        $entity = clone $this;
515        if ($this->toProperty()->clients->isAvailable()) {
516            $client = $entity->getFirstClient();
517            unset($client['familyName']);
518            unset($client['email']);
519        }
520        return $entity;
521    }
522
523    /**
524     * Reduce data of dereferenced entities to a required minimum
525     *
526     */
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()->external_user_id->get();
743    }
744
745    public function setExternalUserId($externalUserId)
746    {
747        $this->external_user_id = $externalUserId;
748        return $this;
749    }
750}