<?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_dos.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_log.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_time.inc.php");
include_once("../inc/func_txt.inc.php");
Archivelog(@$_SESSION["code_log"]);

// Variables Formulaire
$titre = stripslashes(strip_tags(trim(@$_POST["titre"])));
$td = strip_tags(trim(@$_POST["td"]));
$codedos = strip_tags(trim(@$_POST["codedos"]));
$iddosd = intval(strip_tags(trim(@$_POST["nd"])));

$typea = str_majuscules(strip_tags(trim(@$_POST["typea"])));
$clore = strip_tags(trim(@$_POST["clore"]));
$clos=0;
if ($clore=="clos")
	$clos=1;
$dateact = datefrang(strip_tags(trim(@$_POST["dateactf"])));
$heured = strip_tags(trim(@$_POST["heured"]));
if ($heured<>"")
	$heured.=":00";
$heuref = strip_tags(trim(@$_POST["heuref"]));
if ($heuref<>"")
	$heuref.=":00";
$acodemt = strip_tags(trim(@$_POST["a_mt"]));
$aheured = strip_tags(trim(@$_POST["a_heured"]));
$aheured_full = $aheured.":00";
$aheuref = strip_tags(trim(@$_POST["a_heuref"]));
$aheuref_full = $aheuref.":00";
$akm = strip_tags(trim(@$_POST["a_km"]));
$acd = strip_tags(trim(@$_POST["a_cd"]));
$afa = strip_tags(trim(@$_POST["a_fa"]));
$afad = strip_tags(trim(@$_POST["a_fad"]));
$rcodemt = strip_tags(trim(@$_POST["r_mt"]));
$rheured = strip_tags(trim(@$_POST["r_heured"]));
$rheured_full = $rheured.":00";
$rheuref = strip_tags(trim(@$_POST["r_heuref"]));
$rheuref_full = $rheuref.":00";
$rkm = strip_tags(trim(@$_POST["r_km"]));
$rcd = strip_tags(trim(@$_POST["r_cd"]));
$rfa = strip_tags(trim(@$_POST["r_fa"]));
$rfad = strip_tags(trim(@$_POST["r_fad"]));
$obj = str_escape(trim(@$_POST["obj"]));
$res = str_escape(trim(@$_POST["res"]));
$concl = str_escape(trim(@$_POST["concl"]));
$age=$_POST["codeage"];
$tab_cage=$_POST["cage"];
$tab_codetec=$_POST["codetec"];
$cli=$_POST["codecli"];
$tab_ccli=$_POST["ccli"];
$mque=$_POST["mque"];
$tab_cmque=$_POST["cmque"];
$do=$_POST["do"];
$tab_cdo=$_POST["cdo"];
$st=$_POST["st"];
$tab_cst=$_POST["cst"];
$codestatut = str_majuscules(strip_tags(trim(@$_POST["codestatut"])));
$urg = strip_tags(trim(@$_POST["urgence"]));
$proba = strip_tags(trim(@$_POST["proba"]));
$ave = strip_tags(trim(@$_POST["ave"]));

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

$retourliste = TRUE;

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

switch ($td)
{
	// Ajout de la fiche Détail
	case "add" :
		$rech = "SELECT * FROM " .TBDOSD. " WHERE dosd_codedos = '$codedos' AND dosd_id = '$iddosd'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 0)
		{
			$rech2 = "INSERT INTO " .TBDOSD;
			$rech2.= " (dosd_codedos,dosd_codedosta,dosd_dateact,dosd_heured,dosd_heuref,dosd_obj,dosd_res,dosd_concl,dosd_clos,";
			$rech2.= "dosd_acodemt,dosd_aheured,dosd_aheuref,dosd_akm,dosd_acd,dosd_afrais,dosd_afraisd,";
			$rech2.= "dosd_rcodemt,dosd_rheured,dosd_rheuref,dosd_rkm,dosd_rcd,dosd_rfrais,dosd_rfraisd) VALUES";
			$rech2.= " ('$codedos','$typea','$dateact','$heured','$heuref','$obj','$res','$concl','$clos',";
			$rech2.= " '$acodemt','$aheured_full','$aheuref_full','$akm','$acd','$afa','$afad',";
			$rech2.= " '$rcodemt','$rheured_full','$rheuref_full','$rkm','$rcd','$rfa','$rfad')";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			
			$iddosd=mysql_insert_id();

			// MAJ dossier
			if (($urg<>"") OR ($codestatut<>"") OR ($proba<>"") OR ($ave<>""))
			{
				$rech = "UPDATE `" .TBDOS. "` SET ";
				$rech.= "`dos_codemod`='".$codeuser."'";
				if ($urg<>"")
					$rech.=",`dos_urg`='".$urg."'";
				if ($codestatut<>"")
					$rech.=",`dos_codedoss`='".$codestatut."'";
				if ($proba<>"")
					$rech.=",`dos_proba`='".$proba."'";
				if ($ave<>"")
					$rech.=",`dos_avance`='".$ave."'";
				$rech .=" WHERE `dos_code` = '".$codedos."'";
				$rs = mysql_query($rech) or die(mysql_error());
			}
			
			//...MAJ des intervenants agences
			if (count($tab_cage)>0)
			{
				foreach($tab_cage as $value)
				{
					if ($value<>"")
					{
						$rech3 = "INSERT INTO " .TBDOSDI;
						$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
						$rech3 .= " ('$iddosd','0','$value','$age')";
						$rs3 = mysql_query($rech3) or die(mysql_error());
					}
				}
			}
	
			//...MAJ des intervenants techniciens
			if (count($tab_codetec)>0)
			{
				foreach($tab_codetec as $value)
				{
					// Récupération code technicien
					if ($value<>"")
					{
						$tec_code="";
						$rech2="SELECT tec_code FROM ".TBTEC." WHERE `tec_id`='".$value."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if (mysql_num_rows($rs2)>0)
						{
							if ($row2 = mysql_fetch_array($rs2))
								extract($row2);
						}
						$rech3 = "INSERT INTO " .TBDOSDI;
						$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
						$rech3 .= " ('$iddosd','1','$value','$tec_code')";
						$rs3 = mysql_query($rech3) or die(mysql_error());
					}
				}
			}
	
			//...MAJ des intervenants clients
			if (count($tab_ccli)>0)
			{
				foreach($tab_ccli as $value)
				{
					if ($value<>"")
					{
						$typinter=2;
						$id=$value;
						if (substr($value,0,2)=="S_") // C'est un site
						{
							$typinter=6;
							$id=substr($value,2,strlen($value));
						}
						$rech3 = "INSERT INTO " .TBDOSDI;
						$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
						$rech3 .= " ('$iddosd','$typinter','$id','$cli')";
						$rs3 = mysql_query($rech3) or die(mysql_error());
					}
				}
			}
	
			//...MAJ des intervenants marques
			if (count($tab_cmque)>0)
			{
				foreach($tab_cmque as $value)
				{
					if ($value<>"")
					{
						$rech3 = "INSERT INTO " .TBDOSDI;
						$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
						$rech3 .= " ('$iddosd','3','$value','$mque')";
						$rs3 = mysql_query($rech3) or die(mysql_error());
					}
				}
			}
	
			//...MAJ des intervenants donneurs d'ordres
			if (count($tab_cdo)>0)
			{
				foreach($tab_cdo as $value)
				{
					if ($value<>"")
					{
						$rech3 = "INSERT INTO " .TBDOSDI;
						$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
						$rech3 .= " ('$iddosd','4','$value','$do')";
						$rs3 = mysql_query($rech3) or die(mysql_error());
					}
				}
			}
	
			//...MAJ des intervenants sous-traitants
			if (count($tab_cst)>0)
			{
				foreach($tab_cst as $value)
				{
					if ($value<>"")
					{
						$rech3 = "INSERT INTO " .TBDOSDI;
						$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
						$rech3 .= " ('$iddosd','5','$value','$st')";
						$rs3 = mysql_query($rech3) or die(mysql_error());
					}
				}
			}
	
			$msg = $txt_fiche_add;
		}
		else
		{
			$msg = $txt_fiche_addnotok;
			$retourliste = FALSE;
		}
		break;
		
	// MAJ de la fiche Détail
	case "edit" :
		$rech = "UPDATE `" .TBDOSD. "` SET `dosd_codedos`='".$codedos."', `dosd_codedosta`='".$typea."',";
		$rech.=" `dosd_dateact`='".$dateact."', `dosd_heured`='".$heured."', `dosd_heuref`='".$heuref."',";
		$rech.=" `dosd_obj`='".$obj."',`dosd_res`='".$res."',`dosd_concl`='".$concl."',`dosd_clos`='".$clos."',";
		$rech.=" `dosd_acodemt`='".$acodemt."',`dosd_aheured`='".$aheured_full."',`dosd_aheuref`='".$aheuref_full."',";
		$rech.=" `dosd_akm`='".$akm."',`dosd_acd`='".$acd."',`dosd_afrais`='".$afa."', `dosd_afraisd`='".$afad."',";
		$rech.=" `dosd_rcodemt`='".$rcodemt."',`dosd_rheured`='".$rheured_full."',`dosd_rheuref`='".$rheuref_full."',";
		$rech.=" `dosd_rkm`='".$rkm."',`dosd_rcd`='".$rcd."',`dosd_rfrais`='".$rfa."', `dosd_rfraisd`='".$rfad."'";
		$rech.= " WHERE `dosd_codedos` = '".$codedos."' AND `dosd_id` = '".$iddosd."'";
		$rs = mysql_query($rech) or die(mysql_error());

		// MAJ dossier
		if (($urg<>"") OR ($codestatut<>"") OR ($proba<>"") OR ($ave<>""))
		{
			$rech = "UPDATE `" .TBDOS. "` SET ";
			$rech.= "`dos_codemod`='".$codeuser."'";
			if ($urg<>"")
				$rech.=",`dos_urg`='".$urg."'";
			if ($codestatut<>"")
				$rech.=",`dos_codedoss`='".$codestatut."'";
			if ($proba<>"")
				$rech.=",`dos_proba`='".$proba."'";
			if ($ave<>"")
				$rech.=",`dos_avance`='".$ave."'";
			$rech .=" WHERE `dos_code` = '".$codedos."'";
			$rs = mysql_query($rech) or die(mysql_error());
		}

		// MAJ des intervenants
		//... On supprime d'abord tous les intervenants
		$rech = "DELETE FROM `" .TBDOSDI. "` WHERE `dosdi_iddosd` = '$iddosd'";
		$rs = mysql_query($rech) or die(mysql_error());
		
		//...MAJ des intervenants agences
		if (count($tab_cage)>0)
		{
			foreach($tab_cage as $value)
			{
				if ($value<>"")
				{
					$rech3 = "INSERT INTO " .TBDOSDI;
					$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
					$rech3 .= " ('$iddosd','0','$value','$age')";
					$rs3 = mysql_query($rech3) or die(mysql_error());
				}
			}
		}

		//...MAJ des intervenants internes (techniciens)
		if (count($tab_codetec)>0)
		{
			foreach($tab_codetec as $value)
			{
				// Récupération code technicien
				if ($value<>"")
				{
					$tec_code="";
					$rech2="SELECT tec_code FROM ".TBTEC." WHERE `tec_id`='".$value."'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
					if (mysql_num_rows($rs2)>0)
					{
						if ($row2 = mysql_fetch_array($rs2))
							extract($row2);
					}
					$rech3 = "INSERT INTO " .TBDOSDI;
					$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
					$rech3 .= " ('$iddosd','1','$value','$tec_code')";
					$rs3 = mysql_query($rech3) or die(mysql_error());
				}
			}
		}

		//...MAJ des intervenants clients
		if (count($tab_ccli)>0)
		{
			foreach($tab_ccli as $value)
			{
				if ($value<>"")
				{
					$typinter=2;
					$id=$value;
					if (substr($value,0,2)=="S_") // C'est un site
					{
						$typinter=6;
						$id=substr($value,2,strlen($value));
					}
					$rech3 = "INSERT INTO " .TBDOSDI;
					$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
					$rech3 .= " ('$iddosd','$typinter','$id','$cli')";
					$rs3 = mysql_query($rech3) or die(mysql_error());
				}
			}
		}

		//...MAJ des intervenants marques
		if (count($tab_cmque)>0)
		{
			foreach($tab_cmque as $value)
			{
				if ($value<>"")
				{
					$rech3 = "INSERT INTO " .TBDOSDI;
					$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
					$rech3 .= " ('$iddosd','3','$value','$mque')";
					$rs3 = mysql_query($rech3) or die(mysql_error());
				}
			}
		}

		//...MAJ des intervenants donneurs d'ordres
		if (count($tab_cdo)>0)
		{
			foreach($tab_cdo as $value)
			{
				if ($value<>"")
				{
					$rech3 = "INSERT INTO " .TBDOSDI;
					$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
					$rech3 .= " ('$iddosd','4','$value','$do')";
					$rs3 = mysql_query($rech3) or die(mysql_error());
				}
			}
		}

		//...MAJ des intervenants sous-traitants
		if (count($tab_cst)>0)
		{
			foreach($tab_cst as $value)
			{
				if ($value<>"")
				{
					$rech3 = "INSERT INTO " .TBDOSDI;
					$rech3 .= " (dosdi_iddosd,dosdi_typinter,dosdi_idctact,dosdi_codeinter) VALUES";
					$rech3 .= " ('$iddosd','5','$value','$st')";
					$rs3 = mysql_query($rech3) or die(mysql_error());
				}
			}
		}

		$msg = $txt_fiche_mod;
		break;

	// Suppression de la fiche Détail
	case "del" :
		if ($clos==0)
		{
			$rech = "DELETE FROM `" .TBDOSD. "` WHERE `dosd_codedos` = '$codedos' AND `dosd_id` = '$iddosd'";
			$rs = mysql_query($rech) or die(mysql_error());
	
			// Suppression des intervenants
			$rech = "DELETE FROM `" .TBDOSDI. "` WHERE `dosdi_iddosd` = '$iddosd'";
			$rs = mysql_query($rech) or die(mysql_error());
	
			$msg = $txt_fiche_del;
		}
		else
		{
			$msg = $txt_fiche_delnotok;
			$retourliste = FALSE;
		}
		break;
		
	default :
		break;
}
mysql_close($conn);
?>
<!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; ?> : <?PHP echo $title_dosta_maj; ?></title>

<!-- Feuille de style pour les affichages courants -->
<link href="../css/smi.css" rel="stylesheet" type="text/css">

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

<!-- Scripts Javascript pour fenêtres -->
<script language="JavaScript" src="../jscript/fen.js"></script>

</head>
<body>

<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>


<!-- Fermeture temporisée -->
<?PHP 
$row=lit_enr_dos($codedos);
if (is_array($row))
	extract($row);
if (($td=="add") OR ($td=="del"))
	$td="edit";
cre_js_detindos($td,$codedos,"window.opener.");
?>
<script type="text/javascript">
<!--
var objet = window.opener.document.getElementById("pxr");
if (objet)
{
	objet.innerHTML="<?PHP echo format_num(px_revient_dos($codedos,TRUE),2); ?>";
}
<?PHP if ($codestatut<>"") { ?>
var objet=window.opener.document.getElementById("codestatut");
if (objet)
{
	objet.value='<?PHP echo $codestatut; ?>';
}
<?PHP }

if ($urg<>"") { ?>
var objet=window.opener.document.getElementById("urgence");
if (objet)
{
	objet.value='<?PHP echo $urg; ?>';
}
<?PHP }

if ($proba<>"") { ?>
var objet=window.opener.document.getElementById("proba");
if (objet)
{
	objet.value='<?PHP echo $proba; ?>';
	verif_pour_mere(window.opener.document.getElementById('proba').value,'nivproba','1');
}
<?PHP }

if ($ave<>"") { ?>
var objet=window.opener.document.getElementById("ave");
if (objet)
{
	objet.value='<?PHP echo $ave; ?>';
	verif_pour_mere(window.opener.document.getElementById('ave').value,'nivave','2');
}
<?PHP } ?>
var objet = window.opener.document.getElementById('chxdetdosd');
if (objet)
{
<?PHP if (exist_dosd_open_dos($codedos,TRUE)==TRUE) { ?>
	objet.style.display = 'none';
<?PHP } else { ?>
	objet.style.display = 'inline';
<?PHP } ?>
}
detindos();
self.close('fscomp');
//-->
</script>
<p align="center"><?PHP echo $txt_div_sinon; ?>, <a href="javascript:void(0)" onClick="javascript:; self.close('fscomp');"><?PHP echo $txt_lnk_clic_ici; ?></a> </p>

</body>
</html>
