<?PHP
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.6
Copyright (C) 2006  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
****************************************************************************************/


/*****************************************************************/
/* Fonctions de MAJ des textes types pour les générations de PDF */
/*****************************************************************/

// Mise ŕ jour d'un texte en fonction de codes de champs

function parse_txt($type,$code,$texte)
{
	$txt = $texte;

 	// Balayage de 1er niveau
 	switch ($type)
	{
		// Agence
		case "age" :
					$row = lit_enr_age($code);

					$txt = ereg_replace("%cp_age%",Retournecp($row["age_ville"]),$txt);
					$txt = ereg_replace("%ville_age%",Retourneville($row["age_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$code."'";
					break;
					
		// Centre de gestion
		case "cdg" :
					$row = lit_enr_cdg();

					$txt = ereg_replace("%cp_cge%",Retournecp($row["cge_ville"]),$txt);
					$txt = ereg_replace("%ville_cge%",Retourneville($row["cge_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBCG;
					break;
					
		// Client
		case "cli" :
					$row = lit_enr_cli($code);

					$txt = ereg_replace("%cp_cli%",Retournecp($row["cli_ville"]),$txt);
					$txt = ereg_replace("%ville_cli%",Retourneville($row["cli_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$code."'";
					break;
					
		// Contrat
		case "con" :
					$row = lit_enr_cocl($code);
					extract($row);
					$rech2 = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_code` = '".$code."'";
					break;
					
		// Intervention
		case "int" :
					$row = lit_enr_int($code);
					$rech2 = "SELECT * FROM `" .TBINT. "` WHERE `int_code` = '".$code."'";
					break;
					
		// Opérateur
		case "ope" :
					$row = lit_enr_ope($code);

					$txt = ereg_replace("%cp_ope%",Retournecp($row["ope_ville"]),$txt);
					$txt = ereg_replace("%ville_ope%",Retourneville($row["ope_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBOPE. "` WHERE `ope_code` = '".$code."'";
					break;
					
		// Technicien
		case "tec" :
					$row = lit_enr_tec($code);

					$txt = ereg_replace("%cp_tec%",Retournecp($row["tec_ville"]),$txt);
					$txt = ereg_replace("%ville_tec%",Retourneville($row["tec_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBTEC. "` WHERE `tec_code` = '".$code."'";
					break;
					
		default : break;
	}

	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$num = mysql_num_fields($rs2);
	$i = 0;
	for ($i = 0; $i < $num; $i++) 
	{
		$nom = mysql_field_name($rs2, $i);
		$nom_rech = "%".$nom."%";
		if (mysql_field_type($rs2, $i) <> "date")
			$txt = eregi_replace($nom_rech,formatetxtbr($row[$i]),$txt);
		else
			$txt = eregi_replace($nom_rech,formatedate($row[$i]),$txt);
		
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	// Balayage pour les champs complémentaires
	switch ($type)
	{
		// Contrat
		case "con" :
					// Balayage des champs client
					$row = lit_enr_cli($cocl_codecli);
					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$cocl_codecli."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs agence
					$row = lit_enr_age($cocl_codeage);

					$txt = ereg_replace("%cp_age%",Retournecp($row["age_ville"]),$txt);
					$txt = ereg_replace("%ville_age%",Retourneville($row["age_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$cocl_codeage."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs centre de gestion
					$row = lit_enr_cdg();
					$rech2 = "SELECT * FROM `" .TBCG;
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs périodicités
					$row = lit_enr_per($cocl_codecope);

					$rech2 = "SELECT * FROM `" .TBPER. "` WHERE `cope_code` = '".$cocl_codecope."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs quotas d'heures

					$txt = ereg_replace("%heures_cocl%",sec_heure($cocl_nbs),$txt);

					// Balayage des champs sites
					$txt_sites="";
					$j=1;
					if ($cocl_sitep==1)
					{
						$row = lit_enr_cli($cocl_codecli);
						if (is_array($row))
						{
							extract($row);
							$txt_sites.="--- SITE N°".sprintf("%'010s",$j)." ---<BR>";
							$txt_sites.="<B>".$cli_ste."</B><BR>";
							$txt_sites.=$cli_adr1."<BR>";
							if ($cli_adr2<>"")
								$txt_sites.=$cli_adr2."<BR>";
							$txt_sites.=Retournecp($cli_ville)." ".Retourneville($cli_ville)."<BR>";
							$txt_sites.="-----------------------------------<BR>";
							$j++;
						}
					}

					$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rech = "SELECT * FROM ".TBSCLI.",".TBSCLIC." WHERE ".TBSCLIC.".sclic_codecocl = '".$code."' AND ".TBSCLI.".scli_id = ".TBSCLIC.".sclic_idscli";
					$rs = mysql_query($rech);
					while ($row = mysql_fetch_assoc($rs)) 
					{
						extract($row);
						if ($j>1)
							$txt_sites.="<BR>";
						$txt_sites.="--- SITE N°".sprintf("%'010s",$j)." ---<BR>";
						$txt_sites.="<B>".$scli_ste."</B><BR>";
						$txt_sites.=$scli_adr1."<BR>";
						if ($scli_adr2<>"")
							$txt_sites.=$scli_adr2."<BR>";
						$txt_sites.=Retournecp($scli_ville)." ".Retourneville($scli_ville)."<BR>";
						$txt_sites.="-----------------------------------<BR>";
						$j++;
					}
					mysql_free_result($rs);

					$txt = ereg_replace("%sites_cocl%",$txt_sites,$txt);

					break;
					
		// Intervention
		case "int" :
					// Balayage des champs client
					$row = lit_enr_cli($int_codecli);
					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$int_codecli."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs agence
					$row = lit_enr_age($int_codeage);
					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$int_codeage."'";
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs centre de gestion
					$row = lit_enr_cdg();
					$rech2 = "SELECT * FROM `" .TBCG;
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					$rs2 = mysql_query($rech2)or die(mysql_error());
					$num = mysql_num_fields($rs2);
					$i = 0;
					for ($i = 0; $i < $num; $i++) 
					{
						$nom = mysql_field_name($rs2, $i);
						$nom_rech = "%".$nom."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					break;
					
		default : break;
	}

	return $txt;
}