<?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_log.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/fic_cli.inc.php");
Archivelog(@$_SESSION["code_log"]);

// Variables Formulaire + Contrôler action à effectuer
$titre = stripslashes(strip_tags(trim(@$_POST["titre"])));
$td = strip_tags(trim(@$_POST["td"]));
$idscli = strip_tags(trim(@$_POST["id"]));
$prop = str_majuscules(strip_tags(trim(@$_POST["prop"])));
$code = strip_tags(trim(@$_POST["cc"]));
$ste = str_majuscules(str_escape(trim(@$_POST["ste"])));
$rcs = str_majuscules(str_escape(trim(@$_POST["rcs"])));
$ape = str_majuscules(str_escape(trim(@$_POST["ape"])));
$tvai = str_majuscules(str_escape(trim(@$_POST["tvai"])));
$adr1 = str_majuscules(str_escape(trim(@$_POST["adr1"])));
$adr2 = str_majuscules(str_escape(trim(@$_POST["adr2"])));
$dep = str_majuscules(strip_tags(trim(@$_POST["dep"])));
$ville = str_majuscules(strip_tags(trim(@$_POST["ville"])));
$pays = str_majuscules(strip_tags(trim(@$_POST["pays"])));
$telf = strip_tags(trim(@$_POST["telf"]));
$fax = strip_tags(trim(@$_POST["fax"]));
$email = strtolower(strip_tags(trim(@$_POST["email"])));
$rcivil = str_majuscules(strip_tags(trim(@$_POST["rcivil"])));
$rprenom = ucwords(strtolower(str_escape(trim(@$_POST["rprenom"]))));
$rnom = str_majuscules(str_escape(trim(@$_POST["rnom"])));
$rtelf = strip_tags(trim(@$_POST["rtelf"]));
$rfax = strip_tags(trim(@$_POST["rfax"]));
$rtelp = strip_tags(trim(@$_POST["rtelp"]));
$remail = strtolower(strip_tags(trim(@$_POST["remail"])));
$rfunc = str_majuscules(strip_tags(trim(@$_POST["rfunc"])));
$nota = str_escape(trim(@$_POST["nota"]));
$actif = strip_tags(trim(@$_POST["actif"]));
if ($actif == "Oui")
	$actif = TRUE;
else
	$actif = FALSE;
	
$codeuser = @$_SESSION["code_log"];

$retourfiche = TRUE;

switch ($td)
{
	// Ajout de la fiche Site
	case "add" :
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		switch (@$_SESSION["niv_log"])
		{
			case 1 :
			case 6 :
			case 7 :
				$rech = "INSERT INTO " .TBSCLI;
				$rech .= " (scli_datecrea,scli_codecrea,scli_datemod,scli_codemod,scli_codecli,scli_ste,scli_rcs,scli_ape,scli_tvai,scli_adr1,scli_adr2,scli_dep,scli_ville,scli_codepays,scli_telf,scli_fax,scli_email,scli_rcivil,scli_rprenom,scli_rnom,scli_rtelf,scli_rfax,scli_rtelp,scli_remail,scli_rcodefonc,scli_nota,scli_actif) VALUES";
				$rech .= " ('$datea','$codeuser','$datea','$codeuser','$code','$ste','$rcs','$ape','$tvai','$adr1','$adr2','$dep','$ville','$pays','$telf','$fax','$email','$rcivil','$rprenom','$rnom','$rtelf','$rfax','$rtelp','$remail','$rfunc','$nota','$actif')";
				break;
			default :
				$rech = "INSERT INTO " .TBSCLI;
				$rech .= " (scli_datecrea,scli_codecrea,scli_datemod,scli_codemod,scli_prop,scli_codecli,scli_ste,scli_rcs,scli_ape,scli_tvai,scli_adr1,scli_adr2,scli_dep,scli_ville,scli_codepays,scli_telf,scli_fax,scli_email,scli_rcivil,scli_rprenom,scli_rnom,scli_rtelf,scli_rfax,scli_rtelp,scli_remail,scli_rcodefonc,scli_nota,scli_actif) VALUES";
				$rech .= " ('$datea','$codeuser','$datea','$codeuser','$prop','$code','$ste','$rcs','$ape','$tvai','$adr1','$adr2','$dep','$ville','$pays','$telf','$fax','$email','$rcivil','$rprenom','$rnom','$rtelf','$rfax','$rtelp','$remail','$rfunc','$nota','$actif')";
				break;
		}
		$rs = mysql_query($rech) or die(mysql_error());
		mysql_close($conn);
		
		$msg = $txt_fiche_add;
		break;
	
	// MAJ de la fiche Site
	case "edit" :
		$existescliintouv = FALSE;
	
		// Si on ferme le site, il faut vérifier que rien n'est en cours
		if ($actif==FALSE)
		{
			// Vérifie si utilisé dans une fiche intervention en cours
			if (exist_scli_in_int_ouv($idscli) == TRUE)
			{
				$existescliintouv = TRUE;
				$msg = "Ce site client est d&eacute;j&agrave; utilis&eacute; dans une fiche intervention encore ouverte.<br />Vous ne pouvez donc pas le fermer.";
				$retourfiche = FALSE;
			}
		}
		
		if  ($existescliintouv == FALSE)
		{
			// MAJ de la fiche Site
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			switch (@$_SESSION["niv_log"])
			{
				case 1 :
				case 6 :
				case 7 :
					$rech = "UPDATE `" .TBSCLI. "` SET `scli_datemod`='".$datea."', `scli_codemod`='".$codeuser."'";
					$rech.= ", `scli_codecli`='".$code."', `scli_ste`='".$ste."', `scli_rcs`='".$rcs."', `scli_ape`='".$ape."', `scli_tvai`='".$tvai."'";
					$rech.= ", `scli_adr1`='".$adr1."', `scli_adr2`='".$adr2."', `scli_dep`='".$dep."', `scli_ville`='".$ville."'";
					$rech.= ", `scli_codepays`='".$pays."', `scli_telf`='".$telf."', `scli_fax`='".$fax."', `scli_email`='".$email."'";
					$rech.= ", `scli_rcivil`='".$rcivil."', `scli_rprenom`='".$rprenom."', `scli_rnom`='".$rnom."', `scli_rtelf`='".$rtelf."'";
					$rech.= ", `scli_rfax`='".$rfax."', `scli_rtelp`='".$rtelp."', `scli_remail`='".$remail."', `scli_rcodefonc`='".$rfunc."'";
					$rech.= ", `scli_nota`='".$nota."', `scli_actif`='".$actif."' WHERE `scli_id` = '".$idscli."'";
					break;
				default :
					$rech = "UPDATE `" .TBSCLI. "` SET `scli_datemod`='".$datea."', `scli_codemod`='".$codeuser."', `scli_prop`='".$prop."'";
					$rech.= ", `scli_codecli`='".$code."', `scli_ste`='".$ste."', `scli_rcs`='".$rcs."', `scli_ape`='".$ape."', `scli_tvai`='".$tvai."'";
					$rech.= ", `scli_adr1`='".$adr1."', `scli_adr2`='".$adr2."', `scli_dep`='".$dep."', `scli_ville`='".$ville."'";
					$rech.= ", `scli_codepays`='".$pays."', `scli_telf`='".$telf."', `scli_fax`='".$fax."', `scli_email`='".$email."'";
					$rech.= ", `scli_rcivil`='".$rcivil."', `scli_rprenom`='".$rprenom."', `scli_rnom`='".$rnom."', `scli_rtelf`='".$rtelf."'";
					$rech.= ", `scli_rfax`='".$rfax."', `scli_rtelp`='".$rtelp."', `scli_remail`='".$remail."', `scli_rcodefonc`='".$rfunc."'";
					$rech.= ", `scli_nota`='".$nota."', `scli_actif`='".$actif."' WHERE `scli_id` = '".$idscli."'";
					break;
			}
			$rs = mysql_query($rech) or die(mysql_error());
			mysql_close($conn);
	
			$msg = $txt_fiche_mod;
		}
		break;
		
	// Suppression de la fiche Site
	case "del" :
		$existescliint = FALSE;
		$existesclicocl = FALSE;
		$msg="";
	
		// Vérifie si utilisé dans une fiche intervention
		
		if (exist_scli_in_int($idscli) == TRUE)
		{
			$existescliint = TRUE;
			$msg = "Ce site client est d&eacute;j&agrave; utilis&eacute; dans une fiche intervention.<br>Vous ne pouvez donc pas le supprimer.";
			$retourfiche = FALSE;
		}
	
		// Vérifie si utilisé dans un contrat signé
		if (exist_scli_in_cocl_signe($idscli) == TRUE)
		{
			$existesclicocl = TRUE;
			if ($msg<>"")
				$msg.="<br><br>";
			$msg = "Ce site client est d&eacute;j&agrave; utilis&eacute; dans un contrat sign&eacute;.<br>Vous ne pouvez donc pas le supprimer.";
			$retourfiche = FALSE;
		}
	
		if  (($existescliint == FALSE) AND ($existesclicocl == FALSE))
		{
			// SUPPRESSION de la fiche Site
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
	
			$rech = "DELETE FROM `" .TBSCLI. "` WHERE `scli_id` = '$idscli'";
			$rs = mysql_query($rech) or die(mysql_error());
			mysql_close($conn);
	
			$msg = $txt_fiche_del;
		}
		break;
		
	default :
		break;
}
?>
<!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 Client</title>

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

</head>
<body>

<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<p align="center">&nbsp;</p>
<p align="center">
<?PHP if ($retourfiche==FALSE)
	echo '<span class="alerte1">';
else
	echo '<span class="annot_std_gras">';
?>
<?PHP echo $msg; ?>
</span>
</p>

<?PHP if ($retourfiche == FALSE) 
{ 
?>
		<p align="center" class="annot_std_it"><?PHP echo $txt_lnk_ret_saisie.", ".$txt_patience; ?></p>
		<!-- Fermeture temporisée -->
		<script type="text/javascript">
		<!--
		setTimeout("self.close('fscomp');",2000)
		//-->
		</script>
		<p align="center"><?PHP echo $txt_div_sinon; ?>, <a href="javascript:void(0)" onClick="javascript:; self.close('fscomp');"><?PHP echo $txt_lnk_clic_ici; ?></a> </p>
<?PHP 
} 
else
	aff_ret_sais_auto("scli_maj.php?cc=".$code."&td=lst",2000);
?>
</body>
</html>
