<?php
class Traitement_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()
    {

      $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']='Traitement du courrier';
      $this->load->view('Traitement_Courier_List_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 add()
    {
      $ID_COURIER=$this->input->post('ID_COURIER');
      $ID_RH_TRAITANT=$this->input->post('ID_RH_TRAITANT');
      $ID_STATUS_COURIER=$this->input->post('ID_STATUS_COURIER');
      $COMMENTAIRE=$this->input->post('COMMENTAIRE');
      $ATTACHEMENT=$this->input->post('ATTACHEMENT');
      $DESTINATAIRE_PRINCIPAL=$this->input->post('DESTINATAIRE_PRINCIPAL');
      $DEADLINE=$this->input->post('DEADLINE');

      // echo $DEADLINE;
      // echo"<br>";
      // $date_str = "10/09/2024 10:00";
      $deaddate = DateTime::createFromFormat('d/m/Y H:i', $DEADLINE);
      if($deaddate){
          $new_date_str = $deaddate->format('Y-m-d H:i');
          // echo $new_date_str;
      } 
      // else {
      //     echo "Format de date invalide.";
      // }
      // exit();
      


      $this->form_validation->set_rules('ID_STATUS_COURIER', 'Action a faire', 'required');
     
  

   if ($this->form_validation->run() == FALSE){
    $message = "<div class='alert alert-danger'>
                              Traitement du courier non enregistr&eacute;
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                      </div>";
    $this->session->set_flashdata(array('message'=>$message));
    
              
    $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_COURIER.'');
    $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_COURIER.'');
    $data['status_courier']=$this->Model->getRequete('SELECT * FROM `cour_statut_courier` order by DESCRIPTION');
    $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
    $data['title']='Traitement de courrier';
    $this->load->view('Traitement_Courier_Details_View',$data); 
   }
   else{
    
    
    $ATTACHEMENT=$this->upload_file('ATTACHEMENT','scandocument');
    
    $data_courier=array(
      'ID_COURIER'=>$ID_COURIER,
      'ID_RH_TRAITANT'=>$this->session->userdata('SOCAR_ID_USER'),
      'ID_STATUS_COURIER'=>$ID_STATUS_COURIER,
      'COMMENTAIRE'=>$COMMENTAIRE,
      'ATTACHEMENT'=>$ATTACHEMENT,
      'PERSONNEL_DESIGNE'=>$DESTINATAIRE_PRINCIPAL,
      'DEADLINE'=>$new_date_str
    );

    $ID_COURIER_HISTORIQUE = $this->Model->insert_last_id('cour_courier_historique',$data_courier); 

    
    if ($DESTINATAIRE_PRINCIPAL== NULL) {
      $USER_AFFECTE_ACTUEL = $this->session->userdata('SOCAR_ID_USER');
    }
    else{
      $USER_AFFECTE_ACTUEL = $DESTINATAIRE_PRINCIPAL;
    }

    $this->Model->update('cour_courier',array('ID_COURIER'=>$ID_COURIER), array('ID_STATUS_COURIER'=>$ID_STATUS_COURIER,'USER_AFFECTE_ACTUEL'=>$USER_AFFECTE_ACTUEL)
    );

    $this->Model->update('cour_courier_en_copie_traitement',array('ID_COURIER'=>$ID_COURIER,'ID_COURIER_HISTORIQUE'=>$ID_COURIER), array('ID_COURIER_HISTORIQUE'=>$ID_COURIER_HISTORIQUE)
    );

    $message = "<div class='alert alert-success' id='message'>
                            Traitement 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/Traitement_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 traitement($id)
    {
              
      $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['status_courier']=$this->Model->getRequete('SELECT * FROM `cour_statut_courier` order by DESCRIPTION');
      $data['direction']=$this->Model->getRequete('SELECT * FROM `organigramme_direction` order by DESCRIPTION');
      $data['historique']=$this->Model->getRequete('SELECT ID_COURIER_HISTORIQUE, CONCAT(config_user.NOM, " ", config_user.PRENOM) AS ID_RH_TRAITANT, cour_statut_courier.DESCRIPTION AS ID_STATUS_COURIER, COMMENTAIRE, DATE_TRAITEMENT, ATTACHEMENT, CONCAT(rh_employe.NOM, " ", rh_employe.PRENOM) AS PERSONNEL_DESIGNE, DEADLINE FROM `cour_courier_historique` JOIN config_user ON config_user.ID_USER = cour_courier_historique.ID_RH_TRAITANT JOIN cour_statut_courier ON cour_statut_courier.ID_STATUS_COURIER = cour_courier_historique.ID_STATUS_COURIER LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE = cour_courier_historique.PERSONNEL_DESIGNE WHERE ID_COURIER = '.$id.' ORDER BY `cour_courier_historique`.`DATE_TRAITEMENT` DESC');

      

      $data['title']='Traitement de courrier';
      $this->load->view('Traitement_Courier_Details_View',$data);          

    }

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

       $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 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;
  }




}