<?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
****************************************************************************************/

// Vérification des tentatives de hack
$pos=strpos($_SERVER['PHP_SELF'],"inc.php");
if ($pos===FALSE)
	$ok_inc=TRUE;
else
	$ok_inc=FALSE;
if ($ok_inc==FALSE) header("Location: ../index.php");

include_once("../inc/func.inc.php");
include_once("../inc/func_time.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/lst.inc.php");
Archivelog(@$_SESSION["code_log"]);

$orig =  strip_tags(trim($_GET['or'])); // Récupération origine de l'appel
$cc = strip_tags(trim($_GET['cc'])); // Récupération code Client
$search = strip_tags(trim($_GET['search'])); // Récupération clef de recherche

$pbm_cocl=FALSE;

if ($cc <> "")
{
	// Récupération des informations clients
	$row = lit_enr_cli($cc);
	if (is_array($row))
		extract ($row);
	else
		$pbm_cocl=TRUE;
}
else	
	$pbm_cocl=TRUE;
?>
<!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 : Historique des Contrats</title>

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

<!-- 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();">

<p align="center"><span class="titrepage">HISTORIQUE DES CONTRATS</span></p>
<?PHP
if ($pbm_cocl==FALSE)
{
?>
<p align="center"><span class="titre_gras">
<?php 
	if ($cli_ste <> "")
		echo $cli_ste;
	else
		echo $cli_civilite." ".$cli_prenom." ".$cli_nom;
	echo " (Compte : ".$cc.")";
?>
</span></p>

<!-- Formulaire de demande du numéro de contrat -->
<form name="dde_cocl" id="dde_cocl" method="GET" action="coclhd_lst.php">
	<input name="cc" type="hidden" id="cc" value="<?PHP echo $cc ?>">
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
<tr>
	<td>
	<span class="annot_std_gras">Pour le contrat :</span>
	<select name="search" size="1" id="search" onChange="dde_cocl.submit()">
		<?PHP 
		// Création de la liste des contrats
		
		if ($co <> "")
			echo "<option value=\"\">-- Choisissez un contrat --</option>";
		else
			echo "<option value=\"\" selected>-- Choisissez un contrat --</option>";
		
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rs = mysql_query("SELECT * FROM ".TBCOCL." WHERE `cocl_codecli` = '".$cc."' ORDER BY cocl_code");
		
		while ($resultat = mysql_fetch_array($rs)) 
		{
			extract($resultat);
			
			$rech2 = "SELECT * FROM " .TBCO. " WHERE co_code = '".$cocl_codeco."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_array($rs2))
			{
				$txt_cocl = $cocl_code." - ".$row2["co_nom"];
			}

			if ($cocl_code == $search)
				echo '<option value="'.$cocl_code.'" selected>'.$txt_cocl.'</option>';
			else
				echo '<option value="'.$cocl_code.'">'.$txt_cocl.'</option>';
		}
		mysql_free_result($rs);
		mysql_close($conn);
		?>
		</select>
	</td>
</tr>
</table>
</form>
<!-- Fin de formulaire de demande du numéro de contrat -->
<?PHP
if (isset($_GET['search']) && !empty($_GET['search']))  
{
	$row = lit_enr_cocl($_GET['search']);
	if (is_array($row))
	{
		extract ($row);
		$nbh = sec_heure($cocl_nbs);
		$soldeh = sec_heure($cocl_soldes);
		echo "<br>";	
		echo "<span class=\"annot_std_it\">";
		echo "Ce contrat est ";
		echo "</span>";
		echo "<span class=\"annot_gras_it\">";
		if (formatedate($cocl_datefin) <> "")
			echo "clôt";
		else
			echo "ouvert";
		if (formatedate($cocl_datesig) == "")
		{
			echo "</span>";
			echo "<span class=\"annot_std_it\">";
			echo ", et n'est ";
			echo "</span>";
			echo "<span class=\"annot_gras_it\">";
			echo "pas sign&eacute;";
		}
		else
		{
			echo "</span>";
			echo "<span class=\"annot_std_it\">";
			echo ", est ";
			echo "</span>";
			echo "<span class=\"annot_gras_it\">";
			echo "sign&eacute;";
		}
		echo "</span>";
		echo "<span class=\"annot_std_it\">";
		echo ".<br>";
		echo "</span>";
		if (($cocl_typh == 1) AND (formatedate($cocl_datefin) == ""))
	
		{
			echo "<span class=\"annot_std_it\">";
			echo "Heures utilisables sur ce contrat (hors heures d&eacute;j&agrave; utilis&eacute;es sur interventions en cours) : ";
			echo "</span>";
			echo "<span class=\"annot_gras_it\">";
			echo substr($soldeh,0,5);
			echo "</span>";
			echo "<span class=\"annot_std_it\">";
			echo ".<br>";
			echo "</span>";
		}
	
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	
		$rech = cree_strrechlstcoclh();
		$rech .= crestr_triord($coclh_titre_col,"int_code",FALSE,$limit);
		
		$rs = mysql_query($rech) or die(mysql_error());
		$numrows = mysql_num_rows($rs); // Nombre de résultats à la requête
		
		if ($numrows <> 0)
		{
	
			// *** Affichage de la table
			echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";     
		
			// Création entête
			titlst(TRUE,$coclh_titre_col);
		
			$i = 0;
			$totalgen=0;
			$totalgenoff=0;
						
			while ($row = mysql_fetch_assoc($rs)) 
			{         
			 // Affichage résumé Contrat Client
		
				// Création de l'entête de ligne (TR)
				creetrlst($i);
		
				 echo "<td align=\"center\">".$row["int_code"]."</td>\n";
				 echo "<td align=\"center\">";
				 echo formatedate($row["int_datedde"]);
				 echo "</td>\n";
				 echo "<td align=\"center\">";
				 if (formatedate($row["int_daterdv"]) <> "")
					echo formatedate($row["int_daterdv"]);
				else
					echo "&nbsp;";
				 echo "</td>\n";
				 echo "<td align=\"center\">";
				 if (formatedate($row["int_datefin"]) <> "")
					echo formatedate($row["int_datefin"]);
				else
					echo "&nbsp;";
				 echo "</td>\n";
				 echo "<td>".$row["int_codeage"]."</td>\n";
				 echo "<td align=\"center\">";
				// Récupération du total travaillé
				$nbtotalsec = 0;
				$nbtotalsecoff = 0;
			
				// Récupération total travaillé
				$rech2 = "SELECT * FROM `" .TBINTH. "` WHERE `inth_codeint`= '".$row["int_code"]."'";
				$rs2 = mysql_query($rech2);
				if (mysql_num_rows($rs2) <> 0)
				{
					while ($row2 = mysql_fetch_assoc($rs2)) 
					{         
						$nbtotalsec = $nbtotalsec + diff_time_sec($row2["inth_heurea"],$row2["inth_heured"]);
						if ($row2["inth_offert"] == 1)
							$nbtotalsecoff = $nbtotalsecoff + diff_time_sec($row2["inth_heurea"],$row2["inth_heured"]);
					}
				}
				
				$totalgen+=$nbtotalsec;
				$totalgenoff+=$nbtotalsecoff;
	
				// Si un utilisateur a quitté avant de sauver (bien qu'ayant modifié le détail horaire,
				// on remet en état car seul le détail horaire et technicien enregistré est valable
				if ($row["int_durees"] <> $nbtotalsec)
				{
					$rech2 = "UPDATE `" .TBINT. "` SET `int_datemod`='".$datea."', `int_codemod`='".$codeuser."',`int_durees`='".$nbtotalsec."'";
					$rech2 .=" WHERE `int_code` = '".$ci."'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
					$row["int_durees"] = $nbtotalsec;
				}
	
				 if ($row["int_durees"] <> 0)
					echo sec_heure($row["int_durees"]);
				 else
					echo "&nbsp;";
				if ($nbtotalsecoff<>0)
				{
					echo "<span class=\"annot_petit_gras\">";
					echo " <img src=\"../img/gift.gif\" title=\"Temps offert -> ".sec_heure($nbtotalsecoff)."\"  width=\"16\" height=\"16\" border=\"0\" align=\"absmiddle\">";
					echo "</span>";
				}
	
				 echo "</td>\n";
	
				 // Affichage Icône Statut
				 
				 echo "<td>";
				 
				 echo "<div align=\"center\">";
				 
				 $statut = $row["int_codestatut"];
				 $rechico = "SELECT * FROM ".TBSTA." WHERE statut_code = '$statut'";
				 
				 $rsico = mysql_query($rechico) or die(mysql_error());
				 
					if (mysql_num_rows($rsico) <> 0)
					{
						if ($rowico = mysql_fetch_array($rsico))
						{
							extract ($rowico);
							if ($statut_img <> "")
							{
								echo "<img src=\"".$statut_img."\" title=\"".$statut_desc."\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\">";
							}
							else
							{
								 echo "<span class=\"annot_std_it\">";
								 echo $statut;
								 echo "</span>";
							}
						}
					}
			 
					mysql_free_result($rsico);
		
					$url = "int_maj.php?td=edit&ci=".$row["int_code"]."&cc=".$row["coclh_codecli"]."&ca=".$row["int_codeage"];
					$desc = "Voir fiche d'intervention";
				 ?>
				 <a href="javascript:void(0)" onClick="javascript:; window.opener.location='<?PHP echo $url ?>'; self.close('fscomp');")>
				 <img src="../img/view.gif" title="<?PHP echo $desc ?>" width="<?PHP echo $hl_vign ?>" height="<?PHP echo $hl_vign ?>" border="0" align="absmiddle">
				 </a>
				 <?PHP
				 echo "</div>";
				 echo "</td>\n";
		
				 echo "</tr>\n";
			 // Fin affichage résumé Contrat Client
				
				$i++;    
			}  

			// Totaux
			creetrlst($i);
	
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "</tr>\n";
			 
			 $i++;

			creetrlst($i);
	
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "<td align=\"right\">Total g&eacute;n&eacute;ral : </td>\n";
			 echo "<td align=\"center\">";
			 if ($totalgen <> 0)
				echo sec_heure($totalgen);
			 else
				echo "&nbsp;";
			 echo "</td>\n";
			 echo "<td align=\"center\">&nbsp;</td>\n";
			 echo "</tr>\n";
			 
			 if ($totalgenoff <> 0)
			 {
				 $i++;

				 creetrlst($i);
		
				 echo "<td align=\"center\">&nbsp;</td>\n";
				 echo "<td align=\"center\">&nbsp;</td>\n";
				 echo "<td align=\"center\">&nbsp;</td>\n";
				 echo "<td align=\"center\">&nbsp;</td>\n";
				 echo "<td align=\"right\">Dont offert : </td>\n";
				 echo "<td align=\"center\">";
				 echo sec_heure($totalgenoff);
				 echo "</td>\n";
				 echo "<td align=\"center\">&nbsp;</td>\n";
				 echo "</tr>\n";
			}
			 
			echo "</table>\n";
			// *** Fin affichage de la table
			 if ($totalgenoff <> 0)
			 {
				echo "<p align=\"center\">\n";
				echo "<span class=\"annot_petit_gras\">";
				echo " <img src=\"../img/gift.gif\" title=\"Intervention avec du temps offert\" border=\"0\" align=\"absmiddle\">";
				echo " : Temps offert";
				echo "<span class=\"annot_petit_it\">";
				echo " (laisser souris sur l'ic&ocirc;ne pour le d&eacute;tail)";
				echo "</span>";
				echo ".";
				echo "</span>";
				echo "</p>";
			 }
		}
		else
			echo "<br><span class=\"annot_std_gras\">Aucun historique interventions sur ce contrat.</span><br>";
	}
	mysql_free_result($rs);
	mysql_close($conn);
}
else
	echo "<br><span class=\"annot_std_gras\">Merci de choisir un contrat.</span><br>";

?>
	<div align="right">
	<input name="ferm" type="button" class="bton_fmarr_std" id="ferm" value="Fermer la fen&ecirc;tre" onClick="javascript:; window.self.close('');">
	</div>
<?PHP
}
else
{
?>
	<script type="text/javascript">
	self.close('fscomp');
	</script>
<?PHP
}
?>
</body>
</html>
