Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
89 / 89
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
WaitingReport
100.00% covered (success)
100.00%
89 / 89
100.00% covered (success)
100.00%
5 / 5
22
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
3
 writeWaitingReport
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 writeHeader
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
 writeTotals
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
3
 writeReportPart
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2
3/**
4 * @package zmsstatistic
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsstatistic\Download;
9
10use BO\Zmsentities\Exchange as ReportEntity;
11use BO\Zmsstatistic\Helper\Download;
12use BO\Zmsstatistic\Helper\ReportHelper;
13use PhpOffice\PhpSpreadsheet\Spreadsheet;
14use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
15use Psr\Http\Message\RequestInterface;
16use Psr\Http\Message\ResponseInterface;
17
18class WaitingReport extends Base
19{
20    protected $reportPartsTermin = [
21        'waitingtime_termin' => 'Durchschnittliche Wartezeit in Min. (Terminkunden)',
22        'waitingcount_termin' => 'Wartende Terminkunden',
23        'waytime_termin' => 'Durchschnittliche Wegezeit in Min. (Terminkunden)',
24    ];
25
26    protected $reportPartsSpontan = [
27        'waitingtime' => 'Durchschnittliche Wartezeit in Min. (Spontankunden)',
28        'waitingcount' => 'Wartende Spontankunden',
29        'waytime' => 'Durchschnittliche Wegezeit in Min. (Spontankunden)',
30    ];
31
32    /**
33     * @SuppressWarnings(Param)
34     * @return ResponseInterface
35     */
36    public function readResponse(
37        RequestInterface $request,
38        ResponseInterface $response,
39        array $args
40    ) {
41        $title = 'waitingstatistic_' . $args['period'];
42        $download = (new Download($request))->setSpreadSheet($title);
43        $spreadsheet = $download->getSpreadSheet();
44
45        // Blatt 1: Terminkunden
46        $spreadsheet->getActiveSheet()->setTitle('Terminkunden');
47        $spreadsheet->setActiveSheetIndexByName('Terminkunden');
48        $this->writeInfoHeader($args, $spreadsheet);
49        foreach ($args['reports'] as $report) {
50            $this->writeWaitingReport($report, $spreadsheet, /*isTermin*/ true, 'dd.MM.yyyy');
51        }
52
53        // Blatt 2: Spontankunden
54        $spreadsheet->createSheet()->setTitle('Spontankunden');
55        $spreadsheet->setActiveSheetIndexByName('Spontankunden');
56        $this->writeInfoHeader($args, $spreadsheet);
57        foreach ($args['reports'] as $report) {
58            $this->writeWaitingReport($report, $spreadsheet, /*isTermin*/ false, 'dd.MM.yyyy');
59        }
60
61        // Für den Download das erste Blatt aktiv lassen
62        $spreadsheet->setActiveSheetIndexByName('Terminkunden');
63
64        return $download->writeDownload($response);
65    }
66
67    public function writeWaitingReport(
68        ReportEntity $report,
69        Spreadsheet $spreadsheet,
70        bool $isTermin,
71        $datePatternCol = 'dd.MM.yyyy',
72    ) {
73        $sheet = $spreadsheet->getActiveSheet();
74        $this->writeHeader($report, $sheet, $datePatternCol);
75        $this->writeTotals($report, $sheet, $isTermin);
76        if ($isTermin) {
77            $parts = $this->reportPartsTermin;
78        } else {
79            $parts = $this->reportPartsSpontan;
80        }
81        foreach ($parts as $partName => $headline) {
82            $this->writeReportPart($report, $sheet, $partName, $headline);
83        }
84
85        return $spreadsheet;
86    }
87
88    public function writeHeader(ReportEntity $report, $sheet, $datePatternCol)
89    {
90        $reportHeader = [];
91        $reportHeader[] = null;
92        $reportHeader[] = 'Max.';
93        $dates = array_keys($report->data);
94        sort($dates);
95        foreach ($dates as $date) {
96            if (! in_array($date, static::$ignoreColumns)) {
97                $date = $this->getFormatedDates($this->setDateTime($date), $datePatternCol);
98                $reportHeader[] = $date;
99            }
100        }
101        $startRow = $sheet->getHighestRow() + 2;
102        $sheet->fromArray($reportHeader, null, 'A' . $startRow);
103        // Datumszellen (ab B) in dieser Zeile fett schreiben
104        $lastColIdx = count($reportHeader);              // Anzahl der geschriebenen Zellen
105        if ($lastColIdx >= 2) {
106            $start = "B{$startRow}";
107            $end   = Coordinate::stringFromColumnIndex($lastColIdx) . $startRow;
108            $sheet->getStyle("$start:$end")->getFont()->setBold(true);
109        }
110    }
111
112    public function writeTotals(ReportEntity $report, $sheet, bool $isTermin)
113    {
114        $entity = clone $report;
115        $totals = $entity->data['max'];
116        unset($entity->data['max']);
117
118        if ($isTermin) {
119            $keys = [
120                'max' => 'max_waitingtime_termin',
121                'avg' => 'average_waitingtime_termin',
122                'avg_way' => 'average_waytime_termin',
123            ];
124        } else {
125            $keys = [
126                'max' => 'max_waitingtime',
127                'avg' => 'average_waitingtime',
128                'avg_way' => 'average_waytime',
129            ];
130        }
131        $reportTotal['max'][] = 'Stunden-Max (Spaltenmaximum) der Wartezeit in Min.';
132        $reportTotal['average'][] = 'Stundendurchschnitt (Spalten) der Wartezeit in Min.';
133        $reportTotal['average_waytime'][] = 'Stundendurchschnitt (Spalten) der Wegezeit in Min.';
134
135        $reportTotal['max'][] = ReportHelper::formatTimeValue($totals[$keys['max']]);
136        $reportTotal['average'][] = ReportHelper::formatTimeValue($totals[$keys['avg']]);
137        $reportTotal['average_waytime'][] = ReportHelper::formatTimeValue($totals[$keys['avg_way']]);
138
139        foreach ($entity->data as $entry) {
140            $reportTotal['max'][] = ReportHelper::formatTimeValue($entry[$keys['max']]);
141            $reportTotal['average'][] = ReportHelper::formatTimeValue($entry[$keys['avg']]);
142            $reportTotal['average_waytime'][] = ReportHelper::formatTimeValue($entry[$keys['avg_way']]);
143        }
144        $sheet->fromArray($reportTotal, null, 'A' . ($sheet->getHighestRow() + 1));
145    }
146
147    public function writeReportPart(ReportEntity $report, $sheet, $rangeName, $headline)
148    {
149        $entity = clone $report;
150        $totals = $entity->data['max'];
151        unset($entity->data['max']);
152        $reportData['headline'] = ['Zeitabschnitte','Tagesmaximum (Zeilenmaximum)',$headline];
153        $formatAsTime = strpos($rangeName, 'waitingcount') === false;
154        foreach ($entity->data as $entry) {
155            foreach ($entry as $hour => $item) {
156                if (5 < $hour && 19 > $hour) {
157                    if (! isset($reportData[$hour])) {
158                        $reportData[$hour] = [];
159                    }
160                    $range = $hour . '-' . ($hour + 1) . ' Uhr';
161                    if (! in_array($range, $reportData[$hour])) {
162                        $reportData[$hour][] = $range;
163                        $totalValue = $totals[$hour][$rangeName];
164                        $reportData[$hour][] = $formatAsTime
165                            ? ReportHelper::formatTimeValue($totalValue)
166                            : $totalValue;
167                    }
168                    $value = $item[$rangeName] ?? '-';
169                    $reportData[$hour][] = $formatAsTime
170                        ? ReportHelper::formatTimeValue($value)
171                        : $value;
172                }
173            }
174        }
175        $startRow = $sheet->getHighestRow() + 2;
176        $sheet->fromArray($reportData, null, 'A' . $startRow);
177        $sheet->getStyle("A{$startRow}:C{$startRow}")->getFont()->setBold(true);
178    }
179}