<?PHP
/*************************************************************/
/* Fonctions pour les accès aux différents fichiers dans SMI */
/*  UNIQUEMENT pour la génération des PDFs                   */
/*************************************************************/

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

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

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

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

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


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


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

// Lecture d'un site client

function lit_enr_scli($idscli)
{
	$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);
	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="Tous les sites";
			break;
		case 0 :
			$nom_site="Site principal";
			break;
		default :
			$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.= $scli_ste;
				if ($sdl==TRUE)
					$nom_site.="<br>";
				else
					$nom_site.=", ";
				$nom_site.= $scli_rcivil." ".$scli_rprenom." ".$scli_rnom;
				if ($sdl==TRUE)
					$nom_site.="<br>";
				else
					$nom_site.=", ";
				$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.=", ";
				}

				$villes_cp = "";
				
				$rcp = "SELECT villes_insee,villes_cp FROM " .TBVILLES. " WHERE villes_insee='".$scli_ville."'";
				$rscp = mysql_query($rcp) or die(mysql_error());
				if (mysql_num_rows($rscp) == 1) // Uniquement UN SEUL enregistrement !
				{
					if ($rowcp = mysql_fetch_array($rscp)) 
					{
						extract ($rowcp);
					}
				}
			
				$nom_site.=$villes_cp;
				mysql_free_result($rscp);
		
				$ville_nom = "";
				
				$rnom = "SELECT villes_insee,villes_nom FROM " .TBVILLES. " WHERE villes_insee='".$scli_ville."'";;
				$rsnom = mysql_query($rnom) or die(mysql_error());
				if (mysql_num_rows($rsnom) == 1) // Uniquement UN SEUL enregistrement !
				{
					if ($rowville = mysql_fetch_array($rsnom)) 
					{
						extract ($rowville);
					}
				}
			
				$ville = str_majuscules($villes_nom);
				
				// Les villes de LYON, MARSEILLE, PARIS doivent être traitées à part (arrondissements)
				
				if (stristr($villes_nom,"LYON"))
					$ville="LYON";
				if (stristr($villes_nom,"MARSEILLE"))
					$ville="MARSEILLE";
				if (stristr($villes_nom,"PARIS"))
					$ville="PARIS";

				$nom_site.= " ".$ville;
				mysql_free_result($rsnom);

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

?>