<?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/func.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("../classes/phpmailer/class.phpmailer.php");
include_once("mail.inc.php");
Archivelog(@$_SESSION["code_log"]);

$codecocl = strip_tags(trim($_GET['co']));	// Récupération code Contrat
$codeuser = @$_SESSION["code_log"];

$pbm_cocl = FALSE;

if ($codecocl <> "")
{
	$codeco = crecode("CO");

	// Mise à jour fiche contrat client et récupération informations
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech = "SELECT * FROM " .TBCOCL." WHERE `cocl_code` =  '".$codecocl."'";
	$rs = mysql_query($rech) or die(mysql_error());
	if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
	{
		if ($row = mysql_fetch_array($rs))
		{
			extract ($row);

			// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
			if (((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) AND ($cocl_codeage <> @$_SESSION["code_age"]))
				$pbm_cocl = TRUE;
			else
			{
				$renouv = 1; // Contrat passé en mode "renouvelé"

				$upd = "UPDATE `" .TBCOCL;
				$upd .= "` SET `cocl_datemod`='".$datea."', `cocl_codemod`='".$codeuser."', `cocl_renouv`='".$renouv."',";
				$upd .="  `cocl_rnum`='".$codeco."' WHERE `cocl_code` = '".$codecocl."'";
				$rs2 = mysql_query($upd) or die(mysql_error());
			}
		}

	}
	else
		$pbm_cocl=TRUE;
	mysql_close($conn);
	
	if ($pbm_cocl==FALSE)
	{
		// Création d'un nouveau contrat sur la base de l'ancien
		if (formatedate($cocl_datef)<>"")
			$cocl_dated = ajoute_un_jour_date($cocl_datef);
		else
			$cocl_dated=$datea;
		$cocl_datef = "";

		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "INSERT INTO " .TBCOCL;
		$rech.= " (cocl_datecrea,cocl_codecrea,cocl_datemod,cocl_codemod,cocl_dateenv,cocl_daterec,cocl_code,cocl_codeco,cocl_codeper,";
		$rech.= "cocl_codeage,cocl_codecli,cocl_delai,cocl_nbs,cocl_typh,cocl_dated,cocl_datef,cocl_preavis,cocl_daterel,cocl_nivrel,";
		$rech.= "cocl_renouv,cocl_rnum,cocl_datefin,cocl_signe,cocl_soldes,cocl_matos,cocl_pvht)";
		$rech.= " VALUES";
		$rech.= " ('$datea','$codeuser','$datea','$codeuser','$datea','','$codeco','$cocl_codeco','$cocl_codeper',";
		$rech.= "'$cocl_codeage','$cocl_codecli','$cocl_delai','$cocl_nbs','$cocl_typh','$cocl_dated','$cocl_datef','$cocl_preavis','','',";
		$rech.= "'','','','','$cocl_nbs','".addslashes($cocl_matos)."','$cocl_pvht')";
		$rs = mysql_query($rech) or die(mysql_error());
		mysql_close($conn);
				
		$msg = "Nouveau contrat (N°".$codeco.") créé avec succès.";
		$retourliste = TRUE;
	}
}
else
	$pbm_cocl = TRUE;
?>
<!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 : Renouvellement de contrat</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>

<!-- Scripts Javascript pour alertes -->
<script language="JavaScript" src="../jscript/alertes.js"></script>

<!-- Scripts Javascript divers -->
<script language="JavaScript" src="../jscript/divers.js"></script>

</head>
<body onLoad="metFocus();">

<?php 
include_once("entete.inc.php"); 
?>
<p align="center"><span class="titrepage">RENOUVELLEMENT DE CONTRAT </span></p>
<?PHP
if ($pbm_cocl==FALSE)
{
?>
	<div align="center">Contrat N&deg;<?PHP echo $codecocl?></div>
	<p align="center" class="annot_gras_it"><?PHP echo $msg ?></p>
	<?PHP 
	$url_cocl = "cocl_maj.php?td=edit&co=".$codeco."&cc=".$cocl_codecli;
	?>
		<script>
		warncoclren();
		</script>
		<script type="text/javascript">
		window.setTimeout("location=('<?PHP echo $url_cocl; ?>');",2000)
		</script>
		<p align="center" class="annot_std_it">Passage en mode édition dans 2 secondes...</p>
		
<?PHP 
}
else
{
?>
	<script type="text/javascript">
	setTimeout("self.location.href=('coclren_lst.php?ord=DES&tri=Code');",2000)
	</script>
	<p align="center" class="annot_std_it">Merci de choisir un de vos contrats...</p>
<?PHP
}
include_once("pied.php");
?>
