Passed
Push — master ( 3906a7...f39060 )
by Joe Nilson
02:19
created

residentesCronJobs::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
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/residentesFacturaProgramada.php';
18
19
class residentesCronJobs
20
{
21
    public $db;
22
    public $log;
23
    public $ahora;
24
    public $horaActual;
25
    public $residentesFactProg;
26
    public $residentesFactProgEdif;
27
    public $residentesFactProgCon;
28
    public $residentesFacturaProgramada;
29
    public function __construct(&$db, &$core_log)
30
    {
31
        $this->db = $db;
32
        $this->log = $core_log;
33
        $this->ahora = \date('Y-m-d');
34
        $this->horaActual = \date('H');
35
        $this->residentesFactProg = new residentes_facturacion_programada();
36
        $this->residentesFactProgEdif = new residentes_facturacion_programada_edificaciones();
37
        $this->residentesFactProgCon = new residentes_facturacion_programada_conceptos();
38
        $this->residentesFacturaProgramada = new residentesFacturaProgramada($db, $core_log);
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function delete()
45
    {
46
        // TODO: Implement delete() method.
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function exists()
53
    {
54
        // TODO: Implement exists() method.
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function save()
61
    {
62
        // TODO: Implement save() method.
63
    }
64
65
    public function startJob($jobDisponible)
66
    {
67
        $idProg = $jobDisponible->id;
68
        $listaResidentes = $this->residentesFactProgEdif->getByIdProgramacionPendientes($idProg);
69
        foreach ($listaResidentes as $residente) {
70
            $this->stepJob($residente, $jobDisponible);
71
        }
72
73
        $this->finishJob($jobDisponible);
74
    }
75
76
    /**
77
     * @param object $residente
78
     * @param object $jobDisponible
79
     */
80
    public function stepJob(&$residente, &$jobDisponible)
81
    {
82
        if ($residente->procesado === false) {
83
            $this->residentesFacturaProgramada->nuevaFactura($residente, $jobDisponible);
84
        }
85
    }
86
87
    public function finishJob(&$jobDisponible)
88
    {
89
        $residentesPendientes = $this->residentesFactProgEdif->getByIdProgramacionPendientes($jobDisponible->id);
90
        $residentesFacturados = $this->residentesFactProgEdif->getByIdProgramacion($jobDisponible->id);
91
        if ($residentesPendientes === false) {
92
            $jobDisponible->estado = 'CONCLUIDO';
93
            $jobDisponible->facturas_generadas = ($residentesFacturados) ? count($residentesFacturados) : 0;
94
            $jobDisponible->usuario_modificacion = 'cron';
95
            $jobDisponible->fecha_modificacion = \date('Y-m-d H:i:s');
96
            $jobDisponible->save();
97
        }
98
    }
99
100
    public function initCron()
101
    {
102
        $jobEncola = $this->residentesFactProg->get_by_date_hour_status($this->ahora, $this->horaActual, 'ENCOLA');
103
        $jobEnProceso = $this->residentesFactProg->get_by_date_hour_status($this->ahora, $this->horaActual, 'ENPROCESO');
104
        $jobDisponible = ($jobEncola) ?: $jobEnProceso;
105
        if ($jobDisponible) {
106
            echo " ** Se inicia el proceso de Facturación Programada ".$this->ahora." ".$this->horaActual." ** \n";
107
            $this->log->new_advice(' ** Se inicia el proceso de Facturación Programada ** ');
108
            $jobDisponible->estado = 'ENPROCESO';
109
            $jobDisponible->usuario_modificacion = 'cron';
110
            $jobDisponible->fecha_modificacion = \date('Y-m-d H:i:s');
111
            $jobDisponible->save();
112
            $this->startJob($jobDisponible);
113
        } else {
114
            echo " ** No coincide la hora de proceso con la de ejecucion de cron se omite el proceso ".
115
                $this->ahora . " " . $this->horaActual . " ** \n";
116
            $this->log->new_advice(' ** No coincide la hora de proceso con la de ejecucion de cron se omite el proceso ** ');
117
        }
118
    }
119
120
    public function statusJob()
121
    {
122
        $jobDisponible = $this->residentesFactProg->get_by_date_hour_status($this->ahora, $this->horaActual, 'ENPROCESO');
123
        if ($jobDisponible) {
124
            return 'ENPROCESO';
125
        }
126
        return false;
127
    }
128
}