<?php
class Fournisseur  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']='Fournisseur';      
      $this->load->view('Fournisseur_Add_View',$data);

    }



      public function add()
    {
           
      $NOM_FOURNISSEUR=$this->input->post('NOM_FOURNISSEUR');
      $DEBUT_COLLABO=$this->input->post('DEBUT_COLLABO');
      $NIF=$this->input->post('NIF');
      $RC=$this->input->post('RC');

      $this->form_validation->set_rules('NOM_FOURNISSEUR', 'Nom', 'required');
      $this->form_validation->set_rules('DEBUT_COLLABO', 'Date', 'required');
      $this->form_validation->set_rules('NIF', 'NIF', 'required');
      $this->form_validation->set_rules('RC', 'RC', 'required');
  
   if ($this->form_validation->run() == FALSE){
    $message = "<div class='alert alert-danger'>
                              Fournisseur non enregistr&eacute;
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
    $data['title']='Demande interne';
      $this->load->view('Fournisseur_Add_View',$data); 
   }
   else{

    $data=array(
      'NOM_FOURNISSEUR'=>$NOM_FOURNISSEUR,
      'DEBUT_COLLABO'=>$DEBUT_COLLABO,
      'NIF'=>$NIF,
      'RC'=>$RC
      );    
    
    $this->Model->insert_last_id('req_externe_fournisseur',$data);

    $message = "<div class='alert alert-success' id='message'>
                            Fournisseur 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('requisition_externe/Fournisseur/listing'));  
   }

    }


      public function listing()
    {            
       $statusc = ' ';
              
      $data['resultat']=$this->Model->getRequete('SELECT * FROM `req_externe_fournisseur` WHERE 1');

      $data['title']='Fournisseur';
        $this->load->view('Fournisseur_List_View',$data);
    }

    public function desactiver($id)
    {
      $this->Model->update('req_externe_fournisseur',array('ID_FOURNISSEUR'=>$id),array('STATUS'=>0));
      
      $message = "<div class='alert alert-primary' id='message'>
                            Fournisseur 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('requisition_externe/Fournisseur/listing'));  
    }

  public function reactiver($id)
    {
      $this->Model->update('req_externe_fournisseur',array('ID_FOURNISSEUR'=>$id),array('STATUS'=>1));
      $message = "<div class='alert alert-primary' id='message'>
                            Fournisseur 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('requisition_externe/Fournisseur/listing'));  
    }


    
    public function index_update($ID_TYPE_DOC_INTERNE)
    {

      $data['title']='Demande interne';     
      $data['data']=$this->Model->getRequeteOne('SELECT * FROM `doc_interne_type` WHERE 1 AND ID_TYPE_DOC_INTERNE = '.$ID_TYPE_DOC_INTERNE.''); 
      $this->load->view('Fournisseur_Update_View',$data);

    }


    public function update()
    {

       $DESCRIPTION=$this->input->post('DESCRIPTION');
       $ID_TYPE_DOC_INTERNE=$this->input->post('ID_TYPE_DOC_INTERNE');

      $this->form_validation->set_rules('DESCRIPTION', 'Nom du type de document', 'required');
  
   if ($this->form_validation->run() == FALSE){
    $message = "<div class='alert alert-danger'>
                              Type de document non modifi&eacute;
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
    $data['title']='Demande interne';
    $data['data']=$this->Model->getRequeteOne('SELECT * FROM `doc_interne_type` WHERE 1 AND ID_TYPE_DOC_INTERNE = '.$ID_TYPE_DOC_INTERNE.'');
      $this->load->view('Fournisseur_Update_View',$data); 
   }
   else{

    $data=array('DESCRIPTION'=>$DESCRIPTION);    
    $this->Model->update('doc_interne_type',array('ID_TYPE_DOC_INTERNE'=>$ID_TYPE_DOC_INTERNE),$data);

    $message = "<div class='alert alert-success' id='message'>
                            Type de document 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('requisition_externe/Fournisseur/listing'));  
   }

    }


}