Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
93 / 93
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ExchangeClientscope
100.00% covered (success)
100.00%
93 / 93
100.00% covered (success)
100.00%
3 / 3
17
100.00% covered (success)
100.00%
1 / 1
 readEntity
100.00% covered (success)
100.00%
60 / 60
100.00% covered (success)
100.00%
1 / 1
8
 readSubjectList
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 readPeriodList
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3namespace BO\Zmsdb;
4
5use BO\Zmsentities\Exchange;
6
7class ExchangeClientscope extends Base
8{
9    protected $groupBy = array(
10        'month' => '%Y-%m',
11        'day' => '%Y-%m-%d',
12        'hour' => '%H-%i'
13    );
14
15    public function readEntity(
16        $subjectid,
17        \DateTimeInterface $datestart,
18        \DateTimeInterface $dateend,
19        $period = 'day'
20    ) {
21        $config = (new Config())->readEntity();
22        $costs = $config->getNotificationPreferences()['costs'];
23
24        $scope = (new Scope())->readEntity($subjectid);
25        $entity = new Exchange();
26        $entity['title'] = "Kundenstatistik " . (($scope && $scope->contact) ? $scope->contact->name : 'Unknown') . " " . ($scope ? $scope->shortName : 'Unknown');
27        $entity->setPeriod($datestart, $dateend, $period);
28        $entity->addDictionaryEntry('scopeids', 'array', 'Array of scope IDs that contributed data');
29        $entity->addDictionaryEntry('date', 'string', 'Date of entry');
30        $entity->addDictionaryEntry('notificationscount', 'number', 'Amount of notifications sent');
31        $entity->addDictionaryEntry('notificationscost', 'string', 'Costs of notifications');
32        $entity->addDictionaryEntry('clientscount', 'number', 'Amount of clients');
33        $entity->addDictionaryEntry('missed', 'number', 'Amount of missed clients');
34        $entity->addDictionaryEntry('withappointment', 'number', 'Amount of clients with an appointment');
35        $entity->addDictionaryEntry('missedwithappointment', 'number', 'Amount of missed clients with an appointment');
36        $entity->addDictionaryEntry('requestscount', 'number', 'Amount of requests');
37        $entity->addDictionaryEntry('ticketprinter', 'number', 'Amount of spontaneous clients via E-Kiosk (appeared)');
38        $entity->addDictionaryEntry('ticketprintermissed', 'number', 'Amount of spontaneous clients via E-Kiosk (missed)');
39        $subjectIdList = explode(',', $subjectid);
40        $aggregatedData = [];
41
42        $raw = $this->getReader()->fetchAll(
43            constant("\BO\Zmsdb\Query\ExchangeClientscope::QUERY_READ_REPORT"),
44            [
45                'scopeids' => $subjectIdList,
46                'datestart' => $datestart->format('Y-m-d'),
47                'dateend' => $dateend->format('Y-m-d'),
48                'groupby' => $this->groupBy[$period]
49            ]
50        );
51
52        foreach ($raw as $entry) {
53            $date = $entry['date'];
54            $scopeId = implode(',', $subjectIdList);
55
56            if (!isset($aggregatedData[$date])) {
57                $aggregatedData[$date] = [
58                    'scopeids' => [],
59                    'date' => $date,
60                    'notificationscount' => 0,
61                    'notificationscost' => 0,
62                    'clientscount' => 0,
63                    'missed' => 0,
64                    'withappointment' => 0,
65                    'missedwithappointment' => 0,
66                    'requestscount' => 0,
67                    'ticketprinter' => 0,
68                    'ticketprintermissed' => 0
69                ];
70            }
71
72            $aggregatedData[$date]['notificationscount'] += $entry['notificationscount'];
73            $aggregatedData[$date]['clientscount'] += $entry['clientscount'];
74            $aggregatedData[$date]['missed'] += $entry['missed'];
75            $aggregatedData[$date]['withappointment'] += $entry['withappointment'];
76            $aggregatedData[$date]['missedwithappointment'] += $entry['missedwithappointment'];
77            $aggregatedData[$date]['requestscount'] += $entry['requestcount'];
78            $aggregatedData[$date]['ticketprinter'] += $entry['ticketprinter'] ?? 0;
79            $aggregatedData[$date]['ticketprintermissed'] += $entry['ticketprintermissed'] ?? 0;
80
81            if (!in_array($scopeId, $aggregatedData[$date]['scopeids'])) {
82                $aggregatedData[$date]['scopeids'][] = $scopeId;
83            }
84        }
85
86        ksort($aggregatedData);
87        foreach ($aggregatedData as $entry) {
88            $entry['notificationscost'] = $entry['notificationscount'] * $costs;
89            $entity->addDataSet(array_values($entry));
90        }
91        return $entity;
92    }
93
94    public function readSubjectList()
95    {
96        $raw = $this->getReader()->fetchAll(Query\ExchangeClientscope::QUERY_SUBJECTS, []);
97        $entity = new Exchange();
98        $entity['title'] = "Kundenstatistik ";
99        $entity->setPeriod(new \DateTimeImmutable(), new \DateTimeImmutable());
100        $entity->addDictionaryEntry('subject', 'string', 'Standort ID', 'scope.id');
101        $entity->addDictionaryEntry('periodstart', 'string', 'Datum von');
102        $entity->addDictionaryEntry('periodend', 'string', 'Datum bis');
103        $entity->addDictionaryEntry('description', 'string', 'Beschreibung des Standortes');
104        foreach ($raw as $entry) {
105            $entity->addDataSet(array_values($entry));
106        }
107        return $entity;
108    }
109
110    public function readPeriodList($subjectid, $period = 'day')
111    {
112        $scope = (new Scope())->readEntity($subjectid);
113        $entity = new Exchange();
114        $entity['title'] = "Kundenstatistik " . (($scope && $scope->contact) ? $scope->contact->name : 'Unknown') . " " . ($scope ? $scope->shortName : 'Unknown');
115        $entity->setPeriod(new \DateTimeImmutable(), new \DateTimeImmutable(), $period);
116        $entity->addDictionaryEntry('period');
117
118        $montsList = $this->getReader()->fetchAll(
119            constant("\BO\Zmsdb\Query\ExchangeClientscope::QUERY_PERIODLIST_MONTH"),
120            [
121                'scopeid' => $subjectid,
122            ]
123        );
124        $raw = [];
125        foreach ($montsList as $month) {
126            $date = new \DateTimeImmutable($month['date']);
127            $raw[$date->format('Y')][] = $month['date'];
128            rsort($raw[$date->format('Y')]);
129        }
130        krsort($raw);
131
132        foreach ($raw as $year => $months) {
133            $entity->addDataSet([$year]);
134            foreach ($months as $month) {
135                $entity->addDataSet([$month]);
136            }
137        }
138        return $entity;
139    }
140}