<?php
session_start();
require_once('tcpdf_include.php');
$idEtablissement = $_SESSION["idEtablissement"];

include('/home/iitech/progiciel.geststudent.net/configuration_etabli.php');

include_once('../../Library/Basededonnees.php');

$DB_DATABASE = $_SESSION["DB_DATABASE"];

$database = new Basededonnees($DB_DATABASE);

$bdd = $database->getBdd();

$databaseg = new Basededonnees('iitech_universite');

$bddg = $databaseg->getBdd();

header('Content-Type: text/html; charset="utf-8"');
//Recuperer toutes les informations provenant à afficher de la base de donnees 
/***Le matricule et le statut***/
$req= $bdd->prepare('SELECT * FROM tabetudiant WHERE matricule=?');
$req->execute(array($_POST['matricule']));
$matricule_statut=$req->fetch();

/*L'université*/
$uni= $bdd->prepare('SELECT * FROM `universite`');
$uni->execute();
$nomUniversite= $uni->fetch();

/*L'etablissement*/
$etabli= $bdd->prepare('SELECT * FROM etablissement WHERE idEtablissement=?');
$etabli->execute(array($_SESSION["idEtablissement"]));
$nomEtablissement= $etabli->fetch();

$req1=$bdd->prepare('SELECT idMention,idFiliere, intituleFilieres, sigleFilieres FROM filieres WHERE 	idFiliere=?');
$req1->execute(array($_POST['idFiliere']));
$idMention=$req1->fetch();


/***La filiere , la mention et le domaine***/
$req2=$bddg->prepare('SELECT idDomaine,sigleMention FROM mention WHERE idMention=?');
$req2->execute(array($idMention['idMention']));
$resultatMention=$req2->fetch();


$req3=$bddg->prepare('SELECT sigleDomaine FROM domaine WHERE idDomaine=?');
$req3->execute(array($resultatMention['idDomaine']));
$resultatDomaine=$req3->fetch();

$statutetud = 0;
try{
	$sql2 = "SELECT annee, statut, niveauFormation  FROM tabinscription  WHERE (matricule = :matri AND annee_acad = :acad)";
	$statement2 = $bdd->prepare($sql2);
	$statement2->bindParam(':matri',  	$matricule_statut['matricule'], 	PDO::PARAM_STR);
	$statement2->bindParam(':acad',  	$_POST['academique'], 			PDO::PARAM_STR);
	$statement2->execute();
	$row2=$statement2->fetch(PDO::FETCH_ASSOC);
	$statutetud     = $row2['statut'];	
	$niveauFormation = $row2['niveauFormation'];
	$annee = $row2['annee'];
}catch(PDOException  $e ){
		$impossible ='
		<div style="width:500px;background:#fff;padding:20px;border-radius:5px;font-family:Calibri;box-shadow:0px 0px 5px #ccc inset;margin:auto;">
			<h3 style="color:red;">Attention</h3>
			<div style="color:#555;">
				'.$e->getMessage( ).'.<br/>
				<a href=""><button style="width:100px;border:1px solid #ccc;text-align:center;background:#fff;">Retour</button></a>
			</div>
		</div>
		';
		echo $impossible;
}

//Recherche du diplome préparé
if($niveauFormation == 1) {
    $diplomeprepare = 'Licence';
}else{
    $diplomeprepare = 'Master';
}

//Statut de l'etudiant
$statutEtudiant=$bdd->prepare('SELECT statut FROM statuts WHERE idStatut=? AND idEtablissement=?');
$statutEtudiant->execute(array($statutetud, $nomEtablissement['idEtablissement']));
$statutEtudiant= $statutEtudiant->fetch();

//Cherchons le qrCode de l'etudiant
$qrcode= $bdd->prepare('SELECT codeQr FROM qrcode WHERE matricule=? AND idFiliere=? AND annee_acad=? AND annee=?');
$qrcode->execute(array($matricule_statut['matricule'],$idMention['idFiliere'],$_POST['academique'],$_POST['annee'])) or die(print_r($qrcode->errorInfo()));
$qrcode=$qrcode->fetch();

//Cherchons le qrCode de l'etablissement
$qrcodeEta= $bdd->prepare('SELECT codeQrEtablis FROM codeqretablis WHERE idEtablissement=? AND idFiliere=? AND annee_acad=? AND annee=?');
$qrcodeEta->execute(array($nomEtablissement['idEtablissement'],$idMention['idFiliere'],$_POST['academique'],$_POST['annee']));
$qrcodeEta=$qrcodeEta->fetch();

$compte= $bddg->prepare('SELECT * FROM comptebancaire WHERE idEtablissement=?');
$compte->execute(array($_SESSION['idEtablissement']));
$nomCompte= $compte->fetch();


//Déterminer le montant
$resMontant = $bdd->prepare('SELECT * FROM montant WHERE idEtablissement=? AND idFiliere =? AND annee =?');
$resMontant->execute(array($nomEtablissement['idEtablissement'],$idMention['idFiliere'],$_POST['annee']));
$Montant= $resMontant->fetch();
$montantTP   	= $Montant['montantTP'] ; //1
$montantBRS 	= $Montant['montantBRS'] ;//2
$montantRED 	= $Montant['montantRED'] ;//3
$fraisInscription 	= $Montant['fraisInscription'] ;//
$idStatutEtudiant=$statutetud;
$montant	= 0;
$tranche1  	= 0;
$tranche2 	= 0;
$tranche3   = 0; 
$tranche4   = 0; 
if($idStatutEtudiant == 1) {
	$montant 	= $montantTP;
	$tranche1  	= $Montant['tranche1TP'] ;
	$tranche2 	= $Montant['tranche2TP'] ;
	$tranche3   = $Montant['tranche3TP'] ; 
	$tranche4   = $Montant['tranche4TP'] ; 
}elseif($idStatutEtudiant == 2) {
	$montant 	= $montantBRS;
	//$fraisInscription 	= 0;
	$tranche1  	= $Montant['tranche1BRS'] ;
	$tranche2  	= $Montant['tranche2BRS'] ;
	$tranche3   = $Montant['tranche3BRS'] ; 
	$tranche4   = $Montant['tranche4BRS'] ; 
}elseif($idStatutEtudiant == 3) {
	$montant 	= $montantRED;
	$tranche1  	= $Montant['tranche1RED'] ;
	$tranche2 	= $Montant['tranche2RED'] ;
	$tranche3   = $Montant['tranche3RED'] ; 
	$tranche4   = $Montant['tranche4RED'] ; 
}

$niveauFormation = $idMention['sigleFilieres'];
$niveauFormation .= " ";
$niveauFormation .= $annee;

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'A5', true, 'UTF-8', false);
	
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('IITECH');
$pdf->SetTitle('');
$pdf->SetSubject('Reçu');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);



// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$PDF_MARGIN_TOP=8;
$pdf->SetMargins(PDF_MARGIN_LEFT, $PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);


// set auto page breaks
$PDF_MARGIN_BOTTOM=8;
$pdf->SetAutoPageBreak(TRUE, $PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
	require_once(dirname(__FILE__).'/lang/eng.php');
	$pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// add a page
$pdf->AddPage();

//<h3>Ministère de l\'Enseignement Supérieur et de la Recherche Scientifique</h3>
$periode = ($_POST['academique'] - 1).'-'.$_POST['academique'];
$pdf->SetFont('dejavusans', '', 10);
$html = '
		<div style="font-family:Calibri;font-size:10px;">
		<header style="text-align:center;">
			<table style="margin:0;padding:0;padding-top:10px;">
				<tr>
					<td style="width:10%;display:inline-block;vertical-align:top;">
					</td>
					<td style="width:80%;display:inline-block;text-align:center;font-family:Comic;font-size:10px;">
					    <img src="../../images/ic_launcher.png" alt="" style="width:50px;height:50px;"/>
					</td>
					<td style="width:10%;display:inline-block;vertical-align:top;padding-left:100px;">
					</td>
				</tr>
			</table>
		</header>
		<table style="padding-top:5px;">
		<tr>
			<td style="height:20px; width:100%;padding-top:10px;font-size:8px;text-align:center;">
				<div  style="font-family:Calibri;color:#000105;width:35%;text-align:center;">
					<b><i>GestStudent Finance</b></i> <br/>
					<b><i style="font-size:6px;">https://geststudent.net/</b></i><br/>
				</div>
			</td>
		</tr>
		</table>
		
		<div style="font-family:Impact;"><h5 style="margin:0;padding-top:50px;background-color:#2681dc;color:#fff;">Identité</h5></div>
		<table id="identity" style="font-family:Helvetica;font-size:11px;">
			<tr>
			<td id="identity_first" style="width:65%;">
				<span ><u>Nom et Prénom</u> : &nbsp;&nbsp; <b><i style="font-size:8px;"> '.$matricule_statut['nom'].' '.$matricule_statut['prenom'].'</i></b></span><br>
				<span><u>Spécialité</u> :&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<i style="font-size:8px;"><b> '.$niveauFormation.'</b></i></span><br>
				<span><u>Diplôme préparé</u> : &nbsp;<b><i style="font-size:8px;"> '.$diplomeprepare.'</i></b></span><br>
				<span><u>Statut</u> :  &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;<b><i style="font-size:8px;"> '.$statutEtudiant['statut'].'</i></b></span>
			</td>
			<td id="identity_second" style="width:35%;">
				<span><u>Sexe</u> : &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;<b><i style="font-size:8px;">'.$matricule_statut['sexe'].'</i></b></span><br>
				<span><u>Matricule</u> :&nbsp;&nbsp;<b><i style="font-size:8px;">'.$matricule_statut['matricule'].'</i></b></span><br>
				<span><u>Période</u> : &nbsp;&nbsp;<b><i style="font-size:8px;">'.$periode.'</i></b></span><br>
			</td>
			</tr>
		</table>
	
		<section>
			<article>
				<div style="font-family:Impact;"><h5 style="margin:0;padding-top:50px;background-color:#2681dc;color:#fff;">Référence reçu : '.$_POST['refFacture'].'</h5></div>
				<table id="identity" style="font-family:Helvetica;font-size:11px;">
					<tr>
					<td id="identity_first" style="width:50%;">
						<span ><u>Type opération </u> : &nbsp; <b><i style="font-size:8px;">'.$_POST['type'].' </i></b></span><br><br><br>
						<span><u>Montant opération </u> : &nbsp;<b><i style="font-size:8px;">'.$_POST['montant'].' FCFA</i></b></span>
					</td>
					<td id="identity_second" style="width:50%;">
						<span><u>Statut opération </u> : &nbsp; <b><i style="font-size:8px;">'.$_POST['statut'].'</i></b></span>
					</td>
					</tr>
				</table>
			</article>
			<article>
				<div></div>
				<h5 style="margin:0;padding:0;background-color:#2681dc;color:#fff;">Consulter vos résultats avec GestStudent Mobile</h5>
				<p style="border:1px solid #ccc;text-align:justify;font-size:7px;">GestStudent : <b><i>Une application développée par l\'Institut d\'Innovation Technologique(IITECH)</b></i>. A '.$nomUniversite['descruni'].',
				vos résultats sont disponibles à travers l\'application mobile GestStudent téléchargeable sur le Play Stor.</p>
			</article>
		
			<table>
				<tr>
					<td style="width:25%;margin:0;">
						<img style="width:65px;height:65px;" src="https://progiciel.geststudent.net/Inscription/qrcode/'.$_SESSION["idEtablissement"].'/'.$qrcode['codeQr'].'.png" alt=""/>
					</td>
					<td style="text-align:center;width:64%;">
						<div></div>
						<i>Fait le ......../........./...........<br/><br/><br/><br/><br/>Signature</i><br/>
					</td>
					<td style="width:12%;margin:0;">
						<img style="width:120px;height:120px;" src="https://progiciel.geststudent.net/Inscription/qrcode/'.$_SESSION["idEtablissement"].'/'.$qrcodeEta['codeQrEtablis'].'.png" alt=""/>
					</td>
				</tr>
			</table>
		</section>
		<br/>
		<hr/>
        <div >
            <div style="text-align: center ; color:#808080; font-size:6px";>
                <p  class="">Si vous avez des questions ou besoins d\'aide , vous pouvez <a href="https://iitech-benin.net/contact.php" >nous contacter</a><br>Siege Social: Abomey-Calavi  <br /> Tél: (00229) 61373500 <br /> iitech@iitech-benin.net <br />IITECH-BENIN : https://iitech-benin.net/</p>
            </div>
        </div>';

$pdf->writeHTML($html, true, 0, true, true);
$pdf->lastPage();

//echo $html;
// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('GestStudent_Finance.pdf', 'D');
?>
