<?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 require ("../inc/fic.inc.php") ?>
<?PHP
	$pbm_cli=FALSE;
	$pbm_cocl = FALSE;
	
	// Niveau <=2 ne peut que voir les sites des contrats
	if (@$HTTP_SESSION_VARS["niv_log"] <= 2)
		$td = "view";

	if (strip_tags(trim(@$_POST["val"])) == "Valider")
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$lstsites = "";
		if (count($_POST["scli_choisi"]) <> 0)
		{
			for ($i = 0; $i < count($_POST["scli_choisi"]); $i++)
			{
				$lstsites .= $_POST["scli_choisi"][$i]."-";
			}
		}
		mysql_close($conn);
		$HTTP_SESSION_VARS["scli_choisis"] = $lstsites;
	}
	else
	{
		Archivelog(@$HTTP_SESSION_VARS["code_log"]);

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

		if ($cc<>"")
		{
			// Contrôle qu'un client n'essaye pas de "hacker" un contrat
			if ((@$HTTP_SESSION_VARS["niv_log"] == 1) AND ($cc <> @$HTTP_SESSION_VARS["code_log"]))
				$pbm_cli = TRUE;
	
			$row = lit_enr_cli($cc);
			if (is_array($row))
			{
				extract ($row);
	
				// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
		
				if ((@$HTTP_SESSION_VARS["niv_log"] == 4) OR (@$HTTP_SESSION_VARS["niv_log"] == 3))
				{
					$row = lit_enr_age($HTTP_SESSION_VARS["code_age"]);
					extract ($row);
					$zc_couverte = $age_zone;
				}
	
				if ((@$HTTP_SESSION_VARS["niv_log"] == 4) OR (@$HTTP_SESSION_VARS["niv_log"] == 3))
				{
					if (dep_in_zc($cli_dep,$zc_couverte) == TRUE) 
					{
						if (($cli_prop <> @$HTTP_SESSION_VARS["code_age"]) AND ($cli_prop <> NULL))
						{
							$pbm_cli = TRUE;
						}
					}
					else
					{
						$pbm_cli = TRUE;
					}
				}
			}
			else
			{
				$pbm_cli = TRUE;
			}
		}
		else
		{
			$pbm_cli = TRUE;
		}
	
		if (($td<>"edit") AND ($td<>"view") AND ($td<>"add"))
			$pbm_cocl=TRUE;
	
		if ($co <> "")
		{
			// Récupération des informations contrat client
			$row = lit_enr_cocl($co);
			if (is_array($row))
			{
				extract ($row);
				
				// Si contrat terminé, force le mode "view"
				if (formatedate($cocl_datesig) <> "")
					$td="view";
	
				if ($td <> "add")
				{
					// Contrôle qu'un client n'essaye pas de "hacker" un contrat
					if ((@$HTTP_SESSION_VARS["niv_log"] == 1) AND ($cocl_codecli <> @$HTTP_SESSION_VARS["code_log"]))
						$pbm_cocl = TRUE;
				// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
					if (((@$HTTP_SESSION_VARS["niv_log"] == 4) OR (@$HTTP_SESSION_VARS["niv_log"] == 3)) AND ($cocl_codeage <> @$HTTP_SESSION_VARS["code_age"]))
						$pbm_cocl = TRUE;
				}
			}
			else
			{
				if (($td=="edit") OR ($td=="view"))
					$pbm_cocl=TRUE;
			}
		}
		else
		{
			$pbm_cocl = TRUE;
		}

	}
	
?>

<!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 : Sites secondaires</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 (($pbm_cli==FALSE) AND ($pbm_cocl==FALSE))
{
	if (strip_tags(trim(@$_POST["val"])) <> "Valider") // Si le formulaire n'a pas été validé
	{
	?>
		<p align="center"><span class="titrepage">SITES SECONDAIRES</span></p>
		<p align="center">
		<?PHP
		if ($td=="add")
		{
		?>
			Merci de choisir les sites secondaires que vous souhaitez voir couvrir par le contrat en cours de cr&eacute;ation.
		<?PHP
		}
		
		if ($td=="edit")
		{
		?>
			Merci de choisir les sites secondaires que vous souhaitez voir couvrir par le contrat <?PHP echo $co; ?>.
		<?PHP
		}
	
		if ($td=="view")
		{
		?>
			Sites secondaires couverts par le contrat <?PHP echo $co; ?>.
		<?PHP
		}
		?>
		</p>		
	
		<form name="sites_lst" method="post" action="sites_lst.php">
		<?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);
		
		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 "sites secondaires"
			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>";
			if ($td<>"view")
				echo "Choisir";
			else
				echo "&nbsp;";
			echo "</th>\n";
			
			$j = 0;
			$lstsites = "";
		
			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($row["scli_ville"])." ".Retourneville($row["scli_ville"])."<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 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 ($HTTP_SESSION_VARS["scli_choisis"] <> "")
				{
					if (ereg($row["scli_id"],$HTTP_SESSION_VARS["scli_choisis"]))
						$choisi = TRUE;
				}
	
				$readonly="";
				if ($td=="view")
				{
					$readonly="disabled";
				}
		
				if ($choisi == TRUE)
				{
					if($j%2 == 0)  
						echo "<input type=\"checkbox\" name=\"scli_choisi[]\" value=\"".$row["scli_id"]."\" class=\"TRalter1D\" checked ".$readonly.">";
					else
						echo "<input type=\"checkbox\" name=\"scli_choisi[]\" value=\"".$row["site_id"]."\" class=\"TRalter2D\" checked ".$readonly.">";
				}
				else
				{
					if($j%2 == 0)  
						echo "<input type=\"checkbox\" name=\"scli_choisi[]\" value=\"".$row["scli_id"]."\" class=\"TRalter1D\" ".$readonly.">";
					else
						echo "<input type=\"checkbox\" 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);
		?>
		
		<p align="center">
		<?PHP
		if ($td<>"view")
		{
		?>
			<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');">
		<?PHP
		}
		else
		{
		?>
			<input name="annul" type="button" id="annul" value="Fermer" onClick="javascript:; self.close('fscomp');">
		<?PHP
		}
		?>
		</p>
		</form>
	<?PHP
	}
	else
	{
	?>
		<script type="text/javascript">
		window.opener.document.cocl_maj.sitesec.value='<?PHP echo $HTTP_SESSION_VARS["scli_choisis"]; ?>';
		</script>
	<?PHP
		
		if ($HTTP_SESSION_VARS["scli_choisis"]=="")
		{
	?>
			<script type="text/javascript">
			window.opener.document.cocl_maj.sitep.checked=true;
			</script>
	<?PHP
		}
	?>
		<script type="text/javascript">
		self.close();
		</script>
	<?PHP
	}
}
else
{
?>
	<script type="text/javascript">
	self.close();
	</script>
<?PHP
}
?>	