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

Version 0.9.9

Copyright (C) 2006-2009  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");

if (!isset($_SESSION["lng"])) $_SESSION["lng"]="fr";
include_once("../lng/".$_SESSION["lng"].".inc.php"); // Paramètres de language
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/lst.inc.php");
include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_loc.inc.php");
Archivelog(@$_SESSION["code_log"]);

$pbm_site=FALSE;
$pbm_cli=FALSE;
$pbm_loc = FALSE;
$sitep=FALSE;
$lstsites = "";

// On a pas validé
if (strip_tags(trim(@$_POST["val"])) <> $but_valider)
{
	$cc = strip_tags(trim($_GET['cc'])); // Récupération code Client
	$cl = strip_tags(trim($_GET['cl'])); // Récupération code Contrat location
	$td = strip_tags(trim($_GET['td'])); // Récupération type demande
	$si = intval(strip_tags(trim($_GET['si']))); // Récupération site Intervention
	
	if (($td<>"edit") AND ($td<>"view") AND ($td<>"add"))
		$pbm_loc=TRUE;

	// On contrôle le code site
	if (($si==NULL) OR ($si<0))
		$si=0;
	if ($si==0)
		$sitep = TRUE;
		
	if ($si<>0)
	{
		$row=lit_enr_scli_id($si,TRUE);
		if (!is_array($row))
			$pbm_site=TRUE;
	}

	if ($cc<>"")
	{
		// Contrôle qu'un client n'essaye pas de "hacker" un contrat
		if ((@$_SESSION["niv_log"] == 1) AND ($cc <> @$_SESSION["code_log"]))
			$pbm_cli = TRUE;

		$row = lit_enr_cli($cc,TRUE);
		if (is_array($row))
		{
			extract ($row);

			// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
			switch(@$_SESSION["niv_log"])
			{
				case 3 :
				case 4 :
					if (client_age_ok($cli_code,@$_SESSION["code_age"],TRUE)==FALSE)
						$detail_ok = TRUE;
					break;
				default :
					break;
			}
		}
		else
			$pbm_cli = TRUE;
	}
	else
		$pbm_cli = TRUE;

	if ($cl <> "")
	{
		// Récupération des informations intervention client
		$row = lit_enr_coloc($cl);
		if (is_array($row))
		{
			extract ($row);
			
			// Si contrat terminé, force le mode "view"
			if (formatedate($coloc_datefin) <> "")
				$td="view";

			if ($td <> "add")
			{
				// Contrôle qu'un client n'essaye pas de "hacker" un contrat
				if ((@$_SESSION["niv_log"] == 1) AND ($coloc_codecli <> @$_SESSION["code_log"]))
					$pbm_loc = TRUE;
				// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
				if (((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) AND ($coloc_codeage <> @$_SESSION["code_age"]))
					$pbm_loc = TRUE;
			}
		}
		else
		{
			if (($td=="edit") OR ($td=="view"))
				$pbm_loc=TRUE;
		}
	}
	else
	{
		$pbm_loc = TRUE;
	}
}
else
	$cc = strip_tags(trim($_POST['cc'])); // Récupération code Client
?>
<!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 - <?PHP echo $title_esp_prive; ?> : Sites Location</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 pour contrôle de formulaire -->
<script	type="text/javascript" src="../jscript/vform/vform.js"></script>

<script language="javascript">
<!--
var tab_champ=new Array(
	"nom:scli_choisi ; format:radio ; req:Y ; txt:Site qui loue ; stok:saisieok ; stbad:saisiepasok");
//-->
</script>

<!-- Scripts Javascript divers -->
<script	type="text/javascript" src="../jscript/divers.js"></script>

</head>

<body>

<?PHP
if (($pbm_site==FALSE) AND ($pbm_cli==FALSE) AND ($pbm_loc==FALSE))
{
	if (strip_tags(trim(@$_POST["val"])) <> $but_valider) // Si le formulaire n'a pas été validé
	{
	?>
		<p align="center"><span class="titrepage">SITES QUI LOUENT</span></p>
		<p align="center">
		<?PHP
		if (($td=="add") OR ($td=="edit"))
		{
			echo "Merci de choisir le site qui va louer.";
			echo "<br><br>";
			echo "<span class=\"annot_std_it\">";
			echo "Liste de tous les sites actifs.";
			echo "</span>";
		}
		?>
		</p>		
	
		<form name="sitesl_lst" method="post" action="sitesl_lst.php" onSubmit="javascript:; return (VForm(this,tab_champ,'','<?PHP echo $_SESSION["lng"]; ?>'));">
		<input name="cl" type="hidden" id="cl" value="<?PHP echo $cli ?>">
		<input name="cc" type="hidden" id="cc" value="<?PHP echo $cc ?>">
		<input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
		<input name="si" type="hidden" id="si" value="<?PHP echo $si ?>">
		<?PHP
		// Création de la liste des sites secondaires
		
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_codecli`= '".$cc."' AND `scli_actif`=1 ORDER BY `scli_ste`";
		
		$rs = mysql_query($rech) or die(mysql_error());
		
		if ((mysql_num_rows($rs) <> 0) OR ($sitep==TRUE))
		{
			echo "<table class=\"MtableD\" border=\"0\" width=\"890\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";
			 
			// Création de l'entête de la table "sites d'intervention"
			echo "<th>";
			echo "Coordonn&eacute;es";
			echo "</th>\n";
			echo "<th>";
			echo "Tel/Fax";
			echo "</th>\n";
			echo "<th>";
			echo "Contact";
			echo "</th>\n";
			echo "<th>";
			echo "Créé le";
			echo "</th>\n";
			echo "<th>";
			echo "Choisir";
			echo "</th>\n";
			
			$j = 0;

			// Création de l'entête de ligne (TR)
			creetrlstdet($j);
			
			echo "<td>\n";
			echo "Site principal";
			echo "</td>\n";
			echo "<td align=\"center\">\n";
			echo "&nbsp;";
			echo "</td>\n";
			echo "<td align=\"center\">\n";
			echo "&nbsp;";
			echo "</td>\n";
			echo "<td align=\"center\">\n";
			echo "&nbsp;";
			echo "</td>\n";

			echo "<td align=\"center\">\n";

			$choisi = FALSE;

			if ($si==0)
				$choisi = TRUE;

			$readonly="";
			if ($td=="view")
			{
				$readonly="disabled";
			}
	
			if ($choisi == TRUE)
			{
				if($j%2 == 0)  
					echo "<input type=\"radio\" name=\"scli_choisi\" value=\"0\" class=\"TRalter1D\" checked ".$readonly.">";
				else
					echo "<input type=\"radio\" name=\"scli_choisi\" value=\"0\" class=\"TRalter2D\" checked ".$readonly.">";
			}
			else
			{
				if($j%2 == 0)  
					echo "<input type=\"radio\" name=\"scli_choisi\" value=\"0\" class=\"TRalter1D\" ".$readonly.">";
				else
					echo "<input type=\"radio\" name=\"scli_choisi\" value=\"0\" class=\"TRalter2D\" ".$readonly.">";
			}
			echo "</td>\n";
			echo "</tr>\n";
		
			$j++;

			while ($row = mysql_fetch_assoc($rs)) 
			{         
				// Création de l'entête de ligne (TR)
				creetrlstdet($j);
				
				echo "<td>\n";
				echo $row["scli_ste"];
				echo "<br>";
				echo $row["scli_adr1"];
				echo "<br>";
				if ($row["scli_adr2"]<>"")
				{
					echo $row["scli_adr2"];
					echo "<br>";
				}
				echo Retournecp_openId($row["scli_ville"],FALSE)." ".Retourneville_OpenId($row["scli_ville"],FALSE)."<br>";
				echo "</td>\n";
				echo "<td align=\"center\">\n";
				echo "T.".$row["scli_telf"];
				if ($row["scli_fax"]<>"")
				{
					echo"<br>";
					echo "F.".$row["scli_fax"];
				}
				echo "</td>\n";
				echo "<td align=\"center\">\n";
				echo $row["scli_rcivil"]." ".$row["scli_rprenom"]." ".$row["scli_rnom"];
				echo "<br>";
				echo "T.".$row["scli_rtelf"];
				if ($row["scli_rfax"]<>"")
				{
					echo "<br>";
					echo "F.".$row["scli_rfax"];
				}
				if ($row["scli_rtelp"]<>"")
				{
					echo "<br>";
					echo "P.".$row["scli_rtelp"];
				}
				echo "</td>\n";
				echo "<td align=\"center\">\n";
				echo formatedate($row["scli_datecrea"]);
				echo "</td>\n";
	
				echo "<td align=\"center\">\n";
				$choisi = FALSE;
				if ($row["scli_id"]==$si)
					$choisi = TRUE;
	
				$readonly="";
				if ($td=="view")
				{
					$readonly="disabled";
				}
		
				if ($choisi == TRUE)
				{
					if($j%2 == 0)  
						echo "<input type=\"radio\" name=\"scli_choisi\" value=\"".$row["scli_id"]."\" class=\"TRalter1D\" checked ".$readonly.">";
					else
						echo "<input type=\"radio\" name=\"scli_choisi\" value=\"".$row["scli_id"]."\" class=\"TRalter2D\" checked ".$readonly.">";
				}
				else
				{
					if($j%2 == 0)  
						echo "<input type=\"radio\" name=\"scli_choisi\" value=\"".$row["scli_id"]."\" class=\"TRalter1D\" ".$readonly.">";
					else
						echo "<input type=\"radio\" name=\"scli_choisi\" value=\"".$row["scli_id"]."\" class=\"TRalter2D\" ".$readonly.">";
				}
				echo "</td>\n";
				echo "</tr>\n";
			
				$j++;
			}
			
			echo "</table>\n";
			// Fin affichage table liste sites
		}
		mysql_free_result($rs);
		mysql_close($conn);  
		?>
		
		<p align="center">
		<?PHP
		if ($td<>"view")
		{
		?>
			<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="Remise &agrave; Z&eacute;ro">
			<input name="val" type="submit" class="bton_std" id="val" value="<?PHP echo $but_valider; ?>">
			<input name="annul" type="button" class="bton_std" id="annul" value="<?PHP echo $but_cancel; ?>" onClick="javascript:; self.close('fscomp');">
		<?PHP
		}
		else
		{
		?>
			<input name="annul" type="button" class="bton_std" id="annul" value="Fermer" onClick="javascript:; self.close('fscomp');">
		<?PHP
		}
		?>
		</p>
		</form>
	<?PHP
	}
	else
	{
		cre_js_ddeursloc($cc,$_POST["scli_choisi"],"window.opener."); 
	?>
<script type="text/javascript">
<!--
updatddeurs();
<?PHP
echo "var nouvsite=".$_POST["scli_choisi"].";\n";
?>
// On affecte le site et on met à jour la zone d'affichage du nom du site
window.opener.document.coloc_maj.idsite.value=nouvsite;
window.opener.document.getElementById("nomsite").innerHTML='<?PHP echo str_replace("'","\'",nom_scli($_POST["scli_choisi"],TRUE,TRUE,FALSE)); ?>';
self.close();
//-->
</script>
<?PHP
	}
}
else
{
?>
	<script type="text/javascript">
	<!--
	self.close();
	//-->
	</script>
<?PHP
}
?>
</body>
</html>
