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

Version 0.9.6
Copyright (C) 2006  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
****************************************************************************************/

include ("../inc/config.inc.php");
?>
<?php require ("../inc/func.inc.php") ?>
<?php require ("../inc/func_date.inc.php") ?>
<?php require ("../inc/func_log.inc.php") ?>
<?php require ("../inc/func_txt.inc.php") ?>
<?php require ("../inc/lst.inc.php") ?>
<?PHP
	if (strip_tags(trim(@$_POST["val"])) == "Valider")
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$tab_txtmatos=array();
		$lstrlp = "";
		// Injection des codes relevés matériels
		$j=1;
		if (count($_POST["rp_choisi"]) <> 0)
		{
			$tab_txtmatos[]=array("+++ MATERIELS +++","","");
			for ($i = 0; $i < count($_POST["rp_choisi"]); $i++)
			{
				$rech = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_code`= '".$_POST["rp_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);
						
						$txtmatos_titre ="---- MATERIEL N°".sprintf("%'010s",$j)." ----";
						$rech2 = "SELECT * FROM ".TBRPCAT." WHERE `rpcat_code` = '".$rpdt_codecat."'";
						$rs2 = mysql_query($rech2);
						if ($row2 = mysql_fetch_array($rs2))
						{
							extract ($row2);
						}
						$rech2 = "SELECT * FROM ".TBRLPMA." WHERE `rlpma_code` = '".$rpdt_codema."'";
						$rs2 = mysql_query($rech2);
						if ($row2 = mysql_fetch_array($rs2))
						{
							extract ($row2);
						}
						$txtmatos_corps1 = "Relevé ".$rpdt_code." (".$rpdt_codep.") : ".$rpcat_desc." / ".$rlpma_desc." ".$rpdt_modele.", NS N°".$rpdt_ns.", situé ".$rpdt_empl.".";
						mysql_free_result($rs2);

						$txtmatos_pied = "---- FIN DETAIL MATERIEL N°".sprintf("%'010s",$j)." ----";
						$tab_txtmatos[]=array($txtmatos_titre,$txtmatos_corps1,$txtmatos_pied);
					}
					$j++;
				}
				mysql_free_result($rs);
				$lstrlp .= $rpdt_code."-";
			}
		}

		// Injection des codes relevés licences
		$j=1;
		if (count($_POST["rl_choisi"]) <> 0)
		{
			$tab_txtmatos[]=array("","+++ LICENCES +++","");
			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);
						
						$txtmatos_titre ="---- LICENCE N°".sprintf("%'010s",$j)." ----";
						$rech2 = "SELECT * FROM ".TBRLPMA." WHERE `rlpma_code` = '".$rlic_codema."'";
						$rs2 = mysql_query($rech2);
						if ($row2 = mysql_fetch_array($rs2))
						{
							extract ($row2);
						}
						$txtmatos_corps1 = "Relevé ".$rlic_code." : ".$rlpma_desc." ".$rlic_modele." ".$rlic_version.".";
						mysql_free_result($rs2);

						$txtmatos_pied = "---- FIN DETAIL LICENCE N°".sprintf("%'010s",$j)." ----";
						$tab_txtmatos[]=array($txtmatos_titre,$txtmatos_corps1,$txtmatos_pied);
					}
					$j++;
				}
				mysql_free_result($rs);
				$lstrlp .= $rlic_code."-";
			}
		}
		mysql_close($conn);
		$HTTP_SESSION_VARS["rp_choisis"] = $lstrlp;
	}
	else
	{
		Archivelog(@$HTTP_SESSION_VARS["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=$HTTP_SESSION_VARS["scli_choisis"]."0";
		}
		else
		{
			$lst_sites=$HTTP_SESSION_VARS["scli_choisis"];
		}
		$tab_sites=explode("-",$lst_sites);
	}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<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 -->
<link href="../inc/menu.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="../coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour affichages -->
<script language="JavaScript" src="../inc/aff.js"></script>

<!-- Script Javascript pour moulinage des tableaux-->
<script language="JavaScript" src="../inc/phparray2js.js"></script>

</head>

<body>

<?PHP
if (strip_tags(trim(@$_POST["val"])) <> "Valider") // Si le formulaire n'a pas été validé
{
?>
	<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 } ?>
	 </p>	

	<form name="rlp_lst" method="post" action="rlp_lst.php">
	<?PHP
	// 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'";
	
	$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";
				$rech2 = "SELECT * FROM ".TBRLPMA." WHERE `rlpma_code` = '".$row["rpdt_codema"]."'";
				$rs2 = mysql_query($rech2);
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract ($row2);
				}
				$txtmatos = "Relevé ".$row["rpdt_code"]." : ".$rlpma_desc." ".$row["rpdt_modele"]." ".$row["rpdt_version"];
				mysql_free_result($rs2);
		
				echo $txtmatos."<br>";
				echo "</td>\n";
				
				echo "<td align=\"center\">\n";
				$choisi = FALSE;
				if ($HTTP_SESSION_VARS["rp_choisis"] <> "")
				{
					if (ereg($row["rpdt_code"],$HTTP_SESSION_VARS["rp_choisis"]))
						$choisi = TRUE;
				}
		
				if ($choisi == TRUE)
				{
					if($j%2 == 0)  
						echo "<input type=\"checkbox\" name=\"rp_choisi[]\" value=\"".$row["rpdt_code"]."\" class=\"TRalter1D\" checked>";
					else
						echo "<input type=\"checkbox\" name=\"rp_choisi[]\" value=\"".$row["rpdt_code"]."\" class=\"TRalter2D\" checked>";
				}
				else
				{
					if($j%2 == 0)  
						echo "<input type=\"checkbox\" name=\"rp_choisi[]\" value=\"".$row["rpdt_code"]."\" class=\"TRalter1D\">";
					else
						echo "<input type=\"checkbox\" name=\"rp_choisi[]\" value=\"".$row["rpdt_code"]."\" class=\"TRalter2D\">";
				}
				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'";
	
	$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";
				$rech2 = "SELECT * FROM ".TBRLPMA." WHERE `rlpma_code` = '".$row["rlic_codema"]."'";
				$rs2 = mysql_query($rech2);
				if ($row2 = mysql_fetch_array($rs2))
				{
					extract ($row2);
				}
				$txtmatos = "Relevé ".$row["rlic_code"]." : ".$rlpma_desc." ".$row["rlic_modele"]." ".$row["rlic_version"];
				mysql_free_result($rs2);
		
				echo $txtmatos."<br>";
				echo "</td>\n";
				
				echo "<td align=\"center\">\n";
				$choisi = FALSE;
				if ($HTTP_SESSION_VARS["rp_choisis"] <> "")
				{
					if (ereg($row["rlic_code"],$HTTP_SESSION_VARS["rp_choisis"]))
						$choisi = TRUE;
				}
		
				if ($choisi == TRUE)
				{
					if($j%2 == 0)  
						echo "<input type=\"checkbox\" name=\"rl_choisi[]\" value=\"".$row["rlic_code"]."\" class=\"TRalter1D\" checked>";
					else
						echo "<input type=\"checkbox\" name=\"rl_choisi[]\" value=\"".$row["rlic_code"]."\" class=\"TRalter2D\" checked>";
				}
				else
				{
					if($j%2 == 0)  
						echo "<input type=\"checkbox\" name=\"rl_choisi[]\" value=\"".$row["rlic_code"]."\" class=\"TRalter1D\">";
					else
						echo "<input type=\"checkbox\" name=\"rl_choisi[]\" value=\"".$row["rlic_code"]."\" class=\"TRalter2D\">";
				}
				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" id="RAZ" value="Remise &agrave; Z&eacute;ro">
		<input name="val" type="submit" id="val" value="Valider">
		<input name="annul" type="button" 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
}
?>
<?PHP
if (strip_tags(trim(@$_POST["val"])) == "Valider")
{
?>
<script type="text/javascript">
	var tableau1 = new PhpArray2Js("<?php echo htmlspecialchars(serialize($tab_txtmatos)); ?>");
	var tab1 = tableau1.retour();
	var texte1 ="";
	

	for (i=0; i<tab1.length; i++)
	{
		texte1 += tab1[i].join("\n")+"\n\n";
	}

	window.opener.document.cocl_maj.matos.value=texte1;
	window.opener.document.cocl_maj.matos.readOnly = true;
	self.close();
</script>
<?PHP
}
?>