package Models.TraiteClt;

import Controllers.Dialog.ShowDialog;
import Controllers.Traitement.Adaptateur;
import Controllers.Traitement.ParametreSystem;
import Controllers.Traitement.contro;
import Models.Caisse.CaisseClt;
import Models.Caisse.CaisseDB;
import Models.H2JDBC;
import Models.User.User;
import org.apache.log4j.Logger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 * @author Maher Ben Tili
 */
public class TraiteCltDB {
    
    H2JDBC h2jdbc = new H2JDBC();
    ShowDialog SD = new ShowDialog();
    Logger logger = Logger.getLogger(TraiteCltDB.class.getName());
    contro clt = new contro();
    
    public Map GetChequePaiementTraite(String  IdTraitePaiementCheque)
    {
        Map<String, String> map = new HashMap<>();
        
        String req  = " SELECT t_p_c_clt.numero, t_p_c_clt.banque, t_clt.id_traite, " +
                        " f_clt.typeclt, " +
                        " c.nom AS nomPer, c.prenom AS prenomPer, " +
                        " e_clt.nomentrepise, e_clt.matricule_fiscale, " +
                        " p_clt.nom AS nomPass, p_clt.prenom AS prenomPass, " +
                        " FROM traite_paiement_cheque_clt t_p_c_clt, traite_clt t_clt, factureclt f_clt " +
                        " LEFT JOIN cltentreprise e_clt ON (f_clt.idcltentreprise = e_clt.idcliententreprise) AND (e_clt.local_nom = '"+ParametreSystem.NomLocalPC+"')" +
                        " LEFT JOIN client c ON (f_clt.idcltexiste = c.codeclient) AND (c.local_nom = '"+ParametreSystem.NomLocalPC+"')" +
                        " LEFT JOIN cltpassageur p_clt ON (f_clt.idcltpassageur = p_clt.idcltpassageur) AND (p_clt.local_nom = '"+ParametreSystem.NomLocalPC+"')" +
                        " WHERE (t_p_c_clt.id_traite_paiement_cheque = "+IdTraitePaiementCheque+")   AND (t_clt.id_traite = t_p_c_clt.id_traite) AND (t_clt.id_factureclt = f_clt.idfactureclt) AND (t_p_c_clt.local_nom = '"+ParametreSystem.NomLocalPC+"');";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                map.put("IdTraite", resultat.getString("id_traite"));
                map.put("numero", resultat.getString("numero"));
                map.put("banque", resultat.getString("banque"));
                if(resultat.getInt("typeclt") == 0){
                    map.put("Nom", resultat.getString("nomPer"));
                    map.put("Prenom", resultat.getString("prenomPer"));
                }else if(resultat.getInt("typeclt") == 1){
                    map.put("Nom", resultat.getString("nomentrepise"));
                    map.put("Prenom", resultat.getString("matricule_fiscale"));
                }else if(resultat.getInt("typeclt") == 2){
                    map.put("Nom", resultat.getString("nomPass"));
                    map.put("Prenom", resultat.getString("prenomPass"));
                }
            }
        } catch(SQLException ex){
            System.err.println("GetChequePaiementTraite Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDBGetChequePaiementTraite  : GetChequePaiementTraite :" + ex.getMessage());
            }
                if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        
        
        return map;
    }
    
    public Map GetCartePaiementTraite(String  id_traite_paiement_carte)
    {
        Map<String, String> map = new HashMap<>();
        
        String req  = " SELECT t_p_c_clt.numero_carte, t_p_c_clt.banque, t_clt.id_traite," +
                        " f_clt.typeclt, " +
                        " c.nom AS nomPer, c.prenom AS prenomPer, " +
                        " e_clt.nomentrepise, e_clt.matricule_fiscale, " +
                        " p_clt.nom AS nomPass, p_clt.prenom AS prenomPass, " +
                        " FROM traite_paiement_carte_clt t_p_c_clt, traite_clt t_clt, factureclt f_clt " +
                        " LEFT JOIN cltentreprise e_clt ON (f_clt.idcltentreprise = e_clt.idcliententreprise) AND (e_clt.local_nom = '"+ParametreSystem.NomLocalPC+"')" +
                        " LEFT JOIN client c ON (f_clt.idcltexiste = c.codeclient) AND (c.local_nom = '"+ParametreSystem.NomLocalPC+"')" +
                        " LEFT JOIN cltpassageur p_clt ON (f_clt.idcltpassageur = p_clt.idcltpassageur) AND (p_clt.local_nom = '"+ParametreSystem.NomLocalPC+"')" +
                        " WHERE (t_p_c_clt.id_traite_paiement_carte = "+id_traite_paiement_carte+")  AND (t_clt.id_traite = t_p_c_clt.id_traite) AND (t_clt.id_factureclt = f_clt.idfactureclt) AND (t_p_c_clt.local_nom = '"+ParametreSystem.NomLocalPC+"');";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                map.put("IdTraite", resultat.getString("id_traite"));
                map.put("numero_carte", resultat.getString("numero_carte"));
                map.put("banque", resultat.getString("banque"));
                if(resultat.getInt("typeclt") == 0){
                    map.put("Nom", resultat.getString("nomPer"));
                    map.put("Prenom", resultat.getString("prenomPer"));
                }else if(resultat.getInt("typeclt") == 1){
                    map.put("Nom", resultat.getString("nomentrepise"));
                    map.put("Prenom", resultat.getString("matricule_fiscale"));
                }else if(resultat.getInt("typeclt") == 2){
                    map.put("Nom", resultat.getString("nomPass"));
                    map.put("Prenom", resultat.getString("prenomPass"));
                }
            }
        } catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDB  : GetCartePaiementTraite :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return map;
    }
    
    public Map GetEspacePaiementTraite(String  id_traite_paiement_espace)
    {
        Map<String, String> map = new HashMap<>();
        
        String req  = " SELECT " +
                        " t_clt.id_traite, " +  
                        " f_clt.typeclt, " +
                        " c.nom AS nomPer, c.prenom AS prenomPer, " +
                        " e_clt.nomentrepise, e_clt.matricule_fiscale, " +
                        " p_clt.nom AS nomPass, p_clt.prenom AS prenomPass, " +
                        " FROM traite_paiement_espace_clt t_p_e_clt, traite_clt t_clt, factureclt f_clt " +
                        " LEFT JOIN cltentreprise e_clt ON (f_clt.idcltentreprise = e_clt.idcliententreprise) AND (e_clt.local_nom = '"+ParametreSystem.NomLocalPC+"')" +
                        " LEFT JOIN client c ON (f_clt.idcltexiste = c.codeclient) AND (c.local_nom = '"+ParametreSystem.NomLocalPC+"')" +
                        " LEFT JOIN cltpassageur p_clt ON (f_clt.idcltpassageur = p_clt.idcltpassageur) AND (p_clt.local_nom = '"+ParametreSystem.NomLocalPC+"')" +
                        " WHERE (t_p_e_clt.id_traite_paiement_espace = "+id_traite_paiement_espace+")  AND (t_clt.id_traite = t_p_e_clt.id_traite) AND (t_clt.id_factureclt = f_clt.idfactureclt) AND (t_p_e_clt.local_nom = '"+ParametreSystem.NomLocalPC+"');";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                map.put("IdTraite", resultat.getString("id_traite"));
                if(resultat.getInt("typeclt") == 0){
                    map.put("Nom", resultat.getString("nomPer"));
                    map.put("Prenom", resultat.getString("prenomPer"));
                }else if(resultat.getInt("typeclt") == 1){
                    map.put("Nom", resultat.getString("nomentrepise"));
                    map.put("Prenom", resultat.getString("matricule_fiscale"));
                }else if(resultat.getInt("typeclt") == 2){
                    map.put("Nom", resultat.getString("nomPass"));
                    map.put("Prenom", resultat.getString("prenomPass"));
                }
            }
        } catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDB  : GetEspacePaiementTraite :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("GetEspacePaiementTraite Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return map;
    }
    
    public void AddListeTraiteClt(ArrayList<TraiteClt> ListTraites, String CodeFactureClt)
    {
        String ReqTraiClt = "INSERT INTO traite_clt (date_traite,  montant_traite, date_creation, id_factureclt, local_nom, id_profile) VALUES ";    
        String Req = "";  
        
        for(int i=0; i<ListTraites.size(); i++){
           String montant_traite = ListTraites.get(i).getMontantTraiteClt().replace(",",".");
                String date_traite =  Adaptateur.DatabaseDateFormat(ListTraites.get(i).getDateTraiteClt());
                String code_facture = ListTraites.get(i).getCodeFactureClt();
            if(code_facture == null){
                code_facture = CodeFactureClt;
            }
            Req = Req + "( '"+date_traite+"',  "+montant_traite+", now(), "+code_facture+", '"+ParametreSystem.NomLocalPC+"', "+User.idprofile+"),";
        }
        if(Req != null){
            ReqTraiClt = ReqTraiClt + Req.substring(0, Req.length()-1) + ";";
            int nbr = 0;
            try {
                nbr = h2jdbc.InsertUpdateDeleteInTable(ReqTraiClt);
            }catch(SQLException ex){
                System.err.println("Erreur SQL ! \n"+ex.getMessage());
                System.out.println(ReqTraiClt);
                if(ParametreSystem.ErrorShowLogger)
                {
                    logger.error("TraiteCltDB  : AddListeTraiteClt :" + ex.getMessage());
                }
                if(ParametreSystem.ErrorShowDialog)
                {
                    ShowDialog.setTitre("Erreur SQL !");
                    ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                    SD.ShowAletDialog();
                }
            }
        }
    }
    
    public String SetPayerTraiteEnEspaceDB(String IdTraite, Float NewMontantPayer, Float OldMontantPayer)
    {
        String QueryTraitePayementEspace = "INSERT INTO traite_paiement_espace_clt (id_traite, montant_payer, local_nom, id_profile) VALUES ('"+IdTraite+"', '"+NewMontantPayer+"', '"+ParametreSystem.NomLocalPC+"', "+User.idprofile+")";
        String IdTraitePayement = null;
        try {
            IdTraitePayement = h2jdbc.InsertGetLastId(QueryTraitePayementEspace);
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(QueryTraitePayementEspace);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDB  : SetPayerTraiteEnEspaceDB :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        CaisseClt Caisse = new CaisseClt();
        Caisse.setIdTraitePaiementEspace(IdTraitePayement);
        Caisse.setTypeMouvement("3");
        Caisse.setMontant(NewMontantPayer.toString());
        new CaisseDB().AddCaisse(Caisse);
        
        Float TotalMontantPayer = NewMontantPayer + OldMontantPayer;
        String QueryTraite = "UPDATE traite_clt SET date_paiement = now(), montant_payer = '"+TotalMontantPayer+"' WHERE (id_traite = '"+IdTraite+"') AND (local_nom LIKE '"+ParametreSystem.NomLocalPC+"') ";
        try {
            h2jdbc.InsertUpdateDeleteInTable(QueryTraite);
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(QueryTraite);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDB  : SetPayerTraiteEnEspaceDB :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return IdTraitePayement;
    }
    
    
    public String SetPayerTraiteEnChequeDB(String IdTraite, Float NewMontantPayer, Float OldMontantPayer, String Banque, String NumerCheque, String DateCheque)
    {
        String QueryTraitePayementCheque = "INSERT INTO traite_paiement_cheque_clt (id_traite, montant_payer, numero,  banque, local_nom, id_profile, date_payer) VALUES ('"+IdTraite+"', '"+NewMontantPayer+"', '"+NumerCheque+"', '"+Banque+"',  '"+ParametreSystem.NomLocalPC+"', "+User.idprofile+",  '"+DateCheque+"')";
        String IdTraitePayement = null;
        try {
            IdTraitePayement = h2jdbc.InsertGetLastId(QueryTraitePayementCheque);
        }catch(SQLException ex){
            System.err.println("SetPayerTraiteEnChequeDB Erreur SQL ! \n"+ex.getMessage());
            System.out.println(QueryTraitePayementCheque);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDB  : SetPayerTraiteEnChequeDB :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        CaisseClt Caisse = new CaisseClt();
        Caisse.setIdTraitePaiementCheque(IdTraitePayement);
        Caisse.setTypeMouvement("3");
        Caisse.setMontant(NewMontantPayer.toString());
        new CaisseDB().AddCaisse(Caisse);
        
        Float TotalMontantPayer = NewMontantPayer + OldMontantPayer;
        String QueryTraite = "UPDATE traite_clt SET date_paiement = now(), montant_payer = '"+TotalMontantPayer+"' WHERE (id_traite = '"+IdTraite+"') AND (local_nom LIKE '"+ParametreSystem.NomLocalPC+"') ";
        int nbr = 0;
        try {
            nbr = h2jdbc.InsertUpdateDeleteInTable(QueryTraite);
        }catch(SQLException ex){
            System.err.println("SetPayerTraiteEnChequeDB Erreur SQL ! \n"+ex.getMessage());
            System.out.println(QueryTraite);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDB  : SetPayerTraiteEnChequeDB :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return IdTraitePayement;
    }
    
    public String SetPayerTraiteEnCarteDB(String IdTraite, Float NewMontantPayer, Float OldMontantPayer, String Banque, String NumerCarte)
    {
        String QueryTraitePayementCarte = "INSERT INTO traite_paiement_carte_clt (id_traite, montant_payer, numero_carte,  banque, local_nom, id_profile) VALUES ('"+IdTraite+"', '"+NewMontantPayer+"', '"+NumerCarte+"', '"+Banque+"',  '"+ParametreSystem.NomLocalPC+"', "+User.idprofile+")";
        String IdTraitePayement = null;
        try {
            IdTraitePayement = h2jdbc.InsertGetLastId(QueryTraitePayementCarte);
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(QueryTraitePayementCarte);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDB  : SetPayerTraiteEnCarteDB :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("SetPayerTraiteEnCarteDB Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        CaisseClt Caisse = new CaisseClt();
        Caisse.setIdTraitePaiementCarte(IdTraitePayement);
        Caisse.setTypeMouvement("3");
        Caisse.setMontant(NewMontantPayer.toString());
        new CaisseDB().AddCaisse(Caisse);
        
        Float TotalMontantPayer = NewMontantPayer + OldMontantPayer;
        String QueryTraite = "UPDATE traite_clt SET date_paiement = now(), montant_payer = '"+TotalMontantPayer+"' WHERE (id_traite = '"+IdTraite+"') AND (local_nom LIKE '"+ParametreSystem.NomLocalPC+"') ";
        int nbr = 0;
        try {
            nbr = h2jdbc.InsertUpdateDeleteInTable(QueryTraite);
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(QueryTraite);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDB  : SetPayerTraiteEnCarteDB :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("SetPayerTraiteEnCarteDB Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return IdTraitePayement;
    }
    
    public ObservableList<TraiteCltPaiementListe> getListPaiementTraiteClt(String IdTraite)
    {
        ObservableList<TraiteCltPaiementListe> DataPaiementList = FXCollections.observableArrayList();
        
        String QueryEspace = "SELECT id_traite_paiement_espace AS id_traite_paiement, 'espace' AS type, '' AS banque, '' AS numero, to_char(date_payer, 'dd/mm/yyyy') AS date_paiement, to_char(date_payer, 'hh:ii:ss') AS heur_paiement, montant_payer FROM traite_paiement_espace_clt WHERE (id_traite = '"+IdTraite+"') AND (local_nom = '"+ParametreSystem.NomLocalPC+"')" +
                            " UNION ALL " +
                            "SELECT id_traite_paiement_cheque AS id_traite_paiement, 'chèque' AS type, banque, numero, to_char(date_payer, 'dd/mm/yyyy') AS date_paiement, to_char(date_payer, 'hh:ii:ss') AS heur_paiement, montant_payer FROM traite_paiement_cheque_clt WHERE (id_traite = '"+IdTraite+"') AND (local_nom = '"+ParametreSystem.NomLocalPC+"')" +
                            " UNION ALL " +
                            "SELECT id_traite_paiement_carte AS id_traite_paiement, 'carte bancaire' AS type, banque, numero_carte AS numero, to_char(date_payer, 'dd/mm/yyyy') AS date_paiement, to_char(date_payer, 'hh:ii:ss') AS heur_paiement, montant_payer FROM traite_paiement_carte_clt WHERE (id_traite = '"+IdTraite+"') AND (local_nom = '"+ParametreSystem.NomLocalPC+"');";
        try {
            ResultSet Resultat = h2jdbc.selectFromDB(QueryEspace);
            while (Resultat.next()){
                String id_paiement = Resultat.getString("id_traite_paiement");
                String montant_paiement = Resultat.getString("montant_payer");
                String date_paiement = Resultat.getString("date_paiement");
                String heur_paiement = Resultat.getString("heur_paiement");
                String type = Resultat.getString("type");
                String banque = Resultat.getString("banque");
                String numero = Resultat.getString("numero");
                DataPaiementList.add(new TraiteCltPaiementListe( id_paiement, type,  montant_paiement,  date_paiement, heur_paiement, banque, numero));
            }
        } catch(SQLException ex){
            System.err.println("getListPaiementTraiteClt Erreur SQL ! \n"+ex.getMessage());
            System.out.println(QueryEspace);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDB  : getListPaiementTraiteClt :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return DataPaiementList;
    }
    
    public ObservableList SearchTraiteCltRetard(Integer debut, Integer nbr, String DateDebut, String IdFacture, String NomLocal, String CodeClt, String CINClt, String NomClt, boolean All)
    {
        String WhereReq1 = " WHERE (T.date_paiement IS NULL) AND (T.date_traite < now()) AND (T.id_factureclt = F.idfactureclt) AND ";
        
        if(!clt.isStringNull(NomLocal)){
            WhereReq1 += " (T.local_nom = '"+NomLocal+"') AND ";
        }
        
        if(!clt.isNumericNotnull(IdFacture)){
            WhereReq1 += " (T.id_factureclt = '"+IdFacture+"') AND ";
        }
        
        if(!clt.isStringNull(DateDebut)){
            WhereReq1 += " (T.date_traite >= '"+DateDebut+"') AND ";
        }
        
        if(!clt.isNumericNotnull(CodeClt)){
            WhereReq1 += " ( (C.codeclient = '"+CodeClt+"') OR (E.idcliententreprise = '"+CodeClt+"' ) ) AND ";
        }
        
        if(!clt.isNumericNotnull(CINClt)){
            WhereReq1 += " ( (C.cin LIKE '%"+CINClt+"%') OR (E.matricule_fiscale LIKE '%"+CINClt+"%') ) AND ";
        }
        
        if(!clt.isStringNull(NomClt)){
            WhereReq1 += " ( (C.nom LIKE '%"+NomClt+"%') OR (E.nomentrepise LIKE '%"+NomClt+"%') OR (C.prenom LIKE '%"+NomClt+"%') OR (C.prenom LIKE '%"+NomClt+"%') OR (E.matricule_fiscale LIKE '%"+NomClt+"%') ) AND ";
        }
        
        WhereReq1 = WhereReq1.substring(0, WhereReq1.length() - 4);
        
        ObservableList<TraiteCltGestionListe> DataListTraiteClt = FXCollections.observableArrayList();
        
        String Limit = "";
        if(All == false){
            Limit = " LIMIT "+debut+" , "+nbr+" ;";
        }
        String req= "SELECT T.*, F.typeclt, to_char(T.date_traite, 'dd/mm/yyyy') AS DateTraite, "
                    + " C.codeclient, C.cin, concat(C.nom, ' ',C.prenom) AS nom_prenom_client, "
                    + " E.idcliententreprise, E.matricule_fiscale, E.nomentrepise "
                    + " FROM traite_clt T, factureclt F "
                    + " LEFT JOIN client C ON  (F.idcltexiste = C.codeclient) "
                    + " LEFT JOIN cltentreprise E ON (F.idcltentreprise = E.idcliententreprise) "
                    + WhereReq1
                    + Limit ;
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                String NumTraite = resultat.getString("id_traite");
                String Facture  = resultat.getString("id_factureclt");
                String DateTraite = resultat.getString("DateTraite");
                String MontantTraite = Adaptateur.StringToStringEspace(resultat.getString("montant_traite"));
                String LocalNom = resultat.getString("local_nom");
                float FMontant = resultat.getFloat("montant_traite");
                
                //0 Personne  1 Entreprise
                Integer Typeclt = resultat.getInt("typeclt");
                 
                String CodeClient = ""; 
                String NomPrenom = "";
                if(Typeclt == 0){
                    CodeClient = resultat.getString("codeclient");
                    NomPrenom = resultat.getString("nom_prenom_client");
                }if(Typeclt == 1){
                    CodeClient = resultat.getString("idcliententreprise");
                    NomPrenom = resultat.getString("nomentrepise");
                }
                DataListTraiteClt.add(new TraiteCltGestionListe(NumTraite, "", Facture, CodeClient, LocalNom, NomPrenom, DateTraite, "", MontantTraite, FMontant));
            }
        } catch(SQLException ex){
            System.err.println("SearchTraiteCltRetard Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDB  : SearchTraiteCltRetard :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("TraiteCltDB Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return DataListTraiteClt;
    }
    
    
    public Integer nbrTraiteCltRetard(String DateDebut, String IdFacture, String NomLocal, String CodeClt, String CINClt, String NomClt)
    {
        String WhereReq1 = " WHERE (T.date_paiement IS NULL) AND (T.date_traite < now()) AND (T.id_factureclt = F.idfactureclt) AND ";
        
        if(!clt.isStringNull(NomLocal)){
            WhereReq1 += " (T.local_nom = '"+NomLocal+"') AND ";
        }
        
        if(!clt.isStringNull(IdFacture)){
            WhereReq1 += " (T.id_factureclt = '"+IdFacture+"') AND ";
        }
        
        if(!clt.isStringNull(DateDebut)){
            WhereReq1 += " (T.date_traite >= '"+DateDebut+"') AND ";
        }
        
        if(!clt.isStringNull(CodeClt)){
            WhereReq1 += " ( (C.codeclient = '"+CodeClt+"') OR (E.idcliententreprise = '"+CodeClt+"' ) ) AND ";
        }
        
        if(!clt.isStringNull(CINClt)){
            WhereReq1 += " ( (C.cin LIKE '%"+CINClt+"%') OR (E.matricule_fiscale LIKE '%"+CINClt+"%') ) AND ";
        }
        
        if(!clt.isStringNull(NomClt)){
            WhereReq1 += " ( (C.nom LIKE '%"+NomClt+"%') OR (E.nomentrepise LIKE '%"+NomClt+"%') OR (C.prenom LIKE '%"+NomClt+"%') OR (C.prenom LIKE '%"+NomClt+"%') OR (E.matricule_fiscale LIKE '%"+NomClt+"%') ) AND ";
        }
        
        WhereReq1 = WhereReq1.substring(0, WhereReq1.length() - 4);
        
        String req =  " SELECT COUNT(T.id_traite) "
                    + " FROM traite_clt T, factureclt F "
                    + " LEFT JOIN client C ON  (F.idcltexiste = C.codeclient) "
                    + " LEFT JOIN cltentreprise E ON (F.idcltentreprise = E.idcliententreprise) "
                    + WhereReq1 + " ; "; 
        
        int sommme =0;
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            resultat.next();
            sommme = resultat.getInt(1);
        } catch(SQLException ex){
            System.err.println("nbrTraiteCltRetard Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("TraiteCltDB  : nbrTraiteCltRetard :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return sommme ;
    }  
}
