Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.59% |
85 / 88 |
|
70.00% |
7 / 10 |
CRAP | |
0.00% |
0 / 1 |
ClientReport | |
96.59% |
85 / 88 |
|
70.00% |
7 / 10 |
41 | |
0.00% |
0 / 1 |
readResponse | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
writeReport | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
writeReportHeader | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
7 | |||
writeTotalsRow | |
95.00% |
19 / 20 |
|
0.00% |
0 / 1 |
8 | |||
writeReportData | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
8 | |||
writeLegend | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
calculateNoAppointment | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
calculateMissedNoAppointment | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
calculateNoAppointmentForRow | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
calculateMissedNoAppointmentForRow | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | /** |
4 | * @package zmsstatistic |
5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
6 | **/ |
7 | |
8 | namespace BO\Zmsstatistic\Download; |
9 | |
10 | use BO\Zmsentities\Exchange as ReportEntity; |
11 | use BO\Zmsstatistic\Helper\Download; |
12 | use PhpOffice\PhpSpreadsheet\Spreadsheet; |
13 | use Psr\Http\Message\RequestInterface; |
14 | use Psr\Http\Message\ResponseInterface; |
15 | |
16 | class ClientReport extends Base |
17 | { |
18 | /** |
19 | * @SuppressWarnings(Param) |
20 | * @return ResponseInterface |
21 | */ |
22 | public function readResponse( |
23 | RequestInterface $request, |
24 | ResponseInterface $response, |
25 | array $args |
26 | ) { |
27 | $title = 'clientstatistic_' . $args['period']; |
28 | $download = (new Download($request))->setSpreadSheet($title); |
29 | $spreadsheet = $download->getSpreadSheet(); |
30 | $spreadsheet = $this->writeInfoHeader($args, $spreadsheet); |
31 | if ($args['reports']) { |
32 | foreach ($args['reports'] as $report) { |
33 | if ('month' == $report->period) { |
34 | $spreadsheet = $this->writeReport($report, $download->getSpreadSheet(), 'yyyy', 'MMMM', 'yyyy'); |
35 | } else { |
36 | $spreadsheet = $this->writeReport($report, $download->getSpreadSheet()); |
37 | } |
38 | } |
39 | } |
40 | $this->writeLegend($spreadsheet); |
41 | |
42 | return $download->writeDownload($response); |
43 | } |
44 | |
45 | public function writeReport( |
46 | ReportEntity $report, |
47 | Spreadsheet $spreadsheet, |
48 | $datePatternTotals = 'MMMM', |
49 | $datePatternCol1 = 'ccc', |
50 | $datePatternCol2 = 'dd.MM.yy' |
51 | ) { |
52 | $sheet = $spreadsheet->getActiveSheet(); |
53 | $this->writeReportHeader($report, $sheet); |
54 | $this->writeTotalsRow($report, $sheet, $datePatternTotals); |
55 | $this->writeReportData($report, $sheet, $datePatternCol1, $datePatternCol2); |
56 | |
57 | return $spreadsheet; |
58 | } |
59 | |
60 | public function writeReportHeader(ReportEntity $report, $sheet) |
61 | { |
62 | $reportHeader = []; |
63 | if ('totals' == end($report->data)['date'] || 'month' == $report->period) { |
64 | $reportHeader[] = null; |
65 | } |
66 | $columnIndex = 0; |
67 | foreach (array_keys($report->data[0]) as $headline) { |
68 | $columnIndex++; |
69 | if ($columnIndex == 3 || $columnIndex == 4) { |
70 | continue; |
71 | } |
72 | |
73 | if (!in_array($headline, static::$ignoreColumns)) { |
74 | $reportHeader[] = static::$headlines[$headline]; |
75 | } |
76 | } |
77 | |
78 | // Placeholders for the new columns |
79 | $end = array_splice($reportHeader, -1); // get the last two elements |
80 | $reportHeader[] = static::$headlines['noappointment']; |
81 | $reportHeader[] = static::$headlines['missednoappointment']; |
82 | $reportHeader = array_merge($reportHeader, $end); // append them back after adding new headers |
83 | |
84 | |
85 | $sheet->fromArray($reportHeader, null, 'A' . ($sheet->getHighestRow() + 2)); |
86 | } |
87 | |
88 | public function writeTotalsRow(ReportEntity $report, $sheet, $datePatternTotals) |
89 | { |
90 | if ('totals' == end($report->data)['date']) { |
91 | $totals = array_pop($report->data); |
92 | $dateString = $report->firstDay->year . '-' . $report->firstDay->month . '-' . $report->firstDay->day; |
93 | $dateCol1 = ('MMMM' == $datePatternTotals) ? |
94 | $this->getFormatedDates($this->setDateTime($dateString), $datePatternTotals) : |
95 | null; |
96 | $dateCol2 = $this->setDateTime($dateString)->format('Y'); |
97 | $reportTotal = [$dateCol1, $dateCol2]; |
98 | |
99 | $columnIndex = 0; |
100 | foreach ($totals as $key => $item) { |
101 | $columnIndex++; |
102 | if ($columnIndex == 3 || $columnIndex == 4) { |
103 | continue; |
104 | } |
105 | |
106 | if (! in_array($key, static::$ignoreColumns) && 'date' != $key) { |
107 | $reportTotal[] = (string)($item); |
108 | } |
109 | } |
110 | |
111 | // Calculations for the new columns |
112 | $end = array_splice($reportTotal, -1); |
113 | |
114 | $reportTotal[] = $this->calculateNoAppointment($totals); |
115 | $reportTotal[] = $this->calculateMissedNoAppointment($totals); |
116 | |
117 | $reportTotal = array_merge($reportTotal, $end); |
118 | |
119 | $sheet->fromArray($reportTotal, null, 'A' . ($sheet->getHighestRow() + 1)); |
120 | } |
121 | } |
122 | |
123 | public function writeReportData(ReportEntity $report, $sheet, $datePatternCol1, $datePatternCol2) |
124 | { |
125 | $reportData = []; |
126 | |
127 | foreach ($report->data as $row => $entry) { |
128 | $processedRow = []; |
129 | $columnIndex = 0; |
130 | |
131 | foreach ($entry as $key => $item) { |
132 | $columnIndex++; |
133 | if ($columnIndex == 3 || $columnIndex == 4) { |
134 | continue; |
135 | } |
136 | |
137 | if (!in_array($key, static::$ignoreColumns)) { |
138 | if ('date' == $key) { |
139 | $dateCol1 = $this->getFormatedDates($this->setDateTime($item), $datePatternCol1); |
140 | $item = $this->getFormatedDates($this->setDateTime($item), $datePatternCol2); |
141 | $processedRow[] = $dateCol1; |
142 | } |
143 | $processedRow[] = (is_numeric($item)) ? (string)($item) : $item; |
144 | } |
145 | } |
146 | |
147 | // Calculations for the new columns |
148 | $end = array_splice($processedRow, -1); |
149 | |
150 | $processedRow[] = $this->calculateNoAppointmentForRow($entry); |
151 | $processedRow[] = $this->calculateMissedNoAppointmentForRow($entry); |
152 | |
153 | $processedRow = array_merge($processedRow, $end); |
154 | |
155 | $reportData[$row] = $processedRow; |
156 | } |
157 | |
158 | $sheet->fromArray($reportData, null, 'A' . ($sheet->getHighestRow() + 1)); |
159 | } |
160 | |
161 | |
162 | |
163 | protected function writeLegend(Spreadsheet $spreadsheet) |
164 | { |
165 | $sheet = $spreadsheet->getActiveSheet(); |
166 | $legendData[] = '** in dieser Spalte sind nicht abschließend bearbeitete Kunden angegeben'; |
167 | $legendData = array_chunk($legendData, 1); |
168 | $sheet->fromArray($legendData, null, 'A' . ($sheet->getHighestRow() + 1)); |
169 | |
170 | return $spreadsheet; |
171 | } |
172 | |
173 | private function calculateNoAppointment($totals) |
174 | { |
175 | if (isset($totals['clientscount']) && isset($totals['withappointment'])) { |
176 | return (string) ($totals['clientscount'] - $totals['withappointment']); |
177 | } |
178 | return 'N/A'; |
179 | } |
180 | |
181 | private function calculateMissedNoAppointment($totals) |
182 | { |
183 | if (isset($totals['missed']) && isset($totals['missedwithappointment'])) { |
184 | return (string) ($totals['missed'] - $totals['missedwithappointment']); |
185 | } |
186 | return 'N/A'; |
187 | } |
188 | |
189 | private function calculateNoAppointmentForRow($entry) |
190 | { |
191 | if (isset($entry['clientscount']) && isset($entry['withappointment'])) { |
192 | return (string) ($entry['clientscount'] - $entry['withappointment']); |
193 | } |
194 | return 'N/A'; |
195 | } |
196 | |
197 | private function calculateMissedNoAppointmentForRow($entry) |
198 | { |
199 | if (isset($entry['missed']) && isset($entry['missedwithappointment'])) { |
200 | return (string) ($entry['missed'] - $entry['missedwithappointment']); |
201 | } |
202 | return 'N/A'; |
203 | } |
204 | } |