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

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




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

     $this->form_validation->set_rules('DATE_ENTREE_STOCK', 'Date', 'required');
     $this->form_validation->set_rules('QUANTITE', 'Quantite', 'required');
     $this->form_validation->set_rules('ID_TYPE_BOUTEILLE', 'Type Bouteille', 'required');
     $this->form_validation->set_rules('ID_TAILLE_BOUTEILLE', 'Taille bouteille', 'required');
     $this->form_validation->set_rules('ID_FOURNISSEUR', 'Type Bouteille', 'required');
     $this->form_validation->set_rules('PRIX_ACHAT', 'Taille bouteille', 'required');

     
    //SI Valide
     if ($this->form_validation->run() == FALSE){        
        
      $title = "Enregistrement entree stock";

      
        $data['typebout']=$this->Model->getList('syst_type_bouteille',array());
        $data['tailbout']=$this->Model->getList('syst_taille_bouteille',array());
        $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("Entree_Stock_Add_View",$data);

      }else{
        
        $estock = $this->Model->insert_last_id('stock_entre_stock',$data);
        if ($estock) {
          
          $veri = $this->Model->checkvalue('stock_stock',array('ID_TYPE_BOUTEILLE'=>$ID_TYPE_BOUTEILLE,'ID_TAILLE_BOUTEILLE'=>$ID_TAILLE_BOUTEILLE));
          if ($veri) {
            // unction update($table, $criteres, $data) {
              $stockac = $this->Model->getOne('stock_stock',array('ID_TYPE_BOUTEILLE'=>$ID_TYPE_BOUTEILLE,'ID_TAILLE_BOUTEILLE'=>$ID_TAILLE_BOUTEILLE));
              $this->Model->update('stock_stock',array('ID_TYPE_BOUTEILLE'=>$ID_TYPE_BOUTEILLE,'ID_TAILLE_BOUTEILLE'=>$ID_TAILLE_BOUTEILLE), array('QUANTITE'=>$stockac['QUANTITE']+$QUANTITE));
          }
          else{
            $this->Model->insert_last_id('stock_stock',array('ID_TYPE_BOUTEILLE'=>$ID_TYPE_BOUTEILLE,'ID_TAILLE_BOUTEILLE'=>$ID_TAILLE_BOUTEILLE,'QUANTITE'=>$QUANTITE));
          }
        }

          $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('stock/Entree_Stock/'));
      }
  }



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

    }

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

          $estock = $this->Model->insert_last_id('deleted_stock_entre_stock',$data);
          if ($estock) {
            $this->Model->delete('stock_entre_stock',array('ID_ENTREE_STOCK'=>$ID_ENTREE_STOCK));

            $stockac = $this->Model->getOne('stock_stock',array('ID_TYPE_BOUTEILLE'=>$olddata['ID_TYPE_BOUTEILLE'],'ID_TAILLE_BOUTEILLE'=>$olddata['ID_TAILLE_BOUTEILLE']));

            $this->Model->update('stock_stock',array('ID_TYPE_BOUTEILLE'=>$olddata['ID_TYPE_BOUTEILLE'],'ID_TAILLE_BOUTEILLE'=>$olddata['ID_TAILLE_BOUTEILLE']), array('QUANTITE'=>$stockac['QUANTITE']-$olddata['QUANTITE']));
           

            $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('stock/Entree_Stock/listing'));

          }

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

    }


 }                        