package Models.DevisClt;

import Controllers.Dialog.ShowDialog;
import Models.Produit.ListeProduit;
import Controllers.Traitement.Adaptateur;
import Controllers.Traitement.ParametreSystem;
import Controllers.Traitement.contro;
import Models.Client.Client;
import Models.Client.ClientDB;
import Models.Client.ClientEntreprise;
import Models.Client.ClientEntrepriseDB;
import Models.Client.ClientPassager;
import Models.Client.ClientPassagerDB;
import Models.H2JDBC;
import Models.User.User;
import org.apache.log4j.Logger;
import java.sql.ResultSet;
import java.sql.SQLException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 * @author Maher Ben Tili
 */
public class DevisDB {
    
    H2JDBC h2jdbc = new H2JDBC();
    ShowDialog SD = new ShowDialog();
    Logger logger = Logger.getLogger(DevisDB.class.getName());
    
    contro clt = new contro();
    
    public String AddDevis(Devis devis)
    {
        String codedevis = null ;
        
        String datecreation = Adaptateur.DatabaseDateFormat(devis.getDateCreation());
        String codeclient = devis.getCodeClient();
        String datefinvalidite =Adaptateur.DatabaseDateFormat(devis.getValidite());
        String totalhorstaxnet =devis.getTotalHorsTaxNet().replace(",", ".");
        String totaltva = devis.getTotalTVA().replace(",", ".");
        String netapayer = devis.getNetAPayer().replace(",", ".");
        String remise = devis.getRemise().replace(",", ".");
        
        String sql = "";
        
        if(devis.getTypeClient()==0){
          sql ="INSERT INTO devisclt (date_creation, fin_validite, typeclt, type_reglement, codeclient, total_h_t_net, total_tva, remise, timbre_loi, net_a_payer, idprofile, local_nom) VALUES ('"+datecreation+"', '"+datefinvalidite+"', '0', '"+devis.getTypeRegement()+"', '"+codeclient+"',  "+totalhorstaxnet+", "+totaltva+",  "+remise+", '"+ParametreSystem.Timbre+"', '"+netapayer+"', "+User.idprofile+", '"+ParametreSystem.NomLocalPC+"');";
        }
        if(devis.getTypeClient()==1){
          sql ="INSERT INTO devisclt (date_creation, fin_validite, typeclt, type_reglement, idcliententreprise, total_h_t_net, total_tva, remise,  timbre_loi, net_a_payer, idprofile, local_nom) VALUES ('"+datecreation+"', '"+datefinvalidite+"', '1', '"+devis.getTypeRegement()+"', '"+codeclient+"',  "+totalhorstaxnet+", "+totaltva+",  "+remise+",  '"+ParametreSystem.Timbre+"', "+netapayer+", "+User.idprofile+", '"+ParametreSystem.NomLocalPC+"');";
        }
        if(devis.getTypeClient()==2){
            //insertion de passager
            ClientPassager CltPassager = new ClientPassagerDB().addPassager(devis.getPassager());
            if(!CltPassager.getCodeclient().isEmpty()){
                sql ="INSERT INTO devisclt (date_creation, fin_validite, typeclt, type_reglement, idcltpassageur, total_h_t_net, total_tva, remise,  timbre_loi, net_a_payer, idprofile, local_nom) VALUES ('"+datecreation+"', '"+datefinvalidite+"', '2', '"+devis.getTypeRegement()+"', '"+CltPassager.getCodeclient()+"',  '"+totalhorstaxnet+"', '"+totaltva+"', '"+remise+"', '"+ParametreSystem.Timbre+"', '"+netapayer+"', '"+User.idprofile+"', '"+ParametreSystem.NomLocalPC+"');";
            }
        }
        
        try {
            codedevis = h2jdbc.InsertGetLastId(sql);
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(sql);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("DevisDB  : AddDevis :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("AddDevis : Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        
        if(codedevis != null)
        {
            String reqListeProduit = "";
            String PrixHT ;
            String Remise ;
            String TotalHT ;
            String TVA ;
            String TotalTTC ;
            try {
                for(ListeProduit listeprod : devis.getListeproduit()){
                  PrixHT = listeprod.getPrixHT().replace(",",".");
                  Remise= listeprod.getRemise().replace("%","").replace(",",".");
                  TotalHT= listeprod.getTotalHT().replace(",",".");
                  TVA=listeprod.getTVA().replace("%","").replace(",",".");
                  TotalTTC= listeprod.getTotalTTC().replace(",","."); 

                  reqListeProduit = "INSERT INTO list_produit_devisclt (iddevis_devis, reference_produit, designation_produit, quantie, p_u_h_t, remise, p_u_h_t_net, tva, montant, local_nom) VALUES ('"+codedevis+"', '"+listeprod.getReference()+"', '"+listeprod.getDesignation()+"', "+listeprod.getQuantite()+", "+PrixHT+", "+Remise+", "+TotalHT+", "+TVA+", "+TotalTTC+", '"+ParametreSystem.NomLocalPC+"');";
                  h2jdbc.InsertUpdateDeleteInTable(reqListeProduit);
                }
            }catch(SQLException ex){
                System.err.println("AddDevis Erreur SQL ! \n"+ex.getMessage());
                System.out.println(reqListeProduit);
                if(ParametreSystem.ErrorShowLogger)
                {
                    logger.error("DevisDB  : AddDevis :" + ex.getMessage());
                }
                if(ParametreSystem.ErrorShowDialog)
                {
                    ShowDialog.setTitre("Erreur SQL !");
                    ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                    SD.ShowAletDialog();
                }
            }
        }
        return codedevis ;
    }
    
    public Devis getDevis(String codedevis)
    {
        Devis devis = new Devis();
        
        String req = "SELECT iddevis, date_creation, to_char(fin_validite, 'dd/mm/yyyy') as fin_validite , type_reglement, typeclt, codeclient, idcliententreprise, idcltpassageur, total_h_t_net, total_tva, timbre_loi, net_a_payer, idprofile, remise FROM devisclt WHERE iddevis = '"+codedevis+"' AND (local_nom = '"+ParametreSystem.NomLocalPC+"'); ";
        
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                devis.setCodeDevis(resultat.getString("iddevis"));
                devis.setTypeRegement(resultat.getString("type_reglement"));
                devis.setDateCreation(resultat.getString("date_creation"));
                devis.setValidite(resultat.getString("fin_validite"));
                devis.setNetAPayer(resultat.getString("net_a_payer"));
                devis.setTotalHorsTaxNet(resultat.getString("total_h_t_net"));
                devis.setTotalTVA(resultat.getString("total_tva"));
                devis.setTimbre(resultat.getString("timbre_loi"));
                devis.setCodeUser(resultat.getInt("idprofile"));
                devis.setTypeClient(resultat.getInt("typeclt"));
                devis.setRemise(resultat.getString("remise"));
                
                if(devis.getTypeClient()==0){
                    Client client = new ClientDB().getClient(resultat.getString("codeclient"));
                    devis.setClient(client);
                    devis.setCodeClient(resultat.getString("codeclient"));
                }else if(devis.getTypeClient()==1){
                    ClientEntreprise entreprise = new ClientEntrepriseDB().GetClientEtr(resultat.getString("idcliententreprise"));
                    devis.setEntreprise(entreprise);
                    devis.setCodeClient(resultat.getString("idcliententreprise"));
                }else if(devis.getTypeClient()==2){
                    ClientPassager passager= new ClientPassagerDB().getPassager(resultat.getString("idcltpassageur"));
                    devis.setPassager(passager);
                    devis.setCodeClient(resultat.getString("idcltpassageur"));
                }
                
                ObservableList<ListeProduit> dataProdDev=FXCollections.observableArrayList();
                String reqprod = "SELECT * FROM list_produit_devisclt where iddevis_devis = "+codedevis+" AND (local_nom = '"+ParametreSystem.NomLocalPC+"');";
                try {
                    ResultSet resulist = h2jdbc.selectFromDB(reqprod);
                    while (resulist.next()){
                        String reference = resulist.getString("reference_produit");
                        String designaton = resulist.getString("designation_produit");
                        String quantite=resulist.getString("quantie");
                        String PrixHT =resulist.getString("p_u_h_t");
                        String Remise =resulist.getString("remise");
                        String TotalHT =resulist.getString("p_u_h_t_net");
                        String TVA =resulist.getString("tva");
                        String TotalTTC =resulist.getString("montant");
                        dataProdDev.add(new ListeProduit(reference, designaton, quantite, PrixHT, Remise, TotalHT, TVA, TotalTTC));
                    }
                    devis.setListeproduit(dataProdDev);
                } catch(SQLException ex){
                    System.err.println("Erreur SQL ! \n"+ex.getMessage());
                    System.out.println(reqprod);
                    if(ParametreSystem.ErrorShowLogger)
                    {
                        logger.error("DevisDB  : getDevis : " + ex.getMessage());
                    }
                    if(ParametreSystem.ErrorShowDialog)
                    {
                        ShowDialog.setTitre("Erreur SQL !");
                        ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                        SD.ShowAletDialog();
                    }
                }
            }
        } catch(SQLException ex){
            System.err.println("getDevis Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("DevisDB  : getDevis :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return devis ;
    }
    
    
    
    
    /********************************** Initial *******************************/
    public ObservableList<DevisCltListe> SearchDevisCltGestion(Integer debut, Integer nbr, String CodeDevis, String CodeClient, String Reglement, String DateCreation, String NameClient, String CinClient)
    {
        ObservableList<DevisCltListe> data = FXCollections.observableArrayList();
        
        String Where = "";
        
        if(!clt.isNumericNotnull(CodeDevis)){
            Where += " (D.iddevis LIKE '%"+Adaptateur.addSlashes(CodeDevis)+"%') AND ";
        }
        
        if(!clt.isNumericNotnull(CodeClient)){
           Where += " ( (D.idcliententreprise = '"+CodeClient+"') OR (D.codeclient = '"+CodeClient+"') OR (D.idcltpassageur = '"+CodeClient+"') ) AND ";
        }
        
        if(!clt.isStringNull(Reglement)){
            Where += " (D.type_reglement = '"+Reglement+"') AND ";
        }
        
        if(!clt.isStringNull(DateCreation)){
            Where += " (CAST(D.date_creation AS DATE) =  '"+DateCreation+"') AND ";
        }
        
        String LeftJoin = "";
        if(!clt.isStringNull(NameClient) || !clt.isNumericNotnull(CinClient) ){
            LeftJoin =  " LEFT JOIN client C ON (C.codeclient = D.codeclient) AND (D.typeclt = 0) "
                        +" LEFT JOIN cltentreprise E ON (E.idcliententreprise = D.idcliententreprise ) AND (D.typeclt = 1) "
                        +" LEFT JOIN cltpassageur P ON (P.idcltpassageur = D.idcltpassageur) AND (D.typeclt = 2) ";
            
            if(!clt.isStringNull(NameClient)){
                NameClient = Adaptateur.addSlashes(NameClient);
                Where += "( ((C.nom LIKE '%"+NameClient+"%') OR (C.prenom LIKE '%"+NameClient+"%') OR  (C.nomcomplet LIKE '%"+NameClient+"%'))  OR  (E.nomentrepise LIKE '%"+NameClient+"%') OR  (P.nom LIKE '%"+NameClient+"%') ) AND";
            }
            
            if(!clt.isNumericNotnull(CinClient)){
                CinClient = Adaptateur.addSlashes(CinClient);
                Where += "( (C.cin = '"+CinClient+"') OR (E.matricule_fiscale = '"+CinClient+"') ) AND";
            }
        }
        
        if(!Where.equals("")){
            Where = "WHERE  " + Where + " (D.local_nom = '"+ParametreSystem.NomLocalPC+"') ";
        }else{
            Where = "WHERE (D.local_nom = '"+ParametreSystem.NomLocalPC+"') ";
        }
        
        String req="SELECT D.iddevis, to_char(D.date_creation, 'dd/mm/yyyy') as date_creation , to_char(D.fin_validite, 'dd/mm/yyyy') as fin_validite , D.type_reglement, D.codeclient, D.typeclt, D.idcliententreprise, D.idcltpassageur, D.net_a_payer "
                + "FROM devisclt D "
                + LeftJoin
                + Where
                + "ORDER BY D.date_creation DESC "
                + "LIMIT "+debut+" , "+nbr+" ;";
        
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next())
            {
                String code_devis = resultat.getString("iddevis");
                        
                String type_client = "";
                String code_client = "";
                if(resultat.getString("typeclt").equals("0")){
                    type_client ="Exist Client";
                    code_client = resultat.getString("codeclient");
                }else if(resultat.getString("typeclt").equals("1")){
                    type_client ="Entreprise";
                    code_client = resultat.getString("idcliententreprise");
                }else if(resultat.getString("typeclt").equals("2")){
                    type_client ="Passager";
                    code_client = resultat.getString("idcltpassageur");
                }
                
                String total_devis =resultat.getString("net_a_payer");
                
                String mode_reglement = ""; //Mode Paiement
                if(resultat.getString("type_reglement").equals("0")){
                    mode_reglement = "Comptant";
                }else if(resultat.getString("type_reglement").equals("1")){
                    mode_reglement = "Facilité";
                }
                
                String date_creation =resultat.getString("date_creation");
                String date_fin_validite =resultat.getString("fin_validite");
                
                data.add(new DevisCltListe(code_devis, type_client, code_client, mode_reglement, total_devis, date_creation, date_fin_validite));
            }
        } catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("DevisDB  : SearchDevisCltGestion :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        
        return data ;
    }
    
    public Integer nbrDevisCltGestion(String CodeDevis, String CodeClient, String Reglement, String DateCreation, String NameClient, String CinClient)
    {
        String Where = "";
        
        if(!clt.isNumericNotnull(CodeDevis)){
            Where += " (D.iddevis LIKE '%"+Adaptateur.addSlashes(CodeDevis)+"%') AND ";
        }
        
        if(!clt.isNumericNotnull(CodeClient)){
           Where += " ( (D.idcliententreprise = '"+CodeClient+"') OR (D.codeclient = '"+CodeClient+"') OR (D.idcltpassageur = '"+CodeClient+"') ) AND ";
        }
        
        if(!clt.isStringNull(Reglement)){
            Where += " (D.type_reglement = '"+Reglement+"') AND ";
        }
        
        if(!clt.isStringNull(DateCreation)){
            Where += " (CAST(D.date_creation AS DATE) =  '"+DateCreation+"') AND ";
        }
        
        String LeftJoin = "";
        if(!clt.isStringNull(NameClient) || !clt.isNumericNotnull(CinClient) ){
            LeftJoin =  " LEFT JOIN client C ON (C.codeclient = D.codeclient) AND (D.typeclt = 0) "
                        +" LEFT JOIN cltentreprise E ON (E.idcliententreprise = D.idcliententreprise ) AND (D.typeclt = 1) "
                        +" LEFT JOIN cltpassageur P ON (P.idcltpassageur = D.idcltpassageur) AND (D.typeclt = 2) ";
            
            if(!clt.isStringNull(NameClient)){
                NameClient = Adaptateur.addSlashes(NameClient);
                Where += "( ((C.nom LIKE '%"+NameClient+"%') OR (C.prenom LIKE '%"+NameClient+"%') OR  (C.nomcomplet LIKE '%"+NameClient+"%'))  OR  (E.nomentrepise LIKE '%"+NameClient+"%') OR  (P.nom LIKE '%"+NameClient+"%') ) AND";
            }
            
            if(!clt.isNumericNotnull(CinClient)){
                CinClient = Adaptateur.addSlashes(CinClient);
                Where += "( (C.cin = '"+CinClient+"') OR (E.matricule_fiscale = '"+CinClient+"') ) AND";
            }
        }
        
        if(!Where.equals("")){
            Where = "WHERE  " + Where + " (D.local_nom = '"+ParametreSystem.NomLocalPC+"') ";
        }else{
            Where = "WHERE (D.local_nom = '"+ParametreSystem.NomLocalPC+"') ";
        }
        
        String req=" SELECT COUNT(D.iddevis) " 
                    + " FROM devisclt D "
                    + LeftJoin
                    + Where+ " ;"; 
        
        int sommme =0;
        
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            resultat.next();
            sommme = resultat.getInt(1);
        } catch(SQLException ex){
            System.err.println("nbrDevisCltGestion Erreur SQL !  \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("DevisDB  : nbrDevisCltGestion :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return sommme ;
    }
    
    
    /**********************************Controller  Import *******************************/
    public ObservableList<DevisCltListe> getdataImport(String  tab,String  mot, Integer debut, Integer nbr)
    {
        ObservableList<DevisCltListe> data=FXCollections.observableArrayList();
        
        String req="SELECT DC.iddevis, to_char(DC.date_creation, 'dd/mm/yyyy') as date_creation , to_char(DC.fin_validite, 'dd/mm/yyyy') as fin_validite , DC.type_reglement, DC.codeclient, DC.typeclt, DC.idcliententreprise, DC.idcltpassageur, DC.net_a_payer FROM devisclt DC WHERE ("+tab+" LIKE '%"+mot+"%') AND (CAST(DC.fin_validite AS DATE) > now()) AND (DC.iddevis NOT IN (SELECT DISTINCT(iddevis) FROM commandeclt WHERE  (iddevis IS NOT NULL) AND (local_nom = '"+ParametreSystem.NomLocalPC+"'))) AND (DC.local_nom = '"+ParametreSystem.NomLocalPC+"') ORDER BY DC.date_creation DESC LIMIT "+debut+" , "+nbr+" ;";       
        
        if(tab.equals("DC.CodeClient")){
            req="SELECT DC.iddevis, to_char(DC.date_creation, 'dd/mm/yyyy') as date_creation , to_char(DC.fin_validite, 'dd/mm/yyyy') as fin_validite , DC.type_reglement, DC.codeclient, DC.typeclt, DC.idcliententreprise, DC.idcltpassageur, DC.net_a_payer FROM devisclt DC WHERE (CAST(DC.fin_validite AS DATE) > now()) AND (DC.iddevis NOT IN (SELECT DISTINCT(iddevis) FROM commandeclt WHERE (iddevis IS NOT NULL) AND (local_nom = '"+ParametreSystem.NomLocalPC+"'))) AND (DC.idcliententreprise LIKE '%"+mot+"%' OR DC.codeclient LIKE '%"+mot+"%' OR DC.idcltpassageur LIKE '%"+mot+"%') AND (DC.local_nom = '"+ParametreSystem.NomLocalPC+"') ORDER BY DC.date_creation DESC LIMIT "+debut+" , "+nbr+" ;";
        }
        
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                String code_devis = resultat.getString("iddevis");
                        
                String type_client = "";
                String code_client = "";
                if(resultat.getString("typeclt").equals("0")){
                    type_client ="Exist Client";
                    code_client = resultat.getString("codeclient");
                }else if(resultat.getString("typeclt").equals("1")){
                    type_client ="Entreprise";
                    code_client = resultat.getString("idcliententreprise");
                }else if(resultat.getString("typeclt").equals("2")){
                    type_client ="Passager";
                    code_client = resultat.getString("idcltpassageur");
                }
                
                String total_devis =resultat.getString("net_a_payer");
                
                String mode_reglement = ""; //Mode Paiement
                if(resultat.getString("type_reglement").equals("0")){
                    mode_reglement = "Comptant";
                }else if(resultat.getString("type_reglement").equals("1")){
                    mode_reglement = "Facilité";
                }
                
                String date_creation =resultat.getString("date_creation");
                String date_fin_validite =resultat.getString("fin_validite");
                
                data.add(new DevisCltListe(code_devis, type_client, code_client, mode_reglement, total_devis, date_creation, date_fin_validite));
            }
        } catch(SQLException ex){
            System.err.println("getdataImport Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("DevisDB  : getdataImport :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return data ;
    }
    
    public String nbrDevisCltImport(String  tab, String mot)
    {
        String req = "SELECT count(DC.iddevis) FROM devisclt DC, commandeclt CC WHERE ("+tab+" LIKE '%"+mot+"%') AND (CAST(DC.fin_validite AS DATE) > now()) AND (CC.iddevis != DC.iddevis) AND (DC.local_nom = '"+ParametreSystem.NomLocalPC+"') ;";
        
        if(tab.equals("DC.CodeClient")){
            req = "SELECT count(DC.iddevis) FROM devisclt DC, commandeclt CC WHERE (CAST(DC.fin_validite AS DATE) > now()) AND (CC.iddevis != DC.iddevis) AND (DC.idcliententreprise LIKE '%"+mot+"%' OR DC.codeclient LIKE '%"+mot+"%' OR DC.idcltpassageur LIKE '%"+mot+"%') AND (DC.local_nom = '"+ParametreSystem.NomLocalPC+"') ;";
        }
        
        String sommme = "0";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            resultat.next();
            sommme = resultat.getString(1);
        } catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("DevisDB  :  :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return sommme;
    }
    
    
    public ObservableList<Devis> getAllDevis()
    {
        ObservableList<Devis> data =FXCollections.observableArrayList();
        
        String req = "SELECT iddevis, date_creation, to_char(fin_validite, 'dd/mm/yyyy') as fin_validite , type_reglement, typeclt, codeclient, idcliententreprise, idcltpassageur, total_h_t_net, total_tva, timbre_loi, net_a_payer, idprofile, remise FROM devisclt ; ";
        
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                Devis devis = new Devis();
                devis.setCodeDevis(resultat.getString("iddevis"));
                devis.setTypeRegement(resultat.getString("type_reglement"));
                devis.setDateCreation(resultat.getString("date_creation"));
                devis.setValidite(resultat.getString("fin_validite"));
                devis.setNetAPayer(resultat.getString("net_a_payer"));
                devis.setTotalHorsTaxNet(resultat.getString("total_h_t_net"));
                devis.setTotalTVA(resultat.getString("total_tva"));
                devis.setTimbre(resultat.getString("timbre_loi"));
                devis.setCodeUser(resultat.getInt("idprofile"));
                devis.setTypeClient(resultat.getInt("typeclt"));
                devis.setRemise(resultat.getString("remise"));
                
                if(devis.getTypeClient()==0){
                    Client client = new ClientDB().getClient(resultat.getString("codeclient"));
                    devis.setClient(client);
                    devis.setCodeClient(resultat.getString("codeclient"));
                }else if(devis.getTypeClient()==1){
                    ClientEntreprise entreprise = new ClientEntrepriseDB().GetClientEtr(resultat.getString("idcliententreprise"));
                    devis.setEntreprise(entreprise);
                    devis.setCodeClient(resultat.getString("idcliententreprise"));
                }else if(devis.getTypeClient()==2){
                    ClientPassager passager= new ClientPassagerDB().getPassager(resultat.getString("idcltpassageur"));
                    devis.setPassager(passager);
                    devis.setCodeClient(resultat.getString("idcltpassageur"));
                }
                
                ObservableList<ListeProduit> dataProdDev=FXCollections.observableArrayList();
                String reqprod = "SELECT * FROM list_produit_devisclt where iddevis_devis = "+resultat.getString("iddevis")+" AND (local_nom = '"+ParametreSystem.NomLocalPC+"');";
                try {
                    ResultSet resulist = h2jdbc.selectFromDB(reqprod);
                    while (resulist.next()){
                        String reference = resulist.getString("reference_produit");
                        String designaton = resulist.getString("designation_produit");
                        String quantite=resulist.getString("quantie");
                        String PrixHT =resulist.getString("p_u_h_t");
                        String Remise =resulist.getString("remise");
                        String TotalHT =resulist.getString("p_u_h_t_net");
                        String TVA =resulist.getString("tva");
                        String TotalTTC =resulist.getString("montant");
                        dataProdDev.add(new ListeProduit(reference, designaton, quantite, PrixHT, Remise, TotalHT, TVA, TotalTTC));
                    }
                    devis.setListeproduit(dataProdDev);
                } catch(SQLException ex){
                    System.err.println("Erreur SQL ! \n"+ex.getMessage());
                    System.out.println(reqprod);
                    if(ParametreSystem.ErrorShowLogger)
                    {
                        logger.error("DevisDB  : getAllDevis :" + ex.getMessage());
                    }
                }
                data.add(devis);
            }
        } catch(SQLException ex){
            System.err.println("getDevis Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("DevisDB  : getAllDevis :" + ex.getMessage());
            }
        }
        return data ;
    }
    
}
