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


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

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

// Crée une chaîne qui contient la liste des problèmes rattachés à une intervention
function cree_str_lstpbm_int($ci)
{
	$lstpbm = "";
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT rpbm_id FROM `" .TBRPBM. "` WHERE `rpbm_codeint`= '".$ci."' ORDER BY `rpbm_datecrea`";
	$rs2 = mysql_query($rech2);
	while ($row2 = mysql_fetch_assoc($rs2)) 
	{
		extract($row2);
		$lstpbm .= $rpbm_id."-";
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	if ($lstpbm!="")
		$lstpbm = substr($lstpbm,0,strlen($lstpbm)-1);
	
	return $lstpbm;
}

// Crée le texte des matériels (intervention) sur la base de la string des problèmes choisis
function cree_txt_matos_int($lstpbm)
{
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$txtmatos="";
	$tab_lstpbm=explode("-",$lstpbm);
	
	if (count($tab_lstpbm>0))
	{
		for($i=0; $i<count($tab_lstpbm); $i++)
		{
			$rech = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_id`= '".$tab_lstpbm[$i]."'";
			$rs = mysql_query($rech);
			if (mysql_num_rows($rs) == 1) // Il ne peut y avoir qu'un seul résultat
			{
				if ($row = mysql_fetch_array($rs))
				{
					extract ($row);
					$rech2="";
					switch ($rpbm_typrel)
					{
						// C'est une licence
						case 1 :
							$rech2 = "SELECT * FROM `" .TBRLIC. "` WHERE `rlic_code`= '".$rpbm_coderlp."'";
							break;
							
						// C'est du matériel
						case 2 :
							$rech2 = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_code`= '".$rpbm_coderlp."'";
							break;
							
						default :
							break;
					}
					if ($rech2<>"")
					{
						$txtmatos_titre="";
						$txtmatos_corps1="";
						$txtmatos_pied="";
						$rs2 = mysql_query($rech2);
						if ($row2 = mysql_fetch_array($rs2))
						{
							extract ($row2);
							
							switch ($rpbm_typrel)
							{
								// C'est une licence
								case 1 :
									$txtmatos_titre ="---- DETAIL LICENCE CONCERNEE PAR LE PROBLEME N°".sprintf("%'010s",$rpbm_id)." ----";
									$rech3 = "SELECT * FROM ".TBMQUE." WHERE `mque_code` = '".$rlic_codema."'";
									$rs3 = mysql_query($rech3);
									if ($row3 = mysql_fetch_array($rs3))
									{
										extract ($row3);
									}
									$txtmatos_corps1 .= "Relevé ".$rlic_code." : ".$mque_ste." ".$rlic_modele." ".$rlic_version;
									mysql_free_result($rs3);
									break;

								// C'est du matériel
								case 2 :
									$txtmatos_titre ="---- DETAIL MATERIEL CONCERNE PAR LE PROBLEME N°".sprintf("%'010s",$rpbm_id)." ----";
									$rech3 = "SELECT * FROM ".TBRPCAT." WHERE `rpcat_code` = '".$rpdt_codecat."'";
									$rs3 = mysql_query($rech3);
									if ($row3 = mysql_fetch_array($rs3))
									{
										extract ($row3);
									}
									$rech3 = "SELECT * FROM ".TBMQUE." WHERE `mque_code` = '".$rpdt_codema."'";
									$rs3 = mysql_query($rech3);
									if ($row3 = mysql_fetch_array($rs3))
									{
										extract ($row3);
									}
									$txtmatos_corps1 = "Relevé ".$rpdt_code." : ".$rpcat_desc." / ".$mque_ste." ".$rpdt_modele." (".$rpdt_codep.")";
									mysql_free_result($rs3);
									break;
									
								default :
									break;
							}
							
							switch ($rpbm_typrel)
							{
								// C'est une licence
								case 1 :
									$txtmatos_pied = "---- FIN LICENCE CONCERNEE PAR LE PROBLEME N°".sprintf("%'010s",$rpbm_id)." ----";
									break;
									
								// C'est du matériel
								case 2 :
									$txtmatos_pied = "---- FIN MATERIEL CONCERNEE PAR LE PROBLEME N°".sprintf("%'010s",$rpbm_id)." ----";
									break;
									
								default :
									break;
							}
							$txtmatos.=$txtmatos_titre."%%".$txtmatos_corps1."%%".$txtmatos_pied."%%%%";
						}
						mysql_free_result($rs2);
					}
				}
			}
			mysql_free_result($rs);
		}
	}
	mysql_close($conn);
	
	if ($txtmatos!="")
		$txtmatos = substr($txtmatos,0,strlen($txtmatos)-4);
	return $txtmatos;
}


// Crée le texte des problèmes (intervention) sur la base de la string des problèmes choisis
function cree_txt_pbm_int($lstpbm)
{
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$txtpbm="";
	$tab_lstpbm=explode("-",$lstpbm);
	
	if (count($tab_lstpbm>0))
	{
		for($i=0; $i<count($tab_lstpbm); $i++)
		{
			$rech = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_id`= '".$tab_lstpbm[$i]."'";
			$rs = mysql_query($rech);
			if (mysql_num_rows($rs) == 1) // Il ne peut y avoir qu'un seul résultat
			{
				if ($row = mysql_fetch_array($rs))
				{
					extract ($row);
					$rech2="";
					switch ($rpbm_typrel)
					{
						// C'est une licence
						case 1 :
							$rech2 = "SELECT * FROM `" .TBRLIC. "` WHERE `rlic_code`= '".$rpbm_coderlp."'";
							break;
							
						// C'est du matériel
						case 2 :
							$rech2 = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_code`= '".$rpbm_coderlp."'";
							break;
							
						default :
							break;
					}
					if ($rech2<>"")
					{
						$txtpbm_titre="";
						$txtpbm_corps1="";
						$txtpbm_corps1="";
						$txtpbm_pied="";
						$rs2 = mysql_query($rech2);
						if ($row2 = mysql_fetch_array($rs2))
						{
							extract ($row2);
							
							$txtpbm_titre ="---- DETAIL PROBLEME N°".sprintf("%'010s",$rpbm_id)." ----";
							$saut = chr(13).chr(10);
							$txtpbm_corps1 = ereg_replace($saut,"%%",htmlspecialchars_decode($rpbm_pbm));
							$txtpbm_corps2 = "Signalé par ".$rpbm_signale.", le ".formatedate($rpbm_datecrea);
							$txtpbm_pied = "---- FIN PROBLEME N°".sprintf("%'010s",$rpbm_id)." ----";

							$txtpbm.=$txtpbm_titre."%%".$txtpbm_corps1."%%".$txtpbm_corps2."%%".$txtpbm_pied."%%%%";
						}
						mysql_free_result($rs2);
					}
				}
			}
			mysql_free_result($rs);
		}
	}
	mysql_close($conn);
	
	if ($txtpbm!="")
		$txtpbm = substr($txtpbm,0,strlen($txtpbm)-4);
	return $txtpbm;
}

// Crée le texte des travaux effectués (intervention) sur la base de la string des problèmes choisis
function cree_txt_fait_int($lstpbm)
{
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$txtfait="";
	$tab_lstpbm=explode("-",$lstpbm);
	
	if (count($tab_lstpbm>0))
	{
		for($i=0; $i<count($tab_lstpbm); $i++)
		{
			$rech = "SELECT * FROM `" .TBRPBM. "` WHERE `rpbm_id`= '".$tab_lstpbm[$i]."'";
			$rs = mysql_query($rech);
			if (mysql_num_rows($rs) == 1) // Il ne peut y avoir qu'un seul résultat
			{
				if ($row = mysql_fetch_array($rs))
				{
					extract ($row);

					if (trim($rpbm_fait)<>"")
					{
						$txtfait_titre ="---- TRAVAIL EFFECTUE SUR PROBLEME N°".sprintf("%'010s",$rpbm_id)." ----";
						$saut = chr(13).chr(10);
						$txtfait_corps = ereg_replace($saut,"%%",htmlspecialchars_decode($rpbm_fait));
						$txtfait_pied = "---- FIN TRAVAIL EFFECTUE SUR PROBLEME N°".sprintf("%'010s",$rpbm_id)." ----";
						$txtfait.=$txtfait_titre."%%".$txtfait_corps."%%".$txtfait_pied."%%%%";
					}
				}
			}
			mysql_free_result($rs);
		}
	}
	mysql_close($conn);
	
	if ($txtfait!="")
		$txtfait = substr($txtfait,0,strlen($txtfait)-4);
	return $txtfait;
}

/** Détails Horaires **/

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

// Nombre de détail horaire pour une intervention
function nb_inth($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBINTH. "` WHERE `inth_codeint` = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $nbexist;
}

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

/** Produits dans interventions **/

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

// Création du code HTML pour affichage des articles de l'intervention
function cre_html_pdti($td,$code)
{
	$txt_html ="";
	
	if (nb_art_in_int($code)>0)
	{
		$totalint = 0;
		
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "SELECT * FROM ".TBPDTI." WHERE pdti_codeint='".$code."' ORDER BY 'pdti_id'";
		
		$rs = mysql_query($rech) or die(mysql_error());
		
		// *** Affichage de la table
		$txt_html="<table class=\"Ltable\"  border=\"0\" width=\"800\">\n";     
		
		// Création entête
		$txt_html.="<th class=\"thlst\">Code</th>\n";
		$txt_html.= "<th class=\"thlst\">Désignation + NS</th>\n";
		$txt_html.= "<th class=\"thlst\">PU TTC</th>\n";
		$txt_html.= "<th class=\"thlst\">Qté</th>\n";
		$txt_html.= "<th class=\"thlst\">Total</th>\n";
		if ($td == "edit")
			$txt_html.= "<th class=\"thlst\">Cliquer</th>\n";
		else
			$txt_html.= "<th class=\"thlst\">&nbsp;</th>\n";
			
		
		$i = 0;
		
		while ($row = mysql_fetch_assoc($rs)) 
		{         
		
			// Extraction information article
			
			$rech2 = "SELECT pdt_des FROM ".TBPDT." WHERE pdt_code='".$row["pdti_codepdt"]."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			
			if ($row2 = mysql_fetch_assoc($rs2)) 
				extract($row2);
			
			// Couleurs alternées
			if($i%2 == 0)  
				$txt_html.= "<tr class=\"TRalter1\">\n";
			else
				$txt_html.= "<tr class=\"TRalter2\">\n";
			
			$txt_html.= "<td class=\"tdlst\" valign=\"top\">".$row["pdti_codepdt"]."</td>\n";
			$txt_html.= "<td class=\"tdlst\" valign=\"top\">";
			$txt_html.= $pdt_des;
			if ($row["pdti_ns"] <> "")
			{
				$ligne_cesure = cesuretxt_tab($row["pdti_ns"],100);
				$serie = "";
				foreach ($ligne_cesure as $index_ligne=>$ligne)
					$serie .= $ligne."<br>";
				$txt_html.= "<br>";
				$txt_html.= "&raquo; ";
				$txt_html.= "<span class=\"annot_moy_it\">";
				$txt_html.= "N/S : ".$serie;
				$txt_html.= "</span>";
			}
			if ($row["pdti_texte"] <> "")
			{
				if ($row["pdti_ns"] == "")
					$txt_html.= "<br>";
				$ligne_cesure = cesuretxt_tab($row["pdti_texte"],100);
				$commentaire = "";
				foreach ($ligne_cesure as $index_ligne=>$ligne)
					$commentaire .= $ligne."<br>";
				$txt_html.= "<span class=\"annot_moy_it\">";
				$txt_html.= $commentaire;
				$txt_html.= "</span>";
			}
			$txt_html.= "</td>\n";
			$txt_html.= "<td class=\"tdlst\" valign=\"top\">";
			$txt_html.= "<div align=\"right\">";
			$txt_html.= $row["pdti_puttc"]."€";
			$txt_html.= "</div>";
			$txt_html.= "</td>\n";
			$txt_html.= "<td class=\"tdlst\" valign=\"top\">";
			$txt_html.= "<div align=\"right\">";
			$txt_html.= $row["pdti_qte"];
			$txt_html.= "</div>";
			$txt_html.= "</td>\n";
			$total = $row["pdti_puttc"]*$row["pdti_qte"];
			$txt_html.= "<td class=\"tdlst\" valign=\"top\">";
			$txt_html.= "<div align=\"right\">";
			$txt_html.= number_format($total,2,'.','')."€";
			$txt_html.= "</div>";
			$txt_html.= "</td>\n";		
			
			$totalint = $totalint+$total;
			
			if ($td == "edit")
			{
				// CREATION lien dynamique sur icône (édition, suppression, etc...)
				$txt_html.= "<td>";
				$txt_html.= "<div align=\"center\">";
				$urleditart = "pdti_maj.php?td=edit&ci=".$row["pdti_codeint"]."&ca=".$row["pdti_codepdt"];
				$txt_html.= "<a href=\"javascript:;\" onclick=\"fensaisie('".$urleditart."',700,500,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');\")><img src=\"../img/edit.png\" title=\"Modifier la ligne article\" width=\"22\" height=\"22\" border=\"0\" align=\"absmiddle\"></a>";
				$txt_html.= " ";
				$urldelart = "pdti_maj.php?td=del&ci=".$row["pdti_codeint"]."&ca=".$row["pdti_codepdt"];
				$txt_html.= "<a href=\"javascript:;\" onclick=\"fensaisie('".$urldelart."',700,500,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');\")><img src=\"../img/del.gif\" title=\"Supprimer la ligne article\" width=\"22\" height=\"22\" border=\"0\" align=\"absmiddle\"></a>";
				$txt_html.= "</div>";
				$txt_html.= "</td>\n";
			}
			else
			{
				$txt_html.= "<td >";
				$txt_html.= "&nbsp;";
				$txt_html.= "</td>\n";
			}
			
			
			$txt_html.= "</tr>\n";
			
			$i++;
		}  
	
		$txt_html.= "<tr class=\"TRalter3\">\n";
		$txt_html.= "<td>&nbsp;</td>\n";
		$txt_html.= "<td>&nbsp;</td>\n";
		$txt_html.= "<td>&nbsp;</td>\n";
		$txt_html.= "<td>";
		$txt_html.= "<div align=\"right\">";
		$txt_html.= "Total :";
		$txt_html.= "</div>";
		$txt_html.= "</td>\n";
		$txt_html.= "<td>";
		$txt_html.= "<div align=\"right\">";
		$txt_html.= number_format($totalint,2,'.','')."€";
		$txt_html.= "</div>";
		$txt_html.= "</td>\n";		
		$txt_html.= "<td>";
		$txt_html.= "<div align=\"left\">";
		$txt_html.= "TTC";
		$txt_html.= "</div>";
		$txt_html.= "<tr class=\"TRalter3\">\n";
		$txt_html.= "<td>&nbsp;</td>\n";
		$txt_html.= "<td>&nbsp;</td>\n";
		$txt_html.= "<td>&nbsp;</td>\n";
		$txt_html.= "<td>";
		$txt_html.= "<div align=\"right\">";
		$txt_html.= "Acompte :";
		$txt_html.= "</div>";
		$txt_html.= "</td>\n";
		$txt_html.= "<td>";
		$txt_html.= "<div align=\"right\">";
		if ($int_acpte<>0)
			$txt_html.= "-".number_format($int_acpte,2,'.','')."€";
		else
			$txt_html.= "&nbsp;";
		$txt_html.= "</div>";
		$txt_html.= "<td>";
		$txt_html.= "<div align=\"left\">";
		$txt_html.= "TTC";
		$txt_html.= "</div>";
		$txt_html.= "</td>\n";
		$txt_html.= "<tr class=\"TRalter3\">\n";
		$txt_html.= "<td>&nbsp;</td>\n";
		$txt_html.= "<td>&nbsp;</td>\n";
		$txt_html.= "<td>&nbsp;</td>\n";
		$txt_html.= "<td>";
		$txt_html.= "<div align=\"right\">";
		$txt_html.= "Net à payer :";
		$txt_html.= "</div>";
		$txt_html.= "</td>\n";
		$txt_html.= "<td>";
		$txt_html.= "<div align=\"right\">";
		$txt_html.= number_format(($totalint-$int_acpte),2,'.','')."€";
		$txt_html.= "</div>";
		$txt_html.= "</td>\n";
		$txt_html.= "<td>";
		$txt_html.= "<div align=\"left\">";
		$txt_html.= "TTC";
		$txt_html.= "</div>";
		$txt_html.= "</td>\n";
		$txt_html.= "</tr>\n";
		
		$txt_html.= "</table>\n";
		// *** Fin affichage de la table
		
		mysql_free_result($rs);
		mysql_close($conn);  
	}
	else
	{
		$txt_html= "<span class=\"annot_gras_it\">";
		$txt_html.= "Aucun article ou prestation";
		$txt_html.= "</span>";
		$txt_html.= "<br>";
	}
	
	return $txt_html;
}

// Création du code Javascript pour affichage des articles de l'intervention
function cre_js_pdti($td,$code,$acpte,$from)
{
	echo "<script language=\"javascript\">\n";
	echo "function tbpdti() {\n";
	echo "var myid = ".$from."document.getElementById(\"detpdti\");\n";
	echo "myid.innerHTML = '';\n";

	if (nb_art_in_int($code)>0)
	{
		echo "mytable = ".$from."document.createElement(\"TABLE\");\n";
		echo "mytablebody = ".$from."document.createElement(\"tbody\");\n";
		// Création entête
		echo "mycurrent_header = ".$from."document.createElement(\"th\");\n";
		echo "currentth = ".$from."document.createTextNode(\"Code\");\n";
		echo "mycurrent_header.appendChild(currentth);\n";
		echo "mytablebody.appendChild(mycurrent_header);\n";
		echo "mycurrent_header = ".$from."document.createElement(\"th\");\n";
		echo "currentth = ".$from."document.createTextNode(\"Désignation\");\n";
		echo "mycurrent_header.appendChild(currentth);\n";
		echo "mytablebody.appendChild(mycurrent_header);\n";
		echo "mycurrent_header = ".$from."document.createElement(\"th\");\n";
		echo "currentth = ".$from."document.createTextNode(\"PU TTC\");\n";
		echo "mycurrent_header.appendChild(currentth);\n";
		echo "mytablebody.appendChild(mycurrent_header);\n";
		echo "mycurrent_header = ".$from."document.createElement(\"th\");\n";
		echo "currentth = ".$from."document.createTextNode(\"Qté\");\n";
		echo "mycurrent_header.appendChild(currentth);\n";
		echo "mytablebody.appendChild(mycurrent_header);\n";
		echo "mycurrent_header = ".$from."document.createElement(\"th\");\n";
		echo "currentth = ".$from."document.createTextNode(\"Total\");\n";
		echo "mycurrent_header.appendChild(currentth);\n";
		echo "mytablebody.appendChild(mycurrent_header);\n";
		echo "mycurrent_header = ".$from."document.createElement(\"th\");\n";
		if ($td=="edit")
			echo "currentth = ".$from."document.createTextNode(\"Cliquer\");\n";
		else
			echo "currentth = ".$from."document.createTextNode(\"\");\n";
		echo "mycurrent_header.appendChild(currentth);\n";
		echo "mytablebody.appendChild(mycurrent_header);\n";
		// Lignes du tableau
		$totalint = 0;
		
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "SELECT * FROM ".TBPDTI." WHERE pdti_codeint='".$code."' ORDER BY 'pdti_id'";
		
		$rs = mysql_query($rech) or die(mysql_error());
			
		$i = 0;
		
		while ($row = mysql_fetch_assoc($rs)) 
		{         
			// Code produit
			echo "mycurrent_row = ".$from."document.createElement(\"tr\");\n";
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			echo "currenttext = ".$from."document.createTextNode(\"".$row["pdti_codepdt"]."\");\n";
			echo "mycurrent_cell.appendChild(currenttext);\n";
			echo "mycurrent_row.appendChild(mycurrent_cell);\n";
			echo "mycurrent_cell.setAttribute(\"class\", \"tdlst\");\n";
			echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
	
			$rech2 = "SELECT pdt_des FROM ".TBPDT." WHERE pdt_code='".$row["pdti_codepdt"]."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			
			if ($row2 = mysql_fetch_assoc($rs2)) 
				extract($row2);
			
			// Désignation
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			echo "currenttext = ".$from."document.createTextNode(\"".$pdt_des."\");\n";
			echo "mycurrent_cell.appendChild(currenttext);\n";
			// Affichage ligne(s) du numéro de série
			if (trim($row["pdti_ns"]) <> "")
			{
				$ligne_cesure = cesuretxt_tab($row["pdti_ns"],100);
				foreach ($ligne_cesure as $index_ligne=>$ligne)
				{
					$ligne = str_replace('"','\"',$ligne);
					echo "mycurrent_div = ".$from."document.createElement(\"div\");\n";
					echo "mycurrent_div.setAttribute(\"class\", \"annot_moy_it\");\n";
					if ($index_ligne==0)
						$txt="N/S : ";
					else
						$txt="";
					echo "currenttext = ".$from."document.createTextNode(\"".$txt.$ligne."\");\n";
					echo "mycurrent_div.appendChild(currenttext);\n";
					echo "mycurrent_cell.appendChild(mycurrent_div);\n";
					echo "mycurrent_row.appendChild(mycurrent_cell);\n";
				}
			}
			// Affichage ligne(s) du commentaire
			if (trim($row["pdti_texte"]) <> "")
			{
				$ligne_cesure = cesuretxt_tab($row["pdti_texte"],100);
				foreach ($ligne_cesure as $index_ligne=>$ligne)
				{
					$ligne = str_replace('"','\"',$ligne);
					echo "mycurrent_div = ".$from."document.createElement(\"div\");\n";
					echo "mycurrent_div.setAttribute(\"class\", \"annot_moy_it\");\n";
					echo "currenttext = ".$from."document.createTextNode(\"".$ligne."\");\n";
					echo "mycurrent_div.appendChild(currenttext);\n";
					echo "mycurrent_cell.appendChild(mycurrent_div);\n";
					echo "mycurrent_row.appendChild(mycurrent_cell);\n";
				}
			}
			echo "mycurrent_row.appendChild(mycurrent_cell);\n";
			echo "mycurrent_cell.setAttribute(\"class\", \"tdlst\");\n";
			echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
	
			// Prix
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			$txt= $row["pdti_puttc"]."€";
			echo "currenttext = ".$from."document.createTextNode(\"".$txt."\");\n";
			echo "mycurrent_cell.appendChild(currenttext);\n";
			echo "mycurrent_row.appendChild(mycurrent_cell);\n";
			echo "mycurrent_cell.setAttribute(\"class\", \"tdlst\");\n";
			echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
			echo "mycurrent_cell.setAttribute(\"align\", \"right\");\n";
	
			// Quantité
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			echo "currenttext = ".$from."document.createTextNode(\"".$row["pdti_qte"]."\");\n";
			echo "mycurrent_cell.appendChild(currenttext);\n";
			echo "mycurrent_row.appendChild(mycurrent_cell);\n";
			echo "mycurrent_cell.setAttribute(\"class\", \"tdlst\");\n";
			echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
			echo "mycurrent_cell.setAttribute(\"align\", \"right\");\n";
	
			// Total ligne
			$total = $row["pdti_puttc"]*$row["pdti_qte"];
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			$txt= number_format($total,2,'.','')."€";
			echo "currenttext = ".$from."document.createTextNode(\"".$txt."\");\n";
			echo "mycurrent_cell.appendChild(currenttext);\n";
			echo "mycurrent_row.appendChild(mycurrent_cell);\n";
			echo "mycurrent_cell.setAttribute(\"class\", \"tdlst\");\n";
			echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
			echo "mycurrent_cell.setAttribute(\"align\", \"right\");\n";
	
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			if ($td == "edit")
			{
				$urleditart = "pdti_maj.php?td=edit&ci=".$row["pdti_codeint"]."&ca=".$row["pdti_codepdt"];
				echo "mycurrent_link = ".$from."document.createElement(\"a\");\n";
				echo "mycurrent_link.setAttribute(\"href\", \"javascript:;\");\n";
				echo "mycurrent_link.setAttribute(\"onclick\", \"fensaisie('".$urleditart."',700,500,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');\");\n";
				echo "mycurrent_img = ".$from."document.createElement(\"img\");\n";
				echo "mycurrent_img.setAttribute(\"src\", \"../img/edit.png\");\n";
				echo "mycurrent_img.setAttribute(\"title\", \"Modifier la ligne article\");\n";
				echo "mycurrent_img.setAttribute(\"width\", \"22\");\n";
				echo "mycurrent_img.setAttribute(\"height\", \"22\");\n";
				echo "mycurrent_img.setAttribute(\"border\", \"0\");\n";
				echo "mycurrent_img.setAttribute(\"align\", \"absmiddle\");\n";
				echo "mycurrent_link.appendChild(mycurrent_img);\n";
				echo "mycurrent_cell.appendChild(mycurrent_link);\n";
				echo "currenttext = ".$from."document.createTextNode(\" \");\n";
				echo "mycurrent_cell.appendChild(currenttext);\n";
				$urldelart = "pdti_maj.php?td=del&ci=".$row["pdti_codeint"]."&ca=".$row["pdti_codepdt"];
				echo "mycurrent_link = ".$from."document.createElement(\"a\");\n";
				echo "mycurrent_link.setAttribute(\"href\", \"javascript:;\");\n";
				echo "mycurrent_link.setAttribute(\"onclick\", \"fensaisie('".$urldelart."',700,500,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');\");\n";
				echo "mycurrent_img = ".$from."document.createElement(\"img\");\n";
				echo "mycurrent_img.setAttribute(\"src\", \"../img/del.gif\");\n";
				echo "mycurrent_img.setAttribute(\"title\", \"Supprimer la ligne article\");\n";
				echo "mycurrent_img.setAttribute(\"width\", \"22\");\n";
				echo "mycurrent_img.setAttribute(\"height\", \"22\");\n";
				echo "mycurrent_img.setAttribute(\"border\", \"0\");\n";
				echo "mycurrent_img.setAttribute(\"align\", \"absmiddle\");\n";
				echo "mycurrent_link.appendChild(mycurrent_img);\n";
				echo "mycurrent_cell.appendChild(mycurrent_link);\n";
			}
			else
			{
				echo "currenttext = ".$from."document.createTextNode(\"\");\n";
				echo "mycurrent_cell.appendChild(currenttext);\n";
			}
			echo "mycurrent_row.appendChild(mycurrent_cell);\n";
			echo "mycurrent_cell.setAttribute(\"class\", \"tdlst\");\n";
			echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
			echo "mycurrent_cell.setAttribute(\"align\", \"center\");\n";
	
			if ($td == "edit")
			{
				// CREATION lien dynamique sur icône (édition, suppression, etc...)
				$txt_html.= "<td>";
				$txt_html.= "<div align=\"center\">";
				$txt_html.= "<a href=\"javascript:;\" onclick=\"fensaisie('".$urleditart."',700,500,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');\")><img src=\"../img/edit.png\" title=\"Modifier la ligne article\" width=\"22\" height=\"22\" border=\"0\" align=\"absmiddle\"></a>";
				$txt_html.= " ";
				$txt_html.= "<a href=\"javascript:;\" onclick=\"fensaisie('".$urldelart."',700,500,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');\")><img src=\"../img/del.gif\" title=\"Supprimer la ligne article\" width=\"22\" height=\"22\" border=\"0\" align=\"absmiddle\"></a>";
				$txt_html.= "</div>";
				$txt_html.= "</td>\n";
			}
			else
			{
				$txt_html.= "<td >";
				$txt_html.= "&nbsp;";
				$txt_html.= "</td>\n";
			}
	
			echo "mytablebody.appendChild(mycurrent_row);\n";
	
			// Couleurs alternées
			if($i%2 == 0)  
				echo "mycurrent_row.setAttribute(\"class\", \"TRalter1\");\n";
			else
				echo "mycurrent_row.setAttribute(\"class\", \"TRalter2\");\n";

			$totalint = $totalint+$total;
			
			$i++;
		}  
	
		// Pied de liste

		// Total TTC
		echo "mycurrent_row = ".$from."document.createElement(\"tr\");\n";
		for ($j=0;$j<3;$j++)
		{
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			echo "currenttext = ".$from."document.createTextNode(\"\");\n";
			echo "mycurrent_cell.appendChild(currenttext);\n";
			echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		}
		echo "mycurrent_row.setAttribute(\"class\", \"TRalter3\");\n";

		echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
		echo "currenttext = ".$from."document.createTextNode(\"Total :\");\n";
		echo "mycurrent_cell.appendChild(currenttext);\n";
		echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
		echo "mycurrent_cell.setAttribute(\"align\", \"right\");\n";
		echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
		$txt = number_format($totalint,2,'.','')."€";
		echo "currenttext = ".$from."document.createTextNode(\"".$txt."\");\n";
		echo "mycurrent_cell.appendChild(currenttext);\n";
		echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
		echo "mycurrent_cell.setAttribute(\"align\", \"right\");\n";
		echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
		echo "currenttext = ".$from."document.createTextNode(\"TTC\");\n";
		echo "mycurrent_cell.appendChild(currenttext);\n";
		echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
		echo "mycurrent_cell.setAttribute(\"align\", \"left\");\n";
		echo "mytablebody.appendChild(mycurrent_row);\n";
	
		// Acompte
		echo "mycurrent_row = ".$from."document.createElement(\"tr\");\n";
		for ($j=0;$j<3;$j++)
		{
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			echo "currenttext = ".$from."document.createTextNode(\"\");\n";
			echo "mycurrent_cell.appendChild(currenttext);\n";
			echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		}
		echo "mycurrent_row.setAttribute(\"class\", \"TRalter3\");\n";

		echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
		echo "currenttext = ".$from."document.createTextNode(\"Acompte :\");\n";
		echo "mycurrent_cell.appendChild(currenttext);\n";
		echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
		echo "mycurrent_cell.setAttribute(\"align\", \"right\");\n";
		echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
		if ($acpte<>0)
			$txt = "-".number_format($acpte,2,'.','')."€";
		else
			$txt = "";
		echo "currenttext = ".$from."document.createTextNode(\"".$txt."\");\n";
		echo "mycurrent_cell.appendChild(currenttext);\n";
		echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
		echo "mycurrent_cell.setAttribute(\"align\", \"right\");\n";
		echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
		if ($acpte<>0)
			$txt = "TTC";
		else
			$txt = "";
		echo "currenttext = ".$from."document.createTextNode(\"".$txt."\");\n";
		echo "mycurrent_cell.appendChild(currenttext);\n";
		echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
		echo "mycurrent_cell.setAttribute(\"align\", \"left\");\n";
		echo "mytablebody.appendChild(mycurrent_row);\n";
	
		// Net à payer
		echo "mycurrent_row = ".$from."document.createElement(\"tr\");\n";
		for ($j=0;$j<3;$j++)
		{
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			echo "currenttext = ".$from."document.createTextNode(\"\");\n";
			echo "mycurrent_cell.appendChild(currenttext);\n";
			echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		}
		echo "mycurrent_row.setAttribute(\"class\", \"TRalter3\");\n";

		echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
		echo "currenttext = ".$from."document.createTextNode(\"Net à payer :\");\n";
		echo "mycurrent_cell.appendChild(currenttext);\n";
		echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
		echo "mycurrent_cell.setAttribute(\"align\", \"right\");\n";
		echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
		$txt = number_format(($totalint-$acpte),2,'.','')."€";
		echo "currenttext = ".$from."document.createTextNode(\"".$txt."\");\n";
		echo "mycurrent_cell.appendChild(currenttext);\n";
		echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
		echo "mycurrent_cell.setAttribute(\"align\", \"right\");\n";
		echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
		echo "currenttext = ".$from."document.createTextNode(\"TTC\");\n";
		echo "mycurrent_cell.appendChild(currenttext);\n";
		echo "mycurrent_row.appendChild(mycurrent_cell);\n";
		echo "mycurrent_cell.setAttribute(\"valign\", \"top\");\n";
		echo "mycurrent_cell.setAttribute(\"align\", \"left\");\n";
		echo "mytablebody.appendChild(mycurrent_row);\n";

		// Fermeture de la table
		echo "mytable.appendChild(mytablebody);\n";
		echo "myid.appendChild(mytable);\n";
		echo "mytable.setAttribute(\"class\", \"Ltable\");\n";
		echo "mytable.setAttribute(\"border\", \"0\");\n";
		echo "mytable.setAttribute(\"width\", \"800\");\n";
		$totalgen=$totalint-$acpte;
		echo $from."document.int_maj.totalint.value=\"".$totalgen."\";\n";
	}
	else
		echo $from."document.int_maj.totalint.value=\"0\";\n";
	echo "}\n";
	echo "</script>\n";
}

?>