<?php
class Employe  extends CI_Controller{
    function __construct() {
        parent::__construct();
        $this->Is_Connected();

      }

      public function Is_Connected()
       {
       if (empty($this->session->userdata('SOCAR_ID_USER')))
        {
         redirect(base_url('Login/'));
        }
       }

        
    public function index()
    {

      $data['title']='Employé';
      $data['responsabilite']=$this->Model->getRequete('SELECT * FROM `rh_responsabilite` order by DESCRIPTION');
      $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
      $data['departement']=$this->Model->getRequete('SELECT * FROM `organigramme_service` order by DESCRIPTION');
      $data['profil']=$this->Model->getRequete('SELECT * FROM `config_profil` order by DESCRIPTION');
      $this->load->view('Employe_Add_View',$data);

    }

    
    public function get_departement()
  {
  $departement= $this->Model->getList("organigramme_service",array('ID_DIRECTION'=>$this->input->post('ID_DIRECTION')));
  $datas= '<option value="">-- Sélectionner --</option>';
  foreach($departement as $sele_departement){
  $datas.= '<option value="'.$sele_departement["ID_SERVICE"].'">'.$sele_departement["DESCRIPTION"].'</option>';
  }
  $datas.= '';
  echo $datas;
  }

  public function get_service()
  {
  $service= $this->Model->getList("organigramme_section",array('ID_SERVICE'=>$this->input->post('ID_SERVICE')));
  $datas= '<option value="">-- Sélectionner --</option>';
  foreach($service as $sele_service){
  $datas.= '<option value="'.$sele_service["ID_SECTION"].'">'.$sele_service["DESCRIPTION"].'</option>';
  }
  $datas.= '';
  echo $datas;
  }

    public function add()
    {
           
  $NOM=$this->input->post('NOM');
  $PRENOM=$this->input->post('PRENOM');
  $ID_SEXE=$this->input->post('ID_SEXE');
  $EMAIL=$this->input->post('EMAIL');
  $TEL=$this->input->post('TEL');
  $DATE_ENTRE_SERVICE=$this->input->post('DATE_ENTRE_SERVICE');
  $ID_DIRECTION=$this->input->post('ID_DIRECTION');
  $ID_RESPONSABILITE=$this->input->post('ID_RESPONSABILITE');
  $ID_SERVICE=$this->input->post('ID_SERVICE');
  $ID_SECTION=$this->input->post('ID_SECTION');
  $PROFIL_ID=$this->input->post('PROFIL_ID');
  
    
  $this->form_validation->set_rules('NOM', 'Nom', 'required');
  $this->form_validation->set_rules('PRENOM', 'Pr&eacute;nom', 'required');
  $this->form_validation->set_rules('ID_SEXE', 'Sexe', 'required');
  $this->form_validation->set_rules('EMAIL', 'Email', 'required|is_unique[ rh_employe.EMAIL]');
  $this->form_validation->set_rules('TEL', 'Telephone', 'required|is_unique[ rh_employe.TEL]');
  $this->form_validation->set_rules('DATE_ENTRE_SERVICE', 'Date', 'required');
  $this->form_validation->set_rules('ID_DIRECTION', 'Direction', 'required');
  $this->form_validation->set_rules('ID_RESPONSABILITE', 'Rensponsabilite', 'required');

   if ($this->form_validation->run() == FALSE){
    $message = "<div class='alert alert-danger'>
                              Employé non enregistr&eacute;
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
    $data['title']='Employé';
    $data['responsabilite']=$this->Model->getRequete('SELECT * FROM `rh_responsabilite` order by DESCRIPTION');
    $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
    $data['departement']=$this->Model->getRequete('SELECT * FROM `organigramme_service` order by DESCRIPTION');
    $this->load->view('Employe_Add_View',$data);
   }
   else{
    
    $PASSWORD = $this->notifications->generate_UIID(6);
    $DATE_ENTRE_SERVICE=date_create($DATE_ENTRE_SERVICE);
    $DATE_ENTRE_SERVICE = date_format($DATE_ENTRE_SERVICE,"Y-m-d");
    
    $data_employe=array('NOM'=>$NOM,'PRENOM'=>$PRENOM,'ID_SEXE'=>$ID_SEXE,'EMAIL'=>$EMAIL,'TEL'=>$TEL,'DATE_ENTRE_SERVICE'=>$DATE_ENTRE_SERVICE,'PASSWORD'=>md5($PASSWORD));
    $ID_EMPLOYE = $this->Model->insert_last_id('rh_employe',$data_employe);

    $DATE_DEBUT = date('Y-m-d');

    $data_affectation=array('ID_EMPLOYE'=>$ID_EMPLOYE,'DATE_DEBUT'=>$DATE_DEBUT,'ID_SERVICE'=>$ID_SERVICE,'ID_SECTION'=>$ID_SECTION,'ID_DIRECTION'=>$ID_DIRECTION,'ID_RESPONSABILITE'=>$ID_RESPONSABILITE);
    $this->Model->insert_last_id('rh_employe_affectation',$data_affectation);

      if ($ID_DIRECTION) {
      $direction=$this->Model->getRequeteOne('SELECT * FROM `organigramme_direction` WHERE ID_DIRECTION = '.$ID_DIRECTION.' ');
      }



      $datasuser=array(
                       'NOM'=>$NOM,
                       'PRENOM'=>$PRENOM,
                       'USERNAME'=>$EMAIL,
                       'PASSWORD'=>md5($PASSWORD),
                       'PROFIL_ID'=>$PROFIL_ID,
                       'ID_EMPLOYE'=>$ID_EMPLOYE
                      );
                      
    $this->Model->insert_last_id('config_user',$datasuser);  

      
    

      $obj = 'Identifiant a la plateforme de gestion des documents';
          if ($ID_SEXE == 1) {
            $message = 'Monsieur '.$NOM.' '.$PRENOM.',<br>';
          }
          else{
           $message = 'Madame '.$NOM.' '.$PRENOM.',<br>';
          }


          if ($ID_SERVICE != 0 && $ID_SERVICE != NULL ) {
            $service=$this->Model->getRequeteOne('SELECT * FROM `organigramme_service` WHERE ID_SERVICE = '.$ID_SERVICE.'');
            $nservice = ', service '.$departement['DESCRIPTION'].' ';
          }
          else{
            $nservice =' ';
          }


          if ($ID_SECTION != 0 && $ID_SECTION != NULL ) {
            $departement=$this->Model->getRequeteOne('SELECT * FROM `organigramme_section` WHERE ID_SECTION = '.$ID_SECTION.'');
            $ndepart = ', section '.$departement['DESCRIPTION'].' ';
          }
          else{
            $ndepart =' ';
          }


          

          

          $message.= 'Vous avez été inscrit dans le système de gestion de documents du Mininter comme employé affecte à la '.$direction['DESCRIPTION'].' '.$ndepart.' '.$nservice.' <br><br> Vos identifiants de connexion sur le système de gestion et suivie des dans SOCAR VIE sont :<br>Nom d\'utilisateur : <b>'.$EMAIL.'</b><br>Mot de passe : <b>'.$PASSWORD.'</b><br><br>Lien de connexion sur la plateforme : <a href="'.base_url().'" target="_blank">Système Gestion de documents</a> ';


      // $this->notifications->newsend_mail($EMAIL,$obj,NULL,$message,NULL);



    $message = "<div class='alert alert-primary' id='message'>
                            Employe enregistr&eacute; avec succés
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
      redirect(base_url('rh/Employe/listing'));  
   }

    }

    

    public function listing()
    {

      $data['resultat']=$this->Model->getRequete('SELECT rh_employe.ID_EMPLOYE, NOM, PRENOM, rh_sexe.DESCRIPTION AS SEXE, EMAIL, TEL, DATE_ENTRE_SERVICE, rh_employe.STATUS, organigramme_direction.DESCRIPTION AS DIRECTION, organigramme_service.DESCRIPTION AS SERVICE, organigramme_section.DESCRIPTION AS SECTION,  rh_employe_affectation.DATE_DEBUT, rh_responsabilite.DESCRIPTION AS RESPONSABILITE FROM `rh_employe` JOIN rh_sexe ON rh_sexe.ID_SEXE = rh_employe.ID_SEXE JOIN rh_employe_affectation ON rh_employe_affectation.ID_EMPLOYE = rh_employe.ID_EMPLOYE JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = rh_employe_affectation.ID_DIRECTION LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = rh_employe_affectation.ID_SECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE =  rh_employe_affectation.ID_SERVICE JOIN rh_responsabilite ON rh_responsabilite.ID_RESPONSABILITE = rh_employe_affectation.ID_RESPONSABILITE WHERE 1 AND rh_employe_affectation.STATUS_AFFECTATION = 1');
      $tabledata=array();

      $data['title']='Employé';
        $this->load->view('Employe_List_View',$data);

    }


    public function listing_sans()
    {

      $data['resultat']=$this->Model->getRequete('SELECT rh_employe.ID_EMPLOYE, NOM, PRENOM, rh_sexe.DESCRIPTION AS SEXE, EMAIL, TEL, DATE_ENTRE_SERVICE, rh_employe.STATUS, organigramme_direction.DESCRIPTION AS DIRECTION, organigramme_departement.DESCRIPTION AS DEPARTEMENT, organigramme_service.DESCRIPTION AS SERVICE, rh_employe_affectation.DATE_DEBUT, rh_responsabilite.DESCRIPTION AS RESPONSABILITE FROM `rh_employe` JOIN rh_sexe ON rh_sexe.ID_SEXE = rh_employe.ID_SEXE JOIN rh_employe_affectation ON rh_employe_affectation.ID_EMPLOYE = rh_employe.ID_EMPLOYE JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = rh_employe_affectation.ID_DIRECTION LEFT JOIN organigramme_departement ON organigramme_departement.ID_DEPARTEMENT = rh_employe_affectation.ID_DEPARTEMENT LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE =  rh_employe_affectation.ID_SERVICE JOIN rh_responsabilite ON rh_responsabilite.ID_RESPONSABILITE = rh_employe_affectation.ID_RESPONSABILITE WHERE 1 AND rh_employe_affectation.STATUS_AFFECTATION = 0');
      $tabledata=array();

      $data['title']='Employé';
        $this->load->view('Employe_List_Sans_View',$data);

    }


    public function index_update($id)
    {

      $data['title']='Employe';
      $data['data']=$this->Model->getRequeteOne('SELECT * FROM rh_employe WHERE ID_EMPLOYE = '.$id.'');
      $data['affectation']=$this->Model->getRequeteOne('SELECT * FROM rh_employe_affectation WHERE ID_EMPLOYE = '.$id.' AND STATUS_AFFECTATION = 1');
      $data['responsabilite']=$this->Model->getRequete('SELECT * FROM `rh_responsabilite` order by DESCRIPTION');
      $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
      $data['departement']=$this->Model->getRequete('SELECT * FROM `organigramme_service` order by DESCRIPTION');
      $data['service']=$this->Model->getRequete('SELECT * FROM `organigramme_section` order by DESCRIPTION');
      $this->load->view('Employe_Update_View',$data);

    }



    public function update()
    {

      $NOM=$this->input->post('NOM');
  $PRENOM=$this->input->post('PRENOM');
  $ID_SEXE=$this->input->post('ID_SEXE');
  $EMAIL=$this->input->post('EMAIL');
  $TEL=$this->input->post('TEL');
  $DATE_ENTRE_SERVICE=$this->input->post('DATE_ENTRE_SERVICE');
  $ID_EMPLOYE=$this->input->post('ID_EMPLOYE');
  $ID_DIRECTION=$this->input->post('ID_DIRECTION');
  $ID_RESPONSABILITE=$this->input->post('ID_RESPONSABILITE');
  $ID_SERVICE=$this->input->post('ID_SERVICE');
  $ID_SECTION=$this->input->post('ID_SECTION');
  
    
  $this->form_validation->set_rules('NOM', 'Nom', 'required');
  $this->form_validation->set_rules('PRENOM', 'Pr&eacute;nom', 'required');
  $this->form_validation->set_rules('ID_SEXE', 'Sexe', 'required');
  $this->form_validation->set_rules('EMAIL', 'Email', 'required');
  $this->form_validation->set_rules('TEL', 'Telephone', 'required');
  $this->form_validation->set_rules('DATE_ENTRE_SERVICE', 'Date', 'required');
  $this->form_validation->set_rules('ID_DIRECTION', 'Direction', 'required');
  $this->form_validation->set_rules('ID_RESPONSABILITE', 'Rensponsabilite', 'required');

   if ($this->form_validation->run() == FALSE){
    $data['title']='Employe';
      $data['data']=$this->Model->getRequeteOne('SELECT * FROM rh_employe WHERE ID_EMPLOYE = '.$ID_EMPLOYE.'');
      $data['affectation']=$this->Model->getRequeteOne('SELECT * FROM rh_employe_affectation WHERE ID_EMPLOYE = '.$ID_EMPLOYE.' AND STATUS_AFFECTATION = 1');
      $data['responsabilite']=$this->Model->getRequete('SELECT * FROM `rh_responsabilite` order by DESCRIPTION');
      $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
      $data['departement']=$this->Model->getRequete('SELECT * FROM `organigramme_departement` order by DESCRIPTION');
      $data['service']=$this->Model->getRequete('SELECT * FROM `organigramme_service` order by DESCRIPTION');
      $this->load->view('Employe_Update_View',$data);
   }
   else{
    
    $DATE_ENTRE_SERVICE=date_create($DATE_ENTRE_SERVICE);
    $DATE_ENTRE_SERVICE = date_format($DATE_ENTRE_SERVICE,"Y-m-d");
    
    $data_employe=array('NOM'=>$NOM,'PRENOM'=>$PRENOM,'ID_SEXE'=>$ID_SEXE,'EMAIL'=>$EMAIL,'TEL'=>$TEL,'DATE_ENTRE_SERVICE'=>$DATE_ENTRE_SERVICE);
    $this->Model->update('rh_employe',array('ID_EMPLOYE'=>$ID_EMPLOYE),$data_employe);

    // $DATE_DEBUT = date('Y-m-d');

    $data_affectation=array('ID_EMPLOYE'=>$ID_EMPLOYE,'ID_SERVICE'=>$ID_SERVICE,'ID_SECTION'=>$ID_SECTION,'ID_DIRECTION'=>$ID_DIRECTION,'ID_RESPONSABILITE'=>$ID_RESPONSABILITE);
    $this->Model->update('rh_employe_affectation',array('ID_EMPLOYE'=>$ID_EMPLOYE,'STATUS_AFFECTATION'=>1),$data_affectation);

    $message = "<div class='alert alert-primary' id='message'>
                            Employe modifi&eacute; avec succés
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
      redirect(base_url('rh/Employe/listing'));  
   }

    }


    public function desactiver($id)
    {
      $this->Model->update('rh_employe',array('ID_EMPLOYE'=>$id),array('STATUS'=>0));
      $this->Model->update('rh_employe_affectation',array('ID_EMPLOYE'=>$id,'STATUS_AFFECTATION'=>1),array('STATUS_AFFECTATION'=>0,'DATE_FIN'=>date('Y-m-d')));
      $message = "<div class='alert alert-primary' id='message'>
                            Employé désactivé avec succés
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
      $this->session->set_flashdata(array('message'=>$message));
      redirect(base_url('rh/Employe/listing'));  
    }

  public function reactiver($id)
    {
      $this->Model->update('rh_employe',array('ID_EMPLOYE'=>$id),array('STATUS'=>1));
      // $this->Model->update('rh_employe_affectation',array('ID_EMPLOYE'=>$id,'STATUS_AFFECTATION'=>1),array('STATUS_AFFECTATION'=>1));
      $message = "<div class='alert alert-primary' id='message'>
                            Employé Réactivé avec succés
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
      $this->session->set_flashdata(array('message'=>$message));
      redirect(base_url('rh/Employe/listing'));  
    }


    public function details($id)
    {

      $data['title']='Employe';
      $data['data']=$this->Model->getRequeteOne('SELECT ID_EMPLOYE,NOM, PRENOM, rh_sexe.DESCRIPTION AS ID_SEXE, EMAIL, TEL, DATE_ENTRE_SERVICE, IF(STATUS = 1, "Actif", "Innactif") AS STATUS FROM rh_employe LEFT JOIN rh_sexe ON rh_sexe.ID_SEXE = rh_employe.ID_SEXE  WHERE ID_EMPLOYE = '.$id.'');
      $data['affectation']=$this->Model->getRequete('SELECT DATE_DEBUT, DATE_FIN, organigramme_service.DESCRIPTION AS ID_SERVICE, organigramme_section.DESCRIPTION AS SECTION, organigramme_direction.DESCRIPTION AS ID_DIRECTION, IF(STATUS_AFFECTATION = 1, "Actif", "Innactif") AS STATUS_AFFECTATION, rh_responsabilite.DESCRIPTION AS ID_RESPONSABILITE FROM `rh_employe_affectation` LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = rh_employe_affectation.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = rh_employe_affectation.ID_SECTION LEFT JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = rh_employe_affectation.ID_DIRECTION LEFT JOIN rh_responsabilite ON rh_responsabilite.ID_RESPONSABILITE = rh_employe_affectation.ID_RESPONSABILITE WHERE `ID_EMPLOYE` = '.$id.'');
      $data['conge']=$this->Model->getRequete('SELECT ID_EXERCICE, DATE_SYSTEME_DEMANDE, DATE_DERNIER_REPOSE, conge_status_conge.DESCRIPTION AS STATUS_CONGE, NB_JOURS_DEMANDE, NB_JOURS_ACCORDE, config_type_conge.NOM_CONGE AS ID_TYPE_CONGE, DATE_DEBUT_CONGE_ACCORDE, DATE_FIN_CONGE_ACCORDE, DATE_DEBUT_CONGE_DEMANDE, DATE_FIN_CONGE_DEMANDE FROM `conge_demande_conge` JOIN conge_status_conge ON conge_status_conge.STATUS_CONGE = conge_demande_conge.STATUS_CONGE LEFT JOIN config_type_conge ON config_type_conge.ID_TYPE_CONGE = conge_demande_conge.ID_TYPE_CONGE WHERE `ID_EMPLOYE` = '.$id.'');
      
      $this->load->view('Employe_Details_View',$data);

    }
    
    


}