<?php
require 'database.inc.php';
//session_start();
//session_register("lg_user");
//session_register("lg_password");
//session_register("lg_admin");
//session_register("lg_menu1");
//session_register("lg_menu2");
//session_register("lg_menu3");
//session_register("lg_menu4");
//session_register("lg_menu5");
//session_register("lg_menu6");
if ((!empty($User)) && (!empty($Passwd))) {
	$lg_user = $User;
	$lg_password = $Passwd;
}
if (Test_User($lg_user, $lg_password) == TRUE) {
	$err = 0;
	$annee = date("Y");
	if (!isset($new)) {
		$date_debut = "$Debut_annee-" . Chiffre_2_Car($Debut_mois) . "-" . Chiffre_2_Car($Debut_jour);
		$date_fin = "$Fin_annee-" . Chiffre_2_Car($Fin_mois) . "-" . Chiffre_2_Car($Fin_jour);
		$requette = "SELECT * FROM absenceemploye" . ID_GARAGE . " WHERE NoClient=$NoClient AND ((Debut>='$date_debut' AND Debut<='$date_fin')OR(Fin>='$date_debut' AND Fin<='$date_fin')OR(Debut<='$date_debut' AND Fin>='$date_fin')) AND ( NOT (Debut='" . substr($Start, 0, 10) . "' AND DebutMA=" . substr($Start, 10, 1) . ")) ORDER BY Debut, DebutMA";
		if ($resultat =  Connection::executeRawQueryWithfetchAll($requette)) {
			$Dr = $Debut_annee . Chiffre_2_Car($Debut_mois) . Chiffre_2_Car($Debut_jour) . $Debut_periode;
			$Fr = $Fin_annee . Chiffre_2_Car($Fin_mois) . Chiffre_2_Car($Fin_jour) . $Fin_periode;
			$Dri = $Dr * 1;
			$Fri = $Fr * 1;
			$err = 0;
			foreach ($resultat as $ligne) {
				$D = substr($ligne['Debut'], 0, 4) . substr($ligne['Debut'], 5, 2) . substr($ligne['Debut'], 8, 2) . $ligne['DebutMA'];
				$F = substr($ligne['Fin'], 0, 4) . substr($ligne['Fin'], 5, 2) . substr($ligne['Fin'], 8, 2) . $ligne['FinMA'];
				$Di = $D * 1;
				$Fi = $F * 1;
				if (($Di >= $Dri && $Di <= $Fri) || ($Fi >= $Dri && $Fi <= $Fri) || ($Di <= $Dri && $Fi >= $Fri)) {
					echo "deja une donnée dans cet intervalle<BR>";
					$err++;
				}
			}
		} else {
			echo "KO $requette<BR>";
		}
		if ($err == 0) {
			$requette = "UPDATE absenceemploye" . ID_GARAGE . " SET Debut='$date_debut', DebutMA=$Debut_periode , Fin='$date_fin', FinMA=$Fin_periode, Type=$Type, Motif='" . addslashes($Motif) . "' WHERE NoClient=$NoClient AND Debut='" . substr($Start, 0, 10) . "' AND DebutMA=" . substr($Start, 10, 1);
			if ($resultat = Connection::executeRawQuery($requette)) {
				echo "<script language=JavaScript>\n";
				echo "window.opener.location.reload();\n";
				echo "window.close();\n";
				echo "</script>\n";
			} else {
				echo $requette;
				echo "<BR>KO";
			}
		}
	} else {
		$date_debut = "$Debut_annee-" . Chiffre_2_Car($Debut_mois) . "-" . Chiffre_2_Car($Debut_jour);
		$date_fin = "$Fin_annee-" . Chiffre_2_Car($Fin_mois) . "-" . Chiffre_2_Car($Fin_jour);
		$requette = "SELECT * FROM absenceemploye" . ID_GARAGE . " WHERE NoClient=$NoClient AND ((Debut>='$date_debut' AND Debut<='$date_fin')OR(Fin>='$date_debut' AND Fin<='$date_fin')OR(Debut<='$date_debut' AND Fin>='$date_fin')) ORDER BY Debut, DebutMA";
		if ($resultat = AbsenceEmploye::getAbsenceByNoClientAndPeriod($NoClient, $date_debut, $date_fin)) {
			$Dr = $Debut_annee . Chiffre_2_Car($Debut_mois) . Chiffre_2_Car($Debut_jour) . $Debut_periode;
			$Fr = $Fin_annee . Chiffre_2_Car($Fin_mois) . Chiffre_2_Car($Fin_jour) . $Fin_periode;
			$Dri = $Dr * 1;
			$Fri = $Fr * 1;
			$err = 0;
			foreach ($resultat as $ligne) {
				$D = substr($ligne['Debut'], 0, 4) . substr($ligne['Debut'], 5, 2) . substr($ligne['Debut'], 8, 2) . $ligne['DebutMA'];
				$F = substr($ligne['Fin'], 0, 4) . substr($ligne['Fin'], 5, 2) . substr($ligne['Fin'], 8, 2) . $ligne['FinMA'];
				$Di = $D * 1;
				$Fi = $F * 1;
				if (($Di >= $Dri && $Di <= $Fri) || ($Fi >= $Dri && $Fi <= $Fri) || ($Di <= $Dri && $Fi >= $Fri)) {
					echo "deja une donnée dans cet intervalle<BR>";
					$err++;
				}
			}
		} else {
			echo "KO $requette<BR>";
		}
		if ($err == 0) {
			$requette = "INSERT INTO absenceemploye" . ID_GARAGE . " (NoClient, Debut, DebutMA, Fin, FinMA, Type, Motif) values ($NoClient, '$date_debut', $Debut_periode, '$date_fin', $Fin_periode, $Type, '" . addslashes($Motif) . "')";
			if ($resultat = Connection::executeRawQuery($requette)) {
				echo "<script language=JavaScript>\n";
				echo "window.opener.location.reload();\n";
				echo "window.close();\n";
				echo "</script>\n";
			} else {
				echo $requette;
				echo "<BR>KO";
			}
		}
	}
}
