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

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 require ("../inc/func.inc.php") ?>
<?php require ("../inc/func_date.inc.php") ?>
<?php require ("../inc/func_log.inc.php") ?>
<?php include ("../inc/func_txt.inc.php") ?>
<?php require ("../inc/fic.inc.php") ?>
<?php require_once ("../classes/xpm/smtp.php"); ?>
<?php require ("../inc/mail.inc.php") ?>
<?PHP
	Archivelog(@$HTTP_SESSION_VARS["code_log"]);
?>
<?php
	 	// Variables Formulaire
		$titre = strip_tags(trim(@$_POST["titre"]));
		$typdde = strip_tags(trim(@$_POST["typdde"]));

		$salarie = strip_tags(trim(@$_POST["salarie"]));
		if ($salarie == "Oui")
			$parti = FALSE;
		else
			$parti = TRUE;
		$dconges = strip_tags(trim(@$_POST["dconges"]));
		if ($dconges <> "")
			$dconges = datefrang($dconges);
		$fconges = strip_tags(trim(@$_POST["fconges"]));
		if ($fconges <> "")
			$fconges = datefrang($fconges);
		$code = strip_tags(trim(@$_POST["code"]));
		$pass = strip_tags(trim(@$_POST["pass"]));
		$civil = strip_tags(trim(@$_POST["civil"]));
		$prenom = ucwords(strtolower(strip_tags(trim(@$_POST["prenom"]))));
		$nom = str_majuscules(strip_tags(trim(@$_POST["nom"])));
		$adr1 = str_majuscules(strip_tags(trim(@$_POST["adr1"])));
		$adr2 = str_majuscules(strip_tags(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"]));
		$telp = strip_tags(trim(@$_POST["telp"]));
		$email = strtolower(strip_tags(trim(@$_POST["email"])));
		$nota = strip_tags(trim(@$_POST["nota"]));
		
		$codecrea = @$HTTP_SESSION_VARS["code_log"];

		$pass = cremdp(FALSE);
		$passcrypt = md5($pass);

		$retourliste = TRUE;

		if ($typdde == "add")
		{
			$codeo = crecode("O");

			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);

			// Ajout de la fiche Technicien
		
			$rech = "SELECT * FROM " .TBOPE. " WHERE ope_code = '$codeo'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) == 0)
			{
				$rech = "INSERT INTO " .TBOPE;
				$rech.= " (ope_datecrea,ope_codecrea,ope_datemod,ope_congd,ope_congf,ope_code,ope_pass,ope_civilite,ope_prenom,ope_nom,ope_adr1,ope_adr2,ope_dep,ope_ville,ope_pays,ope_telf,ope_telp,ope_email,ope_nota,ope_out,ope_dateout) VALUES";
				$rech.= " ('$datea','$codecrea','$datea','$dconges','$fconges','$codeo','$passcrypt','$civil','$prenom','$nom','$adr1','$adr2','$dep','$ville','$pays','$telf','$telp','$email','$nota','','')";
				$rs2 = mysql_query($rech) or die(mysql_error());

				// Ajout du compte pour connexion
				// Niveau par défaut : 2
				// Actif par défaut
					
				$log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP
				$rech = "INSERT INTO " .TBPW. "(pw_code,pw_pass,pw_level,pw_lastsuccess,pw_lastip,pw_actif) VALUES ('$codeo','$passcrypt','2','$dateheurea','$log_ip','1')";
				$rs2 = mysql_query($rech) or die(mysql_error());
			
				$msg = "Fiche enregistrée avec succès !";
			}
			else
			{
				$msg = "Ce code existe déjà !";
				$retourliste = FALSE;
			}
			// mysql_free_result($rs2);
			mysql_close($conn);

			if ($retourliste == TRUE)
			{
				// Envoi d'email vers l'opérateur
				envoi_mail("Création de votre compte opérateur","ope","cre","xxx","ope",$codeo,$pass,"",TRUE);
			}
		}
		
		if ($typdde == "edit")
		{
			if ($parti == TRUE) // Si l'opérateur n'est plus salarié, quelques contrôles sont nécessaires
			{
				// Plus besoin de dates de congés si licencié
				$dconges="";
				$fconges="";
			}

			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);

			// MAJ de la fiche Opérateur
			$rech = "UPDATE `" .TBOPE. "` SET `ope_datemod`='".$datea."', `ope_congd`='".$dconges."', `ope_congf`='".$fconges."', `ope_civilite`='".$civil."', `ope_prenom`='".$prenom."', `ope_nom`='".$nom."', `ope_adr1`='".$adr1."', `ope_adr2`='".$adr2."', `ope_ville`='".$ville."', `ope_pays`='".$pays."', `ope_telf`='".$telf."', `ope_telp`='".$telp."', `ope_email`='".$email."', `ope_nota`='".$nota."', `ope_out`='".$parti."', `ope_dateout`='".$datea."'  WHERE `ope_code` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());

			if ($parti == TRUE)
			{
				// Suppression du mot de passe rattaché
	
				$rech = "DELETE FROM `" .TBPW. "` WHERE `pw_code` = '$code'";
				$rs = mysql_query($rech) or die(mysql_error());
			}

			// mysql_free_result($rs);
			mysql_close($conn);

			$msg = "Fiche modifiée avec succès !";
		}

		if ($typdde == "del")
		{
			// SUPPRESSION de la fiche Opérateur
			
			$existeint = FALSE;

			// Vérifie si utilisé dans une fiche intervention
			
			if (exist_in_int_codecrea($code) == TRUE)
			{
				$existeint = TRUE;
				$msg = "Cet opérateur a déjà saisi des interventions (vous ne pouvez ne pouvez pas le supprimer).<br>Il suffit de le noter 'licencié'.";
				$retourliste = FALSE;
			}

			if  ($existeint == FALSE)
			{
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
	
				$rech = "DELETE FROM `" .TBOPE. "` WHERE `ope_code` = '$code'";
				$rs = mysql_query($rech) or die(mysql_error());

				// Suppression du mot de passe rattaché
	
				$rech = "DELETE FROM `" .TBPW. "` WHERE `pw_code` = '$code'";
				$rs = mysql_query($rech) or die(mysql_error());

				// mysql_free_result($rs);
				mysql_close($conn);

				$msg = "Fiche supprimée avec succès !";
				$retourliste = TRUE;
	
			}
		}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - ESPACE PRIVE : Fiche Op&eacute;rateur</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>

</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) 
{ 
	if (($typdde == "add") OR ($typdde == "edit"))
	{
?>
		<p align="center">
		<a href="javascript:history.back();">
		Retour &agrave; la saisie
		</a>
<?PHP
	}
?>
	<table width="100%"  border="0">
	  <tr>
		<td width="3%">&nbsp;</td>
		<td width="44%"><div align="right"><a href="ope_lst.php"><img src="../img/liste.gif" width="33" height="34" border="0" align="absmiddle"></a> <a href="ope_lst.php" 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="absmiddle"> </em><span class="annot_gras_it">Revenir &agrave; l'accueil</span> </a></td>
		<td width="3%">&nbsp;</td>
	  </tr>
	</table>
<?PHP
} 
else 
{ 
?>
	<p align="center" class="annot_std_it">Retour à la liste dans 2 secondes...</p>
	<script type="text/javascript">
	window.setTimeout("location=('ope_lst.php');",2000)
	</script>
<?PHP
} 
?>

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

