| Total Complexity | 56 |
| Total Lines | 339 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like lista_vehiculos 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 lista_vehiculos, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class lista_vehiculos extends residentes_controller |
||
| 20 | { |
||
| 21 | public $cliente; |
||
| 22 | public $residente; |
||
| 23 | public $residentes; |
||
| 24 | public $inmueble; |
||
| 25 | public $inmuebles; |
||
| 26 | public $vehiculo; |
||
| 27 | public $vehiculos; |
||
| 28 | public $lista_vehiculos; |
||
| 29 | public $lista_marcas; |
||
| 30 | public $query; |
||
| 31 | public $query_r; |
||
| 32 | public $query_i; |
||
| 33 | public $query_v; |
||
| 34 | public $order; |
||
| 35 | public $sort; |
||
| 36 | public $offset; |
||
| 37 | public $resultados; |
||
| 38 | public $total_resultados; |
||
| 39 | public function __construct() |
||
| 42 | } |
||
| 43 | |||
| 44 | public function private_core() |
||
| 45 | { |
||
| 46 | parent::private_core(); // TODO: Change the autogenerated stub |
||
| 47 | $this->init(); |
||
| 48 | $this->filters(); |
||
| 49 | |||
| 50 | if ($this->filter_request('buscar_cliente')) { |
||
| 51 | $this->buscar_cliente(); |
||
| 52 | } elseif ($this->filter_request('buscar_cliente_avanzado')) { |
||
| 53 | $this->buscar_cliente_avanzado(); |
||
| 54 | } elseif ($this->filter_request('buscar_inmueble')) { |
||
| 55 | $this->buscar_inmueble(); |
||
| 56 | } elseif (filter_input(INPUT_GET, 'delete')) { |
||
| 57 | $inq = $this->residente->get(filter_input(INPUT_GET, 'delete')); |
||
| 58 | if ($inq) { |
||
| 59 | $inq->ocupado = false; |
||
| 60 | $inq->codcliente = ''; |
||
| 61 | $inq->fecha_disponibilidad = null; |
||
| 62 | $inq->fecha_ocupacion = null; |
||
| 63 | if ($inq->save()) { |
||
| 64 | $this->new_message('Inquilino removido correctamente.'); |
||
| 65 | } else { |
||
| 66 | $this->new_error_msg('Error al remover el inquilino.'); |
||
| 67 | } |
||
| 68 | } else { |
||
| 69 | $this->new_error_msg('Inquilino no encontrado.'); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | $tipo = $this->filter_request('type'); |
||
| 74 | if ($tipo === 'select-iddireccion') { |
||
| 75 | $this->mostrar_direcciones_residente(\filter_input(INPUT_GET, 'codcliente')); |
||
| 76 | } |
||
| 77 | |||
| 78 | $accion = $this->filter_request('accion'); |
||
| 79 | switch ($accion) { |
||
| 80 | case "mostrar_informacion_residente": |
||
| 81 | $this->mostrar_informacion_residente(); |
||
| 82 | break; |
||
| 83 | default: |
||
| 84 | $this->buscar(); |
||
| 85 | break; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | public function init() |
||
| 90 | { |
||
| 91 | parent::init(); // TODO: Change the autogenerated stub |
||
| 92 | $this->cliente = new cliente(); |
||
| 93 | $this->residente = new residentes_edificaciones(); |
||
| 94 | $this->residentes = new residentes_edificaciones(); |
||
| 95 | $this->vehiculos = new residentes_vehiculos(); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function filters() |
||
| 99 | { |
||
| 100 | $this->query_r = ''; |
||
| 101 | if ($this->filter_request('query_r')) { |
||
| 102 | $this->query_r = $this->filter_request('query_r'); |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->query_v = ''; |
||
| 106 | if ($this->filter_request('query_v')) { |
||
| 107 | $this->query_v = $this->filter_request('query_v'); |
||
| 108 | } |
||
| 109 | |||
| 110 | $this->query_i = ''; |
||
| 111 | if ($this->filter_request('query_i')) { |
||
| 112 | $this->query_i = $this->filter_request('query_i'); |
||
| 113 | } |
||
| 114 | |||
| 115 | $this->sort = 'ASC'; |
||
| 116 | if ($this->filter_request('sort')) { |
||
| 117 | $this->sort = $this->filter_request('sort'); |
||
| 118 | } |
||
| 119 | |||
| 120 | $this->order = 'r.codigo_interno, r.numero '; |
||
| 121 | if ($this->filter_request('orden')) { |
||
| 122 | $this->order = $this->filter_request('orden'); |
||
| 123 | } |
||
| 124 | |||
| 125 | $this->offset = ($this->filter_request('offset')) ?: 0; |
||
| 126 | |||
| 127 | $this->deudores = $this->filter_request('deudores'); |
||
|
|
|||
| 128 | if ($this->deudores) { |
||
| 129 | $this->sort = 'DESC'; |
||
| 130 | $this->order = 'pendiente'; |
||
| 131 | } |
||
| 132 | |||
| 133 | $this->disponibles = $this->filter_request('disponibles'); |
||
| 134 | if ($this->disponibles) { |
||
| 135 | $this->sort = 'ASC'; |
||
| 136 | $this->order = 'codcliente'; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | public function buscar() |
||
| 141 | { |
||
| 142 | $where = ""; |
||
| 143 | if ($this->query_r) { |
||
| 144 | $param = mb_strtolower($this->cliente->no_html($this->query_r), 'UTF8'); |
||
| 145 | $where = " WHERE ".$this->buscar_residentes($param); |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($this->query_v) { |
||
| 149 | $param = mb_strtolower($this->cliente->no_html($this->query_v), 'UTF8'); |
||
| 150 | $where = " WHERE ".$this->buscar_vehiculos($param); |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($this->query_i) { |
||
| 154 | $param = mb_strtolower($this->cliente->no_html($this->query_i), 'UTF8'); |
||
| 155 | $where = " WHERE ".$this->buscar_inmuebles($param); |
||
| 156 | } |
||
| 157 | |||
| 158 | [$this->resultados, $this->total_resultados] = $this->vehiculos->listByEdificacion( |
||
| 159 | $where, |
||
| 160 | $this->order, |
||
| 161 | $this->sort, |
||
| 162 | FS_ITEM_LIMIT, |
||
| 163 | $this->offset |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function buscar_residentes($param) |
||
| 168 | { |
||
| 169 | if (is_numeric($param)) { |
||
| 170 | $where = "(r.codcliente LIKE '%" . $param . "%'" |
||
| 171 | . " OR c.cifnif LIKE '%" . $param . "%'" |
||
| 172 | . " OR c.telefono1 LIKE '" . $param . "%'" |
||
| 173 | . " OR c.telefono2 LIKE '" . $param . "%'" |
||
| 174 | . " OR ca_telefono LIKE '" . $param . "%'" |
||
| 175 | . " OR r.observaciones LIKE '%" . $param . "%')"; |
||
| 176 | } else { |
||
| 177 | $buscar = str_replace(' ', '%', $param); |
||
| 178 | $where = "(lower(nombre) LIKE '%" . $buscar . "%'" |
||
| 179 | . " OR lower(razonsocial) LIKE '%" . $buscar . "%'" |
||
| 180 | . " OR lower(ca_apellidos) LIKE '%" . $buscar . "%'" |
||
| 181 | . " OR lower(ca_nombres) LIKE '%" . $buscar . "%'" |
||
| 182 | . " OR lower(cifnif) LIKE '%" . $buscar . "%'" |
||
| 183 | . " OR lower(observaciones) LIKE '%" . $buscar . "%'" |
||
| 184 | . " OR lower(ca_email) LIKE '%" . $buscar . "%'" |
||
| 185 | . " OR lower(email) LIKE '%" . $buscar . "%')"; |
||
| 186 | } |
||
| 187 | return $where; |
||
| 188 | } |
||
| 189 | |||
| 190 | public function buscar_inmuebles($param) |
||
| 203 | } |
||
| 204 | |||
| 205 | public function buscar_vehiculos($param) |
||
| 206 | { |
||
| 207 | if (is_numeric($param)) { |
||
| 208 | $where = "(r.codcliente LIKE '%" . $param . "%'" |
||
| 209 | . " OR vehiculo_placa LIKE '%" . $param . "%'" |
||
| 210 | . " OR CAST(idvehiculo AS CHAR) LIKE '" . $param . "%'" |
||
| 211 | . " OR telefono2 LIKE '" . $param . "%'" |
||
| 212 | . " OR observaciones LIKE '%" . $param . "%'" |
||
| 213 | . " OR codigo_tarjeta LIKE '%" . $param . "%')"; |
||
| 214 | } else { |
||
| 215 | $buscar = str_replace(' ', '%', $param); |
||
| 216 | $where = "(lower(vehiculo_marca) LIKE '%" . $buscar . "%'" |
||
| 217 | . " OR lower(vehiculo_modelo) LIKE '%" . $buscar . "%'" |
||
| 218 | . " OR lower(vehiculo_color) LIKE '%" . $buscar . "%'" |
||
| 219 | . " OR lower(vehiculo_placa) LIKE '%" . $buscar . "%'" |
||
| 220 | . " OR lower(vehiculo_tipo) LIKE '%" . $buscar . "%'" |
||
| 221 | . " OR lower(codigo_tarjeta) LIKE '%" . $buscar . "%')"; |
||
| 222 | } |
||
| 223 | return $where; |
||
| 224 | } |
||
| 225 | |||
| 226 | private function buscar_cliente() |
||
| 227 | { |
||
| 228 | /// desactivamos la plantilla HTML |
||
| 229 | $this->template = false; |
||
| 230 | |||
| 231 | $json = array(); |
||
| 232 | foreach ($this->cliente->search($_REQUEST['buscar_cliente']) as $cli) { |
||
| 233 | $json[] = array('value' => $cli->nombre, 'data' => $cli->codcliente); |
||
| 234 | } |
||
| 235 | |||
| 236 | header('Content-Type: application/json'); |
||
| 237 | echo json_encode(array('query' => $_REQUEST['buscar_cliente'], 'suggestions' => $json), JSON_THROW_ON_ERROR); |
||
| 238 | } |
||
| 239 | |||
| 240 | private function buscar_cliente_avanzado() |
||
| 241 | { |
||
| 242 | /// desactivamos la plantilla HTML |
||
| 243 | $this->template = false; |
||
| 244 | $json = array(); |
||
| 245 | //Buscamos en la lista de clientes |
||
| 246 | foreach ($this->cliente->search($_REQUEST['buscar_cliente_avanzado']) as $cli) { |
||
| 247 | $lista = $this->residente->get_by_field('codcliente', $cli->codcliente); |
||
| 248 | if ($lista) { |
||
| 249 | foreach ($lista as $residente) { |
||
| 250 | $json[$cli->codcliente] = array('value' => $cli->nombre, |
||
| 251 | 'data' => $cli->codcliente, |
||
| 252 | 'nombre' => $cli->nombre, |
||
| 253 | 'asignado' => true); |
||
| 254 | } |
||
| 255 | } else { |
||
| 256 | $json[$cli->codcliente] = array('value' => $cli->nombre, |
||
| 257 | 'data' => $cli->codcliente, |
||
| 258 | 'nombre' => $cli->nombre, |
||
| 259 | 'asignado' => false); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | //Buscamos en los datos adicionales del residente |
||
| 263 | foreach ($this->residente_informacion->search($_REQUEST['buscar_cliente_avanzado']) as $cli) { |
||
| 264 | if (!empty($cli)) { |
||
| 265 | $json[$cli->codcliente] = array('value' => $cli->nombre, 'data' => $cli->codcliente); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | //Buscamos en los datos de vehiculos del residente |
||
| 269 | foreach ($this->residente_vehiculo->search($_REQUEST['buscar_cliente_avanzado']) as $cli) { |
||
| 270 | if (!empty($cli)) { |
||
| 271 | $json[$cli->codcliente] = array( |
||
| 272 | 'value' => $cli->nombre.' '.$cli->vehiculo_placa." ". |
||
| 273 | $cli->vehiculo_marca.''.$cli->vehiculo_modelo, 'data' => $cli->codcliente); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | //Buscamos en las residencias |
||
| 277 | foreach ($this->residente->search($_REQUEST['buscar_cliente_avanzado']) as $cli) { |
||
| 278 | if (!empty($cli)) { |
||
| 279 | $json[$cli->codcliente] = array('value' => $cli->nombre." ".$cli->codigo.' '. |
||
| 280 | $cli->numero, 'data' => $cli->id, 'asignado' => true); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | header('Content-Type: application/json'); |
||
| 285 | echo json_encode( |
||
| 286 | array('query' => $_REQUEST['buscar_cliente_avanzado'], 'suggestions' => $json), |
||
| 287 | JSON_THROW_ON_ERROR |
||
| 288 | ); |
||
| 289 | } |
||
| 290 | |||
| 291 | private function buscar_inmueble() |
||
| 292 | { |
||
| 293 | /// desactivamos la plantilla HTML |
||
| 294 | $this->template = false; |
||
| 295 | |||
| 296 | $json = array(); |
||
| 297 | foreach ($this->residente->search($_REQUEST['buscar_inmueble'], 'inmueble') as $inmueble) { |
||
| 298 | if (!$inmueble->ocupado) { |
||
| 299 | $json[] = array('value' => $inmueble->codigo.$inmueble->numero, 'data' => $inmueble->id); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | header('Content-Type: application/json'); |
||
| 304 | echo json_encode( |
||
| 305 | array('query' => $_REQUEST['buscar_inmueble'], 'suggestions' => $json), |
||
| 306 | JSON_THROW_ON_ERROR |
||
| 307 | ); |
||
| 308 | } |
||
| 309 | |||
| 310 | |||
| 311 | public function paginas() |
||
| 358 | } |
||
| 359 | } |