<?php 
 /**
  * 
  */
 class Reparation extends CI_Controller
 {
  
  function __construct()
  {
    parent::__construct();
    $this->load->library('Mylibrary');
    $this->ci = & get_instance();
    $this->ci->load->library("user_agent");
    $this->Is_Connected();

    }

  public function Is_Connected()
       {

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

          public function Is_permis()
       {

       // if ($this->mylibrary->get_permission('Reparation') ==0)
       //  {
       //   redirect(base_url('Login/'));
       //  }
       }


  public function index()
  {
      
      $this->Is_permis();
      $title = "Enregistrement des Reparations";
      // $lastApprovisionnement = $this->Model->getOneOrder('voiture_reparation',array(),'REPARATION_ID');
      // $VAL = $lastApprovisionnement['REPARATION_ID']+1;
      // $NUM_GENERATED = 'REPA-'.$VAL;
      $NUM_GENERATED = $this->notifications->generate_UIID(10);

      $approvisionnement = array(
               'REPARATION_ID'=>NULL,                                   
               'NUM_GENERATED'=>$NUM_GENERATED,                                   
               'PATH_FACTURE'=>NULL,                                   
               'CONSOMATION_DATE'=>NULL,
               'VEHICULE_ID'=>$this->Model->getList('voiture_vehicules',array()), 
               'GARAGE_ID'=>$this->Model->getList('voiture_garage',array()), 
               'CHAUFFEUR_ID'=>$this->Model->getListOrdered('rh_employe','NOM',array()), 
               'QUANTITE_RECU'=>NULL, 
               'PIECE_ID'=>$this->Model->getList('voiture_piece',array()), 
               'PRIX_CARBURANT_ID'=>$this->Model->getList('voiture_prix_carburant',array()), 
               'MONTANT_CONSOMATION'=>NULL, 
               'PRIX_UNITAIRE'=>NULL, 
               'KM_A_APROVISIONNEMENT'=>NULL, 
              );

        
        $data['title'] = $title;
        // $data['pieces'] = $this->Model->getList("masque_piece");
        $data['approv'] = $approvisionnement;
      $this->load->view("Reparation_Add_View",$data);
  }


  public function save_piece_rechange()
  {
     
     $data = array(          
        'PIECE_NOM'=>$this->input->post('PIECE_NOM'),                                 
        'MARQUE_ID'=>$this->input->post('MARQUE_ID'),                                   
        'PIECE_CATEGORIE_ID'=>$this->input->post('PIECE_CATEGORIE_ID'),
        'UNITE_ID'=>0, 
        'NOMBRE_PAR_UNITE'=>0
        );
     
       $PIECE_ID = $this->Model->insert_last_id('masque_piece',$data);
        
        echo '<option value="'.$PIECE_ID.'">'.$this->input->post('PIECE_NOM').'</option>';
      
  }


  public function save_detail_reparation()
  {
     
     $data = array(          
        'PIECE_ID'=>$this->input->post('PIECE_ID'),                                 
        'REPARATION_FAITE'=>$this->input->post('REPARATION_FAITE'),                                   
        'MONTANT_UNITAIRE'=>$this->input->post('MONTANT_UNITAIRE'),
        'NUM_GENERATED'=>$this->input->post('NUM_GENERATED'), 
        );
     
       $this->Model->insert_last_id('voiture_reparation_details',$data);

       $list = $this->Model->getRequete('SELECT voiture_piece.PIECE_NOM AS PIECE_ID, voiture_reparation_details.REPARATION_FAITE, voiture_reparation_details.MONTANT_UNITAIRE FROM `voiture_reparation_details` JOIN voiture_piece ON voiture_piece.PIECE_ID = voiture_reparation_details.PIECE_ID WHERE voiture_reparation_details.NUM_GENERATED LIKE "'.$this->input->post('NUM_GENERATED').'" ');

       $somme = $this->Model->getRequeteOne('SELECT SUM(MONTANT_UNITAIRE) AS MONTANT_TOTAL FROM `voiture_reparation_details` WHERE NUM_GENERATED LIKE "'.$this->input->post('NUM_GENERATED').'"');

       $results = "<table class='table'><tr><td>Piece</td><td>Reparation</td><td>Montant</td><td></td></tr>";
       foreach ($list as $key) {
         $results .= "<tr><td>".$key['PIECE_ID']."</td><td>".$key['REPARATION_FAITE']."</td><td class='text-right'>".$key['MONTANT_UNITAIRE']."</td>
         <td>
         <a class='btn btn-danger btn-xs' href='#' role='button'>Enlever</a>
         </td>
         </tr>";
       }
       $results .= "<tr><td colspan='2'><b>Montant Total de la facture</b></td><td class='text-right'><b>".$somme['MONTANT_TOTAL']."</b></td><td></td></tr></table>";

        echo $results;
      
  }

          
          
            


  public function getlastkm()
  {
    $VEHICULE_ID = $this->input->post('VEHICULE_ID');
    $lastkm = $this->Model->getOneOrder('voiture_carburant_consomation',array('VEHICULE_ID'=>$VEHICULE_ID,'ID_STATUS'=>1,'PANNE_TABLEAU'=>0),'KM_A_APROVISIONNEMENT');

    if ($lastkm['KM_A_APROVISIONNEMENT'] == NULL) {
      echo 0;
    }
    else{
      echo $lastkm['KM_A_APROVISIONNEMENT'];
    }

    
  }

  public function save_reparation()
  {


        $myDateTime = DateTime::createFromFormat('d/m/Y', $this->input->post('CONSOMATION_DATE'));
        $newdate = $myDateTime->format('Y-m-d');

        $browser=$this->ci->agent->browser.' version '.$this->ci->agent->version;
        $os=$this->ci->agent->platform;
        $ip = $_SERVER["REMOTE_ADDR"];
        $dt = new DateTime("now", new DateTimeZone('Africa/Bujumbura'));
        $new_dt = $dt->format('Y-m-d H:i:s');

     // $approvisionnement = array(          
     //    'NUM_GENERATED'=>$this->input->post('NUM_GENERATED'),                                 
     //    'VEHICULE_ID'=>$this->Model->getList('voiture_vehicules',array()), 
     //    'GARAGE_ID'=>$this->Model->getList('voiture_garage',array()), 
     //    'QUANTITE_RECU'=>$this->input->post('QUANTITE_RECU'),
     //    'PROVENANCEOUDESTINATION'=>$this->input->post('PROVENANCEOUDESTINATION'),
     //    // 'TRAJET_ID'=>$this->Model->getList('masque_trajet',array()), 
     //    'MONTANT_CONSOMATION'=>$this->input->post('MONTANT_CONSOMATION'),
     //    'PRIX_UNITAIRE'=>$this->input->post('PRIX_UNITAIRE'),
     //    'CONSOMATION_DATE'=>$this->input->post('CONSOMATION_DATE'),
     //    'KM_A_APROVISIONNEMENT'=>$this->input->post('KM_A_APROVISIONNEMENT'),
     //    'PATH_FACTURE'=>$this->input->post('PATH_FACTURE'),
     //    'CHAUFFEUR_ID'=>$this->input->post('CHAUFFEUR_ID'),
     //    );
        $title = "Enregistrement des Reparations";
      // $lastApprovisionnement = $this->Model->getOneOrder('voiture_reparation',array(),'REPARATION_ID');
      // $VAL = $lastApprovisionnement['REPARATION_ID']+1;
      // $NUM_GENERATED = 'REPA-'.$VAL;
      // echo $NUM_GENERATED;
      // exit();
        $NUM_GENERATED = $this->input->post('NUM_GENERATED');
      $approvisionnement = array(
               'REPARATION_ID'=>NULL,                                   
               'NUM_GENERATED'=>$NUM_GENERATED,                                   
               'PATH_FACTURE'=>NULL,                                   
               'CONSOMATION_DATE'=>NULL,
               'VEHICULE_ID'=>$this->Model->getList('voiture_vehicules',array()), 
               'GARAGE_ID'=>$this->Model->getList('voiture_garage',array()), 
               'CHAUFFEUR_ID'=>$this->Model->getListOrdered('rh_employe','NOM',array()), 
               'QUANTITE_RECU'=>NULL, 
               'PIECE_ID'=>$this->Model->getList('voiture_piece',array()), 
               'PRIX_CARBURANT_ID'=>$this->Model->getList('voiture_prix_carburant',array()), 
               'MONTANT_CONSOMATION'=>NULL, 
               'PRIX_UNITAIRE'=>NULL, 
               'KM_A_APROVISIONNEMENT'=>NULL, 
              );
    
     
     
     $data = array(          
        'CONSOMATION_DATE'=>$newdate,
        'VEHICULE_ID'=>$this->input->post('VEHICULE_ID'),  
        'GARAGE_ID'=>$this->input->post('GARAGE_ID'),
        'MONTANT_CONSOMATION'=>$this->input->post('MONTANT_CONSOMATION'),
        'NUM_GENERATED'=>$this->input->post('NUM_GENERATED'),  
        'USERSAVER'=>$this->session->userdata('SOCAR_ID_USER'),
        'CHAUFFEUR_ID'=>$this->input->post('CHAUFFEUR_ID'),
        'MONTANT_CONSOMATION'=>0,
        'BUJUMBURA_TIME'=>$new_dt,
        'NAVIGATEUR'=>$browser,
        'OPERATING_SYSTEM'=>$os,
        'INTERNET_IP'=>$ip,        
        );
     // echo "<pre>";
     // print_r($data);
     // exit();


     $this->form_validation->set_rules('CONSOMATION_DATE', 'Date facturation', 'required');
     $this->form_validation->set_rules('VEHICULE_ID', 'Vehicule', 'required');
     $this->form_validation->set_rules('GARAGE_ID', 'Garage', 'required');
     $this->form_validation->set_rules('CHAUFFEUR_ID', 'Chauffeur', 'required');
     
     
     

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

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

        $data['title'] = $title;        
        $data['approv'] = $approvisionnement;
        $this->session->set_flashdata(array('message'=>$message));
       $this->load->view("Reparation_Add_View",$data);

      }else{
        $REPARATION_ID = $this->Model->insert_last_id('voiture_reparation',$data);
        $sommes = $this->Model->getRequeteOne('SELECT SUM(MONTANT_UNITAIRE) AS MONTANT_UNITAIRE FROM voiture_reparation_details WHERE NUM_GENERATED LIKE "'.$this->input->post('NUM_GENERATED').'"');


        // echo "<pre>";
        // print_r(array('NUM_GENERATED'=>$NUM_GENERATED));
        // print_r(array('REPARATION_ID'=>$REPARATION_ID));
        // print_r(array('MONTANT_CONSOMATION'=>$sommes['MONTANT_UNITAIRE']));
        // echo "</pre>";
        // exit();
        $this->Model->update('voiture_reparation_details',array('NUM_GENERATED'=>$NUM_GENERATED),array('REPARATION_ID'=>$REPARATION_ID));



        $this->Model->update('voiture_reparation',array('NUM_GENERATED'=>$NUM_GENERATED),array('MONTANT_CONSOMATION'=>$sommes['MONTANT_UNITAIRE']));
        
          $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/Reparation/'));
      }
  }



  public function listing()
    {
      $PERIODE = $this->input->post('PERIODE');
      $VEHICULE_ID = $this->input->post('VEHICULE_ID');
      $data['GARAGE_IDS']= $this->input->post('GARAGE_ID');
      $GARAGE_ID = $this->input->post('GARAGE_ID');
      if ($GARAGE_ID != NULL) {
        $data['condstation'] = 'AND car.GARAGE_ID ='.$GARAGE_ID.' ';
        $statt = $this->Model->getOne('voiture_station',array('GARAGE_ID'=>$GARAGE_ID));
        $stat= ' ,station: '.$statt['GARAGE_NOM'];
      }
      else{
        $data['condstation'] = '';
        $stat= '';
      }
      $data['VEHICULE_IDS']= $this->input->post('VEHICULE_ID');
      if ($VEHICULE_ID !=NULL) {
        $data['condvehicle']='AND car.VEHICULE_ID ='.$VEHICULE_ID.' ';
        $voiture = $this->Model->getOne('voiture_vehicules',array('VEHICULE_ID'=>$VEHICULE_ID));
        $vehi = ', voiture '.$voiture['PLAQUE_VEHICULE'];
      }
      else{
        $data['condvehicle']='';
        $vehi = '';
      }

      if ($PERIODE != null) {
      $str = $PERIODE;
      $dat=explode(" - ",$str);

     $date1 = DateTime::createFromFormat('d/m/Y', $dat[0]);
     $debut = $date1->format('Y-m-d');
     $date2 = DateTime::createFromFormat('d/m/Y', $dat[1]);
     $fin = $date2->format('Y-m-d');
     $data['conditions'] = ' AND CONSOMATION_DATE BETWEEN "'.$debut.'" AND "'.$fin.'"';
     $selectdates = 'Entre '.$debut.' et '.$fin;
     $data['selectdate'] = $PERIODE;
      }
      else{
        $thismonth = date("Y-m");
        $selectdates = date("Y-m");
        $data['conditions'] = ' AND CONSOMATION_DATE LIKE "'.$thismonth.'%"';
        $data['selectdate'] = '';
      }

      $data['options'] = $this->input->post('options');
      $options = $this->input->post('options');
      if ($options == NULL) {
        // echo "NULL";
        $data['selection'] = NULL;
          $data['a0'] ='';
          $data['a1'] ='active';
          $data['a2'] ='';
          $data['a3'] ='';
          $data['a4'] =''; 
          $data['ac0'] ='';
          $data['ac1'] ='checked';
          $data['ac2'] ='';
          $data['ac3'] ='';
          $data['ac4'] ='';  

          
          $data['condition']='car.IS_PAYE = 0';
      }
      else{
        // echo "Quelque chose";
        $data['selection'] = $options;
        // echo $options;
        // echo "<br>";
        if ($options == 0) {
          $data['a0'] ='active';
          $data['a1'] ='';
          $data['a2'] ='';
          $data['a3'] ='';
          $data['a4'] ='';
          $data['ac0'] ='checked';
          $data['ac1'] ='';
          $data['ac2'] ='';
          $data['ac3'] ='';
          $data['ac4'] =''; 
          $data['condition']=' 1';
        }
        elseif ($options == 1) {
          $data['a0'] ='';
          $data['a1'] ='active';
          $data['a2'] ='';
          $data['a3'] ='';
          $data['a4'] ='';  
          $data['ac0'] ='';
          $data['ac1'] ='checked';
          $data['ac2'] ='';
          $data['ac3'] ='';
          $data['ac4'] =''; 
          $data['condition']='car.IS_PAYE = 0';        
        }
        elseif ($options == 2) {
          $data['a0'] ='';
          $data['a1'] ='';
          $data['a2'] ='active';
          $data['a3'] ='';
          $data['a4'] ='';
          $data['ac0'] ='';
          $data['ac1'] ='';
          $data['ac2'] ='checked';
          $data['ac3'] ='';
          $data['ac4'] =''; 
          $data['condition']='car.IS_PAYE = 1';
        }
        elseif ($options == 3) {
          $data['a0'] ='';
          $data['a1'] ='';
          $data['a2'] ='';
          $data['a3'] ='active';
          $data['a4'] ='';
          $data['ac0'] ='';
          $data['ac1'] ='';
          $data['ac2'] ='';
          $data['ac3'] ='checked';
          $data['ac4'] =''; 
          $data['condition']='car.ID_STATUS = 2';
        }
        elseif ($options == 4) {
          $data['a0'] ='';
          $data['a1'] ='';
          $data['a2'] ='';
          $data['a3'] ='';
          $data['a4'] ='active';
          $data['ac0'] ='';
          $data['ac1'] ='';
          $data['ac2'] ='';
          $data['ac3'] ='';
          $data['ac4'] ='checked'; 
          $data['condition']='car.ID_STATUS = 4 or car.ID_STATUS = 3';
        }


      }

    
      
        $data['selectdate'] = $PERIODE;
        $data['title'] = "Reparation des voitures date: ".$selectdates." ".$vehi." ".$stat." ";
        $this->load->view('Reparation_List_View',$data);

    }

 }                        