<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.7

Copyright (C) 2006-2007  Sylvain FATOME
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr

Web : http://smitst.homelinux.com
****************************************************************************************/

session_start(); 
include_once("../inc/config.inc.php");
include_once("../inc/func_log.inc.php");
if (CheckURLLog(FALSE)==FALSE) header("Location: ../index.php");
if (CheckNivLog("9")==FALSE) header("Location: ../index.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_time.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/lst.inc.php");
Archivelog(@$_SESSION["code_log"]);

$pbm_maint=FALSE;

// Variables Formulaire
$type = $_GET["td"];

$tr=-1;
if (!isset($_GET["tr"]))
	$pbm_maint=TRUE;
else
{
	if (!is_numeric($_GET["tr"]))
		$pbm_maint=TRUE;
	else
		$tr = intval($_GET["tr"]);
}

$rep = FALSE;
if (isset($_GET["rep"]))
{
	switch ($_GET["rep"])
	{
		case "Y" :
			$rep=TRUE;
			break;
		case "N" :
			$rep=FALSE;
			break;
		default :
			$pbm_maint=TRUE;
			break;
	}
}

switch ($type)
{
	case "cli" :
		$titre = "MAINTENANCE CLIENTS";
		if (($tr<0) OR ($tr>2))
			$pbm_maint=TRUE;
		break;
	case "conc" :
		$titre = "MAINTENANCE CONTRATS CLIENTS";
		if (($tr<0) OR ($tr>4))
			$pbm_maint=TRUE;
		break;
	case "int" :
		$titre = "MAINTENANCE INTERVENTIONS";
		if (($tr<0) OR ($tr>1))
			$pbm_maint=TRUE;
		break;
	default :
		$pbm_maint=TRUE;
		break;
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - ESPACE PRIVE : Maintenance</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../css/smi.css" rel="stylesheet" type="text/css">

<!-- Feuille de style & Script pour le menu -->

<script language="JavaScript" src="../jscript/coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour fenêtres -->
<script language="JavaScript" src="../jscript/fen.js"></script>

<!-- Scripts Javascript divers -->
<script language="JavaScript" src="../jscript/divers.js"></script>

</head>
<body onLoad="metFocus();">

<?php 
include_once("../inc/entete.inc.php"); 

if ($pbm_maint==FALSE)
{
?>
	
	<p align="center"><span class="titrepage"><?PHP echo $titre; ?></span></p>
	<?PHP
	echo "<p align=\"center\">";
	echo "<span class=\"annot_std_it\">";
	if ($rep==TRUE)
		echo "Mode R&eacute;paration";
	else
		echo "Mode analyse";
	echo "</span></p>";
	$errglob=FALSE;
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	switch ($type)
	{
		case "cli" :
			$rech = "SELECT * FROM `" .TBCLI. "` ORDER BY `cli_code`";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
				// *** Vérification types de clients
				if (($tr==0) OR ($tr==1))
				{
					$rech = "SELECT * FROM `" .TBCLI. "` ORDER BY `cli_code`";
					$rs = mysql_query($rech) or die(mysql_error());
	
					echo "<span class=\"annot_gras_it\">";
					echo "V&eacute;rification des types de clients";
					echo "</span>";
					echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";     
	
					// Création entête
					echo "<th>";
					echo "Code";
					echo "</th>";
					echo "<th>";
					echo "Type";
					echo "</th>";
					echo "<th>";
					echo "Interventions ?";
					echo "</th>";
					echo "<th>";
					echo "Interventions closes ?";
					echo "</th>";
					echo "<th>";
					echo "Coh&eacute;rent ?";
					echo "</th>";
					if ($rep==TRUE)
					{
						echo "<th>";
						echo "R&eacute;par&eacute; ?";
						echo "</th>";
					}
	
					$i = 0;
	
					while ($row = mysql_fetch_assoc($rs)) 
					{         
						// Création de l'entête de ligne (TR)
						creetrlst($i);
				
						echo "<td align=\"center\">".$row["cli_code"]."</td>\n";
						echo "<td align=\"center\">";
						echo $typ_client[$row["cli_type"]];
						echo "</td>\n";
						$typcli=-1;
						$cliint=FALSE;
						$err=FALSE;
						// Déjà utilisé dans une intervention ?
						echo "<td align=\"center\">";
						$rech2 = "SELECT * FROM `" .TBINT. "` WHERE `int_codecli`='".$row["cli_code"]."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if (mysql_num_rows($rs2) <> 0)
						{
							$cliint=TRUE;
							echo "Oui";
							if ($row["cli_type"]==0)
							{
								$err=TRUE;
								$typcli=1;
							}
						}
						else
						{
							echo "Non";
							if ($row["cli_type"]>0)
							{
								$err=TRUE;
								$typcli=0;
							}
						}
						echo "</td>\n";
						// Déjà utilisé dans une intervention close ?
						echo "<td align=\"center\">";
						$rech2 = "SELECT * FROM `" .TBINT. "` WHERE `int_codecli`='".$row["cli_code"]."' AND `int_datefin`<>'0000-00-00'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if (mysql_num_rows($rs2) <> 0)
						{
							echo "Oui";
							if ($row["cli_type"]<=1)
							{
								$err=TRUE;
								$typcli=2;
							}
						}
						else
						{
							echo "Non";
							if ($row["cli_type"]>1)
							{
								$err=TRUE;
								if ($cliint==TRUE)
									$typcli=1;
								else
									$typcli=0;
							}
						}
						echo "</td>\n";
	
	
						echo "<td align=\"center\">";
						if ($err==TRUE)
						{
							$errglob=TRUE;
							echo "<span class=\"annot_gras_it\">";
							echo "pas ok";
							echo "</span>";
						}
						else	
							echo "ok";
						echo "</td>\n";

						if ($rep==TRUE)
						{
							echo "<td align=\"center\">";
							// On ne met à jour que si erreur
							if ($err==TRUE)
							{
								$rech3 = "UPDATE `" .TBCLI. "` SET";
								$rech3.=" `cli_type`='".$typcli."'";
								$rech3.=" WHERE `cli_code` = '".$row["cli_code"]."'";
								$rs3 = mysql_query($rech3) or die(mysql_error());
								echo "Oui";
								echo "<span class=\"annot_petit_it\">";
								echo "<br>Pass&eacute; en '".$typ_client[$typcli]."'";
								echo "</span>";
							}
							else
								echo "Non";
							echo "</td>\n";
						}
						
						$i++;
					}
					
					echo "</table>";
					echo "<br>";
				}	
					
				// *** Vérification des répertoires clients
				if (($tr==0) OR ($tr==2))
				{
					// Vérification du répertoire principal pour les clients
					$rep_clients = $url_gfc."/clients";
					$desc_rep_clients = "Répertoire spécifique aux clients";
					$detail_rep_clients = "Répertoire permettant le stockage des fichiers pour les clients.";
					if (!is_dir($rep_clients)) // Si le sous-répertoire spécifique aux clients n'existe pas, on le crée
					{
						if (mkdir($rep_clients))
						{
							$rech = "INSERT INTO " .TBFIC;
							$rech .= " (fic_nom,fic_type,fic_desc,fic_detail,fic_datecrea,fic_codecrea,fic_nbtel) VALUES";
							$rech .= " ('$rep_clients','0','$desc_rep_clients','$detail_rep_clients','$datea','$admin_code','')";
							$rs = mysql_query($rech) or die(mysql_error());
						}
					}

					$rech = "SELECT * FROM `" .TBCLI. "` ORDER BY `cli_code`";
					$rs = mysql_query($rech) or die(mysql_error());
	
					echo "<span class=\"annot_gras_it\">";
					echo "V&eacute;rification des r&eacute;pertoires priv&eacute;s clients";
					echo "</span>";
					echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";     
	
					// Création entête
					echo "<th>";
					echo "Code";
					echo "</th>";
					echo "<th>";
					echo "R&eacute;pertoire physique ?";
					echo "</th>";
					echo "<th>";
					echo "R&eacute;pertoire MySQL ?";
					echo "</th>";
					echo "<th>";
					echo "Coh&eacute;rent ?";
					echo "</th>";
					if ($rep==TRUE)
					{
						echo "<th>";
						echo "R&eacute;par&eacute; ?";
						echo "</th>";
					}
	
					$i = 0;
	
					while ($row = mysql_fetch_assoc($rs)) 
					{         
						// Création de l'entête de ligne (TR)
						creetrlst($i);
				
						echo "<td align=\"center\">".$row["cli_code"]."</td>\n";
						$err=FALSE;
						$repphys=TRUE;
						$repsql=TRUE;
						$rep_ce_client = $rep_clients."/".strtolower($row["cli_code"]);

						// Répertoire physique ?
						echo "<td align=\"center\">";
						if (is_dir($rep_ce_client))
							echo "Oui";
						else
						{
							echo "Non";
							$err=TRUE;
							$repphys=FALSE;
						}
						echo "</td>\n";

						// Répertoire MySQL ?
						echo "<td align=\"center\">";
						$rech2 = "SELECT * FROM `" .TBFIC. "` WHERE `fic_nom`='".$rep_ce_client."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if (mysql_num_rows($rs2) <> 0)
							echo "Oui";
						else
						{
							echo "Non";
							$err=TRUE;
							$repsql=FALSE;
						}
						echo "</td>\n";
	
						echo "<td align=\"center\">";
						if ($err==TRUE)
						{
							$errglob=TRUE;
							echo "<span class=\"annot_gras_it\">";
							echo "pas ok";
							echo "</span>";
						}
						else	
							echo "ok";
						echo "</td>\n";

						if ($rep==TRUE)
						{
							echo "<td align=\"center\">";
							// On ne met à jour que si erreur
							if ($err==TRUE)
							{
								$msg="";
								if ($row["cli_ste"] <> "")
								{
									$desc_rep_ce_client = "Répertoire client ".$row["cli_ste"]." (".$row["cli_code"].")";
									$detail_rep_ce_client = "Répertoire permettant le stockage des fichiers pour le client ".$row["cli_ste"]." (".$row["cli_code"].")";
								}
								else
								{
									$desc_rep_ce_client = "Répertoire client ".$row["cli_nom"]." ".$row["cli_prenom"]." (".$row["cli_code"].")";
									$detail_rep_ce_client = "Répertoire permettant le stockage des fichiers pour le client ".$row["cli_nom"]." ".$row["cli_prenom"]." (".$row["cli_code"].")";
								}

								if ($repphys==FALSE)
								{								
									if (mkdir($rep_ce_client)) // On crée sous-répertoire spécifique à ce client
										$msg = "R&eacute;pertoire physique cr&eacute;&eacute;.";
									else
										$msg = "Probl&egrave;me cr&eacute;ation r&eacute;pertoire physique";
								}
								
								if ($repsql==FALSE)
								{								
									// On ajoute le détail dans le fichier MySQL
									$rech = "INSERT INTO " .TBFIC;
									$rech .= " (fic_nom,fic_type,fic_desc,fic_detail,fic_datecrea,fic_codecrea,fic_nbtel) VALUES";
									$rech .= " ('$rep_ce_client','0','$desc_rep_ce_client','$detail_rep_ce_client','$datea','$admin_code','')";
									$rs = mysql_query($rech) or die(mysql_error());
									if ($msg<>"")
										$msg.="<br>R&eacute;pertoire MySQL cr&eacute;&eacute;";
									else
										$msg.="R&eacute;pertoire MySQL cr&eacute;&eacute;";
								}			
								echo "Oui";
								echo "<span class=\"annot_petit_it\">";
								echo "<br>".$msg;
								echo "</span>";
							}
							else
								echo "Non";
							echo "</td>\n";
						}
						
						$i++;
					}
					
					echo "</table>";
					echo "<br>";
				}	
			}
			else
				echo "<br><span class=\"annot_std_gras\">Aucun client cr&eacute;&eacute;.</span><br>";
			break;

		// Contrats clients
		case "conc" :
			$rech = "SELECT * FROM `" .TBCOCL. "` ORDER BY `cocl_code`";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
				// *** Vérification signatures & réceptions
				if (($tr==0) OR ($tr==1))
				{
					$rech = "SELECT * FROM `" .TBCOCL. "` ORDER BY `cocl_code`";
					$rs = mysql_query($rech) or die(mysql_error());
	
					echo "<span class=\"annot_gras_it\">";
					echo "V&eacute;rification des signatures &amp; r&eacute;ceptions de contrats";
					echo "</span>";
					echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";     
	
					// Création entête
					echo "<th>";
					echo "Code";
					echo "</th>";
					echo "<th>";
					echo "Type";
					echo "</th>";
					echo "<th>";
					echo "Sign&eacute; le";
					echo "</th>";
					echo "<th>";
					echo "Re&ccedil;u le";
					echo "</th>";
					echo "<th>";
					echo "Cl&ocirc;t le";
					echo "</th>";
					echo "<th>";
					echo "Coh&eacute;rent ?";
					echo "</th>";
					if ($rep==TRUE)
					{
						echo "<th>";
						echo "R&eacute;par&eacute; ?";
						echo "</th>";
					}
	
					$i = 0;
	
					while ($row = mysql_fetch_assoc($rs)) 
					{         
						// Création de l'entête de ligne (TR)
						creetrlst($i);
				
						echo "<td align=\"center\">".$row["cocl_code"]."</td>\n";
						echo "<td align=\"center\">";
						if ($row["cocl_typh"]==0)
							echo "Temps/Intervention";
						else
							echo "Quota Horaire";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if (formatedate($row["cocl_datesig"])<>"")
							echo formatedate($row["cocl_datesig"]);
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if (formatedate($row["cocl_daterec"])<>"")
							echo formatedate($row["cocl_daterec"]);
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if (formatedate($row["cocl_datefin"])<>"")
							echo formatedate($row["cocl_datefin"]);
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						$datesig="";
						$daterec="";
						$signepar="";
						// Déjà utilisé dans une intervention ?
						$rech2 = "SELECT MIN(int_datedde) AS mindate FROM `" .TBINT. "` WHERE `int_codecocl`='".$row["cocl_code"]."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if (mysql_num_rows($rs2) <> 0)
						{
							if ($row2 = mysql_fetch_assoc($rs2)) 
							{
							}
						}
	
						$err=FALSE;
	
						// Il y a une date de signature ?
						// Non
						if (formatedate($row["cocl_datesig"])=="")
						{
							// Déjà reçu ?
							// Oui : pas ok
							if (formatedate($row["cocl_daterec"])<>"")
							{
								$err=TRUE;
								// MAJ possible uniquement si déjà utilisé
								if (formatedate($row2["mindate"])<>"")
								{
									$datesig=$row2["mindate"]; // Au moins la 1ère date d'intervention
									// La date de signature ne doit pas dépasser celle de réception
									if (echeance_depassee_date($datesig,$row["cocl_daterec"])==TRUE)
										$daterec=$datesig;
								}
							}
	
							// Déjà fini ?
							// Oui : pas ok
							if (formatedate($row["cocl_datefin"])<>"")
							{
								$err=TRUE;
								// Déjà reçu ?
								// Non : pas ok
								if (formatedate($row["cocl_daterec"])=="")
								{
									if (formatedate($row2["mindate"])<>"")
									{
										$datesig=$row2["mindate"]; // Au moins la 1ère date d'intervention
										$daterec=$row2["mindate"]; // Au moins la 1ère date d'intervention
									}
								}
								else
									$datesig=$row["cocl_daterec"]; // Au moins la date de réception
							}
						}
						else // Oui
						{
							// Déjà reçu ?
							// Non : pas ok si déjà utilisé
							if ((formatedate($row["cocl_daterec"])=="") AND (formatedate($row2["mindate"])<>""))
							{
								$err=TRUE;
								$daterec=$row2["mindate"]; // Au moins la 1ère date d'intervention
							}
						}
	
						// Si date signature modifiée ou date signature existante, on vérifie signataire
						if (($datesig<>"") OR (formatedate($row["cocl_datesig"])<>""))
						{
							// Signé par ?
							if (trim($row["cocl_signe"])=="") // Si pas signé, on prend au moins le contact principal
							{
								$err=TRUE;
								$rech3 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code`='".$row["cocl_codecli"]."'";
								$rs3 = mysql_query($rech3) or die(mysql_error());
								if (mysql_num_rows($rs3) <> 0)
								{
									if ($row3 = mysql_fetch_assoc($rs3)) 
									{
										$signepar = $row3["cli_civilite"]." ".$row3["cli_prenom"]." ".$row3["cli_nom"];
									}
								}
							}
						}
						if ($err==TRUE)
						{
							$errglob=TRUE;
							echo "<span class=\"annot_gras_it\">";
							echo "pas ok";
							echo "</span>";
						}
						else	
							echo "ok";
						echo "</td>\n";
						if ($rep==TRUE)
						{
							echo "<td align=\"center\">";
							// On ne met à jour que si erreur
							if ($err==TRUE)
							{
								$rech3 = "UPDATE `" .TBCOCL. "` SET";
								if ($datesig<>"")
									$rech3.=" `cocl_datesig`='".$datesig."'";
								if ($daterec<>"")
									$rech3.=" `cocl_daterec`='".$daterec."'";
								if ($signepar<>"")
									$rech3.=", `cocl_signe`='".$signepar."'";
								$rech3.=" WHERE `cocl_code` = '".$row["cocl_code"]."'";
								$rs3 = mysql_query($rech3) or die(mysql_error());
								echo "Oui";
								echo "<span class=\"annot_petit_it\">";
								if ($datesig<>"")
									echo "<br>Sign&eacute; au ".formatedate($datesig);
								if ($daterec<>"")
									echo "<br>Re&ccedil;u le ".formatedate($daterec);
								if ($signepar<>"")
									echo "<br>Sign&eacute; par ".$signepar;
								echo "</span>";
							}
							else
								echo "Non";
							echo "</td>\n";
						}
						
						$i++;
					}
					
					echo "</table>";
					echo "<br>";
				}	

				// *** Vérification sites couverts
				if (($tr==0) OR ($tr==2))
				{
					$rech = "SELECT * FROM `" .TBCOCL. "` ORDER BY `cocl_code`";
					$rs = mysql_query($rech) or die(mysql_error());
	
					echo "<span class=\"annot_gras_it\">";
					echo "V&eacute;rification des sites couverts";
					echo "</span>";
					echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";     
	
					// Création entête
					echo "<th>";
					echo "Code";
					echo "</th>";
					echo "<th>";
					echo "Type";
					echo "</th>";
					echo "<th>";
					echo "Site principal";
					echo "</th>";
					echo "<th>";
					echo "Sites secondaires";
					echo "</th>";
					echo "<th>";
					echo "Coh&eacute;rent ?";
					echo "</th>";
					if ($rep==TRUE)
					{
						echo "<th>";
						echo "R&eacute;par&eacute; ?";
						echo "</th>";
					}
	
					$i = 0;
	
					while ($row = mysql_fetch_assoc($rs)) 
					{         
						// Création de l'entête de ligne (TR)
						creetrlst($i);
				
						echo "<td align=\"center\">".$row["cocl_code"]."</td>\n";
						echo "<td align=\"center\">";
						if ($row["cocl_typh"]==0)
							echo "Temps/Intervention";
						else
							echo "Quota Horaire";
						echo "</td>\n";
						echo "<td align=\"center\">";
						$sitep=TRUE;
						if ($row["cocl_sitep"]==1)
							echo "Oui";
						else
						{
							echo "-";
							$sitep=FALSE;
						}
						echo "</td>\n";
						echo "<td align=\"center\">";
						$sites=FALSE;
						// Sites secondaires
						$rech2 = "SELECT * FROM `" .TBSCLIC. "` WHERE `sclic_codecocl`='".$row["cocl_code"]."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if (mysql_num_rows($rs2) > 0)
						{
							$sites=TRUE;
							echo "Oui";
						}
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						$err=FALSE;
						if (($sitep==FALSE) AND ($sites==FALSE))
							$err=TRUE;
						if ($err==TRUE)
						{
							$errglob=TRUE;
							echo "<span class=\"annot_gras_it\">";
							echo "pas ok";
							echo "</span>";
						}
						else	
							echo "ok";
						echo "</td>\n";
						if ($rep==TRUE)
						{
							echo "<td align=\"center\">";
							// "Pas ok"...on répare
							if ($err==TRUE)
							{
								$rech3 = "UPDATE `" .TBCOCL. "` SET";
								$rech3.=" `cocl_sitep`='1'";
								$rech3.=" WHERE `cocl_code` = '".$row["cocl_code"]."'";
								$rs3 = mysql_query($rech3) or die(mysql_error());
								echo "Oui";
								echo "<span class=\"annot_petit_it\">";
								echo "<br>Site principal rajout&eacute;";
								echo "</span>";
							}
							else
								echo "Non";
							echo "</td>\n";
						}
						
						$i++;
					}
					
					echo "</table>";
					echo "<br>";
				}
				
				// *** Vérification cohérence cloture
				if (($tr==0) OR ($tr==3))
				{
					$rech = "SELECT * FROM `" .TBCOCL. "` ORDER BY `cocl_code`";
					$rs = mysql_query($rech) or die(mysql_error());
	
					echo "<span class=\"annot_gras_it\">";
					echo "V&eacute;rification des contrats cl&ocirc;ts";
					echo "</span>";
					echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";     
	
					// Création entête
					echo "<th>";
					echo "Code";
					echo "</th>";
					echo "<th>";
					echo "Type";
					echo "</th>";
					echo "<th>";
					echo "Fin le";
					echo "</th>";
					echo "<th>";
					echo "Temps initial";
					echo "</th>";
					echo "<th>";
					echo "Solde temps";
					echo "</th>";
					echo "<th>";
					echo "Cl&ocirc;t le";
					echo "</th>";
					echo "<th>";
					echo "Coh&eacute;rent ?";
					echo "</th>";
					if ($rep==TRUE)
					{
						echo "<th>";
						echo "R&eacute;par&eacute; ?";
						echo "</th>";
					}
	
					$i = 0;
	
					while ($row = mysql_fetch_assoc($rs)) 
					{         
						// Création de l'entête de ligne (TR)
						creetrlst($i);
				
						echo "<td align=\"center\">".$row["cocl_code"]."</td>\n";
						echo "<td align=\"center\">";
						if ($row["cocl_typh"]==0)
							echo "Temps/Intervention";
						else
							echo "Quota Horaire";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if (formatedate($row["cocl_datef"])<>"")
							echo formatedate($row["cocl_datef"]);
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if ($row["cocl_nbs"]<>0)
							echo sec_heure($row["cocl_nbs"]);
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if ($row["cocl_soldes"]<>0)
							echo sec_heure($row["cocl_soldes"]);
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if (formatedate($row["cocl_datefin"])<>"")
							echo formatedate($row["cocl_datefin"]);
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						$err=FALSE;
						$datefin="";
						$soldes=-1;
						// Déjà utilisé dans une intervention ?
						$rech2 = "SELECT MAX(int_datefin) AS maxdate FROM `" .TBINT. "` WHERE `int_codecocl`='".$row["cocl_code"]."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if (mysql_num_rows($rs2) <> 0)
						{
							if ($row2 = mysql_fetch_assoc($rs2)) 
							{
							}
						}
	
						// Contrat à temps par intervention
						if ($row["cocl_typh"]==0)
						{
							$soldes=0;
							// Il y a une date de fin de contrat ?
							// Oui
							if (formatedate($row["cocl_datef"])<>"")
							{
								// Cette date est dépassée
								if (echeance_depassee_date($datea,$row["cocl_datef"])==TRUE)
								{
									// Le contrat n'est pas clôt
									if (formatedate($row["cocl_datefin"])=="")
									{
										$err=TRUE;
										$datefin=$row["cocl_datef"]; // On prend la date de fin du contrat
									}
								}
								else // Elle n'est pas dépassée
								{
									if (formatedate($row["cocl_datefin"])<>"")
									{
										$err=TRUE;
										$datefin="0000-00-00"; // On ne doit pas avoir le contrat clôt => suppression date de fin
									}
								}
							}
							else // Non
							{
								if (formatedate($row["cocl_datefin"])<>"")
								{
									$err=TRUE;
									$datefin="0000-00-00";  // On ne doit pas avoir le contrat clôt => suppression date de fin
								}
							}
						}
						else // Contrat à quota de temps
						{
							// Le solde horaire est nul
							if ($row["cocl_soldes"]==0)
							{
								// Il n'y a pas de date de fin : pas ok
								if (formatedate($row["cocl_datefin"])=="")
								{
									$err=TRUE;
									$datefin=$row2["maxdate"]; // On prend la dernière date d'intervention
								}
							}
							else // Il n'est pas nul
							{
								// Il y a une date de fin de contrat
								if (formatedate($row["cocl_datef"])<>"")
								{
									// L'échéance du contrat est dépassée ?
									if (echeance_depassee_date($datea,$row["cocl_datef"])==TRUE)
									{
										// Le contrat n'est pas clôt
										if (formatedate($row["cocl_datefin"])=="")
										{
											$err=TRUE;
											$datefin=$row["cocl_datef"]; // On prend la date de fin du contrat
											$soldes=0; // Il n'y a plus de temps disponible
										}
									}
									else // L'échéance de fin de contrat n'est pas dépassée
									{
										// Il y a une date de fin : pas ok
										if (formatedate($row["cocl_datefin"])<>"")
										{
											$err=TRUE;
											$datefin="0000-00-00";  // On ne doit pas avoir le contrat clôt => suppression date de fin
										}
									}
								}
								else // Il n'y a pas de date de fin de contrat
								{
									if (formatedate($row["cocl_datefin"])<>"")
									{
										$err=TRUE;
										$datefin="0000-00-00";  // On ne doit pas avoir le contrat clôt => suppression date de fin
									}
								}
							}
						}					 
						if ($err==TRUE)
						{
							$errglob=TRUE;
							echo "<span class=\"annot_gras_it\">";
							echo "pas ok";
							echo "</span>";
						}
						else	
							echo "ok";
						echo "</td>\n";
						if ($rep==TRUE)
						{
							echo "<td align=\"center\">";
							// "Pas ok"...on répare
							if ($err==TRUE)
							{
								$rech3 = "UPDATE `" .TBCOCL. "` SET";
								if ($datefin<>"")
									$rech3.=" `cocl_datefin`='".$datefin."'";
								if ($soldes>=0)
									$rech3.=", `cocl_soldes`='".$soldes."'";
								$rech3.=" WHERE `cocl_code` = '".$row["cocl_code"]."'";
								$rs3 = mysql_query($rech3) or die(mysql_error());
								echo "Oui";
								echo "<span class=\"annot_petit_it\">";
								if ($datefin<>"")
								{
									if (formatedate($datefin)<>"")
										echo "<br>Cl&ocirc;tur&eacute; au ".formatedate($datefin);
									else
										echo "<br>Contrat r&eacute;ouvert";
								}
								if ($soldes>=0)
									echo "<br>Solde ramen&eacute; &agrave; ".$soldes." secondes";
								echo "</span>";
							}
							else
								echo "Non";
							echo "</td>\n";
						}
						
						$i++;
					}
					
					echo "</table>";
					echo "<br>";
				}
			
				// *** Vérification quotas horaires
				if (($tr==0) OR ($tr==4))
				{
					$rech = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_typh`='1' ORDER BY `cocl_code`";
					$rs = mysql_query($rech) or die(mysql_error());
	
					echo "<span class=\"annot_gras_it\">";
					echo "V&eacute;rification des contrats &agrave; quotas horaires";
					echo "</span>";
					echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";     
	
					// Création entête
					echo "<th>";
					echo "Code";
					echo "</th>";
					echo "<th>";
					echo "Fin le";
					echo "</th>";
					echo "<th>";
					echo "Temps initial";
					echo "</th>";
					echo "<th>";
					echo "Solde";
					echo "</th>";
					echo "<th>";
					echo "Cl&ocirc;t le";
					echo "</th>";
					echo "<th>";
					echo "Temps effectifs travaill&eacute;s calcul&eacute;s";
					echo "</th>";
					echo "<th>";
					echo "Coh&eacute;rent ?";
					echo "</th>";
					if ($rep==TRUE)
					{
						echo "<th>";
						echo "R&eacute;par&eacute; ?";
						echo "</th>";
					}
	
					$i = 0;
	
					while ($row = mysql_fetch_assoc($rs)) 
					{         
						// Création de l'entête de ligne (TR)
						creetrlst($i);
				
						echo "<td align=\"center\">".$row["cocl_code"]."</td>\n";
						echo "<td align=\"center\">";
						if (formatedate($row["cocl_datef"])<>"")
							echo formatedate($row["cocl_datef"]);
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if ($row["cocl_nbs"]<>0)
							echo sec_heure($row["cocl_nbs"]);
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if ($row["cocl_soldes"]<>0)
							echo sec_heure($row["cocl_soldes"]);
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if (formatedate($row["cocl_datefin"])<>"")
							echo formatedate($row["cocl_datefin"]);
						else
							echo "-";
						echo "</td>\n";
						echo "<td align=\"center\">";
						$err=FALSE;
						$datefin="";
						$soldes=-1;
	
						$nbtotalsec = tps_trav_clot($row["cocl_code"],FALSE);

						// Déjà utilisé dans une intervention ?
						$rech3 = "SELECT MAX(int_datefin) AS maxdate FROM `" .TBINT. "` WHERE `int_codecocl`='".$row["cocl_code"]."'";
						$rs3 = mysql_query($rech3) or die(mysql_error());
						if (mysql_num_rows($rs3) <> 0)
						{
							if ($row3 = mysql_fetch_assoc($rs3)) 
							{
							}
						}
	
						// Si le total  initial de secondes est dépassé : pas ok
						if ($row["cocl_nbs"]<$nbtotalsec)
						{
							if (formatedate($row["cocl_datefin"])=="")
							{
								$err=TRUE;
								$soldes=0; // Il n'y a plus de temps disponible
								$datefin=$row3["maxdate"]; // On prend la dernière date d'intervention pour clore le contrat
							}
						}
						else // Le total initial n'est pas dépassé
						{
							// Le solde horaire est nul : pas ok uniquement si temps initial et temps travaillé différents
							if ($row["cocl_soldes"]==0)
							{
								if ($row["cocl_nbs"]<>$nbtotalsec)
								{
									$err=TRUE;
									$soldes=$row["cocl_nbs"]-$nbtotalsec; // On recalcule le solde
									// Date de fin de contrat en fonction du solde
									if ($soldes<>0)
										$datefin="0000-00-00";  // On ne doit pas avoir le contrat clôt => suppression date de fin
									else
										$datefin=$row3["maxdate"]; // On prend la dernière date d'intervention pour clore le contrat
								}
							}
							else // Il n'est pas nul
							{
								// Si ce solde est mal calculé, on le remet à jour
								if ($row["cocl_soldes"]<>($row["cocl_nbs"]-$nbtotalsec))
								{
									$err=TRUE;
									$soldes=$row["cocl_nbs"]-$nbtotalsec; // On recalcule le solde
									// Date de fin de contrat en fonction du solde
									if ($soldes<>0)
										$datefin="0000-00-00";  // On ne doit pas avoir le contrat clôt => suppression date de fin
									else
										$datefin=$row3["maxdate"]; // On prend la dernière date d'intervention pour clore le contrat
								}
							}
						}
						echo sec_heure($nbtotalsec);
						echo "</td>\n";
						echo "<td align=\"center\">";
						if ($err==TRUE)
						{
							$errglob=TRUE;
							echo "<span class=\"annot_gras_it\">";
							echo "pas ok";
							echo "</span>";
						}
						else	
							echo "ok";
						echo "</td>\n";
						if ($rep==TRUE)
						{
							echo "<td align=\"center\">";
							// "Pas ok"...on répare
							if ($err==TRUE)
							{
								$rech3 = "UPDATE `" .TBCOCL. "` SET";
								if ($datefin<>"")
									$rech3.=" `cocl_datefin`='".$datefin."'";
								if ($soldes>=0)
									$rech3.=", `cocl_soldes`='".$soldes."'";
								$rech3.=" WHERE `cocl_code` = '".$row["cocl_code"]."'";
								$rs3 = mysql_query($rech3) or die(mysql_error());
								echo "Oui";
								echo "<span class=\"annot_petit_it\">";
								if ($datefin<>"")
								{
									if (formatedate($datefin)<>"")
										echo "<br>Cl&ocirc;tur&eacute; au ".formatedate($datefin);
									else
										echo "<br>Contrat r&eacute;ouvert";
								}
								if ($soldes>=0)
									echo "<br>Solde ramen&eacute; &agrave; ".$soldes." secondes";
								echo "</span>";
							}
							else
								echo "Non";
							echo "</td>\n";
						}
						
						$i++;
					}

					echo "</table>";
					echo "<br>";
				}
					
			}
			else
				echo "<br><span class=\"annot_std_gras\">Aucun contrat cr&eacute;&eacute;.</span><br>";
		break;

		case "int" :
			$rech = "SELECT * FROM `" .TBITN. "` ORDER BY `int_code`";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
			}
			else
				echo "<br><span class=\"annot_std_gras\">Aucune intervention.</span><br>";
			break;
	}
	mysql_free_result($rs);
	mysql_close($conn);
	if (($rep==FALSE) AND ($errglob==TRUE))
	{
	?>
	<div align="center">
	<input name="repar" type="button" class="bton_fmarr_std" id="repar" value="R&eacute;parer" onClick="javascript:; window.self.location.href=('maint_data.php?td=<?PHP echo $type; ?>&tr=<?PHP echo $tr; ?>&rep=Y');">
	</div>
<?PHP
	}
}
else
{
?>
	<script type="text/javascript">
	setTimeout("self.location.href=('index.php');",2000)
	</script>
	<p align="center" class="annot_std_it">Probl&ecirc;me dans les param&egrave;tres...</p>
<?php 
}
include_once("pied.php")
?>
