diff --git a/src/persistance/AccesData.java b/src/persistance/AccesData.java index 7716d27..1a640eb 100644 --- a/src/persistance/AccesData.java +++ b/src/persistance/AccesData.java @@ -7,7 +7,12 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; + +import javax.persistence.StoredProcedureQuery; + import metier.FicheFrais; import metier.Region; import metier.Role; @@ -157,6 +162,85 @@ public static Boolean deleteVisiteur(String utilisateurId) { return success; } -} - + public static List getStatsFFMontRegion(String month, int region) { + StoredProcedureQuery query = s.createStoredProcedureQuery("get_stats_ffmont_region"); + query.registerStoredProcedureParameter("mois", String.class, javax.persistence.ParameterMode.IN); + query.registerStoredProcedureParameter("region", Integer.class, javax.persistence.ParameterMode.IN); + query.setParameter("mois", month); + query.setParameter("region", region); + query.execute(); + return query.getResultList(); + } + + public static List getStatsHFMontRegion(String month, int region) { + StoredProcedureQuery query = s.createStoredProcedureQuery("get_stats_hfmont_region"); + query.registerStoredProcedureParameter("mois", String.class, javax.persistence.ParameterMode.IN); + query.registerStoredProcedureParameter("region", Integer.class, javax.persistence.ParameterMode.IN); + query.setParameter("mois", month); + query.setParameter("region", region); + query.execute(); + return query.getResultList(); + } + + public static List getStatsHfRegion(String month, int region) { + StoredProcedureQuery query = s.createStoredProcedureQuery("get_stats_hf_region"); + query.registerStoredProcedureParameter("mois", String.class, javax.persistence.ParameterMode.IN); + query.registerStoredProcedureParameter("region", Integer.class, javax.persistence.ParameterMode.IN); + query.setParameter("mois", month); + query.setParameter("region", region); + query.execute(); + return query.getResultList(); + } + + public static List getCombinedStats(String month, int region) { + List statsFF = getStatsFFMontRegion(month, region); + List statsHF = getStatsHFMontRegion(month, region); + List statsHfRegion = getStatsHfRegion(month, region); + + + + // Combine the results into a single list + List combinedStats = new ArrayList<>(); + for (Object[] ff : statsFF) { + String idUtilisateur = (String) ff[0]; + boolean foundHF = false; + boolean foundHfRegion = false; + double montantFraisHorsForfait = 0; + int nbFraisHorsForfait = 0; + + for (Object[] hf : statsHF) { + if (hf[0].equals(idUtilisateur)) { + if (hf.length > 3) { + montantFraisHorsForfait = ((Number) hf[3]).doubleValue(); + } + foundHF = true; + break; + } + } + for (Object[] hr : statsHfRegion) { + if (hr[0].equals(idUtilisateur)) { + if (hr.length > 3) { + nbFraisHorsForfait = ((Number) hr[3]).intValue(); + } + foundHfRegion = true; + break; + } + } + + Object[] combined = Arrays.copyOf(ff, ff.length + 2); + combined[4] = montantFraisHorsForfait; + combined[5] = nbFraisHorsForfait; + combinedStats.add(combined); + } + + return combinedStats; + } + + public static List retrieveFicheFraisByRegion(int regionId) { + String hql = "SELECT ff FROM FicheFrais ff JOIN Utilisateur u ON ff.id.idVisiteur = u.idUtilisateur WHERE u.region.idRegion = :regionId"; + Query query = s.createQuery(hql, FicheFrais.class); + query.setParameter("regionId", regionId); + return query.list(); + } +} diff --git a/src/vue/Menu.java b/src/vue/Menu.java index 832bbe9..0a7ebe3 100644 --- a/src/vue/Menu.java +++ b/src/vue/Menu.java @@ -31,6 +31,7 @@ private void initialize(Utilisateur util) { JPanel panelBoutons = new JPanel(); JButton btnGestionVisiteurs = new JButton("Gérer les visiteurs"); JButton btnConsulterFicheVisiteurs = new JButton("Consulter les fiches des visiteurs"); + JButton btnConsulterFicheFrais = new JButton("Consulter les FicheFrais"); JButton btnConsulterStats = new JButton("Consulter les statistiques"); btnDeconnexion = new JButton("Déconnexion"); if (util.getRole().getIdRole().equals("s")) { @@ -40,7 +41,10 @@ private void initialize(Utilisateur util) { if (util.getRole().getIdRole().equals("r")) { panelBoutons.add(btnConsulterStats); - btnConsulterStats.addActionListener(e -> new fichefrais()); + panelBoutons.add(btnConsulterFicheFrais); + btnConsulterFicheFrais.addActionListener(e -> new fichefrais()); + btnConsulterStats.addActionListener(e -> new stats()); + } diff --git a/src/vue/fichefrais.java b/src/vue/fichefrais.java index 2226f43..38e2f48 100644 --- a/src/vue/fichefrais.java +++ b/src/vue/fichefrais.java @@ -7,14 +7,16 @@ import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.sql.Connection; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.text.SimpleDateFormat; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; -import javax.swing.JFileChooser; +import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; @@ -31,9 +33,7 @@ import metier.FicheFrais; import metier.Region; -import metier.Utilisateur; import persistance.AccesData; -import javax.swing.JComboBox; public class fichefrais { private JFrame frame; @@ -46,10 +46,13 @@ public class fichefrais { private JMenuBar menuBar; private List regions; private List mois; - - + private SimpleDateFormat dateFormatter; + private JComboBox comboBox1; + private JComboBox comboBox2; + private DefaultTableModel tableModel; public fichefrais() { + dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); // Création de la fenêtre frame = new JFrame("Fenêtre Visiteurs"); frame.setSize(1000, 700); @@ -61,19 +64,15 @@ public fichefrais() { JPanel comboBoxPanel = new JPanel(); comboBoxPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); - regions = AccesData.getLesRegions(); String[] regionNames = regions.stream().map(Region::getLibelleRegion).toArray(String[]::new); - mois = AccesData.getLesMois(); - - - // Création des JComboBox - - JComboBox comboBox1 = new JComboBox<>(regionNames); + List moisList = AccesData.getLesMois(); + String[] moisArray = moisList.toArray(new String[0]); - String[] comboBoxItems2 = {"Item 2-1", "Item 2-2", "Item 2-3"}; - JComboBox comboBox2 = new JComboBox<>(comboBoxItems2); + // Création des JComboBox + comboBox1 = new JComboBox<>(regionNames); + comboBox2 = new JComboBox<>(moisArray); // Ajout des JComboBox au panneau comboBoxPanel.add(comboBox1); @@ -83,20 +82,10 @@ public fichefrais() { frame.getContentPane().add(comboBoxPanel, BorderLayout.NORTH); // Création du modèle de table avec des colonnes - String[] columnNames = {"idVisiteur ", "mois ", "montantValide", "dateModif", "idEtat "}; - DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0); - - List ff = AccesData.getLesFicheFrais(); - for (FicheFrais f : ff) { - Object[] rowData = { - f.getId(), - f.getDateModif(), - f.getEtat(), - f.getMontantValide(), - f.getNbJustificatif(), - }; - tableModel.addRow(rowData); - } + String[] columnNames = {"idVisiteur", "mois", "nbJustificatifs", "montantValide", "dateModif", "idEtat"}; + tableModel = new DefaultTableModel(columnNames, 0); + + updateTableModel(); table = new JTable(tableModel); table.setFillsViewportHeight(true); @@ -133,10 +122,51 @@ public void actionPerformed(ActionEvent e) { } }); + // Ajouter des écouteurs d'événements aux JComboBox + comboBox1.addItemListener(new ItemListener() { + public void itemStateChanged(ItemEvent e) { + if (e.getStateChange() == ItemEvent.SELECTED) { + updateTableModel(); + } + } + }); + + comboBox2.addItemListener(new ItemListener() { + public void itemStateChanged(ItemEvent e) { + if (e.getStateChange() == ItemEvent.SELECTED) { + updateTableModel(); + } + } + }); + // Affichage de la fenêtre frame.setVisible(true); } + private void updateTableModel() { + tableModel.setRowCount(0); + String selectedRegion = (String) comboBox1.getSelectedItem(); + String selectedMois = (String) comboBox2.getSelectedItem(); + Region region = regions.stream().filter(r -> r.getLibelleRegion().equals(selectedRegion)).findFirst().orElse(null); + + if (region != null) { + List ff = AccesData.retrieveFicheFraisByRegion(region.getIdRegion()); + for (FicheFrais f : ff) { + if (selectedMois == null || selectedMois.equals(f.getId().getMois())) { + Object[] rowData = { + f.getId().getIdVisiteur(), + f.getId().getMois(), + f.getNbJustificatif(), + f.getMontantValide(), + dateFormatter.format(f.getDateModif()), + f.getEtat().getLibelleEtat(), + }; + tableModel.addRow(rowData); + } + } + } + } + public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { @@ -148,4 +178,4 @@ public void run() { } }); } -} \ No newline at end of file +} diff --git a/src/vue/stats.java b/src/vue/stats.java index d0241ee..6ebf420 100644 --- a/src/vue/stats.java +++ b/src/vue/stats.java @@ -5,105 +5,60 @@ import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.Font; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.sql.Connection; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; import java.text.SimpleDateFormat; import java.util.List; -import javax.swing.BorderFactory; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.JButton; -import javax.swing.JFileChooser; +import javax.swing.JComboBox; import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JMenu; -import javax.swing.JMenuBar; -import javax.swing.JMenuItem; -import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; -import javax.swing.ListSelectionModel; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; +import javax.swing.JButton; import javax.swing.table.DefaultTableModel; -import metier.FicheFrais; import metier.Region; -import metier.Utilisateur; import persistance.AccesData; -import javax.swing.JComboBox; public class stats { private JFrame frame; private JTable table; - private JPanel buttonPanel; // Panneau pour les boutons - private JButton InsererBDD; + private JPanel buttonPanel; private JButton btnNewButton; - private JLabel TextError; - private JButton SelectXMLButton; - private JMenuBar menuBar; - private List regions; - private List mois; + private JComboBox comboBox1; + private JComboBox comboBox2; private SimpleDateFormat dateFormatter; - - public stats() { - dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); - // Création de la fenêtre - frame = new JFrame("Fenêtre Visiteurs"); + + frame = new JFrame("Statistiques"); frame.setSize(1000, 700); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); - frame.getContentPane().setLayout(new BorderLayout()); + frame.getContentPane().setLayout(new BorderLayout()); - // Création du panneau pour les JComboBox JPanel comboBoxPanel = new JPanel(); comboBoxPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); - - regions = AccesData.getLesRegions(); + List regions = AccesData.getLesRegions(); String[] regionNames = regions.stream().map(Region::getLibelleRegion).toArray(String[]::new); List moisList = AccesData.getLesMois(); String[] moisArray = moisList.toArray(new String[0]); - - - // Création des JComboBox - - JComboBox comboBox1 = new JComboBox<>(regionNames); - - JComboBox comboBox2 = new JComboBox<>(moisArray); + comboBox1 = new JComboBox<>(regionNames); + comboBox2 = new JComboBox<>(moisArray); - // Ajout des JComboBox au panneau comboBoxPanel.add(comboBox1); comboBoxPanel.add(comboBox2); - // Ajout du panneau des JComboBox à la partie nord de la fenêtre frame.getContentPane().add(comboBoxPanel, BorderLayout.NORTH); - // Création du modèle de table avec des colonnes - String[] columnNames = {"idVisiteur ", "Nom ", "Prenom", "MontantFraisHorsForfait", "MontantFraisForfait", "nbFraisHorsForfait "}; + String[] columnNames = {"idUtilisateur", "Nom", "Prenom", "Montant Frais Forfait", "Montant Frais Hors Forfait", "nbFraisHorsForfait"}; DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0); - List ff = AccesData.getLesFicheFrais(); - for (FicheFrais f : ff) { - Object[] rowData = { - f.getId().getIdVisiteur(), - f.getId().getMois(), - f.getNbJustificatif(), - f.getMontantValide(), - dateFormatter.format(f.getDateModif()), - f.getEtat().getLibelleEtat(), - }; - tableModel.addRow(rowData); - } - table = new JTable(tableModel); table.setFillsViewportHeight(true); table.setRowHeight(30); @@ -111,47 +66,71 @@ public stats() { table.setGridColor(Color.LIGHT_GRAY); table.getTableHeader().setReorderingAllowed(false); - table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // Permet une seule ligne sélectionnée à la fois - - // Ajout de la table dans un JScrollPane JScrollPane scrollPane = new JScrollPane(table); frame.getContentPane().add(scrollPane, BorderLayout.CENTER); - // Création du panneau pour les boutons (placé en bas) buttonPanel = new JPanel(); - buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); // FlowLayout pour centrer les boutons - frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH); // Ajouter le panneau des boutons en bas + buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); + frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH); - // Ajout des boutons dans le panneau btnNewButton = new JButton("Retour"); buttonPanel.add(btnNewButton); - // Créer une barre de menu (JMenuBar) - menuBar = new JMenuBar(); - JMenu fileMenu = new JMenu("Menu"); - menuBar.add(fileMenu); - frame.setJMenuBar(menuBar); + btnNewButton.addActionListener(e -> frame.setVisible(false)); - // Action pour le bouton Retour - btnNewButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - frame.setVisible(false); // Masquer la fenêtre actuelle + // Add ItemListeners to the JComboBox + ItemListener itemListener = e -> { + if (e.getStateChange() == ItemEvent.SELECTED) { + updateStats(tableModel); } - }); + }; + + comboBox1.addItemListener(itemListener); + comboBox2.addItemListener(itemListener); + + // Set default selection + if (comboBox1.getItemCount() > 0) { + comboBox1.setSelectedIndex(0); + } + if (comboBox2.getItemCount() > 0) { + comboBox2.setSelectedIndex(0); + } - // Affichage de la fenêtre frame.setVisible(true); + + // Initial update with default selection + updateStats(tableModel); + } + + private void updateStats(DefaultTableModel tableModel) { + String selectedMonth = (String) comboBox2.getSelectedItem(); + int selectedRegion = comboBox1.getSelectedIndex() + 1; // Assuming region IDs are 1-based + + List stats = AccesData.getCombinedStats(selectedMonth, selectedRegion); + tableModel.setRowCount(0); // Clear existing data + + for (Object[] stat : stats) { + if (stat.length >= 6) { + Object[] rowData = { + stat[0], // idUtilisateur + stat[1], // Nom + stat[2], // Prenom + stat[3], // Montant Frais Forfait + stat[4], // Montant Frais Hors Forfait + stat[5] // nbFraisHorsForfait + }; + tableModel.addRow(rowData); + } + } } public static void main(String[] args) { - EventQueue.invokeLater(new Runnable() { - public void run() { - try { - stats window = new stats(); - } catch (Exception e) { - e.printStackTrace(); - } + EventQueue.invokeLater(() -> { + try { + stats window = new stats(); + } catch (Exception e) { + e.printStackTrace(); } }); } -} \ No newline at end of file +}