1document.addEventListener('DOMContentLoaded', function () { 2 3 // Si existe la calculadora 4 if(document.getElementById('calculadora-altiria') && typeof url_home_altiria != 'undefined' ){ 5 // Variables JSON 6 // La variable url_home_altiria viene del shortcode 7 var url_json = url_home_altiria + '/wp-content/plugins/calculadora_altiria/public/json/calculadora.json'; 8 let object; 9 let httpRequest = new XMLHttpRequest(); 10 // Variables inputs 11 let selector_pais = document.getElementById('select-paises'); 12 let input_sms = document.getElementById('input_pais_destino'); 13 let input_euros = document.getElementById('input_euros_deseados'); 14 15 httpRequest.open("GET", url_json, true); 16 httpRequest.send(); 17 httpRequest.addEventListener("readystatechange", function() { 18 if (this.readyState === this.DONE) { 19 // Si tiene respuesta 20 object = JSON.parse(this.response); 21 const paises = object['countries']; 22 montar_paises(paises, codigo_pais_actual); 23 24 // Si cambia el pais 25 selector_pais.addEventListener('change', (evento) => { 26 if((selector_pais.value != '' && input_sms.value != '') || (selector_pais.value != '' && input_euros.value != '')){ 27 recalcular_presupuesto(paises, selector_pais.value, input_sms.value, input_euros.value, 'sms', url_plugin, msg_min, msg_max); 28 } 29 }); 30 // Si cambia el input de SMS 31 input_sms.addEventListener('input', (evento) => { 32 if(selector_pais.value != '' && input_sms.value != ''){ 33 recalcular_presupuesto(paises, selector_pais.value, input_sms.value, input_euros.value, 'sms', url_plugin, msg_min, msg_max); 34 } 35 }); 36 // Si cambia el input de euros 37 input_euros.addEventListener('input', (evento) => { 38 if(selector_pais.value != '' && input_euros.value != ''){ 39 recalcular_presupuesto(paises, selector_pais.value, input_sms.value, input_euros.value, 'euros', url_plugin, msg_min, msg_max); 40 } 41 }); 42 } 43 }); 44 } 45 46 47 /** 48 * Monta el select de paises 49 * 50 */ 51 function montar_paises(paises, codigo_pais_actual){ 52 // Recorre los paises del array 53 Object.values(paises).forEach(function(pais){ 54 var selector_pais = document.getElementById('select-paises'); 55 var opcion = document.createElement('option'); 56 opcion.appendChild(document.createTextNode(pais['label'])); 57 opcion.value = pais['code']; 58 if(pais['code'] == codigo_pais_actual){ 59 opcion.selected = 'selected'; 60 } 61 selector_pais.appendChild(opcion); 62 }); 63 } 64 65 66 /** 67 * Recalcula el presupuesto de la calculadora 68 */ 69 function recalcular_presupuesto(paises, pais, sms, euros, tipo, url_plugin, msg_min, msg_max){ 70 // Variables 71 const objeto_pais = paises[pais]; 72 let resultado_pais = document.getElementById('resultado_pais_destino'); 73 let resultado_sms = document.getElementById('resultado_sms_cantidad'); 74 let resultado_total = document.getElementById('resultado_euros_total'); 75 let precio_sms = document.getElementById('resultado_precio_sms'); 76 let moneda_euros_deseados = document.getElementById('moneda_euros_deseados'); 77 let max_sms = 0; 78 let max_precio = 0; 79 let total_euros = 0; 80 var bandera = document.createElement('img'); 81 var span = document.createElement('span'); 82 bandera.width = 24; 83 const rangos = objeto_pais['lots']; 84 let contenedor_error = document.getElementById('calculadora-contenedor-error'); 85 let mensaje_error = document.getElementById('calculadora-mensaje-error'); 86 var botonera_ok = document.getElementById('botonera-ok-calculadora'); 87 var botonera_no = document.getElementById('botonera-no-calculadora'); 88 var img_error_max = document.getElementById('img-error-max'); 89 var img_error_min = document.getElementById('img-error-min'); 90 91 if(tipo === 'euros'){ 92 sms = calcular_sms(rangos, euros); 93 } 94 if(parseInt(objeto_pais['min']) > parseInt(sms)){ 95 if(contenedor_error.classList.contains('oculto')){ 96 // Si no llega al mÃnimo 97 span.innerHTML = msg_min; 98 mensaje_error.appendChild(span); 99 document.getElementById('cantidad-sms-min').textContent = objeto_pais['min']; 100 contenedor_error.classList.remove('oculto'); 101 botonera_ok.classList.add('oculto'); 102 botonera_no.classList.remove('oculto'); 103 img_error_min.style.display = 'block'; 104 img_error_max.style.display = 'none'; 105 }else{ 106 document.getElementById('cantidad-sms-min').textContent = objeto_pais['min']; 107 } 108 }else if(parseInt(objeto_pais['max']) < parseInt(sms)){ 109 if(contenedor_error.classList.contains('oculto')){ 110 // Si supera el máximo 111 span.innerHTML = msg_max; 112 mensaje_error.appendChild(span); 113 document.getElementById('cantidad-sms-max').textContent = (objeto_pais['max']+1); 114 contenedor_error.classList.remove('oculto'); 115 botonera_ok.classList.add('oculto'); 116 botonera_no.classList.remove('oculto'); 117 img_error_min.style.display = 'none'; 118 img_error_max.style.display = 'block'; 119 }else{ 120 document.getElementById('cantidad-sms-max').textContent = (objeto_pais['max']+1); 121 } 122 }else{ 123 if(!contenedor_error.classList.contains('oculto')){ 124 contenedor_error.classList.add('oculto'); 125 botonera_ok.classList.remove('oculto'); 126 botonera_no.classList.add('oculto'); 127 mensaje_error.textContent = ''; 128 } 129 } 130 131 if(tipo == 'sms'){ 132 console.log(calcular_precio_sms(rangos, sms)); 133 resultado_pais.textContent = objeto_pais['label']; 134 bandera.src = url_plugin + 'public/img/' + pais + '.svg'; 135 resultado_pais.prepend(bandera); 136 resultado_sms.textContent = sms; 137 resultado_total.textContent = calcular_total(rangos, sms); 138 precio_sms.textContent = calcular_precio_sms(rangos, sms) + " " + objeto_pais['currency']; 139 moneda_euros_deseados.textContent = objeto_pais['currency_text']; 140 document.getElementById('input_euros_deseados').value = calcular_total(rangos, sms); 141 } 142 if(tipo == 'euros'){ 143 input_total_euros = document.getElementById('input_euros_deseados'); 144 resultado_pais.textContent = objeto_pais['label']; 145 bandera.src = url_plugin + 'public/img/' + pais + '.svg'; 146 resultado_pais.prepend(bandera); 147 total_euros = calcular_sms(rangos, input_total_euros.value); 148 resultado_sms.innerHTML = total_euros; 149 document.getElementById('input_pais_destino').value = total_euros; 150 resultado_total.textContent = input_total_euros.value; 151 //precio_sms.textContent = parseFloat(input_total_euros.value / total_euros).toFixed(2) + " " + objeto_pais['currency']; 152 precio_sms.textContent = calcular_precio_sms(rangos, sms) + " " + objeto_pais['currency']; 153 moneda_euros_deseados.textContent = objeto_pais['currency_text']; 154 } 155 } 156 157 158 function calcular_sms(rangos, precio){ 159 let total;
160 Object.values(rangos).forEach(function(rango){ 161 if(parseInt(precio) / rango['value'] >= rango['min'] && parseInt(precio) / rango['value'] <= rango['max']){ 162 total = precio / rango['value']; 163 console.log('Precio total: '+precio); 164 console.log('Precio unitario: '+rango['value']); 165 console.log('Total SMS calculados: '+total); 166 } 167 }); 168 return Math.round(total); 169 } 170 171 172 // function calcular_sms(rangos, precio){ 173 // let total; 174 // Object.values(rangos).forEach(function(rango){ 175 // if(rango['value'] * parseInt(precio) >= rango['min'] && rango['value'] * parseInt(precio) <= rango['max']){ 176 // total = precio / rango['value']; 177 // console.log('Precio total: '+precio); 178 // console.log('Precio unitario: '+rango['value']); 179 // console.log('Total SMS calculados: '+total); 180 // } 181 // }); 182 // return Math.round(total); 183 // } 184 185 186 /** 187 * Calcula el total del presupuesto 188 */ 189 function calcular_total(rangos, cantidad){ 190 let total;
191 Object.values(rangos).forEach(function(rango){ 192 if(cantidad >= rango['min'] && cantidad <= rango['max'] ){ 193 total = parseFloat(rango['value']) * parseInt(cantidad); 194 console.log('Cantidad SMS: '+cantidad); 195 console.log('Precio unitario: '+rango['value']); 196 console.log('Total Precio: '+total); 197 } 198 }); 199 200 return total.toFixed(2); 201 } 202 203 204 /** 205 * Saca el precio del SMS dada una cantidad 206 */ 207 function calcular_precio_sms(rangos, cantidad){ 208 let total; 209 Object.values(rangos).forEach(function(rango){ 210 if(cantidad >= rango['min'] && cantidad <= rango['max'] ){ 211 total = rango['value']; 212 } 213 }); 214 215 return total; 216 } 217 218 219 220});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.