Aniadido informes

This commit is contained in:
2020-01-27 12:19:42 +01:00
parent 7bf844a542
commit fdd1710592
54 changed files with 972 additions and 5 deletions

View File

@@ -22,6 +22,7 @@ public class BarraOpciones extends JMenuBar{
JMenuItem gestion;
JMenuItem modificacion;
JMenuItem menuInterfaz;
JMenuItem menuInforme;
JMenu administracion;
JMenuItem aniadirUsuario;
@@ -59,6 +60,8 @@ public class BarraOpciones extends JMenuBar{
this.vistas.add(this.modificacion);
this.menuInterfaz = new JMenuItem("Gestion tema");
this.vistas.add(this.menuInterfaz);
this.menuInforme = new JMenuItem("Crear Informes");
this.vistas.add(this.menuInforme);
this.administracion = new JMenu("Administracion");
this.add(this.administracion);
this.aniadirUsuario = new JMenuItem("Aniadir usuario");

View File

@@ -94,6 +94,10 @@ public class ControladorBarra implements ActionListener{
this.menu.cambiarVista(this.menu.VISTA_INTERFAZ);
break;
}
case "Informes":{
this.menu.cambiarVista(this.menu.VISTA_INFORMES);
break;
}
case "Aniadir usuario":{
Vista loggin = new Vista(Vista.Guardar);
break;
@@ -129,6 +133,8 @@ public class ControladorBarra implements ActionListener{
this.barra.modificacion.setActionCommand("Cambiar Modificar");
this.barra.menuInterfaz.addActionListener(this);
this.barra.menuInterfaz.setActionCommand("Ajustar tema");
this.barra.menuInforme.addActionListener(this);
this.barra.menuInforme.setActionCommand("Informes");
this.barra.aniadirUsuario.addActionListener(this);
this.barra.aniadirUsuario.setActionCommand("Aniadir usuario");
}

View File

@@ -0,0 +1,115 @@
package VistaControlador;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
import net.sf.jasperreports.view.JasperViewer;
public class ControladorInforme implements ActionListener{
VistaInformes vista;
ModeloInformes modelo;
public ControladorInforme(VistaInformes vista, ModeloInformes modelo) {
this.vista = vista;
this.modelo = modelo;
for(String anios:this.modelo.anios()) {
this.vista.opciones1_2.addItem(anios);
}
this.iniciarControlador();
}
private void iniciarControlador() {
this.vista.exportarInforme1.addActionListener(this);
this.vista.exportarInforme1.setActionCommand("exportar informe 1");
this.vista.mostrarInforme1.addActionListener(this);
this.vista.mostrarInforme1.setActionCommand("mostrar informe 1");
this.vista.exportarInforme2.addActionListener(this);
this.vista.exportarInforme2.setActionCommand("exportar informe 2");
this.vista.mostrarInforme2.addActionListener(this);
this.vista.mostrarInforme2.setActionCommand("mostrar informe 2");
}
@Override
public void actionPerformed(ActionEvent ev) {
switch(ev.getActionCommand()) {
case "mostrar informe 1":{
JasperPrint jasperPrint=null;
try {
Map<String, Object> params = new HashMap<String, Object>();
params.put("Order", this.vista.opciones1_1.getSelectedItem());
params.put("Year", this.vista.opciones1_2.getSelectedItem());
jasperPrint=JasperFillManager.fillReport("Cherry.jasper",params,modelo.controlador);
JasperViewer jasperViewver=new JasperViewer(jasperPrint,false);
jasperViewver.setVisible(true);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
case "exportar informe 1":{
JasperPrint jasperPrint=null;
try {
Map<String, Object> params = new HashMap<String, Object>();
params.put("Order", this.vista.opciones1_1.getSelectedItem());
params.put("Year", this.vista.opciones1_2.getSelectedItem());
jasperPrint=JasperFillManager.fillReport("Cherry.jasper",params,modelo.controlador);
JRPdfExporter exp=new JRPdfExporter();
exp.setExporterInput(new SimpleExporterInput(jasperPrint));
exp.setExporterOutput(new SimpleOutputStreamExporterOutput("Informe1.pdf"));
SimplePdfExporterConfiguration conf=new SimplePdfExporterConfiguration();
exp.setConfiguration(conf);
exp.exportReport();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
case "mostrar informe 2":{
JasperPrint jasperPrint=null;
try {
Map<String, Object> params = new HashMap<String, Object>();
params.put("Order",this.vista.opciones2.getSelectedItem());
jasperPrint=JasperFillManager.fillReport("Graficos_gastos.jasper",params,modelo.controlador);
JasperViewer jasperViewver=new JasperViewer(jasperPrint,false);
jasperViewver.setVisible(true);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
case "exportar informe 2":{
JasperPrint jasperPrint=null;
try {
Map<String, Object> params = new HashMap<String, Object>();
params.put("Order",this.vista.opciones2.getSelectedItem());
jasperPrint=JasperFillManager.fillReport("Graficos_gastos.jasper",params,modelo.controlador);
JRPdfExporter exp=new JRPdfExporter();
exp.setExporterInput(new SimpleExporterInput(jasperPrint));
exp.setExporterOutput(new SimpleOutputStreamExporterOutput("Informe2.pdf"));
SimplePdfExporterConfiguration conf=new SimplePdfExporterConfiguration();
exp.setConfiguration(conf);
exp.exportReport();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
}

View File

@@ -31,7 +31,8 @@ import loggin.Vista;
public class Menu extends JFrame{
static final int VISTA_MODIFICAR = 0;
static final int VISTA_GESTION = 1;
static final int VISTA_INTERFAZ =2;
static final int VISTA_INTERFAZ = 2;
static final int VISTA_INFORMES = 3;
JPanel panelCentral;
JTabbedPane pestania;
@@ -39,6 +40,7 @@ public class Menu extends JFrame{
VistaAniadirVisualizar gastos;
VistaPanelGestion panelGestion;
VistaPanelModificar panelModificar;
VistaInformes vista;
VistaPanel panel;
String rutaGuardado;
int tema;
@@ -84,6 +86,7 @@ public class Menu extends JFrame{
this.panelGestion = new VistaPanelGestion(this.meses);
this.panelModificar = new VistaPanelModificar();
this.panelInterfaz = new VistaPanelInterfaz();
this.vista = new VistaInformes();
this.panel = this.panelGestion;
constrain.fill = GridBagConstraints.HORIZONTAL;
@@ -107,6 +110,7 @@ public class Menu extends JFrame{
ControladorPanelGestion controlador3 = new ControladorPanelGestion(this.panelGestion, this);
ControladorPanelModificar controlador4 = new ControladorPanelModificar(this.panelModificar, this);
ControladorPanelInterfaz controlador5 = new ControladorPanelInterfaz(this.panelInterfaz, this);
ControladorInforme controlador6 = new ControladorInforme(this.vista, new ModeloInformes());
this.listenerPestania();
this.elegirMes = new DatePicker();
@@ -379,6 +383,9 @@ public class Menu extends JFrame{
this.panel = this.panelInterfaz;
break;
}
case Menu.VISTA_INFORMES:{
this.panel = this.vista;
}
}
this.panelCentral.add(this.panel,constrain);
this.panel.repaint();

View File

@@ -0,0 +1,38 @@
package VistaControlador;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
public class ModeloInformes {
Connection controlador;
ModeloInformes(){
String url="jdbc:mariadb://localhost:3306/informes?user=root&password=1234";
try {
this.controlador=DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
}
Vector<String> anios() {
Vector<String> ret = new Vector<String>();
String query = "select distinct year(transacciones.fecha) from transacciones;";
try {
PreparedStatement stmt = this.controlador.prepareStatement(query);
ResultSet resultado = stmt.executeQuery();
while(resultado.next()) {
ret.add(resultado.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
}

View File

@@ -0,0 +1,65 @@
package VistaControlador;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class VistaInformes extends VistaPanel{
JButton mostrarInforme1;
JButton exportarInforme1;
JButton mostrarInforme2;
JButton exportarInforme2;
JComboBox<String> opciones1_1;
JComboBox<String> opciones1_2;
JComboBox<String> opciones2;
public VistaInformes() {
super();
this.setLayout(new GridBagLayout());
GridBagConstraints constrain = new GridBagConstraints();
constrain.gridx = 0;
constrain.gridy = 0;
this.mostrarInforme1 = new JButton("Mostrar 1");
this.add(mostrarInforme1,constrain);
constrain.gridx = 0;
constrain.gridy = 1;
this.exportarInforme1 = new JButton("Exportar 1");
this.add(exportarInforme1,constrain);
this.opciones1_1 = new JComboBox<String>();
constrain.gridx = 0;
constrain.gridy = 2;
this.add(opciones1_1, constrain);
this.opciones1_2 = new JComboBox<String>();
constrain.gridx = 0;
constrain.gridy = 3;
this.add(opciones1_2, constrain);
constrain.gridx = 1;
constrain.gridy = 0;
this.mostrarInforme2 = new JButton("Mostrar 2");
this.add(mostrarInforme2,constrain);
constrain.gridx = 1;
constrain.gridy = 1;
this.exportarInforme2 = new JButton("Exportar 2");
this.add(exportarInforme2,constrain);
constrain.gridx = 1;
constrain.gridy = 2;
this.opciones2 = new JComboBox<String>();
this.add(opciones2, constrain);
this.cargarComb();
}
private void cargarComb() {
this.opciones2.addItem("id");
this.opciones2.addItem("usuario");
this.opciones1_1.addItem("fecha");
this.opciones1_1.addItem("usuario");
}
}