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

/**************************************************/
/* Fonctions pour les accès aux fichiers agences  */
/* et fichiers rattachés                          */
/**************************************************/

/** Catégories **/

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

	return $nbexist;
}

// Lecture d'un enregistrement agence
function lit_enr_acat($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBACAT. " WHERE acat_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;
}

/** Agences **/

// Vérification du nombre d'interventions sur une agence
function nb_int($agence)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if ($agence <> "")
		$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codeage` = '".$agence."'";
	else
		$rexist = "SELECT * FROM `" .TBINT. "`";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérification du type de facturation sur une agence (par le CDG ou par l'agence)
function mod_fact_age($code)
{
	$fact_age = 0;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBAGE. " WHERE age_code = '$code'";
	$rs2 = mysql_query($rech2) or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
		$fact_age = $row2["age_fact"];
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $fact_age;
}

// Vérification du nombre d'agences actives
function nb_age_actives()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBAGE. "`  WHERE `age_ouvert` = '1'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérifie si un client est bien géré par une agence (dans le département couvert et pas déjà géré par une autre agence)
// $ouvrir : TRUE -> On ouvre et on referme le fichier
function client_age_ok($client,$agence,$ouvrir)
{
	$result = FALSE;
	
	if ($ouvrir==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	// On lit le client
	$rech = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$client."' AND (`cli_prop` = '".$agence."' OR `cli_prop` IS NULL)";
	$rs = mysql_query($rech) or die(mysql_error());
	if (mysql_num_rows($rs) >0)
	{
		if ($res = mysql_fetch_array($rs)) 
		{
			extract($res);
			if ($cli_prop<>$agence)
			{
				// Ce client n'est pas affecté à cette agence, on lit l'agence pour vérifier si les départements sont ok
				$rech2 = "SELECT * FROM `" .TBZCC. "` WHERE `zcc_code` = '".$agence."' AND `zcc_codepays` = '".$cli_codepays."' AND `zcc_codedep` = '".$cli_dep."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				if (mysql_num_rows($rs2) > 0)
					$result=TRUE;
				mysql_free_result($rs2);
			}
			else
				$result=TRUE;
		}
	}	
	mysql_free_result($rs);
	if ($ouvrir==TRUE)
		mysql_close($conn2);
	return $result;
}

// Donne le nombre d'agences
function nb_age()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBAGE. "`";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Lecture d'un enregistrement agence
function lit_enr_age($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBAGE. " WHERE age_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 de la présence d'une agence dans un contrat non clôt
function exist_age_in_cocl_ouv($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codeage` = '$code' AND `cocl_signe` IS NOT NULL AND `cocl_datefin` = '0000-00-00'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist=TRUE;
	else
		$exist=FALSE;
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $exist;
}

// Vérification de la présence d'une agence dans une fiche intervention
function exist_age_in_int($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codeage` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist=TRUE;
	else
		$exist=FALSE;
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $exist;
}

// Vérification de la présence d'une agence dans une fiche intervention non close
function exist_age_in_int_ouv($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codeage` = '$code' AND `int_datefin` = '0000-00-00'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist=TRUE;
	else
		$exist=FALSE;
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $exist;
}

// Vérification de la présence d'une agence dans une fiche technicien non licencié
function exist_age_in_tec_present($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist= "SELECT * FROM `" .TBTEC. "` WHERE `tec_codeage` = '$code' AND `tec_out` = '0'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist = TRUE;
	else
		$exist = FALSE;
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $exist;
}

// Vérification de la présence d'une agence dans une fiche technicien
function exist_age_in_tec($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist= "SELECT * FROM `" .TBTEC. "` WHERE `tec_codeage` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist = TRUE;
	else
		$exist = FALSE;
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $exist;
}

?>