<?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_hum.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_tac.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"]));

$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$datecrea = datefrang(strip_tags(trim(@$_POST["datecreaf"])));
$datefinp = datefrang(strip_tags(trim(@$_POST["datefinpf"])));
$urg = strip_tags(trim(@$_POST["urgence"]));
$titred = strip_tags(trim(@$_POST["titred"]));
$afaire = str_escape(trim(@$_POST["afaire"]));
$codecat = str_majuscules(strip_tags(trim(@$_POST["codecat"])));
$tdest = strip_tags(trim(@$_POST["typdest"]));
switch ($tdest)
{
	case -1 :
		switch (@$_SESSION["niv_log"])
		{
			case 3 :
				$typdest=1;
				break;
			case 4 :
				$typdest=2;
				break;
			case 5 :
				$typdest=5;
				break;
			case 9 :
				$typdest=6;
				break;
			case 10 :
				$typdest=4;
				break;
			case 11 :
				$typdest=3;
				break;
		}
		break;
	default :
		$typdest=$tdest;
		break;
}
$dest = str_majuscules(strip_tags(trim(@$_POST["dest"])));
switch ($typdest)
{
	case 5 :
		$dest="CGE";
		break;
	case 6 :
		$dest=$admin_code;
		break;
}
$typrat = strip_tags(trim(@$_POST["typrat"]));
$coderat = str_majuscules(strip_tags(trim(@$_POST["rat"])));
$ave = strip_tags(trim(@$_POST["ave"]));
$datefin = datefrang(strip_tags(trim(@$_POST["datefinf"])));

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

$msg="";
$retourliste = TRUE;

switch ($td)
{
	// Ajout de la fiche tāche
	case "add" :
		$codeta = crecode("TA",TRUE);
	
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "SELECT * FROM " .TBTAC. " WHERE `tac_code` = '$codeta'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 0)
		{
			$rech2 = "INSERT INTO " .TBTAC;
			$rech2.=" (tac_code, tac_titre, tac_afaire, tac_datecrea,tac_codecrea,tac_datemod,tac_codemod,";
			$rech2.="tac_typdest,tac_codedest,tac_typrat,tac_coderat,tac_datefinp,tac_urg,tac_codetacc,";
			$rech2.="tac_avance,tac_datefin)";
			$rech2.=" VALUES";
			$rech2.=" ('$codeta','$titred','$afaire','$datecrea','$codeuser','$datea','$codeuser',";
			$rech2.="'$typdest','$dest','$typrat','$coderat','$datefinp','$urg','$codecat','0','')";
			$rs2 = mysql_query($rech2) or die(mysql_error());
	
			$msg = $txt_fiche_add;
	
		}
		else
		{
			$msg = $txt_fiche_addnotok;
			$retourliste = FALSE;
		}
	
		mysql_free_result($rs);
		mysql_close($conn);

		if ($retourliste == TRUE)
		{
			// Envois des emails
			switch ($typdest)
			{
				// Technicien agence
				case 1 :
				case 3 :
					if (@$_SESSION["code_log"] <> $dest)
						envoi_mail("","Nouvelle tāche (".$codeta.")","tac","cre","xxx","tec",$codeta,"","",TRUE);
					break;
				// Agence
				case 2 :
					if (@$_SESSION["code_log"] <> $dest)
						envoi_mail("","Nouvelle tāche (".$codeta.")","tac","cre","xxx","age",$codeta,"","",TRUE);
					break;
				// Sous-traitant
				case 4 :
					if (@$_SESSION["code_log"] <> $dest)
						envoi_mail("","Nouvelle tāche (".$codeta.")","tac","cre","xxx","st",$codeta,"","",TRUE);
					break;
				// Centre de gestion
				case 5 :
					if (@$_SESSION["niv_log"] <> 5)
						envoi_mail("","Nouvelle tāche (".$codeta.")","tac","cre","xxx","cdg",$codeta,"","",TRUE);
					break;
			}
		}
		break;
		
	// MAJ de la fiche Tāche
	case "edit" :
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);

		$rech = "UPDATE `" .TBTAC. "` SET `tac_titre`='".$titred."', `tac_afaire`='".$afaire."',`tac_datemod`='".$datea."',";
		$rech.= "`tac_codemod`='".$codeuser."',`tac_typdest`='".$typdest."',`tac_codedest`='".$dest."',`tac_typrat`='".$typrat."',";
		$rech.="`tac_datefinp`='".$datefinp."',`tac_urg`='".$urg."',`tac_codetacc`='".$codecat."',`tac_avance`='".$ave."',";
		$rech.="`tac_datefin`='".$datefin."',`tac_coderat`='".$coderat."'";
		$rech .=" WHERE `tac_code` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());
				
		mysql_close($conn);

		$msg = $txt_fiche_mod;

		// Envois des emails
		switch ($typdest)
		{
			// Technicien agence
			case 1 :
			case 3 :
				if (@$_SESSION["code_log"] <> $dest)
					envoi_mail("","Modification tāche (".$code.")","tac","mod","xxx","tec",$code,"","",TRUE);
				break;
			// Agence
			case 2 :
				if (@$_SESSION["code_log"] <> $dest)
					envoi_mail("","Modification tāche (".$code.")","tac","mod","xxx","age",$code,"","",TRUE);
				break;
			// Sous-traitant
			case 4 :
				if (@$_SESSION["code_log"] <> $dest)
					envoi_mail("","Modification tāche (".$code.")","tac","mod","xxx","st",$code,"","",TRUE);
				break;
			// Centre de gestion
			case 5 :
				if (@$_SESSION["niv_log"] <> 5)
					envoi_mail("","Modification tāche (".$code.")","tac","mod","xxx","cdg",$code,"","",TRUE);
				break;
		}

		break;		


	// Suppression de la fiche Dossier
	case "del" :
		if ($datefin <> "")
		{
			// Envois des emails
			switch ($typdest)
			{
				// Technicien agence
				case 1 :
				case 3 :
					if (@$_SESSION["code_log"] <> $dest)
						envoi_mail("","Suppression tāche (".$code.")","tac","del","xxx","tec",$code,"","",TRUE);
					break;
				// Agence
				case 2 :
					if (@$_SESSION["code_log"] <> $dest)
						envoi_mail("","Suppression tāche (".$code.")","tac","del","xxx","age",$code,"","",TRUE);
					break;
				// Sous-traitant
				case 4 :
					if (@$_SESSION["code_log"] <> $dest)
						envoi_mail("","Suppression tāche (".$code.")","tac","del","xxx","st",$code,"","",TRUE);
					break;
				// Centre de gestion
				case 5 :
					if (@$_SESSION["niv_log"] <> 5)
						envoi_mail("","Suppression tāche (".$code.")","tac","del","xxx","cdg",$code,"","",TRUE);
					break;
			}
	
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "DELETE FROM `" .TBTAC. "` WHERE `tac_code` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
		
			mysql_close($conn);
	
			$msg = $txt_fiche_del;
		}
		else
		{
			$msg = $txt_fiche_delnotok;
			$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 Tāche</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>

<br>
<br>
<?PHP if ($retourliste == FALSE)
	aff_ret_idx_man($td,"tac_lst.php?ord=DES&tri=Code",$txt_lnk_ret_liste,TRUE);
else 
	aff_ret_lst_auto("tac_lst.php?ord=DES&tri=Code",2000);
include_once("pied.php");
?>
