package Models.BonReceptionFrs;

import Controllers.Traitement.Adaptateur;
import Controllers.Traitement.contro;
import java.sql.ResultSet;
import java.sql.SQLException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import Controllers.Dialog.ShowDialog;
import Controllers.Traitement.ParametreSystem;
import org.apache.log4j.Logger;
import Models.H2JDBC;

/**
 * @author Maher Ben Tili
 */
public class BonReceptionGestionDB {
    
    H2JDBC h2jdbc = new H2JDBC();
    ShowDialog SD = new ShowDialog();
    Logger logger = Logger.getLogger(BonReceptionGestionDB.class.getName());
    contro clt = new contro();
    
    /********************************** Initial *******************************/
    
    public ObservableList<BonReceptionGestionList> SearchBonReceptionGestionFrs(Integer debut, Integer nbr, String Numero, String Transporteur, String Facture, String DateReception, String Fournisseur, String LocalReception)
    {
        ObservableList<BonReceptionGestionList> data=FXCollections.observableArrayList();
        
        String Where = "";
        
        if(!clt.isNumericNotnull(Numero)){
            Where += " (BR.numero = '"+Numero+"') AND ";
        }
        
        if(!clt.isStringNull(Transporteur)){
            Where += " (BR.transporteur LIKE '%"+Adaptateur.addSlashes(Transporteur)+"%') AND ";
        }
        
        if(!clt.isNumericNotnull(Facture)){
            Where += " (BR.id_facture_frs = '"+Facture+"') AND ";
        }
        
        if(!clt.isStringNull(DateReception)){
            Where += " (CAST(BR.date_reception AS DATE) = '"+DateReception+"') AND ";
        }
        
        if(!clt.isNumericNotnull(Fournisseur)){
            Where += " (BR.id_fournisseur = '"+Fournisseur+"') AND ";
        }
        
        if(!clt.isStringNull(LocalReception)){
            Where += " (BR.local_reception = '"+LocalReception+"') AND ";
        }
        
        if(!Where.equals("")){
            Where = Where.substring(0, Where.length() - 4);
            Where = " WHERE (BR.id_fournisseur = F.id_fournisseur) AND " + Where;
        }else{
            Where = " WHERE (BR.id_fournisseur = F.id_fournisseur) ";
        }
        
        String req="SELECT BR.*, to_char(BR.date_reception, 'dd/mm/yyyy') as DateReception, to_char(BR.date_reception, 'hh:ii:ss')  as HeurReception , F.nom "
                + " FROM bonreception_frs BR, fournisseur F "
                + Where
                + " ORDER BY BR.date_reception DESC "
                + "LIMIT "+debut+" , "+nbr+" ;";
        
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                String code = resultat.getString("id_bonreception");
                String numero= resultat.getString("numero");
                String date = resultat.getString("DateReception");
                String heur = resultat.getString("HeurReception");
                String transporteur = resultat.getString("transporteur");
                String fournisseur = resultat.getString("nom");
                String facture = resultat.getString("id_facture_frs");
                String local = resultat.getString("local_reception");
                
                data.add(new BonReceptionGestionList(code, numero, date, heur, transporteur, fournisseur, facture, local));
            }
        } catch(SQLException ex){
            System.err.println("SearchBonReceptionGestionFrs Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("BonReceptionGestionDB  : SearchBonReceptionGestionFrs :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return data ;
    }
    
    public Integer nbrBonReceptionGestionFrsGestion(String Numero, String Transporteur, String Facture, String DateReception, String Fournisseur, String LocalReception)
    {
        String Where = "";
        
        if(!clt.isNumericNotnull(Numero)){
            Where += " (id_bonreception = '"+Numero+"') AND ";
        }
        
        if(!clt.isStringNull(Transporteur)){
            Where += " (transporteur LIKE '%"+Adaptateur.addSlashes(Transporteur)+"%') AND ";
        }
        
        if(!clt.isNumericNotnull(Facture)){
            Where += " (id_facture_frs = '"+Facture+"') AND ";
        }
        
        if(!clt.isStringNull(DateReception)){
            Where += " (CAST(date_reception AS DATE) = '"+DateReception+"') AND ";
        }
        
        if(!clt.isNumericNotnull(Fournisseur)){
            Where += " (id_fournisseur = '"+Fournisseur+"') AND ";
        }
        
        if(!clt.isStringNull(LocalReception)){
            Where += " (local_reception = '"+LocalReception+"') AND ";
        }
        
        if(!Where.equals("")){
            Where = Where.substring(0, Where.length() - 4);
            Where = "WHERE  " + Where;
        }
        
        String req=" SELECT COUNT(id_bonreception) " 
                    + " FROM bonreception_frs "
                    + Where+ " ;"; 
        
        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("BonReceptionGestionDB  :  :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return sommme ;
    }
   
}
