| Total Complexity | 69 |
| Total Lines | 381 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ver_residente 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 ver_residente, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ver_residente extends residentes_controller |
||
| 32 | { |
||
| 33 | public $cliente; |
||
| 34 | public $cliente_data; |
||
| 35 | public $articulos; |
||
| 36 | public $articulos_cobrables; |
||
| 37 | public $familia; |
||
| 38 | public $familias; |
||
| 39 | public $facturas; |
||
| 40 | public $impuesto; |
||
| 41 | public $residente; |
||
| 42 | public $forma_pago; |
||
| 43 | public $lista_notas; |
||
| 44 | public $total_facturas = 0; |
||
| 45 | public $offset = 0; |
||
| 46 | |||
| 47 | public function __construct() |
||
| 48 | { |
||
| 49 | parent::__construct(__CLASS__, 'Residente', 'residentes', false, false); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function initVars() |
||
| 53 | { |
||
| 54 | $this->cliente = new cliente(); |
||
| 55 | $this->facturas = array(); |
||
| 56 | $this->impuesto = new impuesto(); |
||
| 57 | $this->forma_pago = new forma_pago(); |
||
|
|
|||
| 58 | $this->residente = false; |
||
| 59 | $this->articulos = new articulo(); |
||
| 60 | $this->familias = new familia(); |
||
| 61 | $this->familia = $this->familias->get('RESIDENT'); |
||
| 62 | } |
||
| 63 | |||
| 64 | protected function private_core() |
||
| 65 | { |
||
| 66 | $this->initVars(); |
||
| 67 | |||
| 68 | if (\filter_input(INPUT_GET, 'id')) { |
||
| 69 | $res0 = new residentes_edificaciones(); |
||
| 70 | $this->residente = $res0->get(\filter_input(INPUT_GET, 'id')); |
||
| 71 | $this->cliente_data = $this->cliente->get($this->residente->codcliente); |
||
| 72 | $this->cliente_data->codcontacto = ''; |
||
| 73 | $this->total_facturas = $this->residente->totalFacturas(); |
||
| 74 | $this->offset = \filter_input(INPUT_GET, 'offset') ?? 0; |
||
| 75 | if (class_exists('contacto_cliente')) { |
||
| 76 | $concli = new contacto_cliente(); |
||
| 77 | $infoCRM = $concli->all_from_cliente($this->residente->codcliente); |
||
| 78 | $this->cliente_data->codcontacto = $infoCRM[0]->codcontacto; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($this->residente) { |
||
| 83 | $accion = \filter_input(INPUT_POST, 'accion'); |
||
| 84 | if ($accion == 'generar_factura') { |
||
| 85 | $this->nueva_factura(); |
||
| 86 | } |
||
| 87 | $this->informacionResidente(); |
||
| 88 | } else { |
||
| 89 | $this->new_error_msg('Residente no encontrado.'); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | public function url() |
||
| 94 | { |
||
| 95 | if (\filter_input(INPUT_GET, 'id')) { |
||
| 96 | return $this->residente->url(); |
||
| 97 | } |
||
| 98 | return parent::url(); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function informacionResidente() |
||
| 102 | { |
||
| 103 | $this->page->title = 'Residente ' . $this->residente->nombre; |
||
| 104 | $factura = new factura_cliente(); |
||
| 105 | $totalFacturas = $this->residente->totalFacturas(); |
||
| 106 | $cantidadGrupos = ceil($totalFacturas/FS_ITEM_LIMIT); |
||
| 107 | $facts = []; |
||
| 108 | for($i = 0; $i < $cantidadGrupos; $i++ ) { |
||
| 109 | $documentos = $factura->all_from_cliente($this->residente->codcliente, FS_ITEM_LIMIT * $i); |
||
| 110 | $facts = array_merge($facts, $documentos); |
||
| 111 | } |
||
| 112 | $this->facturas = array(); |
||
| 113 | $articulosCobrados = array(); |
||
| 114 | foreach ($facts as $fac) { |
||
| 115 | if(!$fac->anulada) { |
||
| 116 | $fac->referencias = ""; |
||
| 117 | foreach ($fac->get_lineas() as $linea) { |
||
| 118 | if ($linea->referencia) { |
||
| 119 | $fac->referencias .= $linea->referencia . " "; |
||
| 120 | $this->validarArticulos($articulosCobrados, $fac, $linea); |
||
| 121 | } else { |
||
| 122 | $fac->referencias .= $linea->descripcion . " "; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | $this->facturas[] = $fac; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | $this->generarArticulosCobrables($articulosCobrados); |
||
| 129 | } |
||
| 130 | |||
| 131 | public function validarArticulos(&$articulosCobrados, &$fac, &$linea) |
||
| 132 | { |
||
| 133 | if (!$fac->idfacturarect) { |
||
| 134 | $rectificativas = $fac->get_rectificativas(); |
||
| 135 | $articulosDevueltos = array(); |
||
| 136 | $this->validarDevoluciones($articulosDevueltos, $rectificativas); |
||
| 137 | |||
| 138 | if (!isset($articulosDevueltos[$linea->referencia])) { |
||
| 139 | $articulosCobrados[$linea->referencia] = 1; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | public function validarDevoluciones(&$articulosDevueltos, $rectificativas) |
||
| 145 | { |
||
| 146 | foreach ($rectificativas as $rectificativa) { |
||
| 147 | $lineas_r = $rectificativa->get_lineas(); |
||
| 148 | foreach ($lineas_r as $linea_r) { |
||
| 149 | $articulosDevueltos[$linea_r->referencia] = 1; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | public function generarArticulosCobrables($articulos_cobrados) |
||
| 155 | { |
||
| 156 | $articulos = $this->familia->get_articulos(0, 1000); |
||
| 157 | foreach ($articulos as $art) { |
||
| 158 | if (isset($articulos_cobrados[$art->referencia]) === false && !$art->bloqueado) { |
||
| 159 | $this->articulos_cobrables[] = $art; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | private function nueva_factura() |
||
| 165 | { |
||
| 166 | $cliente = $this->cliente->get($this->residente->codcliente); |
||
| 167 | if ($cliente) { |
||
| 168 | $factura = new factura_cliente(); |
||
| 169 | |||
| 170 | $this->cabeceraFactura($factura, $cliente); |
||
| 171 | |||
| 172 | $this->datosClienteFactura($factura, $cliente); |
||
| 173 | |||
| 174 | /// función auxiliar para implementar en los plugins que lo necesiten |
||
| 175 | if (!fs_generar_numero2($factura)) { |
||
| 176 | $factura->numero2 = ''; |
||
| 177 | } |
||
| 178 | if ($factura->save()) { |
||
| 179 | $this->lineasFactura($factura); |
||
| 180 | |||
| 181 | $this->lineasLibresFactura($factura, 'otro'); |
||
| 182 | $this->lineasLibresFactura($factura, 'otro2'); |
||
| 183 | |||
| 184 | $this->totalFactura($factura); |
||
| 185 | } else { |
||
| 186 | $this->new_error_msg('Imposible guardar la factura.'); |
||
| 187 | } |
||
| 188 | } else { |
||
| 189 | $this->new_error_msg('Cliente no encontrado.'); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | private function cabeceraFactura(&$factura, $cliente) |
||
| 194 | { |
||
| 195 | $factura->codserie = ($cliente->codserie) ?: $this->empresa->codserie; |
||
| 196 | $factura->codagente = $this->user->codagente; |
||
| 197 | $factura->codpago = \filter_input(INPUT_POST, 'forma_pago'); |
||
| 198 | $factura->codalmacen = $this->empresa->codalmacen; |
||
| 199 | $factura->set_fecha_hora($this->today(), $this->hour()); |
||
| 200 | $factura->observaciones = htmlentities(trim(\filter_input(INPUT_POST, 'observaciones'))); |
||
| 201 | $this->datosContabilidadFactura($factura, $cliente); |
||
| 202 | |||
| 203 | $div0 = new divisa(); |
||
| 204 | $divisa = $div0->get($cliente->coddivisa); |
||
| 205 | if ($divisa) { |
||
| 206 | $factura->coddivisa = $divisa->coddivisa; |
||
| 207 | $factura->tasaconv = $divisa->tasaconv; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | private function datosContabilidadFactura(&$factura, $cliente) |
||
| 212 | { |
||
| 213 | $eje0 = new ejercicio(); |
||
| 214 | $ejercicio = $eje0->get_by_fecha(date('d-m-Y')); |
||
| 215 | if ($ejercicio) { |
||
| 216 | $factura->codejercicio = $ejercicio->codejercicio; |
||
| 217 | } |
||
| 218 | if ($this->empresa->contintegrada) { |
||
| 219 | $cliente->get_subcuenta($this->empresa->codejercicio); |
||
| 220 | } |
||
| 221 | |||
| 222 | $forma_pago = $this->forma_pago->get($factura->codpago); |
||
| 223 | if ($forma_pago->genrecibos === 'Pagados') { |
||
| 224 | $factura->pagada = true; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | private function datosClienteFactura(&$factura, $cliente) |
||
| 229 | { |
||
| 230 | $factura->codcliente = $cliente->codcliente; |
||
| 231 | foreach ($cliente->get_direcciones() as $d) { |
||
| 232 | if ($d->domfacturacion) { |
||
| 233 | $factura->codcliente = $cliente->codcliente; |
||
| 234 | $factura->cifnif = $cliente->cifnif; |
||
| 235 | $factura->nombrecliente = $cliente->nombre; |
||
| 236 | $factura->apartado = $d->apartado; |
||
| 237 | $factura->ciudad = $d->ciudad; |
||
| 238 | $factura->coddir = $d->id; |
||
| 239 | $factura->codpais = $d->codpais; |
||
| 240 | $factura->codpostal = $d->codpostal; |
||
| 241 | $factura->direccion = $d->direccion; |
||
| 242 | $factura->provincia = $d->provincia; |
||
| 243 | break; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | private function lineasFactura(&$factura) |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | private function lineasLibresFactura(&$factura, $linea_nombre) |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | private function totalFactura(&$factura) |
||
| 310 | { |
||
| 311 | /// redondeamos |
||
| 312 | $factura->neto = round($factura->neto, FS_NF0); |
||
| 313 | $factura->totaliva = round($factura->totaliva, FS_NF0); |
||
| 314 | $factura->totalirpf = round($factura->totalirpf, FS_NF0); |
||
| 315 | $factura->totalrecargo = round($factura->totalrecargo, FS_NF0); |
||
| 316 | $factura->total = $factura->neto + $factura->totaliva - $factura->totalirpf + $factura->totalrecargo; |
||
| 317 | |||
| 318 | if (abs((float)(\filter_input(INPUT_POST, 'total_importe')) - $factura->total) > .01) { |
||
| 319 | $this->new_error_msg("El total difiere entre la vista y el controlador (" . |
||
| 320 | \filter_input(INPUT_POST, 'total_importe') . |
||
| 321 | " frente a " . $factura->total . "). Debes informar del error."); |
||
| 322 | $factura->delete(); |
||
| 323 | } elseif ($factura->save()) { |
||
| 324 | $this->generar_asiento($factura); |
||
| 325 | /// Función de ejecución de tareas post guardado correcto de la factura |
||
| 326 | fs_documento_post_save($factura); |
||
| 327 | $this->new_message("<a href='" . $factura->url() . "'>Factura</a> guardada correctamente."); |
||
| 328 | $this->new_change('Factura Cliente ' . $factura->codigo, $factura->url(), true); |
||
| 329 | } else { |
||
| 330 | $this->new_error_msg("¡Imposible actualizar la <a href='" . $factura->url() . "'>Factura</a>!"); |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Genera el asiento para la factura, si procede |
||
| 336 | * @param factura_cliente $factura |
||
| 337 | */ |
||
| 338 | private function generar_asiento(&$factura) |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | private function buscar_referencia() |
||
| 350 | { |
||
| 351 | /// desactivamos la plantilla HTML |
||
| 352 | $this->template = false; |
||
|
1 ignored issue
–
show
|
|||
| 353 | |||
| 354 | $articulo = new articulo(); |
||
| 355 | $json = array(); |
||
| 356 | foreach ($articulo->search($_REQUEST['buscar_referencia']) as $art) { |
||
| 357 | $json[] = array( |
||
| 358 | 'value' => $art->referencia, |
||
| 359 | 'data' => $art->referencia, |
||
| 360 | 'pvpi' => $art->pvp_iva(false), |
||
| 361 | 'codimpuesto' => $art->codimpuesto, |
||
| 362 | 'descripcion' => $art->descripcion |
||
| 363 | ); |
||
| 364 | } |
||
| 365 | |||
| 366 | header('Content-Type: application/json'); |
||
| 367 | echo json_encode(array('query' => $_REQUEST['buscar_referencia'], 'suggestions' => $json), JSON_THROW_ON_ERROR); |
||
| 368 | } |
||
| 369 | |||
| 370 | public function paginas() |
||
| 412 | } |
||
| 413 | } |
||
| 414 | } |
||
| 415 |
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