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\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_FINISHED = 'finished';
25    public const STATUS_MISSED = 'missed';
26    public const STATUS_PARKED = 'parked';
27    public const STATUS_ARCHIVED = 'archived';
28    public const STATUS_DELETED = 'deleted';
29    public const STATUS_ANONYMIZED = 'anonymized';
30    public const STATUS_BLOCKED = 'blocked';
31    public const STATUS_CONFLICT = 'conflict';
32    public static $schema = "process.json";
33    public function getDefaults()
34    {
35        return [
36            'amendment' => '',
37            'customTextfield' => '',
38            'customTextfield2' => '',
39            'appointments' => new Collection\AppointmentList(),
40            'apiclient' => new Apiclient(),
41            'authKey' => '',
42            'captchaToken' => '',
43            'clients' => new Collection\ClientList(),
44            'createIP' => '',
45            'createTimestamp' => time(),
46            'id' => 0,
47            'archiveId' => 0,
48            'queue' => new Queue(),
49            'reminderTimestamp' => 0,
50            'requests' => new Collection\RequestList(),
51            'scope' => new Scope(),
52            'status' => 'free',
53            'displayNumber' => '',
54            'dbstatus' => 'free',
55            'lastChange' => time(),
56            'wasMissed' => false,
57            'priority' => null,
58            'externalUserId' => null,
59            'parkedBy' => 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        //as current scope - see zmsdb Query/Process EntityMapping
342        return $this->toProperty()->scope->id->get();
343    }
344
345    public function getCurrentScope(): Scope
346    {
347        return $this->getProperty('scope');
348    }
349
350    public function getAmendment()
351    {
352        return $this->toProperty()->amendment->get();
353    }
354
355    /*public function getShowUpTime()
356    {
357        return $this->toProperty()->showUpTime->get();
358    }*/
359
360    public function getShowUpTime($default = 'now', $timezone = null)
361    {
362        $showUpTime = $this->toProperty()->showUpTime->get();
363        $showDateTime = Helper\DateTime::create($default, $timezone);
364        if ($showUpTime) {
365            list($hours, $minutes, $seconds) = explode(':', $showUpTime);
366            $showDateTime = $showDateTime->setTime(intval($hours), intval($minutes), intval($seconds));
367        }
368        return $showDateTime;
369    }
370
371    public function getWaitingTime()
372    {
373        return $this->toProperty()->queue->waitingTime->get();
374    }
375
376    public function getProcessingTime()
377    {
378        return $this->toProperty()->processingTime->get();
379    }
380
381    public function getFinishTime()
382    {
383        return $this->toProperty()->finishTime->get();
384    }
385
386    public function addAmendment($input, $notice = '')
387    {
388        $this->amendment = $notice;
389        $this->amendment .= (isset($input['amendment']) && $input['amendment']) ? $input['amendment'] : '';
390        $this->amendment = trim($this->amendment);
391        return $this;
392    }
393
394    public function getCustomTextfield()
395    {
396        return $this->toProperty()->customTextfield->get();
397    }
398
399    public function addCustomTextfield($input)
400    {
401        $this->customTextfield = (
402            isset($input['customTextfield']) && $input['customTextfield']
403        ) ? $input['customTextfield'] : '';
404        $this->customTextfield = trim($this->customTextfield);
405        return $this;
406    }
407
408    public function getCustomTextfield2()
409    {
410        return $this->toProperty()->customTextfield2->get();
411    }
412
413    public function addCustomTextfield2($input)
414    {
415        $this->customTextfield2 = (
416            isset($input['customTextfield2']) && $input['customTextfield2']
417        ) ? $input['customTextfield2'] : '';
418        $this->customTextfield2 = trim($this->customTextfield2);
419        return $this;
420    }
421
422    public function addPriority($input)
423    {
424        $this->priority = isset($input['priority']) && $input['priority'] ? $input['priority'] : null;
425
426        return $this;
427    }
428
429    public function getAuthKey()
430    {
431        return $this->toProperty()->authKey->get();
432    }
433
434    public function getPriority()
435    {
436        return (int) $this->toProperty()->priority->get();
437    }
438
439    public function setRandomAuthKey()
440    {
441        $this->authKey = substr(md5(rand()), 0, 4);
442    }
443
444    public function setCallTime($dateTime = null)
445    {
446        $this->queue['callTime'] = ($dateTime) ? $dateTime->getTimestamp() : 0;
447        return $this;
448    }
449
450    public function getCallTimeString()
451    {
452        return $this->getCallTime()->format('H:i:s');
453    }
454
455    public function getCallTime($default = 'now', $timezone = null)
456    {
457        $callTime = $this->toProperty()->queue->callTime->get();
458        $callDateTime = Helper\DateTime::create($default, $timezone);
459        if ($callTime) {
460            $callDateTime = $callDateTime->setTimestamp($callTime);
461        }
462        return $callDateTime;
463    }
464
465    public function getFirstClient()
466    {
467        $client = $this->getClients()->getFirst();
468        if (!$client) {
469            $client = new Client();
470            $this->clients->addEntity($client);
471        }
472        return $client;
473    }
474
475    public function getFirstAppointment(): Appointment
476    {
477        $appointment = $this->getAppointments()->getFirst();
478        if (!$appointment) {
479            $appointment = new Appointment();
480            $appointment->scope = $this->scope;
481            $this->appointments->addEntity($appointment);
482        }
483        return $appointment;
484    }
485
486    public function setStatusBySettings()
487    {
488        $scope = new Scope($this->scope);
489        if ('called' == $this->status && $this->queue['callCount'] > $scope->getPreference('queue', 'callCountMax')) {
490            $this->status = 'missed';
491        } elseif ('parked' == $this->status) {
492            $this->status = 'parked';
493        } else {
494            $this->status = 'confirmed';
495        }
496        return $this;
497    }
498
499    public function setClientsCount($count)
500    {
501        $clientList = $this->getClients();
502        while ($clientList->count() < $count) {
503            $clientList->addEntity(new Client());
504        }
505        return $this;
506    }
507
508
509    public function withoutPersonalData()
510    {
511        $entity = clone $this;
512        if ($this->toProperty()->clients->isAvailable()) {
513            $client = $entity->getFirstClient();
514            unset($client['familyName']);
515            unset($client['email']);
516        }
517        return $entity;
518    }
519
520    /**
521     * Reduce data of dereferenced entities to a required minimum
522     *
523     */
524    public function withLessData(array $keepArray = [])
525    {
526        $entity = clone $this;
527        if (!in_array('availability', $keepArray)) {
528            foreach ($entity['appointments'] as $appointment) {
529                if ($appointment->toProperty()->scope->isAvailable()) {
530                    $scopeId = $appointment['scope']['id'];
531                    $appointment['scope'] = [
532                        'id' => $scopeId,
533                        'provider' => $appointment['scope']['provider'] ?? [],
534                        'shortName' => $appointment['scope']['shortName'] ?? ''
535                    ];
536                }
537                if ($appointment->toProperty()->availability->isAvailable()) {
538                    unset($appointment['availability']);
539                }
540            }
541        }
542
543        unset($entity['createTimestamp']);
544        unset($entity['createIP']);
545        if ($entity->toProperty()->scope->status->isAvailable()) {
546            unset($entity['scope']['status']);
547        }
548
549        if ($entity->status == 'free') {
550// delete keys
551            foreach (['authKey', 'queue', 'requests',] as $key) {
552                if (!in_array($key, $keepArray) && $entity->toProperty()->$key->isAvailable()) {
553                    unset($entity[$key]);
554                }
555            }
556            // delete if empty
557            foreach (['amendment', 'id', 'authKey', 'archiveId', 'reminderTimestamp',] as $key) {
558                if (!in_array($key, $keepArray) && $entity->toProperty()->$key->isAvailable() && !$entity[$key]) {
559                    unset($entity[$key]);
560                }
561            }
562            if (!in_array('provider', $keepArray) && $entity->toProperty()->scope->provider->data->isAvailable()) {
563                unset($entity['scope']['provider']['data']);
564            }
565        }
566
567        if (!in_array('dayoff', $keepArray) && $entity->toProperty()->scope->dayoff->isAvailable()) {
568            unset($entity['scope']['dayoff']);
569        }
570        if (!in_array('scope', $keepArray) && $entity->toProperty()->scope->preferences->isAvailable()) {
571            unset($entity['scope']['preferences']);
572        }
573        return $entity;
574    }
575
576    public function toCalendar()
577    {
578        $calendar = new Calendar();
579        $dateTime = $this->getFirstAppointment()->toDateTime();
580        $day = new Day();
581        $day->setDateTime($dateTime);
582        $calendar->firstDay = $day;
583        $calendar->lastDay = $day;
584        $calendar->requests = clone $this->getRequests();
585        $calendar->scopes = new Collection\ScopeList([$this->scope]);
586        return $calendar;
587    }
588
589    public function toQueue(\DateTimeInterface $dateTime)
590    {
591        $queue = new Queue($this->queue);
592        $queue->withAppointment = ($this->getFirstAppointment()->hasTime()) ? true : false;
593        $queue->waitingTime = ($queue->waitingTime) ? $queue->waitingTime : 0;
594        $queue->wayTime = ($queue->wayTime) ? $queue->wayTime : 0;
595        if ($queue->withAppointment) {
596            $queue->number = !empty($this->getDisplayNumber())
597                ? $this->getDisplayNumber()
598                : $this->id;
599        } else {
600            $queue->number = $this->toProperty()->queue->number->get();
601        }
602        $queue->displayNumber = $this->getDisplayNumber();
603        $queue->arrivalTime = $this->getArrivalTime($dateTime)->getTimestamp();
604        $queue->priority = $this->priority;
605        return $queue->setProcess($this);
606    }
607
608    public function hasArrivalTime()
609    {
610        $arrivalTime = 0;
611        if ($this->isWithAppointment()) {
612            $arrivalTime = $this->getFirstAppointment()->date;
613        } else {
614            $arrivalTime = $this->toProperty()->queue->arrivalTime->get();
615        }
616        return ($arrivalTime) ? true : false;
617    }
618
619    public function getArrivalTime($default = 'now', $timezone = null)
620    {
621        $queueArrivalTime = $this->toProperty()->queue->arrivalTime->get();
622
623        if ($queueArrivalTime) {
624            // Falls der Queue-Wert vorhanden ist â€“ auch wenn ein Termin existiert â€“ verwende diesen (dabei ist handelt es sich, um verpasste Termine)
625            $arrivalTime = $queueArrivalTime;
626        } elseif ($this->isWithAppointment()) {
627            $arrivalTime = $this->getFirstAppointment()->date;
628        } else {
629            $arrivalTime = 0;
630        }
631
632        $arrivalTime = (int)$arrivalTime;
633        $arrivalDateTime = Helper\DateTime::create($default, $timezone);
634        if ($arrivalTime) {
635            $arrivalDateTime = $arrivalDateTime->setTimestamp($arrivalTime);
636        }
637        return $arrivalDateTime;
638    }
639
640    public function setArrivalTime(\DateTimeInterface $dateTime = null)
641    {
642        $this->queue['arrivalTime'] = ($dateTime) ? $dateTime->getTimestamp() : 0;
643        return $this;
644    }
645
646    /**
647     * Calculate real waiting time, only available after called
648     */
649    public function getWaitedSeconds($defaultTime = 'now')
650    {
651        return $this->getCallTime($defaultTime)->getTimestamp() - $this->getArrivalTime($defaultTime)->getTimestamp();
652    }
653
654    public function getWaitedMinutes($defaultTime = 'now')
655    {
656        return $this->getWaitedSeconds($defaultTime) / 60;
657    }
658
659    public function getWaySeconds($defaultTime = 'now')
660    {
661        return $this->getShowUpTime($defaultTime)->getTimestamp() - $this->getCallTime($defaultTime)->getTimestamp();
662    }
663
664    public function getWayMinutes($defaultTime = 'now')
665    {
666        return $this->getWaySeconds($defaultTime) / 60;
667    }
668
669    public function setWasMissed(bool $bool)
670    {
671        $this->wasMissed = $bool;
672        $this->status = self::STATUS_MISSED;
673        return $this;
674    }
675
676    public function getWasMissed(): bool
677    {
678        return (bool) $this->wasMissed;
679    }
680
681    public function toDerefencedAmendment()
682    {
683        $lastChange = (new \DateTimeImmutable())->setTimestamp($this->createTimestamp)->format('c');
684        return var_export(array(
685                'BuergerID' => $this->id,
686                'StandortID' => $this->scope['id'],
687                'Anmerkung' => null,
688                'IPTimeStamp' => $this->createTimestamp,
689                'LastChange' => $lastChange,
690            ), 1);
691    }
692
693    public function toDerefencedCustomTextfield()
694    {
695        $lastChange = (new \DateTimeImmutable())->setTimestamp($this->createTimestamp)->format('c');
696        return var_export(array(
697                'BuergerID' => $this->id,
698                'StandortID' => $this->scope['id'],
699                'CustomTextfield' => null,
700                'IPTimeStamp' => $this->createTimestamp,
701                'LastChange' => $lastChange,
702            ), 1);
703    }
704
705    public function toDerefencedCustomTextfield2()
706    {
707        $lastChange = (new \DateTimeImmutable())->setTimestamp($this->createTimestamp)->format('c');
708        return var_export(array(
709                'BuergerID' => $this->id,
710                'StandortID' => $this->scope['id'],
711                'CustomTextfield2' => null,
712                'IPTimeStamp' => $this->createTimestamp,
713                'LastChange' => $lastChange,
714            ), 1);
715    }
716
717    public function __toString()
718    {
719        $string = "process#";
720        $string .= $this->getDisplayNumber() ?: $this->id ?: $this->archiveId;
721        $string .= ":" . $this->authKey;
722        $string .= " (" . $this->status . ")";
723        $string .= " " . $this->getFirstAppointment()->toDateTime()->format('c');
724        $string .= " " . ($this->isWithAppointment() ? "appoint" : "arrival:" . $this->getArrivalTime()->format('c'));
725        $string .= " " . $this->getFirstAppointment()->slotCount . "slots";
726        $string .= "*" . count($this->appointments);
727        foreach ($this->getRequests() as $request) {
728            $string .= " " . $request['source'] . "." . $request['id'];
729        }
730        $string .= " scope." . $this['scope']['id'];
731        $string .= " ~" . base_convert($this['lastChange'], 10, 35);
732        $string .= " client:" . $this['apiclient']['shortname'];
733        $string .= " token:" . ($this['captchaToken'] ?? '(none)');
734        return $string;
735    }
736
737    public function getExternalUserId()
738    {
739        return $this->toProperty()->externalUserId->get();
740    }
741
742    public function setExternalUserId($externalUserId)
743    {
744        $this->externalUserId = $externalUserId;
745        return $this;
746    }
747
748    public function getParkedBy()
749    {
750        return $this->toProperty()->parkedBy->get();
751    }
752
753    public function setParkedBy($name)
754    {
755        $this->parkedBy = $name;
756    }
757}