<?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");

if (!isset($_SESSION["lng"])) $_SESSION["lng"]="fr";
include_once("../lng/".$_SESSION["lng"].".inc.php"); // Paramètres de language
include_once("../inc/config.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_cocl.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_time.inc.php");
include_once("../inc/func_txt.inc.php");
Archivelog(@$_SESSION["code_log"]);

$msg_err_co="Aucun contrat";
$co="";
$rnum="";
$client="";
$typh=0;
$nbsnc=0;
$renauto=0;

$pbm_cocl=FALSE;

if (isset($_GET['co']))
{
	$co = strip_tags(trim($_GET['co'])); // Récupération code contrat
	$row=lit_enr_cocl($co);
	if (is_array($row))
	{
		extract($row);
		$client=$cocl_codecli;
		$typh=$cocl_typh;
		$nbsnc=$cocl_nbsnc;
		$renauto=$cocl_renauto;
		$rnum=$cocl_rnum;
		$row = lit_enr_cli($client,TRUE);
		if (is_array($row)==TRUE)
			extract ($row);
		else
			$pbm_cocl=TRUE;
	}
	else
		$pbm_cocl=TRUE;
}

$titre = "CLOTURE MANUELLE CONTRAT";
?>
<!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>

<!-- 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/verif_form.js"></script>

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

<script language="javascript">
<!--
var tab1_champ=new Array(
	"nom:codecocl ; format:liste ; req:Y ; txt:Contrat à clore ; vmn:1 ; stok:frmok ; stbad:frmbad");
//-->
</script>

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

<!-- Scripts Ajax -->
<script type="text/javascript" src="../ajax/util.js"></script>
<script type="text/javascript" src="../ajax/SelectUpdater.js"></script>
<script type="text/javascript" src="../ajax/SelectUpdater2.js"></script>

<script type="text/javascript">
<!--        
function updatecocl()
{
	cocl3Updater = new SelectUpdater2("lsthnc", "<?PHP echo $rep_appli.'/ajax/'; ?>recupcocl3.php?nco=", "attn", "msg", "<?PHP echo $msg_err_co; ?>", "--- Merci de choisir un contrat à clore ---");
	anciencodecocl = "";
	document.getElementById("codecocl").onchange = function() {
		var listecocl = document.getElementById("codecocl");
		var codecocl = listecocl.options[listecocl.selectedIndex].value;
		if (codecocl.length > 0)
		{
			if (codecocl != anciencodecocl)
			{
				// Faire l'appel Ajax
				cocl3Updater.run(codecocl);
				anciencodecocl = codecocl;
			}
		}
		else
		{
			// Remettre la liste a vide et annuler l'appel eventuel
			cocl3Updater.reset();
			anciencodecocl = "";
		}
	}
}
//-->
</script>

</head>

<body onLoad="metFocus();updatecocl();">

<?php 
include_once("../inc/entete.inc.php"); 
?>
<p align="center"> <span class="titrepage"><?PHP echo $titre ?></span></p>
<?PHP if ($pbm_cocl==FALSE) { ?>
<form name="coclclos_maj" id="coclclos_maj" method="post" action="coclclos_enr.php" onSubmit="javascript:; return (verif_coclclos(this,tab1_champ,'',''));">
<input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
<input name="prev" type="hidden" id="prev" value="<?PHP echo $cli_prev ?>">
	<?PHP
	// Création de la liste des contrats non terminés et réceptionnés
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech ="SELECT * FROM ".TBCOCL." WHERE `cocl_datefin` = '0000-00-00' AND `cocl_dateenv` <> '0000-00-00' AND `cocl_daterec` <> '0000-00-00'";
	if (@$_SESSION["niv_log"] == 4)
		$rech.=" AND `cocl_codeage`='".@$_SESSION["code_age"]."'";
	$rech.=" ORDER BY cocl_code DESC";	
	$rs = mysql_query($rech) or die(mysql_error());
	
	if (mysql_num_rows($rs) <> 0)
	{
	?>
	<br>
		<table width="600"  border="0" cellspacing="0" cellpadding="0" align="center">
		<tr>
			<td class="rubfrm" id="f_codecocl">
				<div align="center">Contrat &agrave; clore</div>
			</td>
		</tr>
		<tr>
			<td>
				<div align="center">
				<select name="codecocl" id="codecocl">
				<?PHP
					echo "<option value=\"\">-- Choisissez un contrat --</option>";

					while ($resultat = mysql_fetch_array($rs)) 
					{
						extract($resultat);
						
						$solde_reel=soldes_reel_contrat($cocl_code,FALSE);
			
						$rech2 = "SELECT * FROM " .TBCO. " WHERE co_code = '".$cocl_codeco."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if ($row2 = mysql_fetch_array($rs2))
							extract($row2);
						$txt_cocl = $cocl_code." - ".$co_nom;
						$rech2 ="SELECT * FROM ".TBCLI." WHERE `cli_code` = '".$cocl_codecli."'";
						$rs2 = mysql_query($rech2) or die(mysql_error());
						
						if (mysql_num_rows($rs2) <> 0)
						{
							if ($row2 = mysql_fetch_array($rs2))
								extract ($row2);
						}
						if ($cli_ste <> "")
							$txt_cocl.= " (".$cli_ste;
						else
							$txt_cocl.= " (".$cli_nom;

					 	if (tps_trav_non_clot($cocl_code,FALSE)>0)
							$txt_cocl.=" - Impossible à clore, interventions en cours)";
						else
						{
							if ($cocl_delai <> 0)
								$txt_cocl.=" - ".$cocl_delai."h";
							else
								$txt_cocl.= " Pas de délai";
	
							if ($cocl_datef<>"0000-00-00")
								$txt_cocl.=", ".$txt_div_ech." ".formatedate($cocl_datef);
							else
								$txt_cocl.=", ".$txt_div_ssech." ";
							$txt_cocl.=", solde réel ".heure_to_hhmm(sec_heure($solde_reel,3,TRUE));
							$txt_cocl.=")";
						}
			
						echo "<option value=\"".$cocl_code."\"";
					 	if (tps_trav_non_clot($cocl_code,FALSE)>0)
							echo " disabled";
						else
						{
							if ($co == $cocl_code)
								echo " selected";
						}
						echo ">".$txt_cocl."</option>";

					}
				?>
				</select>
				</div>
			</td>
		</tr>
		<tr>
			<td colspan="2">&nbsp;</td>
		</tr>
		<tr>
			<td align="center" class="rubfrm" id="f_lsthnc">
				Type de report
			</td>
		</tr>
		<tr>
			<td align="center">
				<select name="lsthnc" id="lsthnc">
				<?PHP if ($co=="") { ?>
				<option value="">-- Merci de choisir un contrat à clore --</option>
				<?PHP } else {
				if ($typh==1)
				{
					if ($nbsnc==1)
					{
						if (trim($rnum)=="")
						{
							if ($renauto==TRUE)
								$txt="Report sur renouvellement (automatique)";
						}
						else
							$txt="Report sur renouvellement (".$rnum.")";
				?>
				<option value=""><?PHP echo $sel_lst_makechx; ?></option>
				<option value="-2">Pas de report</option>
				<option value="-1"><?PHP echo $txt; ?></option>
						<?PHP
							$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
							mysql_select_db(DBNAME);
							$rech = "SELECT * FROM ".TBCOCL." WHERE cocl_code<>'".$co."' AND cocl_codecli='".$client."' AND cocl_typh=1 AND (cocl_datef>='".$datea."' OR cocl_datef='0000-00-00') ORDER BY cocl_code";
							$rs = mysql_query($rech) or die(mysql_error());
							if (mysql_num_rows($rs)>0)
							{
								while($row = mysql_fetch_array($rs))
								{
									extract($row);
									$rech2 = "SELECT * FROM " .TBCO. " WHERE co_code = '".$cocl_codeco."'";
									$rs2 = mysql_query($rech2) or die(mysql_error());
									if ($row2 = mysql_fetch_array($rs2))
										extract($row2);
									$txt_cocl = $cocl_code." - ".$co_nom;
									$rech2 ="SELECT * FROM ".TBCLI." WHERE `cli_code` = '".$cocl_codecli."'";
									$rs2 = mysql_query($rech2) or die(mysql_error());
									
									if (mysql_num_rows($rs2) <> 0)
									{
										if ($row2 = mysql_fetch_array($rs2))
											extract ($row2);
									}
									if ($cli_ste <> "")
										$txt_cocl.= " (".$cli_ste;
									else
										$txt_cocl.= " (".$cli_nom;
			
									if ($cocl_delai <> 0)
										 $txt_cocl.=" - ".$cocl_delai."h";
									else
										$txt_cocl.=" - Pas de délai";
									if ($cocl_datef<>"0000-00-00")
										$txt_cocl.=", ".$txt_div_ech." ".formatedate($cocl_datef);
									else
										$txt_cocl.=", ".$txt_div_ssech." ";
									$solde_reel=soldes_contrat($cocl_code,FALSE)-tps_trav_non_clot($cocl_code,FALSE);
									$txt_cocl.=", solde réel ".heure_to_hhmm(sec_heure($solde_reel,3,TRUE));
									if ($cocl_datesig<>"0000-00-00")
										$txt_cocl.=", Signé";
									else
										$txt_cocl.=", Non signé";
									$txt_cocl.=")";
						
									echo "<option value=\"".$cocl_code."\">".$txt_cocl."</option>";
								}
							}
							mysql_close();
					}
					else
						echo "<option value=\"0\">Paramètre 'Report non consommé' à 'Non' (Cloture simple)</option>";
				}
				else
					echo "<option value=\"0\">Aucun report horaire car ce contrat n'est pas de type 'Quota Horaire' (Cloture simple)</option>";
				?>
				<?PHP } ?>
				</select>
				 <span id="attn"></span><span id="msg" style="background-color:#FF0000; color:#FFFF00; font-weight:bold"></span>
			</td>
		</tr>
		</table>
		<p align="center">
		  <input name="annul" type="button" class="bton_std" id="annul" value="<?PHP echo $but_cancel; ?>" onclick="javascript:; history.back();">
		  <input type="submit" class="bton_std" name="submit" id="submit" value="<?PHP echo $but_valider; ?>">
	<?PHP
	}
	else
	{
		echo "<span class=\"annot_gras_it\">";
		echo "<div align=\"center\">";
		echo "Pas de contrat pouvant être clot";
		echo "</div>";
		echo "</span>";
	}
	
	mysql_free_result($rs);
	mysql_close($conn);
	?>
</p>
</form>
<?PHP
}
else
	aff_ret_pbm_auto("index.php",2000);
include_once("pied.php");
?>
