<?PHP
/*************************************************************/
/* Fonctions pour les accès aux différents fichiers dans SMI */
/*************************************************************/

// 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;
}

// Vérification du type de facturation sur une agence (par le CDG ou par l'agence)

function mod_fact_age($code)
{
	$fact_age = "";
	$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;
}

// Retourne une chaine avec toutes les zones de chalandises sur les agences actives

function zc_age_actives()
{
	$zc_totale = "";
	
	$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());
	if (mysql_num_rows($rs2) > 0)
	{
		while ($res2 = mysql_fetch_array($rs2)) 
		{
			$zc_totale.= "-".$res2["age_zone"];
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $zc_totale;
}

// Vérification du 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 liste d'agences ET d'un code client particulier dans une quelconque fiche intervention non close

function exist_liste_age_cli_in_int_ouv($liste_age,$client)
{
	$age_tab = $liste_age;
	$exist = FALSE;

	if (is_array($age_tab) == TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		foreach ($age_tab as $age_found)
		{
			$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codeage` = '$age_found' AND `int_codecli` = '$client' AND `int_datefin` = '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é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;
}

// Vérification de la présence d'une agence/opérateur/technicien en tant 
// que code de création d'une fiche intervention

function exist_in_int_codecrea($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codecrea` = '$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;
}

// Donne une liste des agences actives couvrant un département

function liste_age_actives_dep($dep)
{
	$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());
	$i = 0;
	if ($row2 = mysql_fetch_array($rs2))
	{
		extract($row2);
		if (dep_in_zc($dep,$age_zone) == TRUE)
		{
			$liste_age[$i]=$age_code;
			$i++;
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $liste_age;
}

// Vérification du nombre d'opérateurs

function nb_ope()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBOPE. "`";
	$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 opérateur

function lit_enr_ope($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBOPE. "` WHERE `ope_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 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;
}


// Vérification du nombre de civilités

function nb_civ()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCIV;
	$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 civilité

function lit_enr_civ($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBCIV. " WHERE civ_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 contrats dans une fiche client

function nb_cocl($agence)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if ($agence <> "")
		$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codeage` = '".$agence."'";
	else
		$rexist = "SELECT * FROM `" .TBCOCL. "`";
	$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 contrats dans une fiche client

function nb_cocl_in_cli($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codecli` = '$code'";
	$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 contrats clients dans l'historique contrats

function nb_cocl_in_coclh($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCLH. "` WHERE `coclh_codecocl` = '$code'";
	$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 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

function nb_cli($agence)
{
	$nbexist = 0;

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

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

	$rexist = "SELECT * FROM `" .TBCLI. "`";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if ($agence <> "")
	{
		if (mysql_num_rows($rs2) <> 0)
		{
			while ($resultat = mysql_fetch_array($rs2)) 
			{
				extract($resultat);
				
				if (dep_in_zc($cli_dep,$age_zone) == TRUE)
					$nbexist++;
			}
		}
	}
	else
	{
		$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;
}

// 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);
}

// 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 une fiche intervention non close (pour une agence particulière)

function exist_cli_in_int_ouv($code,$agence)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codecli` = '$code' AND `int_codeage` = '$agence' 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;
}

// 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;
}

// 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;
}


// 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é licence

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 rattachées à intervention en cours

function exist_rpbm_attache($codeint)
{
	$exist = FALSE;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_codeint` = '".$codeint."'";
	$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 une agence ouverte dans un département

function exist_age_in_dep($dep)
{
	$exist = FALSE; 
	
	$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());
	if (mysql_num_rows($rs2) <> 0)
	{
		while ($resultat = mysql_fetch_array($rs2)) 
		{
			extract($resultat);

			if (exist_chaine_in_seq($dep,$age_zone) == TRUE)
				$exist = TRUE;
		}
	}

	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

// Vérification du nombre de familles produits

function nb_fam()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBFAM;
	$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 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 nombre d'interventions sur un client

function nb_int_cli($client)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_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érification du nombre d'articles dans une fiche intervention

function nb_art_in_int($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBPDTI. "` WHERE `pdti_codeint` = '$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 enregistrement intervention

function lit_enr_int($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBINT. "` WHERE `int_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;
}


// Lecture d'un enregistrement détail horaire intervention

function lit_enr_inth($id)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBINTH. "` WHERE `inth_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érification de la présence d'une intervention dans un historique contrat client

function exist_int_in_coclh($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCLH. "` WHERE `coclh_codeint` = '$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 du nombre de techniciens

function nb_tec($agence)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if ($agence <> "")
		$rexist = "SELECT * FROM `" .TBTEC. "` WHERE `tec_codeage` = '".$agence."'";
	else
		$rexist = "SELECT * FROM `" .TBTEC. "`";
	$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 techniciens actifs

function nb_tec_actifs()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBTEC. "`  WHERE `tec_out` = '0'";
	$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 techniciens dans une agence

function nb_tec_in_age($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());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Lecture d'un enregistrement technicien

function lit_enr_tec($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBTEC. "` WHERE `tec_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 technicien dans une fiche intervention non close

function exist_tec_in_int_ouv($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codetec` = '$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'un technicien dans une fiche intervention

function exist_tec_in_int($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codetec` = '$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);
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codetec` = '$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;
}

// Lecture d'un enregistrement produit

function lit_enr_pdt($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBPDT. "` WHERE `pdt_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 article dans une fiche intervention

function exist_pdt_in_int($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBPDTI. "` WHERE `pdti_codepdt` = '$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;
}

//  Lit un article dans une fiche intervention

function lit_enr_pdt_in_int($codep,$codei)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBPDTI. "` WHERE `pdti_codepdt` = '$codep' AND `pdti_codeint` = '$codei'";
	$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 produit dans une fiche intervention non close

function exist_pdt_in_int_ouv($code)
{

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

	$rech2 = "SELECT ".TBPDTI.".pdti_codepdt, ".TBPDTI.".pdti_codeint, ".TBINT.".int_code, ";
	$rech2.= TBINT.".int_datefin FROM ".TBPDTI.", ".TBINT." WHERE (".TBPDTI.".pdti_codepdt = '$code' AND ".TBPDTI.".pdti_codeint = ".TBINT.".int_code AND int_datefin <> '0000-00-00')";
	$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;
}

// Lecture d'un enregistrement origine

function lit_enr_ori($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBORI. " WHERE orig_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;
}

// Lecture d'un enregistrement périodicité

function lit_enr_per($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBPER. "` WHERE `cope_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 périodicité dans un contrat-client

function exist_per_in_cocl($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codecope` = '$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;
}

// Lecture d'un enregistrement contrat type

function lit_enr_co($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBCO. "` WHERE `co_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 contrat type dans un contrat-client

function exist_co_in_cocl($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codeco` = '$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 qu'un contrat contrat-client est clôt

function cocl_clot($code)
{
	$fini = FALSE;

	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_code` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
		extract ($row2);
		
		if (formatedate($cocl_datefin) <> "")
			$fini = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $fini;
}


// Vérification qu'un contrat contrat-client est signé

function cocl_signed($code)
{
	$signe = FALSE;

	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_code` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
		extract ($row2);
		
		if ($cocl_signe <> "")
			$signe = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $signe;
}


// Vérification qu'un contrat est de type "quota d'heure"

function cocl_quota($code)
{
	$quota = FALSE;

	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_code` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
		extract ($row2);
		
		if ($cocl_typh == 1)
			$quota = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $quota;
}

// Lecture d'un enregistrement demande

function lit_enr_dde($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBDDE. "` WHERE `dde_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 demandes

function nb_dde()
{
	$nbexist = 0;

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

	$rexist = "SELECT * FROM `" .TBDDE. "`";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérification de la présence d'une catégorie de demandeur dans une demande

function exist_ddec_in_dde($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBDDE. "` WHERE `dde_codeddec` = '$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;
}

// Lecture d'un enregistrement catégorie de demandeur

function lit_enr_ddec($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBDDEC. "` WHERE `ddec_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 destinataire de demande dans une demande

function exist_dded_in_dde($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBDDE. "` WHERE `dde_codedded` = '$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;
}

// Lecture d'un enregistrement destinataire de demande

function lit_enr_dded($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBDDED. "` WHERE `dded_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 type de demande dans une demande

function exist_ddet_in_dde($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBDDE. "` WHERE `dde_codeddet` = '$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;
}

// Lecture d'un enregistrement type de demande

function lit_enr_ddet($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBDDET. "` WHERE `ddet_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;
}

// Lecture d'un enregistrement contrat client

function lit_enr_cocl($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_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;
}

// Lecture d'un enregistrement texte type

function lit_enr_texte($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBTXT. "` WHERE `txt_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;
}

// Lecture d'un enregistrement centre de gestion

function lit_enr_cdg()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBCG;
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) == 1) // Uniquement UN SEUL enregistrement !
	{
		if ($row2 = mysql_fetch_array($rs2))
		{
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Vérification du nombre d'origines

function nb_orig()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBORI;
	$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 règlements

function nb_reg()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBREG;
	$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 statuts

function nb_statuts()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBSTA;
	$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 lieux

function nb_lieux()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBLIEU;
	$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 marques

function nb_marques()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBRLPMA;
	$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 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;
}

// 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;
}

// 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;
}

// Lecture d'un enregistrement texte type

function lit_enr_txt($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBTXT. "` WHERE `txt_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;
}

// Minimum (jours) pour le préavis

function preavis_min()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM ".TBCONS;
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) == 1) // Uniquement UN SEUL enregistrement !
	{
		if ($row2 = mysql_fetch_array($rs2))
		{
			extract($row2);
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $cons_relmin;
}

// Maximum (jours) pour le préavis

function preavis_max()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM ".TBCONS;
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) == 1) // Uniquement UN SEUL enregistrement !
	{
		if ($row2 = mysql_fetch_array($rs2))
		{
			extract($row2);
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $cons_relmax;
}


// Lecture d'un enregistrement fichiers internes

function lit_enr_fic($nom)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFIC. " WHERE fic_nom = '$nom'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// MAJ Nb Téléchargements d'un fichier interne

function maj_nbtel_enr_fic($nom)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFIC. " WHERE fic_nom = '$nom'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
		$nbtel = $row2["fic_nbtel"];
		$nbtel++;
		
		$rech3 = "UPDATE `" .TBFIC. "` SET `fic_nbtel`='".$nbtel."'  WHERE `fic_nom` = '".$nom."'";
		$rs3 = mysql_query($rech3) or die(mysql_error());
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}


// Donne le créateur du fichier/répertoire

function donne_createur_ficdir($nom)
{
	$createur = "inconnu";
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFIC. " WHERE fic_nom = '".$nom."'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) == 1) // On a trouvé un enregistrement
	{
		if ($row2 = mysql_fetch_array($rs2))
		{
			$createur = $row2["fic_codecrea"];
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $createur;
}

// Donne le nombre de téléchargements du fichier/répertoire

function donne_nbtel_ficdir($nom)
{
	$nbtel = 0;
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFIC. " WHERE fic_nom = '".$nom."'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) == 1) // On a trouvé un enregistrement
	{
		if ($row2 = mysql_fetch_array($rs2))
		{
			$nbtel = $row2["fic_nbtel"];
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $nbtel;
}


// Vérifie si ($user) est le créateur du fichier/répertoire (ou si c'est un administrateur)

function est_createur_ficdir($nom,$user)
{
	global
		$admin_code;

	if ($user == $admin_code) // L'admin peut tout faire
	{
		return true;
		break;
	}
				
	$est_createur = FALSE;
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBFIC. " WHERE fic_nom = '".$nom."' AND fic_codecrea = '".$user."'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) > 0) // On a trouvé un enregistrement
	{
		$est_createur = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $est_createur;
}

?>