|
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_factura extends rd_controller |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
public $agencia; |
|
26
|
|
|
public $agente; |
|
27
|
|
|
public $agentes; |
|
28
|
|
|
public $almacen; |
|
29
|
|
|
public $cliente; |
|
30
|
|
|
public $divisa; |
|
31
|
|
|
public $ejercicio; |
|
32
|
|
|
public $factura; |
|
33
|
|
|
public $forma_pago; |
|
34
|
|
|
public $mostrar_boton_pagada; |
|
35
|
|
|
public $pais; |
|
36
|
|
|
public $rectificada; |
|
37
|
|
|
public $rectificativa; |
|
38
|
|
|
public $serie; |
|
39
|
|
|
public $ncf; |
|
40
|
|
|
public $impuesto; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct() |
|
43
|
|
|
{ |
|
44
|
|
|
parent::__construct(__CLASS__, 'Factura de cliente', 'ventas', FALSE, FALSE); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function private_core() |
|
48
|
|
|
{ |
|
49
|
|
|
parent::private_core(); |
|
50
|
|
|
|
|
51
|
|
|
$this->ppage = $this->page->get('ventas_facturas'); |
|
|
|
|
|
|
52
|
|
|
$this->shared_extensions(); |
|
53
|
|
|
$this->agencia = new agencia_transporte(); |
|
|
|
|
|
|
54
|
|
|
$this->agente = FALSE; |
|
55
|
|
|
$this->agentes = array(); |
|
56
|
|
|
$this->almacen = new almacen(); |
|
|
|
|
|
|
57
|
|
|
$this->cliente = FALSE; |
|
58
|
|
|
$this->divisa = new divisa(); |
|
|
|
|
|
|
59
|
|
|
$this->ejercicio = new ejercicio(); |
|
|
|
|
|
|
60
|
|
|
$this->factura = FALSE; |
|
61
|
|
|
$this->forma_pago = new forma_pago(); |
|
|
|
|
|
|
62
|
|
|
$this->pais = new pais(); |
|
|
|
|
|
|
63
|
|
|
$this->rectificada = FALSE; |
|
64
|
|
|
$this->rectificativa = FALSE; |
|
65
|
|
|
$this->serie = new serie(); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Si hay alguna extensión de tipo config y texto no_button_pagada, |
|
69
|
|
|
* desactivamos el botón de pagada/sin pagar. |
|
70
|
|
|
*/ |
|
71
|
|
|
$this->mostrar_boton_pagada = TRUE; |
|
72
|
|
|
foreach ($this->extensions as $ext) { |
|
73
|
|
|
if ($ext->type == 'config' && $ext->text == 'no_button_pagada') { |
|
74
|
|
|
$this->mostrar_boton_pagada = FALSE; |
|
75
|
|
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* ¿Modificamos la factura? |
|
81
|
|
|
*/ |
|
82
|
|
|
$factura = new factura_cliente(); |
|
83
|
|
|
if (isset($_POST['idfactura'])) { |
|
84
|
|
|
$this->factura = $factura->get($_POST['idfactura']); |
|
85
|
|
|
$this->modificar(); |
|
86
|
|
|
} else if (isset($_GET['id'])) { |
|
87
|
|
|
$this->factura = $factura->get($_GET['id']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ($this->factura) { |
|
91
|
|
|
$this->page->title = $this->factura->codigo; |
|
92
|
|
|
|
|
93
|
|
|
/// cargamos el agente |
|
94
|
|
|
$agente = new agente(); |
|
|
|
|
|
|
95
|
|
|
if (!is_null($this->factura->codagente)) { |
|
96
|
|
|
$this->agente = $agente->get($this->factura->codagente); |
|
97
|
|
|
} |
|
98
|
|
|
$this->agentes = $agente->all(); |
|
99
|
|
|
|
|
100
|
|
|
/// cargamos el cliente |
|
101
|
|
|
$cliente = new cliente(); |
|
|
|
|
|
|
102
|
|
|
$this->cliente = $cliente->get($this->factura->codcliente); |
|
103
|
|
|
|
|
104
|
|
|
if (isset($_GET['gen_asiento']) && isset($_GET['petid'])) { |
|
105
|
|
|
if ($this->duplicated_petition($_GET['petid'])) { |
|
106
|
|
|
$this->new_error_msg('Petición duplicada. Evita hacer doble clic sobre los botones.'); |
|
107
|
|
|
} else { |
|
108
|
|
|
$this->generar_asiento($this->factura); |
|
109
|
|
|
} |
|
110
|
|
|
} else if (isset($_GET['updatedir'])) { |
|
111
|
|
|
$this->actualizar_direccion(); |
|
112
|
|
|
} else if (isset($_REQUEST['pagada'])) { |
|
113
|
|
|
$this->pagar(($_REQUEST['pagada'] == 'TRUE')); |
|
114
|
|
|
} else if (isset($_POST['anular'])) { |
|
115
|
|
|
$this->anular_factura(); |
|
116
|
|
|
} else if (isset($_POST['rectificar'])) { |
|
117
|
|
|
$this->rectificar_factura(); |
|
118
|
|
|
} else if (isset($_GET['fix_ncf'])) { |
|
119
|
|
|
$this->fix_ncf(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if ($this->factura->idfacturarect) { |
|
123
|
|
|
$this->rectificada = $factura->get($this->factura->idfacturarect); |
|
124
|
|
|
} else { |
|
125
|
|
|
$this->get_factura_rectificativa(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/// comprobamos la factura |
|
129
|
|
|
$this->factura->full_test(); |
|
130
|
|
|
} else { |
|
131
|
|
|
$this->new_error_msg("¡Factura de cliente no encontrada!", 'error', FALSE, FALSE); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function url() |
|
136
|
|
|
{ |
|
137
|
|
|
if (!isset($this->factura)) { |
|
138
|
|
|
return parent::url(); |
|
139
|
|
|
} else if ($this->factura) { |
|
140
|
|
|
return $this->factura->url(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $this->ppage->url(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function fix_ncf() |
|
147
|
|
|
{ |
|
148
|
|
|
$this->ncf_length = (\strtotime($this->factura->fecha) < (\strtotime('01-05-2018'))) ? 19 : $this->ncf_length; |
|
149
|
|
|
$funcion_generar_numero = (\strtotime($this->factura->fecha) < (\strtotime('01-05-2018'))) ? 'generar_numero_ncf_old':'generar_numero_ncf'; |
|
150
|
|
|
if ($this->factura->numero2 != '' and strlen($this->factura->numero2) == $this->ncf_length) { |
|
151
|
|
|
$this->new_error_msg('¡La Factura ya posee un NCF valido, no se hace ninguna modificación!'); |
|
152
|
|
|
} else { |
|
153
|
|
|
/* |
|
154
|
|
|
* Verificación de disponibilidad del Número de NCF para República Dominicana |
|
155
|
|
|
*/ |
|
156
|
|
|
//Obtenemos el tipo de comprobante a generar para el cliente |
|
157
|
|
|
$tipo_comprobante = $this->ncf_tipo_comprobante($this->empresa->id, $this->cliente->codcliente); |
|
158
|
|
|
if($this->factura->idfacturarect){ |
|
159
|
|
|
$tipo_comprobante = '04'; |
|
160
|
|
|
} |
|
161
|
|
|
//Con el codigo del almacen desde donde facturaremos generamos el número de NCF |
|
162
|
|
|
$numero_ncf = $this->$funcion_generar_numero($this->empresa->id, $this->factura->codalmacen, $tipo_comprobante, $this->factura->codpago); |
|
163
|
|
|
$this->factura->numero2 = $numero_ncf['NCF']; |
|
164
|
|
|
if ($this->factura->save()) { |
|
165
|
|
|
$this->guardar_ncf($this->empresa->id, $this->factura, $tipo_comprobante, $numero_ncf); |
|
166
|
|
|
$this->new_message('¡NCF corregido correctamente!'); |
|
167
|
|
|
} else { |
|
168
|
|
|
$this->new_error_msg('Ocurrio un error y no se pudo generar el NCF correctamente, intentelo nuevamente revisando los datos del Cliente.'); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
private function modificar() |
|
174
|
|
|
{ |
|
175
|
|
|
$this->factura->observaciones = $_POST['observaciones']; |
|
176
|
|
|
$this->factura->nombrecliente = $_POST['nombrecliente']; |
|
177
|
|
|
$this->factura->cifnif = $_POST['cifnif']; |
|
178
|
|
|
$this->factura->codpais = $_POST['codpais']; |
|
179
|
|
|
$this->factura->provincia = $_POST['provincia']; |
|
180
|
|
|
$this->factura->ciudad = $_POST['ciudad']; |
|
181
|
|
|
$this->factura->codpostal = $_POST['codpostal']; |
|
182
|
|
|
$this->factura->direccion = $_POST['direccion']; |
|
183
|
|
|
$this->factura->apartado = $_POST['apartado']; |
|
184
|
|
|
|
|
185
|
|
|
$this->factura->envio_nombre = $_POST['envio_nombre']; |
|
186
|
|
|
$this->factura->envio_apellidos = $_POST['envio_apellidos']; |
|
187
|
|
|
$this->factura->envio_codtrans = NULL; |
|
188
|
|
|
if ($_POST['envio_codtrans'] != '') { |
|
189
|
|
|
$this->factura->envio_codtrans = $_POST['envio_codtrans']; |
|
190
|
|
|
} |
|
191
|
|
|
$this->factura->envio_codigo = $_POST['envio_codigo']; |
|
192
|
|
|
$this->factura->envio_codpais = $_POST['envio_codpais']; |
|
193
|
|
|
$this->factura->envio_provincia = $_POST['envio_provincia']; |
|
194
|
|
|
$this->factura->envio_ciudad = $_POST['envio_ciudad']; |
|
195
|
|
|
$this->factura->envio_codpostal = $_POST['envio_codpostal']; |
|
196
|
|
|
$this->factura->envio_direccion = $_POST['envio_direccion']; |
|
197
|
|
|
$this->factura->envio_apartado = $_POST['envio_apartado']; |
|
198
|
|
|
|
|
199
|
|
|
$this->factura->codagente = NULL; |
|
200
|
|
|
$this->factura->porcomision = 0; |
|
201
|
|
|
if ($_POST['codagente'] != '') { |
|
202
|
|
|
$this->factura->codagente = $_POST['codagente']; |
|
203
|
|
|
$this->factura->porcomision = floatval($_POST['porcomision']); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$this->factura->set_fecha_hora($_POST['fecha'], $_POST['hora']); |
|
207
|
|
|
|
|
208
|
|
|
/// ¿cambiamos la forma de pago? |
|
209
|
|
|
if ($this->factura->codpago != $_POST['forma_pago']) { |
|
210
|
|
|
$this->factura->codpago = $_POST['forma_pago']; |
|
211
|
|
|
$this->factura->vencimiento = $this->nuevo_vencimiento($this->factura->fecha, $this->factura->codpago); |
|
212
|
|
|
} else { |
|
213
|
|
|
$this->factura->vencimiento = $_POST['vencimiento']; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/// función auxiliar para implementar en los plugins que lo necesiten |
|
217
|
|
|
if (!fs_generar_numero2($this->factura)) { |
|
218
|
|
|
$this->factura->numero2 = $_POST['numero2']; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
if ($this->factura->save()) { |
|
222
|
|
|
$asiento = $this->factura->get_asiento(); |
|
223
|
|
|
if ($asiento) { |
|
224
|
|
|
$asiento->fecha = $this->factura->fecha; |
|
225
|
|
|
if (!$asiento->save()) { |
|
226
|
|
|
$this->new_error_msg("Imposible modificar la fecha del asiento."); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/// Función de ejecución de tareas post guardado correcto del albarán |
|
231
|
|
|
fs_documento_post_save($this->factura); |
|
232
|
|
|
|
|
233
|
|
|
$this->new_message("Factura modificada correctamente."); |
|
234
|
|
|
$this->new_change('Factura Cliente ' . $this->factura->codigo, $this->factura->url()); |
|
235
|
|
|
} else { |
|
236
|
|
|
$this->new_error_msg("¡Imposible modificar la factura!"); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
private function actualizar_direccion() |
|
241
|
|
|
{ |
|
242
|
|
|
foreach ($this->cliente->get_direcciones() as $dir) { |
|
243
|
|
|
if ($dir->domfacturacion) { |
|
244
|
|
|
$this->factura->cifnif = $this->cliente->cifnif; |
|
245
|
|
|
$this->factura->nombrecliente = $this->cliente->razonsocial; |
|
246
|
|
|
|
|
247
|
|
|
$this->factura->apartado = $dir->apartado; |
|
248
|
|
|
$this->factura->ciudad = $dir->ciudad; |
|
249
|
|
|
$this->factura->coddir = $dir->id; |
|
250
|
|
|
$this->factura->codpais = $dir->codpais; |
|
251
|
|
|
$this->factura->codpostal = $dir->codpostal; |
|
252
|
|
|
$this->factura->direccion = $dir->direccion; |
|
253
|
|
|
$this->factura->provincia = $dir->provincia; |
|
254
|
|
|
|
|
255
|
|
|
if ($this->factura->save()) { |
|
256
|
|
|
$this->new_message('Dirección actualizada correctamente.'); |
|
257
|
|
|
} else { |
|
258
|
|
|
$this->new_error_msg('Imposible actualizar la dirección de la factura.'); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
break; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
private function generar_asiento(&$factura) |
|
267
|
|
|
{ |
|
268
|
|
|
if ($factura->get_asiento()) { |
|
269
|
|
|
$this->new_error_msg('Ya hay un asiento asociado a esta factura.'); |
|
270
|
|
|
} else { |
|
271
|
|
|
$asiento_factura = new asiento_factura(); |
|
|
|
|
|
|
272
|
|
|
$asiento_factura->soloasiento = TRUE; |
|
273
|
|
|
if ($asiento_factura->generar_asiento_venta($factura)) { |
|
274
|
|
|
$this->new_message("<a href='" . $asiento_factura->asiento->url() . "'>Asiento</a> generado correctamente."); |
|
275
|
|
|
|
|
276
|
|
|
if (!$this->empresa->contintegrada) { |
|
277
|
|
|
$this->new_message("¿Quieres que los asientos se generen automáticamente?" |
|
278
|
|
|
. " Activa la <a href='index.php?page=admin_empresa#facturacion'>Contabilidad integrada</a>."); |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
foreach ($asiento_factura->errors as $err) { |
|
283
|
|
|
$this->new_error_msg($err); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
foreach ($asiento_factura->messages as $msg) { |
|
287
|
|
|
$this->new_message($msg); |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
private function pagar($pagada = TRUE) |
|
293
|
|
|
{ |
|
294
|
|
|
/// ¿Hay asiento? |
|
295
|
|
|
if (is_null($this->factura->idasiento)) { |
|
296
|
|
|
$this->factura->pagada = $pagada; |
|
297
|
|
|
$this->factura->save(); |
|
298
|
|
|
} else if (!$pagada && $this->factura->pagada) { |
|
299
|
|
|
/// marcar como impagada |
|
300
|
|
|
$this->factura->pagada = FALSE; |
|
301
|
|
|
|
|
302
|
|
|
/// ¿Eliminamos el asiento de pago? |
|
303
|
|
|
$as1 = new asiento(); |
|
|
|
|
|
|
304
|
|
|
$asiento = $as1->get($this->factura->idasientop); |
|
305
|
|
|
if ($asiento) { |
|
306
|
|
|
$asiento->delete(); |
|
307
|
|
|
$this->new_message('Asiento de pago eliminado.'); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
$this->factura->idasientop = NULL; |
|
311
|
|
|
if ($this->factura->save()) { |
|
312
|
|
|
$this->new_message('Factura marcada como impagada.'); |
|
313
|
|
|
} else { |
|
314
|
|
|
$this->new_error_msg('Error al modificar la factura.'); |
|
315
|
|
|
} |
|
316
|
|
|
} else if ($pagada && !$this->factura->pagada) { |
|
317
|
|
|
/// marcar como pagada |
|
318
|
|
|
$asiento = $this->factura->get_asiento(); |
|
319
|
|
|
if ($asiento) { |
|
320
|
|
|
/// nos aseguramos que el cliente tenga subcuenta en el ejercicio actual |
|
321
|
|
|
$subcli = FALSE; |
|
322
|
|
|
$eje = $this->ejercicio->get_by_fecha($_POST['fpagada']); |
|
323
|
|
|
if ($eje && $this->cliente) { |
|
324
|
|
|
$subcli = $this->cliente->get_subcuenta($eje->codejercicio); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
$importe = $this->euro_convert($this->factura->totaleuros, $this->factura->coddivisa, $this->factura->tasaconv); |
|
328
|
|
|
|
|
329
|
|
|
$asiento_factura = new asiento_factura(); |
|
330
|
|
|
$this->factura->idasientop = $asiento_factura->generar_asiento_pago($asiento, $this->factura->codpago, $_POST['fpagada'], $subcli, $importe); |
|
331
|
|
|
if ($this->factura->idasientop !== NULL) { |
|
332
|
|
|
$this->factura->pagada = TRUE; |
|
333
|
|
|
if ($this->factura->save()) { |
|
334
|
|
|
$this->new_message('<a href="' . $this->factura->asiento_pago_url() . '">Asiento de pago</a> generado.'); |
|
335
|
|
|
} else { |
|
336
|
|
|
$this->new_error_msg('Error al marcar la factura como pagada.'); |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
foreach ($asiento_factura->errors as $err) { |
|
341
|
|
|
$this->new_error_msg($err); |
|
342
|
|
|
} |
|
343
|
|
|
} else { |
|
344
|
|
|
$this->new_error_msg('No se ha encontrado el asiento de la factura.'); |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
private function nuevo_vencimiento($fecha, $codpago) |
|
350
|
|
|
{ |
|
351
|
|
|
$vencimiento = $fecha; |
|
352
|
|
|
|
|
353
|
|
|
$formap = $this->forma_pago->get($codpago); |
|
354
|
|
|
if ($formap) { |
|
355
|
|
|
$cli0 = new cliente(); |
|
356
|
|
|
$cliente = $cli0->get($this->factura->codcliente); |
|
357
|
|
|
if ($cliente) { |
|
358
|
|
|
$vencimiento = $formap->calcular_vencimiento($fecha, $cliente->diaspago); |
|
359
|
|
|
} else { |
|
360
|
|
|
$vencimiento = $formap->calcular_vencimiento($fecha); |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
return $vencimiento; |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
private function anular_factura() |
|
368
|
|
|
{ |
|
369
|
|
|
/* |
|
370
|
|
|
* Verificación de disponibilidad del Número de NCF para Notas de Crédito |
|
371
|
|
|
*/ |
|
372
|
|
|
$tipo_comprobante = '04'; |
|
373
|
|
|
$numero_ncf = $this->generar_numero_ncf($this->empresa->id, $this->factura->codalmacen, $tipo_comprobante, $this->factura->codpago); |
|
374
|
|
|
$motivo = \filter_input(INPUT_POST, 'motivo'); |
|
375
|
|
|
$motivo_anulacion = $this->ncf_tipo_anulacion->get($motivo); |
|
376
|
|
|
/// generamos una factura rectificativa a partir de la actual |
|
377
|
|
|
$factura = clone $this->factura; |
|
378
|
|
|
$factura->idfactura = NULL; |
|
379
|
|
|
$factura->numero = NULL; |
|
380
|
|
|
$factura->numero2 = $numero_ncf['NCF']; |
|
381
|
|
|
$factura->codigo = NULL; |
|
382
|
|
|
$factura->idasiento = NULL; |
|
383
|
|
|
$factura->idasientop = NULL; |
|
384
|
|
|
$factura->femail = NULL; |
|
385
|
|
|
$factura->numdocs = 0; |
|
386
|
|
|
|
|
387
|
|
|
if (isset($this->factura->codruta)) { |
|
388
|
|
|
$factura->codruta = $this->factura->codruta; |
|
389
|
|
|
$factura->codvendedor = $this->factura->codvendedor; |
|
390
|
|
|
} |
|
391
|
|
|
$factura->idfacturarect = $this->factura->idfactura; |
|
392
|
|
|
$factura->codigorect = $this->factura->codigo; |
|
393
|
|
|
$factura->codejercicio = $this->factura->codejercicio; |
|
394
|
|
|
$factura->codserie = $_POST['codserie']; |
|
395
|
|
|
$factura->set_fecha_hora($_POST['fecha'], $this->hour()); |
|
396
|
|
|
$factura->observaciones = "Anulacion generada por: " . $motivo_anulacion->descripcion; |
|
397
|
|
|
$factura->netosindto = 0; |
|
398
|
|
|
$factura->neto = 0; |
|
399
|
|
|
$factura->totalirpf = 0; |
|
400
|
|
|
$factura->totaliva = 0; |
|
401
|
|
|
$factura->totalrecargo = 0; |
|
402
|
|
|
$factura->total = 0; |
|
403
|
|
|
|
|
404
|
|
|
if ($factura->save()) { |
|
405
|
|
|
$articulo = new articulo(); |
|
406
|
|
|
$error = FALSE; |
|
407
|
|
|
|
|
408
|
|
|
/// copiamos las líneas en negativo |
|
409
|
|
|
foreach ($this->factura->get_lineas() as $lin) { |
|
410
|
|
|
$lin->idlinea = NULL; |
|
411
|
|
|
$lin->idalbaran = NULL; |
|
412
|
|
|
$lin->idfactura = $factura->idfactura; |
|
413
|
|
|
$lin->cantidad = 0 - $lin->cantidad; |
|
414
|
|
|
$lin->pvpsindto = $lin->pvpunitario * $lin->cantidad; |
|
415
|
|
|
$lin->pvptotal = $lin->pvpunitario * (100 - $lin->dtopor) / 100 * $lin->cantidad; |
|
416
|
|
|
|
|
417
|
|
|
if ($lin->save()) { |
|
418
|
|
|
if ($lin->referencia) { |
|
419
|
|
|
/// actualizamos el stock |
|
420
|
|
|
$art = $articulo->get($lin->referencia); |
|
421
|
|
|
if ($art) { |
|
422
|
|
|
$art->sum_stock($factura->codalmacen, 0 - $lin->cantidad, FALSE, $lin->codcombinacion); |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
} else { |
|
426
|
|
|
$error = TRUE; |
|
427
|
|
|
} |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/// obtenemos los subtotales por impuesto |
|
431
|
|
|
$due_totales = $this->fbase_calc_due([$factura->dtopor1, $factura->dtopor2, $factura->dtopor3, $factura->dtopor4, $factura->dtopor5]); |
|
432
|
|
|
foreach ($this->fbase_get_subtotales_documento($factura->get_lineas(), $due_totales) as $subt) { |
|
433
|
|
|
$factura->netosindto += $subt['netosindto']; |
|
434
|
|
|
$factura->neto += $subt['neto']; |
|
435
|
|
|
$factura->totaliva += $subt['iva']; |
|
436
|
|
|
$factura->totalirpf += $subt['irpf']; |
|
437
|
|
|
$factura->totalrecargo += $subt['recargo']; |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
$factura->total = round($factura->neto + $factura->totaliva - $factura->totalirpf + $factura->totalrecargo, FS_NF0); |
|
441
|
|
|
|
|
442
|
|
|
if ($error || !$factura->save()) { |
|
443
|
|
|
$factura->delete(); |
|
444
|
|
|
$this->new_error_msg('Se han producido errores al crear la ' . FS_FACTURA_RECTIFICATIVA); |
|
|
|
|
|
|
445
|
|
|
} else { |
|
446
|
|
|
/* |
|
447
|
|
|
* Luego de que todo este correcto generamos el NCF la Nota de Credito |
|
448
|
|
|
*/ |
|
449
|
|
|
//Con el codigo del almacen desde donde facturaremos generamos el número de NCF |
|
450
|
|
|
$this->guardar_ncf($this->empresa->id, $factura, $tipo_comprobante, $numero_ncf, $motivo_anulacion->codigo . " " . $motivo_anulacion->descripcion); |
|
451
|
|
|
$this->new_message('<a href="' . $factura->url() . '">' . ucfirst(FS_FACTURA_RECTIFICATIVA) . '</a> creada correctamente.'); |
|
452
|
|
|
if ($this->empresa->contintegrada) { |
|
453
|
|
|
$this->generar_asiento($factura); |
|
454
|
|
|
} else { |
|
455
|
|
|
/// generamos las líneas de IVA de todas formas |
|
456
|
|
|
$factura->get_lineas_iva(); |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
$ncf_factura = $this->ncf_ventas->get_ncf($this->empresa->id, $this->factura->idfactura, $this->factura->codcliente); |
|
460
|
|
|
$ncf_factura->motivo = $motivo_anulacion->codigo . " " . $motivo_anulacion->descripcion; |
|
461
|
|
|
$ncf_factura->fecha_modificacion = \date('Y-m-d H:i:s'); |
|
462
|
|
|
$ncf_factura->usuario_modificacion = $this->user->nick; |
|
463
|
|
|
$ncf_factura->anular(); |
|
464
|
|
|
$this->factura->observaciones = ucfirst(FS_FACTURA) . " anulada por: " . $motivo_anulacion->descripcion; |
|
|
|
|
|
|
465
|
|
|
$this->factura->anulada = TRUE; |
|
466
|
|
|
$this->factura->save(); |
|
467
|
|
|
} |
|
468
|
|
|
} else { |
|
469
|
|
|
$this->new_error_msg('Error al generar la ' . FS_FACTURA_RECTIFICATIVA . '.'); |
|
470
|
|
|
} |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
public function rectificar_factura() |
|
474
|
|
|
{ |
|
475
|
|
|
/* |
|
476
|
|
|
* Verificación de disponibilidad del Número de NCF para Notas de Crédito |
|
477
|
|
|
*/ |
|
478
|
|
|
$tipo_comprobante = '04'; |
|
479
|
|
|
$numero_ncf = $this->generar_numero_ncf($this->empresa->id, $this->factura->codalmacen, $tipo_comprobante, $this->factura->codpago); |
|
480
|
|
|
$serie = $this->serie->get($_POST['codserie']); |
|
481
|
|
|
if (!$serie) { |
|
482
|
|
|
$this->new_error_msg('Serie no encontrada.'); |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
$motivo = \filter_input(INPUT_POST, 'motivo'); |
|
486
|
|
|
$motivo_anulacion = $this->ncf_tipo_anulacion->get($motivo); |
|
487
|
|
|
$monto0 = \filter_input(INPUT_POST, 'monto'); |
|
488
|
|
|
$monto = ($monto0 > 0) ? ($monto0 * -1) : $monto0; |
|
489
|
|
|
$impuesto0 = \filter_input(INPUT_POST, 'codimpuesto'); |
|
490
|
|
|
$impuesto = ($impuesto0 > 0) ? ($impuesto0 * -1) : $impuesto0; |
|
491
|
|
|
//$monto_total = \filter_input(INPUT_POST, 'monto_total'); |
|
492
|
|
|
$fecha = \filter_input(INPUT_POST, 'fecha'); |
|
493
|
|
|
$irpf = 0; |
|
494
|
|
|
$recargo = 0; |
|
495
|
|
|
$factura = clone $this->factura; |
|
496
|
|
|
$factura->idfactura = null; |
|
497
|
|
|
$factura->numero = null; |
|
498
|
|
|
$factura->numero2 = $numero_ncf['NCF']; |
|
499
|
|
|
$factura->codigo = null; |
|
500
|
|
|
$factura->idasiento = null; |
|
501
|
|
|
$factura->idfacturarect = $this->factura->idfactura; |
|
502
|
|
|
$factura->codigorect = $this->factura->codigo; |
|
503
|
|
|
$factura->fecha = $fecha; |
|
504
|
|
|
$factura->neto = round($monto, FS_NF0); |
|
505
|
|
|
$factura->totaliva = (round(($monto * (($impuesto) / 100)), FS_NF0) * -1); |
|
506
|
|
|
$factura->totalirpf = round($irpf, FS_NF0); |
|
507
|
|
|
$factura->totalrecargo = round($recargo, FS_NF0); |
|
508
|
|
|
$factura->total = $factura->neto + $factura->totaliva - $factura->totalirpf + $factura->totalrecargo; |
|
509
|
|
|
$factura->observaciones = ucfirst(FS_FACTURA_RECTIFICATIVA) . " por rectificación contable de la " . ucfirst(FS_FACTURA) . ": " . $factura->codigorect; |
|
|
|
|
|
|
510
|
|
|
|
|
511
|
|
|
if ($factura->save()) { |
|
512
|
|
|
$linea = new linea_factura_cliente(); |
|
|
|
|
|
|
513
|
|
|
$linea->idfactura = $factura->idfactura; |
|
514
|
|
|
$linea->descripcion = "Rectificación de importe"; |
|
515
|
|
|
if (!$serie->siniva and $this->cliente->regimeniva != 'Exento') { |
|
516
|
|
|
$imp0 = $this->impuesto->get_by_iva($impuesto); |
|
517
|
|
|
$linea->codimpuesto = ($imp0) ? $imp0->codimpuesto : null; |
|
518
|
|
|
$linea->iva = ($impuesto > 0) ? floatval($impuesto) : floatval($impuesto * -1); |
|
519
|
|
|
$linea->recargo = floatval($recargo); |
|
520
|
|
|
} |
|
521
|
|
|
$linea->irpf = floatval($irpf); |
|
522
|
|
|
$linea->pvpunitario = ($monto > 0) ? floatval($monto) : floatval($monto * -1); |
|
523
|
|
|
$linea->cantidad = -1; |
|
524
|
|
|
$linea->dtopor = 0; |
|
525
|
|
|
$linea->pvpsindto = ($linea->pvpunitario * $linea->cantidad); |
|
526
|
|
|
$linea->pvptotal = floatval($monto); |
|
527
|
|
|
if ($linea->save()) { |
|
528
|
|
|
$factura->get_lineas_iva(); |
|
529
|
|
|
/* |
|
530
|
|
|
* Grabación del Número de NCF para República Dominicana |
|
531
|
|
|
* Con el codigo del almacen desde donde facturaremos generamos el número de NCF |
|
532
|
|
|
*/ |
|
533
|
|
|
$this->guardar_ncf($this->empresa->id, $factura, $tipo_comprobante, $numero_ncf, $motivo_anulacion->codigo . " " . $motivo_anulacion->descripcion); |
|
534
|
|
|
$this->generar_asiento($factura); |
|
535
|
|
|
$this->new_message("<a href='" . $factura->url() . "'>Factura</a> guardada correctamente con número NCF: " . $numero_ncf['NCF']); |
|
536
|
|
|
$this->new_change('Factura Cliente ' . $factura->codigo, $factura->url(), true); |
|
537
|
|
|
} |
|
538
|
|
|
} |
|
539
|
|
|
} |
|
540
|
|
|
|
|
541
|
|
|
private function get_factura_rectificativa() |
|
542
|
|
|
{ |
|
543
|
|
|
$sql = "SELECT * FROM facturascli WHERE idfacturarect = " . $this->factura->var2str($this->factura->idfactura); |
|
544
|
|
|
|
|
545
|
|
|
$data = $this->db->select($sql); |
|
546
|
|
|
if ($data) { |
|
547
|
|
|
$this->rectificativa = new factura_cliente($data[0]); |
|
548
|
|
|
} |
|
549
|
|
|
} |
|
550
|
|
|
|
|
551
|
|
|
public function get_cuentas_bancarias() |
|
552
|
|
|
{ |
|
553
|
|
|
$cuentas = array(); |
|
554
|
|
|
|
|
555
|
|
|
$cbc0 = new cuenta_banco_cliente(); |
|
|
|
|
|
|
556
|
|
|
foreach ($cbc0->all_from_cliente($this->factura->codcliente) as $cuenta) { |
|
557
|
|
|
$cuentas[] = $cuenta; |
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
|
|
return $cuentas; |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
public function shared_extensions() |
|
564
|
|
|
{ |
|
565
|
|
|
$extensiones = array( |
|
566
|
|
|
array( |
|
567
|
|
|
'name' => 'ncf_functions_js', |
|
568
|
|
|
'page_from' => __CLASS__, |
|
569
|
|
|
'page_to' => __CLASS__, |
|
570
|
|
|
'type' => 'head', |
|
571
|
|
|
'text' => '<script src="' . FS_PATH . 'plugins/republica_dominicana/view/js/ncf_functions.js" type="text/javascript"></script>', |
|
|
|
|
|
|
572
|
|
|
'params' => '' |
|
573
|
|
|
), |
|
574
|
|
|
array( |
|
575
|
|
|
'name' => 'button_fix_ncf', |
|
576
|
|
|
'page_from' => __CLASS__, |
|
577
|
|
|
'page_to' => __CLASS__, |
|
578
|
|
|
'type' => 'button', |
|
579
|
|
|
'text' => '<span class="fa fa-chain-broken" aria-hidden="true"></span><span class="hidden-xs"> Corregir NCF</span>', |
|
580
|
|
|
'params' => '&fix_ncf=true' |
|
581
|
|
|
) |
|
582
|
|
|
); |
|
583
|
|
|
foreach ($extensiones as $ext) { |
|
584
|
|
|
$fsext0 = new fs_extension($ext); |
|
|
|
|
|
|
585
|
|
|
if (!$fsext0->save()) { |
|
586
|
|
|
$this->new_error_msg('Imposible guardar los datos de la extensión ' . $ext['name'] . '.'); |
|
587
|
|
|
} |
|
588
|
|
|
} |
|
589
|
|
|
} |
|
590
|
|
|
} |
|
591
|
|
|
|