<?php
class Traite_Requisition_Externe  extends CI_Controller{
    function __construct() {
        parent::__construct();
        $this->Is_Connected();

      }

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

        


  
    // public function index()
    // {

    //   $data['title']='Demande externe';

    //   $data['NUM_GENERATED']=$this->notifications->generate_UIID(10);
    //   $data['responsabilite']=$this->Model->getRequete('SELECT * FROM `rh_responsabilite` order by DESCRIPTION');
    //   $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
    //   $data['fournisseur']=$this->Model->getRequete('SELECT * FROM `req_externe_fournisseur` order by NOM_FOURNISSEUR');
    //   $data['types']=$this->Model->getRequete('SELECT * FROM `req_externe_type_budge` WHERE 1  order by DESCRIPTION');
    //   $data['rh']=$this->Model->getRequete('SELECT * FROM `rh_employe` order by NOM');
      
    //   $this->load->view('Requisition_Externe_Add_View',$data);

    // }

  //   public function get_rubrique()
  // {
  // $departement= $this->Model->getList("req_externe_budge_plannifie",array('ID_TYPE_BUDGE'=>$this->input->post('ID_TYPE_BUDGE')));
  // $datas= '<option value="">-- Sélectionner --</option>';
  // foreach($departement as $sele_departement){
  // $datas.= '<option value="'.$sele_departement["ID_BUDGE_PLANNIFIE"].'">'.$sele_departement["RUBRIQUE"].'</option>';
  // }
  // $datas.= '';
  // echo $datas;
  // }

  
  // public function get_restant()
  // {
  // $resultat= $this->Model->getOne("req_externe_budge_plannifie",array('ID_BUDGE_PLANNIFIE'=>$this->input->post('ID_BUDGE_PLANNIFIE')));

  // $datas= '<div class="form-group col-md-3">
  // <b>Detail sur la rublique '.$resultat['RUBRIQUE'].'</b>
  //    </div>
  //     <div class="form-group col-md-3 text-right text-danger">
  //       <b>Total prévu '.number_format($resultat['BUDGE_PREVU'],0,","," ").' BIF</b>                       
  //     </div>
  //     <div class="form-group col-md-3 text-right text-danger">
  //       <b>Total Consomé '.number_format($resultat['BUDGE_CONSOME'],0,","," ").' BIF</b>                       
  //     </div>
  //     <div class="form-group col-md-3 text-right text-danger">
  //       <b>Total disponible maintenant '.number_format($resultat['BUDGE_DISPONIBLE'],0,","," ").' BIF</b>                       
  //     </div>';
  // // foreach($departement as $sele_departement){
  // // $datas.= '<option value="'.$sele_departement["ID_BUDGE_PLANNIFIE"].'">'.$sele_departement["RUBRIQUE"].'</option>';
  // // }
  // // $datas.= '';
  // echo $datas;
  // }

  //   public function save_element()
  // {
     
  //    $data = array(          
  //       'ARTICLE_ELEMENT'=>$this->input->post('ARTICLE_ELEMENT'),
  //       'QUANTITE_DEMANDE'=>$this->input->post('QUANTITE_DEMANDE'),     
  //       'MONTANT_UNITAIRE_DEMANDE'=>$this->input->post('MONTANT_UNITAIRE_DEMANDE'),
  //       'ID_REQ_EXTERNE'=>$this->input->post('NUM_GENERATED'),
  //       'MONTANT_TOTAL'=>$this->input->post('QUANTITE_DEMANDE') * $this->input->post('MONTANT_UNITAIRE_DEMANDE'),
       
  //       );
     
  //      $this->Model->insert_last_id('req_externe_document_detail',$data);
        
  //       $list = $this->Model->getRequete('SELECT ID_REQ_EXTERNE,ID_REQ_EXTERNE_DETAIL, ARTICLE_ELEMENT, QUANTITE_DEMANDE, MONTANT_UNITAIRE_DEMANDE, MONTANT_TOTAL FROM `req_externe_document_detail` WHERE ID_REQ_EXTERNE LIKE "'.$this->input->post('NUM_GENERATED').'" ');

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

  //      $results = "<table class='table'><tr><td>Article</td><td>Quantite</td><td>Montant</td><td>Total</td><td></td></tr>";
  //      foreach ($list as $key) {
  //       $tot = $key['MONTANT_UNITAIRE_DEMANDE'] * $key['QUANTITE_DEMANDE'];
  //        $results .= "<tr><td>".$key['ARTICLE_ELEMENT']."</td><td class='text-right'>".$key['QUANTITE_DEMANDE']."</td><td class='text-right'>".$key['MONTANT_UNITAIRE_DEMANDE']."</td><td class='text-right'>".$key['MONTANT_TOTAL']."</td>
  //        <td>
  //        <a class='btn btn-danger btn-xs' role='button' onclick='removeElement(".$key['ID_REQ_EXTERNE_DETAIL'].")'>Enlever</a>
  //        </td>
  //        </tr>";
  //      }
  //      $results .= "<tr><td colspan='3'><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 removeElement()
  // {
     
     
     
  //      $this->Model->delete('req_externe_document_detail',array('ID_REQ_EXTERNE_DETAIL'=>$this->input->post('ID_REQ_EXTERNE_DETAIL')));
        
  //       $list = $this->Model->getRequete('SELECT ID_REQ_EXTERNE,ID_REQ_EXTERNE_DETAIL, ARTICLE_ELEMENT, QUANTITE_DEMANDE, MONTANT_UNITAIRE_DEMANDE, MONTANT_TOTAL FROM `req_externe_document_detail` WHERE ID_REQ_EXTERNE LIKE "'.$this->input->post('NUM_GENERATED').'" ');

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

  //      $results = "<table class='table'><tr><td>Article</td><td>Quantite</td><td>Montant</td><td>Total</td><td></td></tr>";
  //      foreach ($list as $key) {
  //       $tot = $key['MONTANT_UNITAIRE_DEMANDE'] * $key['QUANTITE_DEMANDE'];
  //        $results .= "<tr><td>".$key['ARTICLE_ELEMENT']."</td><td class='text-right'>".$key['QUANTITE_DEMANDE']."</td><td class='text-right'>".$key['MONTANT_UNITAIRE_DEMANDE']."</td><td class='text-right'>".$key['MONTANT_TOTAL']."</td>
  //        <td>
  //        <a class='btn btn-danger btn-xs' role='button' onclick='removeElement(".$key['ID_REQ_EXTERNE_DETAIL'].")'>Enlever</a>
  //        </td>
  //        </tr>";
  //      }
  //      $results .= "<tr><td colspan='3'><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 add()
  //   {
           
            
      
  //     $ID_DIRECTION=$this->input->post('ID_DIRECTION');
  //     $ID_SERVICE=$this->input->post('ID_SECTION');
  //     $ID_SERVICE=$this->input->post('ID_SERVICE');
  //     $ID_SECTION=$this->input->post('ID_SECTION');
  //     $ID_EMPLOYE=$this->input->post('ID_EMPLOYE');
  //     $NUM_GENERATED=$this->input->post('NUM_GENERATED');
  //     $ID_FOURNISSEUR=$this->input->post('ID_FOURNISSEUR');
  //     $FICHIERS=$this->input->post('FICHIERS');
  //     $MOTIVATION=$this->input->post('MOTIVATION');
  //     $ID_BUDGE_PLANNIFIE=$this->input->post('ID_BUDGE_PLANNIFIE');

      





  //     $this->form_validation->set_rules('ID_DIRECTION', 'Direction', 'required');
  //     $this->form_validation->set_rules('ID_EMPLOYE', 'Employ&eacute;', 'required');
     
  

  //  if ($this->form_validation->run() == FALSE){
  //   $message = "<div class='alert alert-danger'>
  //                             Congé non enregistr&eacute;
  //                           <button type='button' class='close' data-dismiss='alert'>&times;</button>
  //                     </div>";
  //   $this->session->set_flashdata(array('message'=>$message));
  //   $data['title']='Demande externe';

  //     $data['NUM_GENERATED']=$this->notifications->generate_UIID(10);
  //     $data['responsabilite']=$this->Model->getRequete('SELECT * FROM `rh_responsabilite` order by DESCRIPTION');
  //     $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
  //     $data['fournisseur']=$this->Model->getRequete('SELECT * FROM `req_externe_fournisseur` order by NOM_FOURNISSEUR');
  //     $data['types']=$this->Model->getRequete('SELECT * FROM `req_externe_type_budge` WHERE 1  order by DESCRIPTION');
  //     $data['rh']=$this->Model->getRequete('SELECT * FROM `rh_employe` order by NOM');
      
  //     $this->load->view('Requisition_Externe_Add_View',$data);

      
  //  }
  //  else{



  //   // $ID_REQ_ETAPE_DOC_INTERNE = $this->Model->getRequeteOne('SELECT ID_REQ_ETAPE_DOC_INTERNE FROM req_externe_etape WHERE NUMERO =1 AND ID_TYPE_DOC_INTERNE='.$ID_TYPE_DOC_INTERNE.'');

  //   $data_conge=array(
  //     'ID_EMPLOYE_DEMANDEUR'=>$ID_EMPLOYE,
  //     'ID_REQ_ETAPE_DOC_INTERNE'=>1,
  //     'ID_DIRECTION'=>$ID_DIRECTION,
  //     'ID_SERVICE'=>$ID_SERVICE,
  //     'ID_SECTION'=>$ID_SECTION,
  //     'ID_TYPE_DOC_INTERNE'=>$ID_TYPE_DOC_INTERNE,
  //     'MOTIVATION'=>$MOTIVATION,
  //     'ID_FOURNISSEUR'=>$ID_FOURNISSEUR,
  //     'ID_BUDGE_PLANNIFIE'=>$ID_BUDGE_PLANNIFIE,
  //   );
  //   // echo'<pre>';
  //   // print_r($data_conge);
  //   // echo "</pre>";

  //   // exit();



  //   $ID_REQ_EXTERNE = $this->Model->insert_last_id('req_externe_document',$data_conge);


  //   $errorUploadType = $statusMsg = ''; 

              
  //               $filesCount = count($_FILES['files']['name']); 
  //               for($i = 0; $i < $filesCount; $i++){ 
  //                   $_FILES['file']['name']     = $_FILES['files']['name'][$i]; 
  //                   $_FILES['file']['type']     = $_FILES['files']['type'][$i]; 
  //                   $_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i]; 
  //                   $_FILES['file']['error']     = $_FILES['files']['error'][$i]; 
  //                   $_FILES['file']['size']     = $_FILES['files']['size'][$i]; 
                     
  //                   // File upload configuration 
  //                   $uploadPath = 'uploads/fichier_demande_interne/'; 
  //                   $config['upload_path'] = $uploadPath; 
  //                   $config['allowed_types'] = 'jpg|jpeg|png|pdf'; 
                     
  //                   // Load and initialize upload library 
  //                   $this->load->library('upload', $config); 
  //                   $this->upload->initialize($config); 
                     
  //                   // Upload file to server 
  //                   if($this->upload->do_upload('file')){ 
  //                       // Uploaded file data 
  //                       $fileData = $this->upload->data(); 
  //                       $uploadData[$i]['file_name'] = $fileData['file_name']; 
  //                       $uploadData[$i]['uploaded_on'] = date("Y-m-d H:i:s"); 

  //                       $this->Model->insert_last_id('req_externe_document_fichier',array('ID_REQ_EXTERNE'=>$ID_REQ_EXTERNE,'FICHIERS'=>$uploadData[$i]['file_name']));
  //                       print_r(array('ID_REQ_EXTERNE'=>$ID_REQ_EXTERNE,'FICHIERS'=>$fileData['file_name']));
  //                       // exit();
  //                   }else{  
  //                       echo $errorUploadType .= $_FILES['file']['name'].' |<br> ';  
  //                   } 
  //               } 


  //   $sommes=$this->Model->getRequeteOne('SELECT SUM(MONTANT_TOTAL) AS MONTANT_TOTAL FROM `req_externe_document_detail` WHERE ID_REQ_EXTERNE = "'.$NUM_GENERATED.'"');

  //   // print_r($details);
  //   // exit();

  //   $this->Model->update('req_externe_document_detail',array('ID_REQ_EXTERNE'=>$NUM_GENERATED),array('ID_REQ_EXTERNE'=>$ID_REQ_EXTERNE));
  //   $this->Model->update('req_externe_document',array('ID_REQ_EXTERNE'=>$ID_REQ_EXTERNE),array('MONTANT_TOTAL'=>$sommes['MONTANT_TOTAL']));          


  //   $message = "<div class='alert alert-success' id='message'>
  //                           Demande enregistr&eacute; avec succés
  //                           <button type='button' class='close' data-dismiss='alert'>&times;</button>
  //                     </div>";
  //   $this->session->set_flashdata(array('message'=>$message));
  //     redirect(base_url('requisition_externe/Requisition_Externe/listing'));  
  //  }

  //   }


      public function index()
    {
        // echo $this->session->userdata('ID_EMPLOYE');
        // exit();
       $statusc = ' ';
              
      $data['resultat']=$this->Model->getRequete('SELECT req_externe_document.ID_REQ_EXTERNE, req_externe_document.DATE_DEMANDE, req_externe_document.ID_EMPLOYE_DEMANDEUR, rh_employe.NOM, rh_employe.PRENOM, req_externe_document.ID_REQ_ETAPE_DOC_INTERNE, req_externe_etape.DESCRIPTION AS ETAPE, req_externe_document.ID_DIRECTION, organigramme_direction.DESCRIPTION AS DIRECTION , req_externe_document.ID_SERVICE, organigramme_service.DESCRIPTION AS SERVICE, req_externe_document.ID_SECTION, organigramme_section.DESCRIPTION AS SECTION, req_externe_document.MONTANT_TOTAL, req_externe_document.ID_BUDGE_PLANNIFIE, req_externe_budge_plannifie.RUBRIQUE, req_externe_etape.ON_PDF, req_externe_document.ID_FOURNISSEUR, req_externe_document.MOTIVATION, req_externe_fournisseur.NOM_FOURNISSEUR, req_externe_etape.ID_PROFIL FROM `req_externe_document` JOIN rh_employe ON rh_employe.ID_EMPLOYE = req_externe_document.ID_EMPLOYE_DEMANDEUR JOIN req_externe_etape ON req_externe_etape.ID_REQ_ETAPE_DOC_INTERNE = req_externe_document.ID_REQ_ETAPE_DOC_INTERNE JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = req_externe_document.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = req_externe_document.ID_SERVICE LEFT JOIN organigramme_section ON req_externe_document.ID_SECTION = organigramme_section.ID_SECTION JOIN req_externe_budge_plannifie ON req_externe_budge_plannifie.ID_BUDGE_PLANNIFIE = req_externe_document.ID_BUDGE_PLANNIFIE JOIN req_externe_fournisseur ON req_externe_fournisseur.ID_FOURNISSEUR = req_externe_document.ID_FOURNISSEUR WHERE 1 AND ID_PROFIL = '.$this->session->userdata('SOCAR_PROFIL_ID').' OR ID_EMPLOYE_DESINGE = '.$this->session->userdata('ID_EMPLOYE').'');

      $data['title']='Demande externe';
      // $data['typeconge'] = $this->Model->getRequete('SELECT * FROM `conge_status_conge` WHERE 1');
      $this->load->view('Requisition_Externe_List_View',$data);          

    }

      public function dejatraiter()
    {

     

            
      $statusc = ' ';
              
      $data['resultat']=$this->Model->getRequete('SELECT req_externe_document.ID_REQ_EXTERNE, req_externe_document.DATE_DEMANDE, req_externe_document.ID_EMPLOYE_DEMANDEUR, rh_employe.NOM, rh_employe.PRENOM, req_externe_document.ID_REQ_ETAPE_DOC_INTERNE, req_externe_etape.DESCRIPTION AS ETAPE, req_externe_document.ID_DIRECTION, organigramme_direction.DESCRIPTION AS DIRECTION , req_externe_document.ID_SERVICE, organigramme_service.DESCRIPTION AS SERVICE, req_externe_document.ID_SECTION, organigramme_section.DESCRIPTION AS SECTION, req_externe_document.MONTANT_TOTAL, req_externe_document.ID_BUDGE_PLANNIFIE, req_externe_budge_plannifie.RUBRIQUE, req_externe_etape.ON_PDF, req_externe_document.ID_FOURNISSEUR, req_externe_document.MOTIVATION, req_externe_fournisseur.NOM_FOURNISSEUR, req_externe_etape.ID_PROFIL FROM `req_externe_document` JOIN rh_employe ON rh_employe.ID_EMPLOYE = req_externe_document.ID_EMPLOYE_DEMANDEUR JOIN req_externe_etape ON req_externe_etape.ID_REQ_ETAPE_DOC_INTERNE = req_externe_document.ID_REQ_ETAPE_DOC_INTERNE JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = req_externe_document.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = req_externe_document.ID_SERVICE LEFT JOIN organigramme_section ON req_externe_document.ID_SECTION = organigramme_section.ID_SECTION JOIN req_externe_budge_plannifie ON req_externe_budge_plannifie.ID_BUDGE_PLANNIFIE = req_externe_document.ID_BUDGE_PLANNIFIE JOIN req_externe_fournisseur ON req_externe_fournisseur.ID_FOURNISSEUR = req_externe_document.ID_FOURNISSEUR WHERE 1  AND req_externe_document.ID_REQ_ETAPE_DOC_INTERNE = 8');

      $data['title']='Demande externe';
      // $data['typeconge'] = $this->Model->getRequete('SELECT * FROM `conge_status_conge` WHERE 1');
      $this->load->view('Requisition_Externe_List_View',$data);

    }

public function traitement($id)
    {

      $data['data']=$this->Model->getRequeteOne('SELECT req_externe_document.ID_REQ_EXTERNE, req_externe_document.ID_EMPLOYE_DESINGE, req_externe_document.MOTIVATION, req_externe_document.DATE_DEMANDE, req_externe_document.ID_REQ_ETAPE_DOC_INTERNE, rh_employe.NOM, rh_employe.PRENOM, req_externe_etape.DESCRIPTION AS ETAPE, req_externe_etape.ID_REQ_ETAPE_DOC_INTERNE, organigramme_direction.DESCRIPTION AS ID_DIRECTION,  organigramme_service.DESCRIPTION AS ID_SERVICE, organigramme_section.DESCRIPTION AS ID_SECTION,        req_externe_document.ID_BUDGE_PLANNIFIE, req_externe_budge_plannifie.RUBRIQUE, req_externe_budge_plannifie.BUDGE_PREVU, req_externe_budge_plannifie.BUDGE_CONSOME, req_externe_budge_plannifie.BUDGE_DISPONIBLE, req_externe_document.ID_FOURNISSEUR, req_externe_fournisseur.NOM_FOURNISSEUR, req_externe_document.MONTANT_TOTAL FROM     req_externe_document JOIN rh_employe ON rh_employe.ID_EMPLOYE = req_externe_document.ID_EMPLOYE_DEMANDEUR      JOIN req_externe_etape ON req_externe_etape.ID_REQ_ETAPE_DOC_INTERNE = req_externe_document.ID_REQ_ETAPE_DOC_INTERNE JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = req_externe_document.ID_DIRECTION   LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = req_externe_document.ID_SERVICE  LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION =  req_externe_document.ID_SECTION   JOIN req_externe_fournisseur ON req_externe_fournisseur.ID_FOURNISSEUR = req_externe_document.ID_FOURNISSEUR    JOIN req_externe_budge_plannifie ON req_externe_budge_plannifie.ID_BUDGE_PLANNIFIE = req_externe_document.ID_BUDGE_PLANNIFIE WHERE req_externe_document.ID_REQ_EXTERNE = '.$id.' ');

      $data['historique']=$this->Model->getRequete('SELECT req_externe_document_historique.ID_REQ_EXTERNE, req_externe_document_historique.DATE_TRAITEMENT, organigramme_direction.DESCRIPTION AS DIRECTION, organigramme_section.DESCRIPTION AS SECTION, organigramme_service.DESCRIPTION AS SERVICE, rh_employe.NOM, rh_employe.PRENOM, req_externe_etape.DESCRIPTION, req_externe_document_historique.MONTANT_TOTAL, req_externe_document_historique.COMMENTAIRE,req_externe_action.DESCRIPTION AS ACTIONS FROM `req_externe_document_historique` JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = req_externe_document_historique.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = req_externe_document_historique.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = req_externe_document_historique.ID_SECTION JOIN rh_employe ON rh_employe.ID_EMPLOYE = req_externe_document_historique.ID_EMPLOYE_TRAITANT JOIN req_externe_etape ON req_externe_etape.ID_REQ_ETAPE_DOC_INTERNE = req_externe_document_historique.ID_REQ_ETAPE_DOC_INTERNE JOIN req_externe_action ON req_externe_action.ID_REQ_EXTERNE_ACTION = req_externe_document_historique.ID_REQ_EXTERNE_ACTION WHERE 1 AND ID_REQ_EXTERNE = '.$id.'');

      $data['details_demande'] = $this->Model->getRequete('SELECT * FROM req_externe_document_detail WHERE 1 AND ID_REQ_EXTERNE = '.$id.'');
      $data['fichier_demande'] = $this->Model->getRequete('SELECT * FROM req_externe_document_fichier WHERE 1 AND ID_REQ_EXTERNE = '.$id.'');





      $data['title']='Demande interne';
      $this->load->view('Requisition_Externe_Traitement_View',$data);

    }
    


    public function traiter()
    {
           
      
      
      
      // echo"---";
      // exit();
      

      $ID_REQ_ETAPE_DOC_INTERNE=$this->input->post('ID_REQ_ETAPE_DOC_INTERNE');
      $ID_REQ_EXTERNE=$this->input->post('ID_REQ_EXTERNE');
      $ID_REQ_EXTERNE_ACTION=$this->input->post('ID_REQ_EXTERNE_ACTION');
     
      $ID_EMPLOYE=$this->input->post('ID_EMPLOYE');
      $MONTANT_TOTAL=$this->input->post('MONTANT_TOTAL');
      $ID_EMPLOYE=$this->input->post('ID_EMPLOYE');
      $COMMENTAIRE=$this->input->post('COMMENTAIRE');
      $ID_EMPLOYE_DESINGE=$this->input->post('ID_EMPLOYE_DESINGE');

      

      $this->form_validation->set_rules('ID_REQ_EXTERNE_ACTION', 'Decision', 'required');
     
  

   if ($this->form_validation->run() == FALSE){
    $message = "<div class='alert alert-danger'>
                              Requisition non enregistr&eacute;
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));

    $id  = $ID_REQ_EXTERNE;
    
    $data['data']=$this->Model->getRequeteOne('SELECT req_externe_document.ID_REQ_EXTERNE, req_externe_document.ID_EMPLOYE_DESINGE, req_externe_document.MOTIVATION, req_externe_document.DATE_DEMANDE, req_externe_document.ID_REQ_ETAPE_DOC_INTERNE, rh_employe.NOM, rh_employe.PRENOM, req_externe_etape.DESCRIPTION AS ETAPE, req_externe_etape.ID_REQ_ETAPE_DOC_INTERNE, organigramme_direction.DESCRIPTION AS ID_DIRECTION,  organigramme_service.DESCRIPTION AS ID_SERVICE, organigramme_section.DESCRIPTION AS ID_SECTION,        req_externe_document.ID_BUDGE_PLANNIFIE, req_externe_budge_plannifie.RUBRIQUE, req_externe_budge_plannifie.BUDGE_PREVU, req_externe_budge_plannifie.BUDGE_CONSOME, req_externe_budge_plannifie.BUDGE_DISPONIBLE, req_externe_document.ID_FOURNISSEUR, req_externe_fournisseur.NOM_FOURNISSEUR, req_externe_document.MONTANT_TOTAL FROM     req_externe_document JOIN rh_employe ON rh_employe.ID_EMPLOYE = req_externe_document.ID_EMPLOYE_DEMANDEUR      JOIN req_externe_etape ON req_externe_etape.ID_REQ_ETAPE_DOC_INTERNE = req_externe_document.ID_REQ_ETAPE_DOC_INTERNE JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = req_externe_document.ID_DIRECTION   LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = req_externe_document.ID_SERVICE  LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION =  req_externe_document.ID_SECTION   JOIN req_externe_fournisseur ON req_externe_fournisseur.ID_FOURNISSEUR = req_externe_document.ID_FOURNISSEUR    JOIN req_externe_budge_plannifie ON req_externe_budge_plannifie.ID_BUDGE_PLANNIFIE = req_externe_document.ID_BUDGE_PLANNIFIE WHERE req_externe_document.ID_REQ_EXTERNE = '.$id.' ');

      $data['historique']=$this->Model->getRequete('SELECT req_externe_document_historique.ID_REQ_EXTERNE, req_externe_document_historique.DATE_TRAITEMENT, organigramme_direction.DESCRIPTION AS DIRECTION, organigramme_section.DESCRIPTION AS SECTION, organigramme_service.DESCRIPTION AS SERVICE, rh_employe.NOM, rh_employe.PRENOM, req_externe_etape.DESCRIPTION, req_externe_document_historique.MONTANT_TOTAL, req_externe_document_historique.COMMENTAIRE,req_externe_action.DESCRIPTION AS ACTIONS FROM `req_externe_document_historique` JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = req_externe_document_historique.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = req_externe_document_historique.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = req_externe_document_historique.ID_SECTION JOIN rh_employe ON rh_employe.ID_EMPLOYE = req_externe_document_historique.ID_EMPLOYE_TRAITANT JOIN req_externe_etape ON req_externe_etape.ID_REQ_ETAPE_DOC_INTERNE = req_externe_document_historique.ID_REQ_ETAPE_DOC_INTERNE JOIN req_externe_action ON req_externe_action.ID_REQ_EXTERNE_ACTION = req_externe_document_historique.ID_REQ_EXTERNE_ACTION WHERE 1 AND ID_REQ_EXTERNE = '.$id.'');

      $data['details_demande'] = $this->Model->getRequete('SELECT * FROM req_externe_document_detail WHERE 1 AND ID_REQ_EXTERNE = '.$id.'');
      $data['fichier_demande'] = $this->Model->getRequete('SELECT * FROM req_externe_document_fichier WHERE 1 AND ID_REQ_EXTERNE = '.$id.'');
    

    $data['title']='Demande interne';
    $this->load->view('Requisition_Externe_Traitement_View',$data);
   }
   else{
    
    $next_etape = $this->Model->getRequeteOne('SELECT * FROM `req_externe_action` WHERE `ID_REQ_EXTERNE_ACTION` = '.$ID_REQ_EXTERNE_ACTION.'');
    
    $affectation = $this->Model->getRequeteOne('SELECT * FROM `rh_employe_affectation` WHERE `ID_EMPLOYE` = '.$ID_EMPLOYE.' AND `STATUS_AFFECTATION` = 1');
    $detail_histo = $this->Model->getRequete('SELECT * FROM `req_externe_document_detail` WHERE `ID_REQ_EXTERNE` = '.$ID_REQ_EXTERNE.'');
    


   
    
    $dt = new DateTime("now", new DateTimeZone('Africa/Bujumbura'));
    $new_dt = $dt->format('Y-m-d H:i:s');
        // echo $new_dt;
    

     $data_historique = array(
      'ID_REQ_EXTERNE' =>$ID_REQ_EXTERNE,
      'DATE_TRAITEMENT' =>$new_dt,
      'ID_DIRECTION' =>$affectation['ID_DIRECTION'],
      'ID_SERVICE' =>$affectation['ID_SERVICE'],
      'ID_SECTION' =>$affectation['ID_SECTION'],
      'ID_EMPLOYE_TRAITANT' =>$ID_EMPLOYE,
      'ID_REQ_ETAPE_DOC_INTERNE' =>$ID_REQ_ETAPE_DOC_INTERNE,
      'MONTANT_TOTAL' => $MONTANT_TOTAL,
      'COMMENTAIRE' => $COMMENTAIRE,
      'ID_REQ_EXTERNE_ACTION' => $ID_REQ_EXTERNE_ACTION,
      'ID_EMPLOYE_DESINGE'=>$ID_EMPLOYE_DESINGE,
     );

     
     // print_r($data_historique);
     // exit();
     $ID_REQ_EXTERNE_HISTORIQUE = $this->Model->insert_last_id('req_externe_document_historique',$data_historique);


      foreach ($detail_histo as $key ) {
      
      $data_det_historique = array(
      'ID_REQ_EXTERNE' =>$ID_REQ_EXTERNE,
      'ARTICLE_ELEMENT' =>$key['ARTICLE_ELEMENT'],
      'QUANTITE_DEMANDE' =>$key['QUANTITE_DEMANDE'],
      'MONTANT_UNITAIRE_DEMANDE' =>$key['MONTANT_UNITAIRE_DEMANDE'],
      'MONTANT_TOTAL' =>$key['MONTANT_TOTAL'],
      'ID_REQ_EXTERNE_HISTORIQUE' =>$ID_REQ_EXTERNE_HISTORIQUE,
     );

      $this->Model->insert_last_id('req_externe_document_detail_historique',$data_det_historique);

    }
     

     $data_update = array('ID_REQ_ETAPE_DOC_INTERNE'=>$next_etape['NEXT'],
                          'MONTANT_TOTAL'=>$MONTANT_TOTAL,
                          'DATE_DERNIER_REPOSE'=>$new_dt,
                          'ID_EMPLOYE_DESINGE'=>$ID_EMPLOYE_DESINGE
                        );
     // print_r($data_update);
     $this->Model->update('req_externe_document',array('ID_REQ_EXTERNE'=>$ID_REQ_EXTERNE),$data_update);

     if ($next_etape['IS_LAST'] == 2) {
      echo 'Fin du process et mise en stock';
      // req_externe_stock
      $details=$this->Model->getRequete('SELECT * FROM req_externe_document_detail WHERE 1 AND ID_REQ_EXTERNE = '.$ID_REQ_EXTERNE.'');
      foreach ($details as $value) {

        $datastock = array(          
          'ID_REQ_EXTERNE'=>$ID_REQ_EXTERNE,
          'ARTICLE_ELEMENT'=>$value['ARTICLE_ELEMENT'],     
          'QUANTITE_ENTREE_STOCK'=>$value['QUANTITE_DEMANDE'],
          'QUANTITE_ACTUEL'=>$value['QUANTITE_DEMANDE'],
          'QUANTITE_SORTIE'=>0,         
          'DISPONIBLE'=>1,         
          );
          $this->Model->insert_last_id('req_externe_stock',$datastock);

          

      }
        // echo'<pre>';
      $demande_selec = $this->Model->getRequeteOne('SELECT * FROM `req_externe_document` WHERE `ID_REQ_EXTERNE` = '.$ID_REQ_EXTERNE.'');
      // print_r($demande_selec);
      $demande_budget = $this->Model->getRequeteOne('SELECT * FROM `req_externe_budge_plannifie` WHERE `ID_BUDGE_PLANNIFIE` = '.$demande_selec['ID_BUDGE_PLANNIFIE'].'');
      // print_r($demande_budget);
      
      $BUDGE_CONSOME=$demande_budget['BUDGE_CONSOME'] + $demande_selec['MONTANT_TOTAL'];
      // echo'<br>';
      $BUDGE_DISPONIBLE=$demande_budget['BUDGE_DISPONIBLE'] - $demande_selec['MONTANT_TOTAL'];

      $this->Model->update('req_externe_budge_plannifie',array('ID_BUDGE_PLANNIFIE'=>$demande_selec['ID_BUDGE_PLANNIFIE']),array('BUDGE_CONSOME'=>$BUDGE_CONSOME,'BUDGE_DISPONIBLE'=>$BUDGE_DISPONIBLE));
     }
     else{
      echo 'Le process continue';
     }

   
    $message = "<div class='alert alert-success' id='message'>
                            Traitement de la Requisition de cong&eacute; enregistr&eacute; avec succés
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
      redirect(base_url('requisition_externe/Traite_Requisition_Externe'));  
   }

    }




}