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
17
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
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        $subjectIdList = explode(',', $subjectid);
38        $aggregatedData = [];
39
40        $raw = $this->getReader()->fetchAll(
41            constant("\BO\Zmsdb\Query\ExchangeClientscope::QUERY_READ_REPORT"),
42            [
43                'scopeids' => $subjectIdList,
44                'datestart' => $datestart->format('Y-m-d'),
45                'dateend' => $dateend->format('Y-m-d'),
46                'groupby' => $this->groupBy[$period]
47            ]
48        );
49
50        foreach ($raw as $entry) {
51            $date = $entry['date'];
52            $scopeId = implode(',', $subjectIdList);
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        ksort($aggregatedData);
81        foreach ($aggregatedData as $entry) {
82            $entry['notificationscost'] = $entry['notificationscount'] * $costs;
83            $entity->addDataSet(array_values($entry));
84        }
85        return $entity;
86    }
87
88    public function readSubjectList()
89    {
90        $raw = $this->getReader()->fetchAll(Query\ExchangeClientscope::QUERY_SUBJECTS, []);
91        $entity = new Exchange();
92        $entity['title'] = "Kundenstatistik ";
93        $entity->setPeriod(new \DateTimeImmutable(), new \DateTimeImmutable());
94        $entity->addDictionaryEntry('subject', 'string', 'Standort ID', 'scope.id');
95        $entity->addDictionaryEntry('periodstart', 'string', 'Datum von');
96        $entity->addDictionaryEntry('periodend', 'string', 'Datum bis');
97        $entity->addDictionaryEntry('description', 'string', 'Beschreibung des Standortes');
98        foreach ($raw as $entry) {
99            $entity->addDataSet(array_values($entry));
100        }
101        return $entity;
102    }
103
104    public function readPeriodList($subjectid, $period = 'day')
105    {
106        $scope = (new Scope())->readEntity($subjectid);
107        $entity = new Exchange();
108        $entity['title'] = "Kundenstatistik " . (($scope && $scope->contact) ? $scope->contact->name : 'Unknown') . " " . ($scope ? $scope->shortName : 'Unknown');
109        $entity->setPeriod(new \DateTimeImmutable(), new \DateTimeImmutable(), $period);
110        $entity->addDictionaryEntry('period');
111
112        $montsList = $this->getReader()->fetchAll(
113            constant("\BO\Zmsdb\Query\ExchangeClientscope::QUERY_PERIODLIST_MONTH"),
114            [
115                'scopeid' => $subjectid,
116            ]
117        );
118        $raw = [];
119        foreach ($montsList as $month) {
120            $date = new \DateTimeImmutable($month['date']);
121            $raw[$date->format('Y')][] = $month['date'];
122            rsort($raw[$date->format('Y')]);
123        }
124        krsort($raw);
125
126        foreach ($raw as $year => $months) {
127            $entity->addDataSet([$year]);
128            foreach ($months as $month) {
129                $entity->addDataSet([$month]);
130            }
131        }
132        return $entity;
133    }
134}