Refactorizacion

This commit is contained in:
Marklogo 2024-01-30 01:21:46 +01:00
parent 7eb369c1cd
commit 4cb6127f80
3 changed files with 408 additions and 95 deletions

View File

@ -4,24 +4,89 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
outline: none;
}
body {
font-family: 'Roboto', sans-serif;
padding: 1em;
}
input,
select {
font-size: 1.2em;
border: 1px solid rgb(202, 207, 211);
border-radius: 0.3em;
padding: 0.2em;
color: rgb(17, 17, 17);
}
textarea{
border: 1px solid rgb(202, 207, 211);
border-radius: 0.3em;
padding: 0.2em;
color: rgb(17, 17, 17);
}
label {
font-size: 1.2em;
}
button {
padding: 0.4em;
background-color: #0d6efd;
border: 0;
border-radius: 0.4em;
color: white;
font-size: 1.2em;
}
button:hover {
background-color: #0b5ed7;
}
.ct_col {
display: flex;
flex-direction: column;
width: max-content;
}
.ct_row {
display: flex;
flex-direction: row;
align-items: center;
width: max-content;
gap: 1em;
}
.gap_1 {
gap: 1em;
}
</style>
<script src="./js/main.js" defer></script>
<title>Alquiler de coche</title>
</head>
<body>
<form id="formAlquiler">
<div style="display: flex; flex-direction: column; min-width: max-content; gap: 1em;">
<div style="display: flex; flex-direction: row; gap:1em">
<div style="display: flex;flex-direction: column; width: max-content;">
<div class="ct_col gap_1">
<form id="formAlquiler" class="ct_col gap_1">
<div class="ct_row">
<div class="ct_col">
<label for="fechaInicio">Fecha de inicio</label><input type="date" name="fechaInicio"
id="fechaInicio">
</div>
<div style="display: flex;flex-direction: column; width: max-content;">
<label for="fechaFin">Fecha de fin</label><input type="date" name="fechaFin"
id="fechaFin">
<div class="ct_col">
<label for="fechaFin">Fecha de fin</label><input type="date" name="fechaFin" id="fechaFin">
</div>
</div>
<div style="display: flex; gap: 2px;">
<div class="ct_col">
<label for="tipoVehiculo">Tipo de vehiculo</label>
<select name="tipoVehiculo" id="tipoVehiculo">
<option value='basico' selected>Basico (45€ dia)</option>
@ -29,44 +94,47 @@
<option value='lujo'>Lujo (85 dia)</option>
</select>
</div>
<h2 style="padding: 0; margin: 0;">Elementos Opcionales</h2>
<div>
<h2>Elementos Opcionales</h2>
<div>
<input type="checkbox" name="sillainfantil" id="sillainfantil">
<label for="sillainfantil">Silla infantil (15€ por dia)</label>
</div>
<div>
<input type="checkbox" name="gps" id="gps">
<label for="gps">GPS (5€ por dia)</label>
</div>
</div>
<div>
<h2>Seguro</h2>
<p>*** El seguro a terceros es obligatorio y tiene un coste de 10 €/dia
sin
IVA</p>
<div>
<input type="checkbox" name="segurotr" id="segurotr">
<label for="segurotr">Seguro todo riesgo</label>
</div>
<div>
<input type="checkbox" name="menor30" id="menor30">
<label for="menor30">Menor de 30 años (Recargo del 30% sobre el precio final)</label>
</div>
</div>
<div>
<input type="checkbox" name="sillainfantil" id="sillainfantil">
<label for="sillainfantil">Silla infantil (15€ por dia)</label>
</div>
<div>
<input type="checkbox" name="gps" id="gps">
<label for="gps">GPS (5€ por dia)</label>
</div>
<h2 style="padding: 0; margin: 0;">Seguro</h2>
<p style="padding: 0; margin: 0;">*** El seguro a terceros es obligatorio y tiene un coste de 10 €/dia sin IVA</p>
<div>
<input type="checkbox" name="segurotr" id="segurotr">
<label for="segurotr">Seguro todo riesgo</label>
</div>
<div>
<input type="checkbox" name="menor30" id="menor30">
<label for="menor30">Menor de 30 años (Recargo del 30% sobre el precio final)</label>
</div>
<div>
<label for="total">Total Coste</label><input type="text" name="total" id="total">
</div>
<div>
<button type="button" onclick="calculaAlquiler()">Calcula total</button>
<button type="button" onclick="">Aceptar</button>
<button type="button" onclick="limpiarForm()">Limpiar</button>
</div>
</form>
<div class="ct_row">
<label for="total">Total Coste</label><input type="text" name="total" id="total" readonly>
</div>
</form>
<textarea name="resumen" id="resumen" cols="30" rows="10">
</textarea>
<div class="ct_row">
<button type="button" onclick="calculaAlquiler()">Calcula total</button>
<button type="button" onclick="submitForm()">Enviar Formulario</button>
<button type="button" onclick="limpiarForm()">Limpiar</button>
</div>
<textarea style="width:100%; height:30em;" name="resumen" id="resumen"></textarea>
</div>

View File

@ -0,0 +1,230 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
outline: none;
}
body {
font-family: 'Roboto', sans-serif;
padding: 1em;
}
input,
select {
font-size: 1.2em;
border: 1px solid rgb(202, 207, 211);
border-radius: 0.3em;
padding: 0.2em;
color: rgb(17, 17, 17);
}
textarea{
border: 1px solid rgb(202, 207, 211);
border-radius: 0.3em;
padding: 0.2em;
color: rgb(17, 17, 17);
font-size: 0.8em;
}
label {
font-size: 1em;
font-weight: bold;
}
button {
padding: 0.4em;
background-color: #0d6efd;
border: 0;
border-radius: 0.4em;
color: white;
font-size: 1.2em;
}
button:hover {
background-color: #0b5ed7;
}
.ct_col {
display: flex;
flex-direction: column;
width: max-content
}
.ct_row {
display: flex;
flex-direction: row;
align-items: center;
width: max-content;
gap: 1em;
}
.gap_1 {
gap: 1.2em;
}
</style>
<title>Alquiler de coche</title>
</head>
<body>
<div class="ct_col gap_1">
<h1>Alquiler de vehículo</h1>
<form id="formAlquiler" class="ct_col gap_1">
<div class="ct_row">
<div class="ct_col">
<label for="fechaInicio">Fecha de inicio</label><input type="date" name="fechaInicio"
id="fechaInicio">
</div>
<div class="ct_col">
<label for="fechaFin">Fecha de fin</label><input type="date" name="fechaFin" id="fechaFin">
</div>
</div>
<div class="ct_col">
<label for="tipoVehiculo">Tipo de vehiculo</label>
<select name="tipoVehiculo" id="tipoVehiculo">
<option value='basico' selected>Basico (45€ dia)</option>
<option value='gama_media'>Gama media (65€ dia)</option>
<option value='lujo'>Lujo (85€ dia)</option>
</select>
</div>
<div>
<h2>Elementos Opcionales</h2>
<div>
<input type="checkbox" name="sillainfantil" id="sillainfantil">
<label for="sillainfantil">Silla infantil (15€ por dia)</label>
</div>
<div>
<input type="checkbox" name="gps" id="gps">
<label for="gps">GPS (5€ por dia)</label>
</div>
</div>
<div>
<h2>Seguro</h2>
<p>*** El seguro a terceros es obligatorio y tiene un coste de 10 €/dia sin IVA</p>
<div>
<input type="checkbox" name="segurotr" id="segurotr">
<label for="segurotr">Seguro todo riesgo (15€, 25€, 35€)</label>
</div>
<div>
<input type="checkbox" name="menor30" id="menor30">
<label for="menor30">Menor de 30 años (Recargo del 30% sobre el precio final)</label>
</div>
</div>
</form>
<div class="ct_row">
<label for="total">Total Coste</label><input type="text" name="total" id="total" readonly>
</div>
<div class="ct_row">
<button type="button" onclick="calculaAlquiler()">Calcula total</button>
<button type="button" onclick="submitForm()">Enviar Formulario</button>
<button type="button" onclick="limpiarForm()">Limpiar</button>
</div>
<textarea style="width:100%; height:30em;" name="resumen" id="resumen"></textarea>
</div>
</body>
<script>
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];
function calculaAlquiler() {
if (diaInicio.value === "" || diaFin.value === "") {
alert("Debe introducir una fecha en ambos campos");
return;
}
const fechaInicio = new Date(diaInicio.value);
const fechaFin = new Date(diaFin.value);
if (fechaInicio.getTime() > fechaFin.getTime()) {
alert("La fecha de inicio debe ser anterior a la de fin");
return;
}
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 = parseInt(pvpAlquilerXGama+opcSillaInfantil+opcGps+seguro);
let pvpConIva = parseFloat(pvpSinIva * 1.21);
if (menor30.checked) pvpConIva = pvpConIva * 1.3;
total.value = pvpConIva.toFixed(2);
generaResumen(pvpConIva);
}
function generaResumen(pvpConIva) {
const diasEntrega = difFechasDias(new Date(), diaInicio.value);
const msgDiasEntrega = `Faltan ${diasEntrega} días para tu alquiler\n`;
const msgFechaEntrega = `Tu fecha de entrega del vehiculo es: ${new Date(diaFin.value).toLocaleDateString()}\n`;
const costeAnulacion = parseFloat(pvpConIva * 0.3);
const fechaIni=new Date(diaInicio.value)
const fechaAnulacion = fechaIni.setDate(fechaIni.getDate()-5)
let msgAnulacion = `Puedes anular tu reserva hasta el dia ${new Date(fechaAnulacion).toLocaleDateString()}\n`
msgAnulacion+=`El coste de la anulacion será de ${costeAnulacion.toFixed(2)} \n`
let msgExtras = "Tus extras son los siguientes: \n";
if (sillaInfantil.checked) msgExtras += " - Silla Infantil \n";
if (gps.checked) msgExtras += " - Gps \n";
seguroTr.checked ? (msgExtras += " - Has contratado seguro a todo riesgo \n") : (msgExtras += " - Has contratado seguro basico \n");
if (menor30.checked) msgExtras += " - Tienes recargo por ser menor de 30 años \n";
let mensaje = msgDiasEntrega + msgFechaEntrega + msgAnulacion + msgExtras;
txtResumen.value = mensaje;
}
/**
* Devuelve la diferencia en dias entre fechas
* @param {Date} fechaIni - Fecha inicial
* @param {Date} fechaFin - Fecha final
* @returns number
*/
function difFechasDias(fechaIni, fechaFin) {
let dias = Math.ceil((new Date(fechaFin) - new Date(fechaIni)) / (1000 * 60 * 60 * 24));
return dias;
}
/**
* Devuelve el valor del correspondiente a una opcion seleccionada
* valorXOpcion('d',['a','d','f'],[10,20,5],0) Devolveria 20
* @param {*} opc - Opcion a bucar
* @param {*} opciones - Array de opciones
* @param {*} valores - Array de valores
* @param {*} valorDefecto - Valor defecto en caso de no encontrar la opcion
* @returns
*/
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 submitForm() {
document.getElementById("formAlquiler").submit();
}
function limpiarForm() {
document.getElementById("formAlquiler").reset();
}
</script>
</html>

View File

@ -1,76 +1,91 @@
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;
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);
if (diaInicio.value === "" || diaFin.value === "") {
alert("Debe introducir una fecha en ambos campos");
return;
}
const fechaInicio = new Date(diaInicio.value);
const fechaFin = new Date(diaFin.value);
if (fechaInicio.getTime() > fechaFin.getTime()) {
alert("La fecha de inicio debe ser anterior a la de fin");
return;
}
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)
let pvpConIva = parseFloat(pvpSinIva * IVA);
if (menor30.checked) pvpConIva = pvpConIva * 1.3;
total.value = pvpConIva.toFixed(2);
generaResumen(pvpConIva);
}
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
function generaResumen(pvpConIva) {
const diasEntrega = difFechasDias(new Date(), diaInicio.value);
const msgDiasEntrega = `Faltan ${diasEntrega} días para tu alquiler !\n`;
const msgFechaEntrega = "Tu fecha de entrega del vehiculo es: "+new Date(diaInicio.value).toLocaleString("es-ES", {day: "numeric",month: "numeric",year: "numeric",})+" !\n";
const costeAnulacion = parseFloat(pvpConIva * 1.3).toFixed(2);
const msgAnulacion = diasEntrega >= 5 ? `El coste de la anulación será de ${costeAnulacion} euros !\n` : `No se puede anular al quedar solo ${diasEntrega} dias para la entega !\n`;
let msgExtras = "Tus extras son los siguientes: \n";
if (sillaInfantil.checked) msgExtras += "Silla Infantil \n";
if (gps.checked) msgExtras += "Gps \n";
seguroTr.checked ? (msgExtras += "Has contratado seguro a todo riesgo ! \n") : (msgExtras += "Has contratado segurio basico !\n");
if (menor30.checked) msgExtras += "Tienes recargo por ser menor de 30 años !\n";
let mensaje = msgDiasEntrega + msgFechaEntrega + msgAnulacion + msgExtras;
txtResumen.value = mensaje;
}
// ******************** //
// Funciones de Fechas //
// ******************** //
/**
* Devuelve la diferencia en dias entre fechas
* @param {Date} fechaIni - Fecha inicial
* @param {Date} fechaFin - Fecha final
* @returns number
*/
function difFechasDias(fechaIni, fechaFin) {
let dias = Math.ceil((new Date(fechaFin) - new Date(fechaIni)) / (1000 * 60 * 60 * 24));
let dias = Math.ceil(
(new Date(fechaFin) - new Date(fechaIni)) / (1000 * 60 * 60 * 24)
);
return dias;
}
/**
* Devuelve el valor del correspondiente a una opcion seleccionada
* valorXOpcion('d',['a','d','f'],[10,20,5],0) Devolveria 20
* @param {*} opc - Opcion a bucar
* @param {*} opciones - Array de opciones
* @param {*} valores - Array de valores
* @param {*} valorDefecto - Valor defecto en caso de no encontrar la opcion
* @returns
*/
function valorXopcion(opc,opciones,valores,valorDefecto){
function valorXopcion(opc, opciones, valores, valorDefecto) {
const index = opciones.indexOf(opc);
return index !== -1 ? valores[index] : valorDefecto;
}
function totalXOpcion(checked,valorChecked,valorUnchecked){
function totalXOpcion(checked, valorChecked, valorUnchecked) {
return checked ? valorChecked : valorUnchecked;
}
function limpiarForm(){
function submitForm() {
document.getElementById("formAlquiler").submit();
}
function limpiarForm() {
document.getElementById("formAlquiler").reset();
}
}