lista_vehiculos::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
/*
3
 * Copyright (C) 2021 Joe Nilson <[email protected]>
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as
7
 * published by the Free Software Foundation, either version 3 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
require_once 'plugins/residentes/extras/residentes_controller.php';
18
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()
40
    {
41
        parent::__construct(__CLASS__, 'Vehiculos', 'residentes', false, true);
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');
0 ignored issues
show
Bug Best Practice introduced by
The property deudores does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
128
        if ($this->deudores) {
129
            $this->sort = 'DESC';
130
            $this->order = 'pendiente';
131
        }
132
133
        $this->disponibles = $this->filter_request('disponibles');
0 ignored issues
show
Bug Best Practice introduced by
The property disponibles does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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)
191
    {
192
        if (is_numeric($param)) {
193
            $where = " r.codigo LIKE '%" . $param . "%'"
194
                . " OR r.numero LIKE '%" . $param . "%'"
195
                . " OR CONCAT(r.codigo, r.numero) LIKE '%" . $param . "%'";
196
        } else {
197
            $buscar = str_replace(' ', '%', $param);
198
            $where = " lower(r.codigo) LIKE '%" . $buscar . "%'"
199
                . " OR lower(r.numero) LIKE '%" . $buscar . "%'"
200
                . " OR CONCAT(lower(r.codigo), r.numero) LIKE '%" . $param . "%'";
201
        }
202
        return $where;
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()
312
    {
313
        $url = $this->url() . "&query=" . $this->query
314
            . "&query_r=" . $this->query_r
315
            . "&query_v=" . $this->query_v
316
            . "&query_i=" . $this->query_i
317
            . "&orden=" . $this->order;
318
319
        $paginas = array();
320
        $i = 0;
321
        $num = 0;
322
        $actual = 1;
323
324
        /// añadimos todas la página
325
        while ($num < $this->total_resultados) {
326
            $paginas[$i] = array(
327
                'url' => $url . "&offset=" . ($i * FS_ITEM_LIMIT),
328
                'num' => $i + 1,
329
                'actual' => ($num === $this->offset)
330
            );
331
332
            if ($num === $this->offset) {
333
                $actual = $i;
334
            }
335
336
            $i++;
337
            $num += FS_ITEM_LIMIT;
338
        }
339
340
        /// ahora descartamos
341
        foreach ($paginas as $j => $value) {
342
            $enmedio = (int)($i / 2);
343
344
            /**
345
             * descartamos todo excepto la primera, la última, la de enmedio,
346
             * la actual, las 5 anteriores y las 5 siguientes
347
             */
348
            if (($j>1 && $j<$actual-5 && $j !== $enmedio) || ($j > $actual + 5 && $j < $i - 1 && $j !== $enmedio)) {
349
                unset($paginas[$j]);
350
            }
351
        }
352
353
        if (count($paginas) > 1) {
354
            return $paginas;
355
        }
356
357
        return array();
358
    }
359
}