Passed
Push — master ( 9c95d4...eee053 )
by Joe Nilson
03:19
created

Assets/JS/EditFacturaCliente.js   A

Complexity

Total Complexity 26
Complexity/F 1.44

Size

Lines of Code 170
Function Count 18

Duplication

Duplicated Lines 10
Ratio 5.88 %

Importance

Changes 0
Metric Value
eloc 115
c 0
b 0
f 0
dl 10
loc 170
rs 10
wmc 26
mnd 8
bc 8
fnc 18
bpm 0.4444
cpm 1.4443
noi 15

3 Functions

Rating   Name   Duplication   Size   Complexity  
F EditFacturaCliente.js ➔ businessDocViewSave 10 91 14
A EditFacturaCliente.js ➔ cargarTipoMovimiento 0 17 3
A EditFacturaCliente.js ➔ cargarTipoPago 0 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
2
/*
3
 * This file is part of FacturaScripts - Dominican Republic Plugin
4
 * Copyright (C) 2013-2019 Carlos Garcia Gomez <[email protected]>
5
 * Copyright (C) 2019-2020 Joe Nilson <[email protected]>
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
function businessDocViewSubjectChanged() {
21
    var data = {};
22
    $.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...
23
        data[value.name] = value.value;
24
    });
25
    data.action = "subject-changed";
26
    console.log("data", data);
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...
27
28
    $.ajax({
29
        type: "POST",
30
        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...
31
        dataType: "json",
32
        data: data,
33
        success: function (results) {
34
            $("#doc_codpago").val(results.codpago);
35
            $("#doc_codserie").val(results.codserie);
36
            $("#formEditFacturaCliente select[name=ncftipopago]").val(results.ncftipopago);
37
38
            /**
39
             * Review the doc_codsubtipodoc existence,
40
             * if it exist we put the value from the customer data
41
             */
42
            if($("#doc_codsubtipodoc").length !== 0) {
43
                $("#doc_codsubtipodoc").val(results.codsubtipodoc);
44
            }
45
            /**
46
             * Review the doc_codopersaciondoc existence,
47
             * if it exist we put the value from the customer data
48
             */
49
            if($("#doc_codoperaciondoc").length !== 0) {
50
                $("#doc_codoperaciondoc").val(results.codoperaciondoc);
51
            }
52
            
53
            console.log("results", results);
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...
54
55
            businessDocViewRecalculate();
56
        },
57
        error: function (xhr, status, error) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter status is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
58
            alert(xhr.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...
59
        }
60
    });
61
}
62
63
async function cargarTipoPago()
64
{
65
    return $.ajax({
66
        url: 'ListNCFTipoPago',
67
        async: true,
68
        data: {'action': 'busca_pago', 'tipopago': '01'},
69
        type: 'POST',
70
        datatype: 'json',
71
        success: function (response) {
72
            let data = JSON.parse(response);
73
            return data;
74
        },
75
        error: function (xhr, status) {
76
            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...
77
        }
78
    });
79
}
80
81
async function cargarTipoMovimiento()
82
{
83
    return $.ajax({
84
        url: 'ListNCFTipoMovimiento',
85
        async: true,
86
        data: {'action': 'busca_movimiento', 'tipomovimiento': 'VEN'},
87
        type: 'POST',
88
        datatype: 'json',
89
        success: function (response) {
90
            let data = JSON.parse(response);
91
            return data;
92
        },
93
        error: function (xhr, status) {
94
            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...
95
        }
96
    });
97
}
98
99
async function businessDocViewSave()
100
{
101
    $("#btn-document-save").prop("disabled", true);
102
103
    var tipoPago = await cargarTipoPago();
104
    var datosPago = JSON.parse(tipoPago);
105
    var ncfTipoPagoCliente = $("#formEditFacturaCliente select[name=ncftipopago]").val();
106
    var readOnlySelects = ($("#formSalesDocumentLine #doc_idestado").val() === '11')?true:false;
107
    let selectOptionsPagos = "";
108 View Code Duplication
    $.each(datosPago.pagos, function(i, value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
109
        let defaultSelected = ((value.codigo === '17' && ncfTipoPagoCliente === '') || ncfTipoPagoCliente === value.codigo) ? 'selected' : '';
110
        let noSelected = ($("#formSalesDocumentLine #doc_idestado").val() === '11' && defaultSelected !== 'selected') ? ' disabled' : '';
111
        selectOptionsPagos += '<option value="'+value.codigo+'"'+defaultSelected+noSelected+'>'+value.descripcion+'</option>';
112
    });
113
114
    var tipoMovimiento = await cargarTipoMovimiento();
115
    var datosMovimiento = JSON.parse(tipoMovimiento);
116
117
    let selectOptionsMovimientos = "";
118 View Code Duplication
    $.each(datosMovimiento.movimientos, function(i, value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
119
        let defaultSelected = (value.codigo === '1') ? 'selected' : '';
120
        let noSelected = ($("#formSalesDocumentLine #doc_idestado").val() === '11' && defaultSelected !== 'selected') ? ' disabled' : '';
121
        selectOptionsMovimientos += '<option value="'+value.codigo+'"'+defaultSelected+noSelected+'>'+value.descripcion+'</option>';
122
    });
123
124
125
    bootbox.dialog({
0 ignored issues
show
Bug introduced by
The variable bootbox seems to be never declared. If this is a global, consider adding a /** global: bootbox */ 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...
126
        title: "Complete la información faltante",
127
        message: '<div class="form-content">\n' +
128
            '      <form class="form" role="form">\n' +
129
            '        <div class="form-group">\n' +
130
            '          <label for="ncftipopago">Tipo de Pago</label>\n' +
131
            '          <select class="custom-select" id="ncftipopago" name="ncftipopago"'+readOnlySelects+'>\n' +
132
                        selectOptionsPagos +
133
            '          </select>\n' +
134
            '        </div>\n' +
135
            '        <div class="form-group">\n' +
136
            '          <label for="ncftipomovimiento">Tipo de Movimiento</label>\n' +
137
            '          <select class="custom-select" id="ncftipomovimiento" name="ncftipomovimiento"'+readOnlySelects+'>\n' +
138
                        selectOptionsMovimientos +
139
            '          </select>\n' +
140
            '        </div>\n' +
141
            '      </form>\n' +
142
            '    </div>',
143
        buttons: [
144
            {
145
                label: "Guardar",
146
                className: "btn btn-primary",
147
                callback: function() {
148
                    var data = {};
149
                    $.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...
150
                        data[value.name] = value.value;
151
                    });
152
                    data['ncftipopago'] = $('form #ncftipopago').val();
153
                    data['ncftipomovimiento'] = $('form #ncftipomovimiento').val();
154
                    data.action = "save-document";
155
                    data.lines = getGridData();
156
157
                    $.ajax({
158
                        type: "POST",
159
                        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...
160
                        dataType: "text",
161
                        data: data,
162
                        success: function (results) {
163
                            if (results.substring(0, 3) === "OK:") {
164
                                $("#" + 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...
165
                                $("#" + businessDocViewFormName).attr('action', results.substring(3)).submit();
166
                            } else {
167
                                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...
168
                                $("#" + businessDocViewFormName + " :input[name=\"multireqtoken\"]").val(randomString(20));
169
                            }
170
                        },
171
                        error: function (msg) {
172
                            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...
173
                        }
174
                    });
175
176
                }
177
            },
178
            {
179
                label: "Cancelar",
180
                className: "btn btn-danger",
181
                callback: function() {
182
                    return true;
183
                }
184
            }
185
        ],
186
    });
187
188
    $("#btn-document-save").prop("disabled", false);
189
}