IFCD0210/Practicas/Practicas JS/DOM Alquiler Coche V2/js/main.js
Marcos Lopez 7eb369c1cd Dia37
2024-01-29 13:56:01 +01:00

76 lines
2.4 KiB
JavaScript

const diaInicio=document.getElementById("fechaInicio");
const diaFin=document.getElementById("fechaFin");
const gamaSeleccionada=document.getElementById("tipoVehiculo");
const sillaInfantil=document.getElementById("sillainfantil");
const gps=document.getElementById("gps");
const seguroTr=document.getElementById("segurotr");
const menor30=document.getElementById("menor30");
const total=document.getElementById("total");
const txtResumen= document.getElementById("resumen");
const GAMAVEHICULOS=['basico','gama_media','lujo'];
const PVPALQUILERXDIA=[45,65,85];
const PVPSEGUROTR=[15,25,35];
const IVA=1.21;
function calculaAlquiler() {
let diasAlquiler=difFechasDias(diaInicio.value,diaFin.value)
let pvpAlquilerXGama = parseInt(valorXopcion(gamaSeleccionada.value,GAMAVEHICULOS,PVPALQUILERXDIA,0)*diasAlquiler)
let opcSillaInfantil = parseInt(totalXOpcion(sillaInfantil.checked,15,0)* diasAlquiler);
let opcGps = parseInt(totalXOpcion(gps.checked,5,0)* diasAlquiler);
let seguro = parseInt(totalXOpcion(seguroTr.checked,valorXopcion(gamaSeleccionada.value,GAMAVEHICULOS,PVPSEGUROTR,0),10)*diasAlquiler);
let pvpSinIva = diasAlquiler+pvpAlquilerXGama+opcSillaInfantil+opcGps+seguro;
let pvpConIva = parseFloat(pvpSinIva*IVA).toFixed(2);
if (menor30.checked){pvpConIva=(pvpConIva*1.30).toFixed(2)}
total.value=pvpConIva
diasEntrega= difFechasDias(new Date(),diaInicio.value)
generaResumen(diasEntrega, diaFin.value)
}
function generaResumen(diasEntrega, fechaEntrega, fechaAnulacion, costeAnulacion, extras){
let mensaje=`Faltan ${diasEntrega} días para tu alquiler\nTu fecha de entrega del vehiculo es: ${fechaEntrega}`
txtResumen.value=mensaje
}
// ******************** //
// Funciones de Fechas //
// ******************** //
/**
* Devuelve la diferencia en dias entre fechas
* @param {Date} fechaIni - Fecha inicial
* @param {Date} fechaFin - Fecha final
*/
function difFechasDias(fechaIni, fechaFin) {
let dias = Math.ceil((new Date(fechaFin) - new Date(fechaIni)) / (1000 * 60 * 60 * 24));
return dias;
}
function valorXopcion(opc,opciones,valores,valorDefecto){
const index = opciones.indexOf(opc);
return index !== -1 ? valores[index] : valorDefecto;
}
function totalXOpcion(checked,valorChecked,valorUnchecked){
return checked ? valorChecked : valorUnchecked;
}
function limpiarForm(){
document.getElementById("formAlquiler").reset();
}