<?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");

include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_co.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_mque.inc.php");
include_once("../inc/fic_ot.inc.php");
include_once("../inc/fic_ret.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_statut.inc.php");
include_once("../inc/fic_tec.inc.php");
include_once("../inc/fic_txt.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_time.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.inc.php");
include_once("../inc/func_mail.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"]));
$tdo = strip_tags(trim(@$_POST["tdo"]));
$tx = strip_tags(trim(@$_POST["tx"]));
$ce = strip_tags(trim(@$_POST["ce"]));

$coderetp = strip_tags(trim(@$_POST["coderetp"]));
$dateret = datefrang(strip_tags(trim(@$_POST["dateretf"])));
$typeope = strip_tags(trim(@$_POST["typeope"]));
$avoir = strip_tags(trim(@$_POST["avoir"]));
$ns = strip_tags(trim(@$_POST["ns"]));
$raison = strip_tags(trim(@$_POST["raison"]));
$fait = strip_tags(trim(@$_POST["fait"]));
$codeme = strip_tags(trim(@$_POST["codeme"]));
$bord = strip_tags(trim(@$_POST["bord"]));
$clos = strip_tags(trim(@$_POST["clos"]));
$clore=0;
if ($clos=="ok")
	$clore=1;

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

$msg="";
$retourliste = TRUE;

switch ($td)
{
	// Ajout de la fiche traitement de retour
	case "add" :
		if (exist_enr_retr($coderetp,TRUE)==FALSE)
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech2 = "INSERT INTO " .TBRETR;
			$rech2.= " (retr_coderetp,retr_dateret,retr_typeope,retr_ns,retr_avoir,retr_raison,retr_fait,retr_codeme,retr_bord,retr_clos)";
			$rech2.= " VALUES";
			$rech2.= " ('$coderetp','$dateret','$typeope','$ns','$avoir','$raison','$fait','$codeme','$bord','$clore')";
			$rs2 = mysql_query($rech2) or die(mysql_error());

			// Si clot, on met à jour la fiche produit correspondante
			if ($clore==1)
			{
				$rech = "UPDATE `".TBRETP. "` SET `retp_dateret`='".$dateret."' ";
				$rech .=" WHERE `retp_code` = '".$coderetp."'";
				$rs = mysql_query($rech) or die(mysql_error());
			}
			mysql_close($conn);

			$msg = "Fiche enregistr&eacute;e";
		}
		else
		{
			$msg = "Demande d&eacute;j&agrave; effectu&eacute;e pour ce produit!";
			$retourliste = FALSE;
		}

		if (($retourliste == TRUE) AND ($clore==1))
		{
			// Envoi d'email vers le webmaster
			envoi_mailwm("","Retour traité","retr","cre","xxx","age",$coderetp,"","");
			// Envoi d'email vers l'agence (Si pas créé par une agence)
			if ((@$_SESSION["niv_log"] <> 4) AND (@$_SESSION["niv_log"] <> 3))
				envoi_mail("","Retour traité","retr","cre","xxx","age",$coderetp,"","",FALSE);
			// Envoi d'email vers le centre de gestion (Si pas créé par le CDG)
			if (@$_SESSION["niv_log"] <> 5)
				envoi_mail("","Retour traité","retr","cre","xxx","cdg",$coderetp,"","",FALSE);
		}
		break;
		
	// MAJ de la fiche traitement de retour
	case "edit" :
		
		$maj_retr_ok=TRUE;

		if (clos_retr($coderetp,TRUE)==TRUE)
		{
			$maj_retr_ok=FALSE;
			$msg = "Cette fiche est d&eacute;j&agrave; close.";
			$retourliste = FALSE;
		}
		
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		if ($maj_retr_ok==TRUE)
		{
			$rech = "UPDATE `" .TBRETR. "` SET";
			$rech .=" `retr_dateret`='".$dateret."', `retr_typeope`='".$typeope."',";
			$rech .=" `retr_ns`='".$ns."', `retr_avoir`='".$avoir."', `retr_raison`='".$raison."',";
			$rech .=" `retr_fait`='".$fait."', `retr_codeme`='".$codeme."', ";
			$rech .=" `retr_bord`='".$bord."', `retr_clos`='".$clore."'";
			$rech .=" WHERE `retr_coderetp` = '".$coderetp."'";
			$rs = mysql_query($rech) or die(mysql_error());
					
			// Si clot, on met à jour la fiche produit correspondante
			if ($clore==1)
			{
				$rech = "UPDATE `".TBRETP. "` SET `retp_dateret`='".$dateret."' ";
				$rech .=" WHERE `retp_code` = '".$coderetp."'";
				$rs = mysql_query($rech) or die(mysql_error());
			}

			$msg = "Fiche mise &agrave; jour.";
		}
	
		mysql_close($conn);
	
		if (($retourliste == TRUE) AND ($clore==1))
		{
			// Envoi d'email vers le webmaster
			envoi_mailwm("","Retour traité","retr","cre","xxx","age",$coderetp,"","");
			// Envoi d'email vers l'agence (Si pas créé par une agence)
			if ((@$_SESSION["niv_log"] <> 4) AND (@$_SESSION["niv_log"] <> 3))
				envoi_mail("","Retour traité","retr","cre","xxx","age",$coderetp,"","",FALSE);
			// Envoi d'email vers le centre de gestion (Si pas créé par le CDG)
			if (@$_SESSION["niv_log"] <> 5)
				envoi_mail("","Retour traité","retr","cre","xxx","cdg",$coderetp,"","",FALSE);
		}
		break;		


	// Suppression de la fiche traitement de retour
	case "del" :
		if (clos_retr($code,TRUE)==FALSE)
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "DELETE FROM `" .TBRETR. "` WHERE `retr_coderetp` = '$coderetp'";
			$rs = mysql_query($rech) or die(mysql_error());

			// Si clot, on met à jour la fiche produit correspondante
			if ($clore==1)
			{
				$rech = "UPDATE `".TBRETP. "` SET `retp_dateret`='0000-00-00' ";
				$rech .=" WHERE `retp_code` = '".$coderetp."'";
				$rs = mysql_query($rech) or die(mysql_error());
			}
			$msg = $txt_fiche_del;
		}
		else
		{
			$msg = "Fiche d&eacute;j&agrave; close.";
			$retourliste = FALSE;
		}
		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 Traitement de Retour</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 
if ($tx<>"e")
	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>

<br>
<br>
<?PHP
if ($tx=="e") {
cre_js_pdte($tdo,$ce,"window.opener."); 
?>
<script type="text/javascript">
<!--
updatenv();
self.close('fscomp');
//-->
</script>
<?PHP
} else
	aff_ret_lst_auto("retr_lst.php?ord=DES&tri=Date",2000);
if ($tx<>"e")
	include_once("pied.php");
?>
