<?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/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_time.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/lst.inc.php");
Archivelog(@$_SESSION["code_log"]);
$fi="";
if (isset($_GET['fi']) && (trim($_GET['fi'])<>""))
	$fi=intval(strip_tags(trim($_GET['fi'])));

$pbm_cocl=TRUE;
if ((@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3) OR (@$_SESSION["niv_log"] == 1))
	$pbm_cocl=FALSE;
	
if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
{
	$row = lit_enr_age(@$_SESSION["code_age"],TRUE);
	if (is_array($row))
		extract ($row);
	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; ?> : Impression Liste Contrats Clients</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../css/smi.css" rel="stylesheet" type="text/css">

<!-- 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>

</head>

<body onLoad="warnimplst();">

<?PHP
if ($pbm_cocl==FALSE)
{
	if ((@$_SESSION["niv_log"] == 3) OR (@$_SESSION["niv_log"] == 4))
		entimplst("LISTE DE VOS CONTRATS CLIENTS");
	if (@$_SESSION["niv_log"] == 1)
		entimplst("LISTE DE VOS CONTRATS");
	if ((@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 5))
		entimplst("LISTE DES CONTRATS CLIENTS");

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

	switch(@$_SESSION["niv_log"])
	{
		case 1 :
			$tab_cli=give_tab_rcli($_SESSION["code_log"],FALSE);
			if (count($tab_cli)==1)
			{
				$rech = cree_strrechlstcoclc();
				if(!stristr($rech,"WHERE "))
					$rech .= " WHERE cocl_codecli='".@$_SESSION["code_log"]."'";
				else
					$rech .= " AND cocl_codecli='".@$_SESSION["code_log"]."'";
			}
			else
			{
				$rech = cree_strrechlstcocl();
				
				// On rajoute les comptes complémentaires (cas des regroupements de clients)
				if(!stristr($rech,"WHERE "))
				 $rech .= " WHERE ";
				else
				 $rech .= " AND ";
				if (count($tab_cli)>1)
					$rech .= "(";
				foreach($tab_cli as $key=>$value)
				{
					$rech .= " ".TBCOCL.".cocl_codecli='".$value."' OR";
				}
				// Suppression du dernier 'OR'
				$rech = substr($rech,0,strlen($rech)-3);
			
				if (count($tab_cli)>1)
					$rech .=")";
			}
			break;
		default :
			$rech = cree_strrechlstcocl();
			if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
			{
				if(!stristr($rech,"WHERE "))
					$rech .= " WHERE ".TBCOCL.".cocl_codeage='".@$_SESSION["code_age"]."'";
				else
					$rech .= " AND ".TBCOCL.".cocl_codeage='".@$_SESSION["code_age"]."'";
			}
			break;
	}		 
	
	switch ($fi)
	{
		case "1" :
			if(!stristr($rech,"WHERE "))
				$rech .= " WHERE cocl_datesig<>'0000-00-00' AND cocl_dated<='".$datea."' AND (cocl_datef>='".$datea."' OR cocl_datef='0000-00-00') AND cocl_datefin='0000-00-00'";
			else
				$rech .= " AND cocl_datesig<>'0000-00-00' AND cocl_dated<='".$datea."' AND (cocl_datef>='".$datea."' OR cocl_datef='0000-00-00') AND cocl_datefin='0000-00-00'";
			break;
		case "2" :
			if(!stristr($rech,"WHERE "))
				$rech .= " WHERE cocl_dateenv<>'0000-00-00' AND cocl_daterec='0000-00-00'";
			else
				$rech .= " AND cocl_dateenv<>'0000-00-00' AND cocl_daterec='0000-00-00'";
			break;
		default :
			break;
	}
	
	switch(@$_SESSION["niv_log"])
	{
		case 1 :
			if (count($tab_cli)==1)
				$rech .= crestr_triord($coclc_titre_col,"cocl_code",FALSE,$limit);
			else
				$rech .= crestr_triord($cocl_titre_col,"cocl_code",FALSE,$limit);
			break;
		default :
			$rech .= crestr_triord($cocl_titre_col,"cocl_code",FALSE,$limit);
			break;
	}

	$rs = mysql_query($rech) or die(mysql_error());
	$numrows = mysql_num_rows($rs); // Nombre de résultats à la requête
		
	// *** Affichage de la table
     echo "<table class=\"MItable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";     

	// Création entête
	switch(@$_SESSION["niv_log"])
	{
		case 1 :
			if (count($tab_cli)==1)
				titimplst($coclc_titre_col);
			else
				titimplst($cocl_titre_col);
			break;
		default :
			titimplst($cocl_titre_col);
	}
	
	while ($row = mysql_fetch_assoc($rs)) 
	{         
	 // Affichage résumé Contrat Client

		 echo "<tr>";

		 $echeance_ok = TRUE;
		 if (formatedate($row["cocl_datef"]) <> "")
		 {
			if (echeance_depassee_date($datea,$row["cocl_datef"]) == TRUE)
				$echeance_ok = FALSE;
		 }

		 echo "<td>".$row["cocl_code"];

		 // CREATION d'icône si signé
		  if (formatedate($row["cocl_datesig"]) <> "")
		  {
			 echo "<br>";
			 echo "<span class=\"annot_petit_it\">";
			 echo "Signé le ".formatedate($row["cocl_datesig"]);
			 echo "</span>";
		  }

		 // CREATION d'icône si terminé
		  if ((formatedate($row["cocl_datefin"]) <> "") OR ($echeance_ok == FALSE))
		  {
			 echo "<br>";
			 echo "<span class=\"annot_petit_it\">";
			 echo "Terminé le ";
			if (formatedate($row["cocl_datefin"]) <> "")
				echo formatedate($row["cocl_datefin"]);
			else
				echo formatedate($row["cocl_datef"]);
			 echo "</span>";
		  }

		 // Détail contrat
		 echo "<br>";
		 echo "<span class=\"annot_petit_it\">";
		 $txt = formatetxt($row["co_nom"]);
		 echo "->Type : ".htmlspecialchars($txt,ENT_NOQUOTES);
		 echo "</span>";

		 // Géré par
		if ((@$_SESSION["niv_log"] <>3) AND (@$_SESSION["niv_log"] <>4))
		{
			 echo "<br>";
			 echo "<span class=\"annot_petit_it\">";
			 $txt = formatetxt($row["cocl_codeage"]);
			 echo "->Agence : ".htmlspecialchars($txt,ENT_NOQUOTES);
			 echo "</span>";
		}
		 echo "</td>\n";

		// Type de contrat
		 echo "<td align=\"center\">".$typ_horaire[$row["cocl_typh"]]."</td>\n";

		switch(@$_SESSION["niv_log"])
		{
			case 1 :
				if (count($tab_cli)>1)
				{
					echo "<td>";
					echo $row["cocl_codecli"];
		
					// Affichage détail client
					$detailcli = "->";
					if ($row["cli_ste"] <> "")
						$detailcli .= $row["cli_ste"].", ";
					$detailcli .= $row["cli_civilite"]." ".$row["cli_nom"];
					echo "<br>";
					echo "<span class=\"annot_petit_it\">";
					echo htmlspecialchars($detailcli,ENT_NOQUOTES);
					echo "</span>";
					// Fin affichage détail client
					
					echo "</td>\n";
				}
				break;
			default :
				echo "<td>";
				echo $row["cocl_codecli"];
	
				// Affichage détail client
				$detailcli = "->";
				if ($row["cli_ste"] <> "")
					$detailcli .= $row["cli_ste"].", ";
				$detailcli .= $row["cli_civilite"]." ".$row["cli_nom"];
				echo "<br>";
				echo "<span class=\"annot_petit_it\">";
				echo htmlspecialchars($detailcli,ENT_NOQUOTES);
				echo "</span>";
				// Fin affichage détail client
				
				echo "</td>\n";
				break;
		}

         echo "<td align=\"center\">".formatedate($row["cocl_datecrea"])."</td>\n";
         echo "<td align=\"center\">".formatedate($row["cocl_dated"])."</td>\n";
         echo "<td align=\"center\">";
		 if (formatedate($row["cocl_datef"]) == "")
		 	echo "&nbsp;";
		 else
			echo formatedate($row["cocl_datef"]);
		 echo "</td>\n";
		// Temps
		 echo "<td>";
		 echo "<div align=\"center\">";
		 echo "<span class=\"annot_petit_it\">";
		 if ($row["cocl_typh"] == 1)
		 {
			echo "Solde initial : ".heure_to_hhmm(sec_heure($row["cocl_nbs"],3,TRUE));
			$solde=soldes_contrat($row["cocl_code"],FALSE);
			$soldeas=tps_trav_non_clot($row["cocl_code"],FALSE);
			echo "<br>Solde actuel : ";
			echo heure_to_hhmm(sec_heure($solde,3,TRUE));
			echo "<br>A sortir : ";
			echo heure_to_hhmm(sec_heure($soldeas,3,TRUE));
			$solde_reel=$solde-$soldeas;
			echo "<br>Solde r&eacute;el : ";
			echo heure_to_hhmm(sec_heure($solde_reel,3,TRUE));
		 }
		 else
		 	echo "-";			
		 echo "</span>";
		 echo "</div>";
		 echo "</td>\n";
		// Relancé le
		 echo "<td>";
		 echo "<div align=\"center\">";
		 if (formatedate($row["cocl_daterel"]) == "")
			echo "&nbsp;";
		 else
			echo formatedate($row["cocl_daterel"]);
		 echo "</div>";
		 echo "</td>\n";

		// Renouvellement
         echo "<td>";

		 // Affichage détail renouvelé
			 echo "<div align=\"center\">";
			 echo $non_oui[$row["cocl_renouv"]];
			 echo "</div>";
	
		 // Fin affichage détail renouvelé

		 echo "</td>\n";

		 echo "</tr>\n";
	 // Fin affichage résumé Contrat Client
	}  
	echo "</table>\n";
	// *** Fin affichage de la table
	
	mysql_free_result($rs);
	mysql_close($conn);  
}
else
{
?>
	<script type="text/javascript">
	<!--
	window.close();
	//-->
	</script>
<?PHP
}
?>
</body>
</html>