Passed
Push — master ( 9f95e8...6c2f3e )
by Joe Nilson
05:48
created

residentes_vehiculos::listByEdificacion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 24
c 0
b 0
f 0
nc 3
nop 5
dl 0
loc 43
rs 9.536
1
<?php
2
/*
3
 * Copyright (C) 2017 Joe Nilson <joenilson at gmail.com>
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 published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (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
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace FacturaScripts\model;
19
20
/**
21
 * Model de la tabla donde se almacenan los distintos vehiculos y la información de los mismos
22
 * de cada residente
23
 *
24
 * @author Joe Nilson <joenilson at gmail.com>
25
 */
26
class residentes_vehiculos extends \fs_model
27
{
28
    /**
29
     * El Id del vehiculo
30
     * @var integer serial
31
     */
32
    public $idvehiculo;
33
    /**
34
     * El codigo del Residente o cliente al que pertenece el vehiculo
35
     * @var string
36
     */
37
    public $codcliente;
38
    /**
39
     * La marca del Vehiculo
40
     * @var string
41
     */
42
    public $vehiculo_marca;
43
    /**
44
     * El modelo del Vehiculo
45
     * @var string
46
     */
47
    public $vehiculo_modelo;
48
    /**
49
     * El color del Vehiculo
50
     * @var string
51
     */
52
    public $vehiculo_color;
53
    /**
54
     * La placa del Vehiculo
55
     * @var string
56
     */
57
    public $vehiculo_placa;
58
    /**
59
     * El tipo de Vehiculo
60
     * @var string
61
     */
62
    public $vehiculo_tipo;
63
    /**
64
     * Si posee una tarjeta de acceso asignada al vehiculo
65
     * este código se guarda aquí
66
     * @var string
67
     */
68
    public $codigo_tarjeta;
69
    public $cliente;
70
71
    public function __construct($t = false)
72
    {
73
        parent::__construct('residentes_vehiculos', 'plugins/residentes');
74
        if ($t) {
75
            $this->idvehiculo = $t['idvehiculo'];
76
            $this->codcliente = $t['codcliente'];
77
            $this->vehiculo_marca = $t['vehiculo_marca'];
78
            $this->vehiculo_modelo = $t['vehiculo_modelo'];
79
            $this->vehiculo_color = $t['vehiculo_color'];
80
            $this->vehiculo_placa = $t['vehiculo_placa'];
81
            $this->vehiculo_tipo = $t['vehiculo_tipo'];
82
            $this->codigo_tarjeta = $t['codigo_tarjeta'];
83
        } else {
84
            $this->idvehiculo = null;
85
            $this->codcliente = null;
86
            $this->vehiculo_marca = null;
87
            $this->vehiculo_modelo = null;
88
            $this->vehiculo_color = null;
89
            $this->vehiculo_placa = null;
90
            $this->vehiculo_tipo = null;
91
            $this->codigo_tarjeta = null;
92
        }
93
        $this->cliente = new cliente();
1 ignored issue
show
Bug introduced by
The type FacturaScripts\model\cliente was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
94
    }
95
96
    public function install()
97
    {
98
        return "";
99
    }
100
101
    public function info_adicional(&$i)
102
    {
103
        $residente = new residentes_edificaciones();
104
        $residente_informacion = new residentes_informacion();
105
        $data = $this->cliente->get($i->codcliente);
106
        $data_residente = $residente->get_by_field('codcliente', $i->codcliente)[0];
107
        $data_info = $residente_informacion->get($i->codcliente);
108
        $i->nombre = $data->nombre;
109
        $i->telefono1 = $data->telefono1;
110
        $i->telefono2 = $data->telefono2;
111
        $i->email = $data->email;
112
        $i->observaciones = $data->observaciones;
113
        $i->ca_nombres = $data_info->ca_nombres;
114
        $i->ca_apellidos = $data_info->ca_apellidos;
115
        $i->codigo = $data_residente->codigo_externo();
116
        $i->numero = $data_residente->numero;
117
        $i->propietario = $data_info->ca_propietario;
118
        $i->id = $data_residente->id;
119
        $i->fecha_ocupacion = $data_residente->fecha_ocupacion;
120
        $i->edificacion = $data_residente->codigo_externo();
121
        return $i;
122
    }
123
124
    public function listByEdificacion($where = "", $order = "", $sort = "", $limit = FS_ITEM_LIMIT, $offset = 0)
0 ignored issues
show
Bug introduced by
The constant FacturaScripts\model\FS_ITEM_LIMIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
125
    {
126
        $sql = "select ".
127
            "v.*, r.id, r.codcliente, c.nombre, c.cifnif, c.telefono1, c.email, r.codigo, r.numero ".
128
            ",i.propietario, i.ca_nombres, i.ca_apellidos, r.fecha_ocupacion ".
129
//            ", count(v.idvehiculo) as cantidad_vehiculos".
130
            "from residentes_vehiculos as v ".
131
            "RIGHT join residentes_edificaciones as r ON (r.codcliente = v.codcliente AND v.codcliente IS NOT NULL) ".
132
            "left join clientes as c on (c.codcliente = r.codcliente) ".
133
            "left join residentes_informacion as i ON (i.codcliente  = v.codcliente) ".
134
135
            $where.
136
//            " group by ".
137
//            "v.idcliente, r.id, r.codcliente, c.nombre, c.cifnif, c.telefono1, c.email, r.codigo, r.numero, ".
138
//            "i.propietario, i.ca_nombres, i.ca_apellidos, r.fecha_ocupacion ".
139
            " order by ".trim($order)." ".$sort;
140
141
        $sql_count = "SELECT count(v.idvehiculo) as total ".
142
            "from residentes_vehiculos as v ".
143
            "RIGHT join residentes_edificaciones as r ON (r.codcliente = v.codcliente AND v.codcliente IS NOT NULL) ".
144
            "left join clientes as c on (c.codcliente = r.codcliente) ".
145
            "left join residentes_informacion as i ON (i.codcliente  = v.codcliente) ".
146
            $where;
147
        $data_total = $this->db->select($sql_count);
148
149
//        $sql = "SELECT  re.codigo_interno, rv.* FROM ".$this->table_name." as rv " .
150
//                " RIGHT JOIN residentes_edificaciones as re ON (re.codcliente = rv.codcliente) " .
151
//                " WHERE rv.codcliente IS NOT NULL " .
152
//                " ORDER BY re.codigo_interno, rv.codcliente, rv.idvehiculo ";
153
        //echo "<br><br><br>";
154
        //echo $sql;
155
        $data = $this->db->select_limit($sql, $limit, $offset);
156
        //$data = $this->db->select($sql);
157
        if ($data) {
158
            $lista = array();
159
            foreach ($data as $d) {
160
                //$item = new residentes_vehiculos($d);
161
                //$item = $this->info_adicional($item);
162
                $lista[] = (object) $d;
163
            }
164
            return [$lista, $data_total[0]['total']];
165
        } else {
166
            return false;
167
        }
168
    }
169
170
    public function all()
171
    {
172
        $sql = "SELECT * FROM ".$this->table_name." ORDER BY codcliente,idvehiculo";
173
        $data = $this->db->select($sql);
174
        if ($data) {
175
            $lista = array();
176
            foreach ($data as $d) {
177
                $item = new residentes_vehiculos($d);
178
                //$item = $this->info_adicional($item);
179
                $lista[] = $item;
180
            }
181
            ksort($lista, );
182
            return $lista;
183
        } else {
184
            return false;
185
        }
186
    }
187
188
    public function get($codcliente, $id)
189
    {
190
        $sql = "SELECT * FROM ".$this->table_name." WHERE codcliente = ".$this->var2str($codcliente).
191
            " AND idvehiculo = ".$this->intval($id).";";
192
        $data = $this->db->select($sql);
193
        if ($data) {
194
            $item = new residentes_vehiculos($data[0]);
195
            return $item;
196
        } else {
197
            return false;
198
        }
199
    }
200
201
    /**
202
     * //Si queremos buscar por marca o modelo o codcliente o idvehiculo o tipo o placa
203
     * @param $field string
204
     * @param $value string
205
     * @return boolean|\FacturaScripts\model\residentes_vehiculos
206
     */
207
    public function get_by_field($field, $value)
208
    {
209
        $query = (is_string($value))?$this->var2str($value):$this->intval($value);
210
        $sql = "SELECT * FROM ".$this->table_name." WHERE ".strtoupper(trim($field))." = ".$query.";";
211
        $data = $this->db->select($sql);
212
        if ($data) {
213
            $lista = array();
214
            foreach ($data as $d) {
215
                $item = new residentes_vehiculos($d);
216
                //$item = $this->info_adicional($item);
217
                $lista[] = $item;
218
            }
219
            return $lista;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $lista returns the type FacturaScripts\model\residentes_vehiculos[]|array which is incompatible with the documented return type FacturaScripts\model\residentes_vehiculos|boolean.
Loading history...
220
        } else {
221
            return false;
222
        }
223
    }
224
225
    public function exists()
226
    {
227
        if (is_null($this->idvehiculo)) {
0 ignored issues
show
introduced by
The condition is_null($this->idvehiculo) is always false.
Loading history...
228
            return false;
229
        } else {
230
            return $this->get($this->codcliente, $this->idvehiculo);
231
        }
232
    }
233
234
    public function save()
235
    {
236
        if ($this->exists()) {
237
            $sql = "UPDATE ".$this->table_name." SET ".
238
                    "vehiculo_marca = ".$this->var2str($this->vehiculo_marca).", ".
239
                    "vehiculo_modelo = ".$this->var2str($this->vehiculo_modelo).", ".
240
                    "vehiculo_color = ".$this->var2str($this->vehiculo_color).", ".
241
                    "vehiculo_placa = ".$this->var2str($this->vehiculo_placa).", ".
242
                    "vehiculo_tipo = ".$this->var2str($this->vehiculo_tipo).", ".
243
                    "codigo_tarjeta = ".$this->var2str($this->codigo_tarjeta)." ".
244
                    "WHERE idvehiculo = ".$this->intval($this->idvehiculo)." AND ".
245
                    "codcliente = ".$this->var2str($this->codcliente).";";
246
            return $this->db->exec($sql);
247
        } else {
248
            $sql = "INSERT INTO ".$this->table_name." (codcliente, vehiculo_marca, vehiculo_modelo, vehiculo_color, ".
249
                "vehiculo_placa, vehiculo_tipo, codigo_tarjeta) VALUES (".
250
                    $this->var2str($this->codcliente).", ".
251
                    $this->var2str($this->vehiculo_marca).", ".
252
                    $this->var2str($this->vehiculo_modelo).", ".
253
                    $this->var2str($this->vehiculo_color).", ".
254
                    $this->var2str($this->vehiculo_placa).", ".
255
                    $this->var2str($this->vehiculo_tipo).", ".
256
                    $this->var2str($this->codigo_tarjeta).");";
257
            if ($this->db->exec($sql)) {
258
                return $this->db->lastval();
259
            } else {
260
                return false;
261
            }
262
        }
263
    }
264
265
    /**
266
     * Función para realizar buquedas en la mayor cantidad de información de vehiculos del residente
267
     * @param $query string/integer
0 ignored issues
show
Documentation Bug introduced by
The doc comment string/integer at position 0 could not be parsed: Unknown type name 'string/integer' at position 0 in string/integer.
Loading history...
268
     */
269
    public function search($busqueda, $offset = 0)
270
    {
271
        $clilist = array();
272
        $query = mb_strtolower($this->no_html($busqueda), 'UTF8');
273
274
        $consulta = "SELECT * FROM " . $this->table_name . " WHERE ";
275
        if (is_numeric($query)) {
276
            $consulta .= "(codigo_tarjeta LIKE '%" . $query . "%' OR CAST(idvehiculo as CHAR) = '%" . $query . "%')";
277
        } else {
278
            $buscar = str_replace(' ', '%', $query);
279
            $consulta .= "(lower(codigo_tarjeta) LIKE '%" . $buscar .
280
                "%' OR lower(vehiculo_color) LIKE '%" . $buscar . "%'" .
281
                " OR lower(vehiculo_marca) LIKE '%" . $buscar .
282
                "%' OR lower(vehiculo_modelo) LIKE '%" . $buscar . "%'" .
283
                " OR lower(vehiculo_placa) LIKE '%" . $buscar .
284
                "%' OR lower(vehiculo_tipo) LIKE '%" . $buscar . "%')";
285
        }
286
        $consulta .= " ORDER BY codcliente ASC";
287
288
        $data = $this->db->select_limit($consulta, FS_ITEM_LIMIT, $offset);
0 ignored issues
show
Bug introduced by
The constant FacturaScripts\model\FS_ITEM_LIMIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
289
        if ($data) {
290
            foreach ($data as $d) {
291
                $item = new residentes_vehiculos($d);
292
                $clilist[] = $item->info_adicional($item);
293
            }
294
        }
295
296
        return $clilist;
297
    }
298
299
    public function delete()
300
    {
301
        $sql = "DELETE FROM ".$this->table_name." WHERE idvehiculo = ".$this->intval($this->idvehiculo).
302
            " and codcliente = ".$this->var2str($this->codcliente).";";
303
        return $this->db->exec($sql);
304
    }
305
306
}
307