Issues (2050)

controller/facturacion_residentes.php (13 issues)

1
<?php
2
3
/*
4
 * Copyright (C) 2019 Joe Nilson <joenilson at gmail.com>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
require_once 'plugins/residentes/extras/residentes_pdf.php';
21
require_once 'extras/phpmailer/class.phpmailer.php';
22
require_once 'extras/phpmailer/class.smtp.php';
23
require_once 'plugins/residentes/extras/residentes_controller.php';
24
require_once 'plugins/residentes/extras/residentesEnviarMail.php';
25
26
27
/**
28
 * Description of facturacion_residentes
29
 *
30
 * @author joenilson
31
 */
32
33
class facturacion_residentes extends residentes_controller
34
{
35
    public $printExtensions;
36
    public $configuracionEmail;
37
    public $edificaciones_tipo;
38
    public $edificaciones_mapa;
39
    public $familia;
40
    public $familias;
41
    public $impuesto;
42
    public $mapa;
43
    public $padre;
44
    public $padre_interior;
45
    public $referencia;
46
    public $forma_pago;
47
    public $loop_horas;
48
    public $proxima_hora;
49
    public $idProg;
50
    public $programaciones;
51
    public $programaciones_conceptos;
52
    public $programaciones_edificaciones;
53
54
    public function __construct()
55
    {
56
        parent::__construct(__CLASS__, 'Facturación Residentes', 'residentes', false, true);
57
    }
58
    
59
    protected function private_core()
60
    {
61
        parent::private_core();
62
        $this->shared_functions();
63
        $this->tratarAccion();
64
65
        $eCfg = new residentes_email_config();
66
        $this->configuracionEmail = $eCfg->currentConfig();
67
        $this->programaciones = new residentes_facturacion_programada();
68
        $this->programaciones_conceptos = new residentes_facturacion_programada_conceptos();
69
        $this->programaciones_edificaciones = new residentes_facturacion_programada_edificaciones();
70
    }
71
    
72
    public function init()
73
    {
74
        parent::init();
75
        $extensions = new fs_extension();
76
        $this->printExtensions = $extensions->all_to('ventas_factura');
77
        $this->forma_pago = new forma_pago();
0 ignored issues
show
The type forma_pago 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...
78
        $this->familia = new familia();
0 ignored issues
show
The type familia 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...
79
        $this->familias = $this->familia->get("RESIDENT");
80
        $this->impuesto = new impuesto();
81
        $this->loop_horas = [];
82
        //Creamos un array para el selector de horas para cron
83
        for ($x = 0; $x < 24; $x++) {
84
            $this->loop_horas[] = str_pad($x, 2, "0", STR_PAD_LEFT);
85
        }
86
        $this->edificaciones_tipo = new residentes_edificaciones_tipo();
87
        $this->edificaciones_mapa = new residentes_edificaciones_mapa();
88
        $tipos = $this->edificaciones_tipo->all();
89
        $this->padre = $tipos[0];
90
        $this->mapa = $this->edificaciones_mapa->get_by_field('id_tipo', $this->padre->id);
91
        $this->proxima_hora = date('H', strtotime('+1 hour'));
92
    }
93
    
94
    private function shared_functions()
95
    {
96
        $extensiones = array(
97
            array(
98
                'name' => '001_facturacion_residentes',
99
                'page_from' => __CLASS__,
100
                'page_to' => __CLASS__,
101
                'type' => 'head',
102
                'text' => '<link rel="stylesheet" type="text/css" media="screen" href="' . FS_PATH .
0 ignored issues
show
The constant FS_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
103
                            'plugins/residentes/view/css/bootstrap-select.min.css"/>',
104
                'params' => ''
105
            ),
106
            array(
107
                'name' => '002_facturacion_residentes',
108
                'page_from' => __CLASS__,
109
                'page_to' => __CLASS__,
110
                'type' => 'head',
111
                'text' => '<script src="'.FS_PATH.
112
                            'plugins/residentes/view/js/bootstrap-select.min.js" type="text/javascript"></script>',
113
                'params' => ''
114
            ),
115
            array(
116
                'name' => '003_facturacion_residentes',
117
                'page_from' => __CLASS__,
118
                'page_to' => __CLASS__,
119
                'type' => 'head',
120
                'text' => '<script src="'.FS_PATH.
121
                            'plugins/residentes/view/js/i18n/defaults-es_ES.min.js" type="text/javascript"></script>',
122
                'params' => ''
123
            ),
124
            array(
125
                'name' => '004_facturacion_residentes',
126
                'page_from' => __CLASS__,
127
                'page_to' => __CLASS__,
128
                'type' => 'head',
129
                'text' => '<script src="'.FS_PATH.
130
                            'plugins/residentes/view/js/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.js" type="text/javascript"></script>',
131
                'params' => ''
132
            ),
133
            array(
134
                'name' => '005_facturacion_residentes',
135
                'page_from' => __CLASS__,
136
                'page_to' => __CLASS__,
137
                'type' => 'head',
138
                'text' => '<script src="'.FS_PATH.
139
                            'plugins/residentes/view/js/bootstrap-wysihtml5/locales/bootstrap-wysihtml5.es-ES.js" type="text/javascript"></script>',
140
                'params' => ''
141
            ),
142
            array(
143
                'name' => '006_facturacion_residentes',
144
                'page_from' => __CLASS__,
145
                'page_to' => __CLASS__,
146
                'type' => 'head',
147
                'text' => '<link rel="stylesheet" type="text/css" media="screen" href="' . FS_PATH .
148
                    'plugins/residentes/view/js/bootstrap-wysihtml5/bootstrap3-wysihtml5.css"/>',
149
                'params' => ''
150
            ),
151
152
        );
153
154
        foreach ($extensiones as $ext) {
155
            $fsext0 = new fs_extension($ext);
156
            if (!$fsext0->save()) {
157
                $this->new_error_msg('Imposible guardar los datos de la extensión ' . $ext['name'] . '.');
158
            }
159
        }
160
    }
161
    
162
    public function tratarAccion()
163
    {
164
        $accion = $this->filter_request('accion');
165
        switch ($accion) {
166
            case "nueva_programacion":
167
                $this->template = 'block/nueva_programacion_facturacion';
168
                break;
169
            case "configurar_metodo_envio":
170
                $this->template = 'block/configurar_metodo_envio';
171
                break;
172
            case "guardar_configuracion_envio":
173
                $this->guardarConfiguracionMetodoEnvio();
174
                break;
175
            case "guardar_programacion":
176
                $this->guardarProgramacion();
177
                break;
178
            case "eliminar_programacion":
179
                $this->eliminarProgramacion();
180
                break;
181
            case "reiniciar_programacion":
182
                $this->reiniciarProgramacion();
183
                break;
184
            case "lista_programacion":
185
                $this->listaProgramacion();
186
                break;
187
            case "test_email":
188
                $this->testEmail();
189
                break;
190
            default:
191
                break;
192
        }
193
    }
194
    
195
    public function listaProgramacion()
196
    {
197
        $idProgramacion = $this->filter_request('idprogramacion');
198
        $this->template = 'block/lista_programacion_facturacion';
199
        $rfpe = new residentes_facturacion_programada_edificaciones();
200
        $this->lista_programaciones = $rfpe->get_lista_edificaciones($idProgramacion);
0 ignored issues
show
Bug Best Practice introduced by
The property lista_programaciones does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
201
        $this->idProg = $idProgramacion;
202
    }
203
    
204
    public function eliminarProgramacion()
205
    {
206
        $estado = false;
207
        $idProgramacion = $this->filter_request('idprogramacion');
208
        if ($this->user->allow_delete_on(__CLASS__) && isset($idProgramacion) && $idProgramacion !== '') {
209
            $programaciones = new residentes_facturacion_programada();
210
            $programa = $programaciones->get($idProgramacion);
0 ignored issues
show
$idProgramacion of type boolean|string is incompatible with the type integer expected by parameter $id of FacturaScripts\model\res...acion_programada::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

210
            $programa = $programaciones->get(/** @scrutinizer ignore-type */ $idProgramacion);
Loading history...
211
            if ($programa) {
212
                $estado = $programa->delete();
213
            }
214
        }
215
        if ($estado === true) {
216
            $this->new_message('Programaci&oacute;n eliminada correctamente.');
217
        } else {
218
            $this->new_error_msg('La Programaci&oacute;n no pudo ser eliminada, verifique los datos o sus permisos.');
219
        }
220
    }
221
    
222
    public function reiniciarProgramacion()
223
    {
224
        $estado = false;
225
        $idProgramacion = $this->filter_request('idprogramacion');
226
        if ($this->user->allow_delete_on(__CLASS__) && isset($idProgramacion) && $idProgramacion !== '') {
227
            $programaciones = new residentes_facturacion_programada();
228
            $programa = $programaciones->get($idProgramacion);
0 ignored issues
show
$idProgramacion of type boolean|string is incompatible with the type integer expected by parameter $id of FacturaScripts\model\res...acion_programada::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

228
            $programa = $programaciones->get(/** @scrutinizer ignore-type */ $idProgramacion);
Loading history...
229
            if ($programa) {
230
                $programa->eliminar_facturas();
231
                $programa->estado = 'ENCOLA';
232
                $programa->facturas_generadas = 0;
233
                $estado = $programa->save();
234
            }
235
        }
236
        if ($estado === true) {
237
            $this->new_message('Programaci&oacute;n reiniciada correctamente.');
238
        } else {
239
            $this->new_error_msg('La Programaci&oacute;n no pudo ser reiniciada, verifique los datos o sus permisos.');
240
        }
241
    }
242
243
    public function guardarConfiguracionMetodoEnvio()
244
    {
245
        $this->template = 'block/configurar_metodo_envio';
246
        $id = $this->filter_request('id');
247
        $tiposervicio = $this->filter_request('tiposervicio');
248
        $apikey = $this->filter_request('apikey');
249
        $apisecret = $this->filter_request('apisecret');
250
        $apisenderemail = $this->filter_request('apisenderemail');
251
        $apisendername = $this->filter_request('apisendername');
252
        $emailsubject = stripslashes(htmlentities($this->filter_request('emailsubject')));
0 ignored issues
show
It seems like $this->filter_request('emailsubject') can also be of type boolean; however, parameter $string of htmlentities() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

252
        $emailsubject = stripslashes(htmlentities(/** @scrutinizer ignore-type */ $this->filter_request('emailsubject')));
Loading history...
253
        $remcfg = new residentes_email_config();
254
        $remcfg->id = $id;
255
        $remcfg->tiposervicio = $tiposervicio;
256
        $remcfg->apikey = $apikey;
257
        $remcfg->apisecret = $apisecret;
258
        $remcfg->apisenderemail = $apisenderemail;
259
        $remcfg->apisendername = $apisendername;
260
        $remcfg->emailsubject = $emailsubject;
261
        $remcfg->fecha_creacion = \date('Y-m-d h:i:s');
262
        $remcfg->usuario_creacion = $this->user->nick;
263
        $remcfg->fecha_modificacion = \date('Y-m-d h:i:s');
264
        $remcfg->usuario_modificacion = $this->user->nick;
265
        return $remcfg->save();
266
        $this->new_message('Configuración de envio guardada.');
0 ignored issues
show
$this->new_message('Conf... is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
267
    }
268
269
    public function testEmail()
270
    {
271
        $this->template = false;
272
        header('Content-Type: application/json');
273
        $cfg = new residentes_email_config();
274
        $actualConfig = $cfg->currentConfig();
275
        $envioMails = new ResidentesEnviarMail();
276
        switch($actualConfig->tiposervicio) {
277
            case "mailjet":
278
                echo $envioMails->mailjetEmailTest();
279
                break;
280
            case "sendgrid":
281
                echo $envioMails->sendgridEmailTest();
282
                break;
283
            case "interno": default:
284
                echo $envioMails->internalEmailTest();
0 ignored issues
show
Are you sure the usage of $envioMails->internalEmailTest() targeting ResidentesEnviarMail::internalEmailTest() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
285
                break;
286
        }
287
288
    }
289
    
290
    public function guardarProgramacion()
291
    {
292
        $residentesProcesar = $this->cantidadResidentesProcesar();
293
        $cantidadResidentesProcesar  = count($residentesProcesar);
294
        $idProgramacion = $this->cabeceraProgramacion($cantidadResidentesProcesar);
295
        
296
        if ($idProgramacion) {
297
            $this->detalleProgramacion($idProgramacion);
298
            $this->edificacionesProgramacion($idProgramacion, $residentesProcesar);
299
        }
300
        $this->new_message('¡Programaci&oacute;n generada!');
301
    }
302
    
303
    private function cabeceraProgramacion($cantidadResidentesProcesar)
304
    {
305
        //Cabecera de Programacion
306
        $id = $this->filter_request('id');
307
        $descripcion = $this->filter_request('descripcion');
308
        $tipo_programacion = $this->filter_request('tipo_programacion');
309
        $forma_pago = $this->filter_request('forma_pago');
310
        $fecha_vencimiento = $this->filter_request('fecha_vencimiento');
311
        $fecha_envio = $this->filter_request('fecha_envio');
312
        $hora_envio = $this->filter_request('hora_envio');
313
        
314
        $rfp = new residentes_facturacion_programada();
315
        $rfp->id = (isset($id) && $id !== '') ? $id : null;
316
        $rfp->descripcion = htmlentities(trim($descripcion));
0 ignored issues
show
It seems like $descripcion can also be of type boolean; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

316
        $rfp->descripcion = htmlentities(trim(/** @scrutinizer ignore-type */ $descripcion));
Loading history...
317
        $rfp->tipo_programacion = $tipo_programacion;
318
        $rfp->forma_pago = $forma_pago;
319
        $rfp->fecha_vencimiento = $fecha_vencimiento;
320
        $rfp->fecha_envio = $fecha_envio;
321
        $rfp->hora_envio = $hora_envio;
322
        $rfp->residentes_facturar = $cantidadResidentesProcesar;
323
        $rfp->estado = 'ENCOLA';
324
        $rfp->fecha_creacion = \date('Y-m-d h:i:s');
325
        $rfp->usuario_creacion = $this->user->nick;
326
        $rfp->fecha_modificacion = \date('Y-m-d h:i:s');
327
        $rfp->usuario_modificacion = $this->user->nick;
328
        
329
        return $rfp->save();
330
    }
331
    
332
    public function detalleProgramacion($idProgramacion)
333
    {
334
        $referencias = $this->filter_request_array('referencia');
335
        $cantidades = $this->filter_request_array('cantidad');
336
        $pvps = $this->filter_request_array('pvp');
337
        $impuestos = $this->filter_request_array('impuesto');
338
        $importes = $this->filter_request_array('importe');
339
        foreach ($referencias as $id => $referencia) {
0 ignored issues
show
The expression $referencias of type boolean|string is not traversable.
Loading history...
340
            $rfpc = new residentes_facturacion_programada_conceptos();
341
            $rfpc->idprogramacion = $idProgramacion;
342
            $rfpc->referencia = $referencia;
343
            $rfpc->cantidad = $cantidades[$id];
344
            $rfpc->pvp = $pvps[$id];
345
            $rfpc->codimpuesto = $impuestos[$id];
346
            $rfpc->importe = $importes[$id];
347
            if (!$rfpc->save()) {
348
                $this->new_error_msg('¡Ocurri&oacute; un error al grabar el concepto con codigo: '.$referencia);
349
            }
350
        }
351
        return true;
352
    }
353
    
354
    public function edificacionesProgramacion($idProgramacion, $residentesProcesar)
355
    {
356
        $edificaciones_residentes = new residentes_edificaciones();
357
        foreach ($residentesProcesar as $codcliente => $datos) {
358
            $data_edificacion = $edificaciones_residentes->get_by_field('codcliente', $codcliente);
359
            $rfpe = new residentes_facturacion_programada_edificaciones();
360
            $rfpe->idprogramacion = $idProgramacion;
361
            $rfpe->codcliente = $codcliente;
362
            $rfpe->id_edificacion = $data_edificacion[0]->id;
363
            $rfpe->save();
364
        }
365
    }
366
    
367
    private function cantidadResidentesProcesar()
368
    {
369
        $listaResidentes = [];
370
        $iEdificaciones = $this->filter_request_array('edificacion');
371
        $edificaciones_mapa = new residentes_edificaciones_mapa();
372
        foreach ($iEdificaciones as $edificacion_id) {
0 ignored issues
show
The expression $iEdificaciones of type boolean|string is not traversable.
Loading history...
373
            $listaEdificaciones = $edificaciones_mapa->get_by_field('padre_id', $edificacion_id);
374
            foreach ($listaEdificaciones as $edificacion) {
375
                $this->cargarResidentesEdificacion($edificacion->id, $listaResidentes);
376
            }
377
        }
378
        return $listaResidentes;
379
    }
380
    
381
    private function cargarResidentesEdificacion($edificacion, &$listaResidentes)
382
    {
383
        $residentesDisponibles = [];
384
        $edificaciones_residentes = new residentes_edificaciones();
385
        $edificacionesDisponibles = $edificaciones_residentes->get_by_field('id_edificacion', $edificacion);
386
        foreach ($edificacionesDisponibles as $edif) {
387
            if ($edif->ocupado === true) {
388
                $residentesDisponibles[] = ['id_edificacion'=>$edif->id_edificacion,'codcliente'=>$edif->codcliente];
389
            }
390
        }
391
        $this->cargarResidentesFacturables($residentesDisponibles, $listaResidentes);
392
    }
393
    
394
    private function cargarResidentesFacturables($residentesDisponibles, &$listaResidentes)
395
    {
396
        foreach ($residentesDisponibles as $datosResidente) {
397
            $this->generarPrefacturacion($datosResidente['codcliente'], $listaResidentes);
398
        }
399
    }
400
    
401
    private function generarPrefacturacion($codcliente, &$listaResidentes)
402
    {
403
        $listaReferencias = $this->filter_request_array('referencia');
404
        foreach ($listaReferencias as $referencia) {
0 ignored issues
show
The expression $listaReferencias of type boolean|string is not traversable.
Loading history...
405
            $sql = "SELECT count(referencia) as facturado from lineasfacturascli where referencia = '".$referencia."' ".
406
               " AND idfactura IN (select idfactura from facturascli WHERE codcliente = '".$codcliente."');";
407
            $data = $this->db->select($sql);
408
            if (!$data[0]['facturado']) {
409
                $listaResidentes[$codcliente][] = $referencia;
410
            }
411
        }
412
    }
413
}
414