Passed
Push — master ( 88ea97...aa75e3 )
by Joe Nilson
02:36
created

Assets/JS/EditFacturaCliente.js   A

Complexity

Total Complexity 27
Complexity/F 1.59

Size

Lines of Code 168
Function Count 17

Duplication

Duplicated Lines 168
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 114
c 0
b 0
f 0
dl 168
loc 168
rs 10
wmc 27
mnd 10
bc 10
fnc 17
bpm 0.5881
cpm 1.5882
noi 8

4 Functions

Rating   Name   Duplication   Size   Complexity  
A EditFacturaCliente.js ➔ cargarTipoMovimiento 17 17 3
A EditFacturaCliente.js ➔ cargarTipoPago 17 17 3
A EditFacturaCliente.js ➔ cargarInfoCliente 17 17 3
D EditFacturaCliente.js ➔ businessDocViewSave 67 67 12

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 View Code Duplication
function businessDocViewSubjectChanged()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
21
{
22
    var data = {};
23
    $.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...
24
        data[value.name] = value.value;
25
    });
26
    data.action = "subject-changed";
27
    logConsole(data,"subject-changed");
28
29
    $.ajax({
30
        type: "POST",
31
        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...
32
        dataType: "json",
33
        data: data,
34
        success: function (results) {
35
            $("#doc_codpago").val(results.codpago);
36
            $("#doc_codserie").val(results.codserie);
37
            $("#formEditFacturaCliente select[name=ncftipopago]").val(results.ncftipopago);
38
39
            /**
40
             * Review the doc_codsubtipodoc existence,
41
             * if it exist we put the value from the customer data
42
             */
43
44
            if ($("#doc_codsubtipodoc").length !== 0) {
45
                $("#doc_codsubtipodoc").val(results.codsubtipodoc);
46
            }
47
            logConsole(results.codsubtipodoc,"codsubtipodoc");
48
            /**
49
             * Review the doc_codopersaciondoc existence,
50
             * if it exist we put the value from the customer data
51
             */
52
            if ($("#doc_codoperaciondoc").length !== 0) {
53
                $("#doc_codoperaciondoc").val(results.codoperaciondoc);
54
            }
55
            logConsole(results.codoperaciondoc,"codoperaciondoc");
56
            
57
            logConsole(results,"results");
58
59
            businessDocViewRecalculate();
60
        },
61
        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...
62
            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...
63
        }
64
    });
65
}
66
67
async function cargarInfoCliente()
68
{
69
    return $.ajax({
70
        url: 'ListNCFTipo',
71
        async: true,
72
        data: {'action': 'busca_infocliente', 'codcliente': $("#codclienteAutocomplete").val()},
73
        type: 'POST',
74
        datatype: 'json',
75
        success: function (response) {
76
            let data = JSON.parse(response);
77
            return data;
78
        },
79
        error: function (xhr, status) {
80
            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...
81
        }
82
    });
83
}
84
85
async function cargarTipoPago()
86
{
87
    return $.ajax({
88
        url: 'ListNCFTipoPago',
89
        async: true,
90
        data: {'action': 'busca_pago', 'tipopago': '01'},
91
        type: 'POST',
92
        datatype: 'json',
93
        success: function (response) {
94
            let data = JSON.parse(response);
95
            return data;
96
        },
97
        error: function (xhr, status) {
98
            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...
99
        }
100
    });
101
}
102
103
async function cargarTipoMovimiento()
104
{
105
    return $.ajax({
106
        url: 'ListNCFTipoMovimiento',
107
        async: true,
108
        data: {'action': 'busca_movimiento', 'tipomovimiento': 'VEN'},
109
        type: 'POST',
110
        datatype: 'json',
111
        success: function (response) {
112
            let data = JSON.parse(response);
113
            return data;
114
        },
115
        error: function (xhr, status) {
116
            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...
117
        }
118
    });
119
}
120
121
async function businessDocViewSave()
122
{
123
    if ($("#codclienteAutocomplete").val() === '') {
124
        executeModal('errorNoClienteDetectado','No hay Cliente','Debe seleccionar un cliente primero!', 'warning', '');
125
    } else {
126
        $("#btn-document-save").prop("disabled", true);
127
        var infoCliente = await cargarInfoCliente();
128
        logConsole(infoCliente, 'infoCliente');
129
        var datosCliente = JSON.parse(infoCliente);
130
        var tipoPago = await cargarTipoPago();
131
        var datosPago = JSON.parse(tipoPago);
132
        var tipoNCFs = await cargarTipoNCF('Ventas');
133
        logConsole(tipoNCFs, 'tipoNCFs');
134
        var datosTipoNCFs = JSON.parse(tipoNCFs);
135
        let selectTiposNCF = "";
136
        var descInfoClienteTipoComprobante = '';
137
        $.each(datosTipoNCFs.tipocomprobantes, function (i, value) {
138
            let defaultSelected = (datosCliente.infocliente.tipocomprobante === value.tipocomprobante) ? 'selected' : '';
139
            descInfoClienteTipoComprobante = (datosCliente.infocliente.tipocomprobante === value.tipocomprobante)
140
                ? value.descripcion : descInfoClienteTipoComprobante;
141
            selectTiposNCF += '<option value="'+value.tipocomprobante+'"'+defaultSelected+'>'+value.descripcion+'</option>';
142
        });
143
144
        var ncfTipoPagoCliente = datosCliente.infocliente.ncftipopago;
145
        var readOnlySelects = ($("#formSalesDocumentLine #doc_idestado").val() === '11');
146
        var descInfoClienteTipoPago = '';
147
        let selectOptionsPagos = "";
148
        logConsole(ncfTipoPagoCliente, 'ncfTipoPagoCliente');
149
        $.each(datosPago.pagos, function (i, value) {
150
            let defaultSelected = ((value.codigo === '17' && ncfTipoPagoCliente === '') || ncfTipoPagoCliente === value.codigo) ? 'selected' : '';
151
            descInfoClienteTipoPago = (datosCliente.infocliente.ncftipopago === value.codigo)
152
                ? value.descripcion : descInfoClienteTipoPago;
153
            let noSelected = ($("#formSalesDocumentLine #doc_idestado").val() === '11' && defaultSelected !== 'selected') ? ' disabled' : '';
154
            selectOptionsPagos += '<option value="'+value.codigo+'"'+defaultSelected+noSelected+'>'+value.descripcion+'</option>';
155
        });
156
157
        var tipoMovimiento = await cargarTipoMovimiento();
158
        var datosMovimiento = JSON.parse(tipoMovimiento);
159
160
        let selectOptionsMovimientos = "";
161
        $.each(datosMovimiento.movimientos, function (i, value) {
162
            let defaultSelected = (value.codigo === '1') ? 'selected' : '';
163
            let noSelected = ($("#formSalesDocumentLine #doc_idestado").val() === '11' && defaultSelected !== 'selected') ? ' disabled' : '';
164
            selectOptionsMovimientos += '<option value="'+value.codigo+'"'+defaultSelected+noSelected+'>'+value.descripcion+'</option>';
165
        });
166
167
        let message = setBusinessDocViewModalSave(
168
            'Cliente',
169
            readOnlySelects,
170
            descInfoClienteTipoComprobante,
171
            descInfoClienteTipoPago,
172
            selectTiposNCF,
173
            selectOptionsPagos,
174
            selectOptionsMovimientos
175
        );
176
177
        executeModal(
178
            'completeNCFData',
179
            'Complete la información faltante',
180
            message,
181
            'default',
182
            'saveBussinessDocument'
183
        );
184
185
        $("#btn-document-save").prop("disabled", false);
186
    }
187
}
188