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

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

$iddloc = intval(strip_tags(trim(@$_POST["dloc"])));
$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$des = str_majuscules(strip_tags(str_escape(trim(@$_POST["des"]))));
$ns = strip_tags(str_escape(trim(@$_POST["ns"])));
$fam = str_majuscules(strip_tags(trim(@$_POST["fam"])));
$valneuf = strip_tags(trim(@$_POST["valneuf"]));
$valnet = strip_tags(trim(@$_POST["valnet"]));
$caution = strip_tags(trim(@$_POST["caution"]));
$tvacaut = strip_tags(trim(@$_POST["tva"]));
$note = strip_tags(trim(@$_POST["note"]));
$louable = strip_tags(trim(@$_POST["louable"]));
if ($louable == "Oui")
	$louableok = TRUE;
else
	$louableok = FALSE;
$ucons1 = str_majuscules(strip_tags(trim(@$_POST["ucons1"])));
$idxcons1 = strip_tags(trim(@$_POST["idxcons1"]));
$tvacons1 = strip_tags(trim(@$_POST["tvacons1"]));
$ucons2 = str_majuscules(strip_tags(trim(@$_POST["ucons2"])));
$idxcons2 = strip_tags(trim(@$_POST["idxcons2"]));
$tvacons2 = strip_tags(trim(@$_POST["tvacons2"]));

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

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

switch ($td)
{
	// Ajout de la fiche Produit
	case "add" :
		$rech = "SELECT * FROM " .TBPDTLOC. " WHERE pdtloc_code = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 0)
		{
			$rech2 = "INSERT INTO " .TBPDTLOC;
			$rech2 .= " (pdtloc_code,pdtloc_des,pdtloc_ns,pdtloc_datecrea,pdtloc_fam,pdtloc_iddloc,pdtloc_valneuf,pdtloc_valnet,";
			$rech2 .= "pdtloc_caution,pdtloc_tvacaut,pdtloc_note,pdtloc_louable,pdtloc_pdtucons1,pdtloc_idxcons1,pdtloc_tvacons1,";
			$rech2 .= "pdtloc_pdtucons2,pdtloc_idxcons2,pdtloc_tvacons2) VALUES";
			$rech2 .= " ('$code','$des','$ns','$datea','$fam','$iddloc','$valneuf','$valnet','$caution','$tvacaut','$note',";
			$rech2 .= "'$louableok','$ucons1','$idxcons1','$tvacons1','$ucons2','$idxcons2','$tvacons2')";
			$rs2 = mysql_query($rech2) or die(mysql_error());
	
			$msg = $txt_fiche_add;
		}
		else
		{
			$msg = $txt_fiche_exist;
			$retourliste = FALSE;
		}
		mysql_free_result($rs);
		mysql_close($conn);
		break;
		
	// MAJ de la fiche Produit
	case "edit" :
		mysql_close($conn);
		$existecoloc = FALSE;
		$majok=TRUE;

		// Vérifie si utilisé dans une fiche intervention non close
		if (exist_pdt_in_coloc_ouv($code) == TRUE)
		{
			$existecoloc = TRUE;
			if ($louableok == FALSE)
			{
				$majok=FALSE;
				$msg = "Ce produit est pr&eacute;sent dans une location en cours.<br>";
				$retourliste = FALSE;
			}
		}
		
		if ($majok == TRUE)
		{	
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
	
			// MAJ de la fiche Produit
			$rech = "UPDATE `" .TBPDTLOC. "` SET `pdtloc_code`='".$code."', `pdtloc_iddloc`='".$iddloc."',";
			$rech.= " `pdtloc_fam`='".$fam."', `pdtloc_des`='".$des."', `pdtloc_ns`='".$ns."', `pdtloc_valneuf`='".$valneuf."',";
			$rech.= "`pdtloc_valnet`='".$valnet."', `pdtloc_caution`='".$caution."',`pdtloc_tvacaut`='".$tvacaut."',`pdtloc_note`='".$note."',";
			$rech.= "`pdtloc_louable`='".$louableok."',`pdtloc_pdtucons1`='".$ucons1."',`pdtloc_idxcons1`='".$idxcons1."',";
			$rech.= "`pdtloc_tvacons1`='".$tvacons1."',`pdtloc_pdtucons2`='".$ucons2."',`pdtloc_idxcons2`='".$idxcons2."',";
			$rech.= "`pdtloc_tvacons2`='".$tvacons2."'";
			$rech.= " WHERE `pdtloc_code` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());

			mysql_close($conn);

			$msg = $txt_fiche_mod;
		}
		break;
		
	// Suppression de la fiche Produit
	case "del" :
		mysql_close($conn);
		$existcoloc = FALSE;
	
		// Vérifie si utilisée dans une fiche intervention
		if (exist_pdt_in_coloc($code) == TRUE)
		{
			$existcoloc = TRUE;
			$msg = "Ce produit est d&eacute;j&agrave; utilis&eacute; dans une location.<br>Passez-le en mode 'non louable'.";
			$retourliste = FALSE;
		}
		
		if ($existcoloc == FALSE)			
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
	
			// SUPPRESSION de la fiche Produit
			$rech = "DELETE FROM `" .TBPDTLOC. "` WHERE `pdtloc_code` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
			mysql_close($conn);
	
			$msg = $txt_fiche_del;
		}
		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 Produit &agrave; Louer</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>
<?PHP 
if ($retourliste == FALSE)
	aff_ret_idx_man($td,"pdtloc_lst.php?ta=m",$txt_lnk_ret_liste,TRUE);
else
	aff_ret_lst_auto("pdtloc_lst.php?ta=m&ca=".@$_SESSION["code_age"],2000);
include_once("pied.php");
?>
