<?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/fic_age.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_rel.inc.php");
include_once("../inc/fic_txt.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_mail.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.inc.php");
include_once("../classes/phpmailer/class.phpmailer.php");
include_once("../inc/mail.inc.php");
Archivelog(@$_SESSION["code_log"]);

// Variables Formulaire
$titre = strip_tags(trim(@$_POST["titre"]));
$td = strip_tags(trim(@$_POST["td"]));
$typrel = strip_tags(trim(@$_POST["typrel"]));
$coderel = strip_tags(trim(@$_POST["coderel"]));
$id = strip_tags(trim(@$_POST["id"]));
$codecli = str_majuscules(strip_tags(trim(@$_POST["codecli"])));
$site = str_majuscules(strip_tags(trim(@$_POST["site"])));
$cap = strip_tags(trim(@$_POST["cap"]));
$coderlp = str_majuscules(strip_tags(trim(@$_POST["code"])));

$pbm = str_escape(trim(@$_POST["pbm"]));
$datecrea = datefrang(strip_tags(trim(@$_POST["datecreaf"])));
$signale = str_escape(trim(@$_POST["signale"]));

$codeint = strip_tags(trim(@$_POST["codeint"]));

$codeuser = @$_SESSION["code_log"];

$retourliste = TRUE;

switch ($td)
{
	// Ajout de la fiche Déclaration de problème du site principal
	case "add" :
		// Si tous les sites, on génère autant de fiches problèmes que de sites actifs
		if ($site==-1)
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
	
			$site=0;
			$rech = "INSERT INTO " .TBRPBM;
			$rech.= " (rpbm_datecrea,rpbm_codecrea,rpbm_datemod,rpbm_codemod,rpbm_coderlp,rpbm_typrel,rpbm_codecli,rpbm_signale,";
			$rech.= "rpbm_pbm,rpbm_idsite)";
			$rech.= " VALUES";
			$rech.= " ('$datecrea','$codeuser','$datea','$codeuser','$coderlp','$typrel','$codecli','$signale',";
			$rech.= "'$pbm','$site')";
			$rs = mysql_query($rech) or die(mysql_error());
			$tab_id[] = mysql_insert_id();
	
			$rech2 = "SELECT * FROM ".TBSCLI." WHERE `scli_actif` = '1' AND `scli_codecli` = '".$codecli."'";
			$rs2 = mysql_query($rech2);
			$nb_enr=mysql_num_rows($rs2);
		
			if ($nb_enr <> 0)
			{
				while ($row = mysql_fetch_array($rs2)) 
				{
					extract($row);
				
					// Ajout de la fiche Déclaration de problème sur les sites secondaires
					$rech = "INSERT INTO " .TBRPBM;
					$rech.= " (rpbm_datecrea,rpbm_codecrea,rpbm_datemod,rpbm_codemod,rpbm_coderlp,rpbm_typrel,rpbm_codecli,rpbm_signale,";
					$rech.= "rpbm_pbm,rpbm_idsite)";
					$rech.= " VALUES";
					$rech.= " ('$datecrea','$codeuser','$datea','$codeuser','$coderlp','$typrel','$codecli','$signale',";
					$rech.= "'$pbm','$scli_id')";
					$rs = mysql_query($rech) or die(mysql_error());
					$tab_id[] = mysql_insert_id();
				}
			}
			mysql_close($conn);
	
			if ($nb_enr <> 0)
			{
				// Envoi des différents emails
				if (count($tab_id) <> 0)
				{
					for ($i = 0; $i < count($tab_id); $i++)
					{
						if ($tab_id[$i]<>"")
						{
							// Envoi d'email vers le client
							envoi_mail("Déclaration d'un problème licence ou matériel","rpbm","cre","xxx","cli",$tab_id[$i],"","",FALSE);
							// Envoi d'email vers l'agence
							if (@$_SESSION["niv_log"] <> 4)
								envoi_mail("Déclaration d'un problème licence ou matériel","rpbm","cre","xxx","age",$tab_id[$i],"","",FALSE);
							// Envoi d'email vers le centre de gestion
							envoi_mail("Déclaration d'un problème licence ou matériel","rpbm","cre","xxx","cdg",$tab_id[$i],"","",TRUE);
						}
					}
				}
			}
	
			$msg = "Déclarations de problèmes enregistr&eacute;es";
		}
		else
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
	
			// Ajout de la fiche Déclaration de problème
			$rech = "INSERT INTO " .TBRPBM;
			$rech.= " (rpbm_datecrea,rpbm_codecrea,rpbm_datemod,rpbm_codemod,rpbm_coderlp,rpbm_typrel,rpbm_codecli,rpbm_signale,";
			$rech.= "rpbm_pbm,rpbm_idsite)";
			$rech.= " VALUES";
			$rech.= " ('$datecrea','$codeuser','$datea','$codeuser','$coderlp','$typrel','$codecli','$signale',";
			$rech.= "'$pbm','$site')";
			$rs = mysql_query($rech) or die(mysql_error());
			$id = mysql_insert_id();
			mysql_close($conn);
	
			// Envoi d'email vers le client
			envoi_mail("Déclaration d'un problème licence ou matériel","rpbm","cre","xxx","cli",$id,"","",FALSE);
			// Envoi d'email vers l'agence
			if (@$_SESSION["niv_log"] <> 4)
				envoi_mail("Déclaration d'un problème licence ou matériel","rpbm","cre","xxx","age",$id,"","",FALSE);
			// Envoi d'email vers le centre de gestion
			envoi_mail("Déclaration d'un problème licence ou matériel","rpbm","cre","xxx","cdg",$id,"","",TRUE);
	
			$msg = "Déclaration de problème enregistr&eacute;e";
		}
		break;
		
	// MAJ de la fiche relevé
	case "edit" :
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "UPDATE `" .TBRPBM;
		$rech .= "` SET `rpbm_datemod`='".$datea."', `rpbm_codemod`='".$codeuser."',`rpbm_coderlp`='".$coderlp."',`rpbm_typrel`='".$typrel."',";
		$rech .="  `rpbm_codecli`='".$codecli."', `rpbm_signale`='".$signale."', `rpbm_pbm`='".$pbm."', `rpbm_idsite`='".$site."'";
		$rech .=" WHERE `rpbm_id` = '".$id."'";
		$rs = mysql_query($rech) or die(mysql_error());
		mysql_close($conn);
	
		$msg = "Déclaration de problème modifi&eacute;e";
	
		// Envoi d'email vers le client
		envoi_mail("Modification d'un problème déjà déclaré","rpbm","cre","xxx","cli",$id,"","",FALSE);
		// Envoi d'email vers l'agence
		if (@$_SESSION["niv_log"] <> 4)
			envoi_mail("Modification d'un problème déjà déclaré","rpbm","cre","xxx","age",$id,"","",FALSE);
		// Envoi d'email vers le centre de gestion
		envoi_mail("Modification d'un problème déjà déclaré","rpbm","cre","xxx","cdg",$id,"","",TRUE);
		break;
		
	// Suppression de la fiche Relevé
	case "del" :
		if ($codeint == "")
		{
			// SUPPRESSION de la fiche Relevé
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "DELETE FROM `" .TBRPBM. "` WHERE `rpbm_id` = '$id'";
			$rs = mysql_query($rech) or die(mysql_error());
			mysql_close($conn);
			$msg = "Déclaration de problème supprim&eacute;e";
		}
		else
		{
			$retourliste = FALSE;
			$msg = "Impossible de supprimer une déclaration de problème rattachée à une intervention.<br>Sortez-la de l'intervention avant de la supprimer.";
		}
		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 - ESPACE PRIVE : D&eacute;claration de Probl&egrave;me</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>

</head>
</body>

<?php 
include_once("entete.inc.php"); 
?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<p align="center">&nbsp;</p>
<p align="center" class="annot_gras_it"><?PHP echo $msg ?></p>
<?PHP if ($retourliste == FALSE) 
{ 
?>
<br>
<br>
	<table width="100%"  border="0">
	  <tr>
		<td width="3%">&nbsp;</td>
		<td width="44%"><div align="right"><a href="rel_maj.php?td=edit&cr=<?PHP echo $coderel; ?>"><img src="../img/liste.gif" width="33" height="34" border="0" align="absmiddle"></a> <a href="rel_maj.php?td=edit&cr=<?PHP echo $coderel; ?>" class="annot_gras_it">Retour au dossier de relev&eacute;</a></div></td>
		<td width="7%">&nbsp;</td>
		<td width="43%"><a href="index.php"><em><img src="../img/index.gif" width="48" height="48" border="0" align="absmiddle"> </em><span class="annot_gras_it">Revenir &agrave; l'accueil</span> </a></td>
		<td width="3%">&nbsp;</td>
	  </tr>
	</table>
<?PHP 
} 
else 
{
	if ($cap == 0)
	{
?>
	<script type="text/javascript">
<?PHP
	if (@$_SESSION["niv_log"] == 1)
	{
?>
	window.setTimeout("location=('rel_maj.php');",2000)
<?PHP	} else { ?>
	window.setTimeout("location=('rel_maj.php?td=edit&cr=<?PHP echo $coderel; ?>');",2000)
<?PHP	} ?>
	</script>
	<p align="center" class="annot_std_it">Retour au dossier de relev&eacute; dans 2 secondes...</p>
<?PHP
	}
	else
	{
		if (@$_SESSION["niv_log"] == 1)
			$td="view";
		else
			$td="edit";
		if ($typrel == 1)
			$url_ret = "rlic_maj.php?td=edit&crl=".$coderlp;
		else
			$url_ret = "rpdt_maj.php?td=edit&crp=".$coderlp;
?>
		<script type="text/javascript">
		window.setTimeout("location=('<?PHP echo $url_ret; ?>');",2000)
		</script>
		<p align="center" class="annot_std_it">Retour &agrave; la fiche relev&eacute; dans 2 secondes...</p>
<?PHP
	}
}
include_once("pied.php");
?>
