<?php 
 /**
  * 
  */
 class Lot 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 Lot";
      $crequette= $this->Model->getRequeteOne("SELECT COUNT(ID_LOT) AS NOMBRE FROM `prod_lot`");
      $number1 = $crequette['NOMBRE'] + 1;
      $formatted1 = str_pad($number1, 5, '0', STR_PAD_LEFT);
      // Get the current month and year
$currentMonth = date('m'); // Format: MM
$currentYear = date('y'); // Format: YY

$data['NUMERO_LOT'] = 'LO-' . $currentMonth .'-'. $currentYear. '-' . $formatted1 ;

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

  


  public function add()
  {
    //    
    $LOT_PRODUCTION=$this->input->post('LOT_PRODUCTION');
    $DATE_CREATION=$this->input->post('DATE_CREATION');
    
    
     $dataprod = array(          
        'DATE_CREATION'=>$this->input->post('DATE_CREATION'),  
        'LOT_PRODUCTION'=>$this->input->post('LOT_PRODUCTION'),
        'LOT_VOLUME'=>$this->input->post('LOT_VOLUME'), 
        'LOT_RESTANT'=>$this->input->post('LOT_VOLUME'),
        'CREER_PAR'=>$this->session->userdata('SOCAR_ID_USER'),
        'ID_STATUS'=>1,
        );
        

     $this->form_validation->set_rules('DATE_CREATION', 'Date', 'required');
     $this->form_validation->set_rules('LOT_PRODUCTION', 'Lot Production', 'required|is_unique[prod_lot.LOT_PRODUCTION]'); 
     $this->form_validation->set_rules('LOT_VOLUME', 'Volume', 'required');
     
     
    //SI Valide
     if ($this->form_validation->run() == FALSE){        
        
      $title = "Enregistrement Lot";

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

      }else{
        // prod_production

        $pproduct = $this->Model->insert_last_id('prod_lot',$dataprod);
        
      
          $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('production/Lot'));
      }
  }



  public function listing()
    {
      
        $data['title'] = "Lot";
        $this->load->view('Lot_List_View',$data);

    }

    

   

    
    public function deleted()
    {
      
        $data['title'] = "Lot ";
        $this->load->view('Lot_Deleted_List_View',$data);

    }

    public function suppression($id)
    {
      $ID_LOT=$id;  
      $olddata = $this->Model->getOne('prod_lot',array('ID_LOT'=>$ID_LOT));

      $dataprod = array(          
        'ID_LOT'=>$ID_LOT,  
        'LOT_PRODUCTION'=>$olddata['LOT_PRODUCTION'],  
        'ID_STATUS'=>$olddata['ID_STATUS'], 
        'DATE_CREATION'=>$olddata['DATE_CREATION'],
        'DATE_FIN'=>$olddata['DATE_FIN'],        
        'CREER_PAR'=>$olddata['CREER_PAR'],  
        'TERMINER_PAR'=>$olddata['TERMINER_PAR'],                
        'USER_DELETED'=>$this->session->userdata('SOCAR_ID_USER'),
        );

        

        $estock = $this->Model->insert_last_id('deleted_prod_lot',$dataprod);

        if ($estock) {
          $this->Model->delete('prod_lot',array('ID_LOT'=>$ID_LOT));
         

          // $datastock = ;
     
          //  $datasortiestock = ;
          $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('production/Lot/listing'));
     


        }
    }
    
    public function finlot($id)
    {
      $ID_LOT=$id;  
      $olddata = $this->Model->update('prod_lot',array('ID_LOT'=>$ID_LOT),array('TERMINER_PAR'=>$this->session->userdata('SOCAR_ID_USER'),'DATE_FIN'=>date('Y-m-d'),'ID_STATUS'=>2));

      $message = "<div class='alert alert-danger'  id='message'>
      Declaration 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('production/Lot/listing'));
    }

    // public function verification()
    // {
    //     $todaydate = date('Y-m-d'); 
    //     $liste = $this->Model->getRequete('SELECT ID_ASSURANCE FROM `voiture_assurance` WHERE 1 AND DATE_FIN < "'.$todaydate.'" AND ID_STATUS = 1');
    //     foreach ($liste as $key) {
    //       $this->Model->update('voiture_assurance',array('ID_ASSURANCE'=>$key['ID_ASSURANCE']),array('ID_STATUS'=>5));
    //     }
    // }


  //   public function modifier()
  // {
  //     $title = "Modification Garage";
  //     $GARAGE_ID =$this->uri->segment(4);
  //     $onedetail = $this->Model->getOne('voiture_garage',array('GARAGE_ID'=>$GARAGE_ID));
  //     $approvisionnement = array(
  //              'GARAGE_ID'=>$onedetail['GARAGE_ID'],
  //              'GARAGE_NOM'=>$onedetail['GARAGE_NOM'],       
  //              'PROVINCE_ID'=>$onedetail['PROVINCE_ID'],       
  //              'COMMUNE_ID'=>$onedetail['COMMUNE_ID'],       
  //              'province'=>$this->Model->getList('syst_provinces',array()),
  //              'commune'=>$this->Model->getList('syst_communes',array()),
  //             ); 
    
        
  //       $data['title'] = $title;
  //       $data['approv'] = $approvisionnement;


  //   $this->load->view("Garage_Update_View",$data);
  // }

  // public function update_garage()
  // {

  //    $data = array(          
  //       'GARAGE_NOM'=>$this->input->post('GARAGE_NOM'),  
  //       'PROVINCE_ID'=>$this->input->post('PROVINCE_ID'),  
  //       'COMMUNE_ID'=>$this->input->post('COMMUNE_ID'), 
  //       );

  //    $this->form_validation->set_rules('GARAGE_NOM', 'Nom du garage', 'required');
  //    $this->form_validation->set_rules('PROVINCE_ID', 'Province', 'required');
  //    $this->form_validation->set_rules('COMMUNE_ID', 'Commune', 'required');
  //    $title = "Modification Garage";
  //    $GARAGE_ID =$this->input->post('GARAGE_ID');
     
  //   //SI Valide
  //    if ($this->form_validation->run() == FALSE){        
        
      
  //     // echo $GARAGE_ID;
  //     // 
  //     $onedetail = $this->Model->getOne('voiture_garage',array('GARAGE_ID'=>$GARAGE_ID));
  //     // print_r($onedetail);
  //     // exit();
  //     $approvisionnement = array(
  //              'GARAGE_ID'=>$onedetail['GARAGE_ID'],
  //              'GARAGE_NOM'=>$onedetail['GARAGE_NOM'],       
  //              'PROVINCE_ID'=>$onedetail['PROVINCE_ID'],       
  //              'COMMUNE_ID'=>$onedetail['COMMUNE_ID'],       
  //              'province'=>$this->Model->getList('syst_provinces',array()),
  //              'commune'=>$this->Model->getList('syst_communes',array()),
  //             ); 
    
        
  //       $data['title'] = $title;
  //       $data['approv'] = $approvisionnement;


  //   // $this->load->view("Garage_Update_View",$data);

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

  //     }else{
        
  //       $this->Model->update('voiture_garage',array('GARAGE_ID'=>$GARAGE_ID),$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('voiture/Garage/listing'));
  //     }
  // }

 }                        