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.
 
 
 
 
 
 

145 lines
4.4 KiB

<?php
/**
* Use an HTML form to create a new entry in the
* users table.
*
*/
require "../../config.php";
require "../../common.php";
/**
* Esto es para obtener los vehículos dados de alta
*/
$conexion = new PDO($dsn, $username, $password, $options);
$smt = $conexion->prepare('SELECT matricula FROM vehiculos');
$smt->execute();
$vehiculos = $smt->fetchAll();
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 = intval($_POST['importe']);
$cantidad = intval($_POST['litros']);
if(empty($_POST['dtoing'])) {
$dto = 0;
} else {
$dto = ($total*0.03);
}
//$dto = intval($_POST['descuento']);
$totalcondto = $total-$dto;
$ellitro = ($totalcondto / $cantidad);
$nuevo_repostaje = array(
"fecha" => $_POST['fecha'],
"vehiculo" => $_POST['vehiculo'],
"kms" => $_POST['kms'],
"litros" => str_replace(',', '.',$_POST['litros']),
//"descuento" => str_replace(',', '.',$_POST['descuento']),
"descuento" => $dto,
// "precioxlitro" => str_replace(',', '.',$_POST['precioxlitro']),
"precioxlitro" => str_replace(',', '.',strval($ellitro)),
//"importe" => str_replace(',', '.',$_POST['importe']),
"importe" => $totalcondto,
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"carburante",
implode(", ", array_keys($nuevo_repostaje)),
":" . implode(", :", array_keys($nuevo_repostaje))
);
$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 id="five" class="main style1">
<div class="container">
<section>
<h2>Añadir repostaje</h2>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<label for="fecha">Fecha</label>
<input type="date" name="fecha" id="fecha">
<label for="vehiculo">Vehículo</label>
<select name="vehiculo" id="vehiculo">
<?php foreach ($vehiculos as $row) { ?>
<option><?=$row["matricula"]?></option>
<?php } ?>
</select>
<label for="kms">Kilómetros</label>
<input type="text" name="kms" id="kms">
<label for="litros">Litros</label>
<input type="text" name="litros" id="litros">
<!-- <label for="descuento">Descuento</label>
<input type="text" name="descuento" id="descuento"> -->
<!--
<label for="precioxlitro">Precio por litro</label>
<input type="text" name="precioxlitro" id="precioxlitro">
-->
<label for="importe">Importe</label>
<input type="text" name="importe" id="importe">
<div class="col-6 col-12-small">
<input type="checkbox" id="dtoing" name="dtoing">
<label for="dtoing">Descuento ING</label>
</div>
<br>
<input type="submit" name="submit" value="Crear" class="primary">
</form>
<a href="menu.php" class="button small">Volver</a>
</section>
</div>
</section>
<?php require "../templates/footer.php"; ?>