From 2da480231092130d74f89f03b429c753fc28594f Mon Sep 17 00:00:00 2001 From: "maria.ruiz" <zs22016128@estudiantes.uv.mx> Date: Thu, 13 Mar 2025 06:32:39 +0000 Subject: [PATCH] Actualizar Interfaz/control/vender_asientos.php --- Interfaz/{ => control}/vender_asientos.php | 88 ++++++++++------------ 1 file changed, 41 insertions(+), 47 deletions(-) rename Interfaz/{ => control}/vender_asientos.php (82%) diff --git a/Interfaz/vender_asientos.php b/Interfaz/control/vender_asientos.php similarity index 82% rename from Interfaz/vender_asientos.php rename to Interfaz/control/vender_asientos.php index 9999a54..0d404f3 100644 --- a/Interfaz/vender_asientos.php +++ b/Interfaz/control/vender_asientos.php @@ -1,47 +1,41 @@ -<?php -require 'conexionbd.php'; - -$data = json_decode(file_get_contents('php://input'), true); -$artista = $data['artista'] ?? ''; -$dia = $data['dia'] ?? ''; -$asientos = $data['asientos'] ?? []; - -if (empty($artista) || empty($dia) || empty($asientos)) { - die(json_encode(['error' => 'Faltan parámetros (artista, dia, asientos).'])); -} - -try { - // Iniciar una transacción - $conn->beginTransaction(); - - // Registrar la venta y actualizar el estado del asiento - $stmtVenta = $conn->prepare("INSERT INTO ventas (asiento_id, precio) VALUES (:asiento_id, :precio)"); - $stmtAsiento = $conn->prepare("UPDATE asientos SET estado = 'vendido' WHERE id = :id"); - - foreach ($asientos as $asiento) { - // Obtener el ID y precio del asiento - $stmt = $conn->prepare("SELECT id, precio FROM asientos WHERE artista = :artista AND dia = :dia AND asiento = :asiento"); - $stmt->execute([':artista' => $artista, ':dia' => $dia, ':asiento' => $asiento]); - $asientoData = $stmt->fetch(PDO::FETCH_ASSOC); - - if ($asientoData) { - // Registrar la venta - $stmtVenta->execute([ - ':asiento_id' => $asientoData['id'], - ':precio' => $asientoData['precio'] - ]); - - // Marcar el asiento como vendido - $stmtAsiento->execute([':id' => $asientoData['id']]); - } - } - - // Confirmar la transacción - $conn->commit(); - echo json_encode(['success' => true, 'message' => 'Venta registrada correctamente.']); -} catch (PDOException $e) { - // Revertir la transacción en caso de error - $conn->rollBack(); - die(json_encode(['error' => 'Error al registrar la venta: ' . $e->getMessage()])); -} -?> \ No newline at end of file +<?php +require 'conexionbd.php'; + +$data = json_decode(file_get_contents('php://input'), true); +$artista = $data['artista'] ?? ''; +$dia = $data['dia'] ?? ''; +$asientos = $data['asientos'] ?? []; + +if (empty($artista) || empty($dia) || empty($asientos)) { + die(json_encode(['error' => 'Faltan parámetros (artista, dia, asientos).'])); +} + +try { + $conn->beginTransaction(); + + $stmtVenta = $conn->prepare("INSERT INTO ventas (asiento_id, precio) VALUES (:asiento_id, :precio)"); + $stmtAsiento = $conn->prepare("UPDATE asientos SET estado = 'vendido' WHERE id = :id"); + + foreach ($asientos as $asiento) { + $stmt = $conn->prepare("SELECT id, precio FROM asientos WHERE artista = :artista AND dia = :dia AND asiento = :asiento"); + $stmt->execute([':artista' => $artista, ':dia' => $dia, ':asiento' => $asiento]); + $asientoData = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($asientoData) { + $stmtVenta->execute([ + ':asiento_id' => $asientoData['id'], + ':precio' => $asientoData['precio'] + ]); + + $stmtAsiento->execute([':id' => $asientoData['id']]); + } + } + + // Confirmar la transacción + $conn->commit(); + echo json_encode(['success' => true, 'message' => 'Venta registrada correctamente.']); +} catch (PDOException $e) { + $conn->rollBack(); + die(json_encode(['error' => 'Error al registrar la venta: ' . $e->getMessage()])); +} +?>