<?php	

//header( 'content-type: text/html; charset=utf-8' );

	//session_start ();
//////////////////////////////////////////////////////////////////////////	

function DB()
 {	// test si on est en local ou bien sur un serveur

	$site = $_SERVER["SERVER_NAME"];	
	if( ($site == "localhost") || ($site == "127.0.0.1") )
	{		$db_host    = "localhost";	 
			$db_user   = "root";	  
			$db_pass   = ""; 
			$db_name   = "test";
	}	
	else {  /* $db_host    = "sql2";
			$db_user   = "analogde";
			$db_pass   = "rainbow";
			$db_name   = "analogde";*/

			$db_host    = "analogdepat.mysql.db";   
			$db_user   = "analogdepat"; 	   
			$db_pass   = "Un92pac007";	    
			$db_name   = "analogdepat";	
			//$db_host    = "bonneansoudb.mysql.db";   	 
			//$db_user   = "bonneansoudb"; 	  
			//$db_pass   = "Un92pac007";	   
			//$db_name   = "bonneansoudb";		 
			
			//analogdepdl//analogdepat.mysql.db		
		}		

		$connexion = mysqli_connect($db_host,$db_user,$db_pass); 	
		if(!$connexion)
		{	die('Could not connect: ' . mysql_error());		
	    }
 		mysqli_select_db($connexion, $db_name); 		
		return($connexion);
}	
//////////////////////////////////////////////////////////////////////////	

function charger_configuration()
{		
		$connexion = DB(); 
		
		$_SESSION['table_configuration'] = "configuration_v3";
		
		//$sql = 'SELECT * FROM configuration';
		$sql = "SELECT * FROM " .$_SESSION['table_configuration'];

		mysqli_query($connexion, "SET NAMES 'utf8'");
		$result = mysqli_query($connexion, $sql );
		$data = mysqli_fetch_assoc($result);
		
		$annee = date('Y');
		
		// par défaut, on prend l'année en court
		$_SESSION['table_reservation'] = "reservation" .$annee ."_v3";
		$_SESSION['table_gestion'] = "gestion_v3";
		$_SESSION['mode_debug'] = $data['mode_debug'];	
		
		//$_SESSION['year'] = $annee;
		//$table = "reservation_dev" .$annee;
		//$_SESSION['table_reservation'] = $table;	
		//$_SESSION['table_reservation'] = "copy_reservation2017";	
		
		$_SESSION['tarifCE'] = $data['tarifCE'];	
		$_SESSION['tarifnormal'] = $data['tarifnormal'];
		$_SESSION['tarif30'] = $data['tarif30'];
		
		$_SESSION['nb_creneau'] = $data['nb_creneau'];
		
}

//////////////////////////////////////////////////////////////////////////	

function mode_normal($jour)
{		
		$connexion = DB(); 
		$annee = date('Y');
		//$table = "reservation" .$annee;
		$table = $_SESSION['table_reservation'];
		$sql = "select normal from " .$table ." WHERE rendezvous ='$jour' ";
		$res = mysqli_query($connexion, $sql );	
		$data = mysqli_fetch_assoc($res);
		//echo "Etat normal avant enregistrement " .$data['normal'];
		//echo "<br>";
		$val = $data['normal'] + 1;
		//echo "Etat normal aprés enregistrement " .$val;
		//echo "<br>";
		$sql = "UPDATE " .$table ." SET normal = '$val' WHERE rendezvous ='$jour' ";
		$res = mysqli_query($connexion, $sql );	
}
//////////////////////////////////////////////////////////////////////////	

function mode_etendu($jour)
{		
		$connexion = DB(); 
		$annee = date('Y');
		//$table = "reservation" .$annee;
		$table = $_SESSION['table_reservation'];
		$sql = "select etendu from " .$table ." WHERE rendezvous ='$jour' ";
		$res = mysqli_query($connexion, $sql );	
		$data = mysqli_fetch_assoc($res);
		//echo "Etat etendu avant enregistrement " .$data['etendu'];
		//echo "<br>";
		$val = $data['etendu'] + 1 ;
		//echo "Etat etendu aprés enregistrement " .$val;
		//echo "<br>";
		$sql = "UPDATE " .$table ." SET etendu = '$val' WHERE rendezvous ='$jour' ";
		$res = mysqli_query($connexion, $sql );	
}
//////////////////////////////////////////////////////////////////////////	
function allocation($mois)
{		
		$connexion = DB(); 
		
		$annee = date('Y');
		//$table = "reservation" .$annee;
		$table = $_SESSION['table_reservation'];
		
		$ts_du_jour = strtotime(date("d-m-Y"));
		
		$sql = "SELECT * FROM " .$table  ." WHERE mois ='$mois' AND mode = 'actif' ";	
		//echo $sql;
		$result = mysqli_query($connexion, $sql );
	    if(!$result)	
		{	die('Impossible d\'exécuter la requête :' . mysql_error());	
	    }

		while ($datas = mysqli_fetch_assoc ($result))
		{
				$tab_reservation = array();	
		
				$seance1 = $datas['seance1'];
				$seance2 = $datas['seance2'];
				$seance3 = $datas['seance3'];
				$seance4 = $datas['seance4'];
				$seance5 = $datas['seance5'];
				$seance6 = $datas['seance6'];
				$seance7 = $datas['seance7'];
				$seance8 = $datas['seance8'];
				$jour = $datas['rendezvous'];
				
				$str_tmp = $seance1 ." " .$seance2 ." " .$seance3 ." " .$seance4 ." " .$seance5 ." " .$seance6 ." " .$seance7 ." " .$seance8;
				//echo $str_tmp;
				//echo '<br>';
				
				// find fermer
				//$detection = stristr($email, 'fermer');
				//echo $detection;
				//echo '<br>';
				
				//echo substr_count($text2, 'fermer');
				//echo '<br>';
				
				$tab = explode(" ",$str_tmp);
				$nombre_occurence = count(preg_grep('/fermer/', $tab ));
				//echo $nombre_occurence;
								
				$horairedebut = $datas['horairedebut'];
				$horairefin = $datas['horairefin'];
				//$avant = $datas['avant'];
				//$apres = $datas['apres'];
				$ts_reservation = $datas['ts'];
		
				$indice_debut = substr($horairedebut, 5);					
				$indice_fin = substr($horairefin, 4);
		
				$plage = ($indice_fin  - $indice_debut) + 1;
				//echo $plage;
				//echo '<br>';
		
				if(strlen($seance1) !=0 && $seance1 != "unselect" && $seance1 != "creneau ouvert" && $seance1 != "creneau fermer")
				{ $tab_reservation[] = $seance1;
				}
				if(strlen($seance2) !=0 && $seance2 != "unselect" && $seance2 != "creneau ouvert" && $seance2 != "creneau fermer")
				{ $tab_reservation[] = $seance2;
				}
				if(strlen($seance3) !=0 && $seance3 != "unselect" && $seance3 != "creneau ouvert" && $seance3 != "creneau fermer")
				{ $tab_reservation[] = $seance3;
				}
				if(strlen($seance4) !=0 && $seance4 != "unselect" && $seance4 != "creneau ouvert" && $seance4 != "creneau fermer")
				{ $tab_reservation[] = $seance4;
				}
				if(strlen($seance5) !=0 && $seance5 != "unselect" && $seance5 != "creneau ouvert" && $seance5 != "creneau fermer")
				{ $tab_reservation[] = $seance5;
				}
				if(strlen($seance6) !=0 && $seance6 != "unselect" && $seance6 != "creneau ouvert" && $seance6 != "creneau fermer")
				{ $tab_reservation[] = $seance6;
				}
				if(strlen($seance7) !=0 && $seance7 != "unselect" && $seance7 != "creneau ouvert" && $seance7 != "creneau fermer")
				{ $tab_reservation[] = $seance7;
				}
				if(strlen($seance8) !=0 && $seance8 != "unselect" && $seance8 != "creneau ouvert" && $seance8 != "creneau fermer")
				{ $tab_reservation[] = $seance8;
				}
			
				//echo '<pre>';
				//print_r($tab_reservation);
				//echo "----> " .count($tab_reservation);
				//echo '<br>';
			
				if(count($tab_reservation) == $plage && $nombre_occurence != 0 )
				{
						echo '<pre>';
						print_r($tab_reservation);
					
						echo "ouverture " .$plage ."  " .$nombre_occurence ."  " .$jour;
						echo '<br>';
		
						$tab_seance = array();
			
						if($ts_reservation >= $ts_du_jour)	
						{		
			
							if($datas['seance1'] == "creneau fermer")
							{
								$tab_seance[1] = "creneau ouvert";
							}	
							else
							{
								$tab_seance[1] = $datas['seance1'];
							}	
					
							if($datas['seance2'] == "creneau fermer")
							{
								$tab_seance[2] = "creneau ouvert";
							}	
							else
							{
								$tab_seance[2] = $datas['seance2'];
							}	
		
							if($datas['seance3'] == "creneau fermer")
							{
								$tab_seance[3] = "creneau ouvert";
							}	
							else
							{
								$tab_seance[3] = $datas['seance3'];
							}	

							if($datas['seance4'] == "creneau fermer")
							{
								$tab_seance[4] = "creneau ouvert";
							}	
							else
							{
								$tab_seance[4] = $datas['seance4'];
							}	
	
							if($datas['seance5'] == "creneau fermer")
							{
								$tab_seance[5] = "creneau ouvert";
							}	
							else
							{
								$tab_seance[5] = $datas['seance5'];
							}	

							if($datas['seance6'] == "creneau fermer")
							{
								$tab_seance[6] = "creneau ouvert";
							}	
							else
							{
								$tab_seance[6] = $datas['seance6'];
							}	
					
							if($datas['seance7'] == "creneau fermer")
							{
								$tab_seance[7] = "creneau ouvert";
							}	
							else
							{
								$tab_seance[7] = $datas['seance7'];
							}	

							if($datas['seance8'] == "creneau fermer")
							{
								$tab_seance[8] = "creneau ouvert";
							}	
							else
							{
								$tab_seance[8] = $datas['seance8'];
							}	

							echo '<pre>';
							print_r($tab_seance);
							mysqli_query($connexion, "SET NAMES 'utf8'");	
							$sql = "UPDATE " .$table ." SET seance1 = '$tab_seance[1]' , 
															seance2 = '$tab_seance[2]' , 		
															seance3 = '$tab_seance[3]' ,
															seance4 = '$tab_seance[4]' , 			
															seance5 = '$tab_seance[5]' , 
															seance6 = '$tab_seance[6]' , 	
															seance7 = '$tab_seance[7]' , 	
															seance8 = '$tab_seance[8]' 
															WHERE rendezvous ='$jour' ";						
							//echo $sql;
							echo '<br>';
							$res1 = mysqli_query($connexion, $sql );			
							if(!$res1)
							{	
								die('Impossible d\'exécuter la requête :' . mysql_error());				
							}	
				
						}
			
						//print_r($tab_reservation);
						//echo '<pre>';
				}
		}
}
//////////////////////////////////////////////////////////////////////////	
		
function nouvelle_allocation($mois)
{		
		$connexion = DB(); 
	
		$cpt = 0;
		$ts_du_jour = strtotime(date("d-m-Y"));
	    
		$annee = date('Y');
		//$table = "reservation_dev" .$annee;
		$table = $_SESSION['table_reservation'];
		
		$sql = "SELECT * FROM " .$table  ." WHERE mois ='$mois' AND mode = 'actif' ";	
		//echo $sql;
		$result = mysqli_query($connexion, $sql );
	    if(!$result)	
		{	die('Impossible d\'exécuter la requête :' . mysql_error());	
	    }
		
		$nb_semaine = mysqli_num_rows($result);
		
		$_SESSION['seuilcreneau'] = $nb_semaine * $_SESSION['nb_creneau'] ; 		
		
		//while($datas = mysqli_fetch_assoc ($result))
		//{		
		//	$cpt = $cpt + $datas['normal'];	
		//}
		//$permission = 0;
				
		$tab_retour = compteur_reservation($mois);
		$cpt = count($tab_retour);
		
		//echo "****** " . $_SESSION['seuilcreneau'] . "  " .$cpt;
		
// ouverture total - fonctionnelle
// ouverture progressive - a faire si le besoin apparait
		
		if($cpt >= $_SESSION['seuilcreneau']) // 16 
		{	
			//echo "Total 16";
			mysqli_query($connexion, "SET NAMES 'utf8'");	
			$result = mysqli_query($connexion, $sql );
			while($datas = mysqli_fetch_assoc ($result))
			{	
				$indice = $datas['indice'];
				$cpt_normal = $datas['normal'];	
				$cpt_etendu = $datas['etendu'];	
				$jour = $datas['rendezvous'];	
				$ts_reservation = strtotime($jour);
			
				$tab_seance = array();
			
				if($ts_reservation >= $ts_du_jour)	
				{		
			
					if($datas['seance1'] == "creneau fermer")
					{
						$tab_seance[1] = "creneau ouvert";
					}	
					else
					{
						$tab_seance[1] = $datas['seance1'];
					}	
					
					if($datas['seance2'] == "creneau fermer")
					{
						$tab_seance[2] = "creneau ouvert";
					}	
					else
					{
						$tab_seance[2] = $datas['seance2'];
					}	
		
					if($datas['seance3'] == "creneau fermer")
					{
						$tab_seance[3] = "creneau ouvert";
					}	
					else
					{
						$tab_seance[3] = $datas['seance3'];
					}	

					if($datas['seance4'] == "creneau fermer")
					{
						$tab_seance[4] = "creneau ouvert";
					}	
					else
					{
						$tab_seance[4] = $datas['seance4'];
					}	
	
					if($datas['seance5'] == "creneau fermer")
					{
						$tab_seance[5] = "creneau ouvert";
					}	
					else
					{
						$tab_seance[5] = $datas['seance5'];
					}	

					if($datas['seance6'] == "creneau fermer")
					{
						$tab_seance[6] = "creneau ouvert";
					}	
					else
					{
						$tab_seance[6] = $datas['seance6'];
					}	
					
					if($datas['seance7'] == "creneau fermer")
					{
						$tab_seance[7] = "creneau ouvert";
					}	
					else
					{
						$tab_seance[7] = $datas['seance7'];
					}	

					if($datas['seance8'] == "creneau fermer")
					{
						$tab_seance[8] = "creneau ouvert";
					}	
					else
					{
						$tab_seance[8] = $datas['seance8'];
					}	

					//echo '<pre>';
					//print_r($tab_seance);
					mysqli_query($connexion, "SET NAMES 'utf8'");	
					$sql = "UPDATE " .$table ." SET seance1 = '$tab_seance[1]' , 
													seance2 = '$tab_seance[2]' , 		
													seance3 = '$tab_seance[3]' ,
													seance4 = '$tab_seance[4]' , 			
													seance5 = '$tab_seance[5]' , 
													seance6 = '$tab_seance[6]' , 	
													seance7 = '$tab_seance[7]' , 	
													seance8 = '$tab_seance[8]' 
													WHERE rendezvous ='$jour' ";						
					//echo $sql;
						
					$res1 = mysqli_query($connexion, $sql );			
					if(!$res1)
					{	
						die('Impossible d\'exécuter la requête :' . mysql_error());				
					}	
			
				} 
			} // while
		}	// if
			
}				
 //////////////////////////////////////////////////////////////////////////	

/*			
						echo "Trace";
						if($indice == 1 )
						{			
							if($cpt_normal == 4 && $cpt_etendu == 0 )
							{
								$sql = "UPDATE " .$table ." SET seance7 = '' WHERE rendezvous ='$jour' ";	
								$result1 = mysqli_query($connexion, $sql );
								//echo "L1 14H20";
							}	
							if($cpt_normal == 4 && $cpt_etendu == 1 )
							{
								$sql = "UPDATE " .$table ." SET seance8 = '' WHERE rendezvous ='$jour' ";	
								$result1 = mysqli_query($connexion, $sql );	
								//echo "L1 14H40";
							}
							if($cpt_normal == 4 && $cpt_etendu == 2 )
							{	$permission = 2;
							}
						}				
						
						if($indice == 2 && $permission == 2 )
						{			
							if($cpt_normal == 4 && $cpt_etendu == 0 )
							{
								$sql = "UPDATE " .$table ." SET seance7 = '' WHERE rendezvous ='$jour' ";	
								$result1 = mysqli_query($connexion, $sql );
								//echo "L2 14H20";
							}	
							if($cpt_normal == 4 && $cpt_etendu == 1 )
							{
								$sql = "UPDATE " .$table ." SET seance8 = '' WHERE rendezvous ='$jour' ";	
								$result1 = mysqli_query($connexion, $sql );	
								//echo "L2 14H40";
							}
							if($cpt_normal == 4 && $cpt_etendu == 2 )
							{	$permission = 3;
							}
						}
						
						if($indice == 3 && $permission == 3 )
						{			
							if($cpt_normal == 4 && $cpt_etendu == 0 )
							{
								$sql = "UPDATE " .$table ." SET seance3 = '' WHERE jour ='$jour' ";	
								$result1 = mysqli_query($connexion, $sql );
								//echo "L3 13H";
							}	
							if($cpt_normal == 4 && $cpt_etendu == 1 )
							{
								$sql = "UPDATE " .$table ." SET seance8 = '' WHERE jour ='$jour' ";	
								$result1 = mysqli_query($connexion, $sql );	
								//echo "L3 14H40";
							}
							if($cpt_normal == 4 && $cpt_etendu == 2 )
							{	$permission = 4;
							}
						}
						
						if($indice == 4 && $permission == 4)
						{			
							if($cpt_normal == 4 && $cpt_etendu == 0 )
							{
								$sql = "UPDATE " .$table ." SET seance3 = '' WHERE rendezvous ='$jour' ";	
								$result1 = mysqli_query($connexion, $sql );
								//echo "L4 13H";
							}	
							if($cpt_normal == 4 && $cpt_etendu == 1 )
							{
								$sql = "UPDATE " .$table ." SET seance8 = '' WHERE rendezvous ='$jour' ";	
								$result1 = mysqli_query($connexion, $sql );	
								//echo "L4 14H40";
							}
						}
						*/
 
					/*if($id == 1 || $id == 2)
					{	
						if($cpt_normal == 4 && $cpt_etendu == 0)
						{
							$sql = "UPDATE " .$mois_actif ." SET seance7 = '' WHERE jour ='$jour' ";	
							$result1 = mysqli_query($connexion, $sql );
						}	
						if($cpt_normal == 4 && $cpt_etendu == 1)
						{
							$sql = "UPDATE " .$mois_actif ." SET seance8 = '' WHERE jour ='$jour' ";	
							$result1 = mysqli_query($connexion, $sql );	
						}	
					}*/	
					
					/*if($id == 3 || $id == 4 )
					{	
						if($cpt_normal == 4 && $cpt_etendu == 0)
						{
							$sql = "UPDATE " .$mois_actif ." SET seance3 = '' WHERE jour ='$jour' ";	
							$result1 = mysqli_query($connexion, $sql );
					    }	
						if($cpt_normal == 4 && $cpt_etendu == 1)
						{
							$sql = "UPDATE " .$mois_actif ." SET seance8 = '' WHERE jour ='$jour' ";	
							$result1 = mysqli_query($connexion, $sql );	
						}	
					}*/	
 //////////////////////////////////////////////////////////////////////////	

function sauvegarde_table_gestion($annee,$mois,$jour,$client,$seance_choix,$formule)
{
		$connexion = DB(); 
		
		date_default_timezone_set('Europe/Paris');
		$date_inscription = date("d-m-Y") ." " .date("H:i:s");
		
		//$str = "gestion";
		
		$str = $_SESSION['table_gestion'];
		
		mysqli_query($connexion, "SET NAMES 'utf8'");	
		
		// recherche les formule courtes et les formules longues
		
		$sql = "INSERT INTO " .$str ."(id,annee, mois,jour,date_inscription,client,mode,formule,seance,compteur,montant,statut, cheque, banque ) 
						VALUES('', '$annee', '$mois', '$jour', '$date_inscription', '$client' ,'actif' , '$formule' , '$seance_choix'   ,'0','0','Non encaissé','', '')"; 
		//echo $sql;
		//echo "<br>";
		$result = mysqli_query($connexion, $sql );
		if(!$result)
		{
			die('Impossible d\'exécuter la requête :' . mysql_error());
		}
 }
//////////////////////////////////////////////////////////////////////////	
 
 function statistiques($annee,$mois,$formule)
 {
			$connexion = DB();  
	 
			//$table = "reservation_dev" .$annee;
			$table = $_SESSION['table_reservation'];

			$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and formule = '$formule' and mois = '$mois'  ";
			//echo $sql;
	 
			//$sql="SELECT * FROM " .$mois_actif;	
			mysqli_query($connexion, "SET NAMES 'utf8'");	
			$query = mysqli_query($connexion, $sql );
		
			if(!$query)	
			{	die('Impossible d\'exécuter la requête :' . mysql_error());	
			}

			$tab_client = array();
			$tab_statistiques = array();
	
			while ($datas = mysqli_fetch_assoc ($query))
			{
				$seance1 = $datas['seance1'];
				$seance2 = $datas['seance2'];
				$seance3 = $datas['seance3'];
				$seance4 = $datas['seance4'];
				$seance5 = $datas['seance5'];
				$seance6 = $datas['seance6'];
				$seance7 = $datas['seance7'];
				$seance8 = $datas['seance8'];
		
				if(strlen($seance1) != 0 && $seance1 != "unselect")
				{ $tab_client[] = $seance1;
				}
				if(strlen($seance2) != 0 && $seance2 != "unselect")
				{ $tab_client[] = $seance2;
				}
				if(strlen($seance3) != 0 && $seance3 != "unselect")
				{ $tab_client[] = $seance3;
				}
				if(strlen($seance4) != 0 && $seance4 != "unselect")
				{ $tab_client[] = $seance4;
				}
				if(strlen($seance5) != 0 && $seance5 != "unselect")
				{ $tab_client[] = $seance5;
				}
				if(strlen($seance6) != 0&& $seance6 != "unselect")
				{ $tab_client[] = $seance6;
				}
				if(strlen($seance7) != 0 && $seance7 != "unselect")
				{ $tab_client[] = $seance7;
				}
				if(strlen($seance8) != 0 && $seance8 != "unselect")
				{ $tab_client[] = $seance8;
				}
			}

			//echo '<pre>';
			//print_r($tab_client);
			
			$nbre_enregistrements = count($tab_client);	
			
			if($nbre_enregistrements != 0)
			{
	
				$tab_less_doublons = array_unique($tab_client);
				//echo '<pre>';
				//print_r($tab_less_doublons);
	
				$tab_reindex = array_values($tab_less_doublons);
				//echo '<pre>';
				//print_r($tab_reindex);
	
				for( $i=0; $i < count($tab_reindex) ; $i++)
				{
					$cpt = 0;
					for( $j=0; $j < count($tab_client) ; $j++)
					{
						if($tab_client[$j] == $tab_reindex[$i])
						{	$cpt = $cpt + 1;
						}	 
					}
					$tab_statistiques[] = $cpt;
				}
	
				//echo '<pre>';
				//print_r($tab_statistiques);
				return array($tab_statistiques,$tab_reindex);
				
			}
 }
//////////////////////////////////////////////////////////////////////////	

function affichage($datas,$tab_client,$tab_compteur,$i)
 {
							$tarif_CE = 5.0;
							$tarif_plein = 12.67;
 
							$key = array_search($datas['client'], $tab_client);
							//echo $key;
					
							// une seule réservation
							if($tab_compteur[$key] == 0)
							{	$montant = $tarif_plein;
								//echo "step1";
							}	
							if($tab_compteur[$key] == 1)
							{	$montant = $tarif_CE;
								//echo "step2";
							}
							// plusieures réservation
							if($tab_compteur[$key] > 1)
							{	$tab_compteur[$key] = 0;
								$montant = $tarif_CE;
								//echo "step3";
							}	
		
							echo '<td align="center">' .$i .'</td>';
							echo '<td align="center">' .$datas['client'] .'</td>';
							echo '<td align="center">' .$datas['jour'] .'</td>';

										
							switch ($datas['seance'])
							{
								case "1":
									$val = "12H20";
									break;
								case "2":
									$val = "12H40";
									break;	
								case "3":
									$val = "13H";
									break;
								case "4":
									$val = "13H20";
									break;
								case "5":
									$val = "13H40";
									break;
								case "6":
									$val = "14H";
									break;
								case "7":
									$val = "14H20";
									break;
								case "8":
									$val = "14H40";
								break;			
							}
					
							echo '<td align="center">' .$val .'</td>';
							
							echo '<td align="center">' .$montant . ' Euros' .'</td>';
					
							if($_SESSION['nom'] == "admin")
							{
								$select_objet = 'select' .$i;
								echo ' <td align="center"><select name=' .$select_objet .'>';
								if($datas['statut'] == "Payé")
								{
									echo '	<option value="Payé" selected>Payé</option>
											<option value="Non encaissé">Non encaissé</option>';
								}	
					
								if($datas['statut'] == "Non encaissé")
								{
									echo '	<option value="Non encaissé" selected>Non encaissé</option>
											<option value="Payé" >Payé</option>';
								}
							}		
							else
							{	$select_objet = 'select' .$i;
								echo ' <td align="center"><select name=' .$select_objet .' disabled>';
								if($datas['statut'] == "Payé")
								{
									echo '	<option value="Payé" selected>Payé</option>
											<option value="Non encaissé">Non encaissé</option>';
								}	
					
								if($datas['statut'] == "Non encaissé")
								{
									echo '	<option value="Non encaissé" selected>Non encaissé</option>
											<option value="Payé" >Payé</option>';
								}
							}					
					
							$cheque_objet = 'cheque' .$i;
							echo '<td align="center"><input type="text" name=' .$cheque_objet  .' value = ' .$datas['cheque']   .'></td>';
					
							$banque_objet = 'banque' .$i;
							echo '<td align="center"><input type="text" name=' .$banque_objet  .' value = ' .$datas['banque']   .'></td>';
					
							echo '
							</select></td> ';
							echo '</tr>';  
					
							
 }
 
//////////////////////////////////////////////////////////////////////////	

function disponible($seance, $jour)
{
		$connexion = DB(); 
		
		$annee = date('Y');	
		//$table = "reservation_dev" .$annee;
		$table = $_SESSION['table_reservation'];
		
		$sql = "SELECT * FROM " .$table  ." WHERE rendezvous ='$jour' ";	
		$result = mysqli_query($connexion, $sql );	
		$data = mysqli_fetch_assoc($result);
		if( $data[$seance] == "" ||  $data[$seance] == "creneau ouvert" )
		{
			$action = "libre"; 
			//echo "Libre";
		}
		else
		{
			$action = "reserver";
			//echo $data[$seance];		
		}
		//echo '<br>';
		
		return($action);
		
}
//////////////////////////////////////////////////////////////////////////	


function compteur_reservation($mois)
{
		$connexion = DB();  

		$annee = date('Y');
		//$table = "reservation_dev" .$annee;
		$table = $_SESSION['table_reservation'];
		//echo "++++ " .$table;

		$sql = "SELECT * FROM " .$table  ." WHERE mois ='$mois' and mode = 'actif' ";	
		mysqli_query($connexion, "SET NAMES 'utf8'");
		$result = mysqli_query($connexion, $sql );
		
		if(!$result)	
		{	die('Impossible d\'exécuter la requête :' . mysql_error());	
		}

		$tab_reservation = array();
	
		while ($datas = mysqli_fetch_assoc ($result))
		{
			$seance1 = $datas['seance1'];
			$seance2 = $datas['seance2'];
			$seance3 = $datas['seance3'];
			$seance4 = $datas['seance4'];
			$seance5 = $datas['seance5'];
			$seance6 = $datas['seance6'];
			$seance7 = $datas['seance7'];
			$seance8 = $datas['seance8'];
		
			if(strlen($seance1) !=0 && $seance1 != "unselect" && $seance1 != "creneau ouvert" && $seance1 != "creneau fermer")
			{ $tab_reservation[] = $seance1;
			}
			if(strlen($seance2) !=0 && $seance2 != "unselect" && $seance2 != "creneau ouvert" && $seance2 != "creneau fermer")
			{ $tab_reservation[] = $seance2;
			}
			if(strlen($seance3) !=0 && $seance3 != "unselect" && $seance3 != "creneau ouvert" && $seance3 != "creneau fermer")
			{ $tab_reservation[] = $seance3;
			}
			if(strlen($seance4) !=0 && $seance4 != "unselect" && $seance4 != "creneau ouvert" && $seance4 != "creneau fermer")
			{ $tab_reservation[] = $seance4;
			}
			if(strlen($seance5) !=0 && $seance5 != "unselect" && $seance5 != "creneau ouvert" && $seance5 != "creneau fermer")
			{ $tab_reservation[] = $seance5;
			}
			if(strlen($seance6) !=0 && $seance6 != "unselect" && $seance6 != "creneau ouvert" && $seance6 != "creneau fermer")
			{ $tab_reservation[] = $seance6;
			}
			if(strlen($seance7) !=0 && $seance7 != "unselect" && $seance7 != "creneau ouvert" && $seance7 != "creneau fermer")
			{ $tab_reservation[] = $seance7;
			}
			if(strlen($seance8) !=0 && $seance8 != "unselect" && $seance8 != "creneau ouvert" && $seance8 != "creneau fermer")
			{ $tab_reservation[] = $seance8;
			}
		}

		return($tab_reservation);
		//echo '<pre>';
		//print_r($datas);
}
//////////////////////////////////////////////////////////////////////////	

function email_html($action,$nom,$prenom,$email,$date, $msg)
{
	$jour  = date("d-m-Y");
	$heure = date("H:i:s");

	$sujet = 'Massage annulation: Accusé de réception ';
	$sujet = utf8_decode($sujet);
	$sujet = mb_encode_mimeheader($sujet,"UTF-8");
	
	if($action =="reserver")
	{
		$message = "<br /><br />
		$jour à $heure, <br /><br />
		<strong>Bonjour $prenom,
		<br />
		Tu viens de réserver une ou plusieures scéances de massages pour la date suivante:
		<br /> <br />
		Le $date à $msg  <br /> <br />
		Si tu souhaites annuler un rendez-vous, tu peux utiliser l'interface pour libérer le crénaux. 
		</strong>  <br /> <br />";
	
		$message .= "<br> <br>	A Bientôt :)";
	}
	
	if($action =="supprimer")
	{
		$message = "<br /><br />
		$jour à $heure, <br /><br />
		<strong>Bonjour $prenom,
		<br><br>
		Tu viens d'annuler un massage pour la date suivante:
		<br> 
		Le $date à $msg  
		</strong>";
	
		$message .= "<br> <br>	A Bientôt :)";
	}
	
	//$destinataire = 'patrice.delpy@neuf.fr';
	//$headers = "From: patrice.delpy@free.fr\n";
	$headers = "From: " .$email ."\n";
	//$headers .= "Reply-To: patrice.delpy@free.fr\n";
	$headers .= "X-Priority: 1 \n"; 
//	$headers .= "Content-Type: text/html; charset=\"UTF-8\"";
	
	$headers .='Content-Type: text/html; charset="UTF-8"'."\n";
	$headers .='Content-Transfer-Encoding: 8bit';
	
	//$mode_debug = $_SESSION['mode_debug'];
	//if($mode_debug == "no")
	//{
	
		$test = mail($email,$sujet,$message,$headers);
		if($test !=1)
		{
			"[INFOS]: L'email de confirmation n'a pas pu être envoyer !.";
		}
	//}		
}

	//$nom = "Patrice";
	//$prenom = "Delpy";
	//$email = "patrice.delpy@neuf.fr";
	//email_html($nom,$prenom,$email);
 
//////////////////////////////////////////////////////////////////////////	

function generer_mot_de_passe()
{
        $mot_de_passe = "";
       
        $chaine = "abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ023456789";
        $longeur_chaine = strlen($chaine);
		
		$nb_caractere = 6;
       
        for($i = 1; $i <= $nb_caractere; $i++)
        {
            $place_aleatoire = mt_rand(0,($longeur_chaine-1));
            $mot_de_passe .= $chaine[$place_aleatoire];
        }

        return $mot_de_passe;   
}
//////////////////////////////////////////////////////////////////////////	

function email_reset_password($prenom,$mdp,$email)
{
	$jour  = date("d-m-Y");
	$heure = date("H:i:s");

	$sujet = 'Reset de votre password ';
	$sujet = utf8_decode($sujet);
	$sujet = mb_encode_mimeheader($sujet,"UTF-8");
		
	$message = "<br /><br />
	$jour à $heure, <br /><br />
	<strong>Bonjour $prenom,
	<br />
	voici ton nouveau mot de passe : $mdp
	</strong>  <br /> <br />";
	
	$message .= "<br> <br>	A Bientôt :)";
	
	//$destinataire = 'patrice.delpy@neuf.fr';
	//$headers = "From: patrice.delpy@free.fr\n";
	$headers = "From: " .$email ."\n";
	//$headers .= "Reply-To: patrice.delpy@free.fr\n";
	$headers .= "X-Priority: 1 \n"; 
//	$headers .= "Content-Type: text/html; charset=\"UTF-8\"";
	
	$headers .='Content-Type: text/html; charset="UTF-8"'."\n";
	$headers .='Content-Transfer-Encoding: 8bit';
	
	mail($email,$sujet,$message,$headers);
}

	//$nom = "Patrice";
	//$prenom = "Delpy";
	//$email = "patrice.delpy@neuf.fr";
	//email_html($nom,$prenom,$email);
//////////////////////////////////////////////////////////////////////////	
function decoder_horaire($seance)
{
					switch ($seance)
					{
								case "1":
									$val = "12H20";
									break;
								case "2":
									$val = "12H40";
									break;	
								case "3":
									$val = "13H00";
									break;
								case "4":
									$val = "13H20";
									break;
								case "5":
									$val = "13H40";
									break;
								case "6":
									$val = "14H00";
									break;
								case "7":
									$val = "14H20";
									break;
								case "8":
									$val = "14H40";
								break;			
					}
					
					return($val);
 }
 //////////////////////////////////////////////////////////////////////////	
function codeur_horaire($seance)
{
					switch ($seance)
					{
								case "12H20":
									$val = "1";
									break;
								case "12H40":
									$val = "2";
									break;	
								case "13H00":
									$val = "3";
									break;
								case "13H20":
									$val = "4";
									break;
								case "13H40":
									$val = "5";
									break;
								case "14H00":
									$val = "6";
									break;
								case "14H20":
									$val = "7";
									break;
								case "14H40":
									$val = "8";
								break;			
					}
					
					return($val);
 }
 //////////////////////////////////////////////////////////////////////////	
 
function fichier_log($utilisateur)
{
	date_default_timezone_set('Europe/Paris');

	$ip = $_SERVER['REMOTE_ADDR'];
	$navigateur = $_SERVER['HTTP_USER_AGENT'];
	$host = $_SERVER['HTTP_HOST'];
	
	$infos = $utilisateur ." " .$navigateur; 
	
	$fichier2 = fopen("log.txt", "a");
	$tdate=getdate();
	$jour=sprintf("%02.2d",$tdate["mday"])."/".sprintf("%02.2d",$tdate["mon"])."/".$tdate["year"];
	$heure=sprintf("%02.2d",$tdate["hours"])."H".sprintf("%02.2d",$tdate["minutes"]);
	$d="[".$jour." à ".$heure."] ";
	fwrite($fichier2," ".$d.$ip."  ".$infos."\n");
	fclose($fichier2);

	
}
 
 //////////////////////////////////////////////////////////////////////////	
 
function backup_tables()
{
	error_reporting(0);

	$site = $_SERVER["SERVER_NAME"];
	if( ($site == "localhost") || ($site == "127.0.0.1") ) 
	{
			$db_host    = "localhost";
			$db_user   = "root";
			$db_pass   = ""; 
			$db_name   = "test";
	}
	else
	{
		$db_host    = "analogdepat.mysql.db";
		$db_user   = "analogdepat";
		$db_pass   = "Un92pac007";
		$db_name   = "analogdepat";
	}
	
	date_default_timezone_set('Europe/Paris');

	$annee = date('Y');
	//$table_reservation  = "reservation" .$annee;
	$link = mysql_connect($host,$user,$pass);
	mysql_select_db($name,$link);


	$tables = array();
	$tables[] = "configuration";
	$tables[] = "clients";
	$tables[] = $_SESSION['table_gestion']; //"gestion";
	$tables[] = $_SESSION['table_reservation']; //$table_reservation;

	foreach($tables as $table)
	{
		$result = mysql_query('SELECT * FROM '.$table);
		$num_fields = mysql_num_fields($result);
		$return.= 'DROP TABLE '.$table.';';
		$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
		$return.= "\n\n".$row2[1].";\n\n";
		for ($i = 0; $i < $num_fields; $i++) 
		{
			while($row = mysql_fetch_row($result))
			{
				$return.= 'INSERT INTO '.$table.' VALUES(';
				for($j=0; $j<$num_fields; $j++) 
				{
					$row[$j] = addslashes($row[$j]);
					$row[$j] = ereg_replace("\n","\\n",$row[$j]);
					if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
					if ($j<($num_fields-1)) { $return.= ','; }
				}
				$return.= ");\n";
			}
		}


		$return.="\n\n\n";
	}


	$jour = date('d-m-Y');
	$heure = date("H_i_s");
	//$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
	$infos = $jour.'-'.$heure;
	chdir('Backup');
	$handle = fopen('db-backup-'.$infos.'.sql','w+');
	//$handle = fopen($infos.'.sql','w+');
	fwrite($handle,$return);
	fclose($handle);

}

//////////////////////////////////////////////////////////////////////////	
   
function tableau_seance($mois,$jour)
{
		$connexion = DB(); 
		
		$annee = date('Y');	
		//$table = "reservation" .$annee;
		$table = $_SESSION['table_reservation'];
	
		$sql = "SELECT * FROM " .$table ." WHERE annee ='$annee' and mois ='$mois' and rendezvous ='$jour' ";	
		//echo $sql;	
		//echo '<br>';
		
		mysqli_query($connexion, "SET NAMES 'utf8'");	
		$result = mysqli_query($connexion, $sql );

		if(!$result)
		{	
			die('Impossible d\'exécuter la requête :' . mysql_error());
		}
		
		$data = mysqli_fetch_assoc($result);
		
		$horairedebut = $data['horairedebut'];
		$horairefin = $data['horairefin'];
		$avant = $data['avant'];
		$apres = $data['apres'];
		
		$indice_debut = substr($horairedebut, 5);					
		$indice_fin = substr($horairefin, 4);
		
		//echo $indice_debut ." ".$indice_fin ." " .$avant ." " .$apres;
		
		$tab_seance = array();		
		$tab_seance[] = "decalage";

		for( $i=1; $i <9 ; $i++)	
		{	
			$tab_seance[] = "unselect";							
		}	
		
		//echo '<pre>';
		//print_r($tab_seance);	
		
		for( $i = $indice_debut; $i <= $indice_fin ; $i++)				
		{ 
				$tab_seance[$i] = "normale";
		}
		
		//echo '<pre>';
		//print_r($tab_seance);	

		$supplement_avant_debut = $indice_debut - $avant;	
		$supplement_avant_fin = $indice_debut - 1;				
		$supplement_apres_debut = $indice_fin + 1;	
		$supplement_apres_fin = $indice_fin + $apres;

		for( $i=1; $i <9 ; $i++)
		{	/*		
			if( $i >= $indice_debut && $i <= $indice_fin)
			{	
				$tab_seance[$i] = "normale";	
			}*/				
			if( $i >= $supplement_apres_debut && $i <= $supplement_apres_fin)		
			{	
				$tab_seance[$i] = "etendu";	
			}	
			if( $i >= $supplement_avant_debut && $i <= $supplement_avant_fin)	
			{	
				$tab_seance[$i] = "etendu";	
			}				
		}
		
		//echo '<pre>';
		//print_r($tab_seance);	
		
		return($tab_seance);
}
?>	