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

/************************************************/
/* Fonctions pour les accès aux fichiers Tâches */
/************************************************/

/** Tâches */

// Lecture d'un enregistrement tâche
function lit_enr_tac($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBTAC. "` WHERE `tac_code` = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Vérification du nombre de tâches
function nb_tac($typrat,$coderat,$open)
{
	global
		$_SESSION;
		
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rexist = "SELECT * FROM `" .TBTAC."`";
	if ((intval($typrat)>=1) AND (intval($typrat)<=6))
		$rexist.=" WHERE tac_typrat=".intval($typrat);
	if (trim($coderat)<>"")
	{
		if(!stristr($rexist,"WHERE "))
			$rexist.=" WHERE tac_coderat='".$coderat."'";
		else
			$rexist.=" AND tac_coderat='".$coderat."'";
	}

	// Filtre pour les techniciens agences ou sous-traitants
	if ((@$_SESSION["niv_log"] == 3) OR (@$_SESSION["niv_log"] == 11))
	{
		if(!stristr($rexist,"WHERE "))
			$rexist .= " WHERE ".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
		else
			$rexist .= " AND ".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
	}
	
	// Filtre pour les agences
	if (@$_SESSION["niv_log"] == 4)
	{
		// On construit la liste des techniciens actifs de l'agence
		$tab_lst_tec=array();
		$rexist2 = "SELECT tec_code FROM ".TBTEC." WHERE tec_codeage='".@$_SESSION["code_log"]."' AND tec_out=0";
		$rs2 = mysql_query($rexist2) or die(mysql_error());
		while($row2 = mysql_fetch_array($rs2))
		{
			extract($row2);
			$tab_lst_tec[]=$tec_code;
		}
	
		if(!stristr($rexist,"WHERE "))
			$rexist .= " WHERE (".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
		else
			$rexist .= " AND (".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
		foreach ($tab_lst_tec as $key=>$value)
		{
			$rexist .= " OR ".TBTAC.".tac_codedest='".$value."'";
		}
		$rexist.=")";
	}
	
	// Filtre pour le centre de gestion
	if (@$_SESSION["niv_log"] == 5)
	{
		if(!stristr($rexist,"WHERE "))
			$rexist .= " WHERE ".TBTAC.".tac_codedest<>'".$admin_code."'";
		else
			$rexist .= " AND ".TBTAC.".tac_codedest<>'".$admin_code."'";
	}
	
	// Filtre pour les sous-traitants
	if (@$_SESSION["niv_log"] == 10)
	{
		// On construit la liste des techniciens actifs du sous-traitant
		$tab_lst_tec=array();
		$rexist2 = "SELECT tec_code FROM ".TBTEC." WHERE tec_codest='".@$_SESSION["code_log"]."' AND tec_out=0";
		$rs2 = mysql_query($rexist2) or die(mysql_error());
		while($row2 = mysql_fetch_array($rs2))
		{
			extract($row2);
			$tab_lst_tec[]=$tec_code;
		}
	
		if(!stristr($rexist,"WHERE "))
			$rexist .= " WHERE (".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
		else
			$rexist .= " AND (".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
		foreach ($tab_lst_tec as $key=>$value)
		{
			$rexist .= " OR ".TBTAC.".tac_codedest='".$value."'";
		}
		$rexist.=")";
	}

	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);

	return $nbexist;
}

// Vérification du nombre de tâches actives
function nb_tac_actives($typrat,$coderat,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rexist = "SELECT * FROM `" .TBTAC."`";
	if ((intval($typrat)>=1) AND (intval($typrat)<=6))
		$rexist.=" WHERE tac_typrat=".intval($typrat);
	if (trim($coderat)<>"")
	{
		if(!stristr($rexist,"WHERE "))
			$rexist.=" WHERE tac_coderat='".$coderat."'";
		else
			$rexist.=" AND tac_coderat='".$coderat."'";
	}
	// Filtre pour les techniciens agences ou sous-traitants
	if ((@$_SESSION["niv_log"] == 3) OR (@$_SESSION["niv_log"] == 11))
	{
		if(!stristr($rexist,"WHERE "))
			$rexist .= " WHERE ".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
		else
			$rexist .= " AND ".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
	}
	
	// Filtre pour les agences
	if (@$_SESSION["niv_log"] == 4)
	{
		// On construit la liste des techniciens actifs de l'agence
		$tab_lst_tec=array();
		$rexist2 = "SELECT tec_code FROM ".TBTEC." WHERE tec_codeage='".@$_SESSION["code_log"]."' AND tec_out=0";
		$rs2 = mysql_query($rexist2) or die(mysql_error());
		while($row2 = mysql_fetch_array($rs2))
		{
			extract($row2);
			$tab_lst_tec[]=$tec_code;
		}
	
		if(!stristr($rexist,"WHERE "))
			$rexist .= " WHERE (".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
		else
			$rexist .= " AND (".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
		foreach ($tab_lst_tec as $key=>$value)
		{
			$rexist .= " OR ".TBTAC.".tac_codedest='".$value."'";
		}
		$rexist.=")";
	}
	
	// Filtre pour le centre de gestion
	if (@$_SESSION["niv_log"] == 5)
	{
		if(!stristr($rexist,"WHERE "))
			$rexist .= " WHERE ".TBTAC.".tac_codedest<>'".$admin_code."'";
		else
			$rexist .= " AND ".TBTAC.".tac_codedest<>'".$admin_code."'";
	}
	
	// Filtre pour les sous-traitants
	if (@$_SESSION["niv_log"] == 10)
	{
		// On construit la liste des techniciens actifs du sous-traitant
		$tab_lst_tec=array();
		$rexist2 = "SELECT tec_code FROM ".TBTEC." WHERE tec_codest='".@$_SESSION["code_log"]."' AND tec_out=0";
		$rs2 = mysql_query($rexist2) or die(mysql_error());
		while($row2 = mysql_fetch_array($rs2))
		{
			extract($row2);
			$tab_lst_tec[]=$tec_code;
		}
	
		if(!stristr($rexist,"WHERE "))
			$rexist .= " WHERE (".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
		else
			$rexist .= " AND (".TBTAC.".tac_codedest='".@$_SESSION["code_log"]."'";
		foreach ($tab_lst_tec as $key=>$value)
		{
			$rexist .= " OR ".TBTAC.".tac_codedest='".$value."'";
		}
		$rexist.=")";
	}

	if(!stristr($rexist,"WHERE "))
		$rexist.=" WHERE tac_datefin='0000-00-00'";
	else
		$rexist.=" AND tac_datefin='0000-00-00'";

	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);

	return $nbexist;
}

/** Catégories de tâches **/

// Lecture d'un enregistrement tâche
function lit_enr_tacc($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBTACC. "` WHERE `tacc_code` = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Vérification du nombre de catégories de tâches
function nb_tacc()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBTACC;
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

?>