<?php
class Requisition  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 interne';

      $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['departement']=$this->Model->getRequete('SELECT * FROM `organigramme_section` order by DESCRIPTION');
      $data['type_doc']=$this->Model->getRequete('SELECT * FROM `doc_interne_type` WHERE ID_TYPE_DOC_INTERNE IN (SELECT doc_interne_etape.ID_TYPE_DOC_INTERNE FROM doc_interne_etape WHERE doc_interne_etape.NUMERO = 1)  order by DESCRIPTION');
      $data['rh']=$this->Model->getRequete('SELECT * FROM `rh_employe` order by NOM');
      
      $this->load->view('Requisition_Add_View',$data);

    }


    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_DOC_INTERNE'=>$this->input->post('NUM_GENERATED'),
        'MONTANT_TOTAL'=>$this->input->post('QUANTITE_DEMANDE') * $this->input->post('MONTANT_UNITAIRE_DEMANDE'),
       
        );
     
       $this->Model->insert_last_id('doc_interne_document_detail',$data);
        
        $list = $this->Model->getRequete('SELECT ID_DOC_INTERNE,ID_DOC_INTERNE_DETAIL, ARTICLE_ELEMENT, QUANTITE_DEMANDE, MONTANT_UNITAIRE_DEMANDE, MONTANT_TOTAL FROM `doc_interne_document_detail` WHERE ID_DOC_INTERNE LIKE "'.$this->input->post('NUM_GENERATED').'" ');

       $somme = $this->Model->getRequeteOne('SELECT SUM(MONTANT_TOTAL) AS MONTANT_TOTAL FROM `doc_interne_document_detail` WHERE ID_DOC_INTERNE 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_DOC_INTERNE_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('doc_interne_document_detail',array('ID_DOC_INTERNE_DETAIL'=>$this->input->post('ID_DOC_INTERNE_DETAIL')));
        
        $list = $this->Model->getRequete('SELECT ID_DOC_INTERNE,ID_DOC_INTERNE_DETAIL, ARTICLE_ELEMENT, QUANTITE_DEMANDE, MONTANT_UNITAIRE_DEMANDE, MONTANT_TOTAL FROM `doc_interne_document_detail` WHERE ID_DOC_INTERNE LIKE "'.$this->input->post('NUM_GENERATED').'" ');

       $somme = $this->Model->getRequeteOne('SELECT SUM(MONTANT_TOTAL) AS MONTANT_TOTAL FROM `doc_interne_document_detail` WHERE ID_DOC_INTERNE 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_DOC_INTERNE_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_TYPE_DOC_INTERNE=$this->input->post('ID_TYPE_DOC_INTERNE');
      $FICHIERS=$this->input->post('FICHIERS');
      $MOTIVATION=$this->input->post('MOTIVATION');





      $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 interne';
       
      $data['NUM_GENERATED']=$NUM_GENERATED;
      $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['departement']=$this->Model->getRequete('SELECT * FROM `organigramme_section` order by DESCRIPTION');
      $data['type_doc']=$this->Model->getRequete('SELECT * FROM `doc_interne_type` WHERE ID_TYPE_DOC_INTERNE IN (SELECT doc_interne_etape.ID_TYPE_DOC_INTERNE FROM doc_interne_etape WHERE doc_interne_etape.NUMERO = 1)  order by DESCRIPTION');
      $data['rh']=$this->Model->getRequete('SELECT * FROM `rh_employe` order by NOM');
      
      $this->load->view('Requisition_Add_View',$data);

      
    // $this->load->view('Demande_Conge_Add_View',$data);
   }
   else{



    $ID_ETAPE_DOC_INTERNE = $this->Model->getRequeteOne('SELECT ID_ETAPE_DOC_INTERNE FROM doc_interne_etape WHERE NUMERO =1 AND ID_TYPE_DOC_INTERNE='.$ID_TYPE_DOC_INTERNE.'');

    $data_conge=array(
      'ID_EMPLOYE_DEMANDEUR'=>$ID_EMPLOYE,
      'ID_ETAPE_DOC_INTERNE'=>$ID_ETAPE_DOC_INTERNE['ID_ETAPE_DOC_INTERNE'],
      'ID_DIRECTION'=>$ID_DIRECTION,
      'ID_SERVICE'=>$ID_SERVICE,
      'ID_SECTION'=>$ID_SECTION,
      'ID_TYPE_DOC_INTERNE'=>$ID_TYPE_DOC_INTERNE,
      'MOTIVATION'=>$MOTIVATION,
    );
    // echo'<pre>';
    // print_r($data_conge);
    // echo "</pre>";

    // exit();



    $ID_DOC_INTERNE = $this->Model->insert_last_id('doc_interne_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('doc_interne_document_fichier',array('ID_DOC_INTERNE'=>$ID_DOC_INTERNE,'FICHIERS'=>$uploadData[$i]['file_name']));
                        print_r(array('ID_DOC_INTERNE'=>$ID_DOC_INTERNE,'FICHIERS'=>$fileData['file_name']));
                        // exit();
                    }else{  
                        echo $errorUploadType .= $_FILES['file']['name'].' |<br> ';  
                    } 
                } 


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

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

    $this->Model->update('doc_interne_document_detail',array('ID_DOC_INTERNE'=>$NUM_GENERATED),array('ID_DOC_INTERNE'=>$ID_DOC_INTERNE));
    $this->Model->update('doc_interne_document',array('ID_DOC_INTERNE'=>$ID_DOC_INTERNE),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('document_interne/Requisition/listing'));  
   }

    }


      public function listing()
    {
            
       $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');

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

    }




}