55 lines
2.0 KiB
PHP
55 lines
2.0 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 {
|
|
// Verificar si ya existen asientos para el artista y los días especificados
|
|
$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;
|
|
}
|
|
|
|
// Insertar los asientos para cada día
|
|
$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); // Ej: 1A, 1B, ..., 10L
|
|
$precio = obtenerPrecio($i); // Función para calcular el precio según la fila
|
|
$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()]));
|
|
}
|
|
|
|
// Función para calcular el precio según la fila
|
|
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;
|
|
}
|
|
?>
|