Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.89% covered (success)
91.89%
102 / 111
76.92% covered (warning)
76.92%
10 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessStatusArchived
91.89% covered (success)
91.89%
102 / 111
76.92% covered (warning)
76.92%
10 / 13
31.51
0.00% covered (danger)
0.00%
0 / 1
 getEntityMapping
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
1
 addConditionArchiveId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addConditionScopeId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addConditionScopeIds
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 addConditionTime
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addConditionTimes
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 addConditionIsMissed
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addConditionWithAppointment
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addJoinStatisticFailed
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 addValuesNewArchive
77.78% covered (warning)
77.78%
21 / 27
0.00% covered (danger)
0.00%
0 / 1
11.10
 postProcess
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
6
 getArchivedServices
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 addConditionOlderThanDate
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BO\Zmsbackend\Process\Repository;
4
5/**
6*
7* @SuppressWarnings(TooManyPublicMethods)
8* @SuppressWarnings(Complexity)
9 */
10class ProcessStatusArchived extends \BO\Zmsbackend\Query\Base implements \BO\Zmsbackend\Query\MappingInterface
11{
12    /**
13     *
14     * @var String TABLE mysql table reference
15     */
16    public const string TABLE = 'buergerarchiv';
17    public const string STATISTIC_TABLE = 'statistik';
18    public const string ALIAS = 'process';
19
20    const QUERY_INSERT_IN_STATISTIC = '
21        INSERT INTO ' . self::STATISTIC_TABLE . ' SET
22            lastbuergerarchivid = :archiveId,
23            termin = :withAppointment,
24            datum = :date,
25            anliegenid = :requestId,
26            info_dl_id = :providerId,
27            standortid = :scopeId,
28            clusterid = :clusterId,
29            behoerdenid = :departmentId,
30            organisationsid = :organisationId,
31            kundenid = :ownerId,
32            processing_time = :processingTime
33    ';
34
35    /**
36     * @return (\BO\Zmsbackend\Query\Builder\Expression|string)[]
37     *
38     */
39    #[\Override]
40    public function getEntityMapping()
41    {
42        return [
43            'archiveId' => 'process.BuergerarchivID',
44            'status' => self::expression('"archived"'),
45            'appointments__0__date' => self::expression(
46                'CONCAT(`process`.`Datum`, " ", `process`.`Timestamp`)'
47            ),
48            'scope__id' => 'process.StandortID',
49            '__clientsCount' => 'process.AnzahlPersonen',
50            'waitingTime' => 'process.waiting_time',
51            'wayTime' => 'process.way_time',
52            'processingTime' => 'process.processing_time',
53            'name' => 'process.name',
54            'services' => 'process.dienstleistungen',
55            'queue__arrivalTime' => self::expression(
56                'CONCAT(`process`.`Datum`, " 00:00:00")'
57            ),
58            'queue__callTime' => self::expression(
59                'CONCAT(`process`.`Datum`, " ", SEC_TO_TIME(`waiting_time` * 60))'
60            ),
61            'queue__withAppointment' => 'process.mitTermin',
62            'withAppointment' => 'process.mitTermin',
63            'queue__status' => self::expression(
64                'IF(`process`.`nicht_erschienen`,
65                    "missed",
66                    "finished"
67                )'
68            ),
69            'isTicketprinter' => 'process.is_ticketprinter',
70        ];
71    }
72
73    public function addConditionArchiveId($archiveId): static
74    {
75        $this->query->where('process.BuergerarchivID', '=', $archiveId);
76        return $this;
77    }
78
79    public function addConditionScopeId($scopeId): static
80    {
81        $this->query->where('process.StandortID', '=', $scopeId);
82        return $this;
83    }
84
85    public function addConditionScopeIds($scopeIds): static
86    {
87        $this->query->where(function (\BO\Zmsbackend\Query\Builder\ConditionBuilder $condition) use ($scopeIds) {
88            foreach ($scopeIds as $scopeId) {
89                $condition
90                    ->orWith('process.StandortID', '=', $scopeId);
91            }
92        });
93        return $this;
94    }
95
96
97    public function addConditionTime(\DateTimeInterface $now): static
98    {
99        $this->query->where('process.Datum', '=', $now->format('Y-m-d'));
100        return $this;
101    }
102
103    /**
104     * @psalm-api
105     */
106    public function addConditionTimes(array $dateTimes): static
107    {
108        $this->query->where(function (\BO\Zmsbackend\Query\Builder\ConditionBuilder $condition) use ($dateTimes) {
109            foreach ($dateTimes as $dateTime) {
110                $condition
111                    ->orWith('process.Datum', '=', $dateTime->format('Y-m-d'));
112            }
113        });
114        return $this;
115    }
116
117    public function addConditionIsMissed($missed): static
118    {
119        $this->query->where('process.nicht_erschienen', '=', $missed);
120        return $this;
121    }
122
123    public function addConditionWithAppointment($withAppointment): static
124    {
125        $this->query->where('process.mitTermin', '=', $withAppointment);
126        return $this;
127    }
128
129    public function addJoinStatisticFailed($dateTime, \BO\Zmsentities\Scope $scope): static
130    {
131        //use existing index with StandortID and Datum
132        $this->leftJoin(
133            new \BO\Zmsbackend\Query\Alias(self::STATISTIC_TABLE, 'statistic'),
134            self::expression('
135                statistic.StandortID = process.StandortID
136                AND statistic.Datum = process.Datum
137                AND `statistic`.`lastbuergerarchivid` = `process`.`BuergerarchivID`
138            ')
139        );
140        $this->query->where(function (\BO\Zmsbackend\Query\Builder\ConditionBuilder $query) use ($dateTime, $scope) {
141            $query->andWith(
142                self::expression('statistic.lastbuergerarchivid IS NULL AND process.Datum'),
143                '>',
144                $dateTime->format('Y-m-d')
145            );
146            $query->andWith('process.nicht_erschienen', '=', 0);
147            $query->andWith('process.StandortID', '=', $scope->id);
148        });
149        return $this;
150    }
151    public function addValuesNewArchive(\BO\Zmsentities\Process $process, \DateTimeInterface $now): void
152    {
153        $processingTimeStr = $process->getProcessingTime();
154        $bearbeitungszeit = null;
155
156        if (!empty($processingTimeStr)) {
157            list($hours, $minutes, $seconds) = explode(':', $processingTimeStr);
158            $totalMinutes = (double) ($hours * 60 + $minutes + $seconds / 60);
159            $bearbeitungszeit = $totalMinutes;
160        }
161
162        $waitingTimeStr = $process->getWaitingTime();
163        $warteZeit = null;
164
165        if (!empty($waitingTimeStr)) {
166            list($hours, $minutes, $seconds) = explode(':', $waitingTimeStr);
167            $totalMinutes = (double) ($hours * 60 + $minutes + $seconds / 60);
168            $warteZeit = $totalMinutes;
169        }
170
171        $isTicketprinter = isset($process->isTicketprinter) && $process->isTicketprinter ? 1 : 0;
172
173        $this->addValues([
174            'StandortID' => $process->scope['id'],
175            'name' => $process->getFirstClient()['familyName'],
176            'dienstleistungen' => $this->getArchivedServices($process),
177            'Datum' => $process->getFirstAppointment()->toDateTime()->format('Y-m-d'),
178            'mitTermin' => ($process->toQueue($now)->withAppointment) ? 1 : 0,
179            'nicht_erschienen' => ('missed' == $process->queue['status']) ? 1 : 0,
180            'Timestamp' => $process->getArrivalTime()->format('H:i:s'),
181            'waiting_time' => ($warteZeit > 0) ? $warteZeit : 0,
182            'way_time' => ($process->getWayMinutes() > 0) ? $process->getWayMinutes() : 0,
183            'processing_time' => ($bearbeitungszeit > 0) ? $bearbeitungszeit : 0,
184            'AnzahlPersonen' => $process->getClients()->count(),
185            'is_ticketprinter' => $isTicketprinter
186        ]);
187    }
188
189    #[\Override]
190    public function postProcess($data)
191    {
192        $date = $data[$this->getPrefixed("appointments__0__date")] ?? '';
193        $data[$this->getPrefixed("appointments__0__date")] = strtotime($date === null ? '' : $date);
194        $callTime = $data[$this->getPrefixed("queue__callTime")] ?? '';
195        $data[$this->getPrefixed("queue__callTime")] = strtotime($callTime === null ? '' : $callTime);
196        $arrivalTime = $data[$this->getPrefixed("queue__arrivalTime")] ?? '';
197        $data[$this->getPrefixed("queue__arrivalTime")] = strtotime($arrivalTime === null ? '' : $arrivalTime);
198        if (isset($data[$this->getPrefixed('__clientsCount')])) {
199            $clientsCount = $data[$this->getPrefixed('__clientsCount')];
200            unset($data[$this->getPrefixed('__clientsCount')]);
201            while ($clientsCount-- > 0) {
202                $data[$this->getPrefixed('clients__' . $clientsCount . '__familyName')] = 'Unknown';
203            }
204        }
205        return $data;
206    }
207
208    private function getArchivedServices(\BO\Zmsentities\Process $process)
209    {
210        if ($process->getRequests()->count() === 0) {
211            return '';
212        }
213
214        $services = $process->getRequests()->getFirst()->name;
215
216        if ($process->getRequests()->count() > 1) {
217            $services .= ' +' . ($process->getRequests()->count() - 1);
218        }
219
220        return $services;
221    }
222
223    public function addConditionOlderThanDate(\DateTimeInterface $dateTime): static
224    {
225        // Assuming 'Datum' is the column name that holds the date of the record
226        // and you want to select records older than the specified $dateTime
227        $this->query->where('Datum', '<', $dateTime->format('Y-m-d'));
228        return $this;
229    }
230}