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

Version 0.9.7

Copyright (C) 2006-2007  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_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_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/lst.inc.php");
Archivelog(@$_SESSION["code_log"]);
?>
<!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 - ESPACE PRIVE : Liste Contrats Clients</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 divers -->
<script language="JavaScript" src="../jscript/divers.js"></script>

</head>

<body onLoad="metFocus();">

<?php
include_once("entete.inc.php");
if (@$_SESSION["niv_log"] >= 3)
{
?>
<p align="center"><span class="titrepage">CONTRATS CLIENTS</span></p>
<?PHP
}

if (@$_SESSION["niv_log"] == 1)
{
?>
<p align="center"><span class="titrepage">VOS CONTRATS</span></p>
<?PHP
}

if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
{
?>
<p align="center" class="annot_std_it">(G&eacute;r&eacute;s par votre agence)</p>
<?PHP
}
if ((@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 4))
{
	$url_add = "cocl_maj.php?td=add&co=&cc=&ca=";
}
if ((@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
{
	$fic_imp = "cocl_lsti.php";
}

if (@$_SESSION["niv_log"] == 1)
{
	$fic_imp = "cocl_lsti.php";
}

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

if (@$_SESSION["niv_log"] >=3)
{
	$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"]."'";
	}
	$rech .= crestr_triord($cocl_titre_col,"cocl_code",FALSE,$limit);
}

if (@$_SESSION["niv_log"] ==1)
{
	$rech = cree_strrechlstcoclc();
	if(!stristr($rech,"WHERE "))
		$rech .= " WHERE cocl_codecli='".@$_SESSION["code_log"]."'";
	else
		$rech .= " AND cocl_codecli='".@$_SESSION["code_log"]."'";
		
	$rech .= crestr_triord($coclc_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
{
	include_once("formr.inc.php"); 

	if (@$_SESSION["niv_log"] >=3)
	{
		$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"]."'";
		}
		$rech .= crestr_triord($cocl_titre_col,"cocl_code",TRUE,$limit);
	}

	if (@$_SESSION["niv_log"] ==1)
	{
		$rech = cree_strrechlstcoclc();
		if(!stristr($rech,"WHERE "))
			$rech .= " WHERE cocl_codecli='".@$_SESSION["code_log"]."'";
		else
			$rech .= " AND cocl_codecli='".@$_SESSION["code_log"]."'";
			
		$rech .= crestr_triord($coclc_titre_col,"cocl_code",TRUE,$limit);
	}		 

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

		
	if (($numrows > 0) AND (@$_SESSION["niv_log"] >= 4))
	{
		echo "<a href=\"coclrel_maj.php\">Relancer les contrats non reçus</a>";
		echo " / ";
		echo "<a href=\"coclrec_maj.php\">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
	if (@$_SESSION["niv_log"] >=3)
		titlst(TRUE,$cocl_titre_col);
	if (@$_SESSION["niv_log"] ==1)
		titlst(TRUE,$coclc_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\">";
		 $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";

		if (@$_SESSION["niv_log"] >=3)
		{
			echo "<td>";
			echo $row["cocl_codecli"];
			echo " <a href=\"cli_maj.php?td=edit&cc=".$row["cocl_codecli"]."&dep=".$row["cli_dep"]."\"><img src=\"../img/edit.png\" width=\"14\" height=\"14\" border=\"0\" align=\"absmiddle\" title=\"Editer client\"></a>";

			// 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";
		}

		// Création
		 echo "<td>";
		 echo "<div align=\"center\">";
		 echo formatedate($row["cocl_datecrea"]);
		 echo "</div>";
		 echo "</td>\n";
		// Début
		 echo "<td>";
		 echo "<div align=\"center\">";
		 echo formatedate($row["cocl_dated"]);
		 echo "</div>";
		 echo "</td>\n";
		// Fin
		 echo "<td>";
		 echo "<div align=\"center\">";
		 if (formatedate($row["cocl_datef"]) == "")
			echo "&nbsp;";
		 else
			echo formatedate($row["cocl_datef"]);
		 echo "</div>";
		 echo "</td>\n";
		// Temps
		 echo "<td>";
		 echo "<div align=\"center\">";
		 if ($row["cocl_typh"] == 1)
		 {
			 echo substr(sec_heure($row["cocl_nbs"]),0,5);
			 if (formatedate($row["cocl_datesig"]) <> "")
			 {
				 echo "<span class=\"annot_petit_it\">";
				 echo "<br>Solde : ".substr(sec_heure($row["cocl_soldes"]),0,5);
				 echo "<br>A sortir : ".substr(sec_heure(tps_trav_non_clot($row["cocl_code"],FALSE)),0,5);
				 echo "</span>";
			 }
		 }
		 else
		 	echo "-";			
		 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"]];

			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 (@$_SESSION["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 (@$_SESSION["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 (@$_SESSION["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"]."&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 (@$_SESSION["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_histo = TRUE;
		
		 $urlhisto = "cocls_pdf.php?co=".$row["cocl_code"];
		 echo " <a href=\"javascript:;\" onclick=\"lanceimp('".$urlhisto."');\")><img src=\"../img/histo.png\" title=\"Synth&egrave;se du contrat\" 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);  

	$taille_piedla="";
	include_once("piedla.inc.php");
}
include_once("pied.php");
?>