204 lines
9.0 KiB
Java
204 lines
9.0 KiB
Java
package org.example.saladeconciertos;
|
|
|
|
import javafx.geometry.Insets;
|
|
import javafx.geometry.Pos;
|
|
import javafx.scene.Scene;
|
|
import javafx.scene.control.Alert;
|
|
import javafx.scene.control.Button;
|
|
import javafx.scene.control.ComboBox;
|
|
import javafx.scene.control.Label;
|
|
import javafx.scene.image.Image;
|
|
import javafx.scene.image.ImageView;
|
|
import javafx.scene.layout.VBox;
|
|
import javafx.stage.Stage;
|
|
import javafx.scene.control.ButtonType;
|
|
|
|
import java.io.InputStream;
|
|
import java.sql.Connection;
|
|
import java.sql.Date;
|
|
import java.sql.DriverManager;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class EscogerEvento {
|
|
// Datos de conexión a la base de datos
|
|
private static final String DB_URL = "jdbc:mysql://sql10.freesqldatabase.com:3306/sql10766655";
|
|
private static final String DB_USER = "sql10766655";
|
|
private static final String DB_PASSWORD = "7BZbRjEkXZ";
|
|
|
|
// Método para mostrar la ventana de selección de eventos
|
|
public void mostrarVentanaSeleccionEvento(Stage mainStage, VentaBoletos ventaBoletos) {
|
|
// Crear y configurar el label para seleccionar un evento
|
|
Label selectEventLabel = new Label("Seleccione un evento:");
|
|
selectEventLabel.getStyleClass().add("label");
|
|
|
|
// Crear y configurar el ComboBox para listar los eventos
|
|
ComboBox<String> eventComboBox = new ComboBox<>();
|
|
|
|
// Crear y configurar el label para mostrar el evento seleccionado
|
|
Label selectedEventLabel = new Label("Evento seleccionado: Ninguno");
|
|
selectedEventLabel.getStyleClass().add("label");
|
|
|
|
// Crear y configurar el botón de confirmación
|
|
Button confirmButton = new Button("Confirmar");
|
|
confirmButton.getStyleClass().add("button");
|
|
|
|
// Crear y configurar el botón de generar reporte
|
|
Button reportButton = new Button("Generar Reporte");
|
|
reportButton.getStyleClass().add("button");
|
|
|
|
// Cargar eventos desde la base de datos
|
|
try (Connection conexion = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
|
|
String query = "SELECT id, nombre FROM conciertos";
|
|
Statement statement = conexion.createStatement();
|
|
ResultSet resultSet = statement.executeQuery(query);
|
|
|
|
// Añadir los eventos al ComboBox
|
|
while (resultSet.next()) {
|
|
int id = resultSet.getInt("id");
|
|
String nombre = resultSet.getString("nombre");
|
|
eventComboBox.getItems().add(id + " - " + nombre);
|
|
}
|
|
} catch (SQLException e) {
|
|
System.out.println("Error al cargar los eventos.");
|
|
e.printStackTrace();
|
|
}
|
|
|
|
// Actualizar el label cuando se selecciona un evento
|
|
eventComboBox.setOnAction(e -> {
|
|
String selectedEvent = eventComboBox.getValue();
|
|
if (selectedEvent != null) {
|
|
selectedEventLabel.setText("Evento seleccionado: " + selectedEvent);
|
|
}
|
|
});
|
|
|
|
// Acción del botón de confirmación
|
|
confirmButton.setOnAction(e -> {
|
|
String selectedEvent = eventComboBox.getValue();
|
|
if (selectedEvent != null) {
|
|
int eventId = Integer.parseInt(selectedEvent.split(" - ")[0]);
|
|
ventaBoletos.setEventId(eventId);
|
|
ventaBoletos.cargarInformacionEvento();
|
|
ventaBoletos.mostrarVentanaVentaBoletos(mainStage);
|
|
} else {
|
|
ventaBoletos.mostrarAlerta("Advertencia", "No se ha seleccionado un evento", "Por favor, seleccione un evento.");
|
|
}
|
|
});
|
|
|
|
// Acción del botón de generar reporte
|
|
reportButton.setOnAction(e -> {
|
|
String selectedEvent = eventComboBox.getValue();
|
|
if (selectedEvent != null) {
|
|
int eventId = Integer.parseInt(selectedEvent.split(" - ")[0]);
|
|
generarReporte(eventId, selectedEvent.split(" - ")[1]);
|
|
} else {
|
|
ventaBoletos.mostrarAlerta("Advertencia", "No se ha seleccionado un evento", "Por favor, seleccione un evento.");
|
|
}
|
|
});
|
|
|
|
// Agregar el logo de la aplicación
|
|
ImageView logo = new ImageView(new Image(getClass().getResourceAsStream("/logo.png")));
|
|
logo.setFitWidth(100);
|
|
logo.setFitHeight(100);
|
|
|
|
// Crear y configurar el layout principal
|
|
VBox layout = new VBox(20, logo, selectEventLabel, eventComboBox, selectedEventLabel, confirmButton, reportButton);
|
|
layout.setAlignment(Pos.CENTER);
|
|
layout.setPadding(new Insets(20));
|
|
|
|
// Crear y configurar la escena
|
|
Scene scene = new Scene(layout, 400, 400);
|
|
scene.getStylesheets().add(getClass().getResource("/styles.css").toExternalForm());
|
|
mainStage.setScene(scene);
|
|
mainStage.show();
|
|
}
|
|
|
|
// Método para convertir IDs de asientos a nombres de asientos
|
|
private String convertirIdAsientoANombre(int seatId) {
|
|
int fila = (seatId - 1) / 5;
|
|
int numero = (seatId - 1) % 5 + 1;
|
|
char filaLetra = (char) ('A' + fila);
|
|
return filaLetra + String.valueOf(numero);
|
|
}
|
|
|
|
// Método para generar el reporte de boletos vendidos
|
|
private void generarReporte(int eventId, String eventName) {
|
|
List<String> seatDetails = new ArrayList<>();
|
|
StringBuilder reportContent = new StringBuilder();
|
|
reportContent.append("Reporte de Boletos Vendidos\n");
|
|
|
|
// Obtener la fecha del evento
|
|
String eventDate = "";
|
|
try (Connection conexion = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
|
|
String eventQuery = "SELECT fecha FROM conciertos WHERE id = ?";
|
|
PreparedStatement eventStatement = conexion.prepareStatement(eventQuery);
|
|
eventStatement.setInt(1, eventId);
|
|
ResultSet eventResultSet = eventStatement.executeQuery();
|
|
if (eventResultSet.next()) {
|
|
eventDate = eventResultSet.getDate("fecha").toString();
|
|
}
|
|
} catch (SQLException e) {
|
|
System.out.println("Error al obtener la fecha del evento.");
|
|
e.printStackTrace();
|
|
}
|
|
|
|
reportContent.append("Evento: ").append(eventName).append(" - Fecha: ").append(eventDate).append("\n\n");
|
|
|
|
try (Connection conexion = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
|
|
String query = "SELECT a.id, a.precio_base, b.fechaVenta FROM asientos a JOIN boleto b ON a.id = b.id_asiento WHERE b.idevento = ? AND b.status = 'vendido'";
|
|
PreparedStatement statement = conexion.prepareStatement(query);
|
|
statement.setInt(1, eventId);
|
|
ResultSet resultSet = statement.executeQuery();
|
|
|
|
while (resultSet.next()) {
|
|
int seatId = resultSet.getInt("id");
|
|
double price = resultSet.getDouble("precio_base");
|
|
Date saleDate = resultSet.getDate("fechaVenta");
|
|
String seatName = convertirIdAsientoANombre(seatId);
|
|
String seatDetail = "Asiento: " + seatName + ", Precio: $" + price + ", Fecha de Venta: " + saleDate;
|
|
seatDetails.add(seatDetail);
|
|
reportContent.append(seatDetail).append("\n");
|
|
}
|
|
} catch (SQLException e) {
|
|
System.out.println("Error al generar el reporte.");
|
|
e.printStackTrace();
|
|
}
|
|
|
|
// Mostrar el reporte en una alerta
|
|
mostrarAlertaConBotonPDF("Reporte de Boletos Vendidos", "Detalles del Reporte", reportContent.toString(), eventName, seatDetails);
|
|
}
|
|
|
|
// Método para mostrar una alerta con un botón para generar PDF
|
|
private void mostrarAlertaConBotonPDF(String titulo, String encabezado, String contenido, String eventName, List<String> seatDetails) {
|
|
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
|
alert.setTitle(titulo);
|
|
alert.setHeaderText(encabezado);
|
|
alert.setContentText(contenido);
|
|
|
|
ButtonType buttonTypePDF = new ButtonType("Generar PDF");
|
|
alert.getButtonTypes().add(buttonTypePDF);
|
|
|
|
alert.showAndWait().ifPresent(response -> {
|
|
if (response == buttonTypePDF) {
|
|
// Generar el PDF del reporte usando la clase PDFGenerator
|
|
InputStream qrImageStream = getClass().getResourceAsStream("/qrcode.png"); // Cambia esto por la ruta de tu imagen QR
|
|
String timestamp = new java.text.SimpleDateFormat("yyyy-MM-dd HH_mm").format(new java.util.Date());
|
|
String pdfFileName = "Reporte_"+eventName.replace(" ", "_")+"_" + timestamp + ".pdf";
|
|
PDFGenerator.generarPDF(eventName, "", "", 0.0, qrImageStream, String.join("\n", seatDetails), pdfFileName);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Método para mostrar una alerta
|
|
private void mostrarAlerta(String titulo, String encabezado, String contenido) {
|
|
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
|
alert.setTitle(titulo);
|
|
alert.setHeaderText(encabezado);
|
|
alert.setContentText(contenido);
|
|
alert.showAndWait();
|
|
}
|
|
} |