<?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_loc.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_txt.inc.php");
Archivelog(@$_SESSION["code_log"]);

// Variables Formulaire
$titre = stripslashes(strip_tags(trim(@$_POST["titre"])));
$td = strip_tags(trim(@$_POST["td"]));
$codecoloc = strip_tags(trim(@$_POST["codecoloc"]));

$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$dd = datefrang(strip_tags(trim(@$_POST["dd"])));
$hd = strip_tags(trim(@$_POST["hd"])).":00";
$df = datefrang(strip_tags(trim(@$_POST["df"])));
$hf = strip_tags(trim(@$_POST["hf"])).":00";
$coderegc = str_majuscules(strip_tags(trim(@$_POST["coderegc"])));
$datedep = datefrang(strip_tags(trim(@$_POST["datedepf"])));
$heuredep = strip_tags(trim(@$_POST["hdep"])).":00";
$didx1 = strip_tags(trim(@$_POST["didx1"]));
$didx2 = strip_tags(trim(@$_POST["didx2"]));
$prispar = strip_tags(trim(@$_POST["prispar"]));
$dateret = datefrang(strip_tags(trim(@$_POST["dateretf"])));
$heureret = strip_tags(trim(@$_POST["hret"])).":00";
$ridx1 = strip_tags(trim(@$_POST["ridx1"]));
$ridx2 = strip_tags(trim(@$_POST["ridx2"]));
$retpar = strip_tags(trim(@$_POST["prispar"]));
$rcaut = strip_tags(trim(@$_POST["rcaut"]));
$reloc = strip_tags(trim(@$_POST["reloc"]));
$noted = str_escape(trim(@$_POST["noted"]));
$noter = str_escape(trim(@$_POST["noter"]));

$retourliste = TRUE;

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

switch ($td)
{
	// Ajout de la fiche Produit Location (réservation)
	case "add" :
		$rech = "SELECT * FROM " .TBPDTL. " WHERE pdtl_codecoloc = '$codecoloc' AND pdtl_codepdtloc = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 0)
		{
			if ($_SESSION["niv_log"]==3)
				$codeuser = @$_SESSION["code_log"];
			else
				$codeuser=strip_tags(trim(@$_POST["codecrea"]));
			
			$rech2 = "INSERT INTO " .TBPDTL;
			$rech2 .= " (pdtl_codecrea,pdtl_codemod,pdtl_codecoloc,pdtl_codepdtloc,pdtl_datedeb,pdtl_heuredeb,pdtl_datefin,pdtl_heurefin) VALUES";
			$rech2 .= " ('$codeuser','$codeuser','$codecoloc','$code','$dd','$hd','$df','$hf')";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			$lastid=mysql_insert_id();
			
			// Ajout du barême rattaché
			$rech2 = "INSERT INTO " .TBPDTLBL;
			$rech2 .= " (pdtlbl_idpdtl,pdtlbl_idbarloc,pdtlbl_qte,pdtlbl_pvht,pdtlbl_codetva,pdtlbl_caution,pdtlbl_tvacaut,";
			$rech2 .= "pdtlbl_avecass,pdtlbl_mtass,pdtlbl_codetvaass,pdtlbl_qconsi1,pdtlbl_pdtuconsi1,pdtlbl_puconsi1,";
			$rech2 .= "pdtlbl_qconsi2,pdtlbl_pdtuconsi2,pdtlbl_puconsi2) VALUES";
			$rech2 .= " ('$lastid','".$_SESSION["pdtlbl"][0]["idbarloc"]."','".$_SESSION["pdtlbl"][0]["qte"]."','".$_SESSION["pdtlbl"][0]["pvht"]."','".$_SESSION["pdtlbl"][0]["codetva"]."',";
			$rech2 .= "'".$_SESSION["pdtlbl"][0]["caution"]."','".$_SESSION["pdtlbl"][0]["tvacaut"]."','".$_SESSION["pdtlbl"][0]["avecass"]."',";
			$rech2 .= "'".$_SESSION["pdtlbl"][0]["mtass"]."','".$_SESSION["pdtlbl"][0]["codetvaass"]."','".$_SESSION["pdtlbl"][0]["qconsi1"]."','".$_SESSION["pdtlbl"][0]["pdtuconsi1"]."',";
			$rech2 .= "'".$_SESSION["pdtlbl"][0]["puconsi1"]."','".$_SESSION["pdtlbl"][0]["qconsi2"]."','".$_SESSION["pdtlbl"][0]["pdtuconsi2"]."','".$_SESSION["pdtlbl"][0]["puconsi2"]."')";
			$rs2 = mysql_query($rech2) or die(mysql_error());

			$msg = "Produit ajout&eacute;";
		}
		else
		{
			$msg = "Ce produit est d&eacute;j&agrave; saisi dans cette location !";
			$retourliste = FALSE;
		}
		mysql_free_result($rs);
		break;
		
	// MAJ de la fiche Produit Location (Réservation)
	case "edit" :
		$rech = "SELECT * FROM " .TBPDTL. " WHERE pdtl_codecoloc = '$codecoloc' AND pdtl_codepdtloc = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 1)
		{
			if ($row=mysql_fetch_assoc($rs))
			{
				extract($row);
				
				// On met à jour le barême rattaché
				$rech2 = "UPDATE `" .TBPDTLBL. "` SET `pdtlbl_avecass`='".$_SESSION["pdtlbl"][0]["avecass"]."'";
				$rech2 .= " WHERE `pdtlbl_idpdtl` = '".$pdtl_id."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());

				// On met à jour la fiche produit
				$codeuser = @$_SESSION["code_log"];
				
				$rech2 = "UPDATE `" .TBPDTL. "` SET `pdtl_codemod`='".$codeuser."'";
				$rech2 .= " WHERE `pdtl_id` = '".$pdtl_id."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
			}
		}

		$msg = "Produit mis &agrave; jour";
		break;

	// MAJ de la fiche Produit Location (Départ produit)
	case "dep" :
		if ($_SESSION["niv_log"]==3)
			$codeuser = @$_SESSION["code_log"];
		else
			$codeuser=strip_tags(trim(@$_POST["codedep"]));
		
		$rech = "UPDATE `" .TBPDTL. "` SET `pdtl_coderegc`='".$coderegc."', `pdtl_datedep`='".$datedep."',";
		$rech .= " `pdtl_heuredep`='".$heuredep."', `pdtl_didx1`='".$didx1."', `pdtl_didx2`='".$didx2."',";
		$rech .= " `pdtl_prispar`='".$prispar."', `pdtl_noted`='".$noted."', `pdtl_codedep`='".$codeuser."'";
		$rech .= " WHERE `pdtl_codecoloc` = '".$codecoloc."' AND `pdtl_codepdtloc` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());

		// On met à jour la fiche produit
		$rech = "UPDATE `" .TBPDTLOC. "` SET `pdtloc_louable`=0, `pdtloc_loue`=1";
		$rech .= " WHERE `pdtloc_code` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());

		$msg = "Produit mis &agrave; jour";
		break;

	// MAJ de la fiche Produit Location (Retour produit)
	case "ret" :
		$rech = "SELECT * FROM " .TBPDTL. " WHERE pdtl_codecoloc = '$codecoloc' AND pdtl_codepdtloc = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 1)
		{
			if ($row=mysql_fetch_assoc($rs))
			{
				extract($row);
				
				// Suppression de la pénalité location
				$rech2 = "DELETE FROM `" .TBPDTLPL. "` WHERE `pdtlpl_idpdtl` = '$pdtl_id'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				// Injection de la pénalité location
				if ($_SESSION["pdtlpl"][0]["codepen"]<>"-1")
				{
					$pdtlpl_codepen=$_SESSION["pdtlpl"][0]["codepen"];
					$pdtlpl_valeur=$_SESSION["pdtlpl"][0]["valeur"];
					$pdtlpl_codetva=$_SESSION["pdtlpl"][0]["tva"];
					$rech2 = "INSERT INTO " .TBPDTLPL;
					$rech2 .= " (pdtlpl_idpdtl,pdtlpl_codepen,pdtlpl_valeur,pdtlpl_codetva) VALUES";
					$rech2 .= " ('$pdtl_id','$pdtlpl_codepen','$pdtlpl_valeur','$pdtlpl_codetva')";
					$rs2 = mysql_query($rech2) or die(mysql_error());
				}

				// Suppression du monta consommables
				$rech2 = "DELETE FROM `" .TBPDTLCL. "` WHERE `pdtlcl_idpdtl` = '$pdtl_id'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				// Injection du monta consommables
				if ($_SESSION["pdtlcl"][0]["valeur"]<>"-1")
				{
					$pdtlcl_valeur=$_SESSION["pdtlcl"][0]["valeur"];
					$pdtlcl_codetva=$_SESSION["pdtlcl"][0]["tva"];
					$rech2 = "INSERT INTO " .TBPDTLCL;
					$rech2 .= " (pdtlcl_idpdtl,pdtlcl_valeur,pdtlcl_codetva) VALUES";
					$rech2 .= " ('$pdtl_id','$pdtlcl_valeur','$pdtlcl_codetva')";
					$rs2 = mysql_query($rech2) or die(mysql_error());
				}
			}
		}
		if ($_SESSION["niv_log"]==3)
			$codeuser = @$_SESSION["code_log"];
		else
			$codeuser=strip_tags(trim(@$_POST["coderet"]));
		
		$rech = "UPDATE `" .TBPDTL. "` SET `pdtl_dateret`='".$dateret."', `pdtl_heureret`='".$heureret."',";
		$rech .= "`pdtl_ridx1`='".$ridx1."', `pdtl_ridx2`='".$ridx2."', `pdtl_rcaut`='".$rcaut."', `pdtl_reloc`='".$reloc."',";
		$rech .= "`pdtl_retpar`='".$retpar."', `pdtl_noter`='".$noter."', `pdtl_coderet`='".$codeuser."'";
		$rech .= " WHERE `pdtl_codecoloc` = '".$codecoloc."' AND `pdtl_codepdtloc` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());

		// On met à jour la fiche produit
		$rech = "UPDATE `" .TBPDTLOC. "` SET `pdtloc_loue`=0";
		switch ($reloc)
		{
			case 0 :
				$rech.= ", `pdtloc_louable`=0";
				break;
			case 1 :
				$rech.= ", `pdtloc_louable`=1";
				break;
		}
		$rech .= " WHERE `pdtloc_code` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());

		$msg = "Produit mis &agrave; jour";
		break;

	// Suppression de la fiche Produit location
	case "del" :
		$rech = "SELECT * FROM " .TBPDTL. " WHERE pdtl_codecoloc = '$codecoloc' AND pdtl_codepdtloc = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 1)
		{
			if ($row=mysql_fetch_assoc($rs))
			{
				extract($row);
				// Suppression du barême location
				$rech2 = "DELETE FROM `" .TBPDTLBL. "` WHERE `pdtlbl_idpdtl` = '$pdtl_id'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				// Suppression de la pénalité location
				$rech2 = "DELETE FROM `" .TBPDTLPL. "` WHERE `pdtlpl_idpdtl` = '$pdtl_id'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				// Suppression du montant consommables
				$rech2 = "DELETE FROM `" .TBPDTLCL. "` WHERE `pdtlcl_idpdtl` = '$pdtl_id'";
				$rs2 = mysql_query($rech2) or die(mysql_error());

				// On met à jour la fiche produit
				$rech = "UPDATE `" .TBPDTLOC. "` SET `pdtloc_loue`=0";
				$rech.= ", `pdtloc_louable`=1";
				$rech .= " WHERE `pdtloc_code` = '".$code."'";
				$rs = mysql_query($rech) or die(mysql_error());
			}
		}
		$rech = "DELETE FROM `" .TBPDTL. "` WHERE `pdtl_id` = '$pdtl_id'";
		$rs = mysql_query($rech) or die(mysql_error());

		$msg = "Produit supprim&eacute;";
		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; ?> : Article Location</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>

<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
if (($td=="add") OR ($td=="dep") OR ($td=="ret"))
	$urlimp = "pdtldr_pdf.php?cl=".$codecoloc."&ca=".$code;
cre_js_pdtincloc("edit",$codecoloc,"window.opener."); 
?>
<script type="text/javascript">
<!--
pdtincloc();
<?PHP
if (($td=="add") OR ($td=="dep") OR ($td=="ret"))
{ 
?>
lanceimp('<?PHP echo $urlimp; ?>');
<?PHP } ?>
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>
