<?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/config.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_log.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/lst.inc.php");

if (strip_tags(trim(@$_POST["val"])) == "Valider")
{
	$txtmatos="";
	$lstrlp = "";
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if (count($_POST["rlp_choisi"]) <> 0)
	{
		for ($i = 0; $i < count($_POST["rlp_choisi"]); $i++)
		{
			$rech = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_code`= '".$_POST["rlp_choisi"][$i]."'";
			$rs = mysql_query($rech);
			if (mysql_num_rows($rs) == 1) // Il ne peut y avoir qu'un seul résultat
			{
				if ($row = mysql_fetch_array($rs))
				{
					extract ($row);
					$lstrlp .= $rpdt_code."-";
				}
			}
			mysql_free_result($rs);
		}
	}
	if (count($_POST["rl_choisi"]) <> 0)
	{
		for ($i = 0; $i < count($_POST["rl_choisi"]); $i++)
		{
			$rech = "SELECT * FROM `" .TBRLIC. "` WHERE `rlic_code`= '".$_POST["rl_choisi"][$i]."'";
			$rs = mysql_query($rech);
			if (mysql_num_rows($rs) == 1) // Il ne peut y avoir qu'un seul résultat
			{
				if ($row = mysql_fetch_array($rs))
				{
					extract ($row);
					$lstrlp .= $rlic_code."-";
				}
			}
			mysql_free_result($rs);
		}
	}
	mysql_close($conn);

	if ($lstrlp!="")
		$lstrlp = substr($lstrlp,0,strlen($lstrlp)-1);
	if ($lstrlp<>"")
	{
		$txtmatos=htmlspecialchars(cree_txt_matos_cocl($lstrlp),ENT_QUOTES);
		$_SESSION["rlp_choisis"] = $lstrlp;
	}
	else
		$_SESSION["rlp_choisis"] = "";
}
else
{
	Archivelog(@$_SESSION["code_log"]);

	$td = strip_tags(trim($_GET['td'])); // Récupération type de demande
	$cc = strip_tags(trim($_GET['cc'])); // Récupération code Client
	$cocl = strip_tags(trim($_GET['co'])); // Récupération code Contrat
	$sp = strip_tags(trim($_GET['sp'])); // Récupération site principal

	// Si site principal choisi, on l'ajoute à la liste des sites
	if ($sp==1)
	{
		$lst_sites=$_SESSION["scli_choisis"]."-0";
	}
	else
	{
		$lst_sites=$_SESSION["scli_choisis"];
	}
	$tab_sites=explode("-",$lst_sites);
}
?>
<!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 : Liste Mat&eacute;riels &amp; Licences relev&eacute;s</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>

<?PHP
if (strip_tags(trim(@$_POST["val"])) <> "Valider") // Si le formulaire n'a pas été validé
{
?>
<body onLoad="javascript:; document.rlp_lst.check.value=valbouton(document.rlp_lst);">
	<p align="center"><span class="titrepage">LISTE DES MATERIELS &amp; LICENCES RELEVES </span></p>
	<p align="center"><span class="annot_petit_it">(En fonction des sites s&eacute;lectionn&eacute;s)</span></p>
	<p align="center">
	<?PHP if ($td == "add") { ?>
		Merci de choisir les mat&eacute;riels et licences que vous souhaitez affecter au contrat en cours de cr&eacute;ation,
	<?PHP }
	if ($td == "edit") { 
	?>
		Merci de choisir les mat&eacute;riels et licences signal&eacute;s que vous souhaitez affecter au contrat client, <span class="annot_std_gras"><?PHP echo $cocl;?></span>
	<?PHP 
	} 
	?>
	pour les sites s&eacute;lectionn&eacute;s.
	</p>	

	<form name="rlp_lst" method="post" action="rlp_lst.php">
	<?PHP
	echo "<p align=\"center\">";
	echo "<br>";
	echo "<input type=\"button\" name=\"check\" value=\"Tous\" class=\"bton_fgris_petit\" onclick=\"this.value=checkall(this.value,this.form);\">";
	echo "</p>";

	// Création de la liste des relevés de matériels actifs
	echo "Relevés de matériels<br>";
	
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech = "SELECT * FROM ".TBRPDT." WHERE `rpdt_codecli` = '".$cc."' AND `rpdt_dateout` = '0000-00-00' ORDER BY `rpdt_idsite`";
	
	$rs = mysql_query($rech);
	
	if (mysql_num_rows($rs) <> 0)
	{
		
		echo "<table class=\"MtableD\" border=\"0\" width=\"890\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";
		 
	
		// Création de l'entête de la table "relevés de matériels"
		echo "<th>";
		echo "Créé le";
		echo "</th>\n";
		echo "<th>";
		echo "Modifié le";
		echo "</th>\n";
		echo "<th>";
		echo "Détail";
		echo "</th>\n";
		echo "<th>";
		echo "Choisir";
		echo "</th>\n";
		
		$j = 0;
		
		while ($row = mysql_fetch_assoc($rs)) 
		{         
			// On ne prend que les relevés "tous les sites" ou sur les sites choisis sur le contrat
			if ((exist_txt_in_tabtxt($row["rpdt_idsite"],$tab_sites)) OR ($row["rpdt_idsite"]==-1))
			{
				// Création de l'entête de ligne (TR)
				creetrlstdet($j);
				
				echo "<td align=\"center\">\n";
				echo formatedate($row["rpdt_datecrea"]);
				echo "</td>\n";
				echo "<td align=\"center\">\n";
				echo formatedate($row["rpdt_datemod"]);
				echo "</td>\n";
		
				echo "<td>\n";
				echo "<span class=\"annot_petit_gras\">";
				$nom_site="";
				switch ($row["rpdt_idsite"])
				{
					case -1 :
						$nom_site="Tous les sites";
						break;
					case 0 :
						$nom_site="Site principal";
						break;
					default :
						$rech2 = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '".$row["rpdt_idsite"]."'";
						$rs2 = mysql_query($rech2)or die(mysql_error());
						if ($row2 = mysql_fetch_array($rs2))
						{
							extract($row2);
							$nom_site.= $scli_ste;
							$nom_site.= "Site : (".Retournecp_openId($scli_ville,FALSE)." ".Retourneville_openId($scli_ville,FALSE).")";
						}
						mysql_free_result($rs2);
				}
				echo $nom_site."<br>";
				echo "</span>";
				$rech2 = "SELECT * FROM ".TBMQUE." WHERE `mque_code` = '".$row["rpdt_codema"]."'";
				$rs2 = mysql_query($rech2);
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract ($row2);
				}
				$txtmatos = "Relevé ".$row["rpdt_code"]." : ".$mque_ste." ".$row["rpdt_modele"]." ".$row["rpdt_version"];
				mysql_free_result($rs2);
		
				echo $txtmatos."<br>";
				echo "</td>\n";
				
				echo "<td align=\"center\">\n";
				$choisi = FALSE;
				if ($_SESSION["rlp_choisis"] <> "")
				{
					if (ereg($row["rpdt_code"],$_SESSION["rlp_choisis"]))
						$choisi = TRUE;
				}
		
				echo "<input type=\"checkbox\" name=\"rlp_choisi[]\" value=\"".$row["rpdt_code"]."\"";
				if($j%2 == 0)
					echo " class=\"TRalter1D\"";
				else
					echo " class=\"TRalter2D\"";


				if ($choisi == TRUE)
					echo " checked";
					
				if ($td=="view")
					echo " disabled";
				else
					echo " onclick=\"document.rlp_lst.check.value=valbouton(document.rlp_lst);\"";
				echo ">";

				echo "</td>\n";
			
				echo "</tr>\n";
		
				$j++;
			}
		}
		
		echo "</table>\n";
		// Fin affichage table relevés matériels
		if ($j==0)
		{
			echo "<span class=\"annot_petit_it\">";
			echo "Pas de matériel sur les sites sélectionnés.<br>";
			echo "</span>";
		}
	
	}
	else
	{
		echo "Aucun relevé de matériel.<br>";
	}
	mysql_free_result($rs);
	mysql_close($conn);


	// Création de la liste des relevés de licences actifs
	
	echo "<br>Relevés de licences<br>";
	
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech = "SELECT * FROM ".TBRLIC." WHERE `rlic_codecli` = '".$cc."' AND `rlic_dateout` = '0000-00-00'  ORDER BY `rlic_idsite`";
	
	$rs = mysql_query($rech);
	
	if (mysql_num_rows($rs) <> 0)
	{
		
		echo "<table class=\"MtableD\" border=\"0\" width=\"890\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";
		 
		// Création de l'entête de la table "relevés de licences"
		echo "<th>";
		echo "Créé le";
		echo "</th>\n";
		echo "<th>";
		echo "Modifié le";
		echo "</th>\n";
		echo "<th>";
		echo "Détail";
		echo "</th>\n";
		echo "<th>";
		echo "Choisir";
		echo "</th>\n";
		
		$j = 0;
		$lstrlp = "";
		
		while ($row = mysql_fetch_assoc($rs)) 
		{         
			// On ne prend que les relevés "tous les sites" ou sur les sites choisis sur le contrat
			if ((exist_txt_in_tabtxt($row["rlic_idsite"],$tab_sites)) OR ($row["rlic_idsite"]==-1))
			{
				// Création de l'entête de ligne (TR)
				creetrlstdet($j);
				
				echo "<td align=\"center\">\n";
				echo formatedate($row["rlic_datecrea"]);
				echo "</td>\n";
				echo "<td align=\"center\">\n";
				echo formatedate($row["rlic_datemod"]);
				echo "</td>\n";
		
				echo "<td>\n";
				echo "<span class=\"annot_petit_gras\">";
				$nom_site="";
				switch ($row["rlic_idsite"])
				{
					case -1 :
						$nom_site="Tous les sites";
						break;
					case 0 :
						$nom_site="Site principal";
						break;
					default :
						$rech2 = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '".$row["rlic_idsite"]."'";
						$rs2 = mysql_query($rech2)or die(mysql_error());
						if ($row2 = mysql_fetch_array($rs2))
						{
							extract($row2);
							$nom_site.= $scli_ste;
							$nom_site.= "Site :  (".Retournecp_openId($scli_ville,FALSE)." ".Retourneville_openId($scli_ville,FALSE).")";
						}
						mysql_free_result($rs2);
				}

				echo $nom_site."<br>";
				echo "</span>";
				$rech2 = "SELECT * FROM ".TBMQUE." WHERE `mque_code` = '".$row["rlic_codema"]."'";
				$rs2 = mysql_query($rech2);
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract ($row2);
				}
				$txtmatos = "Relevé ".$row["rlic_code"]." : ".$mque_ste." ".$row["rlic_modele"]." ".$row["rlic_version"];
				mysql_free_result($rs2);
		
				echo $txtmatos."<br>";
				echo "</td>\n";
				
				echo "<td align=\"center\">\n";
				$choisi = FALSE;
				if ($_SESSION["rlp_choisis"] <> "")
				{
					if (ereg($row["rlic_code"],$_SESSION["rlp_choisis"]))
						$choisi = TRUE;
				}
		
				echo "<input type=\"checkbox\" name=\"rl_choisi[]\" value=\"".$row["rlic_code"]."\"";
				if($j%2 == 0)
					echo " class=\"TRalter1D\"";
				else
					echo " class=\"TRalter2D\"";


				if ($choisi == TRUE)
					echo " checked";
					
				if ($td=="view")
					echo " disabled";
				else
					echo " onclick=\"document.rlp_lst.check.value=valbouton(document.rlp_lst);\"";
				echo ">";

				echo "</td>\n";
			
				echo "</tr>\n";
			
				$j++;
			}
		}
		
		echo "</table>\n";
		// Fin affichage table relevés licences
		if ($j==0)
		{
			echo "<span class=\"annot_petit_it\">";
			echo "Pas de licences sur les sites sélectionnés.<br>";
			echo "</span>";
		}
	}
	else
	{
		echo "Aucun relevé de licence.<br>";
	}
	mysql_free_result($rs);
	mysql_close($conn);

	?>
	<p align="center">
		<input name="RAZ" type="reset" class="bton_fmarr_std" id="RAZ" value="Remise &agrave; Z&eacute;ro">
		<input name="val" type="submit" class="bton_fmarr_std" id="val" value="Valider">
		<input name="annul" type="button" class="bton_fmarr_std" id="annul" value="Annuler" onClick="javascript:; self.close('fscomp');">
	</p>
	<p align="center" class="annot_gras_it">ATTENTION : apr&egrave;s validation, la zone &quot;mat&eacute;riel concern&eacute;&quot; de la fiche contrat sera r&eacute;initialis&eacute;e avec les nouvelles valeurs.</p>
	</form>
<?PHP 
}

if (strip_tags(trim(@$_POST["val"])) == "Valider")
{
?>
<body>
<script type="text/javascript">
var valmat=window.opener.document.cocl_maj.matos.value;
<?PHP
// On génère la variable qui va permettre de comparer
echo "var valtxtmat='".@$_SESSION["txtmatlst"]."';\n";
// Une fois que c'est fait, on réaffecte le nouveau texte généré
$_SESSION["txtmatlst"]=$txtmatos;
// On génère la variable du nouveau texte
echo "var txtmatos ='".$txtmatos."';\n";
?>
var matos=update_txt_in_txt(valmat,valtxtmat,txtmatos);
window.opener.document.cocl_maj.matos.value=matos;
window.opener.document.cocl_maj.matos.readOnly = true;
self.close();
</script>
<?PHP
}
?>
</body>
</html>
