<?php 
 /**
  * 
  */
 class Consommables extends CI_Controller
 {

  function __construct()
  {
    parent::__construct();
    $this->load->library('Mylibrary');
    $this->ci = & get_instance();
    $this->ci->load->library("user_agent");
    $this->Is_Connected();
    // $this->verification();

  }

  public function Is_Connected()
  {

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




 public function index()
 {
  $title = "Enregistrement entree stock";

  $data['title'] = $title;
  $data['consommables']=$this->Model->getList('type_consommables',array());
  $this->load->view("conso/consommable_Add_View",$data);
}




public function add()
{
    //    
  $DATE_ENTREE_STOCK=$this->input->post('DATE_ENTREE_STOCK');  
  $INTITULE=$this->input->post('INTITULE');
  $QUANTITE=$this->input->post('QUANTITE');
  $PRIX_ACHAT=$this->input->post('PRIX_ACHAT');
  


  $data = array(          
    'DATE_ENTREE_STOCK'=>$this->input->post('DATE_ENTREE_STOCK'),  
    'INTITULE'=>$this->input->post('INTITULE'),  
    'QUANTITE'=>$this->input->post('QUANTITE'), 
    'PRIX_ACHAT'=>$this->input->post('PRIX_ACHAT'),
  );

  // print_r($data );exit();

  $this->form_validation->set_rules('DATE_ENTREE_STOCK', 'Date', 'required');
  $this->form_validation->set_rules('INTITULE', 'Intitule', 'required');
  $this->form_validation->set_rules('QUANTITE', 'Quantite', 'required');
  $this->form_validation->set_rules('PRIX_ACHAT', "Prix d achat", 'required');


    //SI Valide
  if ($this->form_validation->run() == FALSE){        

    $title = "Enregistrement entree stock";

    $data['title'] = $title;
    $data['consommables']=$this->Model->getList('type_consommables',array());

    $message = "<div class='alert alert-danger' id='message'>
    Donne manquantes, Processus échoué.
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    </div>";


    $this->session->set_flashdata(array('message'=>$message));
    $this->load->view("conso/consommable_Add_View",$data);

  }else{

    $estock = $this->Model->insert_last_id('consommable',$data);

    $message = "<div class='alert alert-success'  id='message'>
    Enregistrement fait avec succès.
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    </div>";       
    $this->session->set_flashdata(array('message'=>$message));
    redirect(base_url('donnee/Consommables/'));
  }
}



public function listing()
{
  $data['title'] = "Liste entree stock";
  $this->load->view('conso/consommable_List_View',$data);

}

public function index_update($ID_CONSOMMABLE)
{
  $data['title'] = "Modification des consommables";
  $data['un_conso'] = $this->Model->getOne('consommable',array('ID_CONSOMMABLE'=>$ID_CONSOMMABLE ));
  $data['consommables']=$this->Model->getList('type_consommables',array());
  $this->load->view('conso/consommable_Update_View',$data);

}




public function update()
{

 $DATE_ENTREE_STOCK=$this->input->post('DATE_ENTREE_STOCK');  
 $INTITULE=$this->input->post('INTITULE');
 $QUANTITE=$this->input->post('QUANTITE');
 $PRIX_ACHAT=$this->input->post('PRIX_ACHAT');
 $ID_CONSOMMABLE=$this->input->post('ID_CONSOMMABLE');

 $data = array(          
  'DATE_ENTREE_STOCK'=>$this->input->post('DATE_ENTREE_STOCK'),  
  'INTITULE'=>$this->input->post('INTITULE'),  
  'QUANTITE'=>$this->input->post('QUANTITE'), 
  'PRIX_ACHAT'=>$this->input->post('PRIX_ACHAT'),
);

 $this->form_validation->set_rules('DATE_ENTREE_STOCK', 'Date', 'required');
 $this->form_validation->set_rules('INTITULE', 'Intitule', 'required');
 $this->form_validation->set_rules('QUANTITE', 'Quantite', 'required');
 $this->form_validation->set_rules('PRIX_ACHAT', 'Prix d achat', 'required');


    //SI Valide
 if ($this->form_validation->run() == FALSE){        

  $data['title'] = "Modification des consommables";

  $message = "<div class='alert alert-danger' id='message'>
  Donne manquantes, Processus échoué.
  <button type='button' class='close' data-dismiss='alert'>&times;</button>
  </div>";


  $this->session->set_flashdata(array('message'=>$message));
  $this->load->view("conso/consommable_Update_View",$data);

}else{
  $pproduct = $this->Model->update('consommable',array('ID_CONSOMMABLE'=>$ID_CONSOMMABLE),$data);


  $message = "<div class='alert alert-success'  id='message'>
  Enregistrement fait avec succès.
  <button type='button' class='close' data-dismiss='alert'>&times;</button>
  </div>";       
  $this->session->set_flashdata(array('message'=>$message));
  redirect(base_url('donnee/Consommables/'));
}
}


}                        