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

Version 0.9.9

Copyright (C) 2006-2009  Sylvain FATOME
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr

Web : http://smitst.homelinux.com
****************************************************************************************/


// Vérification des tentatives de hack
$pos=strpos($_SERVER['PHP_SELF'],"inc.php");
if ($pos===FALSE)
	$ok_inc=TRUE;
else
	$ok_inc=FALSE;
if ($ok_inc==FALSE) header("Location: ../index.php");

/**************************************************/
/* Fonctions pour les accès aux fichiers Location */
/**************************************************/

/** Contrats de location */

// Lecture d'un enregistrement contrat
function lit_enr_coloc($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBCOLOC. "` WHERE `coloc_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 de location
function nb_coloc()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOLOC;
	$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 de location sur un client
// Filtre sur niveau et sur code agence/donneur d'ordre
function nb_coloc_cli($client,$level,$codead)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOLOC. "` WHERE `coloc_codecli` = '".$client."'";
	if (trim($codead)<>"")
	{
		if (($level==3) OR ($level==4))
			$rexist.= " AND `coloc_codeage` = '".$codead."'";
	}
	$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 de location sur un technicien
// Filtre sur type client
function nb_coloc_tec($client,$typcli)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOLOC. "` WHERE `coloc_codecli` = '".$client."'";
	$rexist.= " AND `coloc_typcli` = '".$typcli."'";
	$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 Javascript pour affichage des contacts clients
function cre_js_ddeursloc($ncli,$idsite,$from)
{
	echo "<script language=\"javascript\">\n";
	
	echo "function updatddeurs() {\n";
	echo "var myid = ".$from."document.getElementById(\"ddeur\");\n";
	echo 'if (myid) {';
		echo "myid.innerHTML = '';\n";
	
		if (trim($ncli)<>"")
		{
			echo 'option = '.$from.'document.createElement("option");';
			echo 'option.setAttribute("value","");';
			echo 'option.innerHTML ="-- Choisissez un  contact --";';
			echo 'myid.appendChild(option);';

			// On se connecte a la base
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);

			// Données client
			$rech = "SELECT * FROM ".TBCLI." WHERE `cli_code` = '".$ncli."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
				if ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
					$txt_ccli = $cli_nom." ".$cli_prenom." (Contact principal";
					$txt_ccli.= " - ".$cli_telf.")";
					echo 'option = '.$from.'document.createElement("option");';
					echo 'option.setAttribute("value","-1");';
					echo 'option.innerHTML ="'.$txt_ccli.'";';
					echo 'myid.appendChild(option);';
				}
			}

			// Données site client
			if (trim($idsite)<>"")
			{
				if (intval($idsite)>0)
				{
					$rech = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '$idsite'";
					$rs = mysql_query($rech)or die(mysql_error());
					if ($resultat = mysql_fetch_array($rs)) 
					{
						extract($resultat);
						$txt_ccli = $scli_rnom." ".$scli_rprenom." (Contact site";
						$txt_ccli.= " - ".$scli_telf.")";
						echo 'option = '.$from.'document.createElement("option");';
						echo 'option.setAttribute("value","0");';
						echo 'option.innerHTML ="'.$txt_ccli.'";';
						echo 'myid.appendChild(option);';
					}
					mysql_free_result($rs);
				}
			}
			// Filtre sur client
			$rech = "SELECT * FROM ".TBCCLI." WHERE `ccli_codecli` = '".$ncli."' ORDER BY ccli_nom";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
	
					$txt_ccli = $ccli_nom." ".$ccli_prenom;
					$row2=lit_enr_fonc($ccli_codefonc,FALSE);
					if (is_array($row2))
					{
						extract($row2);
						$txt_fonc=$fonc_desc;
					}
					else
						$txt_fonc=$ccli_codefonc;
					$txt_ccli.=" (".$txt_fonc;
					$txt_ccli.= " - ".$ccli_telf.")";
					echo 'option = '.$from.'document.createElement("option");';
					echo 'option.setAttribute("value","0");';
					echo 'option.innerHTML ="'.$txt_ccli.'";';
					echo 'myid.appendChild(option);';
				}
			}		
			mysql_close();
		}
	echo "}\n";
	echo "}\n";
	
	echo "</script>\n";
}

/** Produits à louer */

// Nombre d'articles
function nb_pdtloc()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBPDTLOC."`";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Nombre d'articles dans un contrat de location
function nb_pdtloc_in_coloc($coloc,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rexist = "SELECT * FROM `" .TBPDTL. "` WHERE `pdtl_codecoloc` = '$coloc'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);

	return $nbexist;
}

// Vérifie si tous les produits d'un contrat sont revenus
function all_pdtloc_ret_in_coloc($coloc,$open)
{
	$all=TRUE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rexist = "SELECT * FROM `" .TBPDTL. "` WHERE `pdtl_codecoloc` = '$coloc' AND `pdtl_dateret` = '0000-00-00'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) >0)
		$all=FALSE;
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);

	return $all;
}

// Retourne la dernière date de retour pour les produits d'un contrat donné
function lastdate_ret_in_coloc($coloc,$open)
{
	$lastdate="";
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rexist = "SELECT MAX(pdtl_dateret) AS lastdate FROM `" .TBPDTL. "` WHERE `pdtl_codecoloc` = '$coloc'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) >0)
	{
		if ($row = mysql_fetch_assoc($rs2))
			extract($row);
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	return $lastdate;
}

// Clôt un contrat de location à une date donnée
function clot_coloc($coloc,$dateclos,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech = "UPDATE `" .TBCOLOC;
	$rech .= "` SET `coloc_dateclos`='".$dateclos."'";
	$rech .=" WHERE `coloc_code` = '".$coloc."'";
	$rs2 = mysql_query($rech) or die(mysql_error());
	if ($open==TRUE)
		mysql_close($conn2);
}

// Lecture d'un enregistrement produit
function lit_enr_pdtloc($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBPDTLOC. "` WHERE `pdtloc_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 location
function exist_pdt_in_coloc($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBPDTL. "` WHERE `pdtl_codepdtloc` = '$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 location
function lit_enr_pdt_in_coloc($codep,$codel)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBPDTL. "` WHERE `pdtl_codepdtloc` = '$codep' AND `pdtl_codecoloc` = '$codel'";
	$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 location non close
function exist_pdt_in_coloc_ouv($code)
{

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

	$rech2 = "SELECT ".TBPDTL.".pdtl_codepdtloc, ".TBPDTL.".pdtl_codecoloc, ".TBCOLOC.".coloc_code, ";
	$rech2.= TBCOLOC.".coloc_datefin FROM ".TBPDTL.", ".TBCOLOC." WHERE (".TBPDTL.".pdtl_codepdtloc = '$code' AND ".TBPDTL.".pdtl_codecoloc = ".TBCOLOC.".coloc_code AND ".TBCOLOC.".coloc_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;
}

// Vérification si un produit est à remettre dans une fiche location non close
function exist_adep_in_coloc_ouv($code)
{

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

	$rech2 = "SELECT ".TBPDTL.".pdtl_codepdtloc, ".TBPDTL.".pdtl_codecoloc, ".TBCOLOC.".coloc_code, ";
	$rech2.= TBCOLOC.".coloc_datefin FROM ".TBPDTL.", ".TBCOLOC." WHERE (".TBPDTL.".pdtl_codepdtloc = '$code'";
	$rech2.= " AND ".TBPDTL.".pdtl_datedep = '0000-00-00' AND ".TBPDTL.".pdtl_codecoloc = ".TBCOLOC.".coloc_code AND ".TBCOLOC.".coloc_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;
}

// Vérification si un produit est à retourner dans une fiche location non close
function exist_aret_in_coloc_ouv($code)
{

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

	$rech2 = "SELECT ".TBPDTL.".pdtl_codepdtloc, ".TBPDTL.".pdtl_codecoloc, ".TBCOLOC.".coloc_code, ";
	$rech2.= TBCOLOC.".coloc_datefin FROM ".TBPDTL.", ".TBCOLOC." WHERE (".TBPDTL.".pdtl_codepdtloc = '$code'";
	$rech2.= " AND ".TBPDTL.".pdtl_dateret = '0000-00-00' AND ".TBPDTL.".pdtl_codecoloc = ".TBCOLOC.".coloc_code AND ".TBCOLOC.".coloc_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;
}

// Création du code Javascript pour affichage des barêmes dans une fiche article à louer
function cre_js_barloc($td,$code,$from)
{
	global
		$datea,
		$lang,
		$lng_tjouri_abr,
		$lng_tduree,
		$non_oui,
		$txt_ent_lst_clic;
		
	echo "<script language=\"javascript\">\n";
	
	echo "function tbbarloc() {\n";
	echo "var myid = ".$from."document.getElementById(\"detbarloc\");\n";
	echo "myid.innerHTML = '';\n";

	if (nb_barloc_in_pdtloc($code)>0)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);

		// *** Affichage de la table
		$str_html = "<table class=\"Ltable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">";     

		// Création entête
		$str_html.="<th>";
		$str_html.="Désignation";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Période";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Validité";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Pu HT";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="TVA";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Pu TTC";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Actif";
		$str_html.="</th>";
		
		if ($td == "edit")
		{
			$str_html.="<th>";
			$str_html.=$txt_ent_lst_clic;
			$str_html.="</th>";
		}
		

		// Lignes du tableau
		$rech = "SELECT * FROM ".TBBARLOC." WHERE barloc_codepdtloc='".$code."' ORDER BY `barloc_des`";
		$rs = mysql_query($rech) or die(mysql_error());
			
		$i = 0;
		
		while ($row = mysql_fetch_assoc($rs))
		{         
			// Couleurs alternées
			if($i%2 == 0)  
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter1'\" class=\"TRalter1\">";
			else
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter2'\" class=\"TRalter2\">";

			// Désignation
			$str_html.="<td class=\"tdlst\" valign=\"top\">";
			$str_html.=$row["barloc_des"];

			// Assurance (texte)
			if ($row["barloc_mtass"]<>0)
			{
				$txt = "Assurance";
				if ($row["barloc_assobl"]==1)
					$txt.=" obligatoire";
				else
					$txt.=" facultative";
				$str_html.="<div class=\"annot_moy_it\" align=\"right\">";
				$str_html.=$txt;
				$str_html.="</div>";
			}

			$str_html.="</td>";
	
			// Période
			$rech2 = "SELECT * FROM ".TBPERLOC." WHERE perloc_code='".$row["barloc_codeperloc"]."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);

			// Affichage détail de la période
			//...Plage de démarrage de la location
			$txt="Plage début :";
			if ($perloc_jourd1<>-1)
				$txt.= " ".$lng_tjouri_abr[$perloc_jourd1]." - ".$lng_tjouri_abr[$perloc_jourd2];
			else
				$txt.= " Indifférent";
			$str_html.="<td class=\"tdlst\" valign=\"top\">";
			$str_html.=$txt;

			//...Durée
			$txt = " (sur une durée de ".$perloc_nduree." ".$lng_tduree[$perloc_tduree].")";
			$str_html.="<span class=\"annot_moy_it\">";
			$str_html.=$txt;
			$str_html.="</span>";

			$str_html.="</td>";
	
			// Validité
			if (formatedate($row["barloc_dateval2"])=="")
				$txt="Depuis le ".formatedate($row["barloc_dateval1"]);
			else
				$txt="Du ".$row["barloc_dateval1"]." au ".$row["barloc_dateval2"];
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			$str_html.=$txt;
			$str_html.="</td>";
			
			// PU HT
			$txt=format_num($row["barloc_pvht"],2);
			$txt.=" ".$lang['symbole_monnaie_txt'];
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"right\">";
			$str_html.=$txt;

			// Assurance (HT)
			if ($row["barloc_mtass"]<>0)
			{
				//... Montant
				$txt=format_num($row["barloc_mtass"],2);
				$txt.=" ".$lang['symbole_monnaie_txt'];
				$str_html.="<div class=\"annot_moy_it\">";
				$str_html.=$txt;
				$str_html.="</div>";
			}
			$str_html.="</td>";
	
			// TVA
			$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$row["barloc_codetva"]."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);
			$txtva=$tva_tx/100;
			$puttc=round(($row["barloc_pvht"]*(1+$txtva))*100)/100;
			$txt=format_num($tva_tx,2)."%";

			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"right\">";
			$str_html.=$txt;

			// Assurance (TVA)
			if ($row["barloc_mtass"]<>0)
			{
				//... Taux
				$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$row["barloc_codetvaass"]."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				if ($row2 = mysql_fetch_assoc($rs2))
					extract($row2);
				$txt=format_num($tva_tx,2)."%";
				$txtvaass=$tva_tx/100;
				$mtassttc=round(($row["barloc_mtass"]*(1+$txtvaass))*100)/100;
				$str_html.="<div class=\"annot_moy_it\">";
				$str_html.=$txt;
				$str_html.="</div>";
			}
			$str_html.="</td>";
	
			// PU TTC
			$txt=format_num($puttc,2);
			$txt.=" ".$lang['symbole_monnaie_txt'];
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"right\">";
			$str_html.=$txt;


			// Assurance (TTC)
			if ($row["barloc_mtass"]<>0)
			{
				//... Montant
				$txt=format_num($mtassttc,2);
				$txt.=" ".$lang['symbole_monnaie_txt'];
				$str_html.="<div class=\"annot_moy_it\">";
				$str_html.=$txt;
				$str_html.="</div>";
			}
			$str_html.="</td>";
	
			// Actif
			$txt = $non_oui[$row["barloc_actif"]];
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			$str_html.=$txt;
			$str_html.="</td>";
	
			if ($td == "edit")
			{
				$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
				$urleditbarloc = "barloc_maj.php?td=edit&cp=".$row["barloc_codepdtloc"]."&pl=".$row["barloc_codeperloc"];
				$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urleditbarloc."',900,550,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no')\")>";
				$str_html.="<img src=\"../img/edit.png\" border=\"0\" align=\"absmiddle\" title=\"Modifier le barême\"></a>";
				$str_html.=" ";

				$urldelbarloc = "barloc_maj.php?td=del&cp=".$row["barloc_codepdtloc"]."&pl=".$row["barloc_codeperloc"];
				$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urldelbarloc."',900,550,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no')\")>";
				$str_html.="<img src=\"../img/del.png\" border=\"0\" align=\"absmiddle\" title=\"Supprimer le barême\"></a>";
				$str_html.=" ";
				$str_html.="</td>";
			}
			$str_html.="</tr>";
			$i++;

		}
	
		mysql_close($conn);

		// Fermeture de la table
		$str_html.="</table>";

		// MAJ de l'id
		echo "var str_html = '".addslashes($str_html)."';\n";
		echo "myid.innerHTML=str_html;\n";
	}
	echo "}\n";
	
	echo "</script>\n";
}

// Création du code Javascript pour affichage des tarifs consommables dans une fiche article à louer
function cre_js_tarcons($td,$ibl,$from)
{
	global
		$datea,
		$lang,
		$lng_tjouri_abr,
		$lng_tduree,
		$non_oui,
		$txt_ent_lst_clic;
		
	echo "<script language=\"javascript\">\n";
	
	echo "function tbtarcons1() {\n";
	echo "var myid = ".$from."document.getElementById(\"dettarcons1\");\n";
	echo "if (myid) {\n";
	echo "myid.innerHTML = '';\n";
	
	$nb_tarcons1=nb_tarcons_in_barloc($ibl,1);
	$infini1=exist_infini_tcons_id($ibl,1,TRUE);

	// Type consommable n°1
	if ($nb_tarcons1>0)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);

		// *** Affichage de la table
		$str_html = "<table class=\"Ltable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">";     

		// Création entête
		$str_html.="<th>";
		$str_html.="Depuis (qté)";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Jusqu'à (qté)";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="PU HT";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="TVA";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="PU TTC";
		$str_html.="</th>";
		
		$str_html.="<th>";
		if ($td=="edit")
			$str_html.=$txt_ent_lst_clic;
		else
			$str_html.=" ";
		$str_html.="</th>";

		// Récupération données du barême
		$rech2 = "SELECT * FROM ".TBBARLOC." WHERE barloc_id='".$ibl."'";
		$rs2 = mysql_query($rech2) or die(mysql_error());
		if ($row2 = mysql_fetch_assoc($rs2))
			extract($row2);

		// Lignes du tableau
		$rech = "SELECT * FROM ".TBBARLOCTC." WHERE barloctc_idbarloc='".$ibl."' AND barloctc_ncons='1' ORDER BY `barloctc_min`";
		$rs = mysql_query($rech) or die(mysql_error());
			
		$i = 0;
		
		while ($row = mysql_fetch_assoc($rs))
		{
			extract($row);
		
			// Couleurs alternées
			if($i%2 == 0)  
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter1'\" class=\"TRalter1\">";
			else
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter2'\" class=\"TRalter2\">";

			// Depuis
			$str_html.="<td class=\"tdlst\" valign=\"top\">";
			$str_html.=$barloctc_min;
			$str_html.="</td>";

			// Jusqu'à
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			$txt=$barloctc_max;
			if (intval($barloctc_max)==0)
				$txt="infini";
			$str_html.="<td class=\"tdlst\" valign=\"top\">";
			$str_html.=$txt;
			$str_html.="</td>";

			// PU HT
			$txt=format_num($barloctc_pvht,2);
			$txt.=" ".$lang['symbole_monnaie_txt'];
			$str_html.="<td class=\"TDnumberD\" valign=\"top\">";
			$str_html.=$txt;
			$str_html.="</td>";

			// TVA
			$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$barloc_tvacons1."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);
			$txtva=$tva_tx/100;
			$puttc=round(($barloctc_pvht*(1+$txtva))*100)/100;
			$txt=format_num($tva_tx,2)."%";
			$str_html.="<td class=\"TDnumberD\" valign=\"top\">";
			$str_html.=$txt;
			$str_html.="</td>";
	
			// PU TTC
			$txt=format_num($puttc,2);
			$txt.=" ".$lang['symbole_monnaie_txt'];
			$str_html.="<td class=\"TDnumberD\" valign=\"top\">";
			$str_html.=$txt;
			$str_html.="</td>";

			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			if (($td == "edit") AND ($nb_tarcons1==($i+1)))
			{
				$str_html.="<a href=\"javascript:;\" onclick=\"fen_tcons_barloc('edit','".$ibl."','".$barloctc_id."','1');\")>";
				$str_html.="<img src=\"../img/edit.png\" border=\"0\" align=\"absmiddle\" title=\"Modifier le tarif\"></a>";
				$str_html.=" ";

				$str_html.="<a href=\"javascript:;\" onclick=\"fen_tcons_barloc('del','".$ibl."','".$barloctc_id."','1');\")>";
				$str_html.="<img src=\"../img/del.png\" border=\"0\" align=\"absmiddle\" title=\"Supprimer le tarif\"></a>";
				$str_html.=" ";
			}
			else
				$str_html.=" ";
			$str_html.="</td>";
	
			$str_html.="</tr>";
			$i++;
		}

		mysql_close($conn);
	
		// Fermeture de la table
		$str_html.="</table>";

		// MAJ de l'id
		echo "var str_html = '".addslashes($str_html)."';\n";
		echo "myid.innerHTML=str_html;\n";

		// MAJ de nextmin1 et de addtc1
		echo "var mynm = ".$from."document.getElementById(\"nextmin1\");\n";
		echo "mynm.value='".next_min_tcons($ibl,$barloc_qconsi1,1,TRUE)."';\n";
		echo "var objet = ".$from."document.getElementById('addtc1');\n";
		if ($infini1==TRUE)
			echo "objet.style.display = 'none';\n";
		else
			echo "objet.style.display = 'block';\n";
	}
	echo "}}\n";


	echo "function tbtarcons2() {\n";
	echo "var myid = ".$from."document.getElementById(\"dettarcons2\");\n";
	echo "if (myid) {\n";
	echo "myid.innerHTML = '';\n";
	
	$nb_tarcons2=nb_tarcons_in_barloc($ibl,2);
	$infini2=exist_infini_tcons_id($ibl,2,TRUE);

	// Type consommable n°2
	if ($nb_tarcons2>0)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);

		// *** Affichage de la table
		$str_html = "<table class=\"Ltable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">";     

		// Création entête
		$str_html.="<th>";
		$str_html.="Depuis (qté)";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Jusqu'à (qté)";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="PU HT";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="TVA";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="PU TTC";
		$str_html.="</th>";
		
		$str_html.="<th>";
		if ($td=="edit")
			$str_html.=$txt_ent_lst_clic;
		else
			$str_html.=" ";
		$str_html.="</th>";


		// Récupération données du barême
		$rech2 = "SELECT * FROM ".TBBARLOC." WHERE barloc_id='".$ibl."'";
		$rs2 = mysql_query($rech2) or die(mysql_error());
		if ($row2 = mysql_fetch_assoc($rs2))
			extract($row2);

		// Lignes du tableau
		$rech = "SELECT * FROM ".TBBARLOCTC." WHERE barloctc_idbarloc='".$ibl."' AND barloctc_ncons='2' ORDER BY `barloctc_min`";
		$rs = mysql_query($rech) or die(mysql_error());
			
		$i = 0;
		
		while ($row = mysql_fetch_assoc($rs))
		{
			extract($row);
		
			// Couleurs alternées
			if($i%2 == 0)  
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter1'\" class=\"TRalter1\">";
			else
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter2'\" class=\"TRalter2\">";

			// Depuis
			$str_html.="<td class=\"tdlst\" valign=\"top\">";
			$str_html.=$barloctc_min;
			$str_html.="</td>";

			// Jusqu'à
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			$txt=$barloctc_max;
			if (intval($barloctc_max)==0)
				$txt="infini";
			$str_html.="<td class=\"tdlst\" valign=\"top\">";
			$str_html.=$txt;
			$str_html.="</td>";

			// PU HT
			$txt=format_num($barloctc_pvht,2);
			$txt.=" ".$lang['symbole_monnaie_txt'];
			$str_html.="<td class=\"TDnumberD\" valign=\"top\">";
			$str_html.=$txt;
			$str_html.="</td>";

			// TVA
			$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$barloc_tvacons1."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);
			$txtva=$tva_tx/100;
			$puttc=round(($barloctc_pvht*(1+$txtva))*100)/100;
			$txt=format_num($tva_tx,2)."%";
			$str_html.="<td class=\"TDnumberD\" valign=\"top\">";
			$str_html.=$txt;
			$str_html.="</td>";
	
			// PU TTC
			$txt=format_num($puttc,2);
			$txt.=" ".$lang['symbole_monnaie_txt'];
			$str_html.="<td class=\"TDnumberD\" valign=\"top\">";
			$str_html.=$txt;
			$str_html.="</td>";

			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			if (($td == "edit") AND ($nb_tarcons1==($i+1)))
			{
				$str_html.="<a href=\"javascript:;\" onclick=\"fen_tcons_barloc('edit','".$ibl."','".$barloctc_id."','2');\")>";
				$str_html.="<img src=\"../img/edit.png\" border=\"0\" align=\"absmiddle\" title=\"Modifier le tarif\"></a>";
				$str_html.=" ";

				$str_html.="<a href=\"javascript:;\" onclick=\"fen_tcons_barloc('del','".$ibl."','".$barloctc_id."','2');\")>";
				$str_html.="<img src=\"../img/del.png\" border=\"0\" align=\"absmiddle\" title=\"Supprimer le tarif\"></a>";
				$str_html.=" ";
			}
			else
				$str_html.=" ";
			$str_html.="</td>";
	
			$str_html.="</tr>";
			$i++;
		}

		mysql_close($conn);
	
		// Fermeture de la table
		$str_html.="</table>";

		// MAJ de l'id
		echo "var str_html = '".addslashes($str_html)."';\n";
		echo "myid.innerHTML=str_html;\n";

		// MAJ de nextmin2 et de addtc2
		echo "var mynm = ".$from."document.getElementById(\"nextmin2\");\n";
		echo "mynm.value='".next_min_tcons($ibl,$barloc_qconsi2,2,TRUE)."';\n";
		echo "var objet = ".$from."document.getElementById('addtc2');\n";
		if ($infini1==TRUE)
			echo "objet.style.display = 'none';\n";
		else
			echo "objet.style.display = 'block';\n";
	}
	echo "}}\n";
	
	echo "</script>\n";
}

// Vérification de la disponibilité d'un produit sur une période donnée
function pdtloc_dispo($code,$dated,$heured,$datef,$heuref,$open)
{
	$dispo=TRUE;
	$ts1=getTimeStamp($dated.' '.$heured);
	$ts2=getTimeStamp($datef.' '.$heuref);
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBPDTL. "` WHERE `pdtl_codepdtloc` = '".$code."'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	while ($row2 = mysql_fetch_assoc($rs2)) 
	{
		extract($row2);
		$tspdtldeb=getTimeStamp(datefr($pdtl_datedeb).' '.$pdtl_heuredeb);
		$tspdtlfin=getTimeStamp(datefr($pdtl_datefin).' '.$pdtl_heurefin);
		// Date de début entre une date de début de location et une date de fin de location
		if (($tspdtldeb<=$ts1) AND ($ts1<=$tspdtlfin))
		{
			$dispo=FALSE;
			break;
		}
		// Date de fin entre une date de début de location et une date de fin de location
		if (($tspdtldeb<=$ts2) AND ($ts2<=$tspdtlfin))
		{
			$dispo=FALSE;
			break;
		}
		// Plage de date de location entre la date de début et la date de fin
		if (($ts1<=$tspdtldeb) AND ($tspdtlfin<=$ts2))
		if ((comp_date(datefrang($dated),$pdtl_datedeb)==TRUE) AND (comp_date($pdtl_datefin,datefrang($datef))==TRUE))
		{
			$dispo=FALSE;
			break;
		}
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $dispo;
}

// Calcule le montant des consos HT sur un retour de location,
// à partir de l'id du barême utilisé et les 2 indexes
function calcul_consloc($idbarloc,$codetva,$diffidx1,$diffidx2)
{
	$totconso1=0;
	$totconso2=0;
	$totconsos=0;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	
	// Calcul pour le premier consommable
	if (intval($diffidx1)>0)
	{
		// On recherche dans le barême des consommables
		// ... Il y a une fourchette de valeurs ?
		$rech2 = "SELECT * FROM ".TBBARLOCTC." WHERE barloctc_idbarloc<=".$idbarloc." AND barloctc_ncons=1 AND barloctc_min<=".$diffidx1." AND barloctc_max<=".$diffidx1;
		$rs2 = mysql_query($rech2) or die(mysql_error());
		if (mysql_num_rows($rs2)==1)
		{
			if ($row2 = mysql_fetch_assoc($rs2))
			{
				extract($row2);
				$totconso1=$diffidx1*$barloctc_pvht;
			}
		}
		else // ... Il y a une fourchette "nb unités dépassées - infini" ?
		{
			$rech2 = "SELECT * FROM ".TBBARLOCTC." WHERE barloctc_idbarloc<=".$idbarloc." AND barloctc_ncons=1 AND barloctc_min<=".$diffidx1." AND barloctc_max=0";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if (mysql_num_rows($rs2)==1)
			{
				if ($row2 = mysql_fetch_assoc($rs2))
				{
					extract($row2);
					$totconso1=$diffidx1*$barloctc_pvht;
				}
			}
		}
	}

	// Calcul pour le second consommable
	if (intval($diffidx2)>0)
	{
		// On recherche dans le barême des consommables
		// ... Il y a une fourchette de valeurs ?
		$rech2 = "SELECT * FROM ".TBBARLOCTC." WHERE barloctc_idbarloc<=".$idbarloc." AND barloctc_ncons=2 AND barloctc_min<=".$diffidx2." AND barloctc_max<=".$diffidx2;
		$rs2 = mysql_query($rech2) or die(mysql_error());
		if (mysql_num_rows($rs2)==1)
		{
			if ($row2 = mysql_fetch_assoc($rs2))
			{
				extract($row2);
				$totconso2=$diffidx2*$barloctc_pvht;
			}
		}
		else // ... Il y a une fourchette "nb unités dépassées - infini" ?
		{
			$rech2 = "SELECT * FROM ".TBBARLOCTC." WHERE barloctc_idbarloc<=".$idbarloc." AND barloctc_ncons=2 AND barloctc_min<=".$diffidx2." AND barloctc_max=0";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if (mysql_num_rows($rs2)==1)
			{
				if ($row2 = mysql_fetch_assoc($rs2))
				{
					extract($row2);
					$totconso2=$diffidx1*$barloctc_pvht;
				}
			}
		}
	}
	
	$totconsos=$totconso1+$totconso2;

	mysql_free_result($rs2);
	mysql_close($conn2);
	return $totconsos;
}


// Création du code Javascript pour affichage des barêmes lors de la saisie d'une location
function cre_js_pdtlbl($td,$tab_bl,$tab_pl,$tab_cl,$from)
{
	global
		$datea,
		$lang,
		$lng_tjouri_abr,
		$lng_tduree,
		$non_oui,
		$but_valider,
		$but_choixprea2,
		$but_pen,
		$datemsql,
		$but_idx,
		$txt_ent_lst_clic;
		
	echo "<script language=\"javascript\">\n";
	
	echo "function tbpbarloc() {\n";
	echo "var objet1 = ".$from."document.getElementById('addbarloc');\n";
	echo "var addpen = ".$from."document.getElementById('addpen');\n";
	echo "var addidx = ".$from."document.getElementById('addidx');\n";
	echo "var butenr = ".$from."document.getElementById('enr');\n";
	echo "var objet3 = ".$from."document.getElementById('dateretf');\n";
	echo "if (addpen) { \n";
	echo "var dater = objet3.value; }\n";
	echo "var myid = ".$from."document.getElementById(\"detbarloc\");\n";
	echo "myid.innerHTML = '';\n";
	if (count($tab_bl)>0)
	{
		// *** Affichage de la table
		$str_html = "<table class=\"Ltable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">";     

		// Création entête
		$str_html.="<th>";
		$str_html.="Désignation";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Quantité";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Total HT";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Mt TVA";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Total TTC";
		$str_html.="</th>";
		
		if (($td=="edit") OR ($td=="add") OR ($td=="ret"))
		{
			$str_html.="<th>";
			$str_html.=$txt_ent_lst_clic;
			$str_html.="</th>";
		}

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

		for ($i=0; $i<count($tab_bl); $i++)
		{
			$qte=$tab_bl[$i]["qte"];

			// Récupération enregistrement barême location
			if ($tab_bl[$i]["idbarloc"]<>0)
			{
				$rech2 = "SELECT * FROM ".TBBARLOC." WHERE barloc_id='".$tab_bl[$i]["idbarloc"]."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				if ($row2 = mysql_fetch_assoc($rs2))
					extract($row2);
			}
			else
			{
				$barloc_des="Prêt de matériel sur ".$qte." jour";
				if ($qte>1)
					$barloc_des.="s";
			}

			// Couleurs alternées
			if($i%2 == 0)  
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter1'\" class=\"TRalter1\">";
			else
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter2'\" class=\"TRalter2\">";

			// Désignation
			$txt_des=$barloc_des." (inclus caution";
			if ($tab_bl[$i]["avecass"]==1)
				$txt_des.=" et assurance";
			else
				$txt_des.=", sans assurance";
			$txt_des.=")";
			$str_html.="<td class=\"tdlst\" valign=\"top\">";
			$str_html.=$txt_des;

			if ($tab_bl[$i]["qconsi1"]>0)
			{
				$rech2 = "SELECT * FROM ".TBPDTU." WHERE pdtu_code='".$tab_bl[$i]["pdtuconsi1"]."'";
				$rs2 = mysql_query($rech2)or die(mysql_error());
				if ($row2= mysql_fetch_array($rs2)) 
					extract($row2);
				$txt="Inclus ".$qte." forfait(s) ".$tab_bl[$i]["qconsi1"]." ".strtolower($pdtu_abr);
				$str_html.="<div class=\"annot_moy_it\" valign=\"top\" align=\"right\">";
				$str_html.=$txt;
				$str_html.="</div>";
			}
			if ($tab_bl[$i]["qconsi2"]>0)
			{
				$rech2 = "SELECT * FROM ".TBPDTU." WHERE pdtu_code='".$tab_bl[$i]["pdtuconsi2"]."'";
				$rs2 = mysql_query($rech2)or die(mysql_error());
				if ($row2= mysql_fetch_array($rs2)) 
					extract($row2);
				$txt="Inclus ".$qte." forfait(s) ".$tab_bl[$i]["qconsi2"]." ".strtolower($pdtu_abr);
				$str_html.="<div class=\"annot_moy_it\" valign=\"top\" align=\"right\">";
				$str_html.=$txt;
				$str_html.="</div>";
			}
			$str_html.="</td>";
	
			// Quantité
			$txt=format_num($tab_bl[$i]["qte"],2);
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			$str_html.=$txt;
			$str_html.="</td>";
			
			// Calculs divers
			//... Montant location
			$pvht=$tab_bl[$i]["pvht"];
			$tothtloc=round(($qte*$pvht)*100)/100;
			$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$tab_bl[$i]["codetva"]."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);
			$txtva=$tva_tx/100;
			$totttcloc=round(($tothtloc*(1+$txtva))*100)/100;
			$tottvaloc=$totttcloc-$tothtloc;
			//... Caution
			$pvht=$tab_bl[$i]["caution"];
			$tothtcaut=round($pvht*100)/100;
			$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$tab_bl[$i]["tvacaut"]."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);
			$txtva=$tva_tx/100;
			$totttccaut=round(($tothtcaut*(1+$txtva))*100)/100;
			$tottvacaut=$totttccaut-$tothtcaut;
			//... Assurance
			$tothtass=0;
			$totttcass=0;
			$tottvaass=0;
			if ($tab_bl[$i]["avecass"]==1)
			{
				$pvht=$tab_bl[$i]["mtass"];
				$tothtass=round(($qte*$pvht)*100)/100;
				$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$tab_bl[$i]["codetvaass"]."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				if ($row2 = mysql_fetch_assoc($rs2))
					extract($row2);
				$txtva=$tva_tx/100;
				$totttcass=round(($tothtass*(1+$txtva))*100)/100;
				$tottvaass=$totttcass-$tothtass;
			}
			//...Consommables
			$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$tab_bl[$i]["codetva"]."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);
			$txtva=$tva_tx/100;
			//... Consommable 1
			$pvht=$tab_bl[$i]["puconsi1"];
			$tothtcons1=round(($qte*$pvht)*100)/100;
			$totttccons1=round(($tothtcons1*(1+$txtva))*100)/100;
			$tottvacons1=$totttccons1-$tothtcons1;
			//... Consommable 2
			$pvht=$tab_bl[$i]["puconsi2"];
			$tothtcons2=round(($qte*$pvht)*100)/100;
			$totttccons2=round(($tothtcons2*(1+$txtva))*100)/100;
			$tottvacons2=$totttccons2-$tothtcons2;

			// Affichages
			// Total HT
			$totbarht=$tothtloc+$tothtcaut+$tothtass+$tothtcons1+$tothtcons2;
			$txt=format_num($totbarht,2);
			$txt.=" ".$lang['symbole_monnaie_txt'];
			$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
			$str_html.=$txt;
			$str_html.="</td>";

			// Mt TVA
			$totbartva=$tottvaloc+$tottvacaut+$tottvaass+$tottvacons1+$tottvacons2;
			$txt=format_num($totbartva,2);
			$txt.=" ".$lang['symbole_monnaie_txt'];
			$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
			$str_html.=$txt;
			$str_html.="</td>";

			// total TTC
			$totbarttc=$totttcloc+$totttccaut+$totttcass+$totttccons1+$totttccons2;
			$txt=format_num($totbarttc,2);
			$txt.=" ".$lang['symbole_monnaie_txt'];
			$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
			$str_html.=$txt;
			$str_html.="</td>";

			if (($td == "add") OR ($td == "edit"))
			{
				$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
				$urleditbarloc = "pdtlbl_maj.php?td=edit&cl=".$tab_bl[$i]["codecoloc"]."&ca=".$tab_bl[$i]["codeart"];
				$urleditbarloc.= "&dd=".$tab_bl[$i]["dd"]."&hd=".$tab_bl[$i]["hd"]."&df=".$tab_bl[$i]["df"]."&hf=".$tab_bl[$i]["hf"];
				$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie2('".$urleditbarloc."',800,500,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');\")>";
				$str_html.="<img src=\"../img/edit.png\" border=\"0\" align=\"absmiddle\" title=\"Modifier le barême\"></a>";
				$str_html.=" ";

				$urldelbarloc = "pdtlbl_maj.php?td=del&cl=".$tab_bl[$i]["codecoloc"]."&ca=".$tab_bl[$i]["codeart"];
				$urldelbarloc.= "&dd=".$tab_bl[$i]["dd"]."&hd=".$tab_bl[$i]["hd"]."&df=".$tab_bl[$i]["df"]."&hf=".$tab_bl[$i]["hf"];
				$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie2('".$urldelbarloc."',800,500,'status=no,scrollbars=no,menubar=no,statusbar=no,location=no,locationbar=no');\")>";
				$str_html.="<img src=\"../img/del.png\" border=\"0\" align=\"absmiddle\" title=\"Supprimer le barême\"></a>";
				$str_html.="</td>";
			}
			if ($td == "ret")
			{
				$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
				$str_html.=" ";
				$str_html.="</td>";
			}	
	
			$str_html.="</tr>";
		}
	
		// On affiche les pénalités s'il y en a et si on est en mode retour ou visualisation
		$i++;
		$penht=0;
		$pentva=0;
		$penttc=0;
		if (($td == "ret") OR ($td=="view"))
		{
			if (count($tab_pl)>0)
			{
				// Si "-1" on est ou sans pénalité, ou pénalité offerte
				if ($tab_pl[0]["codepen"]<>"-1")
				{
					// Calculs divers
					//... Montant pénalité
					$penht=$tab_pl[0]["valeur"];
					$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$tab_pl[0]["tva"]."'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
					if ($row2 = mysql_fetch_assoc($rs2))
						extract($row2);
					$txtva=$tva_tx/100;
					$penttc=round(($penht*(1+$txtva))*100)/100;
					$pentva=$penttc-$penht;
		
					if ($tab_pl[0]["valeur"]<>0) // On affiche que si pénalité non nulle
					{
						// Couleurs alternées
						if($i%2 == 0)  
						   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter1'\" class=\"TRalter1\">";
						else
						   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter2'\" class=\"TRalter2\">";
			
						// Désignation
						$str_html.="<td class=\"tdlst\" valign=\"top\">";
						$str_html.="Pénalité sur retard";
						$str_html.="</td>";

						// Quantité
						$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
						$str_html.="1.00";
						$str_html.="</td>";
						
						// Total HT
						$txt=format_num($penht,2);
						$txt.=" ".$lang['symbole_monnaie_txt'];
						$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
						$str_html.=$txt;
						$str_html.="</td>";
			
						// Mt TVA
						$txt=format_num($pentva,2);
						$txt.=" ".$lang['symbole_monnaie_txt'];
						$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
						$str_html.=$txt;
						$str_html.="</td>";
			
						// total TTC
						$txt=format_num($penttc,2);
						$txt.=" ".$lang['symbole_monnaie_txt'];
						$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
						$str_html.=$txt;
						$str_html.="</td>";
			
						// Icône de suppression de pénalité
						if ($td == "ret")
						{
							$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
							$str_html.="<a href=\"javascript:;\" onclick=\"fen_pen_rloc('del');\")>";
							$str_html.="<img src=\"../img/del.png\" border=\"0\" align=\"absmiddle\" title=\"Supprimer la pénalité\"></a>";
							$str_html.="</td>";
						}
						$str_html.="</tr>";
					}
				}
			}
		}
						
		// On affiche le montant des consommables s'il y en a et si on est en mode retour ou visualisation
		$i++;
		$consht=0;
		$constva=0;
		$consttc=0;
		if (($td == "ret") OR ($td=="view"))
		{
			if (count($tab_cl)>0)
			{
				// Calculs divers
				//... Montant pénalité
				$consht=$tab_cl[0]["valeur"];
				$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$tab_cl[0]["tva"]."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				if ($row2 = mysql_fetch_assoc($rs2))
					extract($row2);
				$txtva=$tva_tx/100;
				$consttc=round(($consht*(1+$txtva))*100)/100;
				$constva=$consttc-$consht;
	
				if ($tab_cl[0]["valeur"]<>0) // On affiche que si montant non nul
				{
					// Couleurs alternées
					if($i%2 == 0)  
					   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter1'\" class=\"TRalter1\">";
					else
					   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter2'\" class=\"TRalter2\">";
		
					// Désignation
					$str_html.="<td class=\"tdlst\" valign=\"top\">";
					$str_html.="Consommables en sus";
					$str_html.="</td>";

					// Quantité
					$str_html.="<td class=\"tdlst\" valign=\"top\">";
					$str_html.="1.00";
					$str_html.="</td>";
					
					// Total HT
					$txt=format_num($consht,2);
					$txt.=" ".$lang['symbole_monnaie_txt'];
					$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
					$str_html.=$txt;
					$str_html.="</td>";
		
					// Mt TVA
					$txt=format_num($constva,2);
					$txt.=" ".$lang['symbole_monnaie_txt'];
					$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
					$str_html.=$txt;
					$str_html.="</td>";
		
					// Total TTC
					$txt=format_num($consttc,2);
					$txt.=" ".$lang['symbole_monnaie_txt'];
					$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
					$str_html.=$txt;
					$str_html.="</td>";
		
					// Icône de suppression de consommables
					if ($td == "ret")
					{
						$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
						$str_html.="<a href=\"javascript:;\" onclick=\"fen_cons_rloc('del');\")>";
						$str_html.="<img src=\"../img/del.png\" border=\"0\" align=\"absmiddle\" title=\"Supprimer le montant consommables\"></a>";
						$str_html.="</td>";
					}
					$str_html.="</tr>";
				}
			}
		}
		
		mysql_close($conn);

		// Totaux généraux
		$i++;
		$str_html.="<tr>";

		// 1ère cellule
		$str_html.="<td class=\"annot_gras_it\" valign=\"top\" align=\"right\">";
		$str_html.="Total Général";
		$str_html.="</td>";

		// 2nde cellule
		$str_html.="<td class=\"annot_gras_it\" valign=\"top\" align=\"right\">";
		$str_html.=" ";
		$str_html.="</td>";

		// Total général HT
		$txt=format_num(($totbarht+$penht+$consht),2);
		$txt.=" ".$lang['symbole_monnaie_txt'];
		$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
		$str_html.=$txt;
		$str_html.="</td>";

		// Mt général TVA
		$txt=format_num(($totbartva+$pentva+$constva),2);
		$txt.=" ".$lang['symbole_monnaie_txt'];
		$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
		$str_html.=$txt;
		$str_html.="</td>";

		// total général TTC
		$txt=format_num(($totbarttc+$penttc+$consttc),2);
		$txt.=" ".$lang['symbole_monnaie_txt'];
		$str_html.="<td class=\"TDnumberD\" valign=\"top\" align=\"center\">";
		$str_html.=$txt;
		$str_html.="</td>";

		// 6ème cellule
		if (($td=="edit") OR ($td=="add") OR ($td=="ret"))
		{
			$str_html.="<td valign=\"top\" align=\"center\">";
			$str_html.=" ";
			$str_html.="</td>";
		}	
	
		// Fermeture de la table
		$str_html.="</table>";

		// MAJ de l'id
		echo "var str_html = '".addslashes($str_html)."';\n";
		echo "myid.innerHTML=str_html;\n";

		// Mises à jour boutons & liens
		// Lien ajout de barême
		echo "if (objet1) { \n";
		echo "objet1.style.display = 'none'; }\n";
		// Bouton d'enregistrement
		echo "if (butenr) { \n";
		echo "butenr.className = 'bton_std';\n";
		echo "butenr.value = '".$but_valider."';\n";
		echo "butenr.disabled = false; }\n";
	}
	else
	{
		// Mises à jour boutons & liens
		// Lien ajout de barême
		echo "if (objet1) { \n";
		echo "objet1.style.display = 'block'; }\n";
		// Bouton d'enregistrement
		echo "if (butenr) { \n";
		echo "butenr.className = 'bton_fmarr_dis';\n";
		echo "butenr.value = '".$but_choixprea2."';\n";
		echo "butenr.disabled = true; }\n";
	}
	// Bouton ajout de pénalité
	if (($td<>"view") AND ($td<>"del"))
	{
		echo "if (addpen) { \n";
		if ($tab_pl[0]["codepen"]<>"-1")
			echo "addpen.style.visibility = 'hidden'; }\n";
		else
		{
			$fr=0;
			if (trim($from)<>"") $fr=1;
			echo "verif_dateret('".$fr."',dater,'".$datemsql."'); }\n";
		}
		echo "if (addidx) { \n";
		if ($tab_cl[0]["valeur"]<>0)
			echo "addidx.style.visibility = 'hidden'; }\n";
		else
		{
			$fr=0;
			if (trim($from)<>"") $fr=1;
			echo "verif_indexes('".$fr."'); }\n";
		}
	}
	echo "}\n";
	
	echo "</script>\n";
}

// Création du code Javascript pour affichage des produits dans un contrat location
function cre_js_pdtincloc($td,$code,$from)
{
	global
		$datea,
		$lang,
		$lng_tjouri_abr,
		$lng_tduree,
		$non_oui,
		$but_valider,
		$but_choixprea,
		$txt_ent_lst_clic,
		$_SESSION;
		
	echo "<script language=\"javascript\">\n";
	
	echo "function pdtincloc() {\n";
	echo "var myid = ".$from."document.getElementById(\"pdtincloc\");\n";
	echo "var affsel = ".$from."document.getElementById('affsel');\n";
	echo "var chxdd = ".$from."document.getElementById('affchxdd');\n";
	echo "var chxdf = ".$from."document.getElementById('affchxdf');\n";
	echo "var butenr = ".$from."document.getElementById('enr');\n";
	echo "var butenri = ".$from."document.getElementById('enrimp');\n";
	echo "myid.innerHTML = '';\n";

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

	$rech = "SELECT * FROM ".TBPDTL." WHERE pdtl_codecoloc='".$code."' ORDER BY `pdtl_codepdtloc`";
	$rs = mysql_query($rech) or die(mysql_error());
		
	if (mysql_num_rows($rs)>0)
	{
		// Type de facturation
		$factTTC=TRUE;
		
		$rech2 = "SELECT coloc_codecli,coloc_typcli FROM ".TBCOLOC." WHERE coloc_code='".$code."'";
		$rs2 = mysql_query($rech2) or die(mysql_error());
			
		if ($row2 = mysql_fetch_assoc($rs2))
		{
			extract($row2);

			switch ($coloc_typcli)
			{
				case 0 :
					$rech3 = "SELECT cli_modfact FROM ".TBCLI." WHERE cli_code='".$coloc_codecli."'";
					$rs3 = mysql_query($rech3) or die(mysql_error());
						
					if ($row3 = mysql_fetch_assoc($rs3))
					{
						extract($row3);
						if (($cli_modfact=="0") OR ($cli_modfact=="2"))
							$factTTC=FALSE;
					}
					break;
				case 1 :
				case 3 :
					$factTTC=FALSE;
					break;
			}
		}
		
		// *** Affichage de la table
		$str_html = "<table class=\"Ltable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">";     

		// Création entête
		$str_html.="<th>";
		$str_html.="Désignation";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Du";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Au";
		$str_html.="</th>";
		
		if ($factTTC==TRUE)
			$txt= "TOTAL TTC";
		else
			$txt= "TOTAL HT";
		$str_html.="<th>";
		$str_html.=$txt;
		$str_html.="</th>";

		$str_html.="<th>";
		if ($td=="edit")
			$str_html.=$txt_ent_lst_clic;
		else
			$str_html.=" ";
		$str_html.="</th>";

		// Lignes du tableau
		$i = 0;
		
		while ($row = mysql_fetch_assoc($rs))
		{         
			extract($row);
			
			// Couleurs alternées
			if($i%2 == 0)  
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter1'\" class=\"TRalter1\">";
			else
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter2'\" class=\"TRalter2\">";

			// Désignation
			$rech2 = "SELECT pdtloc_des FROM ".TBPDTLOC." WHERE pdtloc_code='".$pdtl_codepdtloc."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);

			// Récupération enregistrement barême location du produit dans ce contrat
			$rech2 = "SELECT * FROM ".TBPDTLBL." WHERE pdtlbl_idpdtl='".$pdtl_id."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);
			if ($pdtlbl_idbarloc==0)
				$pdtloc_des.=" (prêt)";

			// Désignation
			$str_html.="<td class=\"tdlst\" valign=\"top\">";
			$str_html.=$pdtloc_des;

			if (formatedate($pdtl_datedep)<>"")
			{
				$str_html.=" ";
				$str_html.="<img src=\"../img/sorti.png\" border=\"0\" align=\"absmiddle\" title=\"Produit livré le ".formatedate($pdtl_datedep)."\"></a>";
			}
			if (formatedate($pdtl_dateret)<>"")
			{
				$str_html.=" ";
				$str_html.="<img src=\"../img/revenu.png\" border=\"0\" align=\"absmiddle\" title=\"Produit rendu le ".formatedate($pdtl_dateret)."\"></a>";
			}
			$str_html.="</td>";

			// Du
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			$str_html.=formatedate($pdtl_datedeb)." (".substr($pdtl_heuredeb,0,5).")";
			$str_html.="</td>";

			// Au
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			$str_html.=formatedate($pdtl_datefin)." (".substr($pdtl_heurefin,0,5).")";
			$str_html.="</td>";

			$qte=$pdtlbl_qte;

			// Calculs divers
			//... Montant location
			$pvht=$pdtlbl_pvht;
			$tothtloc=round(($qte*$pvht)*100)/100;
			$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$pdtlbl_codetva."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);
			$txtva=$tva_tx/100;
			$totttcloc=round(($tothtloc*(1+$txtva))*100)/100;
			$tottvaloc=$totttcloc-$tothtloc;
			//... Caution
			$pvht=$pdtlbl_caution;
			$tothtcaut=round($pvht*100)/100;
			$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$pdtlbl_tvacaut."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);
			$txtva=$tva_tx/100;
			$totttccaut=round(($tothtcaut*(1+$txtva))*100)/100;
			$tottvacaut=$totttccaut-$tothtcaut;
			//... Assurance
			$tothtass=0;
			$totttcass=0;
			$tottvaass=0;
			if ($pdtlbl_avecass==1)
			{
				$pvht=$pdtlbl_mtass;
				$tothtass=round(($qte*$pvht)*100)/100;
				$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$pdtlbl_codetvaass."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				if ($row2 = mysql_fetch_assoc($rs2))
					extract($row2);
				$txtva=$tva_tx/100;
				$totttcass=round(($tothtass*(1+$txtva))*100)/100;
				$tottvaass=$totttcass-$tothtass;
			}
			//...Consommables
			$rech2 = "SELECT * FROM ".TBTVA." WHERE tva_code='".$pdtlbl_codetva."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_assoc($rs2))
				extract($row2);
			$txtva=$tva_tx/100;
			//... Consommable 1
			$pvht=$pdtlbl_puconsi1;
			$tothtcons1=round(($qte*$pvht)*100)/100;
			$totttccons1=round(($tothtcons1*(1+$txtva))*100)/100;
			$tottvacons1=$totttccons1-$tothtcons1;
			//... Consommable 2
			$pvht=$pdtlbl_puconsi2;
			$tothtcons2=round(($qte*$pvht)*100)/100;
			$totttccons2=round(($tothtcons2*(1+$txtva))*100)/100;
			$tottvacons2=$totttccons2-$tothtcons2;

			// Total
			if ($factTTC==TRUE)
				$txt=format_num(($totttcloc+$totttccaut+$totttcass+$totttccons1+$totttccons2),2);
			else
				$txt=format_num(($tothtloc+$tothtcaut+$tothtass+$tothtcons1+$tothtcons2),2);
			$txt.=" ".$lang['symbole_monnaie_txt'];
			$str_html.="<td class=\"TDnumberD\" valign=\"top\">";
			$str_html.=$txt;
			$str_html.="</td>";

			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			if ($td == "edit")
			{
				if ((formatedate($pdtl_datedep)=="") AND (formatedate($pdtl_dateret)==""))
				{
					$urleditpdtl = "pdtl_maj.php?td=edit&cl=".$pdtl_codecoloc."&ca=".$pdtl_codepdtloc;
					$urleditpdtl.= "&dd=".formatedate($pdtl_datedeb)."&hd=".substr($pdtl_heuredeb,0,5);
					$urleditpdtl.= "&df=".formatedate($pdtl_datefin)."&hf=".substr($pdtl_heurefin,0,5);
					$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urleditpdtl."',900,700,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');\")>";
					$str_html.="<img src=\"../img/edit.png\" border=\"0\" align=\"absmiddle\" title=\"Modifier le produit à louer\"></a>";
					$str_html.=" ";
				}

				if (formatedate($pdtl_datedep)=="")
				{
					$urldeppdtl = "pdtl_maj.php?td=dep&cl=".$pdtl_codecoloc."&ca=".$pdtl_codepdtloc;
					$urldeppdtl.= "&dd=".formatedate($pdtl_datedeb)."&hd=".substr($pdtl_heuredeb,0,5);
					$urldeppdtl.= "&df=".formatedate($pdtl_datefin)."&hf=".substr($pdtl_heurefin,0,5);
					$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urldeppdtl."',900,700,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');\")>";
					$str_html.="<img src=\"../img/depart.png\" border=\"0\" align=\"absmiddle\" title=\"Livraison du produit\"></a>";
					$str_html.=" ";
				}

				if ((formatedate($pdtl_datedep)<>"") AND (formatedate($pdtl_dateret)==""))
				{
					$urlretpdtl = "pdtl_maj.php?td=ret&cl=".$pdtl_codecoloc."&ca=".$pdtl_codepdtloc;
					$urlretpdtl.= "&dd=".formatedate($pdtl_datedeb)."&hd=".substr($pdtl_heuredeb,0,5);
					$urlretpdtl.= "&df=".formatedate($pdtl_datefin)."&hf=".substr($pdtl_heurefin,0,5);
					$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urlretpdtl."',900,700,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');\")>";
					$str_html.="<img src=\"../img/retour.png\" border=\"0\" align=\"absmiddle\" title=\"Retour du produit\"></a>";
					$str_html.=" ";
				}

				if ((formatedate($pdtl_datedep)<>"") AND (formatedate($pdtl_dateret)<>""))
				{
					$urldetpdtl = "pdtl_maj.php?td=view&cl=".$pdtl_codecoloc."&ca=".$pdtl_codepdtloc;
					$urldetpdtl.= "&dd=".formatedate($pdtl_datedeb)."&hd=".substr($pdtl_heuredeb,0,5);
					$urldetpdtl.= "&df=".formatedate($pdtl_datefin)."&hf=".substr($pdtl_heurefin,0,5);
					$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urldetpdtl."',900,700,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');\")>";
					$str_html.="<img src=\"../img/eye.png\" border=\"0\" align=\"absmiddle\" title=\"Détail de la location\"></a>";
					$str_html.=" ";
				}

				if ($_SESSION["niv_log"]<>1)
				{
					$urlprtart = "pdtldr_pdf.php?cl=".$pdtl_codecoloc."&ca=".$pdtl_codepdtloc;
					$str_html.="<a href=\"javascript:;\" onclick=\"lanceimp('".$urlprtart."');\")>";
					$str_html.="<img src=\"../img/print.png\" border=\"0\" align=\"absmiddle\" title=\"Imprimer fiche départ/retour\"></a>";
					$str_html.=" ";
				}

				if ((formatedate($pdtl_datedep)=="") AND (formatedate($pdtl_dateret)==""))
				{
					$urldelpdtl = "pdtl_maj.php?td=del&cl=".$pdtl_codecoloc."&ca=".$pdtl_codepdtloc;
					$urldelpdtl.= "&dd=".formatedate($pdtl_datedeb)."&hd=".substr($pdtl_heuredeb,0,5);
					$urldelpdtl.= "&df=".formatedate($pdtl_datefin)."&hf=".substr($pdtl_heurefin,0,5);
					$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urldelpdtl."',900,700,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');\")>";
					$str_html.="<img src=\"../img/del.png\" border=\"0\" align=\"absmiddle\" title=\"Supprimer le produit à louer\"></a>";
				}
			}
			else
			{
				$urldetpdtl = "pdtl_maj.php?td=view&cl=".$pdtl_codecoloc."&ca=".$pdtl_codepdtloc;
				$urldetpdtl.= "&dd=".formatedate($pdtl_datedeb)."&hd=".substr($pdtl_heuredeb,0,5);
				$urldetpdtl.= "&df=".formatedate($pdtl_datefin)."&hf=".substr($pdtl_heurefin,0,5);
				$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urldetpdtl."',900,700,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');\")>";
				$str_html.="<img src=\"../img/eye.png\" border=\"0\" align=\"absmiddle\" title=\"Détail de la location\"></a>";
				$str_html.=" ";

				if ($_SESSION["niv_log"]<>1)
				{
					$urlprtart = "pdtldr_pdf.php?cl=".$pdtl_codecoloc."&ca=".$pdtl_codepdtloc;
					$str_html.="<a href=\"javascript:;\" onclick=\"lanceimp('".$urlprtart."');\")>";
					$str_html.="<img src=\"../img/print.png\" border=\"0\" align=\"absmiddle\" title=\"Imprimer fiche départ/retour\"></a>";
				}
			}
			$str_html.="</td>";
	
			$str_html.="</tr>";
			$i++;
		}
	
		mysql_close($conn);

		// Fermeture de la table
		$str_html.="</table>";
		
		// MAJ de l'id
		echo "var str_html = '".addslashes($str_html)."';\n";
		echo "myid.innerHTML=str_html;\n";

		// MAJ boutons
		echo "if (affsel) { \n";
		echo "affsel.style.visibility = 'visible'; }\n ";
		echo "if (chxdd) { \n";
		echo "chxdd.style.visibility = 'hidden'; }\n ";
		echo "if (chxdf) { \n";
		echo "chxdf.style.visibility = 'hidden'; }\n ";

		echo "if (butenr) { \n";
		echo "butenr.className = 'bton_std';\n";
		echo "butenr.value = '".$but_valider."';\n";
		echo "butenr.disabled = false; }\n";

		echo "if (butenri) { \n";
		echo "butenri.className = 'bton_std';\n";
		echo "butenri.disabled = false; }\n";

	}
	else
	{
		if ($td<>"add")
		{
			echo "if (affsel) { \n";
			echo "affsel.style.visibility = 'hidden'; }\n ";
			echo "if (chxdd) { \n";
			echo "chxdd.style.visibility = 'visible'; }\n ";
			echo "if (chxdf) { \n";
			echo "chxdf.style.visibility = 'visible'; }\n ";
	
			echo "if (butenr) { \n";
			echo "butenr.className = '".bton_fmarr_dis."';\n";
			echo "butenr.value = '".$but_choixprea."';\n";
			echo "butenr.disabled = true; }\n";
			echo "if (butenri) { \n";
			echo "butenri.className = 'bton_fmarr_dis';\n";
			echo "butenri.disabled = true; }\n";
		}
	}
	
	echo "}\n";
	
	echo "</script>\n";
}

/*** Barêmes **/

//  Donne le nombre de barêmes dans une fiche article à louer
function nb_barloc_in_pdtloc($codep)
{
	$nbbarloc=0;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBBARLOC. "` WHERE `barloc_codepdtloc` = '$codep'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbbarloc = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $nbbarloc;
}

//  Donne le nombre de tarifs consommables dans un barême donné et un numéro de consommable donné
function nb_tarcons_in_barloc($idbl,$ncons)
{
	$nbtarcons=0;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBBARLOCTC. "` WHERE `barloctc_idbarloc` = '$idbl' AND `barloctc_ncons` = '$ncons'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbtarcons = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $nbtarcons;
}

//  Donne le dernier id d'un tarif consommable dans un barême donné et un numéro de consommable donné
function last_id_tarcons_in_barloc($idbl,$ncons,$open)
{
	$idtc=0;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT barloctc_id FROM `" .TBBARLOCTC. "` WHERE `barloctc_idbarloc` = '$idbl' AND `barloctc_ncons` = '$ncons' ORDER BY `barloctc_min` DESC";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
	{
		if ($row2 = mysql_fetch_array($rs2))
		{
			extract($row2);
			$idtc=$barloctc_id;
		}
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $idtc;
}

//  Vérifie si un tarif consommable possède un maximum "infini", pour un numéro de consommable donné
function exist_infini_tcons_id($idbl,$ncons,$open)
{
	$exist=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT barloctc_id FROM `" .TBBARLOCTC. "` WHERE `barloctc_idbarloc` = '".$idbl."' AND `barloctc_max` = '0' AND `barloctc_ncons` = '$ncons'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$exist=TRUE;
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $exist;
}

// Lecture d'un enregistrement barême, par son id
function lit_enr_barloc_id($id,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBBARLOC. "` WHERE `barloc_id` = '$id'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $row2;
}

// Lecture d'un enregistrement tarif consommables, par son id
function lit_enr_tcons_id($id)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBBARLOCTC. "` WHERE `barloctc_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;
}

// Lecture d'un enregistrement barême, pour un barême et un produit donnés
function lit_enr_barloc_in_pdtloc($codepl,$codep)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBBARLOC. "` WHERE `barloc_codeperloc` = '$codepl' AND `barloc_codepdtloc` = '$codep'";
	$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 barême location dans une fiche location
function exist_barloc_in_coloc($idbl)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM ".TBPDTLBL;
	$rech2.= " WHERE ".TBPDTLBL.".pdtlbl_idbarloc = '".$idbl."'"; 
	$rs2 = mysql_query($rech2) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist = TRUE;
	else
		$exist = FALSE;
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);

	return $exist;
}

// Vérification de la présence d'un barême location dans une fiche location non close
function exist_barloc_in_coloc_ouv($idbl,$open)
{
	$exist=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}

	$rech2 = "SELECT ".TBPDTL.".pdtl_codecoloc FROM ".TBPDTL.", ".TBPDTLBL;
	$rech2.= " WHERE ".TBPDTLBL.".pdtlbl_idbarloc = '".$idbl."'"; 
	$rech2.= " AND ".TBPDTLBL.".pdtlbl_idpdtl = ".TBPDTL.".pdtl_id"; 
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		while ($row2 = mysql_fetch_assoc($rs2))
		{
			extract($row2);
			$rech3 = "SELECT ".TBCOLOC.".coloc_code FROM ".TBCOLOC;
			$rech3.= " WHERE ".TBCOLOC.".coloc_code = '".$pdtl_codecoloc."'"; 
			$rech3.= " AND ".TBCOLOC.".coloc_datefin = '0000-00-00'"; 
			$rs3 = mysql_query($rech3)or die(mysql_error());
			if (mysql_num_rows($rs3) <> 0)
			{
				$exist=TRUE;
				break;
			}
			mysql_free_result($rs3);
		}
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $exist;
}

// Retourne le prochain minimum à appliquer sur un tarif consommables
function next_min_tcons($idbl,$qconsi,$ncons,$open)
{
	$nextmin=$qconsi+1;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}

	$rech2 = "SELECT MAX(".TBBARLOCTC.".barloctc_max) AS maximum FROM ".TBBARLOCTC;
	$rech2.= " WHERE ".TBBARLOCTC.".barloctc_idbarloc = '".$idbl."' AND ".TBBARLOCTC.".barloctc_ncons = '".$ncons."'"; 
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		if ($row2 = mysql_fetch_assoc($rs2))
		{
			extract($row2);
			if ($maximum<>NULL)
				$nextmin=$maximum+1;
		}
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
		
	return $nextmin;
}

// Retourne la quantié de secondes couvertes par un barême pour un produit en location
function nbsc_in_barloc_barloc($idbl,$open)
{
	$nbs=0;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT barloc_codeperloc FROM ".TBBARLOC;
	$rech2.= " WHERE ".TBBARLOC.".barloc_id = '".$idbl."'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		if ($row2 = mysql_fetch_assoc($rs2))
		{
			extract($row2);
			// On récupère les valeurs nombre et secondes de la périodicité
			$rech3 = "SELECT perloc_nduree,perloc_tduree FROM ".TBPERLOC;
			$rech3.= " WHERE ".TBPERLOC.".perloc_code = '".$barloc_codeperloc."'";
			$rs3 = mysql_query($rech3)or die(mysql_error());
			if (mysql_num_rows($rs3) <> 0)
			{
				if ($row3 = mysql_fetch_assoc($rs3))
				{
					extract($row3);
					$nbs=$perloc_nduree*$perloc_tduree;
				}
			}
			mysql_free_result($rs3);
		}
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
		
	return $nbs;
}

// Retourne la quantié de secondes déjà couvertes par des barêmes pour un produit en location
// Dans un contrat de location donné
function nbs_barloc_in_pdtloc($codecl,$codep,$open)
{
	$nbs=0;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	// On récupère l'ID du produit dans le contrat
	$rech2 = "SELECT pdtl_id FROM ".TBPDTL;
	$rech2.= " WHERE ".TBPDTL.".pdtl_codecoloc = '".$codecl."' AND ".TBPDTL.".pdtl_codepdtloc = '".$codep."'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		if ($row2 = mysql_fetch_assoc($rs2))
		{
			extract($row2);
			/** On récupère le total secondes couvertes **/

			// On récupère l'ID du barême de location
			$rech3 = "SELECT pdtlbl_idbarloc FROM ".TBPDTLBL;
			$rech3.= " WHERE ".TBPDTLBL.".pdtlbl_idpdtl = '".$pdtl_id."'";
			$rs3 = mysql_query($rech3)or die(mysql_error());
			if (mysql_num_rows($rs3) <> 0)
			{
				while ($row3 = mysql_fetch_assoc($rs3))
				{
					extract($row3);
					// On récupère le code périodicité du barême de location
					$rech4 = "SELECT barloc_codeperloc FROM ".TBBARLOC;
					$rech4.= " WHERE ".TBBARLOC.".barloc_id = '".$pdtl_idbarloc."'";
					$rs4 = mysql_query($rech4)or die(mysql_error());
					if (mysql_num_rows($rs4) <> 0)
					{
						if ($row4 = mysql_fetch_assoc($rs4))
						{
							extract($row4);
							// On récupère les valeurs nombre et secondes de la périodicité
							$rech5 = "SELECT perloc_nduree,perloc_tduree FROM ".TBPERLOC;
							$rech5.= " WHERE ".TBPERLOC.".perloc_code = '".$barloc_codeperloc."'";
							$rs5 = mysql_query($rech5)or die(mysql_error());
							if (mysql_num_rows($rs5) <> 0)
							{
								if ($row5 = mysql_fetch_assoc($rs5))
								{
									extract($row5);
									$nbs=$nbs+($pdtlbl_qte*$perloc_nduree*$perloc_tduree);
								}
							}
							mysql_free_result($rs5);
						}
					}
					mysql_free_result($rs4);
				}
			}
			mysql_free_result($rs3);
		}
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
		
	return $nbs;
}

/** Produits à louer : réparations */

// Nombre de réparations
function nb_pdtlocr($codepdtloc)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBPDTLOCR."`";
	if ($codepdtloc<>"")
		$rexist.= " WHERE  `pdtlocr_codepdtloc` = '" .$codepdtloc."'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Prix de revient des réparations pour un produit de location donné
function px_revient_pdtlocr($codepdtloc)
{
	$cout_rep=0;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBPDTLOCR."`";
	$rexist.= " WHERE  `pdtlocr_codepdtloc` = '" .$codepdtloc."'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
	{
		while ($row2 = mysql_fetch_assoc($rs2))
		{
			extract($row2);
			// On récupère le prix de revient du technicien en fonction de la date
			$rech3 = "SELECT tecpr_couth FROM `" .TBTECPR. "` WHERE `tecpr_date` <= '$pdtlocr_date' AND `tecpr_codetec` = '".$pdtlocr_codetec."' ORDER BY tecpr_date DESC";
			$rs3 = mysql_query($rech3)or die(mysql_error());
			
			if (mysql_num_rows($rs3)>0)
			{
				if ($row3 = mysql_fetch_array($rs3))
					extract($row3);
				$cout_rep=$cout_rep+(round((($tecpr_couth/3600)*$pdtlocr_durees)*100)/100);
			}
			
			$cout_rep=$cout_rep+$pdtlocr_mtp;
			mysql_free_result($rs3);
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	return $cout_rep;
}

// Lecture d'une réparation
function lit_enr_pdtlocr_id_in_pdtloc($id,$code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBPDTLOCR. "` WHERE `pdtlocr_id` = '$id' AND `pdtlocr_codepdtloc` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $row2;
}


?>