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

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
****************************************************************************************/

session_start(); 
?>
<?php if ((@$HTTP_SESSION_VARS["etat_log"] <> "connecte") or ((@$HTTP_SESSION_VARS["niv_log"] <> 9) AND (@$HTTP_SESSION_VARS["niv_log"] <> 5))) header("Location: ../index.php") ?>
<?php include ("../inc/config.inc.php") ?>
<?php include ("../inc/func.inc.php") ?>
<?php include ("../inc/func_log.inc.php") ?>
<?php include ("../inc/fic.inc.php") ?>
<?PHP
	Archivelog(@$HTTP_SESSION_VARS["code_log"]);
?>
<?php
		$retourliste = TRUE;

	 	// Variables Formulaire
		$titre = strip_tags(trim(@$_POST["titre"]));
		$typdde = strip_tags(trim(@$_POST["typdde"]));

		$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
		$desc = str_majuscules(strip_tags(trim(@$_POST["desc"])));

		$publique = strip_tags(trim(@$_POST["pub"]));
		if ($publique == "Oui")
			$pub = 1;
		else
			$pub = 0;
		$client = strip_tags(trim(@$_POST["client"]));
		if ($client == "Oui")
			$cli = 1;
		else
			$cli = 0;
		$age = strip_tags(trim(@$_POST["age"]));
		if ($age == "Oui")
			$age = 1;
		else
			$age = 0;
		$tec = strip_tags(trim(@$_POST["tec"]));
		if ($tec == "Oui")
			$tec = 1;
		else
			$tec = 0;
		$ope = strip_tags(trim(@$_POST["ope"]));
		if ($ope == "Oui")
			$ope = 1;
		else
			$ope = 0;
		$cge = strip_tags(trim(@$_POST["cge"]));
		if ($cge == "Oui")
			$cge = 1;
		else
			$cge = 0;

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

		if ($retourliste == TRUE)
		{
			if ($typdde == "add")
			{
				// Ajout de la fiche destinataire de demande
			
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rech = "SELECT * FROM " .TBDDED. " WHERE `dded_code` = '$code'";
				$rs = mysql_query($rech) or die(mysql_error());
				if (mysql_num_rows($rs) == 0)
				{
					$rech = "INSERT INTO " .TBDDED. " (dded_code,dded_desc,dded_pub,dded_cli,dded_age,dded_tec,dded_ope,dded_cge,dded_email)";
					$rech.= " VALUES ('$code','$desc','$pub','$cli','$age','$tec','$ope','$cge','$email')";
					$rs = mysql_query($rech) or die(mysql_error());
					$msg = "Fiche Ajoutée avec succès !";			
					$retourliste = TRUE;
				}
				else
				{
					$msg = "Ce code existe déjà !";
					$retourliste = FALSE;
				}
				mysql_close($conn);
			}
	
			if ($typdde == "edit")
			{
				if ($code <> "VA")
				{
					$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
		
					// MAJ de la fiche destinataire de demande
	
					$rech = "UPDATE `" .TBDDED. "` SET `dded_code`='".$code."', `dded_desc`='".$desc."', `dded_pub`='".$pub."',";
					$rech.= "  `dded_cli`='".$cli."', `dded_age`='".$age."',";
					$rech.= " `dded_tec`='".$tec."', `dded_ope`='".$ope."', `dded_cge`='".$cge."', `dded_email`='".$email."' WHERE `dded_code` = '".$code."'";
					$rs = mysql_query($rech) or die(mysql_error());
		
					mysql_close($conn);
	
					$msg = "Fiche modifiée avec succès !";
					$retourliste = TRUE;
				}
				else
				{
					$msg = "Vous ne pouvez pas modifier ce code.";
					$retourliste = FALSE;
				}
			}
	
			if ($typdde == "del")
			{
				if ($code <> "VA")
				{
					$existedde = FALSE;
					
					// Vérifie si utilisé dans une demande
					
					if (exist_dded_in_dde($code) == TRUE)
					{
						$existecocl = TRUE;
						$msg = "Ce type de demande a déjà été utilisé dans une demande.";
						$retourliste = FALSE;
					}
					
					if ($existedde == FALSE) // On ne peut pas supprimer un destinataire de demande déjà utilisé
					{
						$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);
			
						// SUPPRESSION de la fiche destinataire de demande
					
						$rech = "DELETE FROM `" .TBDDED. "` WHERE `dded_code` = '$code'";
						$rs = mysql_query($rech) or die(mysql_error());
		
						$msg = "Fiche supprimée avec succès !";			
						$retourliste = TRUE;
		
						mysql_close($conn);
					}
				}
				else
				{
					$msg = "Vous ne pouvez pas supprimer ce code.";
					$retourliste = FALSE;
				}
			}
		}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - ESPACE PRIVE : Fiche Destinataire de Demande</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../inc/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>

</head>
<body>

<?php 
	include_once ("../inc/entete.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) { ?>
	<table width="100%"  border="0">
	  <tr>
		<td width="3%">&nbsp;</td>
		<td width="44%"><div align="right"><a href="dded_lst.php?ta=m"><img src="../img/liste.gif" width="33" height="34" border="0" align="abddle"></a> <a href="dded_lst.php?ta=m" class="annot_gras_it">Retour &agrave; la liste</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="abddle"> </em><span class="annot_gras_it">Revenir &agrave; l'accueil</span> </a></td>
		<td width="3%">&nbsp;</td>
	  </tr>
	</table>
<?PHP } else { ?>
	<script type="text/javascript">
	window.setTimeout("location=('dded_lst.php');",2000)
	</script>
	<p align="center" class="annot_std_it">Retour à la liste dans 2 secondes...</p>
<?PHP } ?>

<?php 
	include_once ("pied.php")
?>

