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

Version 0.9.7

Copyright (C) 2006-2007  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_do.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_ot.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 = strip_tags(trim(@$_POST["titre"]));
$tx = strip_tags(trim(@$_POST["tx"]));
$td = strip_tags(trim(@$_POST["td"]));

$datedde = datefrang(strip_tags(trim(@$_POST["dateddef"])));
$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$codeint = str_majuscules(strip_tags(trim(@$_POST["codeint"])));
$codest = str_majuscules(strip_tags(trim(@$_POST["codest"])));
$codestatut = str_majuscules(strip_tags(trim(@$_POST["codestatut"])));
$codelieu = str_majuscules(strip_tags(trim(@$_POST["codelieu"])));
$urg = strip_tags(trim(@$_POST["urgence"]));
$daterdvf = strip_tags(trim(@$_POST["daterdvf"]));
if ($daterdvf <> "")
	$daterdv = datefrang($daterdvf);
else
	$daterdv = "";
$hrdv = strip_tags(trim(@$_POST["hrdv"]));
if ($hrdv<>"")
	$hrdv.=":00";
$mat = str_escape(trim(@$_POST["mat"]));
$pbm = str_escape(trim(@$_POST["pbm"]));
$indexgar = strip_tags(trim(@$_POST["gar"]));
if ($indexgar == "0")
	$gar = 0;
else
	$gar = 1;

$fgar = strip_tags(trim(@$_POST["fgar"]));
if ($fgar == "") // Si pas de référence facture saisie, pas sous garantie
	$gar=0;

$codetec = str_majuscules(strip_tags(trim(@$_POST["codetec"])));
$duree = formateheure(strip_tags(trim(@$_POST["duree"])));
$durees = heure_sec($duree);

$fait = str_escape(trim(@$_POST["fait"]));
$dateprevf = strip_tags(trim(@$_POST["dateprevf"]));
if ($dateprevf <> "")
	$dateprev = datefrang($dateprevf);
else
	$dateprev = "";

$datefinf = strip_tags(trim(@$_POST["datefinf"]));
if ($datefinf <> "")
{
	$datefin = datefrang($datefinf);
	$codestatut="CLOT"; // S'il existe une date de fin, c'est que le dossier est clôt
}
else
	$datefin = "";

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

$msg="";
$retourliste = TRUE;

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


switch ($td)
{
	// Ajout de la fiche Ordre
	case "add" :
		$codeot = crecode("OR");
	
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "SELECT * FROM " .TBOT. " WHERE `ot_code` = '$codeot'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 0)
		{
			$rech2 = "INSERT INTO " .TBOT
			. " (ot_datecrea,ot_codecrea,ot_datemod,ot_codemod,ot_datedde,ot_code,ot_codeint,ot_codest,ot_codestatut,ot_codelieu,ot_urg,ot_daterdv,ot_hrdv,ot_mat,ot_pbm,ot_gar,ot_fgar,ot_codetec,ot_durees,ot_fait,ot_dateprev,ot_datefin)"
			. " VALUES"
			. " ('$datea','$codeuser','$datea','$codeuser','$datedde','$codeot','$codeint','$codest','$codestatut','$codelieu','$urg','$daterdv','$hrdv','$mat','$pbm','$gar','$fgar','-1','$durees','$fait','$dateprev','$datefin')";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			$msg = "Ordre enregistré";
		}
		else
		{
			$msg = "Ce code existe déjà !";
			$retourliste = FALSE;
			$imprim = FALSE;
		}
	
		mysql_free_result($rs);
		mysql_close($conn);
	
		if ($retourliste == TRUE)
		{
			// Envoi d'email vers le sous-traitant
			envoi_mail("Nouveau bon de travail (".$codeot.")","ot","cre","xxx","st",$codeot,"","",TRUE);
			// Envoi d'email vers l'agence (Si pas créé par l'agence)
			if ((@$_SESSION["niv_log"] <> 4) AND (@$_SESSION["niv_log"] <> 3))
				envoi_mail("Nouveau bon de travail (".$codeot.")","ot","cre","xxx","age",$codeot,"","",FALSE);
			// Envoi d'email vers le centre de gestion (Si pas créé par le CDG)
			if (@$_SESSION["niv_log"] <> 5)
				envoi_mail("Nouveau bon de travail (".$codeot.")","ot","cre","xxx","cdg",$codeot,"","",FALSE);
		}
		break;
		
	// MAJ de la fiche Ordre
	case "edit" :
	
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "UPDATE `" .TBOT. "` SET `ot_datemod`='".$datea."', `ot_codemod`='".$codeuser."',`ot_daterdv`='".$daterdv."',`ot_hrdv`='".$hrdv."', `ot_codest`='".$codest."', `ot_codestatut`='".$codestatut."',";
		$rech .=" `ot_codelieu`='".$codelieu."', `ot_urg`='".$urg."', `ot_mat`='".$mat."', `ot_pbm`='".$pbm."',  `ot_codetec`='".$codetec."',`ot_durees`='".$durees."', `ot_fait`='".$fait."', `ot_dateprev`='".$dateprev."', `ot_datefin`='".$datefin."',";
		$rech .=" `ot_gar`='".$gar."', `ot_fgar`='".$fgar."'";
		$rech .=" WHERE `ot_code` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());
	
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	
		$msg = "Ordre mis &agrave; jour.";
		
		if ($codestatut <> "CLOT")
		{
			// Si l'intervention est terminée, on prévient l'agence et le centre de gestion
			if ($dateprevf <> "")
			{
				// On prévient l'agence (si pas saisi par l'agence)
				if ((@$_SESSION["niv_log"] <> 4) AND (@$_SESSION["niv_log"] <> 3))
					envoi_mail("Bon de travail ".$code." exécuté","ot","fait","xxx","age",$code,"","",TRUE);
				// Envoi d'email vers le centre de gestion (Si pas édité par le CDG)
				if (@$_SESSION["niv_log"] <> 5)
					envoi_mail("Bon de travail ".$code." exécuté","ot","fait","xxx","cdg",$code,"","",FALSE);
			}
		}
		else
		{
			// Si l'intervention est terminée, on prévient l'agence et le centre de gestion
			if ($dateprevf <> "")
			{
				// On prévient l'agence (si pas saisi par l'agence)
				if ((@$_SESSION["niv_log"] <> 4) AND (@$_SESSION["niv_log"] <> 3))
					envoi_mail("Bon de travail ".$code." clôt","ot","clot","xxx","age",$code,"","",TRUE);
				// On prévient le centre de gestion (si pas saisi par lui)
				if (@$_SESSION["niv_log"] <> 5)
					envoi_mail("Bon de travail ".$code." clôt","ot","clot","xxx","cdg",$code,"","",FALSE);
			}
		}
		break;		


	// Suppression de la fiche Ordre
	case "del" :
		if ($codestatut <> "CLOT")
		{
			// Envoi d'email vers le sous-traitant
			envoi_mail("Annulation bon de travail ".$code,"ot","del","xxx","st",$code,"","",FALSE);
			// Envoi d'email vers le centre de geston
			if (@$_SESSION["niv_log"] <> 5)
				envoi_mail("Annulation bon de travail ".$code,"ot","del","xxx","cdg",$code,"","",FALSE);

			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "DELETE FROM `" .TBOT. "` WHERE `ot_code` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
			mysql_close($conn);
	
			// Suppression du détail horaire
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
	
			$rech = "DELETE FROM `" .TBOTH. "` WHERE `oth_codeot` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
	
			mysql_close($conn);
	
			$msg = "Ordre supprim&eacute;";
		}
		else
		{
			$msg = "Impossible de supprimer un ordre clôt.";
			$retourliste = FALSE;
			$imprim = 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 - ESPACE PRIVE : Fiche Ordre Sous-traitant</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>

<!-- Scripts Javascript pour contrôle de champs -->
<script	type="text/javascript" src="../jscript/verif_field.js"></script>

</head>
<body>
<?php 
if ($tx == "m") 
	include_once("entete.inc.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>
<br>
<br>
<?PHP if ($retourliste == FALSE) 
{ 
	if ($imprim == TRUE)
	{
		if ($td == "add")
		{
			$urlimp1 = "ot_pdf.php?co=".$codeot;
		}
		else
		{
			$urlimp1 = "ot_pdf.php?co=".$code;
		}
?>
		<p align="center">
		<a href="javascript:;" onClick="lanceimp('<?PHP echo $urlimp1; ?>');")><img src="../img/print.gif" title="Imprimer le bon" width="22" height="22" border="0" align="absmiddle"></a> 
			<a href="javascript:;" onClick="lanceimp('<?PHP echo $urlimp1; ?>');")> Imprimer le bon</a></div>
		</p>
<?PHP
		if ($surplus <> 0)
		{
			echo "<br><br>";
			echo "Que souhaitez-vous faire ?";
		}

	}
?>
	<br>
	<br>
	<table width="100%"  border="0">
	  <tr>
		<td width="3%">&nbsp;</td>
		<td width="44%"><div align="right"><a href="ot_lst.php?tx=<?PHP echo $tx; if ($tx=="i") echo "&ci=".$codeint; ?>&ord=DES&tri=Code"><img src="../img/liste.gif" width="33" height="34" border="0" align="absmiddle"></a> <a href="ot_lst.php?tx=<?PHP echo $tx; if ($tx=="i") echo "&ci=".$codeint; ?>&ord=DES&tri=Code" class="annot_gras_it">Retour &agrave; la liste</a></div></td>
		<td width="7%">&nbsp;</td>
		<td width="43%">
		<?PHP if ($tx=="m") { ?>
		<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>
		<?PHP } else { ?>
		&nbsp;
		<?PHP } ?>
		</td>
		<td width="3%">&nbsp;</td>
	  </tr>
	</table>
<?PHP 
} 
else 
{
	if ($tx=="i")
	{
?>
	<script type="text/javascript">
	verif_ot(<?PHP echo nb_ot($ci); ?>,<?PHP echo nb_ot_actifs($ci); ?>);
	</script>
<?PHP
	}
?>
<p align="center" class="annot_std_it">Retour &agrave; la liste dans 2 secondes...</p>
<script type="text/javascript">
window.setTimeout("location=('ot_lst.php?tx=<?PHP echo $tx; ?><?PHP if ($tx=="i") echo "&ci=".$codeint; ?>&ord=DES&tri=Code');",2000)
</script>
<?PHP		
}
if ($tx == "m") 
	include_once("pied.php");
?>
