Actualizar Interfaz/verificar_e_insertar_asientos.php

This commit is contained in:
maria.ruiz 2025-03-13 06:33:21 +00:00
parent 2da4802310
commit b04da3bead
1 changed files with 53 additions and 55 deletions

View File

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