Lines 98.25% 169 / 172
Functions and Methods 57.14% 4 / 7
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ExchangeWaitingscope 98.25% 169 / 172 57.14% 4 / 7 20 0.00% 0 / 1
 readEntity 100.00% 44 / 44 100.00% 1 / 1 3
 readSubjectList 100.00% 11 / 11 100.00% 1 / 1 2
 readPeriodList 100.00% 22 / 22 100.00% 1 / 1 4
 readByDateTime 100.00% 19 / 19 100.00% 1 / 1 2
 writeWaitingTimeCalculated 95.23% 20 / 21 0.00% 0 / 1 3
 writeWaitingTime 96.29% 26 / 27 0.00% 0 / 1 4
 updateWaitingStatistics 96.42% 27 / 28 0.00% 0 / 1 2
1<?php
2
3namespace BO\Zmsbackend\Exchange\Service;
4
5use BO\Zmsentities\Exchange;
6
7class ExchangeWaitingscope extends \BO\Zmsbackend\Base implements \BO\Zmsbackend\Interfaces\ExchangeSubject
8{
9    /**
10     * @return Exchange
11     */
12    #[\Override]
13    public function readEntity(
14        string|int $subjectid,
15        \DateTimeInterface $datestart,
16        \DateTimeInterface $dateend,
17        string $period = 'day'
18    ) {
19        $scope = (new \BO\Zmsbackend\Scope\Service\Scope())->readEntity($subjectid);
20        $entity = new Exchange();
21        $entity['title'] = "Wartestatistik " . $scope->contact->name . " " . $scope->shortName;
22        $entity->setPeriod($datestart, $dateend, $period);
23        $entity->addDictionaryEntry('subjectid', 'string', 'ID of a scope', 'scope.id');
24        $entity->addDictionaryEntry('date', 'string', 'date of report entry');
25        $entity->addDictionaryEntry('hour', 'string', 'hour of report entry');
26        $entity->addDictionaryEntry('waitingcount', 'number', 'amount of waiting spontaneous clients');
27        $entity->addDictionaryEntry('waitingtime', 'number', 'real waitingtime for spontaneous clients');
28        $entity->addDictionaryEntry('waytime', 'number', 'real waytime for spontaneous clients');
29        $entity->addDictionaryEntry('waitingcalculated', 'number', 'calculated waitingtime for spontaneous clients');
30        $entity->addDictionaryEntry('waitingcount_termin', 'number', 'amount of waiting clients with termin');
31        $entity->addDictionaryEntry('waitingtime_termin', 'number', 'real waitingtime with termin');
32        $entity->addDictionaryEntry('waytime_termin', 'number', 'real waytime with appointment');
33        $entity->addDictionaryEntry('waitingcalculated_termin', 'number', 'calculated waitingtime with termin');
34        $entity->addDictionaryEntry('waycount_termin', 'number', 'clients included in appointment way time');
35        $subjectIdList = explode(',', $subjectid);
36
37        $raw = $this
38            ->getReader()
39            ->fetchAll(
40                constant("\BO\Zmsbackend\Exchange\Repository\ExchangeWaitingscope::QUERY_READ_" . strtoupper($period)),
41                [
42                    'scopeid' => $subjectIdList,
43                    'datestart' => $datestart->format('Y-m-d'),
44                    'dateend' => $dateend->format('Y-m-d')
45                ]
46            );
47
48        foreach ($raw as $entry) {
49            foreach (range(7, 19) as $hour) {
50                $entity->addDataSet([
51                    $entry['standortid'],
52                    $entry['datum'],
53                    $hour,
54                    $entry[sprintf('hour_%02d_waiting_count_spontaneous', $hour)],
55                    $entry[sprintf('hour_%02d_waiting_time_spontaneous', $hour)],
56                    $entry[sprintf('hour_%02d_way_time_spontaneous', $hour)],
57                    $entry[sprintf('hour_%02d_estimated_waiting_time_spontaneous', $hour)],
58                    $entry[sprintf('hour_%02d_waiting_count_appointment', $hour)],
59                    $entry[sprintf('hour_%02d_waiting_time_appointment', $hour)],
60                    $entry[sprintf('hour_%02d_way_time_appointment', $hour)],
61                    $entry[sprintf('hour_%02d_estimated_waiting_time_appointment', $hour)],
62                    $entry[sprintf('hour_%02d_way_count_appointment', $hour)],
63                ]);
64            }
65        }
66
67        return $entity;
68    }
69
70    /**
71     * @return Exchange
72     */
73    #[\Override]
74    public function readSubjectList()
75    {
76        $raw = $this->getReader()->fetchAll(\BO\Zmsbackend\Exchange\Repository\ExchangeWaitingscope::QUERY_SUBJECTS, []);
77        $entity = new Exchange();
78        $entity['title'] = "Wartestatistik";
79        $entity->setPeriod(new \DateTimeImmutable(), new \DateTimeImmutable());
80        $entity->addDictionaryEntry('subject', 'string', 'Standort ID', 'scope.id');
81        $entity->addDictionaryEntry('periodstart', 'string', 'Datum von');
82        $entity->addDictionaryEntry('periodend', 'string', 'Datum bis');
83        $entity->addDictionaryEntry('description', 'string', 'Standort Beschreibung');
84        foreach ($raw as $entry) {
85            $entity->addDataSet(array_values($entry));
86        }
87        return $entity;
88    }
89
90    /**
91     * @return Exchange
92     */
93    #[\Override]
94    public function readPeriodList(string|int $subjectid, string $period = 'day')
95    {
96        $scope = (new \BO\Zmsbackend\Scope\Service\Scope())->readEntity($subjectid);
97        $entity = new Exchange();
98        $entity['title'] = "Wartestatistik " . $scope->contact->name . " " . $scope->shortName;
99        $entity->setPeriod(new \DateTimeImmutable(), new \DateTimeImmutable(), $period);
100        $entity->addDictionaryEntry('period');
101
102        $montsList = $this->getReader()->fetchAll(
103            constant("\BO\Zmsbackend\Exchange\Repository\ExchangeWaitingscope::QUERY_PERIODLIST_MONTH"),
104            [
105                'scopeid' => $subjectid,
106            ]
107        );
108        $raw = [];
109        foreach ($montsList as $month) {
110            $date = new \DateTimeImmutable($month['date']);
111            $raw[$date->format('Y')][] = $month['date'];
112            rsort($raw[$date->format('Y')]);
113        }
114        krsort($raw);
115
116        foreach ($raw as $year => $months) {
117            $entity->addDataSet([$year]);
118            foreach ($months as $month) {
119                $entity->addDataSet([$month]);
120            }
121        }
122        return $entity;
123    }
124
125    /**
126     * fetch entry by scope and date or create an entry, if it does not exists
127     * the returned entry is save for updating
128     */
129    public function readByDateTime(
130        \BO\Zmsentities\Scope $scope,
131        \DateTimeInterface $date,
132        bool $isWithAppointment = false
133    ) {
134        $sql = \BO\Zmsbackend\Exchange\Repository\ExchangeWaitingscope::getQuerySelectByDateTime($date, $isWithAppointment);
135        $existingEntry = $this->getReader()->fetchOne(
136            $sql,
137            [
138                'scopeid' => $scope->id,
139                'date' => $date->format('Y-m-d'),
140                'hour' => $date->format('H')
141            ]
142        );
143        if (!$existingEntry) {
144            $this->perform(
145                \BO\Zmsbackend\Exchange\Repository\ExchangeWaitingscope::QUERY_CREATE,
146                [
147                    'scopeid' => $scope->id,
148                    'date' => $date->format('Y-m-d'),
149                ]
150            );
151            $existingEntry = $this->readByDateTime($scope, $date, $isWithAppointment);
152        }
153        return $existingEntry;
154    }
155
156    /**
157     * Write calculated waiting time and count of queued processes into statistic
158     */
159    public function writeWaitingTimeCalculated(
160        \BO\Zmsentities\Scope $scope,
161        \DateTimeInterface $now,
162        bool $isWithAppointment = false
163    ): static {
164        if ($now > (new \DateTime())) {
165            return $this;
166        }
167
168        $queueList = (new \BO\Zmsbackend\Scope\Service\Scope())->readQueueListWithWaitingTime($scope, $now);
169
170        $existingEntry = $this->readByDateTime($scope, $now, $isWithAppointment);
171        $queueEntry = $queueList->getFakeOrLastWaitingnumber();
172        $waitingCalculated = $existingEntry['waitingcalculated'] > $queueEntry['waitingTimeEstimate'] ?
173            $existingEntry['waitingcalculated']
174            : $queueEntry['waitingTimeEstimate'];
175
176        $this->perform(
177            \BO\Zmsbackend\Exchange\Repository\ExchangeWaitingscope::getQueryUpdateByDateTime($now, $isWithAppointment),
178            [
179                'waitingcalculated' => $waitingCalculated,
180                'waitingcount' => $existingEntry['waitingcount'],
181                'waitingtime' => $existingEntry['waitingtime'],
182                'waytime' => $existingEntry['waytime'],
183                'scopeid' => $scope->id,
184                'date' => $now->format('Y-m-d'),
185                'hour' => $now->format('H')
186            ]
187        );
188        return $this;
189    }
190
191    /**
192     * Write real waiting time into statistics
193     */
194    public function writeWaitingTime(
195        \BO\Zmsentities\Process $process,
196        \DateTimeInterface $now
197    ): static {
198        if ($now > (new \DateTime())) {
199            return $this;
200        }
201
202        $waitingTime = $process->getWaitedMinutes($now);
203        $wayTime = $process->getWayMinutes($now);
204        $existingEntry = $this->readByDateTime(
205            $process->scope,
206            $process->getArrivalTime($now),
207            $process->isWithAppointment()
208        );
209        $waitingTime = $existingEntry['waitingtime'] > $waitingTime ? $existingEntry['waitingtime'] : $waitingTime;
210        $wayTime = $existingEntry['waytime'] > $wayTime ? $existingEntry['waytime'] : $wayTime;
211        $this->perform(
212            \BO\Zmsbackend\Exchange\Repository\ExchangeWaitingscope::getQueryUpdateByDateTime(
213                $process->getArrivalTime($now),
214                $process->isWithAppointment()
215            ),
216            [
217                'waitingcalculated' => $existingEntry['waitingcalculated'],
218                'waitingcount' => $existingEntry['waitingcount'],
219                'waitingtime' => $waitingTime,
220                'waytime' => $wayTime,
221                'scopeid' => $process->scope->id,
222                'date' => $now->format('Y-m-d'),
223                'hour' => $now->format('H')
224            ]
225        );
226        return $this;
227    }
228
229    public function updateWaitingStatistics(
230        \BO\Zmsentities\Process $process,
231        \DateTimeInterface $now
232    ): static {
233        if ($now > (new \DateTime())) {
234            return $this;
235        }
236
237        $newWaitingTime = $process->getWaitedMinutes($now);
238        $newWayTime = $process->getWayMinutes($now);
239
240        $existingEntry = $this->readByDateTime(
241            $process->scope,
242            $process->getArrivalTime($now),
243            $process->isWithAppointment()
244        );
245
246        $waitingCount = $existingEntry['waitingcount'] + 1;
247
248        $averageWaitingTime = ($existingEntry['waitingtime'] * $existingEntry['waitingcount'] + $newWaitingTime) / $waitingCount;
249        $averageWayTime = ($existingEntry['waytime'] * $existingEntry['waitingcount'] + $newWayTime) / $waitingCount;
250
251        $this->perform(
252            \BO\Zmsbackend\Exchange\Repository\ExchangeWaitingscope::getQueryUpdateByDateTime(
253                $process->getArrivalTime($now),
254                $process->isWithAppointment()
255            ),
256            [
257                'waitingcalculated' => $existingEntry['waitingcalculated'],
258                'waitingcount' => $waitingCount,
259                'waitingtime' => $averageWaitingTime,
260                'waytime' => $averageWayTime,
261                'scopeid' => $process->scope->id,
262                'date' => $now->format('Y-m-d'),
263                'hour' => $now->format('H')
264            ]
265        );
266
267        return $this;
268    }
269}