<?php
function insertionMontant($matricule, $annee, $niveau, $annee_acad, $idFiliere, $refFacture, $file, $montant, $fraismomo, $dateTransaction, $idComptable, $montantNew, $solde, $depassement, $frais, $affiche, $operateur, $statutOperation, $tel, $uuid, $bdd) {
    $idStatut = 0;
	try{
		$sql  = 'INSERT INTO `facture`(`matricule`, `annee`, `niveau`, `annee_acad`, `idFiliere`, `refFacture`, `recubank`, `montant`, `fraisOperation`, `soldedu`, `dateTransaction`, `dateValidation`, `idComptable`, `typeOperation`, `statutOperation`, `msisdn`, `uidmomo`) ';
		$sql .= 'VALUES (:matricule, :annee, :niveau, :annee_acad, :idFiliere, :refFacture, :recubank, :montant, :fraismomo, :soldedu, :dateTransaction, :dateValidation, :idComptable, :typeOperation, :statutOperation, :msisdn, :uidmomo)';
		$statement = $bdd->prepare($sql);
		$statement->bindParam(':matricule', 		$matricule, PDO::PARAM_STR);
		$statement->bindParam(':annee', 			$annee, 	PDO::PARAM_STR);
		$statement->bindParam(':niveau',  			$niveau, 	PDO::PARAM_STR);
		$statement->bindParam(':annee_acad',  		$annee_acad, 	PDO::PARAM_STR);
		$statement->bindParam(':idFiliere',  		$idFiliere, 	PDO::PARAM_STR);
		$statement->bindParam(':refFacture',  		$refFacture, 	PDO::PARAM_STR);
		$statement->bindParam(':recubank',  		$file, 	PDO::PARAM_STR);
		$statement->bindParam(':montant',  			$montant, 	PDO::PARAM_STR);
		$statement->bindParam(':fraismomo',  		$fraismomo, 	PDO::PARAM_STR);
		$statement->bindParam(':soldedu',  			$solde, 	PDO::PARAM_STR);
		$statement->bindParam(':dateTransaction',  	$dateTransaction, 	PDO::PARAM_STR);
		$statement->bindParam(':dateValidation',  	$dateTransaction, 	PDO::PARAM_STR);
		$statement->bindParam(':idComptable',  		$idComptable, 	PDO::PARAM_STR);
		$statement->bindParam(':typeOperation',  	$operateur, 	PDO::PARAM_STR);
		$statement->bindParam(':statutOperation',	$statutOperation, 	PDO::PARAM_STR);
		$statement->bindParam(':msisdn',	        $tel, 	PDO::PARAM_STR);
		$statement->bindParam(':uidmomo',	        $uuid, 	PDO::PARAM_STR);
		echo $result = $statement->execute();
    	$ref = $bdd->lastInsertId();//id de la transaction dans la table interne de GestStudent
    	return $ref;
	}catch(PDOException  $e ){
	    return false;
		$impossible ='
		<div style="width:400px;background:#fff;padding:20px;border-radius:5px;font-family:Calibri;box-shadow:0px 0px 5px #ccc inset;margin:auto;">
			<div style="color:#555;">
				'.$e->getMessage( ).'
			</div>
		</div>
		';
		echo $impossible;
	}
}

function validationMontant($montant, $montantNew, $depassement, $annee_acad, $annee, $niveau, $idFiliere, $matricule, $idStatut, $bdd) {
	try{
		$sql1  = 'UPDATE `tabinscription` SET montant = :m, depassementScolarite = :d  WHERE annee_acad= :acad AND  annee = :an AND niveauFormation = :niv AND idFiliere = :id AND matricule = :matri';
		$statement1 = $bdd->prepare($sql1);
		$statement1->bindParam(':m', 				$montantNew, PDO::PARAM_STR);
		$statement1->bindParam(':d', 				$depassement, PDO::PARAM_STR);
		$statement1->bindParam(':acad',  			$annee_acad, PDO::PARAM_STR);
		$statement1->bindParam(':an', 				$annee, PDO::PARAM_STR);
		$statement1->bindParam(':niv', 				$niveau, PDO::PARAM_STR);
		$statement1->bindParam(':id', 				$idFiliere, PDO::PARAM_STR);
		$statement1->bindParam(':matri', 			$matricule, PDO::PARAM_STR);
		$result = $statement1->execute();	
		
		/*$sqli  = 'UPDATE `contrat` SET statut = :st WHERE (matricule = :matricule AND  annee = :an AND idFiliere = :id AND montant = :montant)';
		$statementi = $bdd->prepare($sqli);
		$statementi->bindParam(':st', 	        $idStatut, 	PDO::PARAM_STR);
		$statementi->bindParam(':matricule', 	$matricule, 	PDO::PARAM_STR);
		$statementi->bindParam(':an', 			$annee, PDO::PARAM_STR);
		$statementi->bindParam(':id', 			$idFiliere, PDO::PARAM_STR);
		$statementi->bindParam(':montant', 	    $montant, 	PDO::PARAM_STR);
		$statementi->execute();
		if($montantNew >= $frais) {
			$estAjour = 'YES';
			try{
				$sql1  = 'UPDATE `tabinscription` SET estAjour = :a  WHERE annee_acad= :acad AND  annee = :an AND niveauFormation = :niv AND idFiliere = :id AND matricule = :matri';
				$statement1 = $bdd->prepare($sql1);
				$statement1->bindParam(':a', 				$estAjour, PDO::PARAM_STR);
				$statement1->bindParam(':acad',  			$annee_acad, PDO::PARAM_STR);
				$statement1->bindParam(':an', 				$annee, PDO::PARAM_STR);
				$statement1->bindParam(':niv', 				$niveau, PDO::PARAM_STR);
				$statement1->bindParam(':id', 				$idFiliere, PDO::PARAM_STR);
				$statement1->bindParam(':matri', 			$matricule, PDO::PARAM_STR);
				$statement1->execute();
			}catch(PDOException  $e ){
				$impossible ='
				<div style="width:400px;background:#fff;padding:20px;border-radius:5px;font-family:Calibri;box-shadow:0px 0px 5px #ccc inset;margin:auto;">
					<div style="color:#555;">
						'.$e->getMessage( ).'
					</div>
				</div>
				';
				echo $impossible;
			}
		}*/
	}catch(PDOException  $e ){
		$impossible ='
		<div style="width:400px;background:#fff;padding:20px;border-radius:5px;font-family:Calibri;box-shadow:0px 0px 5px #ccc inset;margin:auto;">
			<div style="color:#555;">
				'.$e->getMessage( ).'
			</div>
		</div>
		';
		echo $impossible;
	}
}
?>