package Models.ChequeClt;

import Controllers.Dialog.ShowDialog;
import Controllers.Traitement.Adaptateur;
import Controllers.Traitement.ParametreSystem;
import Controllers.Traitement.contro;
import Models.Caisse.CaisseClt;
import Models.User.User;
import Models.Client.Client;
import Models.Client.ClientEntreprise;
import Models.Client.ClientPassager;
import Models.User.Profile;
import org.apache.log4j.Logger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import Models.Caisse.CaisseDB;
import Models.H2JDBC;
import Models.User.ProfileDB;

/**
 * @author Maher Ben Tili
 */
public class ChequeCltDB {
    
    H2JDBC h2jdbc = new H2JDBC();
    ShowDialog SD = new ShowDialog();
    Logger logger = Logger.getLogger(ChequeCltDB.class.getName());
    contro clt = new contro();
    
    public ChequeClt getChequeCltByIdHistorique(String IdHistoriqueChequeClt){
        
        ChequeClt Cheque = new ChequeClt();
        Profile profile= new Profile();
        Client client = new Client();
        ClientEntreprise Entreprise = new ClientEntreprise();
        ClientPassager Passager = new ClientPassager();
       
        String req ="  SELECT C.id_chequeclt, C.numero, C.banque, C.montant, C.nom_complet, C.etat_cheque, to_char(C.date_paiement, 'dd/mm/yyyy') AS date_paiement, C.date_creation, C.id_factureclt, C.local_nom, " +
             "  C.id_profile, P.nom AS NomProfile, P.prenom AS PrenomProfile, " +
             "  F.typeclt, " +
             "  F.idcltexiste AS IdClient, Clt.nom AS NomClient, Clt.Prenom AS PrenomClient, Clt.telefix, Clt.telemobile, Clt.adresse AS adress_client, " +
             "  F.idcltentreprise AS IdEntreprise, Etr.nomentrepise AS NomEntreprise, Etr.matricule_fiscale AS MatriculeEntreprise, Etr.telefix, Etr.telefix2, Etr.adresse AS adress_entre,  " +
             "  Pas.idcltpassageur AS IdPassageur, Pas.nom AS NomPassageur, Pas.Prenom AS PrenomPassageur, Pas.tele, Pas.gsm, Pas.adresse AS adress_pas  " +
             " FROM historique_cheque_clt H, cheque_clt C, profile P, factureclt F " +
             " LEFT JOIN client Clt ON (F.typeclt = 0) AND (F.idcltexiste = Clt.codeclient) AND (Clt.local_nom = F.local_nom) " +
             " LEFT JOIN cltentreprise Etr ON (F.typeclt = 1) AND (F.idcltentreprise = Etr.idcliententreprise) AND (Etr.local_nom = F.local_nom) " +
             " LEFT JOIN cltpassageur Pas ON (F.typeclt = 2) AND (F.idcltpassageur = Pas.idcltpassageur) AND (Pas.local_nom = F.local_nom) " +
             " WHERE (H.id_historique_cheque_clt = '"+IdHistoriqueChequeClt+"') AND (H.id_chequeclt = C.id_chequeclt) AND (H.local_nom = '"+ParametreSystem.NomLocalPC+"') AND (C.id_profile = P.idprofile) AND (C.id_factureclt = F.idfactureclt);";
        
        try {
            ResultSet Result = h2jdbc.selectFromDB(req);
            while (Result.next()){
                Cheque.setCodeChequeClt(Result.getString("id_chequeclt"));
                Cheque.setNumeroChequeClt(Result.getString("numero"));
                Cheque.setBanqueChequeClt(Result.getString("banque"));
                Cheque.setMontantChequeClt(Result.getString("montant"));
                Cheque.setNomCompletChequeClt(Result.getString("nom_complet"));
                Cheque.setDatePaiement(Result.getString("date_paiement"));
                Cheque.setDateCreation(Result.getString("date_creation"));
                Cheque.setCodeFactureClt(Result.getString("id_factureclt"));
                Cheque.setLocalNom(Result.getString("local_nom"));

                // 0 cheque sans provision     1 Verser (Valider)   2 Contre chéque(espace)
                if(Result.getString("etat_cheque") != null){
                    Cheque.setLastEtat(Result.getInt("etat_cheque"));
                }

                profile.setIdprofile(Result.getInt("id_profile"));
                profile.setNom(Result.getString("NomProfile"));
                profile.setPrenom(Result.getString("PrenomProfile"));
                Cheque.setProfile(profile);

                Cheque.setTypeClient(Result.getInt("typeclt"));
                if(Result.getInt("typeclt") == 0){
                    client.setCodeclient(Result.getString("IdClient"));
                    client.setNom(Result.getString("NomClient"));
                    client.setPrenom(Result.getString("PrenomClient"));
                    client.setTelefixString(Result.getString("telefix"));
                    client.setTelemobileString(Result.getString("telemobile"));
                    client.setAdresse(Result.getString("adress_client"));
                    Cheque.setCltPersonne(client); 
                }else if(Result.getInt("typeclt") == 1){
                    Entreprise.setCodeString(Result.getString("IdEntreprise"));
                    Entreprise.setNom(Result.getString("NomEntreprise"));
                    Entreprise.setMatricule(Result.getString("MatriculeEntreprise"));
                    Entreprise.setTele1String(Result.getString("telefix"));
                    Entreprise.setTele2String(Result.getString("telefix2"));
                    Entreprise.setAdresse(Result.getString("adress_entre"));
                    Cheque.setCltEntreprise(Entreprise);
                }else if(Result.getInt("typeclt") == 2){
                    Passager.setCodeclient(Result.getString("IdPassageur"));
                    Passager.setNom(Result.getString("NomPassageur"));
                    Passager.setPrenom(Result.getString("PrenomPassageur"));
                    Passager.setTelefixString(Result.getString("tele"));
                    Passager.setTelemobileString(Result.getString("gsm"));
                    Passager.setAdresse(Result.getString("adress_pas"));
                    Cheque.setCltPassager(Passager);
                }
            }
        } catch(SQLException ex){
            System.err.println("getChequeCltByIdHistorique Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : getChequeCltByIdHistorique :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return Cheque;
    }
    
    
    public ChequeClt getChequeCltByFacture(String IdFactureClt)
    {
        ChequeClt Cheque = new ChequeClt();
        Profile profile= new Profile();
        Client client = new Client();
        ClientEntreprise Entreprise = new ClientEntreprise();
        ClientPassager Passager = new ClientPassager();
       
        String req ="  SELECT C.id_chequeclt, C.numero, C.banque, C.montant, C.nom_complet, C.etat_cheque, to_char(C.date_paiement, 'dd/mm/yyyy') AS date_paiement, C.date_creation, C.id_factureclt, C.local_nom, " +
             "  C.id_profile, P.nom AS NomProfile, P.prenom AS PrenomProfile, " +
             "  F.typeclt, " +
             "  F.idcltexiste AS IdClient, Clt.nom AS NomClient, Clt.Prenom AS PrenomClient, Clt.telefix, Clt.telemobile, Clt.adresse AS adress_client, " +
             "  F.idcltentreprise AS IdEntreprise, Etr.nomentrepise AS NomEntreprise, Etr.matricule_fiscale AS MatriculeEntreprise, Etr.telefix, Etr.telefix2, Etr.adresse AS adress_entre,  " +
             "  Pas.idcltpassageur AS IdPassageur, Pas.nom AS NomPassageur, Pas.Prenom AS PrenomPassageur, Pas.tele, Pas.gsm, Pas.adresse AS adress_pas  " +
             " FROM cheque_clt C, profile P, factureclt F " +
             " LEFT JOIN client Clt ON (F.typeclt = 0) AND (F.idcltexiste = Clt.codeclient) AND (Clt.local_nom = F.local_nom) " +
             " LEFT JOIN cltentreprise Etr ON (F.typeclt = 1) AND (F.idcltentreprise = Etr.idcliententreprise) AND (Etr.local_nom = F.local_nom) " +
             " LEFT JOIN cltpassageur Pas ON (F.typeclt = 2) AND (F.idcltpassageur = Pas.idcltpassageur) AND (Pas.local_nom = F.local_nom) " +
             " WHERE (C.id_factureclt = '"+IdFactureClt+"') AND (C.id_profile = P.idprofile) AND (C.id_factureclt = F.idfactureclt);";
        try {
            ResultSet Result = h2jdbc.selectFromDB(req);
            while (Result.next()){
                Cheque.setCodeChequeClt(Result.getString("id_chequeclt"));
                Cheque.setNumeroChequeClt(Result.getString("numero"));
                Cheque.setBanqueChequeClt(Result.getString("banque"));
                Cheque.setMontantChequeClt(Result.getString("montant"));
                Cheque.setNomCompletChequeClt(Result.getString("nom_complet"));
                Cheque.setDatePaiement(Result.getString("date_paiement"));
                Cheque.setDateCreation(Result.getString("date_creation"));
                Cheque.setCodeFactureClt(Result.getString("id_factureclt"));
                Cheque.setLocalNom(Result.getString("local_nom"));

                // 0 cheque sans provision     1 Verser (Valider)   2 Contre chéque(espace)
                if(Result.getString("etat_cheque") != null){
                    Cheque.setLastEtat(Result.getInt("etat_cheque"));
                }


                profile.setIdprofile(Result.getInt("id_profile"));
                profile.setNom(Result.getString("NomProfile"));
                profile.setPrenom(Result.getString("PrenomProfile"));
                Cheque.setProfile(profile);

                Cheque.setTypeClient(Result.getInt("typeclt"));
                if(Result.getInt("typeclt") == 0){
                    client.setCodeclient(Result.getString("IdClient"));
                    client.setNom(Result.getString("NomClient"));
                    client.setPrenom(Result.getString("PrenomClient"));
                    client.setTelefixString(Result.getString("telefix"));
                    client.setTelemobileString(Result.getString("telemobile"));
                    client.setAdresse(Result.getString("adress_client"));
                    Cheque.setCltPersonne(client); 
                }else if(Result.getInt("typeclt") == 1){
                    Entreprise.setCodeString(Result.getString("IdEntreprise"));
                    Entreprise.setNom(Result.getString("NomEntreprise"));
                    Entreprise.setMatricule(Result.getString("MatriculeEntreprise"));
                    Entreprise.setTele1String(Result.getString("telefix"));
                    Entreprise.setTele2String(Result.getString("telefix2"));
                    Entreprise.setAdresse(Result.getString("adress_entre"));
                    Cheque.setCltEntreprise(Entreprise);
                }else if(Result.getInt("typeclt") == 2){
                    Passager.setCodeclient(Result.getString("IdPassageur"));
                    Passager.setNom(Result.getString("NomPassageur"));
                    Passager.setPrenom(Result.getString("PrenomPassageur"));
                    Passager.setTelefixString(Result.getString("tele"));
                    Passager.setTelemobileString(Result.getString("gsm"));
                    Passager.setAdresse(Result.getString("adress_pas"));
                    Cheque.setCltPassager(Passager);
                }
            }
        } catch(SQLException ex){
            System.err.println("getChequeCltByFacture Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : getChequeCltByFacture :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return Cheque;
    }
    
    
    
    public ChequeClt getChequeClt(String IdChequeClt, Integer Source)
    {
        ChequeClt Cheque = new ChequeClt();
        Profile profile= new Profile();
        Client client = new Client();
        ClientEntreprise Entreprise = new ClientEntreprise();
        ClientPassager Passager = new ClientPassager();
        
        String req ="";
        
        if(Source == 0)
        {
                req ="SELECT  " +
                        "  C.id_chequeclt, C.numero, C.banque, C.montant, C.nom_complet, C.etat_cheque, to_char(C.date_paiement, 'dd/mm/yyyy') AS date_paiement, C.date_creation, C.id_factureclt, C.local_nom, " +
                        "  C.id_profile, P.nom AS NomProfile, P.prenom AS PrenomProfile, " +
                        "  F.typeclt, " +
                        "  F.idcltexiste AS IdClient, Clt.nom AS NomClient, Clt.Prenom AS PrenomClient, Clt.telefix, Clt.telemobile, Clt.adresse AS adress_client, " +
                        "  F.idcltentreprise AS IdEntreprise, Etr.nomentrepise AS NomEntreprise, Etr.matricule_fiscale AS MatriculeEntreprise, Etr.telefix, Etr.telefix2, Etr.adresse AS adress_entre,  " +
                        "  Pas.idcltpassageur AS IdPassageur, Pas.nom AS NomPassageur, Pas.Prenom AS PrenomPassageur, Pas.tele, Pas.gsm, Pas.adresse AS adress_pas  " +
                        " FROM cheque_clt C, profile P, factureclt F " +
                        " LEFT JOIN client Clt ON (F.typeclt = 0) AND (F.idcltexiste = Clt.codeclient) AND (Clt.local_nom = F.local_nom) " +
                        " LEFT JOIN cltentreprise Etr ON (F.typeclt = 1) AND (F.idcltentreprise = Etr.idcliententreprise) AND (Etr.local_nom = F.local_nom) " +
                        " LEFT JOIN cltpassageur Pas ON (F.typeclt = 2) AND (F.idcltpassageur = Pas.idcltpassageur) AND (Pas.local_nom = F.local_nom) " +
                        " WHERE (id_chequeclt = '"+IdChequeClt+"') AND (C.id_profile = P.idprofile) AND (C.id_factureclt = F.idfactureclt);";
        }else if(Source == 1){
                req ="SELECT  " +
                    "    C.id_traite_paiement_cheque AS id_chequeclt, " +
                    "    C.numero, " +
                    "    C.banque, " +
                    "    C.montant_payer AS montant, " +
                    "    C.nom_complet, " +
                    "    C.etat_cheque, " +
                    "    to_char(C.date_payer, 'dd/mm/yyyy') AS date_paiement, " +
                    "    C.date_creation, " +
                    "    F.idfactureclt AS id_factureclt, " +
                    "    C.local_nom, " +
                    "    C.id_profile, P.nom AS NomProfile, P.prenom AS PrenomProfile, " +
                    "    F.typeclt, " +
                    "  F.idcltexiste AS IdClient, Clt.nom AS NomClient, Clt.Prenom AS PrenomClient, Clt.telefix, Clt.telemobile, Clt.adresse AS adress_client, " +
                    "  F.idcltentreprise AS IdEntreprise, Etr.nomentrepise AS NomEntreprise, Etr.matricule_fiscale AS MatriculeEntreprise, Etr.telefix, Etr.telefix2, Etr.adresse AS adress_entre,  " +
                    "  Pas.idcltpassageur AS IdPassageur, Pas.nom AS NomPassageur, Pas.Prenom AS PrenomPassageur, Pas.tele, Pas.gsm, Pas.adresse AS adress_pas  " +
                    " FROM traite_paiement_cheque_clt C, profile P, traite_clt T, factureclt F " +
                    " LEFT JOIN client Clt ON (F.typeclt = 0) AND (F.idcltexiste = Clt.codeclient) AND (Clt.local_nom = F.local_nom) " +
                    " LEFT JOIN cltentreprise Etr ON (F.typeclt = 1) AND (F.idcltentreprise = Etr.idcliententreprise) AND (Etr.local_nom = F.local_nom) " +
                    " LEFT JOIN cltpassageur Pas ON (F.typeclt = 2) AND (F.idcltpassageur = Pas.idcltpassageur) AND (Pas.local_nom = F.local_nom) " +
                    " WHERE (C.id_traite_paiement_cheque = '"+IdChequeClt+"') AND (C.id_profile = P.idprofile) AND (C.id_traite = T.id_traite) AND (T.id_factureclt = F.idfactureclt) AND (T.local_nom = C.local_nom);";
        }
        try {
            ResultSet Result = h2jdbc.selectFromDB(req);
            while (Result.next()){
                Cheque.setCodeChequeClt(Result.getString("id_chequeclt"));
                Cheque.setNumeroChequeClt(Result.getString("numero"));
                Cheque.setBanqueChequeClt(Result.getString("banque"));
                Cheque.setMontantChequeClt(Result.getString("montant"));
                Cheque.setMontantFloatChequeClt(Result.getFloat("montant"));
                Cheque.setNomCompletChequeClt(Result.getString("nom_complet"));
                Cheque.setDatePaiement(Result.getString("date_paiement"));
                Cheque.setDateCreation(Result.getString("date_creation"));
                Cheque.setCodeFactureClt(Result.getString("id_factureclt"));
                Cheque.setLocalNom(Result.getString("local_nom"));

                // 0 cheque sans provision     1 Verser (Valider)   2 Contre chéque(espace)
                if(Result.getString("etat_cheque") != null){
                    Cheque.setLastEtat(Result.getInt("etat_cheque"));
                }
                
                profile.setIdprofile(Result.getInt("id_profile"));
                profile.setNom(Result.getString("NomProfile"));
                profile.setPrenom(Result.getString("PrenomProfile"));
                Cheque.setProfile(profile);

                Cheque.setTypeClient(Result.getInt("typeclt"));
                if(Result.getInt("typeclt") == 0){
                    client.setCodeclient(Result.getString("IdClient"));
                    client.setNom(Result.getString("NomClient"));
                    client.setPrenom(Result.getString("PrenomClient"));
                    client.setTelefixString(Result.getString("telefix"));
                    client.setTelemobileString(Result.getString("telemobile"));
                    client.setAdresse(Result.getString("adress_client"));
                    Cheque.setCltPersonne(client); 
                }else if(Result.getInt("typeclt") == 1){
                    Entreprise.setCodeString(Result.getString("IdEntreprise"));
                    Entreprise.setNom(Result.getString("NomEntreprise"));
                    Entreprise.setMatricule(Result.getString("MatriculeEntreprise"));
                    Entreprise.setTele1String(Result.getString("telefix"));
                    Entreprise.setTele2String(Result.getString("telefix2"));
                    Entreprise.setAdresse(Result.getString("adress_entre"));
                    Cheque.setCltEntreprise(Entreprise);
                }else if(Result.getInt("typeclt") == 2){
                    Passager.setCodeclient(Result.getString("IdPassageur"));
                    Passager.setNom(Result.getString("NomPassageur"));
                    Passager.setPrenom(Result.getString("PrenomPassageur"));
                    Passager.setTelefixString(Result.getString("tele"));
                    Passager.setTelemobileString(Result.getString("gsm"));
                    Passager.setAdresse(Result.getString("adress_pas"));
                    Cheque.setCltPassager(Passager);
                }
                Cheque.setHistoriqueListe(this.getListChequeHistorique(IdChequeClt, Source));
            }
        } catch(SQLException ex){
            System.err.println("getChequeClt Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : getChequeClt :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return Cheque;
    }
    
    
    public ObservableList<ChequeCltHistoriqueListe> getListChequeHistorique(String id_cheque, Integer source)
    {
        ObservableList<ChequeCltHistoriqueListe> ListChequeHistorique = FXCollections.observableArrayList();
        
        String req = "";
        if(source == 0){
            req = "SELECT C.id_historique_cheque_clt, C.etat_cheque, to_char(C.date, 'dd/mm/yyyy') AS date_operation, to_char(date, 'hh:ii:ss') AS heur_operation, C.id_chequeclt AS id_cheque, C.local_nom, C.id_profile, P.nom, P.prenom "
                    + " FROM historique_cheque_clt C, profile P "
                    + " WHERE (C.local_nom LIKE '"+ParametreSystem.NomLocalPC+"') AND (C.id_chequeclt = '"+id_cheque+"') AND (P.idprofile = C.id_profile) ; ";
        }else if(source == 1){
            req = "SELECT C.id_historique_cheque_clt, C.etat_cheque, to_char(C.date, 'dd/mm/yyyy') AS date_operation, to_char(date, 'hh:ii:ss') AS heur_operation, C.id_chequeclt AS id_cheque, C.local_nom, C.id_profile, P.nom, P.prenom "
                    + " FROM historique_cheque_clt C, profile P "
                    + " WHERE (C.local_nom LIKE '"+ParametreSystem.NomLocalPC+"') AND (C.id_traite_paiement_cheque = '"+id_cheque+"') AND (P.idprofile = C.id_profile) ; ";
        }
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                String id_historique = resultat.getString("id_historique_cheque_clt");
                Integer etat = resultat.getInt("etat_cheque");
                String date_operation  = resultat.getString("date_operation");
                String heur_operation  = resultat.getString("heur_operation");
                String utilisateur  = resultat.getString("nom")+" "+resultat.getString("prenom");
                String locale  = resultat.getString("local_nom");
                ListChequeHistorique.add(new ChequeCltHistoriqueListe(id_historique, etat, source, id_cheque, date_operation, heur_operation, utilisateur, locale));
            }
        } catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : getListChequeHistorique :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return ListChequeHistorique;
    }
    
    
    
    public String AddChequeClt(ChequeClt Cheque)
    {
        String req ="INSERT INTO cheque_clt (numero, banque, montant, nom_complet, date_paiement, date_creation, id_factureclt, local_nom, id_profile) VALUES ('"+Cheque.getNumeroChequeClt()+"', '"+Cheque.getBanqueChequeClt()+"', "+Cheque.getMontantChequeClt()+", '"+Cheque.getNomCompletChequeClt()+"', '"+Cheque.getDatePaiement()+"', now(), "+Cheque.getCodeFactureClt()+", '"+ParametreSystem.NomLocalPC+"', "+User.idprofile+")";
        String id = null;
        try {
            id = h2jdbc.InsertGetLastId(req);
        }catch(SQLException ex){
            System.err.println("AddChequeClt Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : AddChequeClt  :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return id;
    }
   
    public void AddListeChequeClt(ArrayList<ChequeClt> ListChequeClt, String CodeFactureClt)
    {
        String ReqChequeClt = "INSERT INTO cheque_clt (numero, banque, montant, nom_complet, date_paiement, date_creation, id_factureclt, local_nom, id_profile) VALUES ";    
        String Req = "";  
        
        for(int i=0; i<ListChequeClt.size(); i++){
            String montant_cheque = ListChequeClt.get(i).getMontantChequeClt().replace(",",".");
            String date_paiement =  ListChequeClt.get(i).getDatePaiement();
            String code_facture = ListChequeClt.get(i).getCodeFactureClt();
            if(code_facture == null){
                code_facture = CodeFactureClt;
            }
            Req = Req + "("+ListChequeClt.get(i).getNumeroChequeClt()+", '"+ListChequeClt.get(i).getBanqueChequeClt()+"', "+montant_cheque+", '"+ListChequeClt.get(i).getNomCompletChequeClt()+"', '"+date_paiement+"', now(), "+code_facture+", '"+ParametreSystem.NomLocalPC+"', "+User.idprofile+"),";
        }
        if(Req != null){
            ReqChequeClt = ReqChequeClt + Req.substring(0, Req.length()-1) + ";";
            try {
                h2jdbc.InsertUpdateDeleteInTable(ReqChequeClt);
            }catch(SQLException ex){
                System.err.println("Erreur SQL ! \n"+ex.getMessage());
                System.out.println(ReqChequeClt);
                if(ParametreSystem.ErrorShowLogger)
                {
                    logger.error("ChequeCltDB  : ReqChequeClt :" + ex.getMessage());
                }
                if(ParametreSystem.ErrorShowDialog)
                {
                    ShowDialog.setTitre("Erreur SQL !");
                    ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                    SD.ShowAletDialog();
                }
            }
        }
    }
    
    public ObservableList SearchChequeCltGestion(Integer debut, Integer nbr, String NumeroCheque, String Banque, String DateCheque, String CodeClt, String CINClt, String IdFacture, String NomClt, String PrenomClt)
    {
        ObservableList<ChequeCltGestionListe> DataListChequeClt = FXCollections.observableArrayList();
        
        String WhereReq1 = "";
        String WhereReq2 = "";
        
        if(!clt.isNumericNotnull(NumeroCheque)){
            WhereReq1 += " (C.numero LIKE '%"+Adaptateur.addSlashes(NumeroCheque)+"%') AND ";
            WhereReq2 += " (C.numero LIKE '%"+Adaptateur.addSlashes(NumeroCheque)+"%') AND ";
        }
        
        if(!clt.isStringNull(Banque)){
            WhereReq1 += " (C.banque LIKE '%"+Adaptateur.addSlashes(Banque)+"%') AND ";
            WhereReq2 += " (C.banque LIKE '%"+Adaptateur.addSlashes(Banque)+"%') AND ";
        }
        
        if(!clt.isStringNull(DateCheque)){
            WhereReq1 += " (C.date_paiement = '"+DateCheque+"') AND ";
            WhereReq2 += " (C.date_payer = '"+DateCheque+"') AND ";
        }
        
        if(!clt.isNumericNotnull(CodeClt)){
            WhereReq1 += " ( (O.codeclient = '"+CodeClt+"') OR (E.idcliententreprise = '"+CodeClt+"' ) ) AND ";
            WhereReq2 += " ( (O.codeclient = '"+CodeClt+"') OR (E.idcliententreprise = '"+CodeClt+"' ) ) AND ";
        }
        
        if(!clt.isNumericNotnull(CINClt)){
            WhereReq1 += " ( (O.cin LIKE '%"+CINClt+"%') OR (E.matricule_fiscale LIKE '%"+CINClt+"%') ) AND ";
            WhereReq2 += " ( (O.cin LIKE '%"+CINClt+"%') OR (E.matricule_fiscale LIKE '%"+CINClt+"%') ) AND ";
        }
        
        if(!clt.isStringNull(NomClt)){
            WhereReq1 += " ( (P.nom LIKE '%"+NomClt+"%') OR (O.nom LIKE '%"+NomClt+"%') OR (E.nomentrepise LIKE '%"+NomClt+"%') ) AND ";
            WhereReq2 += " ( (O.nom LIKE '%"+NomClt+"%') OR (O.nom LIKE '%"+NomClt+"%') OR (E.nomentrepise LIKE '%"+NomClt+"%') ) AND ";
        }
        
        if(!clt.isStringNull(PrenomClt)){
            WhereReq1 += " ( (P.prenom LIKE '%"+PrenomClt+"%') OR (O.prenom LIKE '%"+PrenomClt+"%') OR (E.matricule_fiscale LIKE '%"+PrenomClt+"%') ) AND ";
            WhereReq2 += " ( (O.prenom LIKE '%"+PrenomClt+"%') OR (O.prenom LIKE '%"+PrenomClt+"%') OR (E.matricule_fiscale LIKE '%"+PrenomClt+"%') ) AND ";
        }
        
        if(!clt.isNumericNotnull(IdFacture)){
            WhereReq1 += " (C.id_factureclt = '"+IdFacture+"') AND ";
            WhereReq2 += " (T.id_factureclt = '"+IdFacture+"') AND ";
        }
        
        String req= "SELECT C.id_chequeclt, '0' AS source_cheque, C.numero, C.banque, C.montant,  C.etat_cheque, to_char(C.date_paiement, 'dd/mm/yyyy') AS date_cheque, C.id_factureclt, F.typeclt, O.codeclient, O.nom, O.prenom, P.idcltpassageur , P.nom AS nom_pass, P.prenom AS prenom_pass, E.idcliententreprise, E.nomentrepise, E.matricule_fiscale "
                + "  FROM cheque_clt C, factureclt F "
                + "  LEFT JOIN client O ON (O.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltexiste = O.codeclient) "
                + "  LEFT JOIN cltpassageur P ON (P.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltpassageur = P.idcltpassageur) "
                + "  LEFT JOIN cltentreprise E ON (E.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltentreprise = E.idcliententreprise) "
                + "  WHERE "+WhereReq1+" (C.local_nom = '"+ParametreSystem.NomLocalPC+"') AND (C.id_factureclt = F.idfactureclt) "
                + "  UNION ALL "   
                + "  SELECT C.id_traite_paiement_cheque AS id_chequeclt, '1' AS source_cheque, C.numero, C.banque, C.montant_payer AS montant,  C.etat_cheque, to_char(C.date_payer, 'dd/mm/yyyy') AS date_cheque, T.id_factureclt, F.typeclt, O.codeclient, O.nom, O.prenom, P.idcltpassageur , P.nom AS nom_pass, P.prenom AS prenom_pass, E.idcliententreprise, E.nomentrepise, E.matricule_fiscale "
                + "  FROM traite_paiement_cheque_clt C, traite_clt T, factureclt F  "
                + "  LEFT JOIN client O ON (O.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltexiste = O.codeclient) "
                + "  LEFT JOIN cltpassageur P ON (P.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltpassageur = P.idcltpassageur) "
                + "  LEFT JOIN cltentreprise E ON (E.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltentreprise = E.idcliententreprise) "
                + "  WHERE "+WhereReq2+" (C.local_nom = '"+ParametreSystem.NomLocalPC+"') AND (T.id_factureclt = F.idfactureclt) AND (C.id_traite = T.id_traite)"
                + "  LIMIT "+debut+" , "+nbr+" ;";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                String id_cheque = resultat.getString("id_chequeclt");
                String numero = resultat.getString("numero");
                String banque = resultat.getString("banque");
                String montant = Adaptateur.FloatToStringEspace(resultat.getFloat("montant"));
                String date_cheque = resultat.getString("date_cheque");
                String id_factureclt = resultat.getString("id_factureclt");
                
                //0 Personne  1 Entreprise
                Integer Typeclt = resultat.getInt("typeclt");
                String TypeClient = ""; String CodeClient = ""; String Nom = ""; String Prenom = "";
                if(Typeclt == 0){
                    TypeClient = "Personne";
                    CodeClient = resultat.getString("codeclient");
                    Nom = resultat.getString("nom");
                    Prenom = resultat.getString("prenom");
                }if(Typeclt == 1){
                    TypeClient = "Entreprise";
                    CodeClient = resultat.getString("idcliententreprise");
                    Nom = resultat.getString("nomentrepise");
                    Prenom = resultat.getString("matricule_fiscale");
                }if(Typeclt == 2){
                    TypeClient = "Passageur";
                    CodeClient = resultat.getString("idcltpassageur");
                    Nom = resultat.getString("nom_pass");
                    Prenom = resultat.getString("prenom_pass");
                }
                
                //  0 ==> Cheque sans provision
                //  1 ==> Verser (Valider) 
                //  2 ==> Contre chèque
                String etat = resultat.getString("etat_cheque");
                
                // 0 ==> Cheque (table cheque_clt)
                // 1 ==> Traite (table traite_paiement_cheque_clt)
                Integer source = resultat.getInt("source_cheque");
                
                DataListChequeClt.add(new ChequeCltGestionListe(id_cheque, numero, banque, date_cheque, montant, CodeClt, TypeClient, Nom, Prenom, id_factureclt, etat, source, false));
            }
        } catch(SQLException ex){
            System.err.println("SearchChequeCltGestion Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : SearchChequeCltGestion :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return DataListChequeClt; 
    }
    
    public Integer nbrChequeCltGestion(String NumeroCheque, String Banque, String DateCheque, String CodeClt, String CINClt, String IdFacture, String NomClt, String PrenomClt)
    {
        String WhereReq1 = "";
        String WhereReq2 = "";
        
        if(!clt.isNumericNotnull(NumeroCheque)){
            WhereReq1 += " (C.numero LIKE '%"+Adaptateur.addSlashes(NumeroCheque)+"%') AND ";
            WhereReq2 += " (C.numero LIKE '%"+Adaptateur.addSlashes(NumeroCheque)+"%') AND ";
        }
        
        if(!clt.isStringNull(Banque)){
            WhereReq1 += " (C.banque LIKE '%"+Adaptateur.addSlashes(Banque)+"%') AND ";
            WhereReq2 += " (C.banque LIKE '%"+Adaptateur.addSlashes(Banque)+"%') AND ";
        }
        
        if(!clt.isStringNull(DateCheque)){
            WhereReq1 += " (C.date_paiement = '"+DateCheque+"') AND ";
            WhereReq2 += " (C.date_payer = '"+DateCheque+"') AND ";
        }
        
        if(!clt.isNumericNotnull(CodeClt)){
            WhereReq1 += " ( (O.codeclient = '"+CodeClt+"') OR (E.idcliententreprise = '"+CodeClt+"' ) ) AND ";
            WhereReq2 += " ( (O.codeclient = '"+CodeClt+"') OR (E.idcliententreprise = '"+CodeClt+"' ) ) AND ";
        }
        
        if(!clt.isNumericNotnull(CINClt)){
            WhereReq1 += " ( (O.cin LIKE '%"+CINClt+"%') OR (E.matricule_fiscale LIKE '%"+CINClt+"%') ) AND ";
            WhereReq2 += " ( (O.cin LIKE '%"+CINClt+"%') OR (E.matricule_fiscale LIKE '%"+CINClt+"%') ) AND ";
        }
        
        if(!clt.isStringNull(NomClt)){
            WhereReq1 += " ( (P.nom LIKE '%"+NomClt+"%') OR (O.nom LIKE '%"+NomClt+"%') OR (E.nomentrepise LIKE '%"+NomClt+"%') ) AND ";
            WhereReq2 += " ( (O.nom LIKE '%"+NomClt+"%') OR (O.nom LIKE '%"+NomClt+"%') OR (E.nomentrepise LIKE '%"+NomClt+"%') ) AND ";
        }
        
        if(!clt.isStringNull(PrenomClt)){
            WhereReq1 += " ( (P.prenom LIKE '%"+PrenomClt+"%') OR (O.prenom LIKE '%"+PrenomClt+"%') OR (E.matricule_fiscale LIKE '%"+PrenomClt+"%') ) AND ";
            WhereReq2 += " ( (O.prenom LIKE '%"+PrenomClt+"%') OR (O.prenom LIKE '%"+PrenomClt+"%') OR (E.matricule_fiscale LIKE '%"+PrenomClt+"%') ) AND ";
        }
        
        if(!clt.isNumericNotnull(IdFacture)){
            WhereReq1 += " (C.id_factureclt = '"+IdFacture+"') AND ";
            WhereReq2 += " (T.id_factureclt = '"+IdFacture+"') AND ";
        }
        
        String req= "SELECT COUNT(C.id_chequeclt) AS nbr_cheque"
                + "  FROM cheque_clt C, factureclt F "
                + "  LEFT JOIN client O ON (O.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltexiste = O.codeclient) "
                + "  LEFT JOIN cltpassageur P ON (P.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltpassageur = P.idcltpassageur) "
                + "  LEFT JOIN cltentreprise E ON (E.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltentreprise = E.idcliententreprise) "
                + "  WHERE "+WhereReq1+" (C.local_nom = '"+ParametreSystem.NomLocalPC+"') AND (C.id_factureclt = F.idfactureclt) "
                + "  UNION ALL "   
                + "  SELECT COUNT(C.id_traite_paiement_cheque) AS nbr_cheque "
                + "  FROM traite_paiement_cheque_clt C, traite_clt T, factureclt F  "
                + "  LEFT JOIN client O ON (O.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltexiste = O.codeclient) "
                + "  LEFT JOIN cltpassageur P ON (P.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltpassageur = P.idcltpassageur) "
                + "  LEFT JOIN cltentreprise E ON (E.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltentreprise = E.idcliententreprise) "
                + "  WHERE "+WhereReq2+" (C.local_nom = '"+ParametreSystem.NomLocalPC+"') AND (T.id_factureclt = F.idfactureclt) AND (C.id_traite = T.id_traite) ; ";
        
        Integer Sommme = 0;
         try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                Sommme += resultat.getInt("nbr_cheque");
            }
        } catch(SQLException ex){
            System.err.println("nbrChequeCltGestion Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : nbrChequeCltGestion :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return  Sommme ;
    }

    
    //Etat     0 cheque sans provision     1 Verser (Valider)   2 Contre chéque(espace)
    //Source   0==>Cheque(table cheque_clt)  1==>Traite(table traite_paiement_cheque_clt)
    public String setHistoriqueCheque(Integer Etat, Integer Source, ChequeClt Cheque)
    {
        String QueryINSERT = ""; 
        String QueryUpdate = ""; 
        
        if(Source == 0){
            QueryINSERT = "INSERT INTO historique_cheque_clt (etat_cheque, date, id_chequeclt, local_nom, id_profile) VALUES ('"+Etat+"', now(), '"+Cheque.getCodeChequeClt()+"', '"+ParametreSystem.NomLocalPC+"', '"+User.idprofile+"');";    
            QueryUpdate = "UPDATE cheque_clt SET etat_cheque='"+Etat+"' WHERE id_chequeclt='"+Cheque.getCodeChequeClt()+"';";
        }else if(Source == 1){
            QueryINSERT = "INSERT INTO historique_cheque_clt (etat_cheque, date, id_traite_paiement_cheque, local_nom, id_profile) VALUES ('"+Etat+"', now(), '"+Cheque.getCodeChequeClt()+"', '"+ParametreSystem.NomLocalPC+"', '"+User.idprofile+"');";    
            QueryUpdate = "UPDATE traite_paiement_cheque_clt SET etat_cheque='"+Etat+"' WHERE id_traite_paiement_cheque='"+Cheque.getCodeChequeClt()+"';";
        }
        try {
            h2jdbc.InsertUpdateDeleteInTable(QueryUpdate);
        }catch(SQLException ex){
            System.err.println("setHistoriqueCheque Erreur SQL ! \n"+ex.getMessage());
            System.out.println(QueryUpdate);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : setHistoriqueCheque :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        
        String IdHistoriqueCheque = null;
        try {
            IdHistoriqueCheque = h2jdbc.InsertGetLastId(QueryINSERT);
        }catch(SQLException ex){
            System.err.println("setHistoriqueCheque Erreur SQL ! \n"+ex.getMessage());
            System.out.println(QueryINSERT);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : setHistoriqueCheque :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        if((Etat == 1) || (Etat == 2)){
            CaisseClt Caisse = new CaisseClt();
            Caisse.setTypeMouvement("4");
            Caisse.setMontant(Cheque.getMontantChequeClt());
            Caisse.setIdHistoriqueCheque(IdHistoriqueCheque); 
            
            CaisseDB CaisseDB = new CaisseDB();
            CaisseDB.AddCaisse(Caisse);
        }
        
        return IdHistoriqueCheque;
    }
    
    
    public ObservableList GetDataChequeAverser(String date_debut, String  date_fin)
    {
        ObservableList<ChequeCltGestionListe> DataListChequeClt = FXCollections.observableArrayList();
       
        String req= "SELECT C.id_chequeclt, '0' AS source_cheque, C.numero, C.banque, C.montant,  C.etat_cheque, to_char(C.date_paiement, 'dd/mm/yyyy') AS date_cheque, C.id_factureclt, F.typeclt, O.codeclient, O.nom, O.prenom, P.idcltpassageur , P.nom AS nom_pass, P.prenom AS prenom_pass, E.idcliententreprise, E.nomentrepise, E.matricule_fiscale "
                + "  FROM cheque_clt C, factureclt F "
                + "  LEFT JOIN client O ON (O.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltexiste = O.codeclient) "
                + "  LEFT JOIN cltpassageur P ON (P.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltpassageur = P.idcltpassageur) "
                + "  LEFT JOIN cltentreprise E ON (E.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltentreprise = E.idcliententreprise) "
                + "  WHERE (C.date_paiement BETWEEN '"+date_debut+"' AND '"+date_fin+"') AND ((C.etat_cheque IS NULL) OR (C.etat_cheque = 0)) AND (C.local_nom = '"+ParametreSystem.NomLocalPC+"') AND (C.id_factureclt = F.idfactureclt) "
                + "  UNION ALL "   
                + "  SELECT C.id_traite_paiement_cheque AS id_chequeclt, '1' AS source_cheque, C.numero, C.banque, C.montant_payer AS montant,  C.etat_cheque, to_char(C.date_payer, 'dd/mm/yyyy') AS date_cheque, T.id_factureclt, F.typeclt, O.codeclient, O.nom, O.prenom, P.idcltpassageur , P.nom AS nom_pass, P.prenom AS prenom_pass, E.idcliententreprise, E.nomentrepise, E.matricule_fiscale "
                + "  FROM traite_paiement_cheque_clt C, traite_clt T, factureclt F  "
                + "  LEFT JOIN client O ON (O.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltexiste = O.codeclient) "
                + "  LEFT JOIN cltpassageur P ON (P.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltpassageur = P.idcltpassageur) "
                + "  LEFT JOIN cltentreprise E ON (E.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltentreprise = E.idcliententreprise) "
                + "  WHERE (C.date_payer BETWEEN '"+date_debut+"' AND  '"+date_fin+"') AND ((C.etat_cheque IS NULL) OR (C.etat_cheque = 0)) AND (C.local_nom = '"+ParametreSystem.NomLocalPC+"') AND (T.id_factureclt = F.idfactureclt) AND (C.id_traite = T.id_traite)"
                + "  ORDER BY date_cheque ASC ;";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
               String id_cheque = resultat.getString("id_chequeclt");
                String numero = resultat.getString("numero");
                String banque = resultat.getString("banque");
                String montant = resultat.getString("montant");
                String date_cheque = resultat.getString("date_cheque");
                
                //0 Personne  1 Entreprise
                Integer Typeclt = resultat.getInt("typeclt");
                String TypeClient = "";  String Nom = ""; String Prenom = "";
                if(Typeclt == 0){
                    TypeClient = "Personne";//ancien client
                    Nom = resultat.getString("nom");
                    Prenom = resultat.getString("prenom");
                }if(Typeclt == 1){
                    TypeClient = "Entreprise";
                    Nom = resultat.getString("nomentrepise");
                    Prenom = resultat.getString("matricule_fiscale");
                }if(Typeclt == 2){
                    TypeClient = "Passageur";
                    Nom = resultat.getString("nom_pass");
                    Prenom = resultat.getString("prenom_pass");
                }
                
                // 0 ==> Cheque (table cheque_clt)
                // 1 ==> Traite (table traite_paiement_cheque_clt)
                Integer source = resultat.getInt("source_cheque");
                
                DataListChequeClt.add(new ChequeCltGestionListe(id_cheque, numero, banque, date_cheque, montant, "", TypeClient, Nom, Prenom, "", "", source, false));
            }
        } catch(SQLException ex){
            System.err.println("GetDataChequeAverser Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : GetDataChequeAverser :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return DataListChequeClt;   
    }
    
    public int UpdateChequeCltVerser(List<String> AryChequeClt, List<String> AryChequePaiementTraite)
    {
        int NbrUpdate = 0;
        if(AryChequeClt.size()>0)
        {
            String StrChequeClt =  String.join(",", AryChequeClt);
            String QueryUpdate1 = "UPDATE cheque_clt SET etat_cheque = '1' WHERE (id_chequeclt IN ("+StrChequeClt+") ) and (local_nom = '"+ParametreSystem.NomLocalPC+"');";
            try {
                NbrUpdate += h2jdbc.InsertUpdateDeleteInTable(QueryUpdate1);
            }catch(SQLException ex){
                System.err.println("Erreur SQL ! \n"+ex.getMessage());
                System.out.println(QueryUpdate1);
                if(ParametreSystem.ErrorShowLogger)
                {
                    logger.error("ChequeCltDB  : UpdateChequeCltVerser :" + ex.getMessage());
                }
                if(ParametreSystem.ErrorShowDialog)
                {
                    ShowDialog.setTitre("Erreur SQL !");
                    ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                    SD.ShowAletDialog();
                }
            }
            String QueryInsert1 = "INSERT INTO historique_cheque_clt (etat_cheque, date, id_chequeclt, local_nom, id_profile) VALUES ";
            for (String IdCheque : AryChequeClt) {
                QueryInsert1 += " ('1', now(), '"+IdCheque+"', '"+ParametreSystem.NomLocalPC+"', '"+User.idprofile+"'),";    
            }
            QueryInsert1 = QueryInsert1.substring(0, QueryInsert1.length()-1) + ";";
            try {
                h2jdbc.InsertUpdateDeleteInTable(QueryInsert1);
            }catch(SQLException ex){
                System.err.println("UpdateChequeCltVerser Erreur SQL ! \n"+ex.getMessage());
                System.out.println(QueryInsert1);
                if(ParametreSystem.ErrorShowLogger)
                {
                    logger.error("ChequeCltDB  : UpdateChequeCltVerser :" + ex.getMessage());
                }
                if(ParametreSystem.ErrorShowDialog)
                {
                    ShowDialog.setTitre("Erreur SQL !");
                    ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                    SD.ShowAletDialog();
                }
            }
        }
        if(AryChequePaiementTraite.size()>0){
            String StrPaiementTraite =  String.join(",", AryChequePaiementTraite);
            String QueryUpdate2 = "UPDATE traite_paiement_cheque_clt SET etat_cheque = '1' WHERE (id_traite_paiement_cheque IN ("+StrPaiementTraite+") ) and (local_nom = '"+ParametreSystem.NomLocalPC+"');";
            try {
                NbrUpdate += h2jdbc.InsertUpdateDeleteInTable(QueryUpdate2);
            }catch(SQLException ex){
                System.err.println("UpdateChequeCltVerser Erreur SQL ! \n"+ex.getMessage());
                System.out.println(QueryUpdate2);
                if(ParametreSystem.ErrorShowLogger)
                {
                    logger.error("ChequeCltDB  : UpdateChequeCltVerser :" + ex.getMessage());
                }
                if(ParametreSystem.ErrorShowDialog)
                {
                    ShowDialog.setTitre("Erreur SQL !");
                    ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                    SD.ShowAletDialog();
                }
            }
            String QueryInsert2 = "INSERT INTO historique_cheque_clt (etat_cheque, date, id_traite_paiement_cheque, local_nom, id_profile) VALUES ";
            for (String IdCheque : AryChequePaiementTraite) {
                QueryInsert2 += " ('1', now(), '"+IdCheque+"', '"+ParametreSystem.NomLocalPC+"', '"+User.idprofile+"'),";    
            }
            QueryInsert2 = QueryInsert2.substring(0, QueryInsert2.length()-1) + ";";
            try {
                h2jdbc.InsertUpdateDeleteInTable(QueryInsert2);
            }catch(SQLException ex){
                System.err.println("UpdateChequeCltVerser Erreur SQL ! \n"+ex.getMessage());
                System.out.println(QueryInsert2);
                if(ParametreSystem.ErrorShowLogger)
                {
                    logger.error("ChequeCltDB  : UpdateChequeCltVerser :" + ex.getMessage());
                }
                if(ParametreSystem.ErrorShowDialog)
                {
                    ShowDialog.setTitre("Erreur SQL !");
                    ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                    SD.ShowAletDialog();
                }
            }
        }
        return NbrUpdate;
    }

    
    public ObservableList SearchChequeCltProvision(Integer debut, Integer nbr, String DateDebut,  String Local, String Banque, String CodeClt, String CINClt, String NomPrenomClt, boolean All)
    {
        String WhereReq1 = " WHERE  (C.etat_cheque = 0)  AND (C.id_factureclt = F.idfactureclt) AND ";
        String WhereReq2 = " WHERE  (C.etat_cheque = 0) AND (C.local_nom = F.local_nom) AND (T.id_factureclt = F.idfactureclt) AND (C.id_traite = T.id_traite) AND ";
        
        if(!clt.isStringNull(Local)){
            WhereReq1 += " (C.local_nom = '"+Local+"') AND ";
            WhereReq2 += " (C.local_nom = '"+Local+"') AND ";
        }
        
        if(!clt.isStringNull(DateDebut)){
            WhereReq1 += " (CAST (C.date_paiement AS DATE) >= '"+DateDebut+"') AND ";
            WhereReq2 += " (CAST(C.date_payer AS DATE) >= '"+DateDebut+"') AND ";
        }
        
        if(!clt.isNumericNotnull(CodeClt)){
            WhereReq1 += " ( (O.codeclient = '"+CodeClt+"') OR (P.idcltpassageur = '"+CodeClt+"') OR (E.idcliententreprise = '"+CodeClt+"' ) ) AND ";
            WhereReq2 += " ( (O.codeclient = '"+CodeClt+"') OR (P.idcltpassageur = '"+CodeClt+"') OR (E.idcliententreprise = '"+CodeClt+"' ) ) AND ";
        }
        
        if(!clt.isNumericNotnull(CINClt)){
            WhereReq1 += " ( (O.cin LIKE '%"+CINClt+"%') OR (E.matricule_fiscale LIKE '%"+CINClt+"%') ) AND ";
            WhereReq2 += " ( (O.cin LIKE '%"+CINClt+"%') OR (E.matricule_fiscale LIKE '%"+CINClt+"%') ) AND ";
        }
        
        if(!clt.isStringNull(NomPrenomClt)){
            WhereReq1 += " ( (O.nom LIKE '%"+NomPrenomClt+"%') OR (O.prenom LIKE '%"+NomPrenomClt+"%') OR (E.nomentrepise LIKE '%"+NomPrenomClt+"%')  OR (P.prenom LIKE '%"+NomPrenomClt+"%') OR (P.nom  LIKE '%"+NomPrenomClt+"%') ) AND ";
            WhereReq2 += " ( (O.nom LIKE '%"+NomPrenomClt+"%') OR (O.prenom LIKE '%"+NomPrenomClt+"%') OR (E.nomentrepise LIKE '%"+NomPrenomClt+"%')  OR (P.prenom LIKE '%"+NomPrenomClt+"%') OR (P.nom  LIKE '%"+NomPrenomClt+"%') ) AND ";
        }
        
        WhereReq1 = WhereReq1.substring(0, WhereReq1.length() - 4);
        WhereReq2 = WhereReq2.substring(0, WhereReq2.length() - 4);
       
       
        String Limit = "";
        if(All == false){
            Limit = " LIMIT "+debut+" , "+nbr+" ; ";
        }
        
        String req= "SELECT C.id_chequeclt, '0' AS source_cheque, C.numero, C.banque, C.montant,  C.etat_cheque, to_char(C.date_paiement, 'dd/mm/yyyy') AS date_cheque, C.id_factureclt, F.typeclt, O.codeclient, O.nom, O.prenom, P.idcltpassageur , P.nom AS nom_pass, P.prenom AS prenom_pass, E.idcliententreprise, E.nomentrepise, E.matricule_fiscale, F.local_nom "
                + "  FROM cheque_clt C, factureclt F "
                + "  LEFT JOIN client O ON (O.local_nom = F.local_nom) AND (F.idcltexiste = O.codeclient) "
                + "  LEFT JOIN cltpassageur P ON (P.local_nom = F.local_nom) AND (F.idcltpassageur = P.idcltpassageur) "
                + "  LEFT JOIN cltentreprise E ON (E.local_nom= F.local_nom) AND (F.idcltentreprise = E.idcliententreprise) "
                + WhereReq1
                + "  UNION ALL "   
                + "  SELECT C.id_traite_paiement_cheque AS id_chequeclt, '1' AS source_cheque, C.numero, C.banque, C.montant_payer AS montant,  C.etat_cheque, to_char(C.date_payer, 'dd/mm/yyyy') AS date_cheque, T.id_factureclt, F.typeclt, O.codeclient, O.nom, O.prenom, P.idcltpassageur , P.nom AS nom_pass, P.prenom AS prenom_pass, E.idcliententreprise, E.nomentrepise, E.matricule_fiscale, F.local_nom "
                + "  FROM traite_paiement_cheque_clt C, traite_clt T, factureclt F  "
                + "  LEFT JOIN client O ON (O.local_nom = F.local_nom) AND (F.idcltexiste = O.codeclient) "
                + "  LEFT JOIN cltpassageur P ON (P.local_nom = F.local_nom) AND (F.idcltpassageur = P.idcltpassageur) "
                + "  LEFT JOIN cltentreprise E ON (E.local_nom = F.local_nom) AND (F.idcltentreprise = E.idcliententreprise) "
                + WhereReq2
                + Limit ;
                
        
        ObservableList<ChequeCltGestionListe> DataListChequeClt = FXCollections.observableArrayList(); 
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                String id_cheque = resultat.getString("id_chequeclt");
                String numero = resultat.getString("numero");
                String banque = resultat.getString("banque");
                String montant = Adaptateur.StringToStringEspace(resultat.getString("montant"));
                String date_cheque = resultat.getString("date_cheque");
                
                //0 Personne  1 Entreprise
                Integer Typeclt = resultat.getInt("typeclt");
                String TypeClient = "";  String Nom = ""; String Prenom = "";
                if(Typeclt == 0){
                    TypeClient = "Personne";//ancien client
                    Nom = resultat.getString("nom");
                    Prenom = resultat.getString("prenom");
                }if(Typeclt == 1){
                    TypeClient = "Entreprise";
                    Nom = resultat.getString("nomentrepise");
                    Prenom = resultat.getString("matricule_fiscale");
                }if(Typeclt == 2){
                    TypeClient = "Passageur";
                    Nom = resultat.getString("nom_pass");
                    Prenom = resultat.getString("prenom_pass");
                }
                
                // 0 ==> Cheque (table cheque_clt)
                // 1 ==> Traite (table traite_paiement_cheque_clt)
                Integer source = resultat.getInt("source_cheque");
                
                String local = resultat.getString("local_nom");
                
                DataListChequeClt.add(new ChequeCltGestionListe(id_cheque, numero, banque, date_cheque, montant, "", TypeClient, Nom, Prenom, "", local, source, false));
            }
        } catch(SQLException ex){
            System.err.println("SearchChequeCltProvision Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : SearchChequeCltProvision :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return DataListChequeClt;   
    }
    
    
    public Integer nbrChequeCltProvision(String DateDebut,  String Local, String Banque, String CodeClt, String CINClt, String NomPrenomClt)
    {
        String WhereReq1 = " WHERE  (C.etat_cheque = 0)  AND (C.id_factureclt = F.idfactureclt) AND ";
        String WhereReq2 = " WHERE  (C.etat_cheque = 0) AND (C.local_nom = F.local_nom) AND (T.id_factureclt = F.idfactureclt) AND (C.id_traite = T.id_traite) AND ";
        
        if(!clt.isStringNull(Local)){
            WhereReq1 += " (C.local_nom = '"+Local+"') AND ";
            WhereReq2 += " (C.local_nom = '"+Local+"') AND ";
        }
        
        if(!clt.isStringNull(DateDebut)){
            WhereReq1 += " (CAST(C.date_paiement AS DATE) >= '"+DateDebut+"') AND ";
            WhereReq2 += " (CAST(C.date_payer AS DATE) >= '"+DateDebut+"') AND ";
        }
        
        if(!clt.isNumericNotnull(CodeClt)){
            WhereReq1 += " ( (O.codeclient = '"+CodeClt+"') OR (P.idcltpassageur = '"+CodeClt+"') OR (E.idcliententreprise = '"+CodeClt+"' ) ) AND ";
            WhereReq2 += " ( (O.codeclient = '"+CodeClt+"') OR (P.idcltpassageur = '"+CodeClt+"') OR (E.idcliententreprise = '"+CodeClt+"' ) ) AND ";
        }
        
        if(!clt.isNumericNotnull(CINClt)){
            WhereReq1 += " ( (O.cin LIKE '%"+CINClt+"%') OR (E.matricule_fiscale LIKE '%"+CINClt+"%') ) AND ";
            WhereReq2 += " ( (O.cin LIKE '%"+CINClt+"%') OR (E.matricule_fiscale LIKE '%"+CINClt+"%') ) AND ";
        }
        
        if(!clt.isStringNull(NomPrenomClt)){
            WhereReq1 += " ( (O.nom LIKE '%"+NomPrenomClt+"%') OR (O.prenom LIKE '%"+NomPrenomClt+"%') OR (E.nomentrepise LIKE '%"+NomPrenomClt+"%')  OR (P.prenom LIKE '%"+NomPrenomClt+"%') OR (P.nom  LIKE '%"+NomPrenomClt+"%') ) AND ";
            WhereReq2 += " ( (O.nom LIKE '%"+NomPrenomClt+"%') OR (O.prenom LIKE '%"+NomPrenomClt+"%') OR (E.nomentrepise LIKE '%"+NomPrenomClt+"%')  OR (P.prenom LIKE '%"+NomPrenomClt+"%') OR (P.nom  LIKE '%"+NomPrenomClt+"%') ) AND ";
        }
        
        WhereReq1 = WhereReq1.substring(0, WhereReq1.length() - 4);
        WhereReq2 = WhereReq2.substring(0, WhereReq2.length() - 4);
       
        String req1 = "SELECT COUNT(C.id_chequeclt) "
                + "  FROM cheque_clt C, factureclt F "
                + "  LEFT JOIN client O ON (O.local_nom= F.local_nom) AND (F.idcltexiste = O.codeclient) "
                + "  LEFT JOIN cltpassageur P ON (P.local_nom = F.local_nom) AND (F.idcltpassageur = P.idcltpassageur) "
                + "  LEFT JOIN cltentreprise E ON (E.local_nom= F.local_nom) AND (F.idcltentreprise = E.idcliententreprise) "
                + WhereReq1
                + " ; ";
        
         String req2 = "  SELECT COUNT(C.id_traite_paiement_cheque) "
                + "  FROM traite_paiement_cheque_clt C, traite_clt T, factureclt F  "
                + "  LEFT JOIN client O ON (O.local_nom = F.local_nom) AND (F.idcltexiste = O.codeclient) "
                + "  LEFT JOIN cltpassageur P ON (P.local_nom = F.local_nom) AND (F.idcltpassageur = P.idcltpassageur) "
                + "  LEFT JOIN cltentreprise E ON (E.local_nom = F.local_nom) AND (F.idcltentreprise = E.idcliententreprise) "
                + WhereReq2
                + " ; ";
        
        int sommme1 = 0;
        int sommme2 = 0;
        try {
            ResultSet resultat1 = h2jdbc.selectFromDB(req1);
            resultat1.next();
            sommme1 = resultat1.getInt(1);
        } catch(SQLException ex){
            System.err.println("nbrChequeCltProvision Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req1);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : nbrChequeCltProvision :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        try {
            ResultSet resultat2 = h2jdbc.selectFromDB(req2);
            resultat2.next();
            sommme2 = resultat2.getInt(1);
        } catch(SQLException ex){
            System.err.println("nbrChequeCltProvision Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req2);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : nbrChequeCltProvision :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return sommme1 + sommme2 ;
    }
    
    
    public ChequeClt getChequeClt(String IdCheque){
        
        ChequeClt Cheque = new ChequeClt();
        
        String req = "SELECT * FROM cheque_clt WHERE id_chequeclt =  "+IdCheque+";";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                Cheque.setCodeChequeClt(resultat.getString("id_chequeclt"));
                Cheque.setNumeroChequeClt(resultat.getString("numero"));
                Cheque.setBanqueChequeClt(resultat.getString("banque"));
                Cheque.setMontantChequeClt(resultat.getString("montant"));
                Cheque.setMontantFloatChequeClt(resultat.getFloat("montant"));
                Cheque.setNomCompletChequeClt(resultat.getString("nom_complet"));
                Cheque.setLastEtat(resultat.getInt("etat_cheque"));
                Cheque.setDatePaiement(resultat.getString("date_paiement"));
                Cheque.setDateCreation(resultat.getString("date_creation"));
                Cheque.setLocalNom(resultat.getString("local_nom"));
                Cheque.setProfile(new ProfileDB().getpro(resultat.getInt("id_profile")));
            }
        } catch(SQLException ex){
            System.err.println("getChequeClt Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ChequeCltDB  : getChequeClt :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return Cheque;
    }
    
}
