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

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

$type = str_escape(trim(@$_POST["type"]));
$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$ste = str_majuscules(str_escape(trim(@$_POST["ste"])));
$rcs = str_majuscules(str_escape(trim(@$_POST["rcs"])));
$ape = str_majuscules(str_escape(trim(@$_POST["ape"])));
$tvai = str_majuscules(str_escape(trim(@$_POST["tvai"])));
$adr1 = str_majuscules(strip_tags(str_escape(trim(@$_POST["adr1"]))));
$adr2 = str_majuscules(strip_tags(str_escape(trim(@$_POST["adr2"]))));
$ville = strip_tags(trim(@$_POST["ville"]));
$dep = str_majuscules(strip_tags(trim(@$_POST["dep"])));
$pays = str_majuscules(strip_tags(trim(@$_POST["pays"])));
$telf = strip_tags(trim(@$_POST["telf"]));
$fax = strip_tags(trim(@$_POST["fax"]));
$notaa = strip_tags(str_escape(trim(@$_POST["notaa"])));
$notat = strip_tags(str_escape(trim(@$_POST["notat"])));
$notap = strip_tags(str_escape(trim(@$_POST["notap"])));

$codeuser=@$_SESSION["code_log"];
if (@$_SESSION["niv_log"]==3)
	$codeuser=@$_SESSION["code_age"];

$retourliste = TRUE;

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

// Vérification de la validité de la ville choisie
$rech = "SELECT * FROM " .TBVILLES. " WHERE villes_id = '$ville' AND villes_codepays = '$pays'";
$rs = mysql_query($rech) or die(mysql_error());
if (mysql_num_rows($rs) == 0)
	$ville=0;
switch ($td)
{
	// Ajout de la fiche Marque
	case "add" :
		$rech = "SELECT * FROM " .TBMQUE. " WHERE mque_code = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 0)
		{
			// Fiche Marque
			$rech ="INSERT INTO " .TBMQUE. "(mque_code,mque_ste,mque_rcs,mque_ape,mque_tvai,mque_type,mque_adr1,mque_adr2,mque_ville,mque_dep,mque_codepays,mque_telf,mque_fax,mque_notaa,mque_notat)";
			$rech.=" VALUES ('$code','$ste','$rcs','$ape','$tvai','$type','$adr1','$adr2','$ville','$dep','$pays','$telf','$fax','$notaa','$notat')";
			$rs = mysql_query($rech) or die(mysql_error());
			// Notes privées
			$rech ="INSERT INTO " .TBMQUEN. "(mquen_codemque,mquen_codeu,mquen_nota)";
			$rech.=" VALUES ('$code','$codeuser','$notap')";
			$rs = mysql_query($rech) or die(mysql_error());
			$msg = $txt_fiche_add;
		}
		else
		{
			$msg = $txt_fiche_exist;
			$retourliste = FALSE;
		}
		break;
		
	// Modification de la fiche Marque
	case "edit" :
		// Fiche Marque
		if (@$_SESSION["niv_log"]>=5)
		{
			$rech = "UPDATE `" .TBMQUE. "`";
			$rech.= " SET `mque_ste`='".$ste."', `mque_rcs`='".$rcs."', `mque_ape`='".$ape."', `mque_tvai`='".$tvai."',";
			$rech.= " `mque_type`='".$type."', `mque_adr1`='".$adr1."', `mque_adr2`='".$adr2."',";
			$rech.= " `mque_ville`='".$ville."', `mque_dep`='".$dep."', `mque_codepays`='".$pays."', `mque_telf`='".$telf."',";
			$rech.= " `mque_fax`='".$fax."', `mque_notaa`='".$notaa."', `mque_notat`='".$notat."'";
			$rech.= " WHERE `mque_code` = '".$code."'";
		}
		else
		{
			$rech = "UPDATE `" .TBMQUE. "`";
			$rech.= " SET `mque_ste`='".$ste."', `mque_rcs`='".$rcs."', `mque_ape`='".$ape."', `mque_tvai`='".$tvai."',";
			$rech.= " `mque_type`='".$type."', `mque_adr1`='".$adr1."', `mque_adr2`='".$adr2."',";
			$rech.= " `mque_ville`='".$ville."', `mque_dep`='".$dep."', `mque_codepays`='".$pays."', `mque_telf`='".$telf."',";
			$rech.= " `mque_fax`='".$fax."', `mque_notat`='".$notat."'";
			$rech.= " WHERE `mque_code` = '".$code."'";
		}
		$rs = mysql_query($rech) or die(mysql_error());
		// Notes privées
		$rech = "SELECT * FROM `" .TBMQUEN. "` WHERE `mquen_codemque` = '$code' AND `mquen_codeu` = '$codeuser'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs)>0)
		{
			$rech ="UPDATE `" .TBMQUEN. "`";
			$rech.= " SET `mquen_nota`='".$notap."'";
			$rech.= " WHERE (`mquen_codemque` = '".$code."' AND `mquen_codeu` = '".$codeuser."')";
		}
		else
		{
			$rech ="INSERT INTO " .TBMQUEN. "(mquen_codemque,mquen_codeu,mquen_nota)";
			$rech.=" VALUES ('$code','$codeuser','$notap')";
		}
		$rs = mysql_query($rech) or die(mysql_error());
		$msg = $txt_fiche_mod;
		break;

	// Suppression de la fiche Marque
	case "del" :
		$ok_del = TRUE;
		$msg = "";
	
		// Vérifie si utilisé dans une demande de retour produit
		$rexist = "SELECT * FROM `" .TBRETD. "` WHERE `retd_codemque` = '$code'";
		$rs = mysql_query($rexist) or die(mysql_error());
		if (mysql_num_rows($rs) > 0)
		{
			$msg = $txt_mque_used_retd;
			$ok_del= FALSE;
			$retourliste = FALSE;
		}
	
		// Vérifie si utilisé dans un envoi de retour produit
		$rexist = "SELECT * FROM `" .TBRETE. "` WHERE `rete_codemque` = '$code'";
		$rs = mysql_query($rexist) or die(mysql_error());
		if (mysql_num_rows($rs) > 0)
		{
			if ($msg == "")
				$msg.= "<br /><br />";
			$msg = $txt_mque_used_rete;
			$ok_del= FALSE;
			$retourliste = FALSE;
		}
	
		// Vérifie si utilisé dans un relevé de matériel
		$rexist = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_codema` = '$code'";
		$rs = mysql_query($rexist) or die(mysql_error());
		if (mysql_num_rows($rs) > 0)
		{
			if ($msg == "")
				$msg.= "<br /><br />";
			$msg = $txt_mque_used_rpdt;
			$ok_del= FALSE;
			$retourliste = FALSE;
		}
	
		// Vérifie si utilisé dans un relevé de licence
		$rexist = "SELECT * FROM `" .TBRLIC. "` WHERE `rlic_codema` = '$code'";
		$rs = mysql_query($rexist) or die(mysql_error());
		if (mysql_num_rows($rs) <> 0)
		{
			if ($msg == "")
				$msg.= "<br /><br />";
			$msg.= $txt_mque_used_rlic;
			$ok_del= FALSE;
			$retourliste = FALSE;
		}
	
		// Vérifie si utilisé dans un type de licence
		$rexist = "SELECT * FROM `" .TBRLTL. "` WHERE `rltl_codema` = '$code'";
		$rs = mysql_query($rexist) or die(mysql_error());
		if (mysql_num_rows($rs) <> 0)
		{
			if ($msg == "")
				$msg.= "<br /><br />";
			$msg.= $txt_mque_used_rltl;
			$ok_del= FALSE;
			$retourliste = FALSE;
		}
	
		if  ($ok_del == TRUE)
		{
			// SUPPRESSION de la fiche Marque
			$rech = "DELETE FROM `" .TBMQUE. "` WHERE `mque_code` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
	
			// SUPPRESSION des fiches contacts
			$rech = "DELETE FROM `" .TBCMQUE. "` WHERE `cmque_codemque` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
	
			// SUPPRESSION des liens webs
			$rexist = "SELECT wmque_id FROM `" .TBWMQUE. "` WHERE `wmque_codemque` = '$code'";
			$rs = mysql_query($rexist) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
				if ($row = mysql_fetch_array($rs))
				{
					extract($row);

					$rech2 = "DELETE FROM `" .TBWMQUEN. "` WHERE `wmquen_idwmque` = '$wmque_id'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
				}
			}
	
			$rech = "DELETE FROM `" .TBWMQUE. "` WHERE `wmque_codemque` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
	
			// SUPPRESSION des fiches Notes privées
			$rexist = "SELECT cmque_id FROM `" .TBCMQUE. "` WHERE `cmque_codemque` = '$code'";
			$rs = mysql_query($rexist) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
				if ($row = mysql_fetch_array($rs))
				{
					extract($row);

					$rech2 = "DELETE FROM `" .TBCMQUEN. "` WHERE `cmquen_idcmque` = '$cmque_id'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
				}
			}
	
			$rech = "DELETE FROM `" .TBMQUEN. "` WHERE `mquen_codemque` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
	
			$msg = $txt_fiche_del;			
		}
		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_mque_maj; ?></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,"mque_lst.php",$txt_lnk_ret_liste,TRUE);
else
	aff_ret_lst_auto("mque_lst.php",2000);
include_once("pied.php");
?>
