TicketMerc/Interfaz/verificar_e_insertar_asient...

54 lines
1.7 KiB
PHP

<?php
require 'conexionbd.php';
$data = json_decode(file_get_contents('php://input'), true);
$artista = $data['artista'] ?? '';
$dias = $data['dias'] ?? [];
$filas = $data['filas'] ?? 0;
$columnas = $data['columnas'] ?? 0;
if (empty($artista) || empty($dias) || $filas <= 0 || $columnas <= 0) {
die(json_encode(['error' => 'Faltan parámetros (artista, dias, filas, columnas).']));
}
try {
$stmt = $conn->prepare("SELECT COUNT(*) AS total FROM asientos WHERE artista = :artista AND dia IN (" . implode(',', $dias) . ")");
$stmt->execute([':artista' => $artista]);
$resultado = $stmt->fetch(PDO::FETCH_ASSOC);
if ($resultado['total'] > 0) {
echo json_encode(['message' => 'Los asientos ya existen en la base de datos.']);
exit;
}
$stmt = $conn->prepare("INSERT INTO asientos (artista, dia, asiento, estado, precio) VALUES (:artista, :dia, :asiento, 'disponible', :precio)");
foreach ($dias as $dia) {
for ($i = 1; $i <= $filas; $i++) {
for ($j = 0; $j < $columnas; $j++) {
$asiento = $i . chr(65 + $j);
$precio = obtenerPrecio($i);
$stmt->execute([
':artista' => $artista,
':dia' => $dia,
':asiento' => $asiento,
':precio' => $precio
]);
}
}
}
echo json_encode(['message' => 'Asientos insertados correctamente.']);
} catch (PDOException $e) {
die(json_encode(['error' => 'Error al insertar los asientos: ' . $e->getMessage()]));
}
function obtenerPrecio($fila) {
if ($fila >= 1 && $fila <= 3) return 4500;
if ($fila >= 4 && $fila <= 7) return 2600;
if ($fila >= 8 && $fila <= 10) return 1300;
return 0;
}
?>