Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.34% |
79 / 82 |
|
66.67% |
6 / 9 |
CRAP | |
0.00% |
0 / 1 |
ClientReport | |
96.34% |
79 / 82 |
|
66.67% |
6 / 9 |
40 | |
0.00% |
0 / 1 |
readResponse | |
100.00% |
10 / 10 |
|
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 | |||
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 | return $download->writeDownload($response); |
41 | } |
42 | |
43 | public function writeReport( |
44 | ReportEntity $report, |
45 | Spreadsheet $spreadsheet, |
46 | $datePatternTotals = 'MMMM', |
47 | $datePatternCol1 = 'ccc', |
48 | $datePatternCol2 = 'dd.MM.yy' |
49 | ) { |
50 | $sheet = $spreadsheet->getActiveSheet(); |
51 | $this->writeReportHeader($report, $sheet); |
52 | $this->writeTotalsRow($report, $sheet, $datePatternTotals); |
53 | $this->writeReportData($report, $sheet, $datePatternCol1, $datePatternCol2); |
54 | |
55 | return $spreadsheet; |
56 | } |
57 | |
58 | public function writeReportHeader(ReportEntity $report, $sheet) |
59 | { |
60 | $reportHeader = []; |
61 | if ('totals' == end($report->data)['date'] || 'month' == $report->period) { |
62 | $reportHeader[] = null; |
63 | } |
64 | $columnIndex = 0; |
65 | foreach (array_keys($report->data[0]) as $headline) { |
66 | $columnIndex++; |
67 | if ($columnIndex == 3 || $columnIndex == 4) { |
68 | continue; |
69 | } |
70 | |
71 | if (!in_array($headline, static::$ignoreColumns)) { |
72 | $reportHeader[] = static::$headlines[$headline]; |
73 | } |
74 | } |
75 | |
76 | // Placeholders for the new columns |
77 | $end = array_splice($reportHeader, -1); // get the last two elements |
78 | $reportHeader[] = static::$headlines['noappointment']; |
79 | $reportHeader[] = static::$headlines['missednoappointment']; |
80 | $reportHeader = array_merge($reportHeader, $end); // append them back after adding new headers |
81 | |
82 | |
83 | $sheet->fromArray($reportHeader, null, 'A' . ($sheet->getHighestRow() + 2)); |
84 | } |
85 | |
86 | public function writeTotalsRow(ReportEntity $report, $sheet, $datePatternTotals) |
87 | { |
88 | if ('totals' == end($report->data)['date']) { |
89 | $totals = array_pop($report->data); |
90 | $dateString = $report->firstDay->year . '-' . $report->firstDay->month . '-' . $report->firstDay->day; |
91 | $dateCol1 = ('MMMM' == $datePatternTotals) ? |
92 | $this->getFormatedDates($this->setDateTime($dateString), $datePatternTotals) : |
93 | null; |
94 | $dateCol2 = $this->setDateTime($dateString)->format('Y'); |
95 | $reportTotal = [$dateCol1, $dateCol2]; |
96 | |
97 | $columnIndex = 0; |
98 | foreach ($totals as $key => $item) { |
99 | $columnIndex++; |
100 | if ($columnIndex == 3 || $columnIndex == 4) { |
101 | continue; |
102 | } |
103 | |
104 | if (! in_array($key, static::$ignoreColumns) && 'date' != $key) { |
105 | $reportTotal[] = (string)($item); |
106 | } |
107 | } |
108 | |
109 | // Calculations for the new columns |
110 | $end = array_splice($reportTotal, -1); |
111 | |
112 | $reportTotal[] = $this->calculateNoAppointment($totals); |
113 | $reportTotal[] = $this->calculateMissedNoAppointment($totals); |
114 | |
115 | $reportTotal = array_merge($reportTotal, $end); |
116 | |
117 | $sheet->fromArray($reportTotal, null, 'A' . ($sheet->getHighestRow() + 1)); |
118 | } |
119 | } |
120 | |
121 | public function writeReportData(ReportEntity $report, $sheet, $datePatternCol1, $datePatternCol2) |
122 | { |
123 | $reportData = []; |
124 | |
125 | foreach ($report->data as $row => $entry) { |
126 | $processedRow = []; |
127 | $columnIndex = 0; |
128 | |
129 | foreach ($entry as $key => $item) { |
130 | $columnIndex++; |
131 | if ($columnIndex == 3 || $columnIndex == 4) { |
132 | continue; |
133 | } |
134 | |
135 | if (!in_array($key, static::$ignoreColumns)) { |
136 | if ('date' == $key) { |
137 | $dateCol1 = $this->getFormatedDates($this->setDateTime($item), $datePatternCol1); |
138 | $item = $this->getFormatedDates($this->setDateTime($item), $datePatternCol2); |
139 | $processedRow[] = $dateCol1; |
140 | } |
141 | $processedRow[] = (is_numeric($item)) ? (string)($item) : $item; |
142 | } |
143 | } |
144 | |
145 | // Calculations for the new columns |
146 | $end = array_splice($processedRow, -1); |
147 | |
148 | $processedRow[] = $this->calculateNoAppointmentForRow($entry); |
149 | $processedRow[] = $this->calculateMissedNoAppointmentForRow($entry); |
150 | |
151 | $processedRow = array_merge($processedRow, $end); |
152 | |
153 | $reportData[$row] = $processedRow; |
154 | } |
155 | |
156 | $sheet->fromArray($reportData, null, 'A' . ($sheet->getHighestRow() + 1)); |
157 | } |
158 | |
159 | private function calculateNoAppointment($totals) |
160 | { |
161 | if (isset($totals['clientscount']) && isset($totals['withappointment'])) { |
162 | return (string) ($totals['clientscount'] - $totals['withappointment']); |
163 | } |
164 | return 'N/A'; |
165 | } |
166 | |
167 | private function calculateMissedNoAppointment($totals) |
168 | { |
169 | if (isset($totals['missed']) && isset($totals['missedwithappointment'])) { |
170 | return (string) ($totals['missed'] - $totals['missedwithappointment']); |
171 | } |
172 | return 'N/A'; |
173 | } |
174 | |
175 | private function calculateNoAppointmentForRow($entry) |
176 | { |
177 | if (isset($entry['clientscount']) && isset($entry['withappointment'])) { |
178 | return (string) ($entry['clientscount'] - $entry['withappointment']); |
179 | } |
180 | return 'N/A'; |
181 | } |
182 | |
183 | private function calculateMissedNoAppointmentForRow($entry) |
184 | { |
185 | if (isset($entry['missed']) && isset($entry['missedwithappointment'])) { |
186 | return (string) ($entry['missed'] - $entry['missedwithappointment']); |
187 | } |
188 | return 'N/A'; |
189 | } |
190 | } |