package Models.FactureFrs;

import Controllers.Traitement.Adaptateur;
import static Controllers.Traitement.Adaptateur.StringToFloat;
import Controllers.Traitement.ParametreSystem;
import Controllers.Traitement.contro;
import Models.BonReceptionFrs.BonReceptionDB;
import Models.BonReceptionFrs.BonReceptionFrs;
import Models.Fournisseur.Fournisseur;
import Models.Fournisseur.FournisseurDB;
import Models.Produit.ListeProduit;
import Models.Stock.StockDetail;
import Models.Stock.StockDetailDB;
import Models.User.Profile;
import Models.User.ProfileDB;
import Models.User.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import Controllers.Dialog.ShowDialog;
import org.apache.log4j.Logger;
import Models.H2JDBC;

/**
 * @author Maher Ben Tili
 */
public class FactureFrsDB {
    
    H2JDBC h2jdbc = new H2JDBC();
    ShowDialog SD = new ShowDialog();
    Logger logger = Logger.getLogger(FactureFrsDB.class.getName());
    contro clt = new contro();
    
    public FactureFrs setFactureBRDB(FactureFrs Facture)
    {
        String Numero = Facture.getNumero();
        String DateCreation = Facture.getDateCreation();
        String HeurCreation = Facture.getHeurCreation();
        String TotalHorsTaxNet = Facture.getTotalHorsTaxNet().replace(",",".");
        String TotalRemise = Facture.getRemise().replace(",",".");
        String TotalTVA = Facture.getTotalTVA().replace(",",".");
        String NetAPayer = Facture.getNetAPayer().replace(",",".");
        String Fourisseur = Facture.getFournisseur().getCode();
        String Local = Facture.getLocal();
        
        ObservableList<ListeProduit> ListProduit = Facture.getListProduit();
        
        ObservableList<BonReceptionFrs> ListBonReception = Facture.getListeBonReception();
        
        String CommandeFrs = "null";
        if(Facture.getCommandeFrs() != null){
            CommandeFrs = "'"+Facture.getCommandeFrs().getCodeCommandeFrs()+"'";
        }
        
        //0  comptant   |     1  facilité
        String TypeReglement = "null";
        if(Facture.getTypeReglement() != null){
            TypeReglement = "'"+Facture.getTypeReglement()+"'";
        }
        
        //  0payer 	|	1en cour 	| NULL      non pays
        String EtatReglement = "null";
        if(Facture.getEtatReglement() != null){
            EtatReglement = "'"+Facture.getEtatReglement()+"'";
        }
        
        String Query = "INSERT INTO facture_frs (numero, date_creation, type_reglement, etat_reglement, bon_reception, devise, total_h_t_net, remise, total_tva, timbre_loi, net_a_payer, id_fournisseur, local_reception, id_commande_frs, id_profile)  VALUES  ('"+Numero+"', '"+DateCreation+" "+HeurCreation+"', "+TypeReglement+", "+EtatReglement+", 1, '"+ ParametreSystem.CurrencySign+"', '"+TotalHorsTaxNet+"', '"+TotalRemise+"', '"+TotalTVA+"', '"+ParametreSystem.Timbre+"', '"+NetAPayer+"', '"+Fourisseur+"', '"+Local+"', "+CommandeFrs+", '"+User.idprofile+"');";
        try {
            String IdFacture = h2jdbc.InsertGetLastId(Query);
            if(IdFacture != null)
            {
                Facture.setIdFactureFrs(IdFacture);
                String Req = "INSERT INTO list_prod_fact_frs (id_facture_frs, reference, designation_produit, quantite, p_u_h_t, remise, p_u_h_t_net, tva, montant) VALUES ";
                for(int i=0; i<ListProduit.size(); i++)
                {
                    String Reference = ListProduit.get(i).getReference();
                    if(!Reference.equals(""))
                    {
                        String Designation = Adaptateur.addSlashes(ListProduit.get(i).getDesignation());
                        String Quantite = ListProduit.get(i).getQuantite().replace(",",".");
                        String PrixUnitaireHorsTaxe = ListProduit.get(i).getPrixHT().replace(",",".");
                        String PrixUnitaireHorsTaxenNet = ListProduit.get(i).getTotalHT().replace(",",".");
                        String Remise = ListProduit.get(i).getRemise().replace(",",".").replace("%","");
                        String TVA = ListProduit.get(i).getTVA().replace(",",".").replace("%","");
                        String Montant = ListProduit.get(i).getTotalTTC().replace(",",".");
                        Req = Req + "('"+IdFacture+"', '"+Reference+"', '"+Designation+"', '"+Quantite+"', '"+PrixUnitaireHorsTaxe+"', '"+Remise+"', '"+PrixUnitaireHorsTaxenNet+"', '"+TVA+"', '"+Montant+"'),";
                    }
                }
                Req = Req.substring(0, Req.length()-1) + ";";
                int nbrListProduit = h2jdbc.InsertUpdateDeleteInTable(Req);
                if(nbrListProduit > 0)
                {
                    String ListIdBonReception = "";
                    for(int j=0; j<ListBonReception.size(); j++)
                    {
                        ListIdBonReception += ListBonReception.get(j).getIdBonReception()+",";
                    }
                    ListIdBonReception = ListIdBonReception.substring(0, ListIdBonReception.length()-1) + "";
                    
                    String QueryUpdateBonReception = "UPDATE bonreception_frs SET id_facture_frs = '"+IdFacture+"' WHERE bonreception_frs.id_bonreception IN ("+ListIdBonReception+") ; ";
                    
                    h2jdbc.InsertUpdateDeleteInTable(QueryUpdateBonReception);
                }
            }
        }catch(SQLException ex){
            System.err.println("setFactureBRDB Erreur SQL ! \n"+ex.getMessage());
            System.out.println(Query);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FactureFrsDB  : setFactureBRDB :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return Facture;
    }
    
    
    
    public FactureFrs setFactureFrsDB(FactureFrs Facture)
    {
        String Numero = Facture.getNumero();
        String DateCreation = Facture.getDateCreation();
        String HeurCreation = Facture.getHeurCreation();
        String TotalHorsTaxNet = Facture.getTotalHorsTaxNet().replace(",",".");
        String TotalRemise = Facture.getRemise().replace(",",".");
        String TotalTVA = Facture.getTotalTVA().replace(",",".");
        String NetAPayer = Facture.getNetAPayer().replace(",",".");
        String Fourisseur = Facture.getFournisseur().getCode();
        String Local = Facture.getLocal();
        
        ObservableList<ListeProduit> ListProduit = Facture.getListProduit();
        
        String CommandeFrs = "NULL";
        if(Facture.getCommandeFrs() != null){
            CommandeFrs = "'"+Facture.getCommandeFrs().getCodeCommandeFrs()+"'";
        }
        
        //0  comptant   |     1  facilité
        String TypeReglement = "NULL";
        if(Facture.getTypeReglement() != null){
            TypeReglement = "'"+Facture.getTypeReglement()+"'";
        }
        
        //  0payer 	|	1en cour 	| NULL      non pays
        String EtatReglement = "NULL";
        if(Facture.getEtatReglement() != null){
            EtatReglement = "'"+Facture.getEtatReglement()+"'";
        }
        
        String req = "INSERT INTO facture_frs (numero, date_creation, type_reglement, etat_reglement, devise, total_h_t_net, remise, total_tva, timbre_loi, net_a_payer, id_fournisseur, local_reception, id_commande_frs, id_profile)  VALUES  ('"+Numero+"', '"+DateCreation+" "+HeurCreation+"', "+TypeReglement+", "+EtatReglement+", '"+ParametreSystem.CurrencySign+"', '"+TotalHorsTaxNet+"', '"+TotalRemise+"', '"+TotalTVA+"', '"+ParametreSystem.Timbre+"', '"+NetAPayer+"', '"+Fourisseur+"', '"+Local+"', "+CommandeFrs+", '"+User.idprofile+"');";
        
        try {
            String IdFacture = h2jdbc.InsertGetLastId(req);
            if(IdFacture != null)
            {
                Facture.setIdFactureFrs(IdFacture);
                ObservableList<StockDetail> ListStockDetail=FXCollections.observableArrayList();
        
                String Req = "INSERT INTO list_prod_fact_frs (id_facture_frs, reference, designation_produit, quantite, p_u_h_t, remise, p_u_h_t_net, tva, montant) VALUES ";
                for(int i=0; i<ListProduit.size(); i++){
                    String Reference = ListProduit.get(i).getReference();
                    if(!Reference.equals("")){
                        String Designation =  Adaptateur.addSlashes(ListProduit.get(i).getDesignation());
                        String Quantite = ListProduit.get(i).getQuantite().replace(",",".");
                        String PrixUnitaireHorsTaxe = ListProduit.get(i).getPrixHT().replace(",",".");
                        String PrixUnitaireHorsTaxenNet = ListProduit.get(i).getTotalHT().replace(",",".");
                        String Remise = ListProduit.get(i).getRemise().replace(",",".").replace("%","");
                        String TVA = ListProduit.get(i).getTVA().replace(",",".").replace("%","");
                        String Montant = ListProduit.get(i).getTotalTTC().replace(",",".");
                        Req = Req + "('"+IdFacture+"', '"+Reference+"', '"+Designation+"', '"+Quantite+"', '"+PrixUnitaireHorsTaxe+"', '"+Remise+"', '"+PrixUnitaireHorsTaxenNet+"', '"+TVA+"', '"+Montant+"'),";

                        ListStockDetail.add(new StockDetail(Reference, StringToFloat(Quantite), null, null, null, null, IdFacture, null, DateCreation));
                    }
                }
                Req = Req.substring(0, Req.length()-1) + ";";
                h2jdbc.InsertGetLastId(Req);
                new StockDetailDB().InsertStockDetail(ListStockDetail);
            }
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FactureFrsDB  : setFactureFrsDB :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return Facture;
    }
    
    
    public FactureFrs getFactureFrs(String IdFactureFrs, String Numero, String IdFournisseur)
    {
        FactureFrs Facture = new FactureFrs();
        
        String req = "SELECT *, to_char(date_creation, 'dd/mm/yyyy') as date_recep, to_char(date_creation, 'hh:ii') as heur_recep FROM facture_frs WHERE id_facture_frs = "+IdFactureFrs+";";
        
        if(Numero!=null && IdFournisseur!=null){
            req = "SELECT *, to_char(date_creation, 'dd/mm/yyyy') as date_recep, to_char(date_creation, 'hh:ii') as heur_recep FROM facture_frs WHERE (numero="+Numero+") AND (id_fournisseur="+IdFournisseur+");";
        }
        
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next())
            {
                Facture.setNumero(resultat.getString("numero"));
                Facture.setIdFactureFrs(resultat.getString("id_facture_frs"));
                Facture.setHeurCreation(resultat.getString("heur_recep"));
                Facture.setDateCreation(resultat.getString("date_recep"));
                
                Facture.setTotalHorsTaxNet(resultat.getString("total_h_t_net"));
                Facture.setTotalTVA(resultat.getString("total_tva"));
                Facture.setRemise(resultat.getString("remise"));
                Facture.setNetAPayer(resultat.getString("net_a_payer"));
                Facture.setTimbre(resultat.getString("timbre_loi"));
                
                Facture.setFTotalHorsTaxNet(resultat.getFloat("total_h_t_net"));
                Facture.setFTotalTVA(resultat.getFloat("total_tva"));
                Facture.setFRemise(resultat.getFloat("remise"));
                Facture.setFNetAPayer(resultat.getFloat("net_a_payer"));
                Facture.setFTimbre(resultat.getFloat("timbre_loi"));
                
                Facture.setLocal(resultat.getString("local_reception"));
                Facture.setTypeReglement(resultat.getString("type_reglement"));
                Facture.setEtatReglement(resultat.getString("etat_reglement"));
                Facture.setDevise(resultat.getString("devise"));
                Facture.setBonReception(resultat.getString("bon_reception"));
                
                Fournisseur Frs = new FournisseurDB().GetFournisseurDB(resultat.getString("id_fournisseur"));
                Facture.setFournisseur(Frs);
                
                Profile profile = new ProfileDB().getpro(resultat.getInt("id_profile"));
                Facture.setProfile(profile);
                
                Facture.setListProduit(this.getFactureProduitList(resultat.getString("id_facture_frs")));
                if(Facture.getBonReception() != null)
                {
                    if(Facture.getBonReception().equals("1") ){
                        BonReceptionDB BonRecepDB= new BonReceptionDB();
                        ObservableList<BonReceptionFrs>  ListeBonReception = BonRecepDB.getBonReceptionByFact(resultat.getString("id_facture_frs"));
                        Facture.setListeBonReception(ListeBonReception);
                    }
                }     
            }
        } catch(SQLException ex){
            System.err.println("getFactureFrs Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FactureFrsDB  : getFactureFrs :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        
        return Facture;
    }
    
    
    public ObservableList<ListeProduit> getFactureProduitList(String IdFactureFrs)
    {
        ObservableList<ListeProduit> ListProd = FXCollections.observableArrayList();
        
        String req = "SELECT * FROM list_prod_fact_frs WHERE (id_facture_frs = "+IdFactureFrs+");";
                
        try {
            ResultSet ResultListProd = h2jdbc.selectFromDB(req);
            while (ResultListProd.next()){
                String  reference = ResultListProd.getString("reference");
                String  designation = ResultListProd.getString("designation_produit");
                String  quantite = ResultListProd.getString("quantite").replace(",",".");
                String  PrixHT = ResultListProd.getString("p_u_h_t").replace(",",".");
                String  Remise = ResultListProd.getString("remise").replace(",",".").replace("%","");
                String  TotalHT = ResultListProd.getString("p_u_h_t_net").replace(",",".");
                String  TVA = ResultListProd.getString("tva").replace(",",".").replace("%","");
                String  TotalTTC= ResultListProd.getString("montant").replace(",",".");
                ListProd.add(new ListeProduit(reference, designation, quantite, PrixHT, Remise, TotalHT, TVA, TotalTTC));
                
            }
        } catch(SQLException ex){
            System.err.println("getFactureProduitList Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FactureFrsDB  : getFactureProduitList :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return ListProd;
    }
    
    public  ObservableList<FactureFrs> SearchFactureFrsGestion(Integer debut, Integer nbr, String NumeroFacture, String IdFournisseur, String LocalReception, String DateReception)
    {
        ObservableList<FactureFrs> ListFactureFrs = FXCollections.observableArrayList();
        
        String Where = "";
        
        if(!clt.isNumericNotnull(NumeroFacture)){
            Where += " (numero = '"+NumeroFacture+"') AND ";
        }
        
        if(!clt.isNumericNotnull(IdFournisseur)){
            Where += " (id_fournisseur = '"+IdFournisseur+"') AND ";
        }
        
        if(!clt.isStringNull(LocalReception)){
            Where += " (local_reception = '"+LocalReception+"') AND ";
        }
        
        if(!clt.isStringNull(DateReception)){
            Where += " (CAST(date_creation AS DATE) = '"+DateReception+"') AND ";
        }
        
        if(!Where.equals("")){
            Where = Where.substring(0, Where.length() - 4);
            Where = "WHERE  " + Where;
        } 
        
        String req="SELECT  *, to_char(date_creation, 'dd/mm/yyyy') as date_recep, to_char(date_creation, 'hh:ii') as heur_recep "
                + "FROM facture_frs   "
                + Where
                + "ORDER BY id_facture_frs DESC "
                + " LIMIT "+debut+" , "+nbr+";";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                FactureFrs Facture = new FactureFrs();
                Facture.setNumero(resultat.getString("numero"));
                Facture.setIdFactureFrs(resultat.getString("id_facture_frs"));
                Facture.setHeurCreation(resultat.getString("heur_recep"));
                Facture.setDateCreation(resultat.getString("date_recep"));

                Facture.setTotalHorsTaxNet(resultat.getString("total_h_t_net"));
                Facture.setTotalTVA(resultat.getString("total_tva"));
                Facture.setRemise(resultat.getString("remise"));
                Facture.setNetAPayer(resultat.getString("net_a_payer"));
                Facture.setTimbre(resultat.getString("timbre_loi"));
                Facture.setLocal(resultat.getString("local_reception"));
                Facture.setTypeReglement(resultat.getString("type_reglement"));
                Facture.setEtatReglement(resultat.getString("etat_reglement"));
                Facture.setDevise(resultat.getString("devise"));
                Facture.setBonReception(resultat.getString("bon_reception"));
                
                Fournisseur Frs = new FournisseurDB().GetFournisseurDB(resultat.getString("id_fournisseur"));
                Facture.setFournisseur(Frs);
                
                ListFactureFrs.add(Facture);
            }
        } catch(SQLException ex){
            System.err.println("SearchFactureFrsGestion Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FactureFrsDB  : SearchFactureFrsGestion :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return ListFactureFrs;
    }
    
    
    public Integer nbrFactureFrsGestion( String NumeroFacture, String IdFournisseur, String LocalReception, String DateReception)
    {
        String Where = "";
        
        if(!clt.isStringNull(NumeroFacture)){
            Where += " (numero = '"+NumeroFacture+"') AND ";
        }
        
        if(!clt.isStringNull(IdFournisseur)){
            Where += " (id_fournisseur = '"+IdFournisseur+"') AND ";
        }
        
        if(!clt.isStringNull(LocalReception)){
            Where += " (local_reception = '"+LocalReception+"') AND ";
        }
        
        if(!clt.isStringNull(DateReception)){
            Where += " (CAST(date_creation AS DATE) = '"+DateReception+"') AND ";
        }
        
        
        if(!Where.equals("")){
            Where = Where.substring(0, Where.length() - 4);
            Where = "WHERE  " + Where;
        }
        
        String req=" SELECT COUNT(id_facture_frs) " 
                    + " FROM facture_frs "
                    + Where+ " ;"; 
        
        int sommme = 0;
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            resultat.next();
            sommme = resultat.getInt(1);
        } catch(SQLException ex){
            System.err.println("nbrFactureFrsGestion Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FactureFrsDB  : nbrFactureFrsGestion :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return sommme ;
    }
    
    public  ObservableList<FactureFrs> getDataFactureFrs(String  tab,String  mot, Integer debut, Integer nbr){
        
        ObservableList<FactureFrs> ListFactureFrs = FXCollections.observableArrayList();
        
        String Limit = "";
        if(debut != null || nbr!= null){
            Limit = "LIMIT "+debut+" , "+nbr;
        }
        String req="SELECT  *, to_char(date_creation, 'dd/mm/yyyy') as date_recep, to_char(date_creation, 'hh:ii') as heur_recep FROM facture_frs   WHERE ("+tab+" "+mot+") AND (id_reglement_frs IS NULL) ORDER BY id_facture_frs DESC "+Limit+" ;";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                FactureFrs Facture = new FactureFrs();
                Facture.setNumero(resultat.getString("numero"));
                Facture.setIdFactureFrs(resultat.getString("id_facture_frs"));
                Facture.setHeurCreation(resultat.getString("heur_recep"));
                Facture.setDateCreation(resultat.getString("date_recep"));

                Facture.setTotalHorsTaxNet(resultat.getString("total_h_t_net"));
                Facture.setTotalTVA(resultat.getString("total_tva"));
                Facture.setRemise(resultat.getString("remise"));
                Facture.setNetAPayer(resultat.getString("net_a_payer"));
                Facture.setTimbre(resultat.getString("timbre_loi"));
                Facture.setLocal(resultat.getString("local_reception"));
                Facture.setTypeReglement(resultat.getString("type_reglement"));
                Facture.setEtatReglement(resultat.getString("etat_reglement"));
                Facture.setDevise(resultat.getString("devise"));
                Facture.setBonReception(resultat.getString("bon_reception"));
                
                Fournisseur Frs = new FournisseurDB().GetFournisseurDB(resultat.getString("id_fournisseur"));
                Facture.setFournisseur(Frs);
                
                ListFactureFrs.add(Facture);
            }
        } catch(SQLException ex){
            System.err.println("getDataFactureFrs Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FactureFrsDB  : getDataFactureFrs :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return ListFactureFrs;
    }
    
    
    public String nbrFactureFrs(String  tab, String mot)
    {
        String req = "SELECT count(*) FROM facture_frs WHERE "+tab+" "+mot+" ;";
        int sommme =0;
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            resultat.next();
            sommme = resultat.getInt(1);
        } catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FactureFrsDB  : nbrFactureFrs :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
       return  Integer.toString(sommme) ;
    }
    
    
    public  ObservableList<FactureFrs> getFactureByIdReglement(String  IdReglement)
    {
        ObservableList<FactureFrs> ListFactureFrs = FXCollections.observableArrayList();
        String req="SELECT  *, to_char(date_creation, 'dd/mm/yyyy') as date_recep, to_char(date_creation, 'hh:ii') as heur_recep FROM facture_frs   WHERE id_reglement_frs = "+IdReglement+" ORDER BY id_facture_frs DESC ;";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                FactureFrs Facture = new FactureFrs();
                Facture.setNumero(resultat.getString("numero"));
                Facture.setIdFactureFrs(resultat.getString("id_facture_frs"));
                Facture.setHeurCreation(resultat.getString("heur_recep"));
                Facture.setDateCreation(resultat.getString("date_recep"));

                Facture.setTotalHorsTaxNet(resultat.getString("total_h_t_net"));
                Facture.setTotalTVA(resultat.getString("total_tva"));
                Facture.setRemise(resultat.getString("remise"));
                Facture.setNetAPayer(resultat.getString("net_a_payer"));
                Facture.setTimbre(resultat.getString("timbre_loi"));
                Facture.setLocal(resultat.getString("local_reception"));
                Facture.setTypeReglement(resultat.getString("type_reglement"));
                Facture.setEtatReglement(resultat.getString("etat_reglement"));
                Facture.setDevise(resultat.getString("devise"));
                Facture.setBonReception(resultat.getString("bon_reception"));
                
                Profile profile = new ProfileDB().getpro(resultat.getInt("id_profile"));
                Facture.setProfile(profile);
                
                if(Facture.getBonReception() != null){
                    if(Facture.getBonReception().equals("1") ){
                        BonReceptionDB BonRecepDB= new BonReceptionDB();
                        ObservableList<BonReceptionFrs>  ListeBonReception = BonRecepDB.getBonReceptionByFact(resultat.getString("id_facture_frs"));
                        Facture.setListeBonReception(ListeBonReception);
                    }
                }
                ListFactureFrs.add(Facture);
            }
        } catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("FactureFrsDB  : getFactureByIdReglement :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return ListFactureFrs;
    }
    
}
