/*
 * 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.TraiteClt;

import Controllers.Dialog.ShowDialog;
import Controllers.Traitement.Adaptateur;
import Controllers.Traitement.ParametreSystem;
import Controllers.Traitement.contro;
import Models.Produit.Produit;
import Models.connexion;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;


/**
 *
 * @author Maher
 */
public class TraiteCltGestionDB {
    
    connexion cnx = new connexion();
    contro clt = new contro();
    ShowDialog SD = new ShowDialog();
    
    public ResultSet GetDetailTraite(String  IdTraiteClt){
        ResultSet Resultat = null;
        
        String Query = "SELECT T.*, F.typeclt, F.net_a_payer, DATE_FORMAT(date_traite, '%d/%m/%Y') AS date_traite_format, "
                    + "IF(montant_traite <=  montant_payer, '1', IF(date_traite >= CURTIME() OR  date_traite >= date_paiement, '2', '3') ) AS etat, "
                    + " C.codeclient, C.cin, C.nom, C.prenom, "
                    + "E.idcliententreprise, E.matricule_fiscale, E.nomentrepise "
                    + "FROM traite_clt T, factureclt F "
                    + "LEFT JOIN client C ON (C.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltexiste = C.codeclient) "
                    + "LEFT JOIN cltentreprise E ON (E.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltentreprise = E.idcliententreprise) "
                    + "WHERE (T.local_nom = '"+ParametreSystem.NomLocalPC+"') AND (T.id_factureclt = F.idfactureclt) "
                    + "AND (T.id_traite = "+IdTraiteClt+") ";
            
        cnx.ouverture();
        
        try {
            Resultat = cnx.excutionquery(Query);

            Resultat.next();
            
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println("req "+Query);
        }

        return Resultat;
    }
    
    
    
    /********************************** Search *******************************/
    
    public ObservableList<TraiteCltGestionListe>  SearchTraiteClt(Integer debut, Integer nbr, String idTraite, String idFactureclt, String dateTraite, String codeClient, String cin, String nom)
    {
        ObservableList<TraiteCltGestionListe> DataListTraiteClt = FXCollections.observableArrayList();
        
        String Where = "";
        
        if(!clt.isStringNull(idTraite)){
            Where += " (T.id_traite = \""+Adaptateur.addSlashes(idTraite)+"\") AND ";
        }
        
        if(!clt.isStringNull(idFactureclt)){
            Where += " (F.idfactureclt = \""+Adaptateur.addSlashes(idFactureclt)+"\") AND ";
        }
        
        if(!clt.isStringNull(codeClient)){
           Where += "( (F.idcltentreprise = \""+Adaptateur.addSlashes(codeClient)+"\") OR (F.idcltexiste = \""+Adaptateur.addSlashes(codeClient)+"\") ) AND ";
        }
        
        if(!clt.isStringNull(nom)){
            Where += "( (E.nomentrepise LIKE \"%"+Adaptateur.addSlashes(nom)+"%\") OR (C.nomcomplet LIKE \"%"+Adaptateur.addSlashes(nom)+"%\") ) AND "; 
        }
        
        if(!clt.isStringNull(cin)){
            Where += "( (C.cin = \""+Adaptateur.addSlashes(cin)+"\") OR (E.matricule_fiscale = \""+Adaptateur.addSlashes(cin)+"\") ) AND ";
        }
        
        if(!clt.isStringNull(dateTraite)){
            Where += " (T.date_traite = \""+dateTraite+"\") AND ";
        }
        
        if(!Where.equals("")){
            Where = " AND "+Where.substring(0, Where.length() - 4);
        }
        
        String req = "SELECT T.*, F.typeclt, "
                    + "IF(montant_traite >=  montant_payer, '1', IF(date_traite >= CURTIME() OR  date_traite >= date_paiement, '2', '3') ) AS etat, "
                    + "C.codeclient, C.cin, concat(C.nom, \" \",C.prenom) AS nom_prenom_client, "
                    + "E.idcliententreprise, E.matricule_fiscale, E.nomentrepise "
                    + "FROM traite_clt T, factureclt F "
                    + "LEFT JOIN client C ON (C.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltexiste = C.codeclient) "
                    + "LEFT JOIN cltentreprise E ON (E.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltentreprise = E.idcliententreprise) "
                    + "WHERE (T.local_nom = '"+ParametreSystem.NomLocalPC+"') AND (T.id_factureclt = F.idfactureclt) "
                    + Where
                    + "LIMIT "+debut+" , "+nbr+" ;";
        
        try {
            ResultSet resultat= cnx.FetchALL(req);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date_system = sdf.parse(sdf.format(new Date()));

            while ( resultat.next() ) {
                String NumTraite = resultat.getString("id_traite");
                String Facture  = resultat.getString("id_factureclt");
                String DatePayer = resultat.getString("date_paiement");
                String DateTraite = resultat.getString("date_traite");
                float FMontant = resultat.getFloat("montant_traite");
                //1 Payer       2 En Cours      3 En Retart
                String Etat = resultat.getString("etat");
                
                //0 Personne  1 Entreprise
                Integer Typeclt = resultat.getInt("typeclt");
                String TypeClient = ""; String CodeClt = ""; String NomPrenom = "";
                if(Typeclt == 0){
                    TypeClient = "Personne";
                    CodeClt = resultat.getString("codeclient");
                    NomPrenom = resultat.getString("nom_prenom_client");
                }if(Typeclt == 1){
                    TypeClient = "Entreprise";
                    CodeClt = resultat.getString("idcliententreprise");
                    NomPrenom = resultat.getString("nomentrepise");
                }
                
                Date date_traite = sdf.parse(DateTraite);
                
                DataListTraiteClt.add(new TraiteCltGestionListe(NumTraite, Etat, Facture, CodeClt, TypeClient, NomPrenom, Adaptateur.NormalDateFormat(DateTraite), Adaptateur.NormalDateFormat(DatePayer), "", FMontant));
            }
            
        } catch(SQLException ex){
                System.err.println("Erreur SQL ! \n"+ex.getMessage());
                System.out.println(req);
        } catch (ParseException ex) {
            Logger.getLogger(TraiteCltGestionDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return DataListTraiteClt; 
        
    }
    
    
    public Integer nbrSearchTraiteClt(String idTraite, String idFactureclt, String dateTraite, String codeClient, String cin, String nom)
    {
        String Where = "";
        
        if(!clt.isStringNull(idTraite)){
            Where += " (T.id_traite = \""+Adaptateur.addSlashes(idTraite)+"\") AND ";
        }
        
        if(!clt.isStringNull(idFactureclt)){
            Where += " (F.idfactureclt = \""+Adaptateur.addSlashes(idFactureclt)+"\") AND ";
        }
        
        if(!clt.isStringNull(codeClient)){
           Where += "( (F.idcltentreprise = \""+Adaptateur.addSlashes(codeClient)+"\") OR (F.idcltexiste = \""+Adaptateur.addSlashes(codeClient)+"\") ) AND ";
        }
        
        if(!clt.isStringNull(nom)){
            Where += "( (E.nomentrepise LIKE \"%"+Adaptateur.addSlashes(nom)+"%\") OR (C.nomcomplet LIKE \"%"+Adaptateur.addSlashes(nom)+"%\") ) AND "; 
        }
        
        if(!clt.isStringNull(cin)){
            Where += "( (C.cin = \""+Adaptateur.addSlashes(cin)+"\") OR (E.matricule_fiscale = \""+Adaptateur.addSlashes(cin)+"\") ) AND ";
        }
        
        if(!clt.isStringNull(dateTraite)){
            Where += " (T.date_traite = \""+dateTraite+"\") AND ";
        }
        
        if(!Where.equals("")){
            Where = " AND "+Where.substring(0, Where.length() - 4);
        }
        
        String req= "SELECT COUNT(*)"
                    + "FROM traite_clt T, factureclt F "
                    + "LEFT JOIN client C ON (C.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltexiste = C.codeclient) "
                    + "LEFT JOIN cltentreprise E ON (E.local_nom= '"+ParametreSystem.NomLocalPC+"') AND (F.idcltentreprise = E.idcliententreprise) "
                    + "WHERE (T.local_nom = '"+ParametreSystem.NomLocalPC+"') AND (T.id_factureclt = F.idfactureclt) "
                    + Where +" ;";            
        
        int sommme =0;
        if(cnx.ouverture()){
            try{
               /* Exécution d'une requête de lecture */
                ResultSet resultat = cnx.excutionquery(req);
                // get the number of rows from the result set
                resultat.next();
                sommme = resultat.getInt(1);
            }
            catch(SQLException ex){
                System.err.println("Erreur SQL ! \n"+ex.getMessage());
                System.out.println(req);
            }
        }
       return sommme ;
    }
    
    
}
