| Total Complexity | 104 |
| Total Lines | 807 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like inventarios_balances 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 inventarios_balances, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class inventarios_balances |
||
| 23 | { |
||
| 24 | |||
| 25 | private $balance; |
||
| 26 | private $balance_cuenta_a; |
||
| 27 | private $db; |
||
| 28 | private $empresa; |
||
| 29 | |||
| 30 | public function __construct(&$db) |
||
| 31 | { |
||
| 32 | $this->balance = new balance(); |
||
|
|
|||
| 33 | $this->balance_cuenta_a = new balance_cuenta_a(); |
||
| 34 | $this->db = $db; |
||
| 35 | $this->empresa = new empresa(); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Función para ejecutar en el cron.php |
||
| 40 | */ |
||
| 41 | public function cron_job() |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * Como es un proceso que tarda mucho, solamente comprobamos los dos primeros |
||
| 45 | * ejercicios de la lista (los más nuevos), más uno aleatorio. |
||
| 46 | */ |
||
| 47 | $ejercicio = new ejercicio(); |
||
| 48 | $ejercicios = $ejercicio->all(); |
||
| 49 | $random = mt_rand(0, count($ejercicios) - 1); |
||
| 50 | foreach ($ejercicios as $num => $eje) { |
||
| 51 | if ($num < 2 || $num == $random) { |
||
| 52 | $this->generar_libro($eje); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Genera el libro de inventarios y balances de un ejercicio. |
||
| 59 | * @param ejercicio $eje |
||
| 60 | */ |
||
| 61 | private function generar_libro(&$eje) |
||
| 62 | { |
||
| 63 | if ($eje) { |
||
| 64 | if (!file_exists('tmp/' . FS_TMP_NAME . 'inventarios_balances')) { |
||
| 65 | mkdir('tmp/' . FS_TMP_NAME . 'inventarios_balances'); |
||
| 66 | } |
||
| 67 | |||
| 68 | if (!file_exists('tmp/' . FS_TMP_NAME . 'inventarios_balances/' . $eje->codejercicio . '.pdf')) { |
||
| 69 | echo '.' . $eje->codejercicio . '.'; |
||
| 70 | |||
| 71 | $pdf_doc = new fs_pdf(); |
||
| 72 | $pdf_doc->pdf->addInfo('Title', 'Libro de inventarios y balances de ' . fs_fix_html($this->empresa->nombre)); |
||
| 73 | $pdf_doc->pdf->addInfo('Subject', 'Libro de inventarios y balances de ' . fs_fix_html($this->empresa->nombre)); |
||
| 74 | $pdf_doc->pdf->addInfo('Author', fs_fix_html($this->empresa->nombre)); |
||
| 75 | $pdf_doc->pdf->ezStartPageNumbers(580, 10, 10, 'left', '{PAGENUM} de {TOTALPAGENUM}'); |
||
| 76 | |||
| 77 | $excluir = FALSE; |
||
| 78 | if (isset($eje->idasientocierre) && isset($eje->idasientopyg)) { |
||
| 79 | $excluir = array($eje->idasientocierre, $eje->idasientopyg); |
||
| 80 | } |
||
| 81 | |||
| 82 | $this->sumas_y_saldos($pdf_doc, $eje, 'de apertura a cierre', $eje->fechainicio, $eje->fechafin, $excluir, FALSE); |
||
| 83 | $this->sumas_y_saldos($pdf_doc, $eje, 'de apertura a apertura', $eje->fechainicio, $eje->fechainicio); |
||
| 84 | $this->sumas_y_saldos($pdf_doc, $eje, 'del 1º trimestre', $eje->fechainicio, '31-3-' . $eje->year()); |
||
| 85 | $this->sumas_y_saldos($pdf_doc, $eje, 'del 2º trimestre', '1-4-' . $eje->year(), '30-6-' . $eje->year()); |
||
| 86 | $this->sumas_y_saldos($pdf_doc, $eje, 'del 3º trimestre', '1-7-' . $eje->year(), '30-9-' . $eje->year()); |
||
| 87 | $this->sumas_y_saldos($pdf_doc, $eje, 'del 4º trimestre', '1-10-' . $eje->year(), $eje->fechafin); |
||
| 88 | $this->perdidas_y_ganancias($pdf_doc, $eje); |
||
| 89 | $this->situacion($pdf_doc, $eje); |
||
| 90 | |||
| 91 | $pdf_doc->save('tmp/' . FS_TMP_NAME . 'inventarios_balances/' . $eje->codejercicio . '.pdf'); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Genera el balance de pérdidas y ganancias de un ejercicio. |
||
| 98 | * @param string $codeje |
||
| 99 | */ |
||
| 100 | public function generar_pyg($codeje) |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Genera el balance de situación. |
||
| 120 | * @param string $codeje |
||
| 121 | */ |
||
| 122 | public function generar_sit($codeje) |
||
| 123 | { |
||
| 124 | $ejercicio = new ejercicio(); |
||
| 125 | |||
| 126 | $eje0 = $ejercicio->get($codeje); |
||
| 127 | if ($eje0) { |
||
| 128 | $pdf_doc = new fs_pdf(); |
||
| 129 | $pdf_doc->pdf->addInfo('Title', 'Balance de pérdidas y ganancias de ' . fs_fix_html($this->empresa->nombre)); |
||
| 130 | $pdf_doc->pdf->addInfo('Subject', 'Balance de pérdidas y ganancias de ' . fs_fix_html($this->empresa->nombre)); |
||
| 131 | $pdf_doc->pdf->addInfo('Author', fs_fix_html($this->empresa->nombre)); |
||
| 132 | $pdf_doc->pdf->ezStartPageNumbers(580, 10, 10, 'left', '{PAGENUM} de {TOTALPAGENUM}'); |
||
| 133 | |||
| 134 | $this->situacion($pdf_doc, $eje0, FALSE); |
||
| 135 | |||
| 136 | $pdf_doc->show(); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Función auxiliar para generar el balance de sumas y saldos de un ejercicio y unas fechas concretas. |
||
| 142 | * Este informe muestra los saldos (distintos de cero) de cada cuenta y subcuenta |
||
| 143 | * por periodos, pero siempre excluyendo los asientos de cierre y pérdidas y ganancias. |
||
| 144 | * @param fs_pdf $pdf_doc |
||
| 145 | * @param ejercicio $eje |
||
| 146 | * @param string $titulo |
||
| 147 | * @param string $fechaini |
||
| 148 | * @param string $fechafin |
||
| 149 | * @param array|boolean $excluir |
||
| 150 | * @param boolean $np |
||
| 151 | */ |
||
| 152 | public function sumas_y_saldos(&$pdf_doc, &$eje, $titulo, $fechaini, $fechafin, $excluir = FALSE, $np = TRUE) |
||
| 350 | ) |
||
| 351 | ); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Función auxiliar para generar el balance de pérdidas y ganancias. |
||
| 357 | * Este informe se confecciona a partir de las cuentas que señalan los códigos |
||
| 358 | * de balance que empiezan por PG. |
||
| 359 | * @param fs_pdf $pdf_doc |
||
| 360 | * @param ejercicio $eje |
||
| 361 | * @param boolean $np |
||
| 362 | */ |
||
| 363 | private function perdidas_y_ganancias(&$pdf_doc, &$eje, $np = TRUE) |
||
| 364 | { |
||
| 365 | if ($np) { |
||
| 366 | $pdf_doc->pdf->ezNewPage(); |
||
| 367 | } |
||
| 368 | |||
| 369 | $pdf_doc->pdf->ezText(fs_fix_html($this->empresa->nombre) . " - Cuenta de pérdidas y ganancias abreviada del ejercicio " . $eje->year() . ".\n\n", 13); |
||
| 370 | |||
| 371 | /// necesitamos el ejercicio anterior |
||
| 372 | $eje0 = $eje->get_by_fecha('1-1-' . (intval($eje->year()) - 1), FALSE, FALSE); |
||
| 373 | if ($eje0) { |
||
| 374 | /// creamos las cabeceras de la tabla |
||
| 375 | $pdf_doc->new_table(); |
||
| 376 | $pdf_doc->add_table_header( |
||
| 377 | array( |
||
| 378 | 'descripcion' => '<b>Descripción</b>', |
||
| 379 | 'actual' => '<b>' . $eje->year() . '</b>', |
||
| 380 | 'anterior' => '<b>' . $eje0->year() . '</b>' |
||
| 381 | ) |
||
| 382 | ); |
||
| 383 | |||
| 384 | $balances = $this->balance->all(); |
||
| 385 | $num = 1; |
||
| 386 | $continuar = TRUE; |
||
| 387 | $totales = array( |
||
| 388 | $eje->year() => array('a' => 0, 'b' => 0, 'c' => 0, 'd' => 0), |
||
| 389 | $eje0->year() => array('a' => 0, 'b' => 0, 'c' => 0, 'd' => 0) |
||
| 390 | ); |
||
| 391 | while ($continuar) { |
||
| 392 | if ($num == 12) { |
||
| 393 | $pdf_doc->add_table_row( |
||
| 394 | array( |
||
| 395 | 'descripcion' => "\n<b>A) RESULTADOS DE EXPLOTACIÓN (1+2+3+4+5+6+7+8+9+10+11)</b>", |
||
| 396 | 'actual' => "\n<b>" . $this->show_numero($totales[$eje->year()]['a']) . '</b>', |
||
| 397 | 'anterior' => "\n<b>" . $this->show_numero($totales[$eje0->year()]['a']) . '</b>' |
||
| 398 | ) |
||
| 399 | ); |
||
| 400 | } else if ($num == 17) { |
||
| 401 | $pdf_doc->add_table_row( |
||
| 402 | array( |
||
| 403 | 'descripcion' => "\n<b>B) RESULTADO FINANCIERO (12+13+14+15+16)</b>", |
||
| 404 | 'actual' => "\n<b>" . $this->show_numero($totales[$eje->year()]['b']) . '</b>', |
||
| 405 | 'anterior' => "\n<b>" . $this->show_numero($totales[$eje0->year()]['b']) . '</b>' |
||
| 406 | ) |
||
| 407 | ); |
||
| 408 | $pdf_doc->add_table_row( |
||
| 409 | array( |
||
| 410 | 'descripcion' => "<b>C) RESULTADO ANTES DE IMPUESTOS (A+B)</b>", |
||
| 411 | 'actual' => '<b>' . $this->show_numero($totales[$eje->year()]['c']) . '</b>', |
||
| 412 | 'anterior' => '<b>' . $this->show_numero($totales[$eje0->year()]['c']) . '</b>' |
||
| 413 | ) |
||
| 414 | ); |
||
| 415 | } |
||
| 416 | |||
| 417 | $encontrado = FALSE; |
||
| 418 | foreach ($balances as $bal) { |
||
| 419 | if ($bal->naturaleza == 'PG' && strstr($bal->codbalance, 'PG')) { |
||
| 420 | $saldo1 = $this->get_saldo_balance('PG', $eje); |
||
| 421 | $saldo0 = $this->get_saldo_balance('PG', $eje0); |
||
| 422 | |||
| 423 | /// añadimos la fila |
||
| 424 | $pdf_doc->add_table_row( |
||
| 425 | array( |
||
| 426 | 'descripcion' => $bal->descripcion2, |
||
| 427 | 'actual' => $this->show_numero($saldo1), |
||
| 428 | 'anterior' => $this->show_numero($saldo0) |
||
| 429 | ) |
||
| 430 | ); |
||
| 431 | |||
| 432 | /// sumamos donde corresponda |
||
| 433 | if ($num <= 11) { |
||
| 434 | $totales[$eje->year()]['a'] += $saldo1; |
||
| 435 | $totales[$eje0->year()]['a'] += $saldo0; |
||
| 436 | } else if ($num <= 16) { |
||
| 437 | $totales[$eje->year()]['b'] += $saldo1; |
||
| 438 | $totales[$eje0->year()]['b'] += $saldo0; |
||
| 439 | |||
| 440 | $totales[$eje->year()]['c'] = $totales[$eje->year()]['a'] + $totales[$eje->year()]['b']; |
||
| 441 | $totales[$eje0->year()]['c'] = $totales[$eje0->year()]['a'] + $totales[$eje0->year()]['b']; |
||
| 442 | } else if ($num == 17) { |
||
| 443 | $totales[$eje->year()]['d'] = $totales[$eje->year()]['c'] + $saldo1; |
||
| 444 | $totales[$eje0->year()]['d'] = $totales[$eje0->year()]['c'] + $saldo0; |
||
| 445 | } |
||
| 446 | |||
| 447 | $encontrado = TRUE; |
||
| 448 | $num++; |
||
| 449 | break; |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | $continuar = $encontrado; |
||
| 454 | } |
||
| 455 | |||
| 456 | $pdf_doc->add_table_row( |
||
| 457 | array( |
||
| 458 | 'descripcion' => "\n<b>D) RESULTADO DEL EJERCICIO (C+17)</b>", |
||
| 459 | 'actual' => "\n<b>" . $this->show_numero($totales[$eje->year()]['d']) . '</b>', |
||
| 460 | 'anterior' => "\n<b>" . $this->show_numero($totales[$eje0->year()]['d']) . '</b>' |
||
| 461 | ) |
||
| 462 | ); |
||
| 463 | |||
| 464 | $pdf_doc->save_table( |
||
| 465 | array( |
||
| 466 | 'fontSize' => 12, |
||
| 467 | 'cols' => array( |
||
| 468 | 'actual' => array('justification' => 'right'), |
||
| 469 | 'anterior' => array('justification' => 'right') |
||
| 470 | ), |
||
| 471 | 'width' => 540, |
||
| 472 | 'shaded' => 0 |
||
| 473 | ) |
||
| 474 | ); |
||
| 475 | } else { |
||
| 476 | /// creamos las cabeceras de la tabla |
||
| 477 | $pdf_doc->new_table(); |
||
| 478 | $pdf_doc->add_table_header( |
||
| 479 | array( |
||
| 480 | 'descripcion' => '<b>Descripción</b>', |
||
| 481 | 'actual' => '<b>' . $eje->year() . '</b>' |
||
| 482 | ) |
||
| 483 | ); |
||
| 484 | |||
| 485 | $balances = $this->balance->all(); |
||
| 486 | $num = 1; |
||
| 487 | $continuar = TRUE; |
||
| 488 | $totales = array($eje->year() => array('a' => 0, 'b' => 0, 'c' => 0, 'd' => 0)); |
||
| 489 | while ($continuar) { |
||
| 490 | if ($num == 12) { |
||
| 491 | $pdf_doc->add_table_row( |
||
| 492 | array( |
||
| 493 | 'descripcion' => "\n<b>A) RESULTADOS DE EXPLOTACIÓN (1+2+3+4+5+6+7+8)</b>", |
||
| 494 | 'actual' => "\n<b>" . $this->show_numero($totales[$eje->year()]['a']) . '</b>' |
||
| 495 | ) |
||
| 496 | ); |
||
| 497 | } else if ($num == 17) { |
||
| 498 | $pdf_doc->add_table_row( |
||
| 499 | array( |
||
| 500 | 'descripcion' => "\n<b>B) RESULTADO FINANCIERO (P1+P2+P3)</b>", |
||
| 501 | 'actual' => "\n<b>" . $this->show_numero($totales[$eje->year()]['b']) . '</b>' |
||
| 502 | ) |
||
| 503 | ); |
||
| 504 | $pdf_doc->add_table_row( |
||
| 505 | array( |
||
| 506 | 'descripcion' => "<b>C) RESULTADO ANTES DE IMPUESTOS (A+B)</b>", |
||
| 507 | 'actual' => '<b>' . $this->show_numero($totales[$eje->year()]['c']) . '</b>' |
||
| 508 | ) |
||
| 509 | ); |
||
| 510 | } |
||
| 511 | |||
| 512 | $encontrado = FALSE; |
||
| 513 | foreach ($balances as $bal) { |
||
| 514 | if ($bal->naturaleza == 'PG' && strstr($bal->codbalance, 'PG-A-' . $num . '-')) { |
||
| 515 | $saldo1 = $this->get_saldo_balance('PG-A-' . $num . '-', $eje); |
||
| 516 | |||
| 517 | /// añadimos la fila |
||
| 518 | $pdf_doc->add_table_row( |
||
| 519 | array( |
||
| 520 | 'descripcion' => $bal->descripcion2, |
||
| 521 | 'actual' => $this->show_numero($saldo1) |
||
| 522 | ) |
||
| 523 | ); |
||
| 524 | |||
| 525 | /// sumamos donde corresponda |
||
| 526 | if ($num <= 11) { |
||
| 527 | $totales[$eje->year()]['a'] += $saldo1; |
||
| 528 | } else if ($num <= 16) { |
||
| 529 | $totales[$eje->year()]['b'] += $saldo1; |
||
| 530 | $totales[$eje->year()]['c'] = $totales[$eje->year()]['a'] + $totales[$eje->year()]['b']; |
||
| 531 | } else if ($num == 17) { |
||
| 532 | $totales[$eje->year()]['d'] = $totales[$eje->year()]['c'] + $saldo1; |
||
| 533 | } |
||
| 534 | |||
| 535 | $encontrado = TRUE; |
||
| 536 | $num++; |
||
| 537 | break; |
||
| 538 | } |
||
| 539 | } |
||
| 540 | |||
| 541 | $continuar = $encontrado; |
||
| 542 | } |
||
| 543 | |||
| 544 | $pdf_doc->add_table_row( |
||
| 545 | array( |
||
| 546 | 'descripcion' => "\n<b>D) RESULTADO DEL EJERCICIO (C+17)</b>", |
||
| 547 | 'actual' => "\n<b>" . $this->show_numero($totales[$eje->year()]['d']) . '</b>' |
||
| 548 | ) |
||
| 549 | ); |
||
| 550 | |||
| 551 | $pdf_doc->save_table( |
||
| 552 | array( |
||
| 553 | 'fontSize' => 12, |
||
| 554 | 'cols' => array( |
||
| 555 | 'actual' => array('justification' => 'right') |
||
| 556 | ), |
||
| 557 | 'width' => 540, |
||
| 558 | 'shaded' => 0 |
||
| 559 | ) |
||
| 560 | ); |
||
| 561 | } |
||
| 562 | } |
||
| 563 | |||
| 564 | private function get_saldo_balance($codbalance, &$ejercicio) |
||
| 565 | { |
||
| 566 | $total = 0; |
||
| 567 | |||
| 568 | foreach ($this->balance_cuenta_a->search_by_codbalance($codbalance) as $bca) { |
||
| 569 | $total += $bca->saldo($ejercicio); |
||
| 570 | } |
||
| 571 | |||
| 572 | return $total; |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Función auxiliar para generar el informe de situación. |
||
| 577 | * Para generar este informe hay que leer los códigos de balance con naturaleza A o P |
||
| 578 | * en orden. Pero como era demasiado sencillo, los hijos de puta de facturalux decidieron |
||
| 579 | * añadir números romanos, para que no puedas ordenarlos fácilemnte. |
||
| 580 | * @param fs_pdf $pdf_doc |
||
| 581 | * @param ejercicio $eje |
||
| 582 | * @param boolean $np |
||
| 583 | */ |
||
| 584 | private function situacion(&$pdf_doc, &$eje, $np = TRUE) |
||
| 796 | ) |
||
| 797 | ); |
||
| 798 | } |
||
| 799 | } |
||
| 800 | } |
||
| 801 | |||
| 802 | private function get_saldo_balance2($codbalance, $ejercicio, $naturaleza = 'A') |
||
| 824 | } |
||
| 825 | |||
| 826 | private function show_numero($num) |
||
| 831 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths