| Total Complexity | 71 |
| Total Lines | 404 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Complex classes like compras_factura 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 compras_factura, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class compras_factura extends fbase_controller |
||
|
|
|||
| 23 | { |
||
| 24 | |||
| 25 | public $agente; |
||
| 26 | public $almacen; |
||
| 27 | public $divisa; |
||
| 28 | public $ejercicio; |
||
| 29 | public $factura; |
||
| 30 | public $forma_pago; |
||
| 31 | public $mostrar_boton_pagada; |
||
| 32 | public $proveedor; |
||
| 33 | public $rectificada; |
||
| 34 | public $rectificativa; |
||
| 35 | public $serie; |
||
| 36 | public $ncf_tipo_anulacion; |
||
| 37 | public $ncf_tipo_pagos_compras; |
||
| 38 | public $impuesto; |
||
| 39 | |||
| 40 | public function __construct() |
||
| 41 | { |
||
| 42 | parent::__construct(__CLASS__, 'Factura de proveedor', 'compras', FALSE, FALSE); |
||
| 43 | } |
||
| 44 | |||
| 45 | protected function private_core() |
||
| 46 | { |
||
| 47 | parent::private_core(); |
||
| 48 | |||
| 49 | $this->ppage = $this->page->get('compras_facturas'); |
||
| 50 | $this->agente = FALSE; |
||
| 51 | $this->almacen = new almacen(); |
||
| 52 | $this->divisa = new divisa(); |
||
| 53 | $this->ejercicio = new ejercicio(); |
||
| 54 | $this->ncf_tipo_anulacion = new ncf_tipo_anulacion(); |
||
| 55 | $this->ncf_tipo_pagos_compras = new ncf_tipo_pagos_compras(); |
||
| 56 | $factura = new factura_proveedor(); |
||
| 57 | $this->factura = FALSE; |
||
| 58 | $this->forma_pago = new forma_pago(); |
||
| 59 | $this->proveedor = FALSE; |
||
| 60 | $this->rectificada = FALSE; |
||
| 61 | $this->rectificativa = FALSE; |
||
| 62 | $this->serie = new serie(); |
||
| 63 | $this->impuesto = new impuesto(); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Si hay alguna extensión de tipo config y texto no_button_pagada, |
||
| 67 | * desactivamos el botón de pagada/sin pagar. |
||
| 68 | */ |
||
| 69 | $this->mostrar_boton_pagada = TRUE; |
||
| 70 | foreach ($this->extensions as $ext) { |
||
| 71 | if ($ext->type == 'config' && $ext->text == 'no_button_pagada') { |
||
| 72 | $this->mostrar_boton_pagada = FALSE; |
||
| 73 | break; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | if (isset($_POST['idfactura'])) { |
||
| 78 | $this->factura = $factura->get($_POST['idfactura']); |
||
| 79 | $this->modificar(); |
||
| 80 | } else if (isset($_GET['id'])) { |
||
| 81 | $this->factura = $factura->get($_GET['id']); |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($this->factura) { |
||
| 85 | $this->page->title = $this->factura->codigo; |
||
| 86 | |||
| 87 | /// cargamos el agente |
||
| 88 | if (!is_null($this->factura->codagente)) { |
||
| 89 | $agente = new agente(); |
||
| 90 | $this->agente = $agente->get($this->factura->codagente); |
||
| 91 | } |
||
| 92 | |||
| 93 | /// cargamos el proveedor |
||
| 94 | $proveedor = new proveedor(); |
||
| 95 | $this->proveedor = $proveedor->get($this->factura->codproveedor); |
||
| 96 | |||
| 97 | if (isset($_GET['gen_asiento']) && isset($_GET['petid'])) { |
||
| 98 | if ($this->duplicated_petition($_GET['petid'])) { |
||
| 99 | $this->new_error_msg('Petición duplicada. Evita hacer doble clic sobre los botones.'); |
||
| 100 | } else { |
||
| 101 | $this->generar_asiento($this->factura); |
||
| 102 | } |
||
| 103 | } else if (isset($_REQUEST['pagada'])) { |
||
| 104 | $this->pagar(($_REQUEST['pagada'] == 'TRUE')); |
||
| 105 | } else if (isset($_POST['anular'])) { |
||
| 106 | if ($_POST['rectificativa'] == 'TRUE') { |
||
| 107 | $this->generar_rectificativa(); |
||
| 108 | } else { |
||
| 109 | $this->anular_factura(); |
||
| 110 | } |
||
| 111 | } elseif (\filter_input(INPUT_POST, 'rectificar')) { |
||
| 112 | $this->rectificar_factura(); |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($this->factura->idfacturarect) { |
||
| 116 | $this->rectificada = $factura->get($this->factura->idfacturarect); |
||
| 117 | } else { |
||
| 118 | $this->get_factura_rectificativa(); |
||
| 119 | } |
||
| 120 | |||
| 121 | /// comprobamos la factura |
||
| 122 | $this->factura->full_test(); |
||
| 123 | } else { |
||
| 124 | $this->new_error_msg("¡Factura de proveedor no encontrada!", 'error', FALSE, FALSE); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | public function url() |
||
| 129 | { |
||
| 130 | if (!isset($this->factura)) { |
||
| 131 | return parent::url(); |
||
| 132 | } else if ($this->factura) { |
||
| 133 | return $this->factura->url(); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $this->page->url(); |
||
| 137 | } |
||
| 138 | |||
| 139 | private function modificar() |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | private function generar_asiento(&$factura) |
||
| 172 | { |
||
| 173 | if ($factura->get_asiento()) { |
||
| 174 | $this->new_error_msg('Ya hay un asiento asociado a esta factura.'); |
||
| 175 | } else { |
||
| 176 | $asiento_factura = new asiento_factura(); |
||
| 177 | $asiento_factura->soloasiento = TRUE; |
||
| 178 | if ($asiento_factura->generar_asiento_compra($factura)) { |
||
| 179 | $this->new_message("<a href='" . $asiento_factura->asiento->url() . "'>Asiento</a> generado correctamente."); |
||
| 180 | |||
| 181 | if (!$this->empresa->contintegrada) { |
||
| 182 | $this->new_message("¿Quieres que los asientos se generen automáticamente?" |
||
| 183 | . " Activa la <a href='index.php?page=admin_empresa#facturacion'>Contabilidad integrada</a>."); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | foreach ($asiento_factura->errors as $err) { |
||
| 188 | $this->new_error_msg($err); |
||
| 189 | } |
||
| 190 | |||
| 191 | foreach ($asiento_factura->messages as $msg) { |
||
| 192 | $this->new_message($msg); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | private function pagar($pagada = TRUE) |
||
| 198 | { |
||
| 199 | /// ¿Hay asiento? |
||
| 200 | if (is_null($this->factura->idasiento)) { |
||
| 201 | $this->factura->pagada = $pagada; |
||
| 202 | $this->factura->save(); |
||
| 203 | } else if (!$pagada && $this->factura->pagada) { |
||
| 204 | /// marcar como impagada |
||
| 205 | $this->factura->pagada = FALSE; |
||
| 206 | |||
| 207 | /// ¿Eliminamos el asiento de pago? |
||
| 208 | $as1 = new asiento(); |
||
| 209 | $asiento = $as1->get($this->factura->idasientop); |
||
| 210 | if ($asiento) { |
||
| 211 | $asiento->delete(); |
||
| 212 | $this->new_message('Asiento de pago eliminado.'); |
||
| 213 | } |
||
| 214 | |||
| 215 | $this->factura->idasientop = NULL; |
||
| 216 | if ($this->factura->save()) { |
||
| 217 | $this->new_message('Factura marcada como impagada.'); |
||
| 218 | } else { |
||
| 219 | $this->new_error_msg('Error al modificar la factura.'); |
||
| 220 | } |
||
| 221 | } else if ($pagada && !$this->factura->pagada) { |
||
| 222 | /// marcar como pagada |
||
| 223 | $asiento = $this->factura->get_asiento(); |
||
| 224 | if ($asiento) { |
||
| 225 | /// nos aseguramos que el proveedor tenga subcuenta en el ejercicio actual |
||
| 226 | $subpro = FALSE; |
||
| 227 | $eje = $this->ejercicio->get_by_fecha($_POST['fpagada']); |
||
| 228 | if ($eje && $this->proveedor) { |
||
| 229 | $subpro = $this->proveedor->get_subcuenta($eje->codejercicio); |
||
| 230 | } |
||
| 231 | |||
| 232 | $importe = $this->euro_convert($this->factura->totaleuros, $this->factura->coddivisa, $this->factura->tasaconv); |
||
| 233 | |||
| 234 | $asiento_factura = new asiento_factura(); |
||
| 235 | $this->factura->idasientop = $asiento_factura->generar_asiento_pago($asiento, $this->factura->codpago, $_POST['fpagada'], $subpro, $importe); |
||
| 236 | if ($this->factura->idasientop !== NULL) { |
||
| 237 | $this->factura->pagada = TRUE; |
||
| 238 | if ($this->factura->save()) { |
||
| 239 | $this->new_message('<a href="' . $this->factura->asiento_pago_url() . '">Asiento de pago</a> generado.'); |
||
| 240 | } else { |
||
| 241 | $this->new_error_msg('Error al marcar la factura como pagada.'); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | foreach ($asiento_factura->errors as $err) { |
||
| 246 | $this->new_error_msg($err); |
||
| 247 | } |
||
| 248 | } else { |
||
| 249 | $this->new_error_msg('No se ha encontrado el asiento de la factura.'); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | private function anular_factura() |
||
| 255 | { |
||
| 256 | $ejercicio = $this->ejercicio->get_by_fecha($this->today()); |
||
| 257 | if ($ejercicio) { |
||
| 258 | /// generamos una factura rectificativa a partir de la actual |
||
| 259 | $factura = clone $this->factura; |
||
| 260 | $factura->idfactura = NULL; |
||
| 261 | $factura->numero = NULL; |
||
| 262 | $factura->numproveedor = NULL; |
||
| 263 | $factura->codigo = NULL; |
||
| 264 | $factura->idasiento = NULL; |
||
| 265 | $factura->idasientop = NULL; |
||
| 266 | $factura->numdocs = 0; |
||
| 267 | |||
| 268 | $factura->idfacturarect = $this->factura->idfactura; |
||
| 269 | $factura->codigorect = $this->factura->codigo; |
||
| 270 | $factura->codejercicio = $ejercicio->codejercicio; |
||
| 271 | $factura->codserie = $_POST['codserie']; |
||
| 272 | $factura->set_fecha_hora($this->today(), $this->hour()); |
||
| 273 | $factura->observaciones = $_POST['motivo']; |
||
| 274 | $factura->neto = 0; |
||
| 275 | $factura->totalirpf = 0; |
||
| 276 | $factura->totaliva = 0; |
||
| 277 | $factura->totalrecargo = 0; |
||
| 278 | $factura->total = 0; |
||
| 279 | |||
| 280 | /// función auxiliar para implementar en los plugins que lo necesiten |
||
| 281 | fs_generar_numproveedor($factura); |
||
| 282 | |||
| 283 | if ($factura->save()) { |
||
| 284 | $articulo = new articulo(); |
||
| 285 | $error = FALSE; |
||
| 286 | |||
| 287 | /// copiamos las líneas en negativo |
||
| 288 | foreach ($this->factura->get_lineas() as $lin) { |
||
| 289 | $lin->idlinea = NULL; |
||
| 290 | $lin->idalbaran = NULL; |
||
| 291 | $lin->idfactura = $factura->idfactura; |
||
| 292 | $lin->cantidad = 0 - $lin->cantidad; |
||
| 293 | $lin->pvpsindto = $lin->pvpunitario * $lin->cantidad; |
||
| 294 | $lin->pvptotal = $lin->pvpunitario * (100 - $lin->dtopor) / 100 * $lin->cantidad; |
||
| 295 | |||
| 296 | if ($lin->save()) { |
||
| 297 | if ($lin->referencia) { |
||
| 298 | /// actualizamos el stock |
||
| 299 | $art = $articulo->get($lin->referencia); |
||
| 300 | if ($art) { |
||
| 301 | $art->sum_stock($factura->codalmacen, $lin->cantidad, TRUE, $lin->codcombinacion); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | } else { |
||
| 305 | $error = TRUE; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | /// obtenemos los subtotales por impuesto |
||
| 310 | foreach ($this->fbase_get_subtotales_documento($factura->get_lineas()) as $subt) { |
||
| 311 | $factura->neto += $subt['neto']; |
||
| 312 | $factura->totaliva += $subt['iva']; |
||
| 313 | $factura->totalirpf += $subt['irpf']; |
||
| 314 | $factura->totalrecargo += $subt['recargo']; |
||
| 315 | } |
||
| 316 | |||
| 317 | $factura->total = round($factura->neto + $factura->totaliva - $factura->totalirpf + $factura->totalrecargo, FS_NF0); |
||
| 318 | |||
| 319 | if ($error || !$factura->save()) { |
||
| 320 | $factura->delete(); |
||
| 321 | $this->new_error_msg('Se han producido errores al crear la ' . FS_FACTURA_RECTIFICATIVA); |
||
| 322 | } else { |
||
| 323 | $this->new_message('<a href="' . $factura->url() . '">' . ucfirst(FS_FACTURA_RECTIFICATIVA) . '</a> creada correctamenmte.'); |
||
| 324 | |||
| 325 | if ($this->empresa->contintegrada) { |
||
| 326 | $this->generar_asiento($factura); |
||
| 327 | } else { |
||
| 328 | /// generamos las líneas de IVA de todas formas |
||
| 329 | $factura->get_lineas_iva(); |
||
| 330 | } |
||
| 331 | |||
| 332 | /// Función de ejecución de tareas post guardado correcto del albarán |
||
| 333 | fs_documento_post_save($factura); |
||
| 334 | |||
| 335 | /// anulamos la factura actual |
||
| 336 | $this->factura->anulada = TRUE; |
||
| 337 | $this->factura->save(); |
||
| 338 | } |
||
| 339 | } else { |
||
| 340 | $this->new_error_msg('Error al generar la ' . FS_FACTURA_RECTIFICATIVA . '.'); |
||
| 341 | } |
||
| 342 | } else { |
||
| 343 | $this->new_error_msg('No se encuentra un ejercicio abierto para la fecha ' . $this->today()); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | public function rectificar_factura() |
||
| 348 | { |
||
| 349 | $serie = $this->serie->get($_POST['codserie']); |
||
| 350 | if (!$serie) { |
||
| 351 | $this->new_error_msg('Serie no encontrada.'); |
||
| 352 | $continuar = false; |
||
| 353 | } |
||
| 354 | |||
| 355 | $motivo = \filter_input(INPUT_POST, 'motivo'); |
||
| 356 | $motivo_anulacion = $this->ncf_tipo_anulacion->get($motivo); |
||
| 357 | $monto0 = \filter_input(INPUT_POST, 'monto'); |
||
| 358 | $monto = ($monto0 > 0) ? ($monto0 * -1) : $monto0; |
||
| 359 | $impuesto0 = \filter_input(INPUT_POST, 'codimpuesto'); |
||
| 360 | $impuesto = ($impuesto0 > 0) ? ($impuesto0 * -1) : $impuesto0; |
||
| 361 | $monto_total = \filter_input(INPUT_POST, 'monto_total'); |
||
| 362 | $fecha = \filter_input(INPUT_POST, 'fecha'); |
||
| 363 | $irpf = 0; |
||
| 364 | $recargo = 0; |
||
| 365 | $factura = clone $this->factura; |
||
| 366 | $factura->idfactura = null; |
||
| 367 | $factura->numero = null; |
||
| 368 | $factura->numproveedor = $_POST['numproveedor']; |
||
| 369 | $factura->codigo = null; |
||
| 370 | $factura->idasiento = null; |
||
| 371 | $factura->idfacturarect = $this->factura->idfactura; |
||
| 372 | $factura->codigorect = $this->factura->codigo; |
||
| 373 | $factura->fecha = $fecha; |
||
| 374 | $factura->neto = round($monto, FS_NF0); |
||
| 375 | $factura->totaliva = (round(($monto * (($impuesto) / 100)), FS_NF0) * -1); |
||
| 376 | $factura->totalirpf = round($irpf, FS_NF0); |
||
| 377 | $factura->totalrecargo = round($recargo, FS_NF0); |
||
| 378 | $factura->total = $factura->neto + $factura->totaliva - $factura->totalirpf + $factura->totalrecargo; |
||
| 379 | $factura->observaciones = ucfirst(FS_FACTURA_RECTIFICATIVA) . " por rectificación contable de la " . ucfirst(FS_FACTURA) . ": " . $factura->codigorect; |
||
| 380 | |||
| 381 | if ($factura->save()) { |
||
| 382 | $linea = new linea_factura_proveedor(); |
||
| 383 | $linea->idfactura = $factura->idfactura; |
||
| 384 | $linea->descripcion = "Rectificación de importe"; |
||
| 385 | if (!$serie->siniva and $this->proveedor->regimeniva != 'Exento') { |
||
| 386 | $imp0 = $this->impuesto->get_by_iva($impuesto); |
||
| 387 | $linea->codimpuesto = ($imp0) ? $imp0->codimpuesto : null; |
||
| 388 | $linea->iva = ($impuesto > 0) ? floatval($impuesto) : floatval($impuesto * -1); |
||
| 389 | $linea->recargo = floatval($recargo); |
||
| 390 | } |
||
| 391 | $linea->irpf = floatval($irpf); |
||
| 392 | $linea->pvpunitario = ($monto > 0) ? floatval($monto) : floatval($monto * -1); |
||
| 393 | $linea->cantidad = -1; |
||
| 394 | $linea->dtopor = 0; |
||
| 395 | $linea->pvpsindto = ($linea->pvpunitario * $linea->cantidad); |
||
| 396 | $linea->pvptotal = floatval($monto); |
||
| 397 | if ($linea->save()) { |
||
| 398 | $factura->get_lineas_iva(); |
||
| 399 | $this->generar_asiento($factura); |
||
| 400 | $this->new_message("<a href='" . $factura->url() . "'>Factura</a> guardada correctamente con número NCF: " . $_POST['numproveedor']); |
||
| 401 | $this->new_change('Factura Cliente ' . $factura->codigo, $factura->url(), true); |
||
| 402 | } |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | private function get_factura_rectificativa() |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | public function get_cuentas_bancarias() |
||
| 417 | { |
||
| 418 | $cuentas = array(); |
||
| 419 | |||
| 426 | } |
||
| 427 | } |
||
| 428 |
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