<?php
class Enregistrement_Courier  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']='Réception de courrier';

      $data['NUM_GENERATED']=$this->notifications->generate_UIID(10);
      $data['type_courier']=$this->Model->getRequete('SELECT * FROM `cour_type_courier` order by DESCRIPTION');
      $data['nature_courier']=$this->Model->getRequete('SELECT * FROM `cour_nature_couriel` order by DESCRIPTION');
      $data['corresponndant']=$this->Model->getRequete('SELECT * FROM `cour_courier_correspondant` order by NOM_CORRESPONDANT');
      $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
      $data['rh']=$this->Model->getRequete('SELECT * FROM `rh_employe` order by NOM');      
      $this->load->view('Enregistrement_Courier_Add_View',$data);

    }

    public function get_departement()
    {
    $departement= $this->Model->getList("organigramme_service",array('ID_DIRECTION'=>$this->input->post('ID_DIRECTION')));
    $datas= '<option value="">-- Sélectionner --</option>';
    foreach($departement as $sele_departement){
    $datas.= '<option value="'.$sele_departement["ID_SERVICE"].'">'.$sele_departement["DESCRIPTION"].'</option>';
    }
    $datas.= '';
    echo $datas;
    }

    
    public function get_destinataire()
    {
    $rh= $this->Model->getRequete("SELECT rh_employe.ID_EMPLOYE, rh_employe.NOM, rh_employe.PRENOM FROM `rh_employe_affectation` JOIN rh_employe ON rh_employe.ID_EMPLOYE = rh_employe_affectation.ID_EMPLOYE WHERE ID_DIRECTION = ".$this->input->post('ID_DIRECTION')." AND STATUS_AFFECTATION = 1");
    $datas= '<option value="">-- Sélectionner --</option>';
    foreach($rh as $rhs){
    $datas.= '<option value="'.$rhs["ID_EMPLOYE"].'">'.$rhs["NOM"].' '.$rhs["PRENOM"].'</option>';
    }
    $datas.= '';
    echo $datas;
    }

    public function get_destinataire_ser()
    {
    $rh= $this->Model->getRequete("SELECT rh_employe.ID_EMPLOYE, rh_employe.NOM, rh_employe.PRENOM FROM `rh_employe_affectation` JOIN rh_employe ON rh_employe.ID_EMPLOYE = rh_employe_affectation.ID_EMPLOYE WHERE ID_SERVICE = ".$this->input->post('ID_SERVICE')." AND STATUS_AFFECTATION = 1");
    $datas= '<option value="">-- Sélectionner --</option>';
    foreach($rh as $rhs){
    $datas.= '<option value="'.$rhs["ID_EMPLOYE"].'">'.$rhs["NOM"].' '.$rhs["PRENOM"].'</option>';
    }
    $datas.= '';
    echo $datas;
    }


    public function save_element()
  {
     
     $data = array(          
        'ID_RH_COPIE'=>$this->input->post('ID_RH_COPIE'),
        'ID_COURIER'=>0,
        'ID_TYPE_COPIE'=>1,
        'NUM_GENERATED'=>$this->input->post('NUM_GENERATED'),       
        );
     
       $this->Model->insert_last_id('cour_courier_en_copie',$data);
        
        $list = $this->Model->getRequete('SELECT cour_courier_en_copie.ID_COURIER_COPIE, cour_courier_en_copie.NUM_GENERATED, rh_employe.NOM, rh_employe.PRENOM FROM `cour_courier_en_copie` JOIN rh_employe ON rh_employe.ID_EMPLOYE = cour_courier_en_copie.ID_RH_COPIE WHERE NUM_GENERATED LIKE "'.$this->input->post('NUM_GENERATED').'" ');

       $results = "<table class='table'><tr><td><b>Nom & Pr&eacute;nom</b></td><td></td></tr>";
       foreach ($list as $key) {
         $results .= "<tr><td>".$key['NOM']." ".$key['PRENOM']."</td>
         <td>
         <a class='btn btn-danger btn-xs' role='button' onclick='removeElement(".$key['ID_COURIER_COPIE'].")'>Enlever</a>
         </td>
         </tr>";
       }
       $results .= "</table>";

        echo $results;
      
  }

  public function removeElement()
  {
     
     
     
       $this->Model->delete('cour_courier_en_copie',array('ID_COURIER_COPIE'=>$this->input->post('ID_COURIER_COPIE')));
        
       $list = $this->Model->getRequete('SELECT cour_courier_en_copie.ID_COURIER_COPIE, cour_courier_en_copie.NUM_GENERATED, rh_employe.NOM, rh_employe.PRENOM FROM `cour_courier_en_copie` JOIN rh_employe ON rh_employe.ID_EMPLOYE = cour_courier_en_copie.ID_RH_COPIE WHERE NUM_GENERATED LIKE "'.$this->input->post('NUM_GENERATED').'" ');

       $results = "<table class='table'><tr><td><b>Nom & Pr&eacute;nom</b></td><td></td></tr>";
       foreach ($list as $key) {
         $results .= "<tr><td>".$key['NOM']." ".$key['PRENOM']."</td>
         <td>
         <a class='btn btn-danger btn-xs' role='button' onclick='removeElement(".$key['ID_COURIER_COPIE'].")'>Enlever</a>
         </td>
         </tr>";
       }
       $results .= "</table>";

        echo $results;
      
  }


      public function add()
    {
      
      $ID_TYPE_COURIER=$this->input->post('ID_TYPE_COURIER');
      $ID_NATURE_COURIER=$this->input->post('ID_NATURE_COURIER');
      $ID_CORRESPONDANT=$this->input->post('ID_CORRESPONDANT');
      $NUM_COURIER=$this->input->post('NUM_COURIER');
      $PDF_SCAN=$this->input->post('PDF_SCAN');
      $DESTINATAIRE_PRINCIPAL=$this->input->post('DESTINATAIRE_PRINCIPAL');
      $OBJET_COURIER=$this->input->post('OBJET_COURIER');
      $COMMENTAIRE=$this->input->post('COMMENTAIRE');


      $this->form_validation->set_rules('ID_TYPE_COURIER', 'Type de courier', 'required');
      $this->form_validation->set_rules('ID_NATURE_COURIER', 'Nature du courier', 'required');
     
  

   if ($this->form_validation->run() == FALSE){
    $message = "<div class='alert alert-danger'>
                              Reception du courier non enregistr&eacute;
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
    $data['title']='Réception de courrier';

    $data['NUM_GENERATED']=$this->input->post('NUM_GENERATED');
    $data['type_courier']=$this->Model->getRequete('SELECT * FROM `cour_type_courier` order by DESCRIPTION');
    $data['nature_courier']=$this->Model->getRequete('SELECT * FROM `cour_nature_couriel` order by DESCRIPTION');
    $data['corresponndant']=$this->Model->getRequete('SELECT * FROM `cour_courier_correspondant` order by NOM_CORRESPONDANT');
    $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
    $data['rh']=$this->Model->getRequete('SELECT * FROM `rh_employe` order by NOM');      
    $this->load->view('Enregistrement_Courier_Add_View',$data);
   }
   else{
    
    
    $SCAN_ORIGINAL=$this->upload_file('PDF_SCAN','scandocument');
    
    $data_courier=array(
      'NUM_COURIER'=>$NUM_COURIER,
      'OBJET_COURIER'=>$OBJET_COURIER,
      'ENTRANT_SORTANT'=>1,
      'ID_TYPE_COURIER'=>$ID_TYPE_COURIER,
      'ID_NATURE_COURIER'=>$ID_NATURE_COURIER,
      'ID_STATUS_COURIER'=>1,
      'DESTINATAIRE_PRINCIPAL'=>$DESTINATAIRE_PRINCIPAL,
      'ID_CORRESPONDANT'=>$ID_CORRESPONDANT,
      'SCAN_ORIGINAL'=>$SCAN_ORIGINAL,
      'COMMENTAIRE'=>$COMMENTAIRE,
      'USER_AFFECTE_ACTUEL'=>$DESTINATAIRE_PRINCIPAL,
    );

    $ID_COURIER = $this->Model->insert_last_id('cour_courier',$data_courier); 


    $this->Model->update('cour_courier_en_copie',array('NUM_GENERATED'=>$this->input->post('NUM_GENERATED')),array('ID_COURIER'=>$ID_COURIER));

    $message = "<div class='alert alert-success' id='message'>
                            Courier 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('courier/Enregistrement_Courier'));  
   }

    }


      public function listing()
    {
            
       $statusc = ' ';
              
      $data['resultat']=$this->Model->getRequete('SELECT ID_COURIER, NUM_COURIER, OBJET_COURIER, cour_type_courier.DESCRIPTION AS TYPE_COURIER, cour_nature_couriel.DESCRIPTION AS NATURE_COURIER, DATE_TIME_ENREGISTREMENT, CONCAT(rh_employe.NOM, " ", rh_employe.PRENOM) AS DESTINATAIRE_PRINCIPAL, cour_courier_correspondant.NOM_CORRESPONDANT FROM `cour_courier` JOIN cour_type_courier ON cour_type_courier.ID_TYPE_COURIER = cour_courier.ID_TYPE_COURIER JOIN cour_nature_couriel ON cour_nature_couriel.ID_NATURE_COURIER = cour_courier.ID_NATURE_COURIER JOIN rh_employe ON rh_employe.ID_EMPLOYE = cour_courier.DESTINATAIRE_PRINCIPAL JOIN cour_courier_correspondant ON cour_courier_correspondant.ID_CORRESPONDANT = cour_courier.ID_CORRESPONDANT WHERE cour_courier.ENTRANT_SORTANT = 1');
      $data['title']='Réception de courrier';
      $this->load->view('Enregistrement_Courier_List_View',$data);          

    }


    public function details($id)
    {
      // echo $id;
            
      //  $statusc = ' ';
              
      $data['resultat']=$this->Model->getRequeteOne('SELECT ID_COURIER, NUM_COURIER, OBJET_COURIER, cour_type_courier.DESCRIPTION AS TYPE_COURIER, cour_nature_couriel.DESCRIPTION AS NATURE_COURIER, DATE_TIME_ENREGISTREMENT, CONCAT(rh_employe.NOM, " ", rh_employe.PRENOM) AS DESTINATAIRE_PRINCIPAL, cour_courier_correspondant.NOM_CORRESPONDANT, cour_courier.SCAN_ORIGINAL, cour_courier.COMMENTAIRE, CONCAT(actu.NOM, " ", actu.PRENOM) AS AFFECTATION_ACTUEL FROM `cour_courier` JOIN cour_type_courier ON cour_type_courier.ID_TYPE_COURIER = cour_courier.ID_TYPE_COURIER JOIN cour_nature_couriel ON cour_nature_couriel.ID_NATURE_COURIER = cour_courier.ID_NATURE_COURIER JOIN rh_employe ON rh_employe.ID_EMPLOYE = cour_courier.DESTINATAIRE_PRINCIPAL JOIN cour_courier_correspondant ON cour_courier_correspondant.ID_CORRESPONDANT = cour_courier.ID_CORRESPONDANT JOIN rh_employe as actu ON actu.ID_EMPLOYE = cour_courier.USER_AFFECTE_ACTUEL WHERE cour_courier.ENTRANT_SORTANT = 1 AND cour_courier.ID_COURIER = '.$id.'');
      $data['resultatcopie']=$this->Model->getRequete('SELECT CONCAT(rh_employe.NOM, " ", rh_employe.PRENOM) AS DESTINATAIRE_COPIE FROM `cour_courier_en_copie` JOIN rh_employe ON rh_employe.ID_EMPLOYE = cour_courier_en_copie.ID_RH_COPIE WHERE `ID_COURIER` = '.$id.'');
      $data['title']='Réception de courrier';
      $this->load->view('Enregistrement_Courier_Details_View',$data);          

    }

    public function atraiter()
    {

     

            
       $statusc = ' ';
              



      $data['resultat']=$this->Model->getRequete('SELECT ID_DOC_INTERNE,MOTIVATION, ID_EMPLOYE_DESINGE, DATE_DEMANDE, rh_employe.NOM, rh_employe.PRENOM, doc_interne_etape.DESCRIPTION AS ETAPE, doc_interne_etape.ID_PROFIL, doc_interne_type.PDF_CONTROLLERS, doc_interne_etape.ON_PDF, organigramme_direction.DESCRIPTION AS ID_DIRECTION, organigramme_service.DESCRIPTION AS ID_SERVICE, organigramme_section.DESCRIPTION AS ID_SECTION, doc_interne_type.DESCRIPTION AS TYPE_DOCUMENT, MONTANT_TOTAL FROM `doc_interne_document` JOIN rh_employe ON rh_employe.ID_EMPLOYE = doc_interne_document.ID_EMPLOYE_DEMANDEUR JOIN doc_interne_etape ON doc_interne_etape.ID_ETAPE_DOC_INTERNE = doc_interne_document.ID_ETAPE_DOC_INTERNE JOIN doc_interne_type ON doc_interne_type.ID_TYPE_DOC_INTERNE = doc_interne_document.ID_TYPE_DOC_INTERNE JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = doc_interne_document.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = doc_interne_document.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = doc_interne_document.ID_SECTION WHERE 1 AND ID_PROFIL = '.$this->session->userdata('SOCAR_PROFIL_ID').' OR ID_EMPLOYE_DESINGE = '.$this->session->userdata('SOCAR_ID_USER').'');



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

    }


    public function dejatraiter()
    {

     

            
       $statusc = ' ';
              



      $data['resultat']=$this->Model->getRequete('SELECT doc_interne_document.ID_DOC_INTERNE, MOTIVATION, doc_interne_document.ID_EMPLOYE_DESINGE, DATE_DEMANDE, rh_employe.NOM, rh_employe.PRENOM,  doc_interne_etape.DESCRIPTION AS ETAPE, doc_interne_etape.ID_PROFIL, doc_interne_type.PDF_CONTROLLERS, doc_interne_etape.ON_PDF, organigramme_direction.DESCRIPTION AS ID_DIRECTION, organigramme_service.DESCRIPTION AS ID_SERVICE, organigramme_section.DESCRIPTION AS ID_SECTION, doc_interne_type.DESCRIPTION AS TYPE_DOCUMENT, doc_interne_document.MONTANT_TOTAL FROM `doc_interne_document` JOIN rh_employe ON rh_employe.ID_EMPLOYE = doc_interne_document.ID_EMPLOYE_DEMANDEUR JOIN doc_interne_etape ON doc_interne_etape.ID_ETAPE_DOC_INTERNE = doc_interne_document.ID_ETAPE_DOC_INTERNE JOIN doc_interne_type ON doc_interne_type.ID_TYPE_DOC_INTERNE = doc_interne_document.ID_TYPE_DOC_INTERNE JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = doc_interne_document.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = doc_interne_document.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = doc_interne_document.ID_SECTION JOIN doc_interne_document_historique ON doc_interne_document_historique.ID_DOC_INTERNE = doc_interne_document.ID_DOC_INTERNE WHERE 1 AND doc_interne_document_historique.ID_EMPLOYE_TRAITANT = '.$this->session->userdata('SOCAR_ID_USER').'');



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

    }

public function traitement($id)
    {

      $data['data']=$this->Model->getRequeteOne('SELECT ID_DOC_INTERNE,ID_EMPLOYE_DESINGE, MOTIVATION, DATE_DEMANDE,doc_interne_document.ID_ETAPE_DOC_INTERNE, rh_employe.NOM, rh_employe.PRENOM, doc_interne_etape.DESCRIPTION AS ETAPE, organigramme_direction.DESCRIPTION AS ID_DIRECTION, organigramme_service.DESCRIPTION AS ID_SERVICE, organigramme_section.DESCRIPTION AS ID_SECTION, doc_interne_type.DESCRIPTION AS TYPE_DOCUMENT, MONTANT_TOTAL FROM `doc_interne_document` JOIN rh_employe ON rh_employe.ID_EMPLOYE = doc_interne_document.ID_EMPLOYE_DEMANDEUR JOIN doc_interne_etape ON doc_interne_etape.ID_ETAPE_DOC_INTERNE = doc_interne_document.ID_ETAPE_DOC_INTERNE JOIN doc_interne_type ON doc_interne_type.ID_TYPE_DOC_INTERNE = doc_interne_document.ID_TYPE_DOC_INTERNE JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = doc_interne_document.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = doc_interne_document.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = doc_interne_document.ID_SECTION WHERE 1 AND doc_interne_document.ID_DOC_INTERNE = '.$id.' ');

      $data['historique']=$this->Model->getRequete('SELECT doc_interne_document_historique.ID_DOC_INTERNE, doc_interne_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, doc_interne_etape.DESCRIPTION, doc_interne_document_historique.MONTANT_TOTAL, doc_interne_document_historique.COMMENTAIRE,doc_interne_action.DESCRIPTION AS ACTIONS FROM `doc_interne_document_historique` JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = doc_interne_document_historique.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = doc_interne_document_historique.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = doc_interne_document_historique.ID_SECTION JOIN rh_employe ON rh_employe.ID_EMPLOYE = doc_interne_document_historique.ID_EMPLOYE_TRAITANT JOIN doc_interne_etape ON doc_interne_etape.ID_ETAPE_DOC_INTERNE = doc_interne_document_historique.ID_ETAPE_DOC_INTERNE JOIN doc_interne_action ON doc_interne_action.ID_ACTION_INTENE = doc_interne_document_historique.ID_ACTION_INTENE WHERE 1 AND ID_DOC_INTERNE = '.$id.'');

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





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

    }
    


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

      $ID_ETAPE_DOC_INTERNE=$this->input->post('ID_ETAPE_DOC_INTERNE');

      $ID_DOC_INTERNE=$this->input->post('ID_DOC_INTERNE');
      $ID_ACTION_INTENE=$this->input->post('ID_ACTION_INTENE');
      // echo $ID_ACTION_INTENE;
      $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_ACTION_INTENE', '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));
    
    $data['data']=$this->Model->getRequeteOne('SELECT ID_DOC_INTERNE, DATE_DEMANDE,doc_interne_document.ID_ETAPE_DOC_INTERNE, rh_employe.NOM, rh_employe.PRENOM, doc_interne_etape.DESCRIPTION AS ETAPE, organigramme_direction.DESCRIPTION AS ID_DIRECTION, organigramme_service.DESCRIPTION AS ID_SERVICE, organigramme_section.DESCRIPTION AS ID_SECTION, doc_interne_type.DESCRIPTION AS TYPE_DOCUMENT, MONTANT_TOTAL FROM `doc_interne_document` JOIN rh_employe ON rh_employe.ID_EMPLOYE = doc_interne_document.ID_EMPLOYE_DEMANDEUR JOIN doc_interne_etape ON doc_interne_etape.ID_ETAPE_DOC_INTERNE = doc_interne_document.ID_ETAPE_DOC_INTERNE JOIN doc_interne_type ON doc_interne_type.ID_TYPE_DOC_INTERNE = doc_interne_document.ID_TYPE_DOC_INTERNE JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = doc_interne_document.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = doc_interne_document.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = doc_interne_document.ID_SECTION WHERE 1 AND doc_interne_document.ID_DOC_INTERNE = '.$ID_DOC_INTERNE.' ');

    $data['historique']=$this->Model->getRequete('SELECT doc_interne_document_historique.ID_DOC_INTERNE, doc_interne_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, doc_interne_etape.DESCRIPTION, doc_interne_document_historique.MONTANT_TOTAL, doc_interne_document_historique.COMMENTAIRE,doc_interne_action.DESCRIPTION AS ACTIONS FROM `doc_interne_document_historique` JOIN organigramme_direction ON organigramme_direction.ID_DIRECTION = doc_interne_document_historique.ID_DIRECTION LEFT JOIN organigramme_service ON organigramme_service.ID_SERVICE = doc_interne_document_historique.ID_SERVICE LEFT JOIN organigramme_section ON organigramme_section.ID_SECTION = doc_interne_document_historique.ID_SECTION JOIN rh_employe ON rh_employe.ID_EMPLOYE = doc_interne_document_historique.ID_EMPLOYE_TRAITANT JOIN doc_interne_etape ON doc_interne_etape.ID_ETAPE_DOC_INTERNE = doc_interne_document_historique.ID_ETAPE_DOC_INTERNE JOIN doc_interne_action ON doc_interne_action.ID_ACTION_INTENE = doc_interne_document_historique.ID_ACTION_INTENE WHERE 1 AND ID_DOC_INTERNE = '.$ID_DOC_INTERNE.'');

    $data['details_demande'] = $this->Model->getRequete('SELECT * FROM doc_interne_document_detail WHERE 1 AND ID_DOC_INTERNE = '.$ID_DOC_INTERNE.'');

    $data['title']='Demande interne';
    $this->load->view('Requisition_Traitement_View',$data);
   }
   else{
    
    $next_etape = $this->Model->getRequeteOne('SELECT * FROM `doc_interne_action` WHERE `ID_ACTION_INTENE` = '.$ID_ACTION_INTENE.'');
    
    $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 `doc_interne_document_detail` WHERE `ID_DOC_INTERNE` = '.$ID_DOC_INTERNE.'');



    // echo "<pre>";
    // print_r($next_etape);

    // echo $ID_DOC_INTERNE;
    // exit();
   
    
    $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_DOC_INTERNE' =>$ID_DOC_INTERNE,
      '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_ETAPE_DOC_INTERNE' =>$ID_ETAPE_DOC_INTERNE,
      'MONTANT_TOTAL' => $MONTANT_TOTAL,
      'COMMENTAIRE' => $COMMENTAIRE,
      'ID_ACTION_INTENE' => $ID_ACTION_INTENE,
      'ID_EMPLOYE_DESINGE'=>$ID_EMPLOYE_DESINGE,
     );

     // ID_ETAPE_DOC_INTERNE
     // print_r($data_historique);
     // exit();
     $ID_DOC_INTERNE_HISTORIQUE = $this->Model->insert_last_id('doc_interne_document_historique',$data_historique);


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

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

    }
     

     $data_update = array('ID_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('doc_interne_document',array('ID_DOC_INTERNE'=>$ID_DOC_INTERNE),$data_update);

   
    $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('document_interne/Requisition/listing'));  
   }

    }


    public function upload_file($input_name=array(),$dossier)
  {
    $nom_file=$_FILES[$input_name]['tmp_name'];
    $nom_champ=$_FILES[$input_name]['name']; 
    $repertoire_fichier =FCPATH.'uploads/'.$dossier.'/';
    $code=uniqid();
    $date="".date('YmdHi');
    $nom_champ=str_replace(" ","",$nom_champ);
    $nomfichier=$code.'-'.$date.'_'.$nom_champ;
    $fichier=basename($nomfichier);
    if(!is_dir($repertoire_fichier)) //create the folder if it does not already exists   
    {
      mkdir($repertoire_fichier,0777,TRUE);                                                         
    }
    move_uploaded_file($nom_file, $repertoire_fichier.$fichier);
    return $nomfichier;
  }




}