/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Models.Home;

import Controllers.Traitement.ParametreSystem;
import Models.BonLivraisonClt.BonLivraisonCltProduitList;
import Models.connexion;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;

/**
 * @author Maher
 */
public class HomeDB {
    
    connexion cnx = new connexion();
    
    public Map HomeNbr()
    {
        Map<String, String> map = new HashMap<>();
        
        String Query =  " SELECT 'factureclt' AS 'colHome', COUNT(idfactureclt) AS nbr " +
                        " FROM `factureclt` "+
                        " WHERE (YEAR(date_facture) = DATE_FORMAT(CURDATE(), '%Y')) AND (local_nom LIKE '"+ParametreSystem.NomLocalPC+"') "+
                
                        " UNION "+
                
                        " SELECT 'client' AS 'colHome', COUNT(codeclient) AS nbr  "+
                        " FROM `client` "+
                        " WHERE (YEAR(`date_add`) = DATE_FORMAT(CURDATE(), '%Y')) AND (local_nom LIKE '"+ParametreSystem.NomLocalPC+"') "+
                
                        " UNION "+
                
                        " SELECT 'cheque' AS 'colHome', COUNT(id_chequeclt) AS nbr  "+
                        " FROM `cheque_clt` "+
                        " WHERE (YEAR(`date_creation`) = DATE_FORMAT(CURDATE(), '%Y')) AND (local_nom LIKE '"+ParametreSystem.NomLocalPC+"'); ";
        
        if(cnx.ouverture()){
            try{
                ResultSet Resultat = cnx.excutionquery(Query);
                Resultat.last();
                int nombreLignes = Resultat.getRow();
                if(nombreLignes>0){
                    Resultat.beforeFirst();
                    while (Resultat.next()){
                        String col = Resultat.getString("colHome");
                        String nbr = Resultat.getString("nbr");
                        map.put(col, nbr);
                    }
                }
            }catch(SQLException ex){
                System.err.println("Erreur SQL ! \n"+ex.getMessage());
                System.out.println(Query);
            }
        }
        
        return map;
    }
    
    
    
    public XYChart.Series HomeChiffreAffaireByMois()
    {
        XYChart.Series series1 = new XYChart.Series();
        
        String Query =  " SELECT DATE_FORMAT(date_facture,'%Y-%m') AS dateFact, sum(net_a_payer) chiffre " +
                        " FROM `factureclt` " +
                        " WHERE local_nom LIKE '"+ParametreSystem.NomLocalPC+"' " +
                        " GROUP BY dateFact " +
                        " ORDER BY dateFact DESC " +
                        " LIMIT 0, 6; ";
                
        if(cnx.ouverture()){
            try{
                ResultSet Resultat = cnx.excutionquery(Query);
                Resultat.last();
                int nombreLignes = Resultat.getRow();
                if(nombreLignes>0){
                    Resultat.beforeFirst();
                    while (Resultat.next()){
                        String dateFact = Resultat.getString("dateFact");
                        Float chiffre = Resultat.getFloat("chiffre");
                        series1.getData().add(new XYChart.Data(dateFact, chiffre));
                    }
                }
            }catch(SQLException ex){
                System.err.println("Erreur SQL ! \n"+ex.getMessage());
                System.out.println(Query);
            }
        }
        
        return series1;
    }


    public ObservableList<PieChart.Data> HomeVente(String year)
    {
        ObservableList<PieChart.Data> ObservableListPieChart = FXCollections.observableArrayList();
        
        String Query =  " SELECT local_nom, SUM(`net_a_payer`) AS total  "+
                        " FROM `factureclt` "+
                        " WHERE (YEAR(`date_facture`) = 2022) "+
                        " GROUP BY local_nom; ";
        
        if(cnx.ouverture()){
            try{
                ResultSet Resultat = cnx.excutionquery(Query);
                Resultat.last();
                int nombreLignes = Resultat.getRow();
                if(nombreLignes>0){
                    Resultat.beforeFirst();
                    while (Resultat.next()){
                        String local_nom = Resultat.getString("local_nom");
                        Float total = Resultat.getFloat("total");
                        ObservableListPieChart.add(new PieChart.Data(local_nom, total));
                    }
                }
            }catch(SQLException ex){
                System.err.println("Erreur SQL ! \n"+ex.getMessage());
                System.out.println(Query);
            }
        }
        
        return ObservableListPieChart;
    }    
}
