<?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/fic_age.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_co.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_ot.inc.php");
include_once("../inc/fic_statut.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_time.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../classes/phpmailer/class.phpmailer.php");
include_once("../inc/mail.inc.php");
Archivelog(@$_SESSION["code_log"]);

$retourok = TRUE;

if (@$_SESSION["niv_log"]==9)
{
	$msg="";
	
	// Variables Formulaire
	$code = str_majuscules(strip_tags(trim(@$_GET["ci"])));
	$codeuser=@$_GET["code_log"];
	
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech = "SELECT * FROM " .TBINT. " WHERE `int_code` = '$code'";
	$rs = mysql_query($rech) or die(mysql_error());
	if (mysql_num_rows($rs) == 1)
	{
		if ($row = mysql_fetch_assoc($rs))
		{
			extract($row);
			
			$reopen_ok=TRUE;
			
			// Si intervention rattachée à un contrat, manipulations complémentaires
			if ($int_codecocl <> "")
			{
				$rech2 = "SELECT * FROM " .TBCOCL." WHERE `cocl_code` = '".$int_codecocl."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				if (mysql_num_rows($rs2) <> 0)
				{
					if ($row2 = mysql_fetch_array($rs2))
					{
						extract($row2);

						// Si la date du jour est supérieure à la date de fin du contrat,
						// il n'est pas possible de réouvrir l'intervention
						if (comp_date($cocl_datef,$datea) == TRUE)
						{
							$reopen_ok=FALSE;
							$msg = "Le contrat rattaché à cette intervention est en échéance dépassée.\nImpossible de réouvrir cette intervention.";
							$retourok = FALSE;
						}
					}
				}
			}
			
			if ($reopen_ok==TRUE)
			{
				// On supprime la date de cloture de cette intervention
				$rech2 = "UPDATE `" .TBINT. "` SET `int_datemod`='".$datea."', `int_codemod`='".$codeuser."', `int_datefin`='', `int_codestatut`='EC'";
				$rech2.= " WHERE `int_code` = '$code'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				$msg="Intervention ".$code." réouverte";
		
				// Remise à niveau type client
				$rech2 = "SELECT * FROM " .TBINT. " WHERE `int_codecli` = '$int_codecli'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				// Une seule intervention (donc celle venant d'être réouverte) ?
				if (mysql_num_rows($rs2) == 1) 
				{
					// Il y a un relevé ou un contrat ?
					$rech3 = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codecli` = '$int_codecli'";
					$rs3 = mysql_query($rech3) or die(mysql_error());
					$nbcocl = mysql_num_rows($rs3);
	
					$rech3 = "SELECT * FROM `" .TBREL. "` WHERE `rel_codecli` = '$int_codecli'";
					$rs3 = mysql_query($rech3) or die(mysql_error());
					$nbrel = mysql_num_rows($rs3);
					
					// Si oui, on ne touche à rien (le client reste en 'déjà client')
					// Si non, on le ramène en 'demande d'intervention' puisqu'il n'y a qu'une intervention pas close
					if (($nbcocl==0) AND ($nbrel==0))
					{
						$rech3 = "UPDATE `" .TBCLI. "` SET `cli_datemod`='".$datea."', `cli_codemod`='".$codeuser."', `cli_prop`='".$int_codeage."', `cli_type`='1'  WHERE `cli_code` = '".$int_codecli."'";
						$rs3 = mysql_query($rech3) or die(mysql_error());
						$msg.="<br><br>Niveau client pass&eacute; &agrave; 'Demande d\'intervention'.";
					}
				}
	
				// Réouverture des problèmes affectés à l'intervention
				$rech2 = "SELECT * FROM " .TBRPBM. " WHERE `rpbm_codeint` = '$code'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				if (mysql_num_rows($rs2) > 0)
				{
					$rech2 = "UPDATE `" .TBRPBM. "` SET `rpbm_datett`= '' WHERE `rpbm_codeint`= '".$code."'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
					$msg.="<br><br>Probl&egrave;mes attach&eacute;s r&eacute;ouverts";
				}
				
				// Réouverture du contrat	
				if ($cocl_typh == 1) // Contrat 'Quota Horaire'
				{
					// Récupération total travaillé
					$nbtotalsec=0;
					$nbtotalsecoff=0;
					
					$rech3 = "SELECT * FROM `" .TBINTH. "` WHERE `inth_codeint`= '".$code."'";
					$rs3 = mysql_query($rech3);
					if (mysql_num_rows($rs) <> 0)
					{
						while ($row3 = mysql_fetch_assoc($rs3)) 
						{         
							extract($row3);
							
							$nbtotalsec = $nbtotalsec + diff_time_sec($inth_heurea,$inth_heured);
							if ($row["inth_offert"] == 1)
								$nbtotalsecoff = $nbtotalsecoff + diff_time_sec($inth_heurea,$inth_heured);
						}
					}
					$tpsreel=$nbtotalsec-$nbtotalsecoff;

					$cocl_soldes = $cocl_soldes + $tpsreel;
					
					// Réinjection du nouveau solde
					$rech3 = "UPDATE `" .TBCOCL;
					$rech3.= "` SET `cocl_soldes`='".$cocl_soldes."', `cocl_datefin`=''";
					$rech3.= " WHERE `cocl_code` = '".$cocl_code."'";
					$msg.="<br><br>Solde contrat ".$cocl_code." remis &aacute; jour.<br>(Contrat r&eacute;ouvert)";
				}
				else
				{
					$rech3 = "UPDATE `" .TBCOCL;
					$rech3.= "` SET `cocl_datefin`=''";
					$rech3.= " WHERE `cocl_code` = '".$cocl_code."'";
					$msg.="<br><br>Contrat ".$cocl_code." r&eacute;ouvert.";
				}
				$rs3 = mysql_query($rech3) or die(mysql_error());
			}
		}
	}
	else
	{
		$msg = "Cette intervention n'existe pas !";
		$retourok = FALSE;
	}
	mysql_free_result($rs);
}
else
{
	$msg = "Vous n'avez pas le droit de r&eacute;ouvrir une intervention";
	$retourok = FALSE;
}
?>
<!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 : R&eacute;ouverture Intervention</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">R&eacute;ouverture Intervention </span></p>
<p align="center">&nbsp;</p>
<p align="center" class="annot_gras_it"><?PHP echo $msg ?></p>
<br>
<br>
<?PHP if ($retourok == FALSE) 
{ 
?>
	<p align="center" class="annot_std_it">Retour à la liste des interventions dans 5 secondes...</p>
	<!-- Fermeture temporisée -->
	<script type="text/javascript">
		window.opener.location.reload();
		setTimeout("self.close('fscomp');",5000)
	</script>
<?PHP 
} 
else 
{
?>
	<p align="center" class="annot_std_it">Retour à la liste des interventions dans 2 secondes...</p>
	<!-- Fermeture temporisée -->
	<script type="text/javascript">
		window.opener.location.reload();
		setTimeout("self.close('fscomp');",2000)
	</script>
<?PHP		
}
?>
<p align="center">Sinon, <a href="javascript:void(0)" onClick="javascript:; window.opener.location.reload(); self.close('fscomp');">cliquez ici</a> </p>
</body>
</html>
