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

nuevaVerificacionContabilidadFactura()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 10
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 'base/fs_model.php';
18
19
class residentesFacturaProgramada extends fs_model
20
{
21
    public $db;
22
    public $log;
23
    public $ahora;
24
    public $horaActual;
25
    public $residentesFactProg;
26
    public $residentesFactProgEdif;
27
    public $residentesFactProgCon;
28
    public function __construct(&$db, &$core_log)
29
    {
30
        $this->db = $db;
31
        $this->log = $core_log;
32
        $this->ahora = \date('Y-m-d');
33
        $this->horaActual = \date('H');
34
        $this->residentesFactProg = new residentes_facturacion_programada();
35
        $this->residentesFactProgEdif = new residentes_facturacion_programada_edificaciones();
36
        $this->residentesFactProgCon = new residentes_facturacion_programada_conceptos();
37
    }
38
39
    public function conceptoFacturable($codcliente, $referencia)
40
    {
41
        $sql = "SELECT count(referencia) as facturado from lineasfacturascli where referencia = ".
42
            $this->var2str($referencia) .
43
            " AND idfactura IN (select idfactura from facturascli WHERE codcliente = ".$this->var2str($codcliente).");";
44
        $data = $this->db->select($sql);
45
        if (!$data[0]['facturado']) {
46
            return true;
47
        }
48
        return false;
49
    }
50
51
    public function nuevaFactura($residenteProg, &$jobDisponible)
52
    {
53
        $clienteTable = new cliente();
54
        $empresaTable = new empresa();
1 ignored issue
show
Bug introduced by
The type empresa 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...
55
        $residente = $clienteTable->get($residenteProg->codcliente);
56
        if ($residente) {
57
            $factura = new factura_cliente();
58
            $this->nuevaCabeceraFactura($factura, $residente, $empresaTable, $jobDisponible);
59
60
            if ($factura->save()) {
61
                $listaArticulos = $this->residentesFactProgCon->getConceptosByIdProgramacion($residenteProg->idprogramacion);
62
63
                $this->nuevoDetalleFactura($factura, $residente, $listaArticulos);
64
65
                $this->nuevoTotalFactura($factura, $residenteProg, $empresaTable);
66
                ++$jobDisponible->facturas_generadas;
67
                $jobDisponible->save();
68
            } else {
69
                $this->log->new_error_msg('Imposible guardar la factura.');
70
            }
71
        } else {
72
            $this->log->new_error_msg('Cliente no encontrado.');
73
        }
74
    }
75
76
    public function nuevaCabeceraFactura(&$factura, &$residente, &$empresaTable, &$jobDisponible)
77
    {
78
        $factura->codserie = ($residente->codserie) ?: $empresaTable->codserie;
79
        $factura->codpago = $jobDisponible->forma_pago;
80
        $factura->codalmacen = $empresaTable->codalmacen;
81
        $factura->codagente = '1';
82
        $factura->set_fecha_hora(\date('Y-m-d'), \date('H:i:s'));
83
84
        $this->nuevaVerificacionContabilidadFactura($factura, $residente, $empresaTable);
85
86
        $this->nuevaDivisaFactura($factura, $residente);
87
88
        $factura->codcliente = $residente->codcliente;
89
        $this->nuevaInformacionResidenteFactura($factura, $residente);
90
91
        /// función auxiliar para implementar en los plugins que lo necesiten
92
        if (!fs_generar_numero2($factura)) {
93
            $factura->numero2 = '';
94
            echo "No hay funcion libre. \n";
95
        }
96
    }
97
98
    public function nuevoDetalleFactura(&$factura, &$residente, $listaArticulos)
99
    {
100
        $art0 = new articulo();
1 ignored issue
show
Bug introduced by
The type articulo 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...
101
        $impuesto = new impuesto();
1 ignored issue
show
Bug introduced by
The type impuesto 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...
102
103
        foreach ($listaArticulos as $concepto) {
104
            $art = $art0->get($concepto->referencia);
105
            if ($this->conceptoFacturable($residente->codcliente, $concepto->referencia)) {
106
                $linea = new linea_factura_cliente();
1 ignored issue
show
Bug introduced by
The type linea_factura_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...
107
                $linea->idfactura = $factura->idfactura;
108
                $linea->referencia = $concepto->referencia;
109
                $linea->descripcion = $art->descripcion;
110
                $linea->cantidad = $concepto->cantidad;
111
                $imp = $impuesto->get($concepto->codimpuesto);
112
                $linea->codimpuesto = $imp->codimpuesto;
113
                $linea->iva = $imp->iva;
114
                $linea->pvpsindto = $concepto->pvp;
115
                $linea->pvpunitario = $concepto->pvp;
116
                $linea->pvptotal = $concepto->pvp * $concepto->cantidad;
117
                $this->nuevoTotalLineasFactura($factura, $linea);
118
            }
119
        }
120
    }
121
122
    public function nuevoTotalLineasFactura(&$factura, &$linea)
123
    {
124
        if ($linea->save()) {
125
            $factura->neto += $linea->pvptotal;
126
            $factura->totaliva += $linea->pvptotal * $linea->iva / 100;
127
        }
128
    }
129
130
    /**
131
     * @param factura_cliente $factura
132
     * @param object $residenteProgramado
133
     * @param object $empresaTable
134
     */
135
    public function nuevoTotalFactura(&$factura, &$residenteProgramado, &$empresaTable)
136
    {
137
        /// redondeamos
138
        $factura->neto = round($factura->neto, FS_NF0);
1 ignored issue
show
Bug introduced by
The constant FS_NF0 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
139
        $factura->totaliva = round($factura->totaliva, FS_NF0);
140
        $factura->totalirpf = round($factura->totalirpf, FS_NF0);
141
        $factura->totalrecargo = round($factura->totalrecargo, FS_NF0);
142
        $factura->total = $factura->neto + $factura->totaliva - $factura->totalirpf + $factura->totalrecargo;
143
        if ($factura->save()) {
144
            $this->generar_asiento($factura, $empresaTable);
145
            /// Función de ejecución de tareas post guardado correcto de la factura
146
            fs_documento_post_save($factura);
147
            //Actualizamos la data del residente
148
            $residenteProgramado->idfactura = $factura->idfactura;
149
            $residenteProgramado->procesado = true;
150
            $residenteProgramado->save();
151
        } else {
152
            $factura->delete();
153
        }
154
    }
155
156
    public function nuevaVerificacionContabilidadFactura(&$factura, &$residente, &$empresaTable)
157
    {
158
        $eje0 = new ejercicio();
1 ignored issue
show
Bug introduced by
The type ejercicio 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...
159
        $ejercicio = $eje0->get_by_fecha(date('d-m-Y'));
160
        if ($ejercicio) {
161
            $factura->codejercicio = $ejercicio->codejercicio;
162
        }
163
        if ($empresaTable->contintegrada) {
164
            /// forzamos crear la subcuenta
165
            $residente->get_subcuenta($empresaTable->codejercicio);
166
        }
167
    }
168
169
    public function nuevaDivisaFactura(&$factura, &$residente)
170
    {
171
        $div0 = new divisa();
1 ignored issue
show
Bug introduced by
The type divisa 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...
172
        $divisa = $div0->get($residente->coddivisa);
173
        if ($divisa) {
174
            $factura->coddivisa = $divisa->coddivisa;
175
            $factura->tasaconv = $divisa->tasaconv;
176
        }
177
    }
178
179
    public function nuevaInformacionResidenteFactura(&$factura, &$residente)
180
    {
181
        foreach ($residente->get_direcciones() as $d) {
182
            if ($d->domfacturacion) {
183
                $factura->codcliente = $residente->codcliente;
184
                $factura->cifnif = $residente->cifnif;
185
                $factura->nombrecliente = $residente->nombre;
186
                $factura->apartado = $d->apartado;
187
                $factura->ciudad = $d->ciudad;
188
                $factura->coddir = $d->id;
189
                $factura->codpais = $d->codpais;
190
                $factura->codpostal = $d->codpostal;
191
                $factura->direccion = $d->direccion;
192
                $factura->provincia = $d->provincia;
193
                break;
194
            }
195
        }
196
    }
197
198
    /**
199
     * Genera el asiento para la factura, si procede
200
     * @param factura_cliente $factura
201
     */
202
    public function generar_asiento(&$factura, &$empresaTable)
203
    {
204
        if ($empresaTable->contintegrada) {
205
            $asiento_factura = new asiento_factura();
1 ignored issue
show
Bug introduced by
The type asiento_factura 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...
206
            $asiento_factura->generar_asiento_venta($factura);
207
        } else {
208
            /// de todas formas forzamos la generación de las líneas de iva
209
            $factura->get_lineas_iva();
210
        }
211
    }
212
213
    public function delete()
214
    {
215
        // TODO: Implement delete() method.
216
    }
217
218
    public function exists()
219
    {
220
        // TODO: Implement exists() method.
221
    }
222
223
    public function save()
224
    {
225
        // TODO: Implement save() method.
226
    }
227
}