Passed
Push — master ( 8e49de...855750 )
by Joe Nilson
03:05
created

CommonDomFunctions.js ➔ cargarTipoNCF   A

Complexity

Conditions 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
dl 0
loc 17
rs 9.8
c 0
b 0
f 0
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 ArrayTipoNCFCompras = ['11','12','16','17'];
24
    if (tipoOperacion === 'Compras' && !ArrayTipoNCFCompras.includes($("#ncftipocomprobante").val())) {
25
        return true;
26
    }
27
    logConsole(tipoComprobante, 'tipoComprobante');
28
    return $.ajax({
29
        url: 'ListNCFRango',
30
        async: true,
31
        data: {'action': 'busca_correlativo', 'tipocomprobante': tipoComprobante },
32
        type: 'POST',
33
        datatype: 'json',
34
        success: function (response) {
35
            let data = JSON.parse(response);
36
            if ( data.existe === false ) {
37
                executeModal(
38
                    'verificaNCF',
39
                    'No hay Correlativo de NCF Disponible',
40
                    'No hay correlativos disponibles para el Tipo de NCF ' +
41
                    tipoComprobante + ' <br/>Por favor revise su maestro de NCFs',
42
                    'warning'
43
                );
44
            }
45
        },
46
        failure: function (response) {
47
            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...
48
        },
49
        error: function (xhr, status) {
50
            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...
51
        }
52
    });
53
}
54
55
/**
56
 * logConsole in Debug mode
57
 * @param  {string|Object|boolean} value
58
 * @param {string} description
59
 */
60
function logConsole(value, description ='data')
61
{
62
    if ($(".debugbar") !== undefined) {
63
        console.log(description, value);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
64
    }
65
}
66
67
/********
68
 * Util Functions
69
 */
70
71
/**
72
 *
73
 * @param {object} btn
74
 * @param {string} text
75
 */
76
function setLoadingButton(btn, text)
77
{
78
    $(btn).prop("disabled", true);
79
    $(btn).html(
80
        '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span><span class="sr-only">' + text +'</span>'
81
    );
82
}
83
84
function setBusinessDocViewModalSave(
85
    tipoDeEntidad,
86
    readOnlySelects,
87
    infoEntidadTipoNCF,
88
    infoEntidadTipoPago,
89
    selectTiposNCF,
90
    selectOptionsPagos,
91
    selectOptionsMovimientos
92
)
93
{
94
    let tipoOperacion = isBusinessDocumentPage();
95
    let message = '<div class="form-content">\n' +
96
        '      <form class="form" role="form">\n' +
97
        '        <div class="form-group">\n' +
98
        '          <label for="infoclientetiponcf">Tipo de NCF del '+tipoDeEntidad+':</label>\n' +
99
        '           <span style="font-weight: bold;" id="infoclientetiponcf">'+infoEntidadTipoNCF+'</span>'+
100
        '        </div>\n' +
101
        '        <div class="form-group">\n' +
102
        '          <label for="infoclientetiponcf">Tipo de Pago del'+tipoDeEntidad+':</label>\n' +
103
        '           <span style="font-weight: bold;" id="infoclientetiponcf">'+infoEntidadTipoPago+'</span>'+
104
        '        </div>\n' +
105
        '        <div class="form-group">\n' +
106
        '          <label for="ncftipocomprobante">Tipo de NCF</label>\n' +
107
        '          <select class="custom-select" id="ncftipocomprobante" name="ncftipocomprobante"'+
108
                        readOnlySelects+' onChange="verificarCorrelativoNCF(this.value, \''+tipoOperacion+'\')">\n' +
109
                        selectTiposNCF +
110
        '          </select>\n' +
111
        '        </div>\n' +
112
        '        <div class="form-group">\n' +
113
        '          <label for="ncftipopago">Tipo de Pago</label>\n' +
114
        '          <select class="custom-select" id="ncftipopago" name="ncftipopago"'+readOnlySelects+'>\n' +
115
                        selectOptionsPagos +
116
        '          </select>\n' +
117
        '        </div>\n' +
118
        '        <div class="form-group">\n' +
119
        '          <label for="ncftipomovimiento">Tipo de Movimiento</label>\n' +
120
        '          <select class="custom-select" id="ncftipomovimiento" name="ncftipomovimiento"'+readOnlySelects+'>\n' +
121
                        selectOptionsMovimientos +
122
        '          </select>\n' +
123
        '        </div>\n' +
124
        '      </form>\n' +
125
        '    </div>';
126
    return message;
127
}
128
129
130
function saveBussinessDocument(btn)
131
{
132
    //Set the save button as loading
133
    setLoadingButton(btn,'Guardando...');
134
135
    var data = {};
136
    $.each($("#" + businessDocViewFormName).serializeArray(), function (key, value) {
0 ignored issues
show
Bug introduced by
The variable businessDocViewFormName seems to be never declared. If this is a global, consider adding a /** global: businessDocViewFormName */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
137
        data[value.name] = value.value;
138
    });
139
    data['ncftipopago'] = $('#ncftipopago').val();
140
    data['ncftipomovimiento'] = $('#ncftipomovimiento').val();
141
    data['tipocomprobante'] = $('#ncftipocomprobante').val();
142
    data.action = "save-document";
143
    data.lines = getGridData();
144
145
    $.ajax({
146
        type: "POST",
147
        url: businessDocViewUrl,
0 ignored issues
show
Bug introduced by
The variable businessDocViewUrl seems to be never declared. If this is a global, consider adding a /** global: businessDocViewUrl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
148
        dataType: "text",
149
        data: data,
150
        success: function (results) {
151
            if (results.substring(0, 3) === "OK:") {
152
                $("#" + businessDocViewFormName + " :input[name=\"action\"]").val('save-ok');
0 ignored issues
show
Bug introduced by
The variable businessDocViewFormName seems to be never declared. If this is a global, consider adding a /** global: businessDocViewFormName */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
153
                $("#" + businessDocViewFormName).attr('action', results.substring(3)).submit();
154
            } else {
155
                alert(results);
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...
156
                $("#" + businessDocViewFormName + " :input[name=\"multireqtoken\"]").val(randomString(20));
157
            }
158
        },
159
        error: function (msg) {
160
            alert(msg.status + " " + msg.responseText);
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...
161
        }
162
    });
163
}
164
165
function isBusinessDocumentPage()
166
{
167
    let businessDocument = '';
168
    if ($('#formEditFacturaProveedor').length > 0) {
169
        businessDocument = 'Compra';
170
    } else if ($('#formEditFacturaCliente').length > 0) {
171
        businessDocument = 'Venta';
172
    }
173
    return businessDocument;
174
}
175
176
async function cargarTipoNCF(tipoOperacion)
177
{
178
    return $.ajax({
179
        url: 'ListNCFTipo',
180
        async: true,
181
        data: {'action': 'busca_tipo', 'tipodocumento': tipoOperacion.toLowerCase() },
182
        type: 'POST',
183
        datatype: 'json',
184
        success: function (response) {
185
            let data = JSON.parse(response);
186
            return data;
187
        },
188
        error: function (xhr, status) {
189
            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...
190
        }
191
    });
192
}
193
194
$(document).ready(function () {
195
    let tipoOperacion = isBusinessDocumentPage();
196
    let varNCFTipoComprobante = $("#ncftipocomprobante");
197
    if (varNCFTipoComprobante.length !== 0 && varNCFTipoComprobante.val() !== '' && tipoOperacion !== '') {
198
        verificarCorrelativoNCF($("#ncftipocomprobante").val(), tipoOperacion);
199
    }
200
201
    varNCFTipoComprobante.change(function () {
202
        logConsole(varNCFTipoComprobante.val(),"#doc_codsubtipodoc val");
203
        if (tipoOperacion !== '') {
204
            verificarCorrelativoNCF(varNCFTipoComprobante.val(), tipoOperacion);
205
        }
206
    });
207
});