Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
87 / 87
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ExchangeClientscope
100.00% covered (success)
100.00%
87 / 87
100.00% covered (success)
100.00%
3 / 3
18
100.00% covered (success)
100.00%
1 / 1
 readEntity
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
1 / 1
9
 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        $subjectIdList = explode(',', $subjectid);
38        $aggregatedData = [];
39
40        foreach ($subjectIdList as $scopeId) {
41            $raw = $this->getReader()->fetchAll(
42                constant("\BO\Zmsdb\Query\ExchangeClientscope::QUERY_READ_REPORT"),
43                [
44                    'scopeid' => $scopeId,
45                    'datestart' => $datestart->format('Y-m-d'),
46                    'dateend' => $dateend->format('Y-m-d'),
47                    'groupby' => $this->groupBy[$period]
48                ]
49            );
50
51            foreach ($raw as $entry) {
52                $date = $entry['date'];
53
54                if (!isset($aggregatedData[$date])) {
55                    $aggregatedData[$date] = [
56                        'scopeids' => [],
57                        'date' => $date,
58                        'notificationscount' => 0,
59                        'notificationscost' => 0,
60                        'clientscount' => 0,
61                        'missed' => 0,
62                        'withappointment' => 0,
63                        'missedwithappointment' => 0,
64                        'requestscount' => 0
65                    ];
66                }
67
68                $aggregatedData[$date]['notificationscount'] += $entry['notificationscount'];
69                $aggregatedData[$date]['clientscount'] += $entry['clientscount'];
70                $aggregatedData[$date]['missed'] += $entry['missed'];
71                $aggregatedData[$date]['withappointment'] += $entry['withappointment'];
72                $aggregatedData[$date]['missedwithappointment'] += $entry['missedwithappointment'];
73                $aggregatedData[$date]['requestscount'] += $entry['requestcount'];
74
75                if (!in_array($scopeId, $aggregatedData[$date]['scopeids'])) {
76                    $aggregatedData[$date]['scopeids'][] = $scopeId;
77                }
78            }
79        }
80
81        ksort($aggregatedData);
82        foreach ($aggregatedData as $entry) {
83            $entry['notificationscost'] = $entry['notificationscount'] * $costs;
84            $entity->addDataSet(array_values($entry));
85        }
86        return $entity;
87    }
88
89    public function readSubjectList()
90    {
91        $raw = $this->getReader()->fetchAll(Query\ExchangeClientscope::QUERY_SUBJECTS, []);
92        $entity = new Exchange();
93        $entity['title'] = "Kundenstatistik ";
94        $entity->setPeriod(new \DateTimeImmutable(), new \DateTimeImmutable());
95        $entity->addDictionaryEntry('subject', 'string', 'Standort ID', 'scope.id');
96        $entity->addDictionaryEntry('periodstart', 'string', 'Datum von');
97        $entity->addDictionaryEntry('periodend', 'string', 'Datum bis');
98        $entity->addDictionaryEntry('description', 'string', 'Beschreibung des Standortes');
99        foreach ($raw as $entry) {
100            $entity->addDataSet(array_values($entry));
101        }
102        return $entity;
103    }
104
105    public function readPeriodList($subjectid, $period = 'day')
106    {
107        $scope = (new Scope())->readEntity($subjectid);
108        $entity = new Exchange();
109        $entity['title'] = "Kundenstatistik " . (($scope && $scope->contact) ? $scope->contact->name : 'Unknown') . " " . ($scope ? $scope->shortName : 'Unknown');
110        $entity->setPeriod(new \DateTimeImmutable(), new \DateTimeImmutable(), $period);
111        $entity->addDictionaryEntry('period');
112
113        $montsList = $this->getReader()->fetchAll(
114            constant("\BO\Zmsdb\Query\ExchangeClientscope::QUERY_PERIODLIST_MONTH"),
115            [
116                'scopeid' => $subjectid,
117            ]
118        );
119        $raw = [];
120        foreach ($montsList as $month) {
121            $date = new \DateTimeImmutable($month['date']);
122            $raw[$date->format('Y')][] = $month['date'];
123            rsort($raw[$date->format('Y')]);
124        }
125        krsort($raw);
126
127        foreach ($raw as $year => $months) {
128            $entity->addDataSet([$year]);
129            foreach ($months as $month) {
130                $entity->addDataSet([$month]);
131            }
132        }
133        return $entity;
134    }
135}