Total Complexity | 93 |
Total Lines | 473 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ReportTaxes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ReportTaxes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class ReportTaxes extends Controller |
||
39 | { |
||
40 | const MAX_TOTAL_DIFF = 0.05; |
||
41 | |||
42 | /** @var string */ |
||
43 | public $coddivisa; |
||
44 | |||
45 | /** @var string */ |
||
46 | public $codpais; |
||
47 | |||
48 | /** @var string */ |
||
49 | public $codserie; |
||
50 | |||
51 | /** @var string */ |
||
52 | public $datefrom; |
||
53 | |||
54 | /** @var string */ |
||
55 | public $dateto; |
||
56 | |||
57 | /** @var Divisa */ |
||
58 | public $divisa; |
||
59 | |||
60 | /** @var string */ |
||
61 | public $format; |
||
62 | |||
63 | /** @var int */ |
||
64 | public $idempresa; |
||
65 | |||
66 | /** @var Pais */ |
||
67 | public $pais; |
||
68 | |||
69 | /** @var Serie */ |
||
70 | public $serie; |
||
71 | |||
72 | /** @var string */ |
||
73 | public $source; |
||
74 | |||
75 | /** @var string */ |
||
76 | public $typeDate; |
||
77 | |||
78 | public function getPageData(): array |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param Response $response |
||
89 | * @param User $user |
||
90 | * @param ControllerPermissions $permissions |
||
91 | */ |
||
92 | public function privateCore(&$response, $user, $permissions) |
||
103 | } |
||
104 | } |
||
105 | |||
106 | protected function exportAction(): void |
||
107 | { |
||
108 | $i18n = Tools::lang(); |
||
109 | $data = $this->getReportData(); |
||
110 | if (empty($data)) { |
||
111 | Tools::log()->warning('no-data'); |
||
112 | return; |
||
113 | } |
||
114 | |||
115 | // prepare lines |
||
116 | $lastCode = ''; |
||
117 | $lines = []; |
||
118 | foreach ($data as $row) { |
||
119 | $hide = $row['codigo'] === $lastCode && $this->format === 'PDF'; |
||
120 | $num2title = $this->source === 'sales' ? 'number2' : 'numproveedor'; |
||
121 | |||
122 | $lines[] = [ |
||
123 | $i18n->trans('serie') => $hide ? '' : $row['codserie'], |
||
124 | $i18n->trans('code') => $hide ? '' : $row['codigo'], |
||
125 | $i18n->trans($num2title) => $hide ? '' : $row['numero2'], |
||
126 | $i18n->trans('date') => $hide ? '' : Tools::date($row['fecha']), |
||
127 | $i18n->trans('name') => $hide ? '' : Tools::fixHtml($row['nombre']), |
||
128 | $i18n->trans('cifnif') => $hide ? '' : $row['cifnif'], |
||
129 | $i18n->trans('net') => $this->exportFieldFormat('number', $row['neto']), |
||
130 | $i18n->trans('pct-tax') => $this->exportFieldFormat('number', $row['iva']), |
||
131 | $i18n->trans('tax') => $this->exportFieldFormat('number', $row['totaliva']), |
||
132 | $i18n->trans('pct-surcharge') => $this->exportFieldFormat('number', $row['recargo']), |
||
133 | $i18n->trans('surcharge') => $this->exportFieldFormat('number', $row['totalrecargo']), |
||
134 | $i18n->trans('pct-irpf') => $this->exportFieldFormat('number', $row['irpf']), |
||
135 | $i18n->trans('irpf') => $this->exportFieldFormat('number', $row['totalirpf']), |
||
136 | $i18n->trans('supplied-amount') => $this->exportFieldFormat('number', $row['suplidos']), |
||
137 | $i18n->trans('total') => $hide ? '' : $this->exportFieldFormat('number', $row['total']) |
||
138 | ]; |
||
139 | |||
140 | $lastCode = $row['codigo']; |
||
141 | } |
||
142 | |||
143 | $totalsData = $this->getTotals($data); |
||
144 | if (false === $this->validateTotals($totalsData)) { |
||
145 | return; |
||
146 | } |
||
147 | |||
148 | // prepare totals |
||
149 | $totals = []; |
||
150 | foreach ($totalsData as $row) { |
||
151 | $total = $row['neto'] + $row['totaliva'] + $row['totalrecargo'] - $row['totalirpf'] - $row['suplidos']; |
||
152 | $totals[] = [ |
||
153 | $i18n->trans('net') => $this->exportFieldFormat('number', $row['neto']), |
||
154 | $i18n->trans('pct-tax') => $this->exportFieldFormat('percentage', $row['iva']), |
||
155 | $i18n->trans('tax') => $this->exportFieldFormat('number', $row['totaliva']), |
||
156 | $i18n->trans('pct-surcharge') => $this->exportFieldFormat('percentage', $row['recargo']), |
||
157 | $i18n->trans('surcharge') => $this->exportFieldFormat('number', $row['totalrecargo']), |
||
158 | $i18n->trans('pct-irpf') => $this->exportFieldFormat('percentage', $row['irpf']), |
||
159 | $i18n->trans('irpf') => $this->exportFieldFormat('number', $row['totalirpf']), |
||
160 | $i18n->trans('supplied-amount') => $this->exportFieldFormat('number', $row['suplidos']), |
||
161 | $i18n->trans('total') => $this->exportFieldFormat('number', $total) |
||
162 | ]; |
||
163 | } |
||
164 | |||
165 | $this->setTemplate(false); |
||
166 | $this->processLayout($lines, $totals); |
||
167 | } |
||
168 | |||
169 | protected function exportFieldFormat(string $format, string $value): string |
||
170 | { |
||
171 | switch ($format) { |
||
172 | case 'number': |
||
173 | return $this->format === 'PDF' ? Tools::number($value) : $value; |
||
|
|||
174 | |||
175 | case 'percentage': |
||
176 | return $this->format === 'PDF' ? Tools::number($value) . ' %' : $value; |
||
177 | |||
178 | default: |
||
179 | return $value; |
||
180 | } |
||
181 | } |
||
182 | |||
183 | protected function getQuarterDate(bool $start): string |
||
184 | { |
||
185 | $month = (int)date('m'); |
||
186 | |||
187 | // si la fecha actual es de enero, seleccionamos el trimestre anterior |
||
188 | if ($month === 1) { |
||
189 | return $start ? |
||
190 | date('Y-10-01', strtotime('-1 year')) : |
||
191 | date('Y-12-31', strtotime('-1 year')); |
||
192 | } |
||
193 | |||
194 | // comprobamos si la fecha actual está en el primer trimestre o justo en el siguiente mes |
||
195 | if ($month >= 1 && $month <= 4) { |
||
196 | return $start ? date('Y-01-01') : date('Y-03-31'); |
||
197 | } |
||
198 | |||
199 | // comprobamos si la fecha actual está en el segundo trimestre o justo en el siguiente mes |
||
200 | if ($month >= 4 && $month <= 7) { |
||
201 | return $start ? date('Y-04-01') : date('Y-06-30'); |
||
202 | } |
||
203 | |||
204 | // comprobamos si la fecha actual está en el tercer trimestre o justo en el siguiente mes |
||
205 | if ($month >= 7 && $month <= 10) { |
||
206 | return $start ? date('Y-07-01') : date('Y-09-30'); |
||
207 | } |
||
208 | |||
209 | // la fecha actual está en el cuarto trimestre |
||
210 | return $start ? date('Y-10-01') : date('Y-12-31'); |
||
211 | } |
||
212 | |||
213 | protected function getReportData(): array |
||
214 | { |
||
215 | $sql = ''; |
||
216 | $numCol = strtolower(FS_DB_TYPE) == 'postgresql' ? 'CAST(f.numero as integer)' : 'CAST(f.numero as unsigned)'; |
||
217 | $columnDate = $this->typeDate === 'create' ? 'f.fecha' : 'COALESCE(f.fechadevengo, f.fecha)'; |
||
218 | switch ($this->source) { |
||
219 | case 'purchases': |
||
220 | $sql .= 'SELECT f.codserie, f.codigo, f.numproveedor AS numero2, f.fecha, f.fechadevengo, f.nombre, f.cifnif, l.pvptotal,' |
||
221 | . ' l.iva, l.recargo, l.irpf, l.suplido, f.dtopor1, f.dtopor2, f.total' |
||
222 | . ' FROM lineasfacturasprov AS l' |
||
223 | . ' LEFT JOIN facturasprov AS f ON l.idfactura = f.idfactura ' |
||
224 | . ' WHERE f.idempresa = ' . $this->dataBase->var2str($this->idempresa) |
||
225 | . ' AND ' . $columnDate . ' >= ' . $this->dataBase->var2str($this->datefrom) |
||
226 | . ' AND ' . $columnDate . ' <= ' . $this->dataBase->var2str($this->dateto) |
||
227 | . ' AND (l.pvptotal <> 0.00 OR l.iva <> 0.00)' |
||
228 | . ' AND f.coddivisa = ' . $this->dataBase->var2str($this->coddivisa); |
||
229 | break; |
||
230 | |||
231 | case 'sales': |
||
232 | $sql .= 'SELECT f.codserie, f.codigo, f.numero2, f.fecha, f.fechadevengo, f.nombrecliente AS nombre, f.cifnif, l.pvptotal,' |
||
233 | . ' l.iva, l.recargo, l.irpf, l.suplido, f.dtopor1, f.dtopor2, f.total' |
||
234 | . ' FROM lineasfacturascli AS l' |
||
235 | . ' LEFT JOIN facturascli AS f ON l.idfactura = f.idfactura ' |
||
236 | . ' WHERE f.idempresa = ' . $this->dataBase->var2str($this->idempresa) |
||
237 | . ' AND ' . $columnDate . ' >= ' . $this->dataBase->var2str($this->datefrom) |
||
238 | . ' AND ' . $columnDate . ' <= ' . $this->dataBase->var2str($this->dateto) |
||
239 | . ' AND (l.pvptotal <> 0.00 OR l.iva <> 0.00)' |
||
240 | . ' AND f.coddivisa = ' . $this->dataBase->var2str($this->coddivisa); |
||
241 | if ($this->codpais) { |
||
242 | $sql .= ' AND codpais = ' . $this->dataBase->var2str($this->codpais); |
||
243 | } |
||
244 | break; |
||
245 | |||
246 | default: |
||
247 | Tools::log()->warning('wrong-source'); |
||
248 | return []; |
||
249 | } |
||
250 | if ($this->codserie) { |
||
251 | $sql .= ' AND codserie = ' . $this->dataBase->var2str($this->codserie); |
||
252 | } |
||
253 | $sql .= ' ORDER BY ' . $columnDate . ', ' . $numCol . ' ASC;'; |
||
254 | |||
255 | $data = []; |
||
256 | foreach ($this->dataBase->select($sql) as $row) { |
||
257 | $pvpTotal = floatval($row['pvptotal']) * (100 - floatval($row['dtopor1'])) * (100 - floatval($row['dtopor2'])) / 10000; |
||
258 | $code = $row['codigo'] . '-' . $row['iva'] . '-' . $row['recargo'] . '-' . $row['irpf'] . '-' . $row['suplido']; |
||
259 | if (isset($data[$code])) { |
||
260 | $data[$code]['neto'] += $row['suplido'] ? 0 : $pvpTotal; |
||
261 | $data[$code]['totaliva'] += $row['suplido'] ? 0 : (float)$row['iva'] * $pvpTotal / 100; |
||
262 | $data[$code]['totalrecargo'] += $row['suplido'] ? 0 : (float)$row['recargo'] * $pvpTotal / 100; |
||
263 | $data[$code]['totalirpf'] += $row['suplido'] ? 0 : (float)$row['irpf'] * $pvpTotal / 100; |
||
264 | $data[$code]['suplidos'] += $row['suplido'] ? $pvpTotal : 0; |
||
265 | continue; |
||
266 | } |
||
267 | |||
268 | $data[$code] = [ |
||
269 | 'codserie' => $row['codserie'], |
||
270 | 'codigo' => $row['codigo'], |
||
271 | 'numero2' => $row['numero2'], |
||
272 | 'fecha' => $this->typeDate == 'create' ? |
||
273 | $row['fecha'] : |
||
274 | $row['fechadevengo'] ?? $row['fecha'], |
||
275 | 'nombre' => $row['nombre'], |
||
276 | 'cifnif' => $row['cifnif'], |
||
277 | 'neto' => $row['suplido'] ? 0 : $pvpTotal, |
||
278 | 'iva' => $row['suplido'] ? 0 : (float)$row['iva'], |
||
279 | 'totaliva' => $row['suplido'] ? 0 : (float)$row['iva'] * $pvpTotal / 100, |
||
280 | 'recargo' => $row['suplido'] ? 0 : (float)$row['recargo'], |
||
281 | 'totalrecargo' => $row['suplido'] ? 0 : (float)$row['recargo'] * $pvpTotal / 100, |
||
282 | 'irpf' => $row['suplido'] ? 0 : (float)$row['irpf'], |
||
283 | 'totalirpf' => $row['suplido'] ? 0 : (float)$row['irpf'] * $pvpTotal / 100, |
||
284 | 'suplidos' => $row['suplido'] ? $pvpTotal : 0, |
||
285 | 'total' => (float)$row['total'] |
||
286 | ]; |
||
287 | } |
||
288 | |||
289 | // round |
||
290 | foreach ($data as $key => $value) { |
||
291 | $data[$key]['neto'] = round($value['neto'], FS_NF0); |
||
292 | $data[$key]['totaliva'] = round($value['totaliva'], FS_NF0); |
||
293 | $data[$key]['totalrecargo'] = round($value['totalrecargo'], FS_NF0); |
||
294 | $data[$key]['totalirpf'] = round($value['totalirpf'], FS_NF0); |
||
295 | $data[$key]['suplidos'] = round($value['suplidos'], FS_NF0); |
||
296 | } |
||
297 | |||
298 | return $data; |
||
299 | } |
||
300 | |||
301 | protected function getTotals(array $data): array |
||
302 | { |
||
303 | $totals = []; |
||
304 | foreach ($data as $row) { |
||
305 | $code = $row['iva'] . '-' . $row['recargo'] . '-' . $row['irpf']; |
||
306 | if (isset($totals[$code])) { |
||
307 | $totals[$code]['neto'] += $row['neto']; |
||
308 | $totals[$code]['totaliva'] += $row['totaliva']; |
||
309 | $totals[$code]['totalrecargo'] += $row['totalrecargo']; |
||
310 | $totals[$code]['totalirpf'] += $row['totalirpf']; |
||
311 | $totals[$code]['suplidos'] += $row['suplidos']; |
||
312 | continue; |
||
313 | } |
||
314 | |||
315 | $totals[$code] = [ |
||
316 | 'neto' => $row['neto'], |
||
317 | 'iva' => $row['iva'], |
||
318 | 'totaliva' => $row['totaliva'], |
||
319 | 'recargo' => $row['recargo'], |
||
320 | 'totalrecargo' => $row['totalrecargo'], |
||
321 | 'irpf' => $row['irpf'], |
||
322 | 'totalirpf' => $row['totalirpf'], |
||
323 | 'suplidos' => $row['suplidos'] |
||
324 | ]; |
||
325 | } |
||
326 | |||
327 | return $totals; |
||
328 | } |
||
329 | |||
330 | protected function initFilters(): void |
||
331 | { |
||
332 | $this->coddivisa = $this->request->request->get( |
||
333 | 'coddivisa', |
||
334 | Tools::settings('default', 'coddivisa') |
||
335 | ); |
||
336 | |||
337 | $this->codpais = $this->request->request->get('codpais', ''); |
||
338 | $this->codserie = $this->request->request->get('codserie', ''); |
||
339 | $this->datefrom = $this->request->request->get('datefrom', $this->getQuarterDate(true)); |
||
340 | $this->dateto = $this->request->request->get('dateto', $this->getQuarterDate(false)); |
||
341 | |||
342 | $this->idempresa = (int)$this->request->request->get( |
||
343 | 'idempresa', |
||
344 | Tools::settings('default', 'idempresa') |
||
345 | ); |
||
346 | |||
347 | $this->format = $this->request->request->get('format'); |
||
348 | $this->source = $this->request->request->get('source'); |
||
349 | $this->typeDate = $this->request->request->get('type-date'); |
||
350 | } |
||
351 | |||
352 | protected function processLayout(array &$lines, array &$totals): void |
||
353 | { |
||
354 | $i18n = Tools::lang(); |
||
355 | $exportManager = new ExportManager(); |
||
356 | $exportManager->setOrientation('landscape'); |
||
357 | $exportManager->newDoc($this->format, $i18n->trans('taxes')); |
||
358 | |||
359 | // add information table |
||
360 | $exportManager->addTablePage( |
||
361 | [ |
||
362 | $i18n->trans('report'), |
||
363 | $i18n->trans('currency'), |
||
364 | $i18n->trans('date'), |
||
365 | $i18n->trans('from-date'), |
||
366 | $i18n->trans('until-date') |
||
367 | ], |
||
368 | [[ |
||
369 | $i18n->trans('report') => $i18n->trans('taxes') . ' ' . $i18n->trans($this->source), |
||
370 | $i18n->trans('currency') => Divisas::get($this->coddivisa)->descripcion, |
||
371 | $i18n->trans('date') => $i18n->trans($this->typeDate === 'create' ? 'creation-date' : 'accrual-date'), |
||
372 | $i18n->trans('from-date') => Tools::date($this->datefrom), |
||
373 | $i18n->trans('until-date') => Tools::date($this->dateto) |
||
374 | ]] |
||
375 | ); |
||
376 | |||
377 | $options = [ |
||
378 | $i18n->trans('net') => ['display' => 'right'], |
||
379 | $i18n->trans('pct-tax') => ['display' => 'right'], |
||
380 | $i18n->trans('tax') => ['display' => 'right'], |
||
381 | $i18n->trans('pct-surcharge') => ['display' => 'right'], |
||
382 | $i18n->trans('surcharge') => ['display' => 'right'], |
||
383 | $i18n->trans('pct-irpf') => ['display' => 'right'], |
||
384 | $i18n->trans('irpf') => ['display' => 'right'], |
||
385 | $i18n->trans('supplied-amount') => ['display' => 'right'], |
||
386 | $i18n->trans('total') => ['display' => 'right'] |
||
387 | ]; |
||
388 | |||
389 | // add lines table |
||
390 | $this->reduceLines($lines); |
||
391 | $headers = empty($lines) ? [] : array_keys(end($lines)); |
||
392 | $exportManager->addTablePage($headers, $lines, $options); |
||
393 | |||
394 | // add totals table |
||
395 | $headTotals = empty($totals) ? [] : array_keys(end($totals)); |
||
396 | $exportManager->addTablePage($headTotals, $totals, $options); |
||
397 | |||
398 | $exportManager->show($this->response); |
||
399 | } |
||
400 | |||
401 | protected function reduceLines(array &$lines): void |
||
455 | } |
||
456 | } |
||
457 | } |
||
458 | |||
459 | protected function validateTotals(array $totalsData): bool |
||
511 | } |
||
512 | } |
||
513 |