102 lines
3.6 KiB
PHP
102 lines
3.6 KiB
PHP
<?php
|
|
require_once 'Sala.php';
|
|
require_once 'Boleto.php';
|
|
require_once 'Venta.php';
|
|
|
|
class BaseDatos {
|
|
private $conexion;
|
|
|
|
public function __construct($host, $usuario, $password, $nombreBD) {
|
|
$this->conexion = new mysqli($host, $usuario, $password, $nombreBD);
|
|
|
|
if ($this->conexion->connect_error) {
|
|
die("Error de conexión: " . $this->conexion->connect_error);
|
|
}
|
|
}
|
|
|
|
public function cargarSala($idSala) {
|
|
$sala = null;
|
|
$query = "SELECT id, nombre FROM salas WHERE id = ?";
|
|
$stmt = $this->conexion->prepare($query);
|
|
$stmt->bind_param("i", $idSala);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
if ($fila = $result->fetch_assoc()) {
|
|
$sala = new Sala($fila['id'], $fila['nombre']);
|
|
|
|
// Cargar boletos de la sala
|
|
$queryBoletos = "SELECT id, fila, numero, precio, estado FROM boletos WHERE id_sala = ?";
|
|
$stmtBoletos = $this->conexion->prepare($queryBoletos);
|
|
$stmtBoletos->bind_param("i", $idSala);
|
|
$stmtBoletos->execute();
|
|
$resultBoletos = $stmtBoletos->get_result();
|
|
|
|
$boletos = [];
|
|
while ($filaBoleto = $resultBoletos->fetch_assoc()) {
|
|
$boleto = new Boleto($filaBoleto['id'], $filaBoleto['fila'], $filaBoleto['numero'], $filaBoleto['precio']);
|
|
if ($filaBoleto['estado'] === 'vendido') {
|
|
$boleto->marcarComoVendido();
|
|
}
|
|
$boletos[] = $boleto;
|
|
}
|
|
|
|
// Asignar boletos a la sala
|
|
$sala->setBoletos($boletos);
|
|
}
|
|
|
|
return $sala;
|
|
}
|
|
|
|
public function guardarVenta($venta) {
|
|
// Iniciar transacción
|
|
$this->conexion->begin_transaction();
|
|
|
|
try {
|
|
// Insertar venta
|
|
$query = "INSERT INTO ventas (id, fecha, nombre_cliente, total) VALUES (?, ?, ?, ?)";
|
|
$stmt = $this->conexion->prepare($query);
|
|
$id = $venta->getId();
|
|
$fecha = $venta->getFecha();
|
|
$nombreCliente = $venta->getNombreCliente();
|
|
$total = $venta->getTotal();
|
|
$stmt->bind_param("sssd", $id, $fecha, $nombreCliente, $total);
|
|
$stmt->execute();
|
|
|
|
// Actualizar estado de boletos
|
|
foreach ($venta->getBoletos() as $boleto) {
|
|
$queryBoleto = "UPDATE boletos SET estado = 'vendido' WHERE id = ?";
|
|
$stmtBoleto = $this->conexion->prepare($queryBoleto);
|
|
$idBoleto = $boleto->getId();
|
|
$stmtBoleto->bind_param("i", $idBoleto);
|
|
$stmtBoleto->execute();
|
|
|
|
// Insertar relación venta-boleto
|
|
$queryRelacion = "INSERT INTO venta_boletos (id_venta, id_boleto) VALUES (?, ?)";
|
|
$stmtRelacion = $this->conexion->prepare($queryRelacion);
|
|
$stmtRelacion->bind_param("si", $id, $idBoleto);
|
|
$stmtRelacion->execute();
|
|
}
|
|
|
|
// Confirmar transacción
|
|
$this->conexion->commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
// Revertir transacción en caso de error
|
|
$this->conexion->rollback();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function getConexion() {
|
|
return $this->conexion;
|
|
}
|
|
|
|
public function __destruct() {
|
|
$this->conexion->close();
|
|
}
|
|
|
|
public function cerrarConexion() {
|
|
$this->conexion->close();
|
|
}
|
|
} |