<?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_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_do.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_time.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"]);

$titre = stripslashes(strip_tags(trim(@$_POST["titre"])));

$codecocl = str_majuscules(strip_tags(trim(@$_POST["codecocl"])));
$rephnc = str_majuscules(strip_tags(trim(@$_POST["lsthnc"])));

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

$retourliste = TRUE;
$soldes_reel=0;
$cloture_ok=FALSE;
$renouv_ok=FALSE;

if ($codecocl<>"")
{
	// MAJ de la fiche contrat client
	$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);

			if (formatedate($cocl_datefin)=="")
			{
				// Si c'est un contrat 'Quota horaire', on récupère le solde du contrat (inclus les reports)
				if ($cocl_typh == 1)
					 $soldes_reel=soldes_contrat($row["cocl_code"],FALSE);

				// Cloture du contrat
				$rech2 = "UPDATE `" .TBCOCL;
				$rech2.= "` SET `cocl_datefin`='".$datea."'";
				$rech2.= " WHERE `cocl_code` = '".$codecocl."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				
				$retourliste = FALSE;
				$cloture_ok=TRUE;
				$msg = "Cloture du contrat ".$codecocl." effectu&eacute;e.";
	
				// Report si demandé
				if ($rephnc != "")
				{
					$rech="";
					switch ($rephnc)
					{
						// Inutile ou Pas de report
						case "0" :
						case "-2" :
							break;
						// Renouvellement automatique ou sur déjà renouvelé
						case "-1" :
							// On vérifie si le contrat a déjà été renouvelé
							if ($cocl_renouv==FALSE) 
							{
								// Si non, on vérifie s'il est en mode "renouvellement automatique"
								if ($cocl_renauto==TRUE)
								{
									$codeco = crecode("CO",FALSE);
								
									// Mise à jour fiche contrat client et récupération informations
									$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);
											$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 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 = "";
							
									$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)";
									$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.= "'','','','','".addslashes($cocl_matos)."','$cocl_pvht')";
									$rs = mysql_query($rech) or die(mysql_error());
											
									$msg.= "<br><br>Nouveau contrat (N°".$codeco.") cr&eacute;&eacute; en renouvellement.";
									$cocl_rnum=$codeco;
									$renouv_ok=TRUE;
								}
								else
								{
									$msg = "Contrat pas en mode 'renouvellement automatique'.";
									$retourliste=FALSE;
								}
							}
	
							// Si c'est un contrat 'Quota horaire', il faut reporter le solde sur le nouveau contrat
							// ou sur le contrat de renouvellement déjà saisi
							if ($cocl_typh == 1)
							{
								if ($cocl_rnum<>"")
								{
									if ($soldes_reel>0)
									{
										// On supprime déjà les reports existants
										$rech = "DELETE FROM `" .TBCOCLR. "` WHERE `coclr_coclori` = '$code'";
										$rs = mysql_query($rech) or die(mysql_error());
									}
	
									// On inscrit le report dans le nouveau contrat
									$rech = "INSERT INTO " .TBCOCLR;
									$rech.= " (coclr_coclori,coclr_cocldest,coclr_nbsrep)";
									$rech.= " VALUES";
									$rech.= " ('$codecocl','$cocl_rnum','$soldes_reel')";
									$rs = mysql_query($rech) or die(mysql_error());
									$msg.= "<br><br>Non consomm&eacute; (".heure_to_hhmm(sec_heure($soldes_reel,3,TRUE)).") report&eacute; sur contrat ".$cocl_rnum.".";
								}
							}
							break;
						// Report sur contrat spécifique
						default :
							// Si c'est un contrat 'Quota horaire', il faut reporter le solde sur le contrat choisi
							if ($cocl_typh == 1)
							{
								if ($soldes_reel>0)
								{
									// On supprime déjà les reports existants
									$rech = "DELETE FROM `" .TBCOCLR. "` WHERE `coclr_coclori` = '$code'";
									$rs = mysql_query($rech) or die(mysql_error());
								}

								// On inscrit le report dans le contrat choisi
								$rech = "INSERT INTO " .TBCOCLR;
								$rech.= " (coclr_coclori,coclr_cocldest,coclr_nbsrep)";
								$rech.= " VALUES";
								$rech.= " ('$codecocl','$rephnc','$soldes_reel')";
								$rs = mysql_query($rech) or die(mysql_error());
								$msg.= "<br><br>Non consomm&eacute; (".heure_to_hhmm(sec_heure($soldes_reel,3,TRUE)).") report&eacute; sur contrat ".$rephnc.".";
							}
							break;
					}
				}
			}
			else
			{
				$msg = "Contrat d&eacute;j&agrave; clot.";
				$retourliste=FALSE;
			}
		}
	}
	else
	{
		$msg = "Contrat inconnu.";
		$retourliste=FALSE;
	}
	mysql_close($conn);

	if ($cloture_ok==TRUE)
	{
		$contrat=$codecocl;
		// Envoi d'email vers le client
		if ($prev==0)
			envoi_mail("","Clôture de votre contrat","con","clo","xxx","cli",$codecocl,"","",FALSE);
		else
			envoi_mailwm("","Clôture de contrat","con","clo","xxx","cli",$codecocl,"","");
		// Envoi d'email vers l'agence
		envoi_mail("","Clôture de contrat","con","clo","xxx","age",$codecocl,"","",FALSE);
		// Envoi d'email vers le centre de gestion
		envoi_mail("","Clôture de contrat","con","clo","xxx","cdg",$codecocl,"","",TRUE);
	}
	
	if ($renouv_ok==TRUE) // Si besoin, on prévient du renouvellement de contrat
	{
		$ncontrat=$codeco;
		$url_cocl = "cocl_maj.php?td=edit&co=".$codeco."&cc=".$cocl_codecli;
		// Envoi d'email vers le client
		if ($prev==0)
			envoi_mail("","Renouvellement de votre contrat","con","ren2","xxx","cli",$codecocl,"","",FALSE);
		else
			envoi_mailwm("","Renouvellement de contrat","con","ren2","xxx","cli",$codecocl,"","");
		// Envoi d'email vers l'agence
		if (@$_SESSION["niv_log"] <> 4)
			envoi_mail("","Renouvellement de contrat","con","ren2","xxx","age",$codecocl,"","",FALSE);
		// Envoi d'email vers le centre de gestion
		envoi_mail("","Renouvellement de contrat","con","ren2","xxx","cdg",$codecocl,"","",TRUE);
	
	}
}
else
{
	$msg = "Contrat inconnu.";
	$retourliste=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 - <?PHP echo $title_esp_prive; ?> : Cloture manuelle 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>

</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)
{
	if ($renouv_ok==TRUE)
	{
?>
	<br>
	<a href="<?PHP echo $url_cocl; ?>"><img src="../img/contrat.png" border="0" align="absmiddle"></a> <a href="<?PHP echo $url_cocl; ?>" class="annot_gras_it">Editer nouveau contrat (<?PHP echo $ncontrat; ?>)</a>
<?PHP 
	}
	aff_ret_idx_man($td,"cocl_lst.php?ord=DES&tri=Code",$txt_lnk_ret_liste,FALSE);
} else
	aff_ret_lst_auto("cocl_lst.php?ord=DES&tri=Code",2000);
include_once("pied.php");
?>
