ventas_albaran   F
last analyzed

Complexity

Total Complexity 71

Size/Duplication

Total Lines 435
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 277
c 2
b 1
f 0
dl 0
loc 435
rs 2.7199
wmc 71

6 Methods

Rating   Name   Duplication   Size   Complexity  
A generar_factura() 0 3 1
A url() 0 9 3
F modificar() 0 292 49
A propagar_cifnif() 0 20 5
A __construct() 0 3 1
C private_core() 0 70 12

How to fix   Complexity   

Complex Class

Complex classes like ventas_albaran often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ventas_albaran, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * This file is part of facturacion_base
4
 * Copyright (C) 2013-2017  Carlos Garcia Gomez  [email protected]
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
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (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/republica_dominicana/extras/rd_controller.php';
21
22
class ventas_albaran extends rd_controller
23
{
24
25
    public $agencia;
26
    public $agente;
27
    public $albaran;
28
    public $allow_delete_fac;
29
    public $almacen;
30
    public $cliente;
31
    public $cliente_s;
32
    public $divisa;
33
    public $ejercicio;
34
    public $fabricante;
35
    public $familia;
36
    public $forma_pago;
37
    public $impuesto;
38
    public $nuevo_albaran_url;
39
    public $pais;
40
    public $serie;
41
42
    public function __construct()
43
    {
44
        parent::__construct(__CLASS__, FS_ALBARAN . ' de cliente', 'ventas', FALSE, FALSE);
0 ignored issues
show
Bug introduced by
The constant FS_ALBARAN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
45
    }
46
47
    protected function private_core()
48
    {
49
        parent::private_core();
50
51
        $this->ppage = $this->page->get('ventas_albaranes');
0 ignored issues
show
Bug Best Practice introduced by
The property ppage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
52
        $this->agente = FALSE;
53
54
        $this->agencia = new agencia_transporte();
0 ignored issues
show
Bug introduced by
The type agencia_transporte 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
        $albaran = new albaran_cliente();
0 ignored issues
show
Bug introduced by
The type albaran_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...
56
        $this->albaran = FALSE;
57
        $this->almacen = new almacen();
0 ignored issues
show
Bug introduced by
The type almacen 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...
58
        $this->cliente = new cliente();
0 ignored issues
show
Bug introduced by
The type 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...
59
        $this->cliente_s = FALSE;
60
        $this->divisa = new divisa();
0 ignored issues
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...
61
        $this->ejercicio = new ejercicio();
0 ignored issues
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...
62
        $this->fabricante = new fabricante();
0 ignored issues
show
Bug introduced by
The type fabricante 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...
63
        $this->familia = new familia();
0 ignored issues
show
Bug introduced by
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...
64
        $this->forma_pago = new forma_pago();
0 ignored issues
show
Bug introduced by
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...
65
        $this->impuesto = new impuesto();
0 ignored issues
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...
66
        $this->nuevo_albaran_url = FALSE;
67
        $this->pais = new pais();
0 ignored issues
show
Bug introduced by
The type pais 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...
68
        $this->serie = new serie();
0 ignored issues
show
Bug introduced by
The type serie 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...
69
70
        /// ¿El usuario tiene permiso para eliminar la factura?
71
        $this->allow_delete_fac = $this->user->allow_delete_on('ventas_factura');
72
73
        /**
74
         * Comprobamos si el usuario tiene acceso a nueva_venta,
75
         * necesario para poder añadir líneas.
76
         */
77
        if ($this->user->have_access_to('nueva_venta', FALSE)) {
78
            $nuevoalbp = $this->page->get('nueva_venta');
79
            if ($nuevoalbp) {
80
                $this->nuevo_albaran_url = $nuevoalbp->url();
81
            }
82
        }
83
84
        if (isset($_POST['idalbaran'])) {
85
            $this->albaran = $albaran->get($_POST['idalbaran']);
86
            $this->modificar();
87
        } else if (isset($_GET['id'])) {
88
            $this->albaran = $albaran->get($_GET['id']);
89
        }
90
91
        if ($this->albaran) {
92
            $this->page->title = $this->albaran->codigo;
93
94
            /// cargamos el agente
95
            if (!is_null($this->albaran->codagente)) {
96
                $agente = new agente();
0 ignored issues
show
Bug introduced by
The type agente 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...
97
                $this->agente = $agente->get($this->albaran->codagente);
98
            }
99
100
            /// cargamos el cliente
101
            $this->cliente_s = $this->cliente->get($this->albaran->codcliente);
102
103
            /// comprobamos el albarán
104
            $this->albaran->full_test();
105
106
            if (isset($_REQUEST['facturar']) && isset($_REQUEST['petid'])) {
107
                if ($this->duplicated_petition($_REQUEST['petid'])) {
108
                    $this->new_error_msg('Petición duplicada. Evita hacer doble clic sobre los botones.');
109
                } else if (!$this->albaran->ptefactura || !is_null($this->albaran->idfactura)) {
110
                    $this->new_error_msg('Parece que este ' . FS_ALBARAN . ' ya está facturado.');
0 ignored issues
show
Bug introduced by
The constant FS_ALBARAN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
111
                } else {
112
                    $this->generar_factura();
113
                }
114
            }
115
        } else {
116
            $this->new_error_msg("¡" . ucfirst(FS_ALBARAN) . " de venta no encontrado!", 'error', FALSE, FALSE);
117
        }
118
    }
119
120
    public function url()
121
    {
122
        if (!isset($this->albaran)) {
123
            return parent::url();
124
        } else if ($this->albaran) {
125
            return $this->albaran->url();
126
        }
127
128
        return $this->page->url();
129
    }
130
131
    private function modificar()
132
    {
133
        $this->albaran->observaciones = $_POST['observaciones'];
134
135
        /// ¿Es editable o ya ha sido facturado?
136
        if ($this->albaran->ptefactura) {
137
            $eje0 = $this->ejercicio->get_by_fecha($_POST['fecha'], FALSE);
138
            if ($eje0) {
139
                $this->albaran->fecha = $_POST['fecha'];
140
                $this->albaran->hora = $_POST['hora'];
141
142
                if ($this->albaran->codejercicio != $eje0->codejercicio) {
143
                    $this->albaran->codejercicio = $eje0->codejercicio;
144
                    $this->albaran->new_codigo();
145
                }
146
            } else {
147
                $this->new_error_msg('Ningún ejercicio encontrado.');
148
            }
149
150
            /// ¿cambiamos el cliente?
151
            if ($_POST['cliente'] != $this->albaran->codcliente) {
152
                $cliente = $this->cliente->get($_POST['cliente']);
153
                if ($cliente) {
154
                    $this->albaran->codcliente = $cliente->codcliente;
155
                    $this->albaran->cifnif = $cliente->cifnif;
156
                    $this->albaran->nombrecliente = $cliente->razonsocial;
157
                    $this->albaran->apartado = NULL;
158
                    $this->albaran->ciudad = NULL;
159
                    $this->albaran->coddir = NULL;
160
                    $this->albaran->codpais = NULL;
161
                    $this->albaran->codpostal = NULL;
162
                    $this->albaran->direccion = NULL;
163
                    $this->albaran->provincia = NULL;
164
165
                    foreach ($cliente->get_direcciones() as $d) {
166
                        if ($d->domfacturacion) {
167
                            $this->albaran->apartado = $d->apartado;
168
                            $this->albaran->ciudad = $d->ciudad;
169
                            $this->albaran->coddir = $d->id;
170
                            $this->albaran->codpais = $d->codpais;
171
                            $this->albaran->codpostal = $d->codpostal;
172
                            $this->albaran->direccion = $d->direccion;
173
                            $this->albaran->provincia = $d->provincia;
174
                            break;
175
                        }
176
                    }
177
                } else {
178
                    $this->albaran->codcliente = NULL;
179
                    $this->albaran->nombrecliente = $_POST['nombrecliente'];
180
                    $this->albaran->cifnif = $_POST['cifnif'];
181
                    $this->albaran->coddir = NULL;
182
                }
183
            } else {
184
                $this->albaran->nombrecliente = $_POST['nombrecliente'];
185
                $this->albaran->cifnif = $_POST['cifnif'];
186
                $this->albaran->codpais = $_POST['codpais'];
187
                $this->albaran->provincia = $_POST['provincia'];
188
                $this->albaran->ciudad = $_POST['ciudad'];
189
                $this->albaran->codpostal = $_POST['codpostal'];
190
                $this->albaran->direccion = $_POST['direccion'];
191
                $this->albaran->apartado = $_POST['apartado'];
192
193
                $this->albaran->envio_nombre = $_POST['envio_nombre'];
194
                $this->albaran->envio_apellidos = $_POST['envio_apellidos'];
195
                $this->albaran->envio_codtrans = NULL;
196
                if ($_POST['envio_codtrans'] != '') {
197
                    $this->albaran->envio_codtrans = $_POST['envio_codtrans'];
198
                }
199
                $this->albaran->envio_codigo = $_POST['envio_codigo'];
200
                $this->albaran->envio_codpais = $_POST['envio_codpais'];
201
                $this->albaran->envio_provincia = $_POST['envio_provincia'];
202
                $this->albaran->envio_ciudad = $_POST['envio_ciudad'];
203
                $this->albaran->envio_codpostal = $_POST['envio_codpostal'];
204
                $this->albaran->envio_direccion = $_POST['envio_direccion'];
205
                $this->albaran->envio_apartado = $_POST['envio_apartado'];
206
207
                $cliente = $this->cliente->get($this->albaran->codcliente);
208
            }
209
210
            $serie = $this->serie->get($this->albaran->codserie);
211
212
            /// ¿cambiamos la serie?
213
            if ($_POST['serie'] != $this->albaran->codserie) {
214
                $serie2 = $this->serie->get($_POST['serie']);
215
                if ($serie2) {
216
                    $this->albaran->codserie = $serie2->codserie;
217
                    $this->albaran->new_codigo();
218
219
                    $serie = $serie2;
220
                }
221
            }
222
223
            $this->albaran->codpago = $_POST['forma_pago'];
224
225
            /// ¿Cambiamos la divisa?
226
            if ($_POST['divisa'] != $this->albaran->coddivisa) {
227
                $divisa = $this->divisa->get($_POST['divisa']);
228
                if ($divisa) {
229
                    $this->albaran->coddivisa = $divisa->coddivisa;
230
                    $this->albaran->tasaconv = $divisa->tasaconv;
231
                }
232
            } else if ($_POST['tasaconv'] != '') {
233
                $this->albaran->tasaconv = floatval($_POST['tasaconv']);
234
            }
235
236
            if (isset($_POST['numlineas'])) {
237
                $numlineas = intval($_POST['numlineas']);
238
239
                $this->albaran->irpf = 0;
240
                $this->albaran->netosindto = 0;
241
                $this->albaran->neto = 0;
242
                $this->albaran->totaliva = 0;
243
                $this->albaran->totalirpf = 0;
244
                $this->albaran->totalrecargo = 0;
245
                $this->albaran->dtopor1 = floatval($_POST['adtopor1']);
246
                $this->albaran->dtopor2 = floatval($_POST['adtopor2']);
247
                $this->albaran->dtopor3 = floatval($_POST['adtopor3']);
248
                $this->albaran->dtopor4 = floatval($_POST['adtopor4']);
249
                $this->albaran->dtopor5 = floatval($_POST['adtopor5']);
250
251
                $lineas = $this->albaran->get_lineas();
252
                $articulo = new articulo();
253
254
                /// eliminamos las líneas que no encontremos en el $_POST
255
                foreach ($lineas as $l) {
256
                    $encontrada = FALSE;
257
                    for ($num = 0; $num <= $numlineas; $num++) {
258
                        if (isset($_POST['idlinea_' . $num]) && $l->idlinea == intval($_POST['idlinea_' . $num])) {
259
                            $encontrada = TRUE;
260
                            break;
261
                        }
262
                    }
263
                    if (!$encontrada) {
264
                        if ($l->delete()) {
265
                            /// actualizamos el stock
266
                            $art0 = $articulo->get($l->referencia);
267
                            if ($art0) {
268
                                $art0->sum_stock($this->albaran->codalmacen, $l->cantidad, FALSE, $l->codcombinacion);
269
                            }
270
                        } else {
271
                            $this->new_error_msg("¡Imposible eliminar la línea del artículo " . $l->referencia . "!");
272
                        }
273
                    }
274
                }
275
276
                $regimeniva = 'general';
277
                if ($cliente) {
278
                    $regimeniva = $cliente->regimeniva;
279
                }
280
281
                /// modificamos y/o añadimos las demás líneas
282
                for ($num = 0; $num <= $numlineas; $num++) {
283
                    $encontrada = FALSE;
284
                    if (isset($_POST['idlinea_' . $num])) {
285
                        foreach ($lineas as $k => $value) {
286
                            /// modificamos la línea
287
                            if ($value->idlinea == intval($_POST['idlinea_' . $num])) {
288
                                $encontrada = TRUE;
289
                                $cantidad_old = $value->cantidad;
290
                                $lineas[$k]->cantidad = floatval($_POST['cantidad_' . $num]);
291
                                $lineas[$k]->pvpunitario = floatval($_POST['pvp_' . $num]);
292
                                $lineas[$k]->dtopor = floatval(fs_filter_input_post('dto_' . $num, 0));
0 ignored issues
show
Bug introduced by
The function fs_filter_input_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

292
                                $lineas[$k]->dtopor = floatval(/** @scrutinizer ignore-call */ fs_filter_input_post('dto_' . $num, 0));
Loading history...
293
                                $lineas[$k]->dtopor2 = floatval(fs_filter_input_post('dto2_' . $num, 0));
294
                                $lineas[$k]->dtopor3 = floatval(fs_filter_input_post('dto3_' . $num, 0));
295
                                $lineas[$k]->dtopor4 = floatval(fs_filter_input_post('dto4_' . $num, 0));
296
                                $lineas[$k]->pvpsindto = $value->cantidad * $value->pvpunitario;
297
                                
298
                                // Descuento Unificado Equivalente
299
                                $due_linea = $this->fbase_calc_due(array($lineas[$k]->dtopor, $lineas[$k]->dtopor2, $lineas[$k]->dtopor3, $lineas[$k]->dtopor4));
300
                                $lineas[$k]->pvptotal = $lineas[$k]->cantidad * $lineas[$k]->pvpunitario * $due_linea;
301
                                
302
                                $lineas[$k]->descripcion = $_POST['desc_' . $num];
303
                                $lineas[$k]->codimpuesto = NULL;
304
                                $lineas[$k]->iva = 0;
305
                                $lineas[$k]->recargo = 0;
306
                                $lineas[$k]->irpf = floatval(fs_filter_input_post('irpf_' . $num, 0));
307
                                if (!$serie->siniva && $regimeniva != 'Exento') {
308
                                    $imp0 = $this->impuesto->get_by_iva($_POST['iva_' . $num]);
309
                                    if ($imp0) {
310
                                        $lineas[$k]->codimpuesto = $imp0->codimpuesto;
311
                                    }
312
313
                                    $lineas[$k]->iva = floatval($_POST['iva_' . $num]);
314
                                    $lineas[$k]->recargo = floatval(fs_filter_input_post('recargo_' . $num, 0));
315
                                }
316
317
                                if ($lineas[$k]->save()) {
318
                                    if ($value->irpf > $this->albaran->irpf) {
319
                                        $this->albaran->irpf = $value->irpf;
320
                                    }
321
322
                                    if ($lineas[$k]->cantidad != $cantidad_old) {
323
                                        /// actualizamos el stock
324
                                        $art0 = $articulo->get($value->referencia);
325
                                        if ($art0) {
326
                                            $art0->sum_stock($this->albaran->codalmacen, $cantidad_old - $lineas[$k]->cantidad, FALSE, $lineas[$k]->codcombinacion);
327
                                        }
328
                                    }
329
                                } else {
330
                                    $this->new_error_msg("¡Imposible modificar la línea del artículo " . $value->referencia . "!");
331
                                }
332
333
                                break;
334
                            }
335
                        }
336
337
                        /// añadimos la línea
338
                        if (!$encontrada && intval($_POST['idlinea_' . $num]) == -1 && isset($_POST['referencia_' . $num])) {
339
                            $linea = new linea_albaran_cliente();
0 ignored issues
show
Bug introduced by
The type linea_albaran_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...
340
                            $linea->idalbaran = $this->albaran->idalbaran;
341
                            $linea->descripcion = $_POST['desc_' . $num];
342
343
                            if (!$serie->siniva && $regimeniva != 'Exento') {
344
                                $imp0 = $this->impuesto->get_by_iva($_POST['iva_' . $num]);
345
                                if ($imp0) {
346
                                    $linea->codimpuesto = $imp0->codimpuesto;
347
                                }
348
349
                                $linea->iva = floatval($_POST['iva_' . $num]);
350
                                $linea->recargo = floatval(fs_filter_input_post('recargo_' . $num, 0));
351
                            }
352
353
                            $linea->irpf = floatval(fs_filter_input_post('irpf_' . $num, 0));
354
                            $linea->cantidad = floatval($_POST['cantidad_' . $num]);
355
                            $linea->pvpunitario = floatval($_POST['pvp_' . $num]);
356
                            $linea->dtopor = floatval(fs_filter_input_post('dto_' . $num, 0));
357
                            $linea->dtopor2 = floatval(fs_filter_input_post('dto2_' . $num, 0));
358
                            $linea->dtopor3 = floatval(fs_filter_input_post('dto3_' . $num, 0));
359
                            $linea->dtopor4 = floatval(fs_filter_input_post('dto4_' . $num, 0));
360
                            $linea->pvpsindto = $linea->cantidad * $linea->pvpunitario;
361
362
                            // Descuento Unificado Equivalente
363
                            $due_linea = $this->fbase_calc_due(array($linea->dtopor, $linea->dtopor2, $linea->dtopor3, $linea->dtopor4));
364
                            $linea->pvptotal = $linea->cantidad * $linea->pvpunitario * $due_linea;
365
366
                            $art0 = $articulo->get($_POST['referencia_' . $num]);
367
                            if ($art0) {
368
                                $linea->referencia = $art0->referencia;
369
                                if ($_POST['codcombinacion_' . $num]) {
370
                                    $linea->codcombinacion = $_POST['codcombinacion_' . $num];
371
                                }
372
                            }
373
374
                            if ($linea->save()) {
375
                                if ($art0) {
376
                                    /// actualizamos el stock
377
                                    $art0->sum_stock($this->albaran->codalmacen, 0 - $linea->cantidad, FALSE, $linea->codcombinacion);
378
                                }
379
380
                                if ($linea->irpf > $this->albaran->irpf) {
381
                                    $this->albaran->irpf = $linea->irpf;
382
                                }
383
                            } else {
384
                                $this->new_error_msg("¡Imposible guardar la línea del artículo " . $linea->referencia . "!");
385
                            }
386
                        }
387
                    }
388
                }
389
390
                /// obtenemos los subtotales por impuesto
391
                $due_totales = $this->fbase_calc_due([$this->albaran->dtopor1, $this->albaran->dtopor2, $this->albaran->dtopor3, $this->albaran->dtopor4, $this->albaran->dtopor5]);
392
                foreach ($this->fbase_get_subtotales_documento($this->albaran->get_lineas(), $due_totales) as $subt) {
393
                    $this->albaran->netosindto += $subt['netosindto'];
394
                    $this->albaran->neto += $subt['neto'];
395
                    $this->albaran->totaliva += $subt['iva'];
396
                    $this->albaran->totalirpf += $subt['irpf'];
397
                    $this->albaran->totalrecargo += $subt['recargo'];
398
                }
399
400
                $this->albaran->total = round($this->albaran->neto + $this->albaran->totaliva - $this->albaran->totalirpf + $this->albaran->totalrecargo, FS_NF0);
401
402
                if (abs(floatval($_POST['atotal']) - $this->albaran->total) > .01) {
403
                    $this->new_error_msg("El total difiere entre el controlador y la vista (" . $this->albaran->total .
404
                            " frente a " . $_POST['atotal'] . "). Debes informar del error.");
405
                }
406
            }
407
        }
408
409
        /// función auxiliar para implementar en los plugins que lo necesiten
410
        if (!fs_generar_numero2($this->albaran)) {
411
            $this->albaran->numero2 = $_POST['numero2'];
412
        }
413
414
        if ($this->albaran->save()) {
415
            /// Función de ejecución de tareas post guardado correcto del albarán
416
            fs_documento_post_save($this->albaran);
417
418
            $this->new_message(ucfirst(FS_ALBARAN) . " modificado correctamente.");
0 ignored issues
show
Bug introduced by
The constant FS_ALBARAN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
419
            $this->propagar_cifnif();
420
            $this->new_change(ucfirst(FS_ALBARAN) . ' Cliente ' . $this->albaran->codigo, $this->albaran->url());
421
        } else {
422
            $this->new_error_msg("¡Imposible modificar el " . FS_ALBARAN . "!");
423
        }
424
    }
425
426
    /**
427
     * Actualizamos el cif/nif en el cliente y los albaranes de este cliente que no tenga cif/nif
428
     */
429
    private function propagar_cifnif()
430
    {
431
        if ($this->albaran->cifnif) {
432
            /// buscamos el cliente
433
            $cliente = $this->cliente->get($this->albaran->codcliente);
434
            if ($cliente && !$cliente->cifnif) {
435
                /// actualizamos el cliente
436
                $cliente->cifnif = $this->albaran->cifnif;
437
                if ($cliente->save()) {
438
                    /// actualizamos albaranes
439
                    $sql = "UPDATE albaranescli SET cifnif = " . $cliente->var2str($this->albaran->cifnif)
440
                            . " WHERE codcliente = " . $cliente->var2str($this->albaran->codcliente)
441
                            . " AND cifnif = '' AND fecha >= " . $cliente->var2str(date('01-01-Y')) . ";";
442
                    $this->db->exec($sql);
443
444
                    /// actualizamos facturas
445
                    $sql = "UPDATE facturascli SET cifnif = " . $cliente->var2str($this->albaran->cifnif)
446
                            . " WHERE codcliente = " . $cliente->var2str($this->albaran->codcliente)
447
                            . " AND cifnif = '' AND fecha >= " . $cliente->var2str(date('01-01-Y')) . ";";
448
                    $this->db->exec($sql);
449
                }
450
            }
451
        }
452
    }
453
454
    private function generar_factura()
455
    {
456
        $this->fbase_facturar_albaran_cliente([$this->albaran], $_REQUEST['facturar'], $_REQUEST['codpago']);
457
    }
458
}
459