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

// Lecture d'un enregistrement relevé
function lit_enr_rel($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBREL. "` WHERE `rel_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 dossiers de relevés sur une agence
function nb_rela($agence)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if ($agence <> "")
		$rexist = "SELECT * FROM `" .TBREL. "` WHERE `rel_codeage` = '".$agence."'";
	else
		$rexist = "SELECT * FROM `" .TBREL. "`";
	$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 relevé à partir du code client
function lit_enr_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 ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Donne le numéro de relevé à partir du code client
function num_rel_cli($codecli)
{
	$num_rel ="";
	$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 ($row2 = mysql_fetch_array($rs2))
	{
		$num_rel = $row2["rel_code"];
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $num_rel;
}

// Vérifie si un dossier relevé est utilisé dans un relevé licence
function exist_rel_in_rlic($coderel)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRLIC. "` WHERE `rlic_coderel` = '".$coderel."'";
	$rs2 = mysql_query($rexist) or die(mysql_error());

	if (mysql_num_rows($rs2) <> 0)
	{
		$exist = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérifie si un dossier relevé est utilisé dans un relevé produit
function exist_rel_in_rpdt($coderel)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_coderel` = '".$coderel."'";
	$rs2 = mysql_query($rexist) or die(mysql_error());

	if (mysql_num_rows($rs2) <> 0)
	{
		$exist = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Lecture d'un enregistrement relevé licence
function lit_enr_rlic($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBRLIC. "` WHERE `rlic_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 relevés licences sur une agence
function nb_rlica($agence)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if ($agence <> "")
		$rexist = "SELECT * FROM `" .TBRLIC. "` WHERE `rlic_codeage` = '".$agence."'";
	else
		$rexist = "SELECT * FROM `" .TBRLIC. "`";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérification le nombre de fois où est utilisé un relevé licence dans des fiches relevés matériels d'un client
function nb_rlic($client,$coderlic)
{
	$nbexist = 0;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_codecli` = '".$client."'";
	$rs2 = mysql_query($rexist) or die(mysql_error());

	if (mysql_num_rows($rs2) <> 0)
	{
		while ($resultat = mysql_fetch_array($rs2)) 
		{
			if (ereg($coderlic,$resultat["rpdt_coderlic"]))
				$nbexist++;
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérifie s'il existe des relevés de licences actifs pour un client particulier
function exist_rlic_actif($codecli)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM ".TBRLIC." WHERE `rlic_codecli` = '".$codecli."' AND `rlic_dateout` = '0000-00-00'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist = TRUE;
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérifie si un relevé licence est présent dans un relevé de problème
function exist_rlic_in_rpbm($coderlic)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_coderlp` = '".$coderlic."'";
	$rs2 = mysql_query($rexist) or die(mysql_error());

	if (mysql_num_rows($rs2) <> 0)
	{
		$exist = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérifie si un relevé licence est présent dans un relevé de problème non clôt
function exist_rlic_in_rpbm_actif($coderlic)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_coderlp` = '".$coderlic."' AND `rpbm_datett` = '0000-00-00'";
	$rs2 = mysql_query($rexist) or die(mysql_error());

	if (mysql_num_rows($rs2) <> 0)
	{
		$exist = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérifie si un relevé licence est présent dans une fiche relevé de matériel d'un client
function exist_rlic_in_rpdt($client,$coderlic)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_codecli` = '".$client."'";
	$rs2 = mysql_query($rexist) or die(mysql_error());

	if (mysql_num_rows($rs2) <> 0)
	{
		while ($resultat = mysql_fetch_array($rs2)) 
		{
			if (ereg($coderlic,$resultat["rpdt_coderlic"]))
				$exist = TRUE;
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérifie si un relevé licence est présent dans une fiche relevé de matériel ACTIVE d'un client 
function exist_rlic_in_rpdt_actif($client,$coderlic)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_codecli` = '".$client."' AND `rpdt_dateout` ='0000-00-00'";
	$rs2 = mysql_query($rexist) or die(mysql_error());

	if (mysql_num_rows($rs2) <> 0)
	{
		while ($resultat = mysql_fetch_array($rs2)) 
		{
			if (ereg($coderlic,$resultat["rpdt_coderlic"]))
				$exist = TRUE;
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Lecture d'un enregistrement relevé produit
function lit_enr_rpdt($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_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 relevés matériel sur une agence
function nb_rpdta($agence)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if ($agence <> "")
		$rexist = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_codeage` = '".$agence."'";
	else
		$rexist = "SELECT * FROM `" .TBRPDT. "`";
	$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 relevés matériel sur un client
function nb_rpdtc($client)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if ($client <> "")
		$rexist = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_codecli` = '".$client."'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérifie s'il existe des relevés de produits actifs pour un client particulier
function exist_rpdt_actif($codecli)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM ".TBRPDT." WHERE `rpdt_codecli` = '".$codecli."' AND `rpdt_dateout` = '0000-00-00'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist = TRUE;
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérifie si un relevé matériel est présent dans un relevé de problème
function exist_rpdt_in_rpbm($coderpdt)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_coderlp` = '".$coderpdt."'";
	$rs2 = mysql_query($rexist) or die(mysql_error());

	if (mysql_num_rows($rs2) <> 0)
	{
		$exist = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérifie si un relevé matériel est présent dans un relevé de problème non clôt
function exist_rpdt_in_rpbm_actif($coderpdt)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_coderlp` = '".$coderpdt."' AND `rpbm_datett` = '0000-00-00'";
	$rs2 = mysql_query($rexist) or die(mysql_error());

	if (mysql_num_rows($rs2) <> 0)
	{
		$exist = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Lecture d'un enregistrement déclaration problème
function lit_enr_rpbm($id)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_id` = '$id'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Vérifie s'il existe des déclarations de problèmes pour une fiche relevé licence ou matériel
function exist_rpbm($code)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_coderlp` = '".$code."'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist = TRUE;
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérifie s'il existe des déclarations de problèmes non affectées (sauf code en cours)
function exist_rpbm_actif($mode,$codecli,$codeint)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if ($mode == "edit") // On est édition d'intervention
		$rexist = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_codeint` = '".$codeint."' OR (`rpbm_codecli`= '".$codecli."' AND `rpbm_datett`= '0000-00-00' AND `rpbm_codeint` IS NULL)";
	if ($mode == "add") // On est en création d'intervention
		$rexist = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_codecli`= '".$codecli."' AND `rpbm_codeint` IS NULL";			
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist = TRUE;
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérifie s'il existe des déclarations de problèmes non affectées (sauf code en cours) sur un site donné
function exist_rpbm_actif_site($mode,$codecli,$codeint,$site)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if ($mode == "edit") // On est édition d'intervention
		$rexist = "SELECT * FROM `" .TBRPBM. "` WHERE (`rpbm_codeint` = '".$codeint."' AND `rpbm_idsite`= '".$site."') OR (`rpbm_codecli`= '".$codecli."' AND `rpbm_idsite`= '".$site."' AND `rpbm_codeint` IS NULL)";
	if ($mode == "add") // On est en création d'intervention
		$rexist = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_codecli`= '".$codecli."' AND `rpbm_idsite`= '".$site."' AND `rpbm_codeint` IS NULL";	
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist = TRUE;
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

/** Types licences **/

// Vérification du nombre de types de licences
function nb_typlic()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRLTL;
	$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 type licence
function lit_enr_rltl($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRLTL. " WHERE rltl_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;
}


/** Etats Produits **/

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

	return $nbexist;
}

/** Catégories Produits **/

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

	return $nbexist;
}

/** Type accès Télémaintenance **/

// Lecture d'un enregistrement type accès télémaintenance
function lit_enr_tmta($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRLPTMTA. " WHERE rlptmta_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;
}


/** Type licence **/

// Lecture d'un enregistrement type accès télémaintenance
function lit_enr_tl($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRLTL. " WHERE rltl_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;
}


/** Media **/

// Vérification du nombre de types de médias
function nb_media()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRLMED;
	$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 média
function lit_enr_med($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRLMED. " WHERE rlmed_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;
}

/** Catégorie Produit **/

// Lecture d'un enregistrement catégorie produit
function lit_enr_rpcat($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRPCAT. " WHERE rpcat_code = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

/** Logiciel télémaintenance **/

// Lecture d'un enregistrement logiciel de télémaintenance
function lit_enr_rlptml($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRLPTML. " WHERE rlptml_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;
}


/** Mode Mise à Jour **/

// Lecture d'un enregistrement mode mise à jour
function lit_enr_rlpmm($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRLPMM. " WHERE rlpmm_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;
}

// Nombre d'enregistrements mode mise à jour
function nb_rlpmm()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBRLPMM;
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

?>