<?php
// CLASSE QUI GERE LES PDF POUR DOCUMENTS EXTERNES
// AUTEUR :  ALEXIS BADIA
// TEL : 79839653
// EMAIL : barekensabealexiss@gmail.com
// DATE : 01/02/2023
ini_set('max_execution_time', 0);
ini_set('memory_limit','54048M'); 

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Pdf_Avance_Pension extends CI_Controller {

    function __construct() {
        parent::__construct();
    }
    
  //FONCTION QUI GENERE L'ORDRE DE REGLEMENT
    public function index($id){  

      include 'pdfinclude/fpdf/mc_table.php';
      include 'pdfinclude/fpdf/pdf_config.php';

      $infos=$this->Model->getRequeteOne('SELECT doc_demande_historique_fichier.FILE_PATH,doc_demande_historique.USER_ID,doc_demande_historique.DEMANDE_HISTO_ID FROM `doc_demande` LEFT JOIN doc_demande_historique ON doc_demande_historique.DEMANDE_ID=doc_demande.DEMANDE_ID LEFT JOIN doc_demande_historique_fichier ON doc_demande_historique_fichier.DEMANDE_HISTO_ID=doc_demande_historique.DEMANDE_HISTO_ID WHERE doc_demande_historique_fichier.FICHIER_ID=2 AND doc_demande.DEMANDE_ID='.$id);

      //print_r($infos);

      $SIGNATUREDG=$this->Model->getRequeteOne('SELECT doc_demande_historique.FILE_PATH,doc_demande_historique.USER_ID,SIGNATURE FROM `doc_demande` LEFT JOIN doc_demande_historique ON doc_demande_historique.DEMANDE_ID=doc_demande.DEMANDE_ID LEFT JOIN config_user ON config_user.ID_USER=doc_demande_historique.USER_ID LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE=config_user.ID_EMPLOYE LEFT JOIN doc_statut ON doc_statut.STATUT_DOC_ID=doc_demande_historique.STATUT_DOC_ID WHERE doc_statut.HAVE_SIGNATURE=1 AND doc_statut.PROFIL_ID=3 AND doc_demande_historique.ACTION_DOC_ID=1 AND doc_demande.DEMANDE_ID='.$id.' ORDER BY doc_demande_historique.DEMANDE_HISTO_ID DESC');

      $SIGNATUREDT=$this->Model->getRequeteOne('SELECT doc_demande_historique.FILE_PATH,doc_demande_historique.USER_ID,SIGNATURE FROM `doc_demande` LEFT JOIN doc_demande_historique ON doc_demande_historique.DEMANDE_ID=doc_demande.DEMANDE_ID LEFT JOIN config_user ON config_user.ID_USER=doc_demande_historique.USER_ID LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE=config_user.ID_EMPLOYE LEFT JOIN doc_statut ON doc_statut.STATUT_DOC_ID=doc_demande_historique.STATUT_DOC_ID WHERE doc_statut.HAVE_SIGNATURE=1 AND doc_statut.PROFIL_ID=2 AND doc_demande_historique.ACTION_DOC_ID=1 AND doc_demande.DEMANDE_ID='.$id.' ORDER BY doc_demande_historique.DEMANDE_HISTO_ID DESC');

      $SIGNATURESERVSOU=$this->Model->getRequeteOne('SELECT doc_demande_historique.FILE_PATH,doc_demande_historique.USER_ID,SIGNATURE FROM `doc_demande` LEFT JOIN doc_demande_historique ON doc_demande_historique.DEMANDE_ID=doc_demande.DEMANDE_ID LEFT JOIN config_user ON config_user.ID_USER=doc_demande_historique.USER_ID LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE=config_user.ID_EMPLOYE LEFT JOIN doc_statut ON doc_statut.STATUT_DOC_ID=doc_demande_historique.STATUT_DOC_ID WHERE doc_statut.HAVE_SIGNATURE=1 AND doc_statut.PROFIL_ID=12 AND doc_demande_historique.ACTION_DOC_ID=1 AND doc_demande.DEMANDE_ID='.$id.' ORDER BY doc_demande_historique.DEMANDE_HISTO_ID DESC');

      $pdf = new PDF_CONFIG('P','mm','A4');
      $pdf->addPage();

      // BACKGROUND
      if ($infos['FILE_PATH'])
       {
       $pdf->Image(FCPATH . 'uploads/document_rapport/'.$infos['FILE_PATH'],0,0,220,290);

      }
      
      //SIGNATURE SERVICE SOUSCRIPTION
      if ($SIGNATURESERVSOU['SIGNATURE'])
       {
        if(file_exists(FCPATH . 'uploads/signatures/'.$SIGNATURESERVSOU['SIGNATURE'])){
        $pdf->Image(FCPATH . 'uploads/signatures/'.$SIGNATURESERVSOU['SIGNATURE'],25,190,25,0);
      }
      }


    //SIGNATURE DT
      if ($SIGNATUREDT['SIGNATURE']) {
           if(file_exists(FCPATH . 'uploads/signatures/'.$SIGNATUREDT['SIGNATURE'])){
        $pdf->Image(FCPATH . 'uploads/signatures/'.$SIGNATUREDT['SIGNATURE'],100,190,25,0);
      }
      }


      //SIGNATURE DG
      if ($SIGNATUREDG['SIGNATURE']) {
            if(file_exists(FCPATH . 'uploads/signatures/'.$SIGNATUREDG['SIGNATURE'])){
        $pdf->Image(FCPATH . 'uploads/signatures/'.$SIGNATUREDG['SIGNATURE'],150,190,25,0);
      }
      }
      $pdf->Output('pdf_avance_'.$id.'.pdf','I');

    }

    //FONCTION QUI GENERE LE PDF POUR L'ACTE D'ANGAGEMENT
    public function acte($id){
      
      include 'pdfinclude/fpdf/mc_table.php';
      include 'pdfinclude/fpdf/pdf_config.php';

      $infos=$this->Model->getRequeteOne('SELECT PATH_DOC,IS_NEW FROM `doc_demande` LEFT JOIN doc_demande_historique ON doc_demande_historique.DEMANDE_ID=doc_demande.DEMANDE_ID WHERE doc_demande.DEMANDE_ID='.$id);

      $SIGNATUREDG=$this->Model->getRequeteOne('SELECT doc_demande_historique.FILE_PATH,doc_demande_historique.USER_ID,SIGNATURE,config_user.PROFIL_ID FROM `doc_demande` LEFT JOIN doc_demande_historique ON doc_demande_historique.DEMANDE_ID=doc_demande.DEMANDE_ID LEFT JOIN config_user ON config_user.ID_USER=doc_demande_historique.USER_ID LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE=config_user.ID_EMPLOYE LEFT JOIN doc_statut ON doc_statut.STATUT_DOC_ID=doc_demande_historique.STATUT_DOC_ID WHERE doc_statut.HAVE_SIGNATURE=1 AND doc_statut.PROFIL_ID=3 AND doc_demande_historique.ACTION_DOC_ID=1 AND doc_demande.DEMANDE_ID='.$id.' ORDER BY doc_demande_historique.DEMANDE_HISTO_ID DESC');

      $SIGNATUREDT=$this->Model->getRequeteOne('SELECT doc_demande_historique.FILE_PATH,doc_demande_historique.USER_ID,SIGNATURE,config_user.PROFIL_ID  FROM `doc_demande` LEFT JOIN doc_demande_historique ON doc_demande_historique.DEMANDE_ID=doc_demande.DEMANDE_ID LEFT JOIN config_user ON config_user.ID_USER=doc_demande_historique.USER_ID LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE=config_user.ID_EMPLOYE LEFT JOIN doc_statut ON doc_statut.STATUT_DOC_ID=doc_demande_historique.STATUT_DOC_ID WHERE doc_statut.HAVE_SIGNATURE=1 AND doc_statut.PROFIL_ID=2 AND doc_demande_historique.ACTION_DOC_ID=1 AND doc_demande.DEMANDE_ID='.$id.' ORDER BY doc_demande_historique.DEMANDE_HISTO_ID DESC');

      $SIGNATURESERVSOU=$this->Model->getRequeteOne('SELECT doc_demande_historique.FILE_PATH,doc_demande_historique.USER_ID,SIGNATURE,config_user.PROFIL_ID  FROM `doc_demande` LEFT JOIN doc_demande_historique ON doc_demande_historique.DEMANDE_ID=doc_demande.DEMANDE_ID LEFT JOIN config_user ON config_user.ID_USER=doc_demande_historique.USER_ID LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE=config_user.ID_EMPLOYE LEFT JOIN doc_statut ON doc_statut.STATUT_DOC_ID=doc_demande_historique.STATUT_DOC_ID WHERE doc_statut.HAVE_SIGNATURE=1 AND doc_statut.PROFIL_ID=12 AND doc_demande_historique.ACTION_DOC_ID=1 AND doc_demande.DEMANDE_ID='.$id.' ORDER BY doc_demande_historique.DEMANDE_HISTO_ID DESC');

      // $logo_banque='dist/logo_pdf.png';
     
      $pdf = new PDF_CONFIG('P','mm','A4');
      $pdf->addPage();

      // BACKGROUND
      if($infos['IS_NEW']==1)
      {
      $pdf->Image(FCPATH . 'uploads/document_rapport/'.$infos['PATH_DOC'],0,0,220,290);
     }else{
      $pdf->Image(FCPATH . 'uploads/document_demandes/'.$infos['PATH_DOC'],0,0,220,290);
     }
      
      //SIGNATURE SERVICE SOUSCRIPTION
      if ($SIGNATURESERVSOU['SIGNATURE'] && $SIGNATURESERVSOU['PROFIL_ID']==12) 
      {
      if(file_exists(FCPATH . 'uploads/signatures/'.$SIGNATURESERVSOU['SIGNATURE'])){
        $pdf->Image(FCPATH . 'uploads/signatures/'.$SIGNATURESERVSOU['SIGNATURE'],25,130,25,0);
      }
      }
      if ($SIGNATURESERVSOU['SIGNATURE'] && $SIGNATURESERVSOU['PROFIL_ID']!=12)
      {
      if(file_exists(FCPATH . 'uploads/signatures/'.$SIGNATURESERVSOU['SIGNATURE'])){
        // $pdf->Cell(23,130,utf8_decode('P.O'),25,0,'L');
        $pdf->Image(FCPATH . 'uploads/signatures/'.$SIGNATURESERVSOU['SIGNATURE'],25,130,25,0);
      }
      }
 

     //SIGNATURE DT
      if ($SIGNATUREDT['SIGNATURE'] && $SIGNATUREDT['PROFIL_ID']==2)
       {
      if(file_exists(FCPATH . 'uploads/signatures/'.$SIGNATUREDT['SIGNATURE'])){
        $pdf->Image(FCPATH . 'uploads/signatures/'.$SIGNATUREDT['SIGNATURE'],100,125,25,0);
      }
      }
      if ($SIGNATUREDT['SIGNATURE'] && $SIGNATUREDT['PROFIL_ID']!=2)
      {
        if(file_exists(FCPATH . 'uploads/signatures/'.$SIGNATUREDT['SIGNATURE'])){
          $pdf->Cell(180,230,utf8_decode('P.O'),25,0,'C');
          $pdf->Image(FCPATH . 'uploads/signatures/'.$SIGNATUREDT['SIGNATURE'],100,125,25,0);
        }  
      }


      //SIGNATURE DG
      if ($SIGNATUREDG['SIGNATURE'] && $SIGNATUREDG['PROFIL_ID']==3){
        if(file_exists(FCPATH . 'uploads/signatures/'.$SIGNATUREDG['SIGNATURE'])){
        $pdf->Image(FCPATH . 'uploads/signatures/'.$SIGNATUREDG['SIGNATURE'],165,125,25,0);
      }
      }
      if ($SIGNATUREDG['SIGNATURE'] && $SIGNATUREDG['PROFIL_ID']!=3)
      {
        if(file_exists(FCPATH . 'uploads/signatures/'.$SIGNATUREDG['SIGNATURE'])){
          // $pdf->Cell(5,230,utf8_decode('P.OA'),25,0,'B');
          $pdf->Image(FCPATH . 'uploads/signatures/'.$SIGNATUREDG['SIGNATURE'],165,125,25,0);
        }  
      }

      $pdf->Output('pdf_acte_'.$id.'.pdf','I');

    }




    //FONCTION QUI GENERE LA QUITANCE DE REGLEMENT 
    public function quitance($id){
      include 'pdfinclude/fpdf/mc_table.php';
      include 'pdfinclude/fpdf/pdf_config.php';

      $infos=$this->Model->getRequeteOne('SELECT doc_demande_historique_fichier.FILE_PATH,doc_demande_historique.USER_ID FROM `doc_demande` LEFT JOIN doc_demande_historique ON doc_demande_historique.DEMANDE_ID=doc_demande.DEMANDE_ID LEFT JOIN doc_demande_historique_fichier ON doc_demande_historique_fichier.DEMANDE_HISTO_ID=doc_demande_historique.DEMANDE_HISTO_ID WHERE doc_demande_historique_fichier.FICHIER_ID=4 AND doc_demande.DEMANDE_ID='.$id);

      $SIGNATUREDG=$this->Model->getRequeteOne('SELECT doc_demande_historique.FILE_PATH,doc_demande_historique.USER_ID,SIGNATURE FROM `doc_demande` LEFT JOIN doc_demande_historique ON doc_demande_historique.DEMANDE_ID=doc_demande.DEMANDE_ID LEFT JOIN config_user ON config_user.ID_USER=doc_demande_historique.USER_ID LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE=config_user.ID_EMPLOYE LEFT JOIN doc_statut ON doc_statut.STATUT_DOC_ID=doc_demande_historique.STATUT_DOC_ID WHERE doc_statut.HAVE_SIGNATURE=1 AND doc_statut.PROFIL_ID=3 AND doc_demande_historique.ACTION_DOC_ID=1 AND doc_demande.DEMANDE_ID='.$id.' ORDER BY doc_demande_historique.DEMANDE_HISTO_ID DESC');

      $SIGNATURESERV_COMP=$this->Model->getRequeteOne('SELECT doc_demande_historique.FILE_PATH,doc_demande_historique.USER_ID,SIGNATURE FROM `doc_demande` LEFT JOIN doc_demande_historique ON doc_demande_historique.DEMANDE_ID=doc_demande.DEMANDE_ID LEFT JOIN config_user ON config_user.ID_USER=doc_demande_historique.USER_ID LEFT JOIN rh_employe ON rh_employe.ID_EMPLOYE=config_user.ID_EMPLOYE LEFT JOIN doc_statut ON doc_statut.STATUT_DOC_ID=doc_demande_historique.STATUT_DOC_ID WHERE doc_statut.HAVE_SIGNATURE=1 AND doc_statut.PROFIL_ID=4 AND doc_demande_historique.ACTION_DOC_ID=1 AND doc_demande.DEMANDE_ID='.$id.' ORDER BY doc_demande_historique.DEMANDE_HISTO_ID DESC');

      $pdf = new PDF_CONFIG('P','mm','A4');
      $pdf->addPage();

      // BACKGROUND
      $pdf->Image(FCPATH . 'uploads/document_rapport/'.$infos['FILE_PATH'],0,0,220,290);


      //SIGNATURE DG
      if ($SIGNATUREDG['SIGNATURE']) 
      {
      if(file_exists(FCPATH . 'uploads/signatures/'.$SIGNATUREDG['SIGNATURE'])){
        $pdf->Image(FCPATH . 'uploads/signatures/'.$SIGNATUREDG['SIGNATURE'],170,220,25,0);
      }
      }

      //SIGNATURE DAF
      if ($SIGNATURESERV_COMP['SIGNATURE']) 
      {
      if(file_exists(FCPATH . 'uploads/signatures/'.$SIGNATURESERV_COMP['SIGNATURE'])){
        $pdf->Image(FCPATH . 'uploads/signatures/'.$SIGNATURESERV_COMP['SIGNATURE'],140,220,25,0);
      }
      }

      $pdf->Output('pdf_quitance_'.$id.'.pdf','I');

    }


    

    
}
