Passed
Pull Request — master (#43)
by Kiran
03:49
created

euvat.js ➔ wpinv_recalculate_taxes   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 5
nop 1
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A euvat.js ➔ ... ➔ jQuery.fail 0 4 1
B euvat.js ➔ ... ➔ jQuery.ajax.success 0 27 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
jQuery(function($) {
2
    $('body').bind('wpinv_tax_recalculated', function(e, data) {
3
        var el = $('#wpinv_adddress_confirm');
4
        el.hide();
5
        if (WPInv_VAT_Vars.ApplyVATRules) {
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
6
            var eu_state = wpinv_is_eu_state(data.post.country);
7
            var non_eu = !eu_state && !wpinv_is_eu_state(el.attr('value'));
8
            if (!non_eu && $('#wpinv_vat_number').val().trim().length === 0 && data.post.country !== el.attr('value')) {
9
                el.show();
10
            }
11
        }
12
    });
13
    var WPInv_VAT_Config = {
14
        init: function() {
15
            this.taxes(this);
16
            var me = this;
0 ignored issues
show
Unused Code introduced by
The variable me seems to be never used. Consider removing it.
Loading history...
17
        },
18
        checkVATNumber: function(el, err) {
19
            try {
20
                if (el) {
21
                    var valid = false;
22
                    var msg = '';
23
                    var value = el.val();
24
                    if (value.length > 0) {
25
                        if (checkVATNumber(value)) {
26
                            valid = true;
27
                        } else {
28
                            msg = WPInv_VAT_Vars.ErrInvalidVat;
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
29
                        }
30
                    } else {
31
                        msg = WPInv_VAT_Vars.EmptyVAT;
32
                    }
33
                    if (valid) {
34
                        return true;
35
                    } else if (err && msg) {
36
                        alert(msg);
37
                    }
38
                    return false;
39
                }
40
                return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
41
            } catch (e) {
42
                if (err) {
43
                    alert(WPInv_VAT_Vars.ErrValidateVAT + ": " + e.message);
44
                }
45
                return false;
46
            }
47
        },
48
        taxes: function(config) {
49
            var has_vat = $('#wpi_vat_info').is(':visible');
50
            var eu_states = WPInv_VAT_Vars.EUStates;
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
51
            $('body').bind('wpinv_tax_recalculated', function(e, data) {
52
                $('.wpinv_errors').html('').hide();
53
                if (data.post.country === 'UK') {
54
                    data.post.country = 'GB';
55
                }
56
                var eu_state = wpinv_is_eu_state(data.post.country);
57
                if (eu_state && WPInv_VAT_Vars.disableVATSameCountry && wpinv_is_base_country(data.post.country)) {
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
58
                    eu_state = false;
59
                }
60
                if (eu_state && !WPInv_VAT_Vars.HideVatFields) {
61
                    $('#wpi_vat_info').show();
62
                    $('#wpi_vat_info').parent('.wpi-vat-details').show();
63
                } else {
64
                    $('#wpi_vat_info').hide();
65
                }
66
                if (has_vat == eu_state) {
67
                    if (eu_state) {
68
                        config.reset(config, $('#wpinv_vat_reset'), false);
69
                    }
70
                    return;
71
                }
72
                has_vat = eu_state;
73
                wpinv_recalculate_taxes();
74
            });
75
            $('#wpi_add_eu_states').on('click', function() {
76
                var rate = $('#wpinv_settings_rates_eu_fallback_rate').val();
77
                if (rate === null || rate === '') {
78
                    if (!confirm(WPInv_VAT_Vars.NoRateSet)) return;
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
79
                }
80
                $('#wpi_remove_eu_states').trigger('click');
81
                var row = $('#wpinv_tax_rates tbody tr:last');
82
                var clone = row.clone();
83
                var body = row.parent();
84
                var count = $('#wpinv_tax_rates tbody tr').length;
85
                $.each(eu_states, function(i, state) {
86
                    row = clone.clone();
87
                    row.find('td input').val('');
88
                    row.find('input, select').each(function() {
89
                        var name = $(this).attr('name');
90
                        name = name.replace(/\[(\d+)\]/, '[' + parseInt(count) + ']');
91
                        $(this).attr('name', name).attr('id', name);
92
                    });
93
                    row.find('#tax_rates\\[' + count + '\\]\\[rate\\]').val(rate);
94
                    row.find('#tax_rates\\[' + count + '\\]\\[country\\]').val(state);
95
                    row.find('#tax_rates\\[' + count + '\\]\\[state\\]').replaceWith('<input type="text" class="regular-text" value="" id="tax_rates[' + count + '][state]" name="tax_rates[' + count + '][state]">');
96
                    row.find('#tax_rates\\[' + count + '\\]\\[global\\]').prop('checked', true);
97
                    row.find('#tax_rates\\[' + count + '\\]\\[global\\]').val(1);
98
                    body.append(row);
99
                    count++;
100
                });
101
                return false;
102
            });
103
            $('#wpi_remove_eu_states').on('click', function() {
104
                $('#wpinv_tax_rates select.wpinv-tax-country').each(function(i) {
0 ignored issues
show
Unused Code introduced by
The parameter i 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...
105
                    if (jQuery(this).val() && wpinv_is_eu_state(jQuery(this).val())) {
106
                        if ($('#wpinv_tax_rates tbody tr').length === 1) {
107
                            $('#wpinv_tax_rates select').val('');
108
                            $('#wpinv_tax_rates select').trigger('change');
109
                            $('#wpinv_tax_rates input[type="text"]').val('');
110
                            $('#wpinv_tax_rates input[type="number"]').val('');
111
                            $('#wpinv_tax_rates input[type="checkbox"]').attr('checked', false);
112
                        } else {
113
                            jQuery(this).closest('tr').remove();
114
                        }
115
                    }
116
                });
117
            });
118
            jQuery('#wpi_vat_get_rates').on('click', function(e) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
119
                e.preventDefault();
120
                var $this = $(this);
121
                $this.blur();
122
                WPInv_VAT_Config.disableButtons();
123
                var $loading = $this.closest('span').find('.fa-refresh');
124
                $loading.show();
125
                var data = {
126
                    action: 'wpinv_update_vat_rates',
127
                    group: 'standard'
128
                };
129
                jQuery.post(ajaxurl, data, function(response) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
130
                        try {
131
                            if (!response) {
132
                                WPInv_VAT_Config.showError(WPInv_VAT_Vars.ErrRateResponse);
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
133
                                return;
134
                            }
135
                            var vat_rates_array = response;
136
                            if (vat_rates_array.success !== true) {
137
                                WPInv_VAT_Config.showError(WPInv_VAT_Vars.GetRateRequestFailed + (vat_rates_array.error ? vat_rates_array.error : 'reason unknown'));
138
                                return;
139
                            }
140
                            if (!vat_rates_array.data) {
141
                                WPInv_VAT_Config.showError(WPInv_VAT_Vars.NoRateInformationInResponse);
142
                                return;
143
                            }
144
                            var vat_rates = vat_rates_array.data;
145
                            var countries = jQuery('#wpinv_tax_rates select.wpinv-tax-country');
146
                            countries.each(function(i, country_td) {
147
                                var country_el = jQuery(country_td);
148
                                var code = country_el.val();
149
                                if (!vat_rates.rates[code]) {
150
                                    return;
151
                                }
152
                                var rate_el = country_el.closest('tr').find('.wpinv_tax_rate input');
153
                                var rate_class = country_el.closest('tr').find('.wpinv_tax_rate input');
0 ignored issues
show
Unused Code introduced by
The variable rate_class seems to be never used. Consider removing it.
Loading history...
154
                                var rate = vat_rates.rates[code].standard;
155
                                rate_el.val(rate);
156
                            });
157
                            $loading.hide();
158
                            WPInv_VAT_Config.showError(WPInv_VAT_Vars.RatesUpdated);
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
159
                        } catch (e) {
160
                            $loading.hide();
161
                            WPInv_VAT_Config.showError(e.message);
162
                        }
163
                    })
164
                    .fail(function(jqXHR, textStatus, errorThrown) {
0 ignored issues
show
Unused Code introduced by
The parameter errorThrown 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 textStatus 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 jqXHR 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...
165
                        $loading.hide();
166
                        $this.removeAttr('disabled', 'disabled');
167
                    });
168
            });
169
            jQuery('#wpi_vat_get_rates_group').on('click', function(e) {
170
                e.preventDefault();
171
                var $this = $(this);
172
                $this.blur();
173
                WPInv_VAT_Config.disableButtons();
174
                var $loading = $this.closest('span').find('.fa-refresh');
175
                $loading.show();
176
                var data = {
177
                    action: 'wpinv_update_vat_rates'
178
                };
179
                jQuery.post(ajaxurl, data, function(response) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
180
                        try {
181
                            if (!response) {
182
                                WPInv_VAT_Config.showError(WPInv_VAT_Vars.ErrRateResponse);
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
183
                                return;
184
                            }
185
                            var vat_rates_array = response;
186
                            if (vat_rates_array.success !== true) {
187
                                WPInv_VAT_Config.showError(WPInv_VAT_Vars.GetRateRequestFailed + (vat_rates_array.error ? vat_rates_array.error : 'reason unknown'));
188
                                return;
189
                            }
190
                            if (!vat_rates_array.data) {
191
                                WPInv_VAT_Config.showError(WPInv_VAT_Vars.NoRateInformationInResponse);
192
                                return;
193
                            }
194
                            var vat_rates = vat_rates_array.data;
195
                            jQuery.each(vat_rates.rates, function(sCode, oRate) {
196
                                if (sCode) {
197
                                    var sGroup = jQuery('.wpinv_vat_group select[name="vat_rates[' + sCode + '][group]"]').val();
198
                                    if (sGroup && typeof oRate[sGroup] !== 'undefined') {
199
                                        jQuery('.wpinv_vat_rate input[name="vat_rates[' + sCode + '][rate]"]').val(parseFloat(oRate[sGroup]));
200
                                    }
201
                                }
202
                            });
203
                            $loading.hide();
204
                            WPInv_VAT_Config.showError(WPInv_VAT_Vars.RatesUpdated);
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
205
                        } catch (e) {
206
                            $loading.hide();
207
                            WPInv_VAT_Config.showError(e.message);
208
                        }
209
                    })
210
                    .fail(function(jqXHR, textStatus, errorThrown) {
0 ignored issues
show
Unused Code introduced by
The parameter errorThrown 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 jqXHR 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 textStatus 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...
211
                        $loading.hide();
212
                        $this.removeAttr('disabled', 'disabled');
213
                    });
214
            });
215
            $('#wpi_vat_rate_add').on('click', function() {
216
                var $n = $('#wpinv_settings\\[vat_rate_name\\]');
217
                var $d = $('#wpinv_settings\\[vat_rate_desc\\]');
218
                var $r = $('.wpi-vat-rate-actions .fa-refresh');
219
                var n = $.trim($n.val());
220
                var d = $.trim($d.val());
221
                if (!n) {
222
                    $n.focus();
223
                    return false;
224
                }
225
                var me = $(this);
226
                me.attr('disabled', 'disabled');
227
                $r.show();
228
                var data = {
229
                    action: 'wpinv_add_vat_class',
230
                    name: n,
231
                    desc: d
232
                };
233
                $.post(ajaxurl, data, function(json) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
234
                        var error = '';
235
                        var message = '';
0 ignored issues
show
Unused Code introduced by
The variable message seems to be never used. Consider removing it.
Loading history...
236
                        if (json && typeof json == 'object') {
237
                            if (json.success === true) {
238
                                $r.removeClass('fa-refresh fa-spin').addClass('fa-check-circle orange');
239
                                window.location = json.redirect;
240
                                return;
241
                            } else {
242
                                error = json.error ? json.error : '';
243
                            }
244
                        }
245
                        me.removeAttr('disabled');
246
                        $r.hide();
247
                        if (error) {
248
                            alert(error);
249
                        }
250
                        return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
251
                    })
252
                    .fail(function() {
253
                        me.removeAttr('disabled');
254
                        $r.hide();
255
                    });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
256
            });
257
            $('#wpi_vat_rate_delete').on('click', function() {
258
                var $c = $('#wpinv_settings\\[vat_rates_class\\]');
259
                var $r = $('.wpi-vat-rate-actions .fa-refresh');
260
                var c = $.trim($c.val());
261
                if (!confirm(WPInv_VAT_Vars.ConfirmDeleteClass)) {
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
262
                    return;
263
                }
264
                var me = $(this);
265
                me.attr('disabled', 'disabled');
266
                $r.show();
267
                var data = {
268
                    action: 'wpinv_delete_vat_class',
269
                    class: c,
270
                };
271
                $.post(ajaxurl, data, function(json) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
272
                        var error = '';
273
                        var message = '';
0 ignored issues
show
Unused Code introduced by
The variable message seems to be never used. Consider removing it.
Loading history...
274
                        if (json && typeof json == 'object') {
275
                            if (json.success === true) {
276
                                $r.removeClass('fa-refresh fa-spin').addClass('fa-check-circle orange');
277
                                window.location = json.redirect;
278
                                return;
279
                            } else {
280
                                error = json.error ? json.error : '';
281
                            }
282
                        }
283
                        me.removeAttr('disabled');
284
                        $r.hide();
285
                        if (error) {
286
                            alert(error);
287
                        }
288
                        return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
289
                    })
290
                    .fail(function() {
291
                        me.removeAttr('disabled');
292
                        $r.hide();
293
                    });
294
            });
295
            jQuery('#wpi_geoip2').on('click', function(e) {
296
                e.preventDefault();
297
                var el = $(this);
298
                var action = el.attr('action');
299
                el.blur().attr('disabled', 'disabled');
300
                el.after('&nbsp;<i id="wpi-downloading" class="fa fa-refresh fa-spin"></i>');
301
                $('#wpi-downloading').show();
302
                var data = {
303
                    action: 'wpinv_geoip2',
304
                };
305
                jQuery.post(ajaxurl, data,
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
306
                    function(response) {
307
                        var msg = '';
308
                        var reload = false;
309
                        try {
310
                            if (response) {
311
                                reload = action == 'download' ? true : false;
312
                                msg = response + (action == 'download' ? ' ' + WPInv_VAT_Vars.PageRefresh : '');
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
313
                            } else {
314
                                msg = WPInv_VAT_Vars.ErrResponse;
315
                            }
316
                        } catch (e) {
317
                            msg = e.message;
318
                        }
319
                        WPInv_VAT_Config.showGeoIP2Error(msg, el, reload);
320
                    }).fail(function(jqXHR, textStatus, errorThrown) {
0 ignored issues
show
Unused Code introduced by
The parameter errorThrown 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...
321
                    WPInv_VAT_Config.showGeoIP2Error("Status: " + textStatus, el);
322
                })
323
            });
324
            $('#wpinv_vat_reset').on('click', function(e) {
325
                e.preventDefault();
326
                WPInv_VAT_Config.reset(WPInv_VAT_Config, $(this), true);
327
            });
328
            if (WPInv_VAT_Vars.isFront) {
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
329
                var elErr = $('.wpi-cart-field-actions .wpi-vat-box-error');
330
                var elInfo = $('.wpi-cart-field-actions .wpi-vat-box-info');
331
                $('#wpinv_vat_validate').on('click', function() {
332
                    elErr.hide();
333
                    elInfo.hide();
334
                    var companyEl = $('#wpinv_checkout_form #wpinv_company');
335
                    var company = companyEl.val();
336
                    var numberEl = $('#wpinv_checkout_form #wpinv_vat_number');
337
                    var vat_number = numberEl.val();
338
                    if ((company && (company.length > 0)) || (vat_number && (vat_number.length > 0))) {
339
                        if (!(company && (company.length > 0))) {
340
                            WPInv_VAT_Config.displayMessage(WPInv_VAT_Vars.EmptyCompany, 'error');
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
341
                            return false;
342
                        }
343
                        if (!(vat_number && (vat_number.length > 0))) {
344
                            WPInv_VAT_Config.displayMessage(WPInv_VAT_Vars.EmptyVAT, 'error');
345
                            return false;
346
                        }
347
                    }
348
                    if (!WPInv_VAT_Vars.disableVATSimpleCheck && (vat_number && (vat_number.length > 0)) && !WPInv_VAT_Config.checkVATNumber(numberEl, false)) {
349
                        WPInv_VAT_Config.displayMessage(WPInv_VAT_Vars.ErrValidateVAT, 'error');
350
                        return false;
351
                    }
352
                    var number = numberEl.val();
353
                    var nonce = $('input[name=_wpi_nonce]').val();
354
                    var me = $(this);
355
                    me.attr('disabled', 'disabled');
356
                    $('.wpinv-vat-stat').removeClass('wpinv-vat-stat-0 wpinv-vat-stat-1').addClass('wpinv-vat-stat-2');
357
                    $('.wpinv-vat-stat font').html(WPInv_VAT_Vars.VatValidating);
358
                    var data = {
359
                        action: 'wpinv_vat_validate',
360
                        company: company,
361
                        number: number,
362
                        country: $('[name=wpinv_country]').val(),
363
                        source: 'checkout',
364
                        _wpi_nonce: nonce
365
                    };
366
                    $.post(ajaxurl, data, function(json) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
367
                            var validated = false;
368
                            var error = '';
369
                            var message = '';
370
                            if (json && typeof json == 'object') {
371
                                if (json.success === true) {
372
                                    validated = true;
373
                                    message = json.message ? json.message : '';
374
                                } else {
375
                                    error = json.error ? json.error : json.message;
376
                                }
377
                            }
378
                            me.removeAttr('disabled');
379
                            if (validated) {
380
                                if (number.length === 0) {} else {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
381
                                    me.hide();
382
                                    $('#wpinv_vat_reset').show();
383
                                }
384
                                $('.wpinv-vat-stat font').html(message ? message : WPInv_VAT_Vars.VatValidated);
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
385
                                $('.wpinv-vat-stat').removeClass('wpinv-vat-stat-2').addClass('wpinv-vat-stat-1');
386
                            } else {
387
                                $('.wpinv-vat-stat font').html(WPInv_VAT_Vars.VatNotValidated);
388
                                $('.wpinv-vat-stat').removeClass('wpinv-vat-stat-2').addClass('wpinv-vat-stat-0');
389
                            }
390
                            if (number.length > 0 || $('#wpinv_country').val() === $('#wpinv_adddress_confirm').attr('value')) {
391
                                $('#wpinv_adddress_confirm').hide();
392
                            } else {
393
                                $('#wpinv_adddress_confirm').show();
394
                            }
395
                            if (error) {
396
                                config.displayMessage(error + '<br>' + WPInv_VAT_Vars.TotalsRefreshed, 'error');
397
                            } else {
398
                                config.displayMessage(WPInv_VAT_Vars.TotalsRefreshed, 'info');
399
                            }
400
                            wpinv_recalculate_taxes();
401
                            return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
402
                        })
403
                        .fail(function() {
404
                            me.removeAttr('disabled');
405
                            $('.wpinv-vat-stat font').html(WPInv_VAT_Vars.VatNotValidated);
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
406
                            $('.wpinv-vat-stat').removeClass('wpinv-vat-stat-2').addClass('wpinv-vat-stat-0');
407
                            WPInv_VAT_Config.displayMessage(WPInv_VAT_Vars.ErrValidateVAT, 'error');
408
                        });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
409
                });
410
            } else {
411
                $('#wpinv_vat_validate').on('click', function() {
412
                    var companyEl = $('#wpinv_settings\\[vat_company_name\\]');
413
                    var company = companyEl.val();
414
                    if (company.length == 0) {
415
                        alert(WPInv_VAT_Vars.EmptyCompany);
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
416
                        return false;
417
                    }
418
                    var vatEl = $('#wpinv_settings\\[vat_number\\]');
419
                    var number = vatEl.val();
420
                    if (!WPInv_VAT_Vars.disableVATSimpleCheck && !WPInv_VAT_Config.checkVATNumber(vatEl, true)) {
421
                        return false;
422
                    }
423
                    var nonce = $('input[name=_wpi_nonce]').val();
424
                    var me = $(this);
425
                    me.attr('disabled', 'disabled');
426
                    $('.wpinv-vat-stat').removeClass('wpinv-vat-stat-0 wpinv-vat-stat-1').addClass('wpinv-vat-stat-2');
427
                    $('.wpinv-vat-stat font').html(WPInv_VAT_Vars.VatValidating);
428
                    var data = {
429
                        action: 'wpinv_vat_validate',
430
                        company: company,
431
                        number: number,
432
                        _wpi_nonce: nonce,
433
                        source: 'admin'
434
                    };
435
                    $.post(ajaxurl, data, function(json) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
436
                            var validated = false;
437
                            var error = '';
438
                            var message = '';
439
                            if (json && typeof json == 'object') {
440
                                if (json.success === true) {
441
                                    validated = true;
442
                                    message = json.message ? json.message : '';
0 ignored issues
show
Unused Code introduced by
The assignment to variable message seems to be never used. Consider removing it.
Loading history...
443
                                } else {
444
                                    error = json.message ? json.message : json.error;
445
                                }
446
                            }
447
                            if (validated) {
448
                                $('.wpinv-vat-stat font').html(WPInv_VAT_Vars.VatValidated);
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
449
                                $('.wpinv-vat-stat').removeClass('wpinv-vat-stat-2').addClass('wpinv-vat-stat-1');
450
                            } else {
451
                                me.removeAttr('disabled');
452
                                $('.wpinv-vat-stat font').html(WPInv_VAT_Vars.VatNotValidated);
453
                                $('.wpinv-vat-stat').removeClass('wpinv-vat-stat-2').addClass('wpinv-vat-stat-0');
454
                                if (error) {
455
                                    alert(error);
456
                                }
457
                            }
458
                            return;
459
                        })
460
                        .fail(function() {
461
                            me.removeAttr('disabled');
462
                            $('.wpinv-vat-stat font').html(WPInv_VAT_Vars.VatNotValidated);
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
463
                            $('.wpinv-vat-stat').removeClass('wpinv-vat-stat-2').addClass('wpinv-vat-stat-0');
464
                            alert(WPInv_VAT_Vars.ErrValidateVAT);
465
                        });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
466
                });
467
            }
468
        },
469
        clearBox: function() {
470
            var texts = $('.wpi-vat-box #text');
471
            texts.html('');
472
            var boxes = texts.parents(".wpi-vat-box")
473
            boxes.fadeOut('fast');
474
        },
475
        reset: function(config, me, updateTaxes) {
476
            var tax = parseFloat($('#wpinv_checkout_form .wpinv_cart_tax_amount').attr('data-tax'));
477
            var total = parseFloat($('#wpinv_checkout_form .wpinv_cart_amount').attr('data-total'));
478
            var wpiCCaddressEl = $('#wpinv-fields .wpi-billing');
479
            var countryEl = wpiCCaddressEl.find('#wpinv_country').val();
480
            if (total === "0") {
481
                $('#wpi_vat_info').hide();
482
                return;
483
            }
484
            if (tax !== "0" && countryEl === undefined) {
485
                window.location.reload()
486
            }
487
            if (!WPInv_VAT_Vars.HideVatFields) {
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
488
                $('#wpi_vat_info').parent('.wpi-vat-details').show();
489
                $('#wpi_vat_info').show();
490
            }
491
            if (!updateTaxes) {
492
                return;
493
            }
494
            var numberEl = $('#wpinv_vat_number');
495
            var number = numberEl.val();
496
            if (number.length === 0 && $('.wpinv-vat-stat').hasClass('wpinv-vat-stat-1')) {
497
                return;
498
            }
499
            var nonce = $('input[name=_wpi_nonce]').val();
500
            me.attr('disabled', 'disabled');
501
            $('.wpinv-vat-stat').removeClass('wpinv-vat-stat-0 wpinv-vat-stat-1').addClass('wpinv-vat-stat-2');
502
            $('.wpinv-vat-stat font').html(WPInv_VAT_Vars.VatReseting);
503
            var validateButton = $('#wpinv_vat_validate');
504
            validateButton.hide();
505
            var data = {
506
                action: 'wpinv_vat_reset',
507
                _wpi_nonce: nonce,
508
                source: 'checkout'
509
            };
510
            $.post(ajaxurl, data, function(response) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
511
                    var json = response;
512
                    $('#wpinv_company').val("");
513
                    $('#wpinv_vat_number').val("");
514
                    $('.wpinv-vat-stat font').html(WPInv_VAT_Vars.VatNotValidated);
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
515
                    $('.wpinv-vat-stat').removeClass('wpinv-vat-stat-2').addClass('wpinv-vat-stat-0');
516
                    validateButton.show();
517
                    me.removeAttr('disabled');
518
                    me.hide();
519
                    if (json.success) {
520
                        $('#wpinv_company').val(json.data.company); // TODO
521
                        $('#wpinv_vat_number').val(json.data.number); // TODO
522
                        if (updateTaxes) {
523
                            wpinv_recalculate_taxes();
524
                        }
525
                        return;
526
                    }
527
                    config.displayMessage(json === undefined || json.message === undefined ? WPInv_VAT_Vars.ErrInvalidResponse : json.message, 'error');
528
                })
529
                .fail(function(jqXHR, textStatus, errorThrown) {
530
                    me.removeAttr('disabled');
531
                    me.show();
532
                    $('.wpinv-vat-stat font').html(WPInv_VAT_Vars.VatNotValidated);
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
533
                    $('.wpinv-vat-stat').removeClass('wpinv-vat-stat-2').addClass('wpinv-vat-stat-0');
534
                    config.displayMessage(WPInv_VAT_Vars.ErrResetVAT + " (" + textStatus + " - " + errorThrown + ")", 'error');
535
                })
536
        },
537
        showGeoIP2Error: function(msg, el, reload) {
538
            el.removeAttr('disabled');
539
            jQuery('#wpi-downloading').hide();
540
            jQuery('#wpinv-geoip2-errors').html(msg).show();
541
            setTimeout(function() {
542
                if (reload) {
543
                    window.location.reload();
544
                } else {
545
                    jQuery('#wpinv-geoip2-errors').hide();
546
                }
547
            }, 10000);
548
        },
549
        showError: function(message) {
550
            WPInv_VAT_Config.enableButtons();
551
            var errorEl = jQuery('#wpinv-rates-error-wrap');
552
            errorEl.html(message);
553
            errorEl.css("display", "block");
554
            setTimeout(function() {
555
                errorEl.hide();
556
            }, 10000);
557
        },
558
        disableButtons: function() {
559
            $('#wpi_vat_get_rates').attr('disabled', 'disabled');
560
            $('#wpi_add_eu_states').attr('disabled', 'disabled');
561
            $('#wpi_remove_eu_states').attr('disabled', 'disabled');
562
            $('#wpi_vat_get_rates_group').attr('disabled', 'disabled');
563
        },
564
        enableButtons: function() {
565
            $('#wpi_vat_get_rates').removeAttr('disabled');
566
            $('#wpi_add_eu_states').removeAttr('disabled');
567
            $('#wpi_remove_eu_states').removeAttr('disabled');
568
            $('#wpi_vat_get_rates_group').removeAttr('disabled');
569
        },
570
        displayMessage: function(m, c) {
571
            var box = $('.wpi-vat-box.wpi-vat-box-' + c + ' #text');
572
            if (box) {
573
                box.html(m);
574
                box.parent().show().slideDown().css("display", "inline-block");
575
            }
576
        }
577
    };
578
    WPInv_VAT_Config.init();
579
});
580
581
function wpinv_recalculate_taxes(state) {
582
    if (!WPInv_VAT_Vars.UseTaxes) {
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
583
        return false;
584
    }
585
    var $address = jQuery('#wpi-billing');
586
    if (!state) {
587
        state = $address.find('#wpinv_state').val();
588
    }
589
    var postData = {
590
        action: 'wpinv_recalculate_tax',
591
        nonce: WPInv_VAT_Vars.checkoutNonce,
592
        country: $address.find('#wpinv_country').val(),
593
        state: state
594
    };
595
    wpinvBlock(jQuery('#wpinv_checkout_cart_wrap'));
596
    jQuery.ajax({
597
        type: "POST",
598
        data: postData,
599
        dataType: "json",
600
        url: ajaxurl,
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
601
        success: function(res) {
602
            jQuery('#wpinv_checkout_cart_wrap').unblock();
603
            if (res && typeof res == 'object') {
604
                var $wrap = jQuery('#wpinv_checkout_form_wrap');
605
                jQuery('#wpinv_checkout_cart_form').replaceWith(res.html);
606
                jQuery('.wpinv-chdeckout-total', $wrap).text(res.total);
607
                if (res.data.free) {
608
                    $('#wpinv_payment_mode_select', $wrap).hide();
609
                    gw = 'manual';
0 ignored issues
show
Bug introduced by
The variable gw seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.gw.
Loading history...
610
                } else {
611
                    $('#wpinv_payment_mode_select', $wrap).show();
612
                    gw = $('#wpinv_payment_mode_select', $wrap).data('gateway');
613
                }
614
                $('input[name="wpi-gateway"]', $wrap).val(gw);
615
                var data = new Object();
616
                data.post = postData;
617
                data.response = res;
618
                data.recalculated = true;
619
                jQuery('body').trigger('wpinv_tax_recalculated', [data]);
620
            }
621
            setTimeout(function() {
622
                var texts = jQuery('.wpi-vat-box #text');
623
                texts.html('');
624
                var boxes = texts.parents(".wpi-vat-box")
625
                boxes.fadeOut('fast');
626
            }, 15000);
627
        }
628
    }).fail(function(jqXHR, textStatus, errorThrown) {
629
        jQuery('#wpinv_checkout_cart_wrap').unblock();
630
        console.log(errorThrown);
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...
631
    });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
632
}
633
634
function wpinv_is_eu_state(country) {
635
    return jQuery.inArray(country, WPInv_VAT_Vars.EUStates) !== -1 ? true : false;
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
636
}
637
638
function wpinv_is_base_country(country) {
639
    var baseCountry = WPInv_VAT_Vars.baseCountry;
0 ignored issues
show
Bug introduced by
The variable WPInv_VAT_Vars seems to be never declared. If this is a global, consider adding a /** global: WPInv_VAT_Vars */ 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...
640
    if (baseCountry === 'UK') {
641
        baseCountry = 'GB';
642
    }
643
    if (country == 'UK') {
644
        country = 'GB';
645
    }
646
    return (country && country === baseCountry) ? true : false;
647
}