You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

165 lines
5.2 KiB

<?php
/**
* Use an HTML form to create a new entry in the
* users table.
*
*/
require "../config.php";
require "../common.php";
if (isset($_POST['submit'])) {
if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
if (empty($_POST['fecha']) or
empty($_POST['kms']) or
empty($_POST['litros']) or
empty($_POST['importe'])) {
?>
<?php require "../templates/header.php"; ?>
<section id="four" class="main style2 special">
<div class="container">
<header class="major">
<h2>Introducción de repostajes</h2>
</header>
<section>
<p>Por favor, rellena todos los campos.</p>
<ul class="actions special">
<li><a href="create.php" class="button wide primary">Volver</a></li>
</ul>
</section>
</div>
</section>
<?php require "../templates/footer.php"; ?>
<?php
die();
} else {
try {
$connection = new PDO($dsn, $username, $password, $options);
// Calcula el precio por litro
//$total = floatval($_POST['importe']);
$total=floatval(str_replace(',', '.', str_replace('.', '', $_POST['importe'])));
$cantidad = floatval(str_replace(',', '.', str_replace('.', '', $_POST['litros'])));
if(empty($_POST['dtoing'])) {
$dto = 0;
} else {
$dto = ($total*0.03);
}
//$dto = intval($_POST['descuento']);
$totalcondto = $total-$dto;
$ellitro = ($totalcondto / $cantidad);
// Estas líneas son para conocer los kms del último repostaje
$sql = sprintf('SELECT kms FROM carburante WHERE vehiculo = ? order by identificador DESC LIMIT 1');
$smt = $connection->prepare($sql);
$smt->execute([$_POST['vehiculo']]);
$filas = $smt->fetchAll();
foreach ($filas as $row) {
$kmsanteriores=$row["kms"];
}
$kmsrecorridos=$_POST['kms']-$kmsanteriores;
//
// Hasta aquí lo de conocer los kms del último repostaje
$nuevo_repostaje = array(
"fecha" => $_POST['fecha'],
"vehiculo" => $_POST['vehiculo'],
"kms" => $_POST['kms'],
"litros" => str_replace(',', '.',$_POST['litros']),
"descuento" => $dto,
"precioxlitro" => str_replace(',', '.',strval($ellitro)),
"importe" => $totalcondto,
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"carburante",
implode(", ", array_keys($nuevo_repostaje)),
":" . implode(", :", array_keys($nuevo_repostaje))
);
/*
* Logs
*/
$log_msg='CREA REPOSTAJE-> Descuento: '.$dto.'. Total con descuento: '.$totalcondto.'. Total introducido: '.$_POST['importe'].'. Kilometros anteriores: '.$kmsanteriores.'. Kilometros recorridos: '.$kmsrecorridos;
escribe_log($log_msg);
$statement = $connection->prepare($sql);
$statement->execute($nuevo_repostaje);
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
}
?>
<?php require "../templates/header.php"; ?>
<?php if (isset($_POST['submit']) && $statement) : ?>
<blockquote><?php echo escape($_POST['fecha']); ?> successfully added.</blockquote>
<?php endif; ?>
<section class="principal">
<div class="container">
<section>
<h2>Añadir repostaje</h2>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<p><label for="fecha">Fecha</label>
<p><input type="date" name="fecha" id="fecha" required>
<p><label for="vehiculo">Vehículo</label>
<p><select name="vehiculo" id="vehiculo" required>
<option value="" disabled selected>Selecciona un vehículo</option>
<?php
try {
$conexion = new PDO($dsn, $username, $password, $options);
$smt = $conexion->prepare('SELECT matricula FROM vehiculos');
$smt->execute();
$vehiculos = $smt->fetchAll();
foreach ($vehiculos as $row) {
echo "<option value='".$row["matricula"]."'>" . $row["matricula"]."</option>";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
</select>
<p><label for="kms">Kilómetros</label></p>
<p><input type="number" name="kms" id="kms" required></p>
<p><label for="litros">Litros</label></p>
<p><input type="number" name="litros" id="litros" required step="0.01"></p>
<p><label for="importe">Importe</label></p>
<p><input type="number" name="importe" id="importe" required step="0.01"></p>
<p><input type="checkbox" id="dtoing" name="dtoing">
<label for="dtoing">Descuento ING</label>
</p>
<br>
<p><input type="submit" name="submit" value="Crear" class="primary"></p>
</form>
</section>
</div>
</section>
<?php require "../templates/footer.php"; ?>