<?php 
 /**
  * 
  */
 class AchatDivers 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['typeboutaccess']=$this->Model->getList('type_accessoires',array());
        $data['fournisseur']=$this->Model->getRequete('SELECT `ID_FOURNISSEUR`,`NOM`,`PRENOM` FROM `fournisseur`');
        $data['tailbout']=$this->Model->getList('syst_taille_bouteille',array());
        $data['tailboutchon']=$this->Model->getList('type_detail_accessoires',array());

        $data['title'] = $title;
      $this->load->view("achats/Achat_diver_Add_View",$data);
  }




  public function add()
  {
    //    
    $DATE_ENTREE_STOCK=$this->input->post('DATE_ENTREE_STOCK');  
    $QUANTITE=$this->input->post('QUANTITE');
    $ID_ACCESS=$this->input->post('ID_ACCESS');
    $ID_TAILLE_BOUTEILLE=$this->input->post('ID_TAILLE_BOUTEILLE');
    $ID_BOUCHONS_ACCESS=$this->input->post('ID_BOUCHONS_ACCESS');
    
       
    
     $data = array(          
        'DATE_ENTREE_STOCK'=>$this->input->post('DATE_ENTREE_STOCK'),  
        'QUANTITE'=>$this->input->post('QUANTITE'),  
        'ID_ACCESS'=>$this->input->post('ID_ACCESS'), 
        'ID_USER'=>$this->session->userdata('SOCAR_ID_USER'),
        'ID_FOURNISSEUR'=>$this->input->post('ID_FOURNISSEUR'), 
        'PRIX_ACHAT'=>$this->input->post('PRIX_ACHAT'),
        'ID_TAILLE_BOUTEILLE'=>$this->input->post('ID_TAILLE_BOUTEILLE'), 
        'ID_BOUCHONS_ACCESS'=>$this->input->post('ID_BOUCHONS_ACCESS'),
        );

     $this->form_validation->set_rules('DATE_ENTREE_STOCK', 'Date', 'required');
     $this->form_validation->set_rules('QUANTITE', 'Quantite', 'required');
     $this->form_validation->set_rules('ID_ACCESS', 'Type Bouteille', 'required');
     $this->form_validation->set_rules('ID_FOURNISSEUR', 'Fournisseur', 'required');
     $this->form_validation->set_rules('PRIX_ACHAT', 'Prix d achat', 'required');

     if ($this->form_validation->run() == FALSE){        
        
      $title = "Enregistrement entree stock";
     
      $data['typebout']=$this->Model->getList('type_accessoires',array());
      $data['fournisseur']=$this->Model->getRequete('SELECT `ID_FOURNISSEUR`,`NOM`,`PRENOM` FROM `fournisseur`');

        $data['title'] = $title;
      

        $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("achats/Achat_diver_Add_View",$data);

      }else{
        
        $estock = $this->Model->insert_last_id('stock_entre_accessoires',$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/AchatDivers/'));
      }
  }



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

    }

    public function suppression($id)
    {
      
        $ID_ARTICLE_STOCK =$id;  
        $olddata = $this->Model->getOne('stock_entre_accessoires',array('ID_ARTICLE_STOCK '=>$ID_ARTICLE_STOCK ));
        $data = array(    
          'ID_ARTICLE_STOCK'=> $olddata['ID_ARTICLE_STOCK'],
          'DATE_ENTREE_STOCK'=> $olddata['DATE_ENTREE_STOCK'],
          'ID_ARTICLE_STOCK'=> $olddata['ID_ARTICLE_STOCK'],
          'QUANTITE'=> $olddata['QUANTITE'],
          'ID_ACCESS'=> $olddata['ID_ACCESS'],
          'DATE_ENREGISTREMENT'=> $olddata['DATE_ENREGISTREMENT'],
          'ID_USER'=> $olddata['ID_USER'],
          'MODE'=> $olddata['MODE'],
          'USER_DELETED'=> $this->session->userdata('SOCAR_ID_USER'),
          'ID_FOURNISSEUR'=> $olddata['ID_FOURNISSEUR'],
          'PRIX_ACHAT'=> $olddata['PRIX_ACHAT'],
          'ID_TAILLE_BOUTEILLE'=> $olddata['ID_TAILLE_BOUTEILLE'],
          'ID_BOUCHONS_ACCESS'=> $olddata['ID_BOUCHONS_ACCESS'],
          'ID_USER'=> $olddata['ID_USER'],
          );

          $estock = $this->Model->insert_last_id('deleted_stock_entre_accessoires',$data);
          if($estock ){
            $this->Model->delete('stock_entre_accessoires',array('ID_ARTICLE_STOCK'=>$ID_ARTICLE_STOCK));
            $message = "<div class='alert alert-danger'  id='message'>
            Suppression 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/AchatDivers/listing'));

          

    }
    public function deleted()
    {
        $data['title'] = "Liste entree stock";
        $this->load->view('achats/Achat_diver_Deleted_View',$data);

    }


 }                        