<?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 clients */
/* et fichiers rattachés                         */
/*************************************************/

/** Clients **/

// Vérification du nombre de catégories de clients
function nb_ccat()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCCAT;
	$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 nombre de clients
// dans l'ensemble ou pour une agence donnée
function nb_cli($agence)
{
	$nbexist = 0;

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

	if ($agence <> "")
	{
		$rexist2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$agence."'";
		$rs2 = mysql_query($rexist) or die(mysql_error());
		if ($row2 = mysql_fetch_array($rs2))
		{
			extract($row2);

			$rexist3 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_codepays`='".$age_codepays."' OR `cli_prop`='".$age_code."'";
			$rs3 = mysql_query($rexist3) or die(mysql_error());
			if (mysql_num_rows($rs2) <> 0)
			{
				while ($resultat = mysql_fetch_array($rs2)) 
				{
					extract($resultat);
					
					if ($cli_prop<>$age_code)
					{
						if (dep_in_zcc_pays_open($cli_dep,$cli_codepays,FALSE) == TRUE)
							$nbexist++;
					}
					else
						$nbexist++;
				}
			}
			mysql_free_result($rs3);
		}
	}
	else
	{
		$rexist2 = "SELECT * FROM `" .TBCLI. "`";
		$rs2 = mysql_query($rexist2) or die(mysql_error());
		$nbexist = mysql_num_rows($rs2);
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérification du nombre d'interventions sur un client
// Filtre sur niveau et sur code agence/donneur d'ordre
function nb_int_cli($client,$level,$codead)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codecli` = '".$client."'";
	if (trim($codead)<>"")
	{
		if (($level==3) OR ($level==4))
			$rexist.= " AND `int_codeage` = '".$codead."'";
		if (($level==6) OR ($level==7))
			$rexist.= " AND `int_codedo` = '".$codead."'";
	}
	$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 client
function lit_enr_cli($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_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'un client dans un contrat client signé
function exist_cli_in_cocl_signe($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codecli` = '$code' AND `cocl_signe` IS NOT NULL";
	$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'un client dans une fiche intervention
function exist_cli_in_int($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codecli` = '$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'un client dans un relevé de parc
function exist_cli_in_rel($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBREL. "` WHERE `rel_codecli` = '$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;
}

/** Contacts Clients **/

// Vérification du nombre de contacts dans une fiche client
function nb_ccli_in_cli($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$code'";
	$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 contact client
function lit_enr_ccli_id_in_cli($id,$code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCCLI. "` WHERE `ccli_id` = '$id' AND `ccli_codecli` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $row2;
}

// Suppression des contacts dans une fiche client
function del_ccli_in_cli($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "DELETE FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	mysql_close($conn2);
}

/** Sites Clients **/

// Lecture d'un site client
function lit_enr_scli($idscli,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '$idscli'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $row2;
}

// Donne le nom complet d'un site client
function nom_scli($idscli,$sdl)
{
	$nom_site="";
	switch ($idscli)
	{
		case -1 :
			$nom_site="<strong>Tous les sites</strong>";
			break;
		case 0 :
			$nom_site="<strong>Site principal</strong>";
			break;
		default :
			$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech2 = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '$idscli'";
			$rs2 = mysql_query($rech2)or die(mysql_error());
			if ($row2 = mysql_fetch_array($rs2))
			{
				extract($row2);
				$nom_site.= "<strong>".$scli_ste."</strong>";
				if ($sdl==TRUE)
					$nom_site.="<br>";
				else
					$nom_site.=", ";
				$nom_site.= "<span class=\"annot_gras_it\">";
				$nom_site.= $scli_rcivil." ".$scli_rprenom." ".$scli_rnom;
				if ($sdl==TRUE)
					$nom_site.="<br>";
				else
					$nom_site.=", ";
				$nom_site.= "</span>";
				$nom_site.= $scli_adr1;
				if ($sdl==TRUE)
					$nom_site.="<br>";
				else
					$nom_site.=", ";
				if ($scli_adr2<>"")
				{
					$nom_site.= $scli_adr2;
					if ($sdl==TRUE)
						$nom_site.="<br>";
					else
						$nom_site.=", ";
				}
				$nom_site.= Retournecp_openId($scli_ville,FALSE)." ".Retourneville_openId($scli_ville,FALSE);
				if ($sdl==TRUE)
					$nom_site.="<br>";
				else
					$nom_site.=", ";
				$nom_site.= "Tel : ".$scli_telf;
				if ($sdl==TRUE)
					$nom_site.="<br>";
			}
			mysql_free_result($rs2);
	}
	
	return $nom_site;
}

// Nombre de sites pour un client donné
function nb_scli($codecli)
{
	$nb=0;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM " .TBSCLI. " WHERE scli_codecli = '".$codecli."'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nb=mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nb;
}

// Nombre de sites actifs pour un client donné
function nb_scli_actifs($codecli)
{
	$nb=0;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM " .TBSCLI. " WHERE scli_codecli = '".$codecli."' AND scli_actif=1";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nb=mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nb;
}

// Suppression des sites dans une fiche client
function del_scli_in_cli($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "DELETE FROM `" .TBSCLI. "` WHERE `scli_codecli` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	mysql_close($conn2);
}

// Vérification de la présence d'un site client dans un contrat
// (Recherche sur le code du client)
function exist_scli_in_cocl_cc($codecli)
{
	$exist=FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist2 = "SELECT * FROM " .TBSCLI. " WHERE scli_codecli = '".$codecli."'";
	$rs2 = mysql_query($rexist2) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		while (($row2 = mysql_fetch_assoc($rs2)) AND ($exist==FALSE))
		{         
			extract($row2);
			$rexist3 = "SELECT * FROM " .TBSCLIC. " WHERE sclic_idscli = '".$scli_id."'";
			$rs3 = mysql_query($rexist3) or die(mysql_error());
			if (mysql_num_rows($rs3) <> 0)
				$exist = TRUE;
		}
		mysql_free_result($rs3);
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérification de la présence d'un site client dans un contrat client signé
function exist_scli_in_cocl_signe($idscli)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM " .TBSCLIC. ", ".TBCOCL." WHERE ".TBSCLIC.".sclic_idscli = '".$idscli."'";
	$rexist.= " AND ".TBCOCL.".cocl_signe IS NOT NULL AND ".TBSCLIC.".sclic_codecocl=".TBCOCL.".cocl_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'un site client dans une fiche intervention
// (Recherche sur l'id du site)
function exist_scli_in_int($idscli)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM " .TBSCLII. " WHERE sclii_idscli = '".$idscli."'";
	$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'un site client dans une fiche intervention
// (Recherche sur le code du client)
function exist_scli_in_int_cc($codecli)
{
	$exist=FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist2 = "SELECT * FROM " .TBSCLI. " WHERE scli_codecli = '".$codecli."'";
	$rs2 = mysql_query($rexist2) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		while (($row2 = mysql_fetch_assoc($rs2)) AND ($exist==FALSE))
		{         
			extract($row2);
			$rexist3 = "SELECT * FROM " .TBSCLII. " WHERE sclii_idscli = '".$scli_id."'";
			$rs3 = mysql_query($rexist3) or die(mysql_error());
			if (mysql_num_rows($rs3) <> 0)
				$exist = TRUE;
		}
		mysql_free_result($rs3);
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérification de la présence d'un site client dans une fiche intervention non close
function exist_scli_in_int_ouv($idscli)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM " .TBSCLII. ", ".TBINT." WHERE ".TBSCLII.".sclii_idscli = '".$idscli."'";
	$rexist.= " AND ".TBINT.".int_datefin='0000-00-00' AND ".TBSCLII.".sclii_codeint=".TBINT.".int_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érifie s'il existe un relevé pour un code client
function exist_rel_cli($codecli)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBREL. "` WHERE `rel_codecli` = '$codecli'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist = TRUE;
	else
		$exist = FALSE;
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $exist;
}


?>