<?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_ot.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.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"]);

$tx = strip_tags(trim($_GET['tx'])); // Source de l'appel m:depuis le menu, i:depuis l'intervention
$ci = strip_tags(trim($_GET['ci'])); // Récupération code Intervention

if (($tx<>"m") AND ($tx<>"i"))
	$pbm_ot=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 - ESPACE PRIVE : Liste Ordres Sous-traitants</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 pour contrôle de champs -->
<script	type="text/javascript" src="../jscript/verif_field.js"></script>

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

</head>

<body onLoad="metFocus();">
<?php
if ($tx == "m") 
	include_once("entete.inc.php");
if ((@$_SESSION["niv_log"] == 3) OR (@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9))
{
?>
<p align="center"><span class="titrepage">BONS DE TRAVAIL</span></p>
<?PHP
}
else
{
?>
<p align="center"><span class="titrepage">VOS BONS DE TRAVAIL</span></p>
<?PHP
}
	if ($ci<>"")
	{
?>
<p align="center"><span class="annot_std_it">(Pour l'intervention n°<span class="annot_std_gras"><?PHP echo $ci; ?></span>)</span></p>
<?PHP } if ($tx == "i") { ?>
<p align="right"><input name="quit" type="button" class="bton_fmarr_std" id="quit" value="Quitter" style="text-decoration: none; font-size: 11px; color:White ;font-weight:bold;background-color: #666666;" onClick="javascript:; self.close();"></p>
<?PHP } ?>
<?PHP
$fic_imp = "ot_lsti.php";

if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3) OR (@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9))
{
	if ($ci<>"")
		$url_add = "ot_maj.php?tx=".$tx."&td=add&ci=".$ci;
}
$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
mysql_select_db(DBNAME);

if ((@$_SESSION["niv_log"] <> 11) AND (@$_SESSION["niv_log"] <> 10))
	$rech = cree_strrechlstot($niv_urgence);
else
	$rech = cree_strrechlstotst($niv_urgence);

if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
{
	if(!stristr($rech,"WHERE "))
		$rech .= " WHERE ".TBINT.".int_codeage='".@$_SESSION["code_age"]."'";
	else
		$rech .= " AND ".TBINT.".int_codeage='".@$_SESSION["code_age"]."'";
}
if ((@$_SESSION["niv_log"] == 11) OR (@$_SESSION["niv_log"] == 10))
{
	if(!stristr($rech,"WHERE "))
		$rech .= " WHERE ".TBOT.".ot_codest='".@$_SESSION["code_st"]."'";
	else
		$rech .= " AND ".TBOT.".ot_codest='".@$_SESSION["code_st"]."'";
}
if ($ci<>"")
{
	if(!stristr($rech,"WHERE "))
		$rech .= " WHERE ".TBOT.".ot_codeint='".$ci."'";
	else
		$rech .= " AND ".TBOT.".ot_codeint='".$ci."'";
}
$rech .= crestr_triord($ot_titre_col,"ot_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"] <> 11) AND (@$_SESSION["niv_log"] <> 10))
		$rech = cree_strrechlstot($niv_urgence);
	else
		$rech = cree_strrechlstotst($niv_urgence);

	if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
	{
		if(!stristr($rech,"WHERE "))
			$rech .= " WHERE ".TBINT.".int_codeage='".@$_SESSION["code_age"]."'";
		else
			$rech .= " AND ".TBINT.".int_codeage='".@$_SESSION["code_age"]."'";
	}
	if ((@$_SESSION["niv_log"] == 11) OR (@$_SESSION["niv_log"] == 10))
	{
		if(!stristr($rech,"WHERE "))
			$rech .= " WHERE ".TBOT.".ot_codest='".@$_SESSION["code_st"]."'";
		else
			$rech .= " AND ".TBOT.".ot_codest='".@$_SESSION["code_st"]."'";
	}
	if ($ci<>"")
	{
		if(!stristr($rech,"WHERE "))
			$rech .= " WHERE ".TBOT.".ot_codeint='".$ci."'";
		else
			$rech .= " AND ".TBOT.".ot_codeint='".$ci."'";
	}
	$rech .= crestr_triord($ot_titre_col,"ot_code",FALSE,$limit);

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

	// *** 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"] <> 11) AND (@$_SESSION["niv_log"] <> 10))
		titlst(TRUE,$ot_titre_col);
	else
		titlst(TRUE,$ot2_titre_col);
	echo "<th>".ucfirst("Cliquer")."</th>\n";
		
	
	$i = 0;
	
	
	while ($row = mysql_fetch_assoc($rs)) 
	{         
	 // Affichage résumé ordre

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

		echo "<td>";
		echo $row["ot_code"];

		// Affichage détail intervention rattachée
		if ($tx=="m")
		{
			if ((@$_SESSION["niv_log"] <> 11) AND (@$_SESSION["niv_log"] <> 10))
			{
				echo "<div class=\"annot_petit_it\" align=\"right\">";
				$txtint="";
				if ($row["int_codedo"]<>"")
					$txtint="Ordre intervention";
				else
					$txtint="Intervention";
				echo $txtint." ".$row["ot_codeint"];
				echo " <a href=\"int_maj.php?td=edit&ci=".$row["ot_codeint"]."\"><img src=\"../img/edit.png\" width=\"14\" height=\"14\" border=\"0\" align=\"absmiddle\" title=\"Editer/Voir ".$txtint."\"></a>";
				echo "</div>";
			}
			echo "<div class=\"annot_petit_it\" align=\"right\">";
			echo Client." ".$row["int_codecli"];
			if ((@$_SESSION["niv_log"] <> 11) AND (@$_SESSION["niv_log"] <> 10))
			{
				echo " <a href=\"cli_maj.php?td=edit&cc=".$row["int_codecli"]."\"><img src=\"../img/edit.png\" width=\"14\" height=\"14\" border=\"0\" align=\"absmiddle\" title=\"Editer client\"></a>";
			}
			echo "</div>";
		}
		echo "</td>\n";

		echo "<td>";
		echo "<div align=\"center\">";
		echo formatedate($row["ot_datedde"]);
		echo "</div>";
		echo "</td>\n";

		 // Affichage Icône Lieu
		 echo "<td>";
		 echo "<div align=\"center\">";
		 
		 $lieu = $row["ot_codelieu"];
		 $rechlieu = "SELECT * FROM ".TBLIEU." WHERE lieu_code = '$lieu'";
		 
		 $rslieu = mysql_query($rechlieu) or die(mysql_error());
		 
			if (mysql_num_rows($rslieu) <> 0)
			{
				 while ($rowlieu = mysql_fetch_array($rslieu)) 
				{
					extract ($rowlieu);
					if ($rowlieu["lieu_img"] <> "")
					{
						$lieuint = $rowlieu["lieu_desc"]." (".$lieu.")";
						echo "<img src=\"".$rowlieu["lieu_img"]."\" title=\"".$lieuint."\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\">";
					}
					else
					{
						 echo "<span class=\"annot_std_it\">";
						 echo $lieu;
						 echo "</span>";
					}
				}
			}
		 
			mysql_free_result($rslieu);

		 echo "</div>";
		 echo "</td>\n";

		echo "<td>";
		
		// Affichage détail urgence
		
		echo "<div align=\"center\">";
		echo $niv_urgence[$row["ot_urg"]];
		echo "</div>";
		
		// Fin affichage détail urgence
		
		echo "</td>\n";

		if ((@$_SESSION["niv_log"] <> 11) AND (@$_SESSION["niv_log"] <> 10))
		{
			echo "<td>";
			echo $row["ot_codest"];
			if ($tx=="m")
			{
				$rech2="SELECT st_dep FROM ".TBST." WHERE `st_code`='".$row["ot_codest"]."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				if ($row2 = mysql_fetch_array($rs2))
				{
					echo " <a href=\"st_maj.php?td=edit&code=".$row["ot_codest"]."&dep=".$row2["st_dep"]."\"><img src=\"../img/edit.png\" width=\"14\" height=\"14\" border=\"0\" align=\"absmiddle\" title=\"Editer sous-traitant\"></a>";
				}
			}
			
			// Affichage détail sous-traitant
			$detailst = "->";
			$detailst .= $row["st_nom"].", ";
			$detailst .= $row["st_resp_civilite"]." ".$row["st_resp_nom"];
			echo "<div class=\"annot_std_it\">";
			echo $detailst;
			echo "</div>";
			
			// Fin affichage détail sous-trainat
			
			echo "</td>\n";
		}

		 // Affichage Icône Statut
		 echo "<td>";
		 echo "<div align=\"center\">";
		 
		 $statut = $row["ot_codestatut"];
		 $rechico = "SELECT * FROM ".TBSTA." WHERE statut_code = '$statut'";
		 
		 $rsico = mysql_query($rechico) or die(mysql_error());
		 
			if (mysql_num_rows($rsico) <> 0)
			{
				 while ($rowico = mysql_fetch_array($rsico)) 
				{
					extract ($rowico);
					if ($rowico["statut_img"] <> "")
					{
						$statint = $rowico["statut_desc"]." (".$statut.")";
						echo "<img src=\"".$rowico["statut_img"]."\" title=\"".$statint."\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\">";
					}
					else
					{
						 echo "<span class=\"annot_std_it\">";
						 echo $statut;
						 echo "</span>";
					}
				}
			}
		 
			mysql_free_result($rsico);

		 echo "</div>";
		 echo "</td>\n";

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

		if ($row["ot_codestatut"] <> "CLOT")
		{
			$lst_maj = TRUE;
			echo "<a href=\"ot_maj.php?tx=".$tx."&td=edit&ci=".$row["ot_codeint"]."&co=".$row["ot_code"]."\"><img src=\"../img/edit.png\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
			echo " ";
		}
		else
		{
			$lst_voir = TRUE;
			echo "<a href=\"ot_maj.php?tx=".$tx."&td=view&ci=".$row["ot_codeint"]."&co=".$row["ot_code"]."\"><img src=\"../img/detail.gif\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
			echo " ";
		}

		$lst_imp = TRUE;
		$urlimp = "ot_pdf.php?co=".$row["ot_code"];
		echo "<a href=\"javascript:;\" onclick=\"lanceimp('".$urlimp."');\")><img src=\"../img/print.gif\" title=\"Imprimer le bon\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
		echo " ";

		if ($row["ot_codestatut"] <> "CLOT")
		{
			$del=TRUE;

			if ((@$_SESSION["niv_log"] == 11) OR (@$_SESSION["niv_log"] == 10))
				$del=FALSE;
	
			if ($del==TRUE)
			{
				$lst_del = TRUE;
				echo "<a href=\"ot_maj.php?tx=".$tx."&td=del&ci=".$row["ot_codeint"]."&co=".$row["ot_code"]."\"><img src=\"../img/del.gif\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
			}
		}
		 echo "</div>";
		 echo "</td>\n";

		 echo "</tr>\n";
	 // Fin affichage résumé ordre
		
		$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'
		if(!empty($_GET['tx']))
			$url .= "&tx=".$_GET['tx']; // CREATION de 'tx'
	   
	   
	   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'
		if(!empty($_GET['tx']))
			$url .= "&tx=".$_GET['tx']; // CREATION de 'tx'
						  
		  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'
		if(!empty($_GET['tx']))
			$url .= "&tx=".$_GET['tx']; // CREATION de 'tx'
	   
	   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"); 
}
	if ($tx=="i")
	{
?>
	<script type="text/javascript">
	verif_ot(<?PHP echo nb_ot($ci); ?>,<?PHP echo nb_ot_actifs($ci); ?>);
	</script>
<?PHP
	}
if ($tx == "m") 
	include_once("pied.php");
?>
