flujo de pedido y pago
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
session_start();
|
||||
include("db.php");
|
||||
|
||||
if (empty($_SESSION['carrito'])) {
|
||||
header("Location: carrito.php");
|
||||
exit();
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Datos de Entrega - La Pecosa</title>
|
||||
<link rel="stylesheet" href="css/all.min.css">
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body { font-family: 'Segoe UI', sans-serif; background-color: #fffef8; color: #333; padding: 50px; }
|
||||
.box { max-width: 500px; margin: 0 auto; background: white; padding: 30px; border-radius: 20px; box-shadow: 0 10px 30px rgba(0,0,0,0.05); }
|
||||
|
||||
.header-form { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
|
||||
h2 { color: #e02828; font-size: 1.5rem; display: flex; align-items: center; gap: 10px; }
|
||||
|
||||
.btn-volver {
|
||||
color: #888;
|
||||
text-decoration: none;
|
||||
font-size: 1.2rem;
|
||||
transition: 0.2s;
|
||||
padding: 5px 10px;
|
||||
border-radius: 50px;
|
||||
}
|
||||
.btn-volver:hover { color: #e02828; background-color: #fff5f5; }
|
||||
|
||||
.form-group { margin-bottom: 15px; width: 100%; }
|
||||
label { display: block; margin-bottom: 5px; font-weight: 600; }
|
||||
|
||||
input, select { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 8px; font-size: 1rem; box-sizing: border-box; }
|
||||
input[type="file"] { padding: 5px; background: #f9f9f9; }
|
||||
|
||||
.btn-enviar { background: #e02828; color: white; border: none; padding: 12px 20px; border-radius: 50px; cursor: pointer; font-weight: bold; width: 100%; margin-top: 15px; transition: 0.3s; }
|
||||
.btn-enviar:hover { background: #c02020; transform: scale(1.02); }
|
||||
|
||||
#seccion_envio, #seccion_retiro { display: none; width: 100%; }
|
||||
|
||||
.info-pago-box {
|
||||
background-color: #fff5f5;
|
||||
border: 2px dashed #e02828;
|
||||
border-radius: 10px;
|
||||
padding: 15px;
|
||||
text-align: center;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
.info-pago-box h4 { color: #333; font-size: 0.9rem; letter-spacing: 1px; margin-bottom: 5px; }
|
||||
.info-pago-box .alias { color: #e02828; font-size: 1.3rem; font-weight: bold; margin: 5px 0; }
|
||||
.info-pago-box p { font-size: 0.85rem; color: #555; line-height: 1.4; }
|
||||
|
||||
.info-retiro-box {
|
||||
background-color: #f4fbf7;
|
||||
border: 1px solid #2ecc71;
|
||||
border-radius: 10px;
|
||||
padding: 12px;
|
||||
margin-bottom: 15px;
|
||||
font-size: 0.9rem;
|
||||
color: #27ae60;
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
function evaluarTipo(select) {
|
||||
var seccionEnvio = document.getElementById('seccion_envio');
|
||||
var seccionRetiro = document.getElementById('seccion_retiro');
|
||||
var inputDir = document.getElementById('direccion');
|
||||
var inputComprobante = document.getElementById('comprobante');
|
||||
|
||||
if (select.value === 'envio') {
|
||||
seccionEnvio.style.display = 'block';
|
||||
seccionRetiro.style.display = 'none';
|
||||
inputDir.required = true;
|
||||
inputComprobante.required = true;
|
||||
} else if (select.value === 'retiro') {
|
||||
seccionEnvio.style.display = 'none';
|
||||
seccionRetiro.style.display = 'block';
|
||||
inputDir.required = false;
|
||||
inputComprobante.required = false;
|
||||
inputDir.value = '';
|
||||
inputComprobante.value = '';
|
||||
} else {
|
||||
seccionEnvio.style.display = 'none';
|
||||
seccionRetiro.style.display = 'none';
|
||||
inputDir.required = false;
|
||||
inputComprobante.required = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="box">
|
||||
<div class="header-form">
|
||||
<h2><i class="fa-solid fa-truck"></i> Opciones de entrega</h2>
|
||||
<a href="carrito.php" class="btn-volver" title="Volver al carrito">
|
||||
<i class="fa-solid fa-arrow-left"></i> Volver
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<form action="procesar_pago.php" method="POST" enctype="multipart/form-data">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="tipo_entrega">¿Cómo querés recibir tu pedido?</label>
|
||||
<select name="tipo_entrega" id="tipo_entrega" onchange="evaluarTipo(this)" required>
|
||||
<option value="">-- Seleccioná una opción --</option>
|
||||
<option value="retiro">Retiro en el local (México 569)</option>
|
||||
<option value="envio">Envío a domicilio</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="telefono">Teléfono de contacto:</label>
|
||||
<input type="text" name="telefono" id="telefono" placeholder="Ej: 3434567890" required>
|
||||
</div>
|
||||
|
||||
<div id="seccion_retiro">
|
||||
<div class="info-retiro-box">
|
||||
<i class="fa-solid fa-circle-info"></i> <b>Formas de pago en el local:</b><br>
|
||||
Podés abonar al momento de retirar en <b>Efectivo, Débito o Transferencia</b>.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="seccion_envio">
|
||||
<div class="form-group">
|
||||
<label for="direccion">Dirección de entrega (en Paraná):</label>
|
||||
<input type="text" name="direccion" id="direccion" placeholder="Calle, Número y Barrio">
|
||||
</div>
|
||||
|
||||
<div class="info-pago-box">
|
||||
<h4><i class="fa-solid fa-credit-card"></i> PAGO POR TRANSFERENCIA</h4>
|
||||
<div class="alias">Alias: LA.PECOSA.PARANA</div>
|
||||
<p>
|
||||
⚠️ <b>Única forma de pago para envíos.</b><br>
|
||||
Realizá la transferencia del pedido y adjuntá el comprobante abajo.<br>
|
||||
<i>El costo del envío se abona en efectivo directamente al repartidor al recibirlo.</i>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="comprobante"><i class="fa-solid fa-receipt"></i> Adjuntar Comprobante de Transferencia:</label>
|
||||
<input type="file" name="comprobante" id="comprobante" accept="image/*,.pdf">
|
||||
<small style="color: #666; display: block; margin-top: 4px;">Obligatorio para procesar tu envío.</small>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn-enviar">Confirmar y registrar pedido →</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
session_start();
|
||||
include("db.php");
|
||||
|
||||
if (!isset($_SESSION['id_usuario']) || empty($_SESSION['carrito'])) {
|
||||
header("Location: carrito.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
// Recogemos las variables del formulario de entrega
|
||||
$tipo_entrega = $_POST['tipo_entrega'] ?? 'retiro';
|
||||
$telefono = $_POST['telefono'] ?? '';
|
||||
$direccion = ($tipo_entrega === 'envio') ? ($_POST['direccion'] ?? '') : 'Retira en local';
|
||||
|
||||
$nombre_comprobante = null;
|
||||
|
||||
if ($tipo_entrega === 'envio' && isset($_FILES['comprobante']) && $_FILES['comprobante']['error'] === UPLOAD_ERR_OK) {
|
||||
|
||||
$file_tmp = $_FILES['comprobante']['tmp_name'];
|
||||
$file_name = $_FILES['comprobante']['name'];
|
||||
$ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
|
||||
|
||||
// Validamos extensiones permitidas por seguridad
|
||||
$extensiones_permitidas = ['jpg', 'jpeg', 'png', 'pdf'];
|
||||
|
||||
if (in_array($ext, $extensiones_permitidas)) {
|
||||
$nombre_comprobante = "comprobante_" . time() . "_" . uniqid() . "." . $ext;
|
||||
|
||||
$ruta_destino = "admin/comprobantes/" . $nombre_comprobante;
|
||||
|
||||
if (!move_uploaded_file($file_tmp, $ruta_destino)) {
|
||||
escribir_log("[ERROR ARCHIVO] No se pudo mover el comprobante '$file_name' a la carpeta destino.");
|
||||
$nombre_comprobante = null;
|
||||
}
|
||||
} else {
|
||||
escribir_log("[ERROR VALIDACIÓN] Intento de subida de archivo con extensión no válida: .$ext");
|
||||
}
|
||||
}
|
||||
|
||||
$id_usuario = $_SESSION['id_usuario'];
|
||||
$estado = "En Cocina";
|
||||
|
||||
$total_pedido = 0;
|
||||
$ids = array_keys($_SESSION['carrito']);
|
||||
$lista_ids = implode(',', $ids);
|
||||
$query_p = "SELECT * FROM productos WHERE id IN ($lista_ids)";
|
||||
$res_p = $conexion->query($query_p);
|
||||
|
||||
$productos_data = [];
|
||||
while ($row = $res_p->fetch_assoc()) {
|
||||
$row['cantidad'] = $_SESSION['carrito'][$row['id']];
|
||||
$total_pedido += ($row['precio'] * $row['cantidad']);
|
||||
$productos_data[] = $row;
|
||||
}
|
||||
|
||||
$tipo_entrega = $conexion->real_escape_string($tipo_entrega);
|
||||
$direccion = $conexion->real_escape_string($direccion);
|
||||
$telefono = $conexion->real_escape_string($telefono);
|
||||
|
||||
$nombre_comprobante_sql = $nombre_comprobante ? "'".$conexion->real_escape_string($nombre_comprobante)."'" : "NULL";
|
||||
|
||||
$sql_pedido = "INSERT INTO pedidos (id_usuario, total, estado, tipo_entrega, direccion_envio, telefono_contacto, comprobante)
|
||||
VALUES ('$id_usuario', '$total_pedido', '$estado', '$tipo_entrega', '$direccion', '$telefono', $nombre_comprobante_sql)";
|
||||
|
||||
if (mysqli_query($conexion, $sql_pedido)) {
|
||||
$id_recien_creado = mysqli_insert_id($conexion);
|
||||
|
||||
// LOG DE AUDITORÍA
|
||||
$nombre_cliente = $_SESSION['usuario'] ?? 'Usuario ID: ' . $id_usuario;
|
||||
$info_comprobante = $nombre_comprobante ? "Comprobante: $nombre_comprobante" : "Sin comprobante (Retiro)";
|
||||
escribir_log("[PEDIDO] ¡Venta realizada! ID #$id_recien_creado por el cliente '$nombre_cliente' | Total: $$total_pedido | Entrega: $tipo_entrega | $info_comprobante");
|
||||
|
||||
foreach ($productos_data as $p) {
|
||||
$id_prod = $p['id'];
|
||||
$cant = $p['cantidad'];
|
||||
$precio = $p['precio'];
|
||||
$nombre = $p['nombre'];
|
||||
|
||||
// Insertar detalle
|
||||
$sql_det = "INSERT INTO detalle_pedido (pedido_id, producto_id, cantidad, precio_unitario)
|
||||
VALUES ('$id_recien_creado', '$id_prod', '$cant', '$precio')";
|
||||
mysqli_query($conexion, $sql_det);
|
||||
|
||||
// Restar stock físico
|
||||
$sql_restar_stock = "UPDATE productos SET stock = stock - $cant WHERE id = '$id_prod'";
|
||||
mysqli_query($conexion, $sql_restar_stock);
|
||||
|
||||
// Estadísticas
|
||||
$sql_stats = "INSERT INTO estadisticas_productos (nombre_producto, cantidad_vendida)
|
||||
VALUES ('$nombre', '$cant')
|
||||
ON DUPLICATE KEY UPDATE cantidad_vendida = cantidad_vendida + $cant";
|
||||
mysqli_query($conexion, $sql_stats);
|
||||
}
|
||||
|
||||
unset($_SESSION['carrito']);
|
||||
header("Location: index.php?exito=1");
|
||||
} else {
|
||||
// LOG DE ERROR
|
||||
escribir_log("[ERROR SQL] Falló la inserción del pedido para el usuario ID $id_usuario. Error: " . mysqli_error($conexion));
|
||||
echo "Error al procesar el pedido: " . mysqli_error($conexion);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
ob_start();
|
||||
include("db.php");
|
||||
ob_clean();
|
||||
|
||||
$query = "SELECT id, mensaje, fecha_publicacion
|
||||
FROM promociones
|
||||
WHERE fecha_publicacion >= NOW() - INTERVAL 1 DAY
|
||||
ORDER BY id DESC LIMIT 1";
|
||||
|
||||
$resultado = mysqli_query($conexion, $query);
|
||||
|
||||
$promo = null;
|
||||
if ($fila = mysqli_fetch_assoc($resultado)) {
|
||||
$promo = [
|
||||
'id' => (int)$fila['id'],
|
||||
'mensaje' => $fila['mensaje'],
|
||||
'fecha' => $fila['fecha_publicacion']
|
||||
];
|
||||
}
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($promo);
|
||||
exit();
|
||||
?>
|
||||
Reference in New Issue
Block a user