<?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/fic_age.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_do.inc.php");
include_once("../inc/fic_hum.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_geo.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../inc/func_time.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("mail.inc.php");
Archivelog(@$_SESSION["code_log"]);

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

if (strip_tags(trim(@$_POST["val"])) == html_entity_decode($but_valider))
{
	$okval = TRUE;
	$codecocl = strip_tags(trim($_POST['co']));	// Récupération code Contrat
}

if ($codecocl <> "")
{
	$row=lit_enr_cocl($codecocl);
	if (is_array($row))
	{
		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
		$pbm_cocl=TRUE;

	if ($pbm_cocl==FALSE)
	{
		if ($okval==TRUE)
		{
			// Création d'un nouveau contrat sur la base de l'ancien
			$codeco = crecode("CO",TRUE);
		
			$datedf = strip_tags(trim($_POST["datedf"]));
			if ($datedf <> "")
				$dated = datefrang($datedf);
			else
				$dated = $datedf;
			$dateff = strip_tags(trim($_POST["dateff"]));
			if ($dateff <> "")
				$datef = datefrang($dateff);
			else
				$datef = $dateff;
			$nval = strip_tags(trim($_POST["nval"]));

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

			// Réinjection des sites secondaires
			$tab_sites=$_POST["scli"];
			foreach ($tab_sites as $id_site)
			{
				if (($id_site <> "") AND ($id_site <> 0))
				{
					$rech = "INSERT INTO ". TBSCLIC;
					$rech.= " (sclic_datecrea,sclic_datemod,sclic_idscli,sclic_codecocl)";
					$rech.= " VALUES";
					$rech.= " ('$datea','$datea','$id_site','$codeco')";
					$rs = mysql_query($rech) or die(mysql_error());
				}
				else
					if (($id_site <> "") AND ($id_site == 0))
						$sitep=1;
			}

			// Mise à jour fiche contrat client
			$upd = "UPDATE `" .TBCOCL;
			$upd .= "` SET `cocl_datemod`='".$datea."', `cocl_codemod`='".$codeuser."', `cocl_renouv`='1',";
			$upd .="  `cocl_rnum`='".$codeco."' WHERE `cocl_code` = '".$codecocl."'";
			$rs2 = mysql_query($upd) or die(mysql_error());
			
			// Création du contrat
			$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_matos,cocl_pvht,cocl_coderlp,cocl_sitep,cocl_codetva,";
			$rech.= "cocl_renauto)";
			$rech.= " VALUES";
			$rech.= " ('$datea','$codeuser','$datea','$codeuser','$datea','','$codeco','$cocl_codeco','$cocl_codeper',";
			$rech.= "'$cocl_codeage','$cocl_codecli','$cocl_delai','$cocl_nbs','$cocl_typh','$dated','datef','$cocl_preavis','','',";
			$rech.= "'','','','','".addslashes($cocl_matos)."','$nval','$cocl_coderlp','$sitep','$cocl_codetva','$cocl_renauto')";
			$rs = mysql_query($rech) or die(mysql_error());
			
			mysql_close($conn);
					
			// Envoi d'email vers le client
			envoi_mail("","Renouvellement de votre contrat","con","ren","xxx","cli",$codecocl,"","",FALSE);
			// Envoi d'email vers l'agence
			if (@$_SESSION["niv_log"] <> 4)
				envoi_mail("","Proposition de renouvellement de contrat","con","ren","xxx","age",$codecocl,"","",FALSE);
			// Envoi d'email vers le centre de gestion
			envoi_mail("","Proposition de renouvellement de contrat","con","ren","xxx","cdg",$codecocl,"","",TRUE);
		
			$msg = "Nouveau contrat (N°".$codeco.") cr&eacute;&eacute;";
			$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 - <?PHP echo $title_esp_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 sur les champs -->
<script language="JavaScript" src="../jscript/verif_field.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>

<!-- Définition Javascript pour calendrier popup -->
  <!-- Feuille de Style -->
  <link rel="stylesheet" type="text/css" media="all" href="../jscript/jscal/skins/aqua/theme.css" title="Aqua" />

  <!-- Script Javascript Principal -->
  <script type="text/javascript" src="../jscript/jscal/calendar.js"></script>

  <!-- Choix du langage -->
  <script type="text/javascript" src="../jscript/jscal/lang/calendar-<?PHP echo $_SESSION["lng"]; ?>.js"></script>

  <!-- the following script defines the Calendar.setup helper function, which makes
       adding a calendar a matter of 1 or 2 lines of code. -->
  <script type="text/javascript" src="../jscript/jscal/calendar-setup.js"></script>
<!-- Fin de la définition Javascript pour calendrier popup -->

<!-- Scripts Javascript pour contrôle de formulaire -->
<script	type="text/javascript" src="../jscript/vform/vform.js"></script>

<?PHP
if ($okval == FALSE)
{
?>
<script language="javascript">
<!--
var tab1_champ=new Array(
	"nom:datedf ; format:date ; masque:<?PHP echo $datemsql; ?> ; req:Y ; vmn:<?PHP if (formatedate($cocl_datef)<>"") echo formatedate(ajoute_un_jour_date($cocl_datef)); else echo $cocl_dated; ?> ; txt:Date début ; stok:frmok ; stbad:frmbad",
	"nom:dateff ; format:date ; masque:<?PHP echo $datemsql; ?> ; req:N ; txt:Date fin ; stok:frmok ; stbad:frmbad",
	"nom:valpour ; format:reel_ns ; req:Y ; txt:Pourcentage revalorisation ; lmx:10 ; stok:frmok ; stbad:frmbad",
	"nom:scli[] ; format:listem ; req:Y ; txt:Sites clients ; vmn:1 ; stok:frmok ; stbad:frmbad");
</script>
<?PHP
}
?>

</head>
<body onLoad="nval_from_mtpour('exval','valpour','nval','2');metFocus();">

<?php 
include_once("../inc/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>
<?PHP 
	if ($okval==TRUE)
	{
?>
	<p align="center">
<?PHP if ($retourliste==FALSE)
	echo '<span class="alerte1">';
else
	echo '<span class="annot_std_gras">';
echo $msg;
?>
</span>
</p>

	<?PHP 
	$url_cocl = "cocl_maj.php?td=edit&co=".$codeco."&cc=".$cocl_codecli;
	?>
		<script type="text/javascript">
		<!--
		warncoclren();
		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
	{
?>
<br />
<form name="coclren_maj" method="post" action="coclren_maj.php" onSubmit="javascript:; return (VForm(this,tab1_champ,'','<?PHP echo $_SESSION["lng"]; ?>'));">
<input name="co" type="hidden" id="code" value="<?PHP echo $codecocl ?>">
<!-- Date de départ -->
<table width="900"  border="0" align="center">
<tr>
	<td width="160" class="rubfrm" id="f_datedf" align="right">D&eacute;but / Fin</td>
	<td>
		<input name="datedf" type="text" id="datedf" value="" size="10" maxlength="10" readonly="1">
		<img src="../img/calendar.png" align="absmiddle" id="f_img_datedf" title="<?PHP echo $alt_chxdate; ?>">
		<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
		<a href="javascript:;" onclick="document.coclren_maj.datedf.value=''"><img src="../img/raz.png" border="0" align="absmiddle"></a> 
		 / <input name="dateff" type="text" id="dateff" value="" size="10" maxlength="10" readonly="1">
		<img src="../img/calendar.png" align="absmiddle" id="f_img_dateff" title="<?PHP echo $alt_chxdate; ?>">
		<a href="javascript:;" onclick="document.coclren_maj.dateff.value=''"><img src="../img/raz.png" border="0" align="absmiddle"></a> 
		 <span class="annot_petit_it">(Pas de de date de fin si illimit&eacute;)</span>
		<div class="annot_petit_it">
		(Ancienne p&eacute;riode : du <?PHP echo formatedate($cocl_dated); ?> au <?PHP if (formatedate($cocl_datef)<>"") echo formatedate($cocl_datef); else echo "..."; ?>)
		</div>
	</td>
</tr>
</table>
<table width="900"  border="0" align="center">
<tr>
	<td width="160" class="rubfrm" id="f_valpour" align="right" rowspan="2">Valorisation</td>
	<td width="120" align="right" class="annot_petit_it">Taux : </td>
	<td class="annot_petit_it">
	<input name="exval" type="hidden" id="exval" value="<?PHP echo $cocl_pvht; ?>">
	<input name="valpour" type="text" id="valpour" style="text-align:right" value="" size="10" maxlength="10" onChange="formatedeci(this.id,2);nval_from_mtpour('exval',this.id,'nval','2');"> %
	<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	(Ancien montant HT <?PHP echo format_num($cocl_pvht,2)." ".$lang['symbole_monnaie']; ?>)
	</td>
</tr>
<tr>
	<td align="right" class="annot_petit_it">Nouveau montant HT : </td>
	<td class="annot_petit_it">
	<input name="nval" type="text" id="nval" style="text-align:right" value="" size="10" maxlength="10" onChange="formatedeci(this.id,2);ntx_from_exvalnval('exval','valpour',this.id,'2');"> <?PHP echo $lang['symbole_monnaie']; ?>
	</td>
</tr>
</table>
<table width="900"  border="0" align="center">
<tr>
	<td width="160" class="rubfrm" id="f_scli" align="right">Sites client</td>
	<td>
	<select name="scli[]" id="scli" size="5" multiple="multiple">
	<?PHP
	echo "<option>-- Choisissez les sites concernés --</option>";
	echo '<option value="0"';
	if ($cocl_sitep==1)
		echo' selected';
	echo '>Site principal</option>';

	// Création de la liste des sites actifs
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech = "SELECT * FROM ".TBSCLI."  WHERE scli_actif = '1' AND scli_codecli = '".$cocl_codecli."' ORDER BY scli_ste";
	$rs = mysql_query($rech) or die(mysql_error());

	if (mysql_num_rows($rs) <> 0)
	{
	?>
	<?PHP
		while ($resultat = mysql_fetch_array($rs)) 
		{
			extract($resultat);
			echo '<option value="'.$scli_id.'"';
			$rech2 = "SELECT sclic_id FROM ".TBSCLIC." WHERE sclic_idscli = '".$scli_id."' AND sclic_codecocl = '".$codecocl."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if (mysql_num_rows($rs2) == 1)
				echo' selected';
			mysql_free_result($rs2);
			$txt_scli=$scli_ste.", ".Retournecp_openId($scli_ville,FALSE)." ".Retourneville_openId($scli_ville,FALSE);
			echo '>'.$txt_scli.'</option>';
		}
	}
	mysql_close($conn);
	?>
    </select>
	 <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	<div class="annot_petit_it">
	(S&eacute;lectionnez les sites avec Ctrl + clic gauche)
	</div>
	</td>
</tr>
</table>
<?PHP /* if ($cocl_renauto==1) {
echo '<br />';
echo '<div align="center" class="annot_gras_it">Ce contrat est en reconduction tacite. Il en sera automatiquement accus&eacute; r&eacute;ception d&egrave;s que vous aurez valid&eacute;</div>';
echo '<br />';
} */ ?>
<p align="center">
<input name="val" type="submit" class="bton_std" id="val" value="<?PHP echo $but_valider; ?>">
<input name="ann" type="button" class="bton_std" id="ann" value="<?PHP echo $but_cancel; ?>" onClick="javascript:; window.location=('coclren_lst.php?ord=DES&tri=Code');">
</p>
</form>
<script type="text/javascript">
<!--
	Calendar.setup({
		inputField     :    "datedf",     // id of the input field
		ifFormat       :    "<?PHP echo $datefmtc; ?>",      // format of the input field
		button         :    "f_img_datedf",  // trigger for the calendar (button ID)
		align          :    "Br",           // alignment (defaults to "Bl")
		electric	   :	false,
		singleClick    :    true
	});
	Calendar.setup({
		inputField     :    "dateff",     // id of the input field
		ifFormat       :    "<?PHP echo $datefmtc; ?>",      // format of the input field
		button         :    "f_img_dateff",  // trigger for the calendar (button ID)
		align          :    "Br",           // alignment (defaults to "Bl")
		electric	   :	false,
		singleClick    :    true
	});
//-->
</script>
<?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");
?>
