<?php

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

class Requisition_Article extends CI_Controller {

    function __construct() {
        parent::__construct();
    }
    
    public function index($id){

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


      // $info_conge=$this->Model->getOne('conge_demande_conge',array('ID_DEMANDE_CONGE'=>$id));
      // $info_conge_historique=$this->Model->getList('doc_demande_historique',array('DEMANDE_ID'=>$id));
      $requisition=$this->Model->getRequeteOne('SELECT ID_DOC_INTERNE,ID_EMPLOYE_DESINGE, MOTIVATION,SIGNATURE, 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.'');

      $requisition_details = $this->Model->getRequete('SELECT * FROM doc_interne_document_detail WHERE 1 AND ID_DOC_INTERNE = '.$id.'');

      $urlpass = base_url();
      $elementspas = explode("/", $urlpass);
      $nouveau_urlpass = null ;
        $indicepass = sizeof($elementspas);
        for ($i = 0; $i < ($indicepass - 2); $i++) {
            $nouveau_urlpass .=$elementspas[$i] . '/';
        }

      $img_singature=$nouveau_urlpass.'uploads/signatures/'.$requisition['SIGNATURE'];
     

      $pdf = new PDF_CONFIG('P','mm','A4');
      $pdf->addPage();
      $pdf->Ln(30);
      $pdf->SetFont('Arial','B',15);

      // $pdf->SetX(10);
      $pdf->Cell(190,5,utf8_decode(strtoupper('Requisition Article')),0,1,'C');
      
      $pdf->SetFont('Arial','',10);
      $pdf->Ln(4);
      $pdf->SetX(10);
      $pdf->Cell(55,6,utf8_decode('Demandeur'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$requisition['NOM'].' '.$requisition['PRENOM'].''),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Service'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$requisition['ID_SERVICE'].''),0,1,'L');
      $pdf->Cell(55,6,utf8_decode('Direction'),0,0,'L');
      $pdf->Cell(100,6,utf8_decode(':'.$requisition['ID_DIRECTION'].''),0,1,'L');
      // $pdf->Cell(55,6,utf8_decode('Date demande'),0,0,'L');
      // $pdf->Cell(100,6,utf8_decode(':'),0,1,'L');
      // $pdf->Cell(55,6,utf8_decode('Période de congé'),0,0,'L');
      // $pdf->Cell(100,6,utf8_decode(':'),0,1,'L');

      // $pdf->Ln(4);
      // $pdf->Cell(55,6,utf8_decode('Type de congé'),0,0,'L');
      // $pdf->Cell(8,6,utf8_decode(' '),1,0,'L');
      // $pdf->Cell(100,6,utf8_decode('Congé annuel '),0,1,'L');
      // $pdf->Cell(55,6,utf8_decode(''),0,0,'L');
      // $pdf->Cell(8,6,utf8_decode(' '),1,0,'L');
      // $pdf->Cell(100,6,utf8_decode('Congé de circonstance'),0,1,'L');
      // $pdf->Cell(55,6,utf8_decode(''),0,0,'L');
      // $pdf->Cell(8,6,utf8_decode(' '),1,0,'L');
      // $pdf->Cell(100,6,utf8_decode('Autres : '),0,1,'L');

      $pdf->Ln(5);
      $pdf->Cell(10,6,utf8_decode('Le'),0,0,'L');
      $pdf->Cell(10,6,utf8_decode(''.$requisition['DATE_DEMANDE'].''),0,1,'L');
      $pdf->Cell(5,4,utf8_decode(''),0,0,'L');
      $pdf->Cell(50,0,utf8_decode(''),1,1,'L');


      $pdf->Ln(10);
      $pdf->Cell(45,6,utf8_decode('Signature du demandeur '),0,0,'L');
      $pdf->Image($img_singature, 60, 55, 90, 0, 'PNG');
      $pdf->Cell(45,6,utf8_decode(''),0,1,'L');
      $pdf->Cell(45,6,utf8_decode(''),0,0,'L');
      $pdf->Cell(100,0,utf8_decode(''),1,1,'L');
     

      $pdf->Ln(15);
      $pdf->Cell(190,0,utf8_decode(' '),1,1,'L');
      $pdf->Ln(5);

      // $pdf->Cell(55,8,utf8_decode('Visa du Service Administration '),0,1,'L');
      // $pdf->Cell(55,0,utf8_decode(''),1,1,'L');
      // $pdf->Cell(15,5,utf8_decode(''),0,0,'L');
      // $pdf->Cell(70,5,utf8_decode('- Nombre de jrs de congés dus : 23.5 jrs ex 2021'),0,1,'L');
      // $pdf->Cell(15,5,utf8_decode(''),0,0,'L');
      // $pdf->Cell(55,5,utf8_decode('- Nombre de jrs demandés : 1/2 Jrn '),0,1,'L');
      // $pdf->Cell(15,5,utf8_decode(''),0,0,'L');
      // $pdf->Cell(55,5,utf8_decode('- Nombre de jrs de congés restants : 23 jours ex 2021 '),0,1,'L');
      // // $pdf->Cell(40,0,utf8_decode(''),1,1,'L');


      $pdf->Ln(10);
      $pdf->Cell(210,6,utf8_decode('Detail de la Requisition'),0,1,'C');
      $pdf->Cell(55,6,utf8_decode('Nom'),0,0,'L');
      $pdf->Cell(55,6,utf8_decode('Quantite'),0,0,'L');
      $pdf->Cell(55,6,utf8_decode('Prix Unitaire'),0,0,'L');
      $pdf->Cell(55,6,utf8_decode('Prix Total'),0,1,'L');
      $mont =0; 
      foreach ($requisition_details as $key) {
          // code...
      $pdf->Cell(55,6,utf8_decode($key['ARTICLE_ELEMENT']),0,0,'L');
      $pdf->Cell(55,6,utf8_decode($key['QUANTITE_DEMANDE']),0,0,'L');
      $pdf->Cell(55,6,utf8_decode($key['MONTANT_UNITAIRE_DEMANDE']),0,0,'L');
      $pdf->Cell(55,6,utf8_decode($key['MONTANT_TOTAL']),0,1,'L');
      $mont +=$key['MONTANT_TOTAL']; 
      }

      $pdf->Cell(55,6,utf8_decode(''),0,0,'L');
      $pdf->Cell(55,6,utf8_decode(''),0,0,'L');
      $pdf->Cell(55,6,utf8_decode(''),0,0,'L');
      $pdf->Cell(55,6,utf8_decode($mont),0,1,'L');

      
      // $pdf->Cell(55,6,utf8_decode('Nom'),0,0,'L');
      // $pdf->Cell(55,6,utf8_decode('Quantite'),0,0,'L');
      // $pdf->Cell(55,6,utf8_decode('Prix Unitaire'),0,0,'L');
      // $pdf->Cell(55,6,utf8_decode('Prix Total'),0,1,'L');

      // $pdf->Cell(100,6,utf8_decode('Congé annuel '),0,1,'L');
      // $pdf->Cell(55,6,utf8_decode(''),0,0,'L');
      // $pdf->Cell(8,6,utf8_decode(' '),1,0,'L');
      // $pdf->Cell(100,6,utf8_decode('Congé de circonstance'),0,1,'L');
      // $pdf->Cell(55,6,utf8_decode(''),0,0,'L');
      // $pdf->Cell(8,6,utf8_decode(' '),1,0,'L');
      // $pdf->Cell(100,6,utf8_decode('Autres : '),0,1,'L');



      // $pdf->Ln(5);
      // $pdf->Cell(10,8,utf8_decode('Le '),0,0,'L');
      // $pdf->Cell(10,8,utf8_decode('15/11/2022'),0,1,'L');
      // $pdf->Cell(10,8,utf8_decode('Signature du Chef de Service Administration '),0,1,'L');
      
      // $pdf->Ln(10);
      // $pdf->Cell(50,0,utf8_decode(''),1,1,'L');
      // $pdf->Ln(10);
      // $pdf->Cell(190,0,utf8_decode(' '),1,1,'L');
      // $pdf->Ln(5);



      // $pdf->Cell(90,5,utf8_decode('Accordé le'),0,0,'L');
      // $pdf->Cell(10,5,utf8_decode(''),0,0,'L');
      // $pdf->Cell(90,5,utf8_decode('Refusé le'),0,1,'L');
      // // $pdf->Ln(10);
      // $pdf->Cell(100,5,utf8_decode(''),0,0,'L');
      // $pdf->Cell(90,5,utf8_decode('Motif:'),0,1,'L');
      // // $pdf->Ln(2);
      // $pdf->Cell(100,0,utf8_decode(''),0,0,'L');
      // $pdf->Cell(90,0,utf8_decode(''),1,1,'L');
      
      $log=$this->Model->getRequeteOne('SELECT DATE_TRAITEMENT FROM doc_interne_document_historique WHERE 1 AND doc_interne_document_historique.ID_ETAPE_DOC_INTERNE = 1 AND doc_interne_document_historique.ID_ACTION_INTENE = 1 AND ID_DOC_INTERNE ='.$id.' ');
      $emp_log=$this->Model->getRequeteOne('SELECT rh_employe.NOM, rh_employe.PRENOM, rh_employe.SIGNATURE FROM config_user JOIN rh_employe ON rh_employe.ID_EMPLOYE = config_user.ID_EMPLOYE WHERE 1 AND config_user.PROFIL_ID = 8');
      $sign_chef_log=$nouveau_urlpass.'uploads/signatures/'.$emp_log['SIGNATURE'];


      $dg=$this->Model->getRequeteOne('SELECT DATE_TRAITEMENT FROM doc_interne_document_historique WHERE 1 AND doc_interne_document_historique.ID_ETAPE_DOC_INTERNE = 2 AND doc_interne_document_historique.ID_ACTION_INTENE = 4 AND ID_DOC_INTERNE ='.$id.' ');
      $dg_log=$this->Model->getRequeteOne('SELECT rh_employe.NOM, rh_employe.PRENOM, rh_employe.SIGNATURE FROM config_user JOIN rh_employe ON rh_employe.ID_EMPLOYE = config_user.ID_EMPLOYE WHERE 1 AND config_user.PROFIL_ID = 3');
      $sign_dg_log=$nouveau_urlpass.'uploads/signatures/'.$dg_log['SIGNATURE'];

      $pdf->Ln(20);
      $pdf->SetFont('Arial','B',10);
      $pdf->Cell(90,5,utf8_decode($emp_log['NOM'].' '.$emp_log['PRENOM']),0,0,'L');
      $pdf->Image($sign_chef_log, 10, 195, 60, 0, 'PNG');
      $pdf->Cell(10,5,utf8_decode(''),0,0,'L');
      $pdf->Cell(90,5,utf8_decode($dg_log['NOM'].' '.$dg_log['PRENOM']),0,0,'L');
      $pdf->Cell(90,5,utf8_decode(''),0,1,'L');
      $pdf->Cell(90,5,utf8_decode($log['DATE_TRAITEMENT']),0,0,'L');
      $pdf->Cell(10,5,utf8_decode(''),0,0,'L');
      $pdf->Image($sign_dg_log, 100, 195, 60, 0, 'PNG');
      $pdf->Cell(90,5,utf8_decode($dg['DATE_TRAITEMENT']),0,0,'L');
      
      // $pdf->Cell(90,5,utf8_decode($dg['DATE_TRAITEMENT']),0,0,'L');

   


     

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

    }


    

    
}
