Issues (232)

Assets/JS/CommonDomFunctions.js (15 issues)

1
/*
2
 * Copyright (C) 2021 Joe Nilson <[email protected]>
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as
6
 * published by the Free Software Foundation, either version 3 of the
7
 * License, or (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 * You should have received a copy of the GNU Lesser General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 */
16
17
/**
18
 * @param {string} tipoComprobante
19
 * @returns {Promise<*>}
20
 */
21
async function verificarCorrelativoNCF(tipoComprobante, tipoOperacion)
22
{
23
    let pagina = (tipoOperacion === 'Venta') ? 'EditFacturaCliente' : 'EditFacturaProveedor';
24
    let ArrayTipoNCFCompras = ['11','12','16','17'];
25
    if (tipoOperacion === 'Compra' && !ArrayTipoNCFCompras.includes(tipoComprobante)) {
26
        return true;
27
    }
28
    $("select[name='tipocomprobante']").val(tipoComprobante);
29
    return $.ajax({
30
        url: pagina,
31
        async: true,
32
        data: {'action': 'busca_correlativo', 'tipocomprobante': tipoComprobante },
33
        type: 'POST',
34
        datatype: 'json',
35
        success: function (response) {
36
            let data = JSON.parse(response);
37
            logConsole(data.existe, 'resultado');
38
            if ( data.existe === false ) {
39
                executeModal(
40
                    'verificaNCF',
41
                    'No hay Correlativo de NCF Disponible',
42
                    'No hay correlativos disponibles para el Tipo de NCF ' +
43
                    tipoComprobante + ' <br/>Por favor revise su maestro de NCFs',
44
                    'warning'
45
                );
46
            } else {
47
                if (tipoComprobante !== '02') {
48
                    var fecha = data.existe[0].fechavencimiento.split("-");
49
                    var AnhoVencimiento = fecha[2];
50
                    var MesVencimiento = fecha[1];
51
                    var DiaVencimiento = fecha[0];
52
                    logConsole(fecha+': '+AnhoVencimiento+'-'+MesVencimiento+'-'+DiaVencimiento, 'fVen');
53
                    $("input[name='ncffechavencimiento']").val(AnhoVencimiento+'-'+MesVencimiento+'-'+DiaVencimiento);
54
                    $("input[name='ncffechavencimiento']").focus();
55
                }
56
            }
57
        },
58
        failure: function (response) {
59
            alert('Ha ocurrido algĂșn tipo de falla ' + response);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
60
        },
61
        error: function (xhr, status) {
62
            alert('Ha ocurrido algĂșn tipo de error ' + status);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
63
        }
64
    });
65
}
66
67
async function actualizarInformacionParaNCF()
68
{
69
    var infoCliente = await cargarInfoCliente();
70
    logConsole(infoCliente, 'infoCliente');
71
    var datosCliente = JSON.parse(infoCliente);
0 ignored issues
show
The variable datosCliente seems to be never used. Consider removing it.
Loading history...
72
    var tipoPago = await cargarTipoPago();
73
    var datosPago = JSON.parse(tipoPago);
0 ignored issues
show
The variable datosPago seems to be never used. Consider removing it.
Loading history...
74
    var tipoNCFs = await cargarTipoNCF('Ventas');
75
    logConsole(tipoNCFs, 'tipoNCFs');
76
    var datosTipoNCFs = JSON.parse(tipoNCFs);
0 ignored issues
show
The variable datosTipoNCFs seems to be never used. Consider removing it.
Loading history...
77
    let selectTiposNCF = "";
0 ignored issues
show
The variable selectTiposNCF seems to be never used. Consider removing it.
Loading history...
78
    var descInfoClienteTipoComprobante = '';
0 ignored issues
show
The variable descInfoClienteTipoComprobante seems to be never used. Consider removing it.
Loading history...
79
    var tipoMovimiento = await cargarTipoMovimiento();
80
    var datosMovimiento = JSON.parse(tipoMovimiento);
0 ignored issues
show
The variable datosMovimiento seems to be never used. Consider removing it.
Loading history...
81
82
}
83
84
/**
85
 * logConsole in Debug mode
86
 * @param  {string|Object|boolean} value
87
 * @param {string} description
88
 */
89
function logConsole(value, description ='data')
90
{
91
    if ($(".debugbar") !== undefined) {
92
        console.log(description, value);
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
93
    }
94
}
95
96
/********
97
 * Util Functions
98
 */
99
function isBusinessDocumentPage()
100
{
101
    let businessDocument = '';
102
    if ($('#purchaseFormHeader').length > 0) {
103
        businessDocument = 'Compra';
104
    } else if ($('#salesFormHeader').length > 0) {
105
        businessDocument = 'Venta';
106
    }
107
    return businessDocument;
108
}
109
110
async function cargarTipoNCF(businessDocument, tipoOperacion)
111
{
112
    let pagina = (businessDocument === 'Venta') ? 'EditFacturaCliente' : 'EditFacturaProveedor';
113
    return $.ajax({
114
        url: pagina,
115
        async: true,
116
        data: {'action': 'busca_tipo', 'tipodocumento': tipoOperacion.toLowerCase() },
117
        type: 'POST',
118
        datatype: 'json',
119
        success: function (response) {
120
            let data = JSON.parse(response);
121
            return data;
122
        },
123
        error: function (xhr, status) {
124
            alert('Ha ocurrido algĂșn tipo de error ' + status);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
125
        }
126
    });
127
}
128
129
async function cargarInfoCliente()
130
{
131
    return $.ajax({
132
        url: 'EditFacturaCliente',
133
        async: true,
134
        data: {'action': 'busca_infocliente', 'codcliente': $("input[name=codcliente]").val()},
135
        type: 'POST',
136
        datatype: 'json',
137
        success: function (response) {
138
            let data = JSON.parse(response);
139
            return data;
140
        },
141
        error: function (xhr, status) {
142
            alert('Ha ocurrido algĂșn tipo de error ' + status);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
143
        }
144
    });
145
}
146
147
async function cargarTipoPago(businessDocument)
148
{
149
    let pagina = (businessDocument === 'Venta') ? 'EditFacturaCliente' : 'EditFacturaProveedor';
150
    let tipoPago = (businessDocument === 'Venta') ? '01' : '02';
151
    return $.ajax({
152
        url: pagina,
153
        async: true,
154
        data: {'action': 'busca_pago', 'tipopago': tipoPago},
155
        type: 'POST',
156
        datatype: 'json',
157
        success: function (response) {
158
            let data = JSON.parse(response);
159
            return data;
160
        },
161
        error: function (xhr, status) {
162
            alert('Ha ocurrido algĂșn tipo de error ' + status);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
163
        }
164
    });
165
}
166
167
async function purchasesNCFVerify()
168
{
169
    let ncf = $("input[name='numeroncf']").val();
170
    logConsole(ncf, 'NCF');
171
    let proveedor = $("input[name='codproveedor']").val();
172
    let now = new Date();
173
    let ncfDueDate = now.getFullYear()+'-12-31';
174
    if (proveedor === '' || ncf === '') {
175
        executeModal(
176
            'proveedorOrNcfEmpty',
177
            'Complete los datos',
178
            'Seleccione un Proveedor y un NCF para validar el documento',
179
            'warning'
180
        );
181
        return undefined;
182
    } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
183
        return $.ajax({
184
            url: 'EditFacturaProveedor',
185
            async: true,
186
            data: {'action': 'verifica_documento', 'ncf': ncf, 'proveedor': proveedor},
187
            type: 'POST',
188
            datatype: 'json',
189
            success: function (response) {
190
                let data = JSON.parse(response);
191
                if (data.success) {
192
                    $("#btnVerifyNCF").attr('class', '').addClass("btn btn-success btn-spin-action");
193
                    $("#iconBtnVerify").attr('class', '').addClass("fas fa-check-circle fa-fw");
194
                    var formNumProveedorType = ncf.slice(-10, -8);
195
                    $("input[name='tipocomprobante']").val(formNumProveedorType);
196
                    if (formNumProveedorType !== '02') {
197
                        $("input[name='ncffechavencimiento']").val(ncfDueDate);
198
                        $("input[name='ncffechavencimiento']").focus();
199
                    }
200
                }
201
                if (data.error) {
202
                    $("#btnVerifyNCF").attr('class', '').addClass("btn btn-danger btn-spin-action");
203
                    $("#iconBtnVerify").attr('class', '').addClass("fas fa-exclamation-circle fa-fw");
204
                    executeModal(
205
                        'ncfExists',
206
                        'NCF Ya registrado',
207
                        'el NCF ya ha sido registrado con la '+data.message,
208
                        'warning'
209
                    );
210
                }
211
                return data;
212
            },
213
            error: function (xhr, status) {
214
                alert('Ha ocurrido algĂșn tipo de error ' + status);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
215
            }
216
        });
217
    }
218
}
219
220
async function cargarTipoMovimiento(businessDocument)
221
{
222
    let pagina = (businessDocument === 'Venta') ? 'EditFacturaCliente' : 'EditFacturaProveedor';
223
    let tipoMovimiento = (businessDocument === 'Venta') ? 'VEN' : 'COM';
224
    return $.ajax({
225
        url: pagina,
226
        async: true,
227
        data: {'action': 'busca_movimiento', 'tipomovimiento': tipoMovimiento},
228
        type: 'POST',
229
        datatype: 'json',
230
        success: function (response) {
231
            let data = JSON.parse(response);
232
            return data;
233
        },
234
        error: function (xhr, status) {
235
            alert('Ha ocurrido algĂșn tipo de error ' + status);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
236
        }
237
    });
238
}
239
240
$(document).ready(function () {
241
    logConsole($("input[name=codcliente]").val(), 'codcliente');
242
    let tipoOperacion = isBusinessDocumentPage();
243
    let varNCFTipoComprobante = $("select[name='tipocomprobante']");
244
    logConsole($("select[name='tipocomprobante']").val(), 'tipocomprobante');
245
246
    if (varNCFTipoComprobante.length !== 0 && varNCFTipoComprobante.val() !== '' && tipoOperacion !== '') {
247
        verificarCorrelativoNCF($("select[name='tipocomprobante']").val(), tipoOperacion);
248
    }
249
250
    $("#findCustomerModal").on('hidden.bs.modal', function () {
251
        setTimeout(async function () {
252
            var infoCliente = await cargarInfoCliente();
253
            logConsole(infoCliente, 'infoCliente');
254
            var datosCliente = JSON.parse(infoCliente);
255
            logConsole(datosCliente, 'datosCliente');
256
            let varNCFTipoComprobante = datosCliente.infocliente.tipocomprobante;
257
            logConsole($("input[name=codcliente]").val(), 'codcliente 2');
258
            logConsole(varNCFTipoComprobante, 'tipocomprobante 2');
259
            logConsole(tipoOperacion, 'tipoOperacion 2');
260
            if (varNCFTipoComprobante.length !== 0 && varNCFTipoComprobante !== '' && tipoOperacion !== '') {
261
                verificarCorrelativoNCF(varNCFTipoComprobante, tipoOperacion);
262
                $("select[name='ncftipopago']").val(datosCliente.infocliente.ncftipopago);
263
            }
264
        },300);
265
    });
266
267
    varNCFTipoComprobante.change(function () {
268
        logConsole(varNCFTipoComprobante.val(),"tipocomprobante val");
269
        if (tipoOperacion !== '') {
270
            verificarCorrelativoNCF(varNCFTipoComprobante.val(), tipoOperacion);
271
        }
272
    });
273
});