Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.64% covered (warning)
67.64%
324 / 479
76.92% covered (warning)
76.92%
30 / 39
CRAP
0.00% covered (danger)
0.00%
0 / 1
Availability
67.64% covered (warning)
67.64%
324 / 479
76.92% covered (warning)
76.92%
30 / 39
1212.68
0.00% covered (danger)
0.00%
0 / 1
 getDefaults
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
1
 hasDate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 hasBookableDates
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 isOpenedOnDate
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
8
 overridesDayOff
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
6.03
 isOpened
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
 hasWeekDay
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 hasAppointment
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 hasTime
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getAvailableSecondsPerDay
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 hasDay
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 hasDayOff
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 hasWeek
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
10
 getStartDateTime
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getEndDateTime
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getDuration
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getBookableStart
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 getBookableEnd
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 isBookable
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
7
 hasDateBetween
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 validateStartTime
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
306
 validateWeekdays
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
110
 validateEndTime
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
12
 validateOriginEndTime
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
72
 validateType
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 validateSlotTime
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
12
 validateBookableDayRange
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getSlotList
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 getSlotTimeInMinutes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getConflict
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
 isMatchOf
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
15
 hasSharedWeekdayWith
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
9
 getTimeOverlaps
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
1 / 1
12
 withCalculatedSlots
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
 withScope
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
3
 offsetSet
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isNewerThan
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withLessData
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2
3namespace BO\Zmsentities;
4
5/**
6 * @SuppressWarnings(Complexity)
7 * @SuppressWarnings(Coupling)
8 * @SuppressWarnings(PublicMethod)
9 *
10 */
11class Availability extends Schema\Entity
12{
13    public const PRIMARY = 'id';
14
15    public static $schema = "availability.json";
16
17    /**
18     * @var array $weekday english localized weekdays to avoid problems with setlocale()
19     */
20    protected static $weekdayNameList = [
21        'sunday',
22        'monday',
23        'tuesday',
24        'wednesday',
25        'thursday',
26        'friday',
27        'saturday'
28    ];
29
30    /**
31     * Performance costs for modifying time are high, cache the calculated value
32     * @var \DateTimeImmutable $startTimeCache
33     */
34    protected $startTimeCache;
35
36    /**
37     * Performance costs for modifying time are high, cache the calculated value
38     * @var \DateTimeImmutable $endTimeCache
39     */
40    protected $endTimeCache;
41
42    /**
43     * Set Default values
44     */
45    public function getDefaults()
46    {
47        return [
48            'id' => 0,
49            'weekday' => array_fill_keys(self::$weekdayNameList, 0),
50            'repeat' => [
51                'afterWeeks' => 1,
52                'weekOfMonth' => 0,
53            ],
54            'bookable' => [
55                'startInDays' => 1,
56                'endInDays' => 60,
57            ],
58            'workstationCount' => [
59                'public' => 0,
60                'intern' => 0,
61            ],
62            'lastChange' => 0,
63            'multipleSlotsAllowed' => true,
64            'slotTimeInMinutes' => 10,
65            'startDate' => 0,
66            'endDate' => 0,
67            'startTime' => '0:00',
68            'endTime' => '23:59',
69            'type' => 'appointment',
70            'version' => 1,
71            'scope' => [
72                'id' => 123,
73                'provider' => [
74                    'id' => 0,
75                    'name' => '',
76                    'source' => ''
77                ],
78                'shortName' => ''
79            ]
80        ];
81    }
82
83    /**
84     * Check, if the dateTime contains a day given by the settings
85     * ATTENTION: Time critical function, keep highly optimized
86     * Compared to isOpened() the Booking time is checked too
87     *
88     * @param \DateTimeInterface $dateTime
89     *
90     * @return Bool
91     */
92    public function hasDate(\DateTimeInterface $dateTime, \DateTimeInterface $now)
93    {
94        $dateTime = Helper\DateTime::create($dateTime);
95        if (
96            !$this->isOpenedOnDate($dateTime)
97            || !$this->isBookable($dateTime, $now)
98        ) {
99            // Out of date range
100            return false;
101        }
102        return true;
103    }
104
105    public function hasBookableDates(\DateTimeInterface $now)
106    {
107        if ($this->workstationCount['intern'] <= 0) {
108            return false;
109        }
110        if ($this->getEndDateTime()->getTimestamp() < $now->getTimestamp()) {
111            return false;
112        }
113        $stopDate = $this->getBookableEnd($now);
114        if ($this->getStartDateTime()->getTimestamp() > $stopDate->getTimestamp()) {
115            return false;
116        }
117        return $this->hasDateBetween($this->getBookableStart($now), $this->getBookableEnd($now), $now);
118    }
119
120    /**
121     * Check, if the dateTime contains a day
122     * ATTENTION: Time critical function, keep highly optimized
123     *
124     * @param \DateTimeInterface $dateTime
125     * @param String $type of "openinghours", "appointment" or false to ignore type
126     *
127     * @return Bool
128     */
129    public function isOpenedOnDate(\DateTimeInterface $dateTime, $type = false)
130    {
131        $dateTime = Helper\DateTime::create($dateTime);
132        if (
133            !$this->hasWeekDay($dateTime)
134            || ($type !== false && $this->type != $type)
135            || !$this->hasDay($dateTime)
136            || !$this->hasWeek($dateTime)
137            || ($this->getDuration() > 1 && $this->hasDayOff($dateTime))
138        ) {
139            // Out of date range
140            return false;
141        }
142        return true;
143    }
144
145    public function overridesDayOff(): bool
146    {
147        if ($this->getDuration() >= 2) {
148            return false;
149        }
150
151        $start = $this->getStartDateTime()->modify('00:00:00');
152        $end   = $this->getEndDateTime()->modify('23:59:59');
153        $current = clone $start;
154
155        while ($current <= $end) {
156            try {
157                if ($this->hasWeekDay($current) && $this->hasDayOff($current)) {
158                    return true;
159                }
160            } catch (Exception\DayoffMissing $e) {
161                // Scope doesn't have dayoff data, skip this day
162            }
163            $current = $current->modify('+1 day');
164        }
165        return false;
166    }
167
168    /**
169     * Check if date and time is in availability
170     * Compared to hasDate() the time of the day is checked, but not booking time
171     *
172     * @param \DateTimeInterface $dateTime
173     * @param String $type of "openinghours", "appointment" or false to ignore type
174     *
175     */
176    public function isOpened(\DateTimeInterface $dateTime, $type = false)
177    {
178        return (!$this->isOpenedOnDate($dateTime, $type) || !$this->hasTime($dateTime)) ? false : true;
179    }
180
181    public function hasWeekDay(\DateTimeInterface $dateTime)
182    {
183        $weekDayName = self::$weekdayNameList[$dateTime->format('w')];
184        if (!$this['weekday'][$weekDayName]) {
185            // Wrong weekday
186            return false;
187        }
188        return true;
189    }
190
191    public function hasAppointment(Appointment $appointment)
192    {
193        $dateTime = $appointment->toDateTime();
194        $isOpenedStart = $this->isOpened($dateTime, false);
195        $duration = $this->slotTimeInMinutes * $appointment->slotCount;
196        $endTime = $dateTime->modify("+" . $duration . "minutes")
197            ->modify("-1 second"); // To allow the last slot for an appointment
198        $isOpenedEnd = $this->isOpened($endTime, false);
199        return ($isOpenedStart && $isOpenedEnd);
200    }
201
202    /**
203     * Check, if the dateTime is a time covered by availability
204     *
205     * @param \DateTimeInterface $dateTime
206     *
207     * @return Bool
208     */
209    public function hasTime(\DateTimeInterface $dateTime)
210    {
211        $start = $this->getStartDateTime()->getSecondsOfDay();
212        $end = $this->getEndDateTime()->getSecondsOfDay();
213        $compare = Helper\DateTime::create($dateTime)->getSecondsOfDay();
214        if ($start > $compare || $end <= $compare) {
215            // Out of time range
216            return false;
217        }
218        return true;
219    }
220
221    public function getAvailableSecondsPerDay($type = "intern")
222    {
223        $start = $this->getStartDateTime()->getSecondsOfDay();
224        $end = $this->getEndDateTime()->getSecondsOfDay();
225        return ($end - $start) * $this->workstationCount[$type];
226    }
227
228    /**
229     * Check, if the dateTime is a day covered by availability
230     *
231     * @param \DateTimeInterface $dateTime
232     *
233     * @return Bool
234     */
235    public function hasDay(\DateTimeInterface $dateTime)
236    {
237        $start = $this->getStartDateTime()->modify('0:00:00');
238        $end = $this->getEndDateTime()->modify('23:59:59');
239        if ($dateTime->getTimestamp() < $start->getTimestamp() || $dateTime->getTimestamp() > $end->getTimestamp()) {
240            // Out of date range
241            return false;
242        }
243        return true;
244    }
245
246    /**
247     * Check, if the dateTime is a dayoff date
248     *
249     * @param \DateTimeInterface $dateTime
250     *
251     * @return Bool
252     */
253    public function hasDayOff(\DateTimeInterface $dateTime)
254    {
255        if (isset($this['scope']['dayoff'])) {
256            $timeStamp = $dateTime->format('Y-m-d');
257            foreach ($this['scope']['dayoff'] as $dayOff) {
258                if (date('Y-m-d', $dayOff['date']) == $timeStamp) {
259                    return true;
260                }
261            }
262        } else {
263            throw new Exception\DayoffMissing();
264        }
265        return false;
266    }
267
268    /**
269     * Check, if the dateTime contains a week given by the week repetition settings
270     *
271     * @param \DateTimeInterface $dateTime
272     *
273     * @return Bool
274     */
275    public function hasWeek(\DateTimeInterface $dateTime)
276    {
277        $dateTime = Helper\DateTime::create($dateTime);
278        $start = $this->getStartDateTime();
279        $monday = "monday this week";
280        if (
281            $this['repeat']['afterWeeks']
282            && ($this['repeat']['afterWeeks'] == 1
283                || 0 ===
284                    $dateTime->modify($monday)->diff($start->modify($monday))->days
285                 % ($this['repeat']['afterWeeks'] * 7)
286            )
287        ) {
288            return true;
289        }
290        if (
291            $this['repeat']['weekOfMonth']
292            && (
293                $dateTime->isWeekOfMonth($this['repeat']['weekOfMonth'])
294                // On a value of 5, always take the last week
295                || ($this['repeat']['weekOfMonth'] >= 5 && $dateTime->isLastWeekOfMonth())
296            )
297        ) {
298            return true;
299        }
300        if (!$this['repeat']['weekOfMonth'] && !$this['repeat']['afterWeeks']) {
301            return true;
302        }
303        return false;
304    }
305
306    /**
307     * Get DateTimeInterface for start time of availability
308     *
309     * @return \DateTimeInterface
310     */
311    public function getStartDateTime()
312    {
313        if (!$this->startTimeCache) {
314            $this->startTimeCache = Helper\DateTime::create()
315                ->setTimestamp($this['startDate'])
316                ->modify('today ' .  $this['startTime']);
317        }
318        return $this->startTimeCache;
319    }
320
321    /**
322     * Get DateTimeInterface for end time of availability
323     *
324     * @return \DateTimeInterface
325     */
326    public function getEndDateTime()
327    {
328        if (!$this->endTimeCache) {
329            $this->endTimeCache = Helper\DateTime::create()
330                ->setTimestamp($this['endDate'])
331                ->modify('today ' .  $this['endTime']);
332        }
333        return $this->endTimeCache;
334    }
335
336    /**
337     * Get duration of availability
338     *
339     * @return integer
340     */
341    public function getDuration()
342    {
343        $startTime = $this->getStartDateTime();
344        $endTime = $this->getEndDateTime();
345        return (int)$endTime->diff($startTime)->format("%a");
346    }
347
348    /**
349     * Get DateTimeInterface for start booking time of availability
350     *
351     * @param \DateTimeInterface $now relative time to compare booking settings
352     *
353     * @return \DateTimeInterface
354     */
355    public function getBookableStart(\DateTimeInterface $now)
356    {
357        $now = Helper\DateTime::create($now);
358        $availabilityStart = Helper\Property::create($this)->bookable->startInDays->get();
359        $time = $this->getStartDateTime()->format('H:i:s');
360        if (null !== $availabilityStart) {
361            return $now->modify('+' . $availabilityStart . 'days')->modify($time);
362        }
363        $scopeStart = Helper\Property::create($this)->scope->preferences->appointment->startInDaysDefault->get();
364        if (null !== $scopeStart) {
365            return $now->modify('+' . $scopeStart . 'days')->modify($time);
366        }
367        throw new \BO\Zmsentities\Exception\ProcessBookableFailed(
368            "Undefined start time for booking, try to set the scope properly"
369        );
370    }
371
372    /**
373     * Get DateTimeInterface for end booking time of availability
374     *
375     * @param \DateTimeInterface $now relative time to compare booking settings
376     *
377     * @return \DateTimeInterface
378     */
379    public function getBookableEnd(\DateTimeInterface $now)
380    {
381        $now = Helper\DateTime::create($now);
382        $availabilityEnd = Helper\Property::create($this)->bookable->endInDays->get();
383        $time = $this->getEndDateTime()->format('H:i:s');
384        if (null !== $availabilityEnd) {
385            return $now->modify('+' . $availabilityEnd . 'days')->modify($time);
386        }
387        $scopeEnd = Helper\Property::create($this)->scope->preferences->appointment->endInDaysDefault->get();
388        if (null !== $scopeEnd) {
389            return $now->modify('+' . $scopeEnd . 'days')->modify($time);
390        }
391        throw new \BO\Zmsentities\Exception\ProcessBookableFailed(
392            "Undefined end time for booking, try to set the scope properly"
393        );
394    }
395
396    /**
397     * Check, if the dateTime contains is within the bookable range (usually for public access)
398     * The current time is used to compare the start Time of the availability
399     *
400     * @param \DateTimeInterface $dateTime
401     * @param \DateTimeInterface $now relative time to compare booking settings
402     *
403     * @return Bool
404     */
405    public function isBookable(\DateTimeInterface $bookableDate, \DateTimeInterface $now)
406    {
407        if (!$this->hasDay($bookableDate)) {
408            return false;
409        }
410        $bookableCurrentTime = $bookableDate->modify($now->format('H:i:s'));
411        Helper\DateTime::create($bookableDate)->getTimestamp() + Helper\DateTime::create($now)->getSecondsOfDay();
412        $startDate = $this->getBookableStart($now)->modify('00:00:00');
413
414        if ($bookableCurrentTime->getTimestamp() < $startDate->getTimestamp()) {
415            //error_log("START " . $bookableCurrentTime->format('c').'<'.$startDate->format('c'). " " . $this);
416            return false;
417        }
418        $endDate = $this->getBookableEnd($now)->modify('23:59:59');
419        if ($bookableCurrentTime->getTimestamp() > $endDate->getTimestamp()) {
420            //error_log("END " . $bookableCurrentTime->format('c').'>'.$endDate->format('c'). " " . $this);
421            return false;
422        }
423        if (
424            $bookableDate->format('Y-m-d') == $endDate->format('Y-m-d')
425            && $now->format('Y-m-d') != $this->getEndDateTime()->format('Y-m-d')
426        ) {
427            // Avoid releasing all appointments on midnight, allow smaller contingents distributed over the day
428            $delayedStart = $this->getBookableEnd($now)->modify($this->getStartDateTime()->format('H:i:s'));
429            if ($bookableCurrentTime->getTimestamp() < $delayedStart->getTimestamp()) {
430                //error_log(
431                //    sprintf("DELAY %s<%s", $bookableCurrentTime->format('c'), $delayedStart->format('c'))
432                //    ." $this"
433                //);
434                return false;
435            }
436        }
437        return true;
438    }
439
440    /**
441     * Check, if a day between two dates is included
442     *
443     * @return Array of arrays with the keys time, public, intern
444     */
445    public function hasDateBetween(\DateTimeInterface $startTime, \DateTimeInterface $stopTime, \DateTimeInterface $now): bool
446    {
447        if ($startTime->getTimestamp() < $now->getTimestamp()) {
448            $startTime = $now;
449        }
450        if ($stopTime->getTimestamp() < $now->getTimestamp()) {
451            return false;
452        }
453        do {
454            if ($this->hasDate($startTime, $now)) {
455                return true;
456            }
457            $startTime = $startTime->modify('+1 day');
458        } while ($startTime->getTimestamp() <= $stopTime->getTimestamp());
459        return false;
460    }
461
462    public function validateStartTime(\DateTimeInterface $today, \DateTimeInterface $tomorrow, \DateTimeInterface $startDate, \DateTimeInterface $endDate, \DateTimeInterface $selectedDate, string $kind): array
463    {
464        $errorList = [];
465
466        $startTime = (clone $startDate)->setTime(0, 0);
467        $startHour = (int) $startDate->format('H');
468        $endHour = (int) $endDate->format('H');
469        $startMinute = (int) $startDate->format('i');
470        $endMinute = (int) $endDate->format('i');
471        $isFuture = ($kind && $kind === 'future');
472
473        if (
474            !$isFuture &&
475            $selectedDate->getTimestamp() > $today->getTimestamp() &&
476            $startTime->getTimestamp() > (clone $selectedDate)->setTime(0, 0)->getTimestamp()
477        ) {
478            $errorList[] = [
479                'type' => 'startTimeFuture',
480                'message' => "Das Startdatum der Ã–ffnungszeit muss vor dem " . $tomorrow->format('d.m.Y') . " liegen."
481            ];
482        }
483
484        if (
485            ($startHour === 22 && $startMinute > 0) ||
486            $startHour === 23 ||
487            $startHour === 0 ||
488            ($endHour === 22 && $endMinute > 0) ||
489            $endHour === 23 ||
490            $endHour === 0 ||
491            ($startHour === 1 && $startMinute > 0) ||
492            ($endHour === 1 && $endMinute > 0)
493        ) {
494            $errorList[] = [
495                'type' => 'startOfDay',
496                'message' => 'Die Uhrzeit darf nicht zwischen 22:00 und 01:00 liegen, da in diesem Zeitraum der tägliche Cronjob ausgeführt wird.'
497            ];
498        }
499
500        return $errorList;
501    }
502
503    public function validateWeekdays(\DateTimeInterface $startDate, \DateTimeInterface $endDate, array $weekday, string $kind): array
504    {
505        $errorList = [];
506
507        // Skip validation if this is part of a split series
508        if ($kind === 'origin' || $kind === 'future') {
509            return $errorList;
510        }
511
512        if ($startDate > $endDate) {
513            return $errorList;
514        }
515
516        $hasSelectedDay = false;
517        foreach (self::$weekdayNameList as $day) {
518            if ((int)$weekday[$day] > 0) {
519                $hasSelectedDay = true;
520                break;
521            }
522        }
523
524        if (!$hasSelectedDay) {
525            $errorList[] = [
526                'type' => 'weekdayRequired',
527                'message' => 'Mindestens ein Wochentag muss ausgewählt sein.'
528            ];
529            return $errorList;
530        }
531
532        $germanWeekdays = [
533            'sunday' => 'Sonntag',
534            'monday' => 'Montag',
535            'tuesday' => 'Dienstag',
536            'wednesday' => 'Mittwoch',
537            'thursday' => 'Donnerstag',
538            'friday' => 'Freitag',
539            'saturday' => 'Samstag'
540        ];
541
542        $selectedWeekdays = array_filter(self::$weekdayNameList, function ($day) use ($weekday) {
543            return (int)$weekday[$day] > 0;
544        });
545        $foundWeekdays = [];
546
547        $currentDate = clone $startDate;
548        while ($currentDate <= $endDate) {
549            $weekDayName = self::$weekdayNameList[$currentDate->format('w')];
550            if (in_array($weekDayName, $selectedWeekdays)) {
551                $foundWeekdays[] = $weekDayName;
552            }
553            $currentDate = $currentDate->modify('+1 day');
554        }
555
556        $missingWeekdays = array_diff($selectedWeekdays, array_unique($foundWeekdays));
557        if (!empty($missingWeekdays)) {
558            $germanMissingWeekdays = array_map(function ($day) use ($germanWeekdays) {
559                return $germanWeekdays[$day];
560            }, $missingWeekdays);
561
562            $errorList[] = [
563                'type' => 'invalidWeekday',
564                'message' => sprintf(
565                    'Die ausgewählten Wochentage (%s) kommen im gewählten Zeitraum nicht vor.',
566                    implode(', ', $germanMissingWeekdays)
567                )
568            ];
569        }
570
571        return $errorList;
572    }
573
574    public function validateEndTime(\DateTimeInterface $startDate, \DateTimeInterface $endDate): array
575    {
576        $errorList = [];
577
578        $startHour = (int) $startDate->format('H');
579        $endHour = (int) $endDate->format('H');
580        $startMinute = (int) $startDate->format('i');
581        $endMinute = (int) $endDate->format('i');
582        $dayMinutesStart = ($startHour * 60) + $startMinute;
583        $dayMinutesEnd = ($endHour * 60) + $endMinute;
584        $startTimestamp = $startDate->getTimestamp();
585        $endTimestamp = $endDate->getTimestamp();
586
587        if ($dayMinutesEnd <= $dayMinutesStart) {
588            $errorList[] = [
589                'type' => 'endTime',
590                'message' => 'Die Endzeit darf nicht vor der Startzeit liegen.'
591            ];
592        } elseif ($startTimestamp >= $endTimestamp) {
593            $errorList[] = [
594                'type' => 'endTime',
595                'message' => 'Das Enddatum darf nicht vor dem Startdatum liegen.'
596            ];
597        }
598
599        return $errorList;
600    }
601
602    public function validateOriginEndTime(\DateTimeInterface $today, \DateTimeInterface $yesterday, \DateTimeInterface $endDate, \DateTimeInterface $selectedDate, string $kind): array
603    {
604        $errorList = [];
605        $endHour = (int) $endDate->format('H');
606        $endMinute = (int) $endDate->format('i');
607        $startDate = $this->getStartDateTime();
608        $startHour = (int) $startDate->format('H');
609        $startMinute = (int) $startDate->format('i');
610        $endDateTime = (clone $endDate)->setTime($endHour, $endMinute);
611        $startDateTime = (clone $startDate)->setTime($startHour, $startMinute);
612        $endTimestamp = $endDateTime->getTimestamp();
613        $startTimestamp = $startDateTime->getTimestamp();
614        $isOrigin = ($kind && $kind === 'origin');
615
616        if (!$isOrigin && $selectedDate->getTimestamp() > $today->getTimestamp() && $endDate < (clone $selectedDate)->setTime(0, 0)) {
617            $errorList[] = [
618                'type' => 'endTimeFuture',
619                'message' => "Das Enddatum der Ã–ffnungszeit muss nach dem " . $yesterday->format('d.m.Y') . " liegen."
620            ];
621        }
622
623        if (!$isOrigin && $startTimestamp < $today->getTimestamp() && $endTimestamp < $today->getTimestamp()) {
624            $errorList[] = [
625                'type' => 'endTimePast',
626                'message' => 'Öffnungszeiten in der Vergangenheit lassen sich nicht bearbeiten '
627                    . '(Die aktuelle Zeit "' . $today->format('d.m.Y H:i') . ' Uhr" liegt nach dem Terminende am "'
628                    . $endDateTime->format('d.m.Y H:i') . ' Uhr" und dem Terminanfang am "'
629                    . $startDateTime->format('d.m.Y H:i') . ' Uhr").'
630            ];
631        }
632
633        return $errorList;
634    }
635
636    public function validateType(string $kind): array
637    {
638        $errorList = [];
639        if (empty($kind)) {
640            $errorList[] = [
641                'type' => 'type',
642                'message' => 'Typ erforderlich'
643            ];
644        }
645        return $errorList;
646    }
647
648    public function validateSlotTime(\DateTimeInterface $startDate, \DateTimeInterface $endDate): array
649    {
650        $errorList = [];
651        $slotTime = $this['slotTimeInMinutes'];
652
653        $startHour = (int)$startDate->format('H');
654        $startMinute = (int)$startDate->format('i');
655        $endHour = (int)$endDate->format('H');
656        $endMinute = (int)$endDate->format('i');
657
658        $totalMinutes = (($endHour - $startHour) * 60) + ($endMinute - $startMinute);
659
660        if ($slotTime === 0) {
661            $errorList[] = [
662                'type' => 'slotTime',
663                'message' => 'Die Slot-Zeit darf nicht 0 sein.'
664            ];
665            return $errorList;
666        }
667
668        if ($totalMinutes % $slotTime > 0) {
669            $errorList[] = [
670                'type' => 'slotCount',
671                'message' => 'Zeitschlitze müssen sich gleichmäßig in der Ã–ffnungszeit aufteilen lassen.'
672            ];
673        }
674
675        return $errorList;
676    }
677
678    public function validateBookableDayRange(int $startInDays, int $endInDays): array
679    {
680        $errorList = [];
681        if ($startInDays > $endInDays) {
682            $errorList[] = [
683                'type' => 'bookableDayRange',
684                'message' => 'Bitte geben Sie im Feld \'von\' eine kleinere Zahl ein als im Feld \'bis\', wenn Sie bei \'Buchbar\' sind.'
685            ];
686        }
687
688        return $errorList;
689    }
690
691    /**
692     * Creates a list of slots available on a valid day
693     *
694     * @return Array of arrays with the keys time, public, intern
695     */
696    public function getSlotList()
697    {
698        $startTime = Helper\DateTime::create($this['startTime']);
699        $stopTime = Helper\DateTime::create($this['endTime']);
700        $slotList = new Collection\SlotList();
701        $slotInstance = new Slot($this['workstationCount']);
702        if ($this['slotTimeInMinutes'] > 0) {
703            do {
704                $slot = clone $slotInstance;
705                $slot->setTime($startTime);
706                $slotList[] = $slot;
707                $startTime = $startTime->modify('+' . $this['slotTimeInMinutes'] . 'minute');
708                // Only add a slot, if at least a minute is left, otherwise do not ("<" instead "<=")
709            } while ($startTime->getTimestamp() < $stopTime->getTimestamp());
710        }
711        return $slotList;
712    }
713
714    public function getSlotTimeInMinutes()
715    {
716        return $this['slotTimeInMinutes'];
717    }
718
719    /**
720     * Get problems on configuration of this availability
721     *
722     * @return Collection\ProcessList with processes in status "conflict"
723     */
724    public function getConflict()
725    {
726        $start = $this->getStartDateTime()->getSecondsOfDay();
727        $end = $this->getEndDateTime()->getSecondsOfDay();
728        $minutesPerDay = floor(($end - $start) / 60);
729        if ($minutesPerDay % $this->slotTimeInMinutes > 0) {
730            $conflict = new Process();
731            $conflict->status = 'conflict';
732            $appointment = $conflict->getFirstAppointment();
733            $appointment->availability = $this;
734            $appointment->date = $this->getStartDateTime()->getTimestamp();
735            $conflict->amendment =
736                "Der eingestellte Zeitschlitz von {$this->slotTimeInMinutes} Minuten"
737                . " sollte in die eingestellte Uhrzeit passen.";
738            return $conflict;
739        }
740        return false;
741    }
742
743    /**
744     * Check of a different availability has the same opening configuration
745     *
746     */
747    public function isMatchOf(Availability $availability)
748    {
749        return ($this->type != $availability->type
750            || $this->startTime != $availability->startTime
751            || $this->endTime != $availability->endTime
752            || $this->startDate != $availability->startDate
753            || $this->endDate != $availability->endDate
754            || $this->repeat['afterWeeks'] != $availability->repeat['afterWeeks']
755            || $this->repeat['weekOfMonth'] != $availability->repeat['weekOfMonth']
756            || (bool)$this->weekday['monday'] != (bool)$availability->weekday['monday']
757            || (bool)$this->weekday['tuesday'] != (bool)$availability->weekday['tuesday']
758            || (bool)$this->weekday['wednesday'] != (bool)$availability->weekday['wednesday']
759            || (bool)$this->weekday['thursday'] != (bool)$availability->weekday['thursday']
760            || (bool)$this->weekday['friday'] != (bool)$availability->weekday['friday']
761            || (bool)$this->weekday['saturday'] != (bool)$availability->weekday['saturday']
762            || (bool)$this->weekday['sunday'] != (bool)$availability->weekday['sunday']
763        ) ? false : true;
764    }
765
766    public function hasSharedWeekdayWith(Availability $availability)
767    {
768        return ($this->type == $availability->type
769            && (bool)$this->weekday['monday'] != (bool)$availability->weekday['monday']
770            && (bool)$this->weekday['tuesday'] != (bool)$availability->weekday['tuesday']
771            && (bool)$this->weekday['wednesday'] != (bool)$availability->weekday['wednesday']
772            && (bool)$this->weekday['thursday'] != (bool)$availability->weekday['thursday']
773            && (bool)$this->weekday['friday'] != (bool)$availability->weekday['friday']
774            && (bool)$this->weekday['saturday'] != (bool)$availability->weekday['saturday']
775            && (bool)$this->weekday['sunday'] != (bool)$availability->weekday['sunday']
776        ) ? false : true;
777    }
778
779    /**
780     * Get overlaps on daytime
781     * This functions does not check, if two availabilities are openend on the same day!
782     *
783     * @param Availability $availability for comparision
784     *
785     * @return Collection\ProcessList with processes in status "conflict"
786     *
787     *
788     */
789
790    /*
791    1
792    Case 01:  |-----|
793              |-----|
794                 2
795
796                 1
797    Case 02:  |-----|
798                 |-----|
799                    2
800
801                    1
802    Case 03:     |-----|
803              |-----|
804                 2
805
806                   1
807    Case 04:  |---------|
808                |-----|
809                   2
810
811                   1
812    Case 05:    |-----|
813              |---------|
814                   2
815
816                 1
817    Case 06:  |-----|
818                      |-----|
819                         2
820
821                         1
822    Case 07:          |-----|
823              |-----|
824                 2
825
826                 1
827    Case 08:  |-----|
828                    |-----|
829                       2
830
831                       1
832    Case 09:        |-----|
833              |-----|
834                 2
835
836                 1
837    Case 10:     |
838              |-----|
839                 2
840
841                 1
842    Case 11:  |-----|
843                 |
844                 2
845
846              1
847    Case 12:  |
848              |-----|
849                 2
850
851                    1
852    Case 13:        |
853              |-----|
854                 2
855
856                 1
857    Case 14:  |-----|
858              |
859              2
860
861                 1
862    Case 15:  |-----|
863                    |
864                    2
865
866              1
867    Case 16:  |
868              |
869              2
870
871            |                         |    Operlap    |     Overlap
872      Case  |         Example         | Open Interval | Closed Interval
873    --------|-------------------------|---------------|-----------------
874    Case 01 | 09:00-11:00 09:00-11:00 |      Yes      |        Yes
875    Case 02 | 09:00-11:00 10:00-12:00 |      Yes      |        Yes
876    Case 03 | 10:00-12:00 09:00-11:00 |      Yes      |        Yes
877    Case 04 | 09:00-12:00 10:00-11:00 |      Yes      |        Yes
878    Case 05 | 10:00-11:00 09:00-12:00 |      Yes      |        Yes
879    Case 06 | 09:00-10:00 11:00-12:00 |      No       |        No
880    Case 07 | 11:00-12:00 09:00-10:00 |      No       |        No
881    Case 08 | 09:00-10:00 10:00-11:00 |      No       |        Yes
882    Case 09 | 10:00-11:00 09:00-10:00 |      No       |        Yes
883    Case 10 | 10:00-10:00 09:00-11:00 |      Yes      |        Yes
884    Case 11 | 09:00-11:00 10:00-10:00 |      Yes      |        Yes
885    Case 12 | 09:00-09:00 09:00-10:00 |      No       |        Yes
886    Case 13 | 10:00-10:00 09:00-10:00 |      No       |        Yes
887    Case 14 | 09:00-10:00 09:00-09:00 |      No       |        Yes
888    Case 15 | 09:00-10:00 10:00-10:00 |      No       |        Yes
889    Case 16 | 09:00-09:00 09:00-09:00 |      No       |        Yes
890    */
891
892    public function getTimeOverlaps(Availability $availability, \DateTimeInterface $currentDate)
893    {
894        $processList = new Collection\ProcessList();
895        if (
896            $availability->id != $this->id
897            && $availability->type == $this->type
898            && $this->hasSharedWeekdayWith($availability)
899        ) {
900            $processTemplate = new Process();
901            $processTemplate->amendment = "Zwei Ã–ffnungszeiten Ã¼berschneiden sich.";
902            $processTemplate->status = 'conflict';
903            $appointment = $processTemplate->getFirstAppointment();
904            $appointment->availability = $this;
905            $appointment->date = $this->getStartDateTime()->getTimestamp();
906            $thisStart = $this->getStartDateTime()->getSecondsOfDay();
907            $thisEnd = $this->getEndDateTime()->getSecondsOfDay();
908            $availabilityStart = $availability->getStartDateTime()->getSecondsOfDay();
909            $availabilityEnd = $availability->getEndDateTime()->getSecondsOfDay();
910
911            $isEqual = ($availabilityStart == $thisStart && $availabilityEnd == $thisEnd);
912
913            if ($availabilityStart < $thisEnd && $thisStart < $availabilityEnd && ! $isEqual) {
914                $process = clone $processTemplate;
915                $process->getFirstAppointment()->date = $this
916                    ->getStartDateTime()
917                    ->modify($currentDate->format("Y-m-d"))
918                    ->getTimestamp();
919                $processList->addEntity($process);
920            } elseif ($thisEnd < $availabilityStart && $availabilityEnd < $thisStart && ! $isEqual) {
921                $process = clone $processTemplate;
922                $process->getFirstAppointment()->date = $availability
923                    ->getStartDateTime()
924                    ->modify($currentDate->format("Y-m-d"))
925                    ->getTimestamp();
926                $processList->addEntity($process);
927            } elseif ($isEqual) {
928                $process = clone $processTemplate;
929                $process->amendment = "Zwei Ã–ffnungszeiten sind gleich.";
930                $process->getFirstAppointment()->date = $availability
931                    ->getStartDateTime()
932                    ->modify($currentDate->format("Y-m-d"))
933                    ->getTimestamp();
934                $processList->addEntity($process);
935            }
936        }
937        return $processList;
938    }
939
940    /**
941     * Update workstationCount to number of calculated appointments
942     *
943     * @return self cloned
944     */
945    public function withCalculatedSlots()
946    {
947        $availability = clone $this;
948        $startTime = Helper\DateTime::create($this['startTime']);
949        $stopTime = Helper\DateTime::create($this['endTime']);
950        $openingSeconds = $stopTime->getTimestamp() - $startTime->getTimestamp();
951        $openingMinutes = floor($openingSeconds / 60);
952        $slices = 0;
953        if ($this['slotTimeInMinutes'] > 0) {
954            $slices = floor($openingMinutes / $this['slotTimeInMinutes']);
955        }
956        $slot = new Slot([
957            'type' => Slot::FREE,
958            'intern' => $this['workstationCount']['intern'] * $slices,
959            'public' => $this['workstationCount']['public'] * $slices,
960        ]);
961        $availability['workstationCount'] = $slot;
962        return $availability;
963    }
964
965    public function withScope(\BO\Zmsentities\Scope $scope)
966    {
967        $availability = clone $this;
968        $availability->scope = $scope;
969        return $availability;
970    }
971
972    public function __toString()
973    {
974        $info = "Availability." . $this['type'] . " #" . $this['id'];
975        $info .= " starting " . $this->startDate . $this->getStartDateTime()->format(' Y-m-d');
976        $info .= "||now+" . $this['bookable']['startInDays'] . " ";
977        $info .= " until " . $this->getEndDateTime()->format('Y-m-d');
978        $info .= "||now+" . $this['bookable']['endInDays'] . " ";
979        if ($this['repeat']['afterWeeks']) {
980            $info .= " every " . $this['repeat']['afterWeeks'] . " week(s)";
981        }
982        if ($this['repeat']['weekOfMonth']) {
983            $info .= " each " . $this['repeat']['weekOfMonth'] . ". weekOfMonth";
984        }
985        $info .= " on ";
986        $weekdays = array_filter($this['weekday'], function ($value) {
987            return $value > 0;
988        });
989        $info .= implode(',', array_keys($weekdays));
990        $info .= " from " . $this->getStartDateTime()->format('H:i');
991        $info .= " to " . $this->getEndDateTime()->format('H:i');
992        $info .= " using " . $this['slotTimeInMinutes'] . "min slots";
993        $info .= " with p{$this['workstationCount']['public']}/";
994        $info .= "i{$this['workstationCount']['intern']}";
995        $day = $this->getSlotList()->getSummerizedSlot();
996        $info .= " day $day";
997        return $info;
998    }
999
1000    /**
1001     * Delete cache on changes
1002     *
1003     */
1004    public function offsetSet(mixed $index, mixed $value): void
1005    {
1006        $this->startTimeCache = null;
1007        $this->endTimeCache = null;
1008        parent::offsetSet($index, $value);
1009    }
1010
1011    /**
1012     * Check if availability is newer than given time
1013     *
1014     * @return bool
1015     */
1016    public function isNewerThan(\DateTimeInterface $dateTime)
1017    {
1018        return ($dateTime->getTimestamp() < $this->lastChange);
1019    }
1020
1021    /**
1022     * Reduce data of dereferenced entities to a required minimum
1023     *
1024     */
1025    public function withLessData(array $keepArray = [])
1026    {
1027        $entity = clone $this;
1028        if (! in_array('repeat', $keepArray)) {
1029            unset($entity['repeat']);
1030        }
1031        if (! in_array('id', $keepArray)) {
1032            unset($entity['id']);
1033        }
1034        if (! in_array('bookable', $keepArray)) {
1035            unset($entity['bookable']);
1036        }
1037        if (! in_array('workstationCount', $keepArray)) {
1038            unset($entity['workstationCount']);
1039        }
1040        if (! in_array('multipleSlotsAllowed', $keepArray)) {
1041            unset($entity['multipleSlotsAllowed']);
1042        }
1043        if (! in_array('lastChange', $keepArray)) {
1044            unset($entity['lastChange']);
1045        }
1046        if (! in_array('slotTimeInMinutes', $keepArray)) {
1047            unset($entity['slotTimeInMinutes']);
1048        }
1049        if (! in_array('description', $keepArray)) {
1050            unset($entity['description']);
1051        }
1052
1053        return $entity;
1054    }
1055}