/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Models.BonReceptionFrs;

import Controllers.Traitement.Adaptateur;
import Controllers.Traitement.contro;
import Models.connexion;
import java.sql.ResultSet;
import java.sql.SQLException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 *
 * @author Maher
 */
public class BonReceptionGestionDB {
    
    connexion cnx = new connexion();
    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.isStringNull(Numero)){
            Where += " (BR.`numero` = \""+Numero+"\") AND ";
        }
        
        if(!clt.isStringNull(Transporteur)){
            Where += " (BR.`transporteur` LIKE \"%"+Adaptateur.addSlashes(Transporteur)+"%\") AND ";
        }
        
        if(!clt.isStringNull(Facture)){
            Where += " (BR.`id_facture_frs` = \""+Facture+"\") AND ";
        }
        
        if(!clt.isStringNull(DateReception)){
            Where += " (DATE(BR.`date_reception`) = \""+DateReception+"\") AND ";
        }
        
        if(!clt.isStringNull(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_fourisseur) AND " + Where;
        }else{
            Where = " WHERE (BR.id_fournisseur = F.id_fourisseur) ";
        }
        
        String req="SELECT BR.*, DATE_FORMAT(BR.date_reception, '%d/%m/%Y') as DateReception, DATE_FORMAT(BR.date_reception, '%H:%i:%s')  as HeurReception , F.nom "
                + " FROM bonreception_frs BR, fournisseur F "
                + Where
                + " ORDER BY BR.date_reception DESC "
                + "LIMIT "+debut+" , "+nbr+" ;";
        
        if(cnx.ouverture()){
            try{
               // Exécution d'une requête de lecture 
                ResultSet resultat = cnx.excutionquery(req);
                //on place le curseur sur le dernier tuple
                resultat.last();
                //on récupère le numéro de la ligne
                int nombreLignes = resultat.getRow();
                if(nombreLignes>0){
                    //on repace le curseur avant la première ligne
                    resultat.beforeFirst();
                    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("Erreur SQL ! \n"+ex.getMessage());
                System.out.println(req);
            }
        }
        
       return data ;
    }
    
    public Integer nbrBonReceptionGestionFrsGestion(String Numero, String Transporteur, String Facture, String DateReception, String Fournisseur, String LocalReception)
    {
        String Where = "";
        
        if(!clt.isStringNull(Numero)){
            Where += " (`id_bonreception` = \""+Numero+"\") AND ";
        }
        
        if(!clt.isStringNull(Transporteur)){
            Where += " (`transporteur` LIKE \"%"+Adaptateur.addSlashes(Transporteur)+"%\") AND ";
        }
        
        if(!clt.isStringNull(Facture)){
            Where += " (`id_facture_frs` = \""+Facture+"\") AND ";
        }
        
        if(!clt.isStringNull(DateReception)){
            Where += " (DATE(`date_reception`) = \""+DateReception+"\") AND ";
        }
        
        if(!clt.isStringNull(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;
        
        if(cnx.ouverture()){
            try{
               /* Exécution d'une requête de lecture */
                ResultSet resultat = cnx.excutionquery(req);
                resultat.next();    // get the number of rows from the result set
                sommme = resultat.getInt(1);
            }
            catch(SQLException ex){
                System.err.println("Erreur SQL ! \n"+ex.getMessage());
                System.out.println(req);
            }
        }
        return sommme ;
    }
   
}
