<?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/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_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 = stripslashes(strip_tags(trim(@$_POST["titre"])));
$td = strip_tags(trim(@$_POST["td"]));

$datecrea = datefrang(strip_tags(trim(@$_POST["datecreaf"])));
$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$codecli = str_majuscules(strip_tags(trim(@$_POST["codecli"])));
$codeage = str_majuscules(strip_tags(trim(@$_POST["codeage"])));
$rnum = str_majuscules(strip_tags(trim(@$_POST["rnum"])));
$num = str_majuscules(strip_tags(trim(@$_POST["num"])));
$nota = str_escape(trim(@$_POST["nota"]));

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

$retourliste = TRUE;

if (strip_tags(trim(@$_POST["enrimp"])) == "Enregistrer & Imprimer")
{
	$imprim = TRUE;
	$retourliste = FALSE;
}

switch ($td)
{
	// Ajout de la fiche Relevé
	case "add" :
		$coder = crecode("R",TRUE);
	
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "INSERT INTO " .TBREL;
		$rech.= " (rel_datecrea,rel_codecrea,rel_datemod,rel_codemod,rel_code,rel_codecli,rel_codeage,";
		$rech.= "rel_rnum,rel_num,rel_nota)";
		$rech.= " VALUES";
		$rech.= " ('$datecrea','$codeuser','$datea','$codeuser','$coder','$codecli','$codeage',";
		$rech.= "'$rnum','$num','$nota')";
		$rs = mysql_query($rech) or die(mysql_error());
	
		// MAJ de la fiche Client (pour rendre l'agence ayant effectué le relevé "propriétaire" du client)
		$rech2 = "UPDATE `" .TBCLI. "` SET `cli_datemod`='".$datea."', `cli_codemod`='".$codeuser."', `cli_prop`='".$codeage."', `cli_type`='2'  WHERE `cli_code` = '".$codecli."'";
		$rs2 = mysql_query($rech2) or die(mysql_error());
	
	
		mysql_close($conn);
	
		$msg = "Fiche enregistrée";
		
		// Envoi d'email vers le client
		envoi_mail("","Création de votre dossier de relevé de parc","rel","cre","xxx","cli",$coder,"","",FALSE);
		// Envoi d'email vers l'agence
		if (@$_SESSION["niv_log"] <> 4)
			envoi_mail("","Création de dossier relevé de parc","rel","cre","xxx","age",$coder,"","",FALSE);
		// Envoi d'email vers le centre de gestion
		envoi_mail("","Création de dossier de relevé de parc","rel","cre","xxx","cdg",$coder,"","",TRUE);
		break;
		
	// MAJ de la fiche relevé
	case "edit" :
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "UPDATE `" .TBREL;
		$rech .= "` SET `rel_datemod`='".$datea."', `rel_codemod`='".$codeuser."',`rel_code`='".$code."', `rel_codecli`='".$codecli."', `rel_codeage`='".$codeage."',";
		$rech .=" `rel_rnum`='".$rnum."', `rel_num`='".$num."', `rel_nota`='".$nota."'";
		$rech .=" WHERE `rel_code` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());
		mysql_close($conn);
	
		$msg = $txt_fiche_mod;
	
		// Envoi d'email vers le client
		envoi_mail("","Modification de votre dossier de relevé de parc","rel","mod","xxx","cli",$code,"","",FALSE);
		// Envoi d'email vers l'agence
		if (@$_SESSION["niv_log"] <> 4)
			envoi_mail("","Modification de dossier de relevé de parc","rel","mod","xxx","age",$code,"","",FALSE);
		// Envoi d'email vers le centre de gestion
		envoi_mail("","Modification de dossier de relevé de parc","rel","mod","xxx","cdg",$code,"","",TRUE);
		break;
		
	// Suppression de la fiche Relevé
	case "del" :
		$ok_del = TRUE;
		
		$exist_rel_in_rlic = exist_rel_in_rlic($code);
		if ($exist_rel_in_rlic == TRUE)
		{
			$ok_del = FALSE;
			$retourliste = FALSE;
			$msg = "Ce dossier relevé contient des fiches de relevés licences. Supprimez-les.";
		}
		
		$exist_rel_in_rpdt = exist_rel_in_rpdt($code);
		if ($exist_rel_in_rpdt == TRUE)
		{
			$ok_del = FALSE;
			$retourliste = FALSE;
			if ($msg == "")
				$msg = "Ce dossier relevé contient des fiches de relevés matériels. Supprimez-les.";
			else
				$msg .= "<br><br>Ce dossier relevé contient des fiches de relevés matériels. Supprimez-les.";
		}
		
		if ($ok_del == TRUE)
		{
			// SUPPRESSION de la fiche Relevé
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "DELETE FROM `" .TBREL. "` WHERE `rel_code` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());

			// Suppression des tâches rattachées à ce relevé
			$rech = "DELETE FROM `" .TBTAC. "` WHERE `tac_coderat` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());

			mysql_close($conn);

			// Remise à niveau type client
			upd_nivcli($codecli,TRUE);

			$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; ?> : Fiche Dossier Relev&eacute;</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("../inc/entete.inc.php"); 
?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<p align="center">&nbsp;</p>
<p align="center">
<?PHP if ($retourliste==FALSE)
	echo '<span class="alerte1">';
else
	echo '<span class="annot_std_gras">';
?>
<?PHP echo $msg ?>
</span>
</p>

<?PHP if ($retourliste == FALSE) 
{ 
	if ($imprim == TRUE)
	{
		if ($td == "add")
			$urlimp = "rel_pdf.php?code=".$codeco;
		else
			$urlimp = "rel_pdf.php?code=".$code;
?>
	<p align="center">
    <a href="javascript:;" onClick="lanceimp('<?PHP echo $urlimp; ?>');")><img src="../img/print.png" title="<?PHP echo $alt_impcocl; ?>" border="0" align="absmiddle"></a> 
		<a href="javascript:;" onClick="lanceimp('<?PHP echo $urlimp; ?>');")> <?PHP echo $alt_impcocl; ?> </a></div>
	</p>
<?PHP
	}
	aff_ret_idx_man($td,"rel_lst.php",$txt_lnk_ret_liste,TRUE);
} 
else 
	aff_ret_lst_auto("rel_lst.php",2000);
include_once("pied.php");
?>
