<?php
class Affectation_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['resultat']=$this->Model->getRequete(' SELECT ID_AFFECTATION, rh_employe.ID_EMPLOYE, rh_employe.NOM, rh_employe.PRENOM, DATE_DEBUT, DATE_FIN, organigramme_service.DESCRIPTION AS SERVICE, organigramme_section.DESCRIPTION AS SECTION, organigramme_direction.DESCRIPTION as DIRECTION, STATUS_AFFECTATION, rh_responsabilite.DESCRIPTION AS RESPONSABILITE FROM `rh_employe_affectation` JOIN rh_employe ON rh_employe.ID_EMPLOYE = rh_employe_affectation.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 STATUS_AFFECTATION = 1');
         $tabledata=array();
   
         $data['title']='Affectation';
           $this->load->view('Affectation_Employe_List_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 listing_sans()
    {

      $data['resultat']=$this->Model->getRequete(' SELECT ID_AFFECTATION, rh_employe.NOM, rh_employe.PRENOM, DATE_DEBUT, DATE_FIN, organigramme_service.DESCRIPTION AS SERVICE, organigramme_section.DESCRIPTION AS DEPARTEMENT, organigramme_direction.DESCRIPTION as DIRECTION, STATUS_AFFECTATION, rh_responsabilite.DESCRIPTION AS RESPONSABILITE FROM `rh_employe_affectation` JOIN rh_employe ON rh_employe.ID_EMPLOYE = rh_employe_affectation.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 STATUS_AFFECTATION = 0');
      $tabledata=array();

      $data['title']='Affectation';
      $this->load->view('Affectation_Employe_Innactif_List_View',$data);

    }


    public function index_update($id)
    {

      $data['title']='Affectation';
      $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['service']=$this->Model->getRequete('SELECT * FROM `organigramme_service` order by DESCRIPTION');
      $data['departement']=$this->Model->getRequete('SELECT * FROM `organigramme_section` order by DESCRIPTION');
      // echo '<pre>';
      // print_r($data);
      // exit();

      $this->load->view('Affectation_Employe_Update_View',$data);

    }



    public function update()
    {

  $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_DEPARTEMENT=$this->input->post('ID_DEPARTEMENT');
  $ID_AFFECTATION=$this->input->post('ID_AFFECTATION');
  
    
  $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('Affectation_Employe_Update_View',$data);
   }
   else{
    

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

    $ndata_affectation=array('DATE_FIN'=>$DATE_DEBUT,'STATUS_AFFECTATION'=>0);
    $this->Model->update('rh_employe_affectation',array('ID_AFFECTATION'=>$ID_AFFECTATION,'STATUS_AFFECTATION'=>1),$ndata_affectation);

    

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

    

    $message = "<div class='alert alert-success' id='message'>
                            Affectation 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/Affectation_Employe'));  
   }

    }


    public function desactiver($id)
    {
      $this->Model->update('rh_employe_affectation',array('ID_AFFECTATION'=>$id,'STATUS_AFFECTATION'=>1),array('STATUS_AFFECTATION'=>0,'DATE_FIN'=>date('Y-m-d')));
      $message = "<div class='alert alert-success' 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/Affectation_Employe'));  
    }

  

    
    


}