<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Copyright (C) 2006  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
****************************************************************************************/

include ("../inc/config.inc.php");
?>
<?php require ("../inc/func.inc.php") ?>
<?php require ("../inc/func_date.inc.php") ?>
<?php require ("../inc/func_log.inc.php") ?>
<?php require ("../inc/fic.inc.php") ?>
<?php require ("../inc/lst.inc.php") ?>
<?PHP
	Archivelog(@$HTTP_SESSION_VARS["code_log"]);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<title>SMI - ESPACE PRIVE : Liste Contrats Clients</title>

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

<!-- Feuille de style & Script pour le menu -->
<link href="../inc/menu.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="../coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour affichages -->
<script language="JavaScript" src="../inc/aff.js"></script>

<!-- Affichage nouvelle fenêtre pour impression -->
<script type="text/javascript">
	function lanceimp(page)
	{
	  window.open(page,'impression','width='+screen.width+' height='+screen.height);
	}
</script>

</head>

<body>

<?php
	include_once ("../inc/entete.php"); 
?>

<p align="center"><span class="titrepage">LISTE DES CONTRATS CLIENTS </span></p>
<?PHP
if ((@$HTTP_SESSION_VARS["niv_log"] == 4) OR (@$HTTP_SESSION_VARS["niv_log"] == 3))
{
?>
<p align="center" class="annot_std_it">(G&eacute;r&eacute;s par votre agence)</p>
<?PHP
}
	if (@$HTTP_SESSION_VARS["niv_log"] >= 4)
	{
		$fic_imp = "cocl_lsti.php";
		$url_add = "cocl_maj.php?td=add&co=&cc=&ca=";
	}
	include_once ("../inc/formr.php"); 

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

	$rech = cree_strrechlstcocl();
	if ((@$HTTP_SESSION_VARS["niv_log"] == 4) OR (@$HTTP_SESSION_VARS["niv_log"] == 3))
	{
		if(!stristr($rech,"WHERE "))
			$rech .= " WHERE ".TBCOCL.".cocl_codeage='".@$HTTP_SESSION_VARS["code_age"]."'";
		else
			$rech .= " AND ".TBCOCL.".cocl_codeage='".@$HTTP_SESSION_VARS["code_age"]."'";
	}
	$rech .= crestr_triord($cocl_titre_col,"cocl_code",FALSE,$limit);
		 
	$rs = mysql_query($rech) or die(mysql_error());
	$numrows = mysql_num_rows($rs); // Nombre de résultats à la requête
	

	// *** Pagination
	if(empty($_GET['page']))$_GET['page'] = 0; // Valeur de par défaut du nombre de pages
	
	$pages = intval($numrows/$limit); // Nombre total de pages
	
	if($numrows%$limit) $pages++; // S'il y a un reste à la division, alors page+1
	$current = ($_GET['page']/$limit) + 1; // Numéro de page courante
		
	if(($pages < 1) || ($pages == 0)) 
	  $total = 1; // Si le nombre de pages est inférieur à 1 ou égal à 0, alors le nombre total de pages est 1
	else 
	 $total = $pages; // Sinon, on conserve la variable $page telle qu'elle
		
	$first = $_GET['page'] + 1; // Premier résultat
		
	if(((($_GET['page'] + $limit) / $limit) >= $pages) && $pages != 1) 
		 $last = $_GET['page'] + $limit; // Si ce n'est pas la dernière page de résultats, alors la dernière page de résultat est $page + $limit
	else
		$last = $numrows; // Si c'est la dernière page de résultats, le dernier résultat est égal au nombre total de résultats
	// *** Fin de Pagination

	if($numrows == 0)
	{
		retpp();
	}
	 else
	{
		$rech = cree_strrechlstcocl();
		if ((@$HTTP_SESSION_VARS["niv_log"] == 4) OR (@$HTTP_SESSION_VARS["niv_log"] == 3))
		{
			if(!stristr($rech,"WHERE "))
				$rech .= " WHERE ".TBCOCL.".cocl_codeage='".@$HTTP_SESSION_VARS["code_age"]."'";
			else
				$rech .= " AND ".TBCOCL.".cocl_codeage='".@$HTTP_SESSION_VARS["code_age"]."'";
		}
		$rech .= crestr_triord($cocl_titre_col,"cocl_code",TRUE,$limit);
	}

	// *** Fin de Chaîne de Recherche

	$rs = mysql_query($rech) or die(mysql_error());
	$numrows = mysql_num_rows($rs);

		
	if (($numrows > 0) AND (@$HTTP_SESSION_VARS["niv_log"] >= 4))
	{
		echo "<a href=\"coclrel_maj.php?ta=m\">Relancer les contrats non reçus</a>";
		echo " / ";
		echo "<a href=\"coclrec_maj.php?ta=m\">Accuser réception des contrats reçus</a>";
	}

	// *** Affichage de la table
     echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";     

	// Création entête
	titlst(TRUE,$cocl_titre_col);
    echo "<th>".ucfirst("Cliquer")."</th>\n";
		
	
	$i = 0;
	
	while ($row = mysql_fetch_assoc($rs)) 
	{         
	 // Affichage résumé Contrat Client

		// Création de l'entête de ligne (TR)
		creetrlst($i);

		 $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"]) <> "")
		  {
		  	$lst_sig = TRUE;
			 echo " <img src=\"../img/ok.gif\" title =\"Signé le ".formatedate($row["cocl_datesig"])."\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\">";
		  }

		 // CREATION d'icône si terminé
		  if ((formatedate($row["cocl_datefin"]) <> "") OR ($echeance_ok == FALSE))
		  {
		  	$lst_fini = TRUE;
			if (formatedate($row["cocl_datefin"]) <> "")
				echo " <img src=\"../img/warn.gif\" title =\"Terminé le ".formatedate($row["cocl_datefin"])."\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\">";
			else
				echo " <img src=\"../img/warn.gif\" title =\"Terminé le ".formatedate($row["cocl_datef"])."\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\">";
		  }

		 // Détail contrat
		 echo "<br>";
		 echo "<span class=\"annot_petit_it\">";
		 $rech2 = "SELECT * FROM " .TBCO. " WHERE co_code = '".$row["cocl_codeco"]."'";
		 $rs2 = mysql_query($rech2) or die(mysql_error());
		 if ($row2 = mysql_fetch_array($rs2))
		 {
			$txt = formatetxt($row2["co_nom"]);
			$txt = cesuretxt($txt,40);
			echo "->".$txt;
		 }
		 echo "</span>";
		 echo "</td>\n";
		 
         echo "<td>".$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 $detailcli;
			 echo "</span>";
	
		 // Fin affichage détail client

		 echo "</td>\n";

         echo "<td>";
		 echo "<div align=\"center\">";
		 echo formatedate($row["cocl_datecrea"]);
		 echo "</div>";
		 echo "</td>\n";
         echo "<td>";
		 echo "<div align=\"center\">";
		 echo formatedate($row["cocl_dated"]);
		 echo "</div>";
		 echo "</td>\n";
         echo "<td>";
		 echo "<div align=\"center\">";
		 if (formatedate($row["cocl_datef"]) == "")
		 	echo "&nbsp;";
		 else
			echo formatedate($row["cocl_datef"]);
		 echo "</div>";
		 echo "</td>\n";
         echo "<td>";
		 echo "<div align=\"center\">";
		 if (formatedate($row["cocl_daterel"]) == "")
		 	echo "&nbsp;";
		 else
			echo formatedate($row["cocl_daterel"]);
		 echo "</div>";
		 echo "</td>\n";

         echo "<td>";

		 // Affichage détail renouvelé

			 echo "<div align=\"center\">";
			 echo $niv_renouv[$row["cocl_renouv"]];

			if ($row["cocl_renouv"] == 1)
			{
				 // Affichage détail renouvellement
	
				 $detailren = "(".$row["cocl_rnum"].")";
				 echo "<br>";
				 echo "<span class=\"annot_petit_it\">";
				 echo $detailren;
				 echo "</span>";
		
				 // Fin affichage détail renouvellement
			}

			 echo "</div>";
	
		 // Fin affichage détail renouvelé

		 echo "</td>\n";

		// CREATION lien dynamique sur icône (édition, suppression, etc...)
		 echo "<td>";
		 echo "<div align=\"center\">";


		 if (@$HTTP_SESSION_VARS["niv_log"] >= 4) // Edition uniquement si >=agence
		 {
			if ((formatedate($row["cocl_datefin"]) == "") AND ($echeance_ok == TRUE))
			{
				$lst_maj = TRUE;
				
				echo "<a href=\"cocl_maj.php?td=edit&co=".$row["cocl_code"]."&cc=".$row["cocl_codecli"]."\"><img src=\"../img/edit.png\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
			}
			else
			{
				$lst_voir = TRUE;
				
				echo "<a href=\"cocl_maj.php?td=view&co=".$row["cocl_code"]."&cc=".$row["cocl_codecli"]."\"><img src=\"../img/detail.gif\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
			}
		 }	

		 if (@$HTTP_SESSION_VARS["niv_log"] <= 3) // Voir si <=Technicien
		 {
			$lst_voir = TRUE;
			
			echo "<a href=\"cocl_maj.php?td=view&co=".$row["cocl_code"]."&cc=".$row["cocl_codecli"]."\"><img src=\"../img/detail.gif\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
		 }	

		 if (@$HTTP_SESSION_VARS["niv_log"] >= 3) // Création intervention uniquement si  >=technicien et pas fini
		 {
			 if ((formatedate($row["cocl_datefin"]) == "") AND (formatedate($row["cocl_datesig"]) <> "") AND ($echeance_ok == TRUE))
			 {
				$lst_int = TRUE;
				
				echo " ";
				$url_int = "int_maj.php?td=add&ci=&cc=".$row["cocl_codecli"]."&ct=&ca=".$row["cocl_codeage"]."&co=".$row["cocl_code"];
				echo "<a href=\"".$url_int."\"><img src=\"../img/int.gif\" title=\"Saisir intervention sur le contrat ".$row["cocl_code"]."\"  width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
			 }
		 }
		
		 if (@$HTTP_SESSION_VARS["niv_log"] >= 4) // Suppression uniquement si >=agence et si pas signé
		 {
			 if (formatedate($row["cocl_datesig"]) == "")
			 {
				$lst_del = TRUE;
	
				echo " ";
				echo "<a href=\"cocl_maj.php?td=del&co=".$row["cocl_code"]."&cc=".$row["cocl_codecli"]."&ca=".$row["cocl_codeage"]."\"><img src=\"../img/del.gif\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
			 }
		 }

		 $lst_imp = TRUE;
		
		 $urlimp = "cocl_pdf.php?code=".$row["cocl_code"];
		 echo " ";
		 echo "<a href=\"javascript:;\" onclick=\"lanceimp('".$urlimp."');\")><img src=\"../img/print.gif\" title=\"Imprimer le contrat\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
	
		 echo "</div>";
		 echo "</td>\n";

		 echo "</tr>\n";
	 // Fin affichage résumé Contrat Client
		
		$i++;    
	}  
	echo "</table>\n";
	// *** Fin affichage de la table
	
	mysql_free_result($rs);
	
	// *** Pagination de pied
	echo "<table width=\"100%\" border=\"0\">
	<tr>
	<td align=\"center\">\n";
		
	if (!empty($_GET['page']) && $_GET['page'] > 0)
	{
	   $back_page = $_GET['page'] - $limit;
	   $url = $_SERVER["PHP_SELF"]."?page=$back_page";
	
		if(!empty($_GET['search']))
			$url .= "&search=".$_GET['search']; // CREATION de 'search'
		if(!empty($_GET['rech']))
			$url .= "&rech=".$_GET['rech']; // CREATION de 'rech'
		if(!empty($_GET['ord']))
			$url .= "&ord=".$_GET['ord']; // CREATION de 'ord'
		if(!empty($_GET['tri']))
			$url .= "&tri=".$_GET['tri']; // CREATION de 'tri'
	   
	   
	   echo "<a href=\"$url\"><img src=\"../img/prec.png\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>    \n";
	}
	 
	// Boucle sur chaque page et CREATION d'un lien sur celle-ci
	// Pas de page s'il n'y a seulement qu'une page
	if($pages > 1)
	{
	 for ($i=1; $i <= $pages; $i++) 
	 {
	   $ppage = $limit*($i - 1);
	   if ($ppage == $_GET['page'])
		  echo("<span class=\"psel\">$i</span> \n");
	   else
	   {
		  $url = $_SERVER["PHP_SELF"]."?page=$ppage";
						  
		if(!empty($_GET['search']))
			$url .= "&search=".$_GET['search']; // CREATION de 'search'
		if(!empty($_GET['rech']))
			$url .= "&rech=".$_GET['rech']; // CREATION de 'rech'
		if(!empty($_GET['ord']))
			$url .= "&ord=".$_GET['ord']; // CREATION de 'ord'
		if(!empty($_GET['tri']))
			$url .= "&tri=".$_GET['tri']; // CREATION de 'tri'
						  
		  echo("<span class=\"pnsel\"><a href=\"$url\">$i</a></span> \n");
	   }
	 }
	}
	
	if(((($_GET['page']+$limit) / $limit) < $pages) && $pages != 1) 
	{   
	   $next_page = $_GET['page'] + $limit; // Si c'est la dernière page, pas de page suivante
	   $url = $_SERVER["PHP_SELF"]."?page=$next_page";
		
		if(!empty($_GET['search']))
			$url .= "&search=".$_GET['search']; // CREATION de 'search'
		if(!empty($_GET['rech']))
			$url .= "&rech=".$_GET['rech']; // CREATION de 'rech'
		if(!empty($_GET['ord']))
			$url .= "&ord=".$_GET['ord']; // CREATION de 'ord'
		if(!empty($_GET['tri']))
			$url .= "&tri=".$_GET['tri']; // CREATION de 'tri'
	   
	   echo "    <a href=\"$url\"><img src=\"../img/suiv.png\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>\n";
	}
	 
	echo "</td>
		 </tr>
		 </table>"; 

	// *** Fin de pagination de pied	

	mysql_close($conn);  

	include_once ("../inc/piedla.php"); 
?>

<?php 
	include_once ("pied.php")
?>