<?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/config.inc.php");
include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_mque.inc.php");
include_once("../inc/fic_rel.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_date.inc.php");
include_once("../inc/func_log.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.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"]));
$td = strip_tags(trim(@$_POST["td"]));

$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$daterel = datefrang(strip_tags(trim(@$_POST["daterelf"])));
if (strip_tags(trim(@$_POST["dateoutf"])) <> "")
	$dateout = datefrang(strip_tags(trim(@$_POST["dateoutf"])));
else
	$dateout = "";
$coderel = str_majuscules(strip_tags(trim(@$_POST["coderel"])));
$codecli = str_majuscules(strip_tags(trim(@$_POST["codecli"])));
$codeage = str_majuscules(strip_tags(trim(@$_POST["codeage"])));
$site = str_majuscules(strip_tags(trim(@$_POST["site"])));
$codetec = str_majuscules(strip_tags(trim(@$_POST["codetec"])));
$codema = str_majuscules(strip_tags(trim(@$_POST["codema"])));
$modele = str_escape(trim(@$_POST["modele"]));
$version = str_escape(trim(@$_POST["version"]));
$codetl = str_majuscules(strip_tags(trim(@$_POST["codetl"])));
$maxcli = str_majuscules(strip_tags(trim(@$_POST["maxcli"])));
if ($maxcli <=0)
	$maxcli = 1;
$codemed = str_majuscules(strip_tags(trim(@$_POST["codemed"])));
$numlic = str_escape(trim(@$_POST["numlic"]));
$cleflic = str_escape(trim(@$_POST["cleflic"]));
$ajourf = strip_tags(trim(@$_POST["ajour"]));
if ($ajourf == "Oui")
	$ajour = TRUE;
else
	$ajour = FALSE;
$legalf = strip_tags(trim(@$_POST["legale"]));
if ($legalf == "Oui")
	$legal = TRUE;
else
	$legal = FALSE;
if (strip_tags(trim(@$_POST["datevalf"])) <> "")
	$dateval = datefrang(strip_tags(trim(@$_POST["datevalf"])));
else
	$dateval = "";

$nota = str_escape(trim(@$_POST["nota"]));
$signataire = str_escape(trim(@$_POST["signataire"]));
$codeta = str_majuscules(strip_tags(trim(@$_POST["codeta"])));
$tma = str_majuscules(strip_tags(trim(@$_POST["tma"])));
$codel = str_majuscules(strip_tags(trim(@$_POST["codel"])));
$tserf= strip_tags(trim(@$_POST["tser"]));
if ($tserf == "Oui")
	$tser = TRUE;
else
	$tser = FALSE;
$tlien= str_escape(trim(@$_POST["tlien"]));
$tlogin= str_escape(trim(@$_POST["tlogin"]));
$tmdp= str_escape(trim(@$_POST["tmdp"]));
$taef= strip_tags(trim(@$_POST["tae"]));
if ($taef == "Oui")
	$tae = TRUE;
else
	$tae = FALSE;
$tredf= strip_tags(trim(@$_POST["tred"]));
if ($tredf == "Oui")
	$tred = TRUE;
else
	$tred = FALSE;
$codem = str_majuscules(strip_tags(trim(@$_POST["codem"])));
$mper = str_majuscules(strip_tags(trim(@$_POST["mper"])));
$mtype= str_escape(trim(@$_POST["mtype"]));
$mlogin= str_escape(trim(@$_POST["mlogin"]));
$mmdp= str_escape(trim(@$_POST["mmdp"]));
if (strip_tags(trim(@$_POST["mdvalf"])) <> "")
	$mdval = datefrang(strip_tags(trim(@$_POST["mdvalf"])));
else
	$mdval = "";
if (strip_tags(trim(@$_POST["mdlmajf"])) <> "")
	$mdlmaj = datefrang(strip_tags(trim(@$_POST["mdlmajf"])));
else
	$mdlmaj = "";
$maef= strip_tags(trim(@$_POST["mae"]));
if ($maef == "Oui")
	$mae = TRUE;
else
	$mae = FALSE;
$mredf= strip_tags(trim(@$_POST["mred"]));
if ($mredf == "Oui")
	$mred = TRUE;
else
	$mred = FALSE;

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

$retourliste = TRUE;

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

switch ($td)
{
	// Ajout de la fiche Relevé Licence
	case "add" :
		$coderl = crecode("RL");
	
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "INSERT INTO " .TBRLIC;
		$rech.= " (rlic_datecrea,rlic_codecrea,rlic_datemod,rlic_codemod,rlic_coderel,rlic_code,rlic_daterel,rlic_codecli,rlic_codeage,";
		$rech.= "rlic_codetec,rlic_codema,rlic_modele,rlic_version,rlic_codetl,rlic_maxcli,rlic_codemed,rlic_numlic,rlic_cleflic,rlic_ajour,";
		$rech.= "rlic_legal,rlic_dateval,rlic_dateout,rlic_nota,rlic_signataire,rlic_idsite,";
		$rech.= "rlic_codeta,rlic_tma,rlic_codel,rlic_tser,rlic_tlien,rlic_tlogin,rlic_tmdp,rlic_tae,rlic_tred,";
		$rech.= "rlic_codem,rlic_mmode,rlic_mper,rlic_mlogin,rlic_mmdp,rlic_mdval,rlic_mdlmaj,rlic_mae,rlic_mred)";
		$rech.= " VALUES";
		$rech.= " ('$datea','$codeuser','$datea','$codeuser','$coderel','$coderl','$daterel','$codecli','$codeage',";
		$rech.= "'$codetec','$codema','$modele','$version','$codetl','$maxcli','$codemed','$numlic','$cleflic','$ajour',";
		$rech.= "'$legal','$dateval','$dateout','$nota','$signataire','$site',";
		$rech.= "'$codeta','$tma','$codel','$tser','$tlien','$tlogin','$tmdp','$tae','$tred',";
		$rech.= "'$codem','$mtype','$mper','$mlogin','$mmdp','$mdval','$mdlmaj','$mae','$mred')";
		$rs = mysql_query($rech) or die(mysql_error());
		mysql_close($conn);
	
		$msg = "Fiche enregistrée";

		// Envoi d'email vers l'agence
		if (@$_SESSION["niv_log"] <> 4)
			envoi_mail("Ajout d'une fiche relevé licence","rlic","cre","xxx","age",$coderl,"","",FALSE);
		// Envoi d'email vers le centre de gestion
		envoi_mail("Ajout d'une fiche relevé licence","rlic","cre","xxx","cdg",$coderl,"","",TRUE);
		break;
		
	// MAJ de la fiche relevé Licence
	case "edit" :
		$ok_maj = TRUE;
		
		if ($dateout <> "")
		{
			$exist_rlic_in_rpdt_actif = exist_rlic_in_rpdt_actif($codecli,$code);
			if ($exist_rlic_in_rpdt_actif == TRUE)
			{
				$ok_maj = FALSE;
				$retourliste = FALSE;
				$msg = "Cette fiche est encore utilisée dans un ou des relevé(s) matériel(s) actif(s).<br>Enlevez-le du (des) fiche(s) relevé(s) matériel(s) concernée(s).";
			}
			
			$exist_rlic_in_rpbm_actif = exist_rlic_in_rpbm_actif($code);
			if ($exist_rlic_in_rpbm_actif == TRUE)
			{
				$ok_maj = FALSE;
				$retourliste = FALSE;
				if ($msg == "")
					$msg = "Cette fiche contient des problèmes déclarés et non solutionnés.<br>Supprimez la (les) déclaration(s) de problème concernée(s).";
				else
					$msg .= "<br><br>Cette fiche contient des problèmes déclarés et non solutionnés.<br>Supprimez la (les) déclaration(s) de problème concernée(s).";
			}
			
		}
		
		if ($ok_maj == TRUE)
		{
			// MAJ de la fiche relevé Licence
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "UPDATE `" .TBRLIC;
			$rech .= "` SET `rlic_datemod`='".$datea."', `rlic_codemod`='".$codeuser."', `rlic_code`='".$code."', `rlic_daterel`='".$daterel."', `rlic_codecli`='".$codecli."', `rlic_codeage`='".$codeage."', `rlic_codetec`='".$codetec."', `rlic_codema`='".$codema."', `rlic_modele`='".$modele."', `rlic_version`='".$version."',";
			$rech .=" `rlic_codetl`='".$codetl."', `rlic_maxcli`='".$maxcli."', `rlic_codemed`='".$codemed."',`rlic_numlic`='".$numlic."', `rlic_cleflic`='".$cleflic."', `rlic_ajour`='".$ajour."', `rlic_legal`='".$legal."', `rlic_dateval`='".$dateval."',";
			$rech .=" `rlic_dateout`='".$dateout."', `rlic_nota`='".$nota."', `rlic_signataire`='".$signataire."', `rlic_idsite`='".$site."',";
			$rech .=" `rlic_codeta`='".$codeta."', `rlic_tma`='".$tma."', `rlic_codel`='".$codel."', `rlic_tser`='".$tser."', `rlic_tlien`='".$tlien."', `rlic_tlogin`='".$tlogin."', `rlic_tmdp`='".$tmdp."', `rlic_tae`='".$tae."', `rlic_tred`='".$tred."',";
			$rech .=" `rlic_codem`='".$codem."', `rlic_mmode`='".$mtype."', `rlic_mper`='".$mper."', `rlic_mlogin`='".$mlogin."', `rlic_mmdp`='".$mmdp."', `rlic_mdval`='".$mdval."', `rlic_mdlmaj`='".$mdlmaj."', `rlic_mae`='".$mae."', `rlic_mred`='".$mred."'";
			$rech .=" WHERE `rlic_code` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());
			mysql_close($conn);
	
			$msg = "Fiche modifiée";
	
			// Envoi d'email vers l'agence
			if (@$_SESSION["niv_log"] <> 4)
				envoi_mail("Modification d'une fiche relevé licence","rlic","mod","xxx","age",$code,"","",FALSE);
			// Envoi d'email vers le centre de gestion
			envoi_mail("Modification d'une fiche relevé licence","rlic","mod","xxx","cdg",$code,"","",TRUE);
	
		}
		break;
		
	// Suppression de la fiche Relevé Licence
	case "del" :
		// Vérification si le relevé de licence existe dans un relevé produit
		$exist_in_rpdt = exist_rlic_in_rpdt($codecli,$code);
		$exist_in_rpbm = exist_rlic_in_rpbm($code);
		
		if (($exist_in_rpdt == FALSE) AND ($exist_in_rpbm == FALSE))
		{
			// SUPPRESSION de la fiche Relevé Licence
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "DELETE FROM `" .TBRLIC. "` WHERE `rlic_code` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
			mysql_close($conn);
			$msg = "Fiche supprimée";
		}
		else
		{
			if ($exist_in_rpdt == TRUE) 
			{
				$retourliste = FALSE;
				$msg = "Impossible de supprimer un relevé de licence utilisé dans une fiche relevé de matériel.<br>Choisissez plutôt de le sortir du parc...ou enlevez-le des fiches relevés matériel concernées.";
			}
			if ($exist_in_rpbm == TRUE) 
			{
				$retourliste = FALSE;
				$msg = "Impossible de supprimer un relevé de licence utilisé dans une déclaration de problème.<br>Choisissez plutôt de le sortir du parc...ou supprimez la déclaration de problème.";
			}
		}
		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 Relev&eacute; Licence</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("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>
<?PHP if ($retourliste == FALSE) 
{ 
?>
<br>
<br>
	<table width="100%"  border="0">
	  <tr>
		<td width="3%">&nbsp;</td>
		<td width="44%"><div align="right"><a href="rel_maj.php?td=edit&cr=<?PHP echo $coderel; ?>"><img src="../img/liste.gif" width="33" height="34" border="0" align="absmiddle"></a> <a href="rel_maj.php?td=edit&cr=<?PHP echo $coderel; ?>" class="annot_gras_it">Retour au dossier de relev&eacute;</a></div></td>
		<td width="7%">&nbsp;</td>
		<td width="43%"><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></td>
		<td width="3%">&nbsp;</td>
	  </tr>
	</table>
<?PHP 
} 
else 
{
?>
	<script type="text/javascript">
	window.setTimeout("location=('rel_maj.php?td=edit&cr=<?PHP echo $coderel; ?>');",2000)
	</script>
	<p align="center" class="annot_std_it">Retour au dossier de relev&eacute; dans 2 secondes...</p>
<?PHP
}
include_once("pied.php");
?>
