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

/** Contrats **/

// Vérification du nombre de contrats gérés par une agence
function nb_cocl($agence)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if ($agence <> "")
		$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codeage` = '".$agence."'";
	else
		$rexist = "SELECT * FROM `" .TBCOCL. "`";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérification du nombre de contrats pour un client
function nb_cocl_in_cli($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codecli` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérification du nombre de contrats pour un client
function nb_cocl_actifs_in_cli($code,$open)
{
	global
		$datea;
	
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codecli` = '$code'";
	$rexist .= " AND cocl_datesig<>'0000-00-00' AND cocl_dated<='".$datea."' AND (cocl_datef>='".$datea."' OR cocl_datef='0000-00-00') AND cocl_datefin='0000-00-00'";
	$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érification du nombre de contrats clients dans l'historique contrats
function nb_cocl_in_coclh($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCLH. "` WHERE `coclh_codecocl` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérification qu'un contrat contrat-client est signé
function cocl_signed($code)
{
	$signe = FALSE;

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

	return $signe;
}

// Vérification qu'un contrat est de type "quota d'heure"
function cocl_quota($code)
{
	$quota = FALSE;

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

	return $quota;
}

// Crée une chaîne qui contient la liste des sites rattachés à un contrat
function cree_str_lstsites_cocl($co,$open)
{
	// Construction de la chaîne des sites sélectionnés
	$lstsites = "";
	
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech = "SELECT scli_id FROM ".TBSCLI.",".TBSCLIC." WHERE ".TBSCLIC.".sclic_codecocl = '".$co."' AND ".TBSCLI.".scli_id = ".TBSCLIC.".sclic_idscli";
	$rs = mysql_query($rech) or die(mysql_error());
	while ($row = mysql_fetch_assoc($rs)) 
	{
		extract($row);
		$lstsites .= $scli_id."-";
	}
	mysql_free_result($rs);
	if ($open==TRUE)
		mysql_close($conn);
	
	if ($lstsites!="")
		$lstsites = substr($lstsites,0,strlen($lstsites)-1);
	
	return $lstsites;
}

// Crée le texte des matériels (contrats) sur la base de la string des matériel/licences choisis
function cree_txt_matos_cocl($lstrlp)
{
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$txtmatos="";
	$tab_lstrlp=explode("-",$lstrlp);
	
	if (count($tab_lstrlp>0))
	{
		// Injection des matériels
		$j=1;
		$titregen=TRUE;
		$titre="=== DETAIL DU MATERIEL CONCERNE ===%%%%";
		for($i=0; $i<count($tab_lstrlp); $i++)
		{
			$rech = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_code`= '".$tab_lstrlp[$i]."' ORDER BY `rpdt_idsite`";
			$rs = mysql_query($rech) or die(mysql_error());
			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 ($titregen==TRUE)
					{
						$txtmatos_titre =$titre."---- MATERIEL N°".sprintf("%'010s",$j)." ----";
						$titregen=FALSE;
					}
					else
						$txtmatos_titre ="---- MATERIEL N°".sprintf("%'010s",$j)." ----";
					$nom_site="";
					switch ($rpdt_idsite)
					{
						case -1 :
							$nom_site="Tous les sites";
							break;
						case 0 :
							$nom_site="Site principal";
							break;
						default :
							$rech2 = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '".$rpdt_idsite."'";
							$rs2 = mysql_query($rech2)or die(mysql_error());
							if ($row2 = mysql_fetch_array($rs2))
							{
								extract($row2);
								$nom_site.= $scli_ste;
								$nom_site.= " (".Retournecp_openId($scli_ville,FALSE)." ".Retourneville_openId($scli_ville,FALSE).")";
							}
							mysql_free_result($rs2);
					}
					$rech2 = "SELECT * FROM ".TBRPCAT." WHERE `rpcat_code` = '".$rpdt_codecat."'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
					if ($row2 = mysql_fetch_array($rs2))
					{
						extract ($row2);
					}
					$rech2 = "SELECT * FROM ".TBMQUE." WHERE `mque_code` = '".$rpdt_codema."'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
					if ($row2 = mysql_fetch_array($rs2))
					{
						extract ($row2);
					}
					$saut = chr(13).chr(10);
					$txtmatos_corps1 = "Site : ".$nom_site."%%Relevé ".$rpdt_code." (".$rpdt_codep.") : ".$rpcat_desc." / ".$mque_ste." ".$rpdt_modele.", NS N°".$rpdt_ns.", situé ".$rpdt_empl.".";
					$txtmatos_corps1 = ereg_replace($saut,"%%",htmlspecialchars_decode($txtmatos_corps1));
					mysql_free_result($rs2);

					$txtmatos_pied = "---- FIN DETAIL MATERIEL N°".sprintf("%'010s",$j)." ----";
					$txtmatos.=$txtmatos_titre."%%".$txtmatos_corps1."%%".$txtmatos_pied."%%%%";
				}
				$j++;
			}
			mysql_free_result($rs);
		}
		// Injection des licences
		$j=1;
		$titregen=TRUE;
		$titre="%%=== DETAIL DES LICENCES CONCERNEES ===%%%%";
		for($i=0; $i<count($tab_lstrlp); $i++)
		{
			$rech = "SELECT * FROM `" .TBRLIC. "` WHERE `rlic_code`= '".$tab_lstrlp[$i]."'  ORDER BY `rlic_idsite`";
			$rs = mysql_query($rech) or die(mysql_error());
			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 ($titregen==TRUE)
					{
						$txtmatos_titre =$titre."---- LICENCE N°".sprintf("%'010s",$j)." ----";
						$titregen=FALSE;
					}
					else
						$txtmatos_titre ="---- LICENCE N°".sprintf("%'010s",$j)." ----";
					$nom_site="";
					switch ($rlic_idsite)
					{
						case -1 :
							$nom_site="Tous les sites";
							break;
						case 0 :
							$nom_site="Site principal";
							break;
						default :
							$rech2 = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '".$rpdt_idsite."'";
							$rs2 = mysql_query($rech2)or die(mysql_error());
							if ($row2 = mysql_fetch_array($rs2))
							{
								extract($row2);
								$nom_site.= $scli_ste;
								$nom_site.= " (".Retournecp_openId($scli_ville,FALSE)." ".Retourneville_openId($scli_ville,FALSE).")";
							}
							mysql_free_result($rs2);
					}
					$rech2 = "SELECT * FROM ".TBMQUE." WHERE `mque_code` = '".$rlic_codema."'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
					if ($row2 = mysql_fetch_array($rs2))
					{
						extract ($row2);
					}
					$saut = chr(13).chr(10);
					$txtmatos_corps1 = "Site : ".$nom_site."%%Relevé ".$rlic_code." : ".$mque_ste." ".$rlic_modele." ".$rlic_version.".";
					$txtmatos_corps1 = ereg_replace($saut,"%%",htmlspecialchars_decode($txtmatos_corps1));
					mysql_free_result($rs2);

					$txtmatos_pied = "---- FIN DETAIL LICENCE N°".sprintf("%'010s",$j)." ----";
					$txtmatos.=$txtmatos_titre."%%".$txtmatos_corps1."%%".$txtmatos_pied."%%%%";
				}
				$j++;
			}
			mysql_free_result($rs);
		}
	}
	mysql_close($conn);
	
	if ($txtmatos!="")
		$txtmatos = substr($txtmatos,0,strlen($txtmatos)-4);
	return $txtmatos;
}

// Lecture d'un enregistrement contrat client
function lit_enr_cocl($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_code` = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

/** Préavis **/

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

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

// Récupération du temps travaillé sur un contrat donné
// pour une intervention données
function tps_trav_int($contrat,$inter,$open)
{
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	// Calcul des cumuls
	// Récupération du total travaillé
	$nbtotalsec = 0;

	$rech2 = "SELECT ".TBINTH.".inth_heurea, ".TBINTH.".inth_heured , ".TBINTH.".inth_codeind ";
	$rech2.= "FROM ".TBINT.", ".TBINTH;
	$rech2.= " WHERE ".TBINT.".int_codecocl = '".$contrat."'";
	$rech2.= " AND ".TBINT.".int_code = ".TBINTH.".inth_codeint";
	$rech2.= " AND ".TBINTH.".inth_offert = 0 AND ".TBINTH.".inth_codeint = '".$inter."'";

	$rs2 = mysql_query($rech2) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		while ($row2 = mysql_fetch_assoc($rs2)) 
		{         
			$ind_coeff=1;
			if ($row2["inth_codeind"]<>"-1")
			{
				$row3=lit_enr_ind($row2["inth_codeind"],FALSE);
				if (is_array($row3))
					extract($row3);
			}
			$nbtotalsec = $nbtotalsec + (diff_time_sec($row2["inth_heurea"],$row2["inth_heured"])*$ind_coeff);
		}
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn);
	
	return $nbtotalsec;
}

// Récupération du temps travaillé sur un contrat donné
// pour des interventions closes
function tps_trav_clot($contrat,$open)
{
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	// Calcul des cumuls
	// Récupération du total travaillé
	$nbtotalsec = 0;

	$rech2 = "SELECT ".TBINTH.".inth_heurea, ".TBINTH.".inth_heured , ".TBINTH.".inth_codeind ";
	$rech2.= "FROM ".TBINT.", ".TBINTH;
	$rech2.= " WHERE ".TBINT.".int_codecocl = '".$contrat."'";
	$rech2.= " AND ".TBINT.".int_code = ".TBINTH.".inth_codeint";
	$rech2.= " AND ".TBINT.".int_datefin <> '0000-00-00'";
	$rech2.= " AND ".TBINTH.".inth_offert = 0";

	$rs2 = mysql_query($rech2) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		while ($row2 = mysql_fetch_assoc($rs2)) 
		{         
			$ind_coeff=1;
			if ($row2["inth_codeind"]<>"-1")
			{
				$row3=lit_enr_ind($row2["inth_codeind"],FALSE);
				if (is_array($row3))
					extract($row3);
			}
			$nbtotalsec = $nbtotalsec + (diff_time_sec($row2["inth_heurea"],$row2["inth_heured"])*$ind_coeff);
		}
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn);
	
	return $nbtotalsec;
}

// Récupération du temps travaillé sur un contrat donné
// pour des interventions non closes
function tps_trav_non_clot($contrat,$open)
{
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	// Calcul des cumuls
	// Récupération du total travaillé
	$nbtotalsec = 0;

	$rech2 = "SELECT ".TBINTH.".inth_heurea, ".TBINTH.".inth_heured , ".TBINTH.".inth_codeind ";
	$rech2.= "FROM ".TBINT.", ".TBINTH;
	$rech2.= " WHERE ".TBINT.".int_codecocl = '".$contrat."'";
	$rech2.= " AND ".TBINT.".int_code = ".TBINTH.".inth_codeint";
	$rech2.= " AND ".TBINT.".int_datefin = '0000-00-00'";
	$rech2.= " AND ".TBINTH.".inth_offert = 0";
	$rs2 = mysql_query($rech2) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		while ($row2 = mysql_fetch_assoc($rs2)) 
		{         
			$ind_coeff=1;
			if ($row2["inth_codeind"]<>"-1")
			{
				$row3=lit_enr_ind($row2["inth_codeind"],FALSE);
				if (is_array($row3))
					extract($row3);
			}
			$nbtotalsec = $nbtotalsec + (diff_time_sec($row2["inth_heurea"],$row2["inth_heured"])*$ind_coeff);
		}
	}

	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn);
	return $nbtotalsec;
}

// Récupération du total reporté vers un contrat
function apports_vers_contrat($contrat,$open)
{
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$reports = 0;

	// Solde des reports
	$rech2 = "SELECT SUM(coclr_nbsrep) AS total_report";
	$rech2.= " FROM ".TBCOCLR;
	$rech2.= " WHERE coclr_cocldest = '".$contrat."'";
	$rs2 = mysql_query($rech2) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		if ($row2 = mysql_fetch_assoc($rs2)) 
		{
			extract($row2);
			$reports=$total_report;
		}
	}
	mysql_free_result($rs2);

	if ($open==TRUE)
		mysql_close($conn);

	return $reports;
}

// Récupération du total sorti en report depuis un contrat
// On ne prend que les montants positifs, qui correspondent à des sorties
function reports_depuis_contrat($contrat,$open)
{
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$reports = 0;

	// Solde des reports
	$rech2 = "SELECT SUM(coclr_nbsrep) AS total_report";
	$rech2.= " FROM ".TBCOCLR;
	$rech2.= " WHERE coclr_coclori = '".$contrat."'";
	$rech2.= " AND coclr_nbsrep > 0";

	$rs2 = mysql_query($rech2) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		if ($row2 = mysql_fetch_assoc($rs2)) 
		{
			extract($row2);
			$reports=$total_report;
		}
	}
	mysql_free_result($rs2);

	if ($open==TRUE)
		mysql_close($conn);

	return $reports;
}

// Récupération du solde en secondes d'un contrat donné
// Sans les interventions en cours
function soldes_contrat($contrat,$open)
{
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$soldes = 0;
	// Montant initial du contrat
	$rech2 = "SELECT cocl_nbs";
	$rech2.= " FROM ".TBCOCL;
	$rech2.= " WHERE cocl_code = '".$contrat."'";
	$rs2 = mysql_query($rech2) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		if ($row2 = mysql_fetch_assoc($rs2)) 
		{
			extract($row2);
			$soldes=$cocl_nbs;
		}
	}
	mysql_free_result($rs2);
	
	// On retire les temps travaillés clots et non clots
	$soldes=$soldes-tps_trav_clot($contrat,FALSE);
	// On rajoute les apports et on soustrait les reports
	$soldes=$soldes+apports_vers_contrat($contrat,FALSE)-reports_depuis_contrat($contrat,FALSE);

	if ($open==TRUE)
		mysql_close($conn);

	return $soldes;
}

// Récupération du solde réel en secondes d'un contrat donné
// (réel : solde complet, y compris interventions en cours)
function soldes_reel_contrat($contrat,$open)
{
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$soldes = 0;

	// Montant initial du contrat
	$rech2 = "SELECT cocl_nbs";
	$rech2.= " FROM ".TBCOCL;
	$rech2.= " WHERE cocl_code = '".$contrat."'";
	$rs2 = mysql_query($rech2) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
	{
		if ($row2 = mysql_fetch_assoc($rs2)) 
		{
			extract($row2);
			$soldes=$cocl_nbs;
		}
	}
	mysql_free_result($rs2);
	// On retire les temps travaillés clots et non clots
	$soldes=$soldes-tps_trav_clot($contrat,FALSE)-tps_trav_non_clot($contrat,FALSE);
	
	// On rajoute les apports et on soustrait les reports
	$soldes=$soldes+apports_vers_contrat($contrat,FALSE)-reports_depuis_contrat($contrat,FALSE);

	if ($open==TRUE)
	{
		mysql_close($conn);
	}

	return $soldes;
}

// Calcule le prix de revient d'un contrat
function px_revient_cocl($contrat,$open)
{
	$pr=0;
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	
	// Récupération prix de revient pour le travail effectué (inclus offert)
	$rech = "SELECT coclh_codeint FROM `" .TBCOCLH. "` WHERE `coclh_codecocl`= '".$contrat."'";
	$rs = mysql_query($rech) or die(mysql_error());
	if (mysql_num_rows($rs) <> 0)
	{
		while ($row = mysql_fetch_assoc($rs)) 
		{
			extract($row);    
			// Récupération prix de revient pour le travail effectué (inclus offert)
			$rech2 = "SELECT * FROM `" .TBINTH. "` WHERE `inth_codeint`= '".$coclh_codeint."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if (mysql_num_rows($rs2) <> 0)
			{
				while ($row2 = mysql_fetch_assoc($rs2)) 
				{
					extract($row2);    
					// Calcul du coût de déplacement
					// On récupère le prix de revient du technicien en fonction de la date
					$rech3 = "SELECT tecpr_couth FROM `" .TBTECPR. "` WHERE `tecpr_date` <= '$inth_date' AND `tecpr_codetec` = '".$inth_codetec."' ORDER BY tecpr_date DESC";
					$rs3 = mysql_query($rech3)or die(mysql_error());
					if ($row3 = mysql_fetch_array($rs3))
					{
						extract($row3);
						$cout_site=0;
						$tps = diff_time_sec($inth_heurea,$inth_heured); // Temps passé sur site
						$cout_aller=0;
						$tpsa = diff_time_sec($inth_aheured,$inth_aheuref); // Temps pour aller
						$cout_retour=0;
						$tpsr = diff_time_sec($inth_rheured,$inth_rheuref); // Temps pour retour
						// ... Aller
						$rech4 = "SELECT * FROM `" .TBMT. "` WHERE `mt_code` = '$inth_acodemt'";
						$rs4 = mysql_query($rech4)or die(mysql_error());
						if ($row4 = mysql_fetch_array($rs4))
						{
							extract($row4);
							if ($mt_coutk>0) // C'est un coût interne
								$cout_aller=$inth_akm*$mt_coutk; // Montant "kilométrique" aller
							else // C'est sous-traité
							{
								// Coût forfaitaire
								if ($mt_coutas==0) // A saisir ?
									$cout_aller=$mt_coutf; // Oui : Montant forfaitaire pour l'aller
								else
									$cout_aller=$inth_acd; // Non : Montant saisi pour l'aller
								// Coût kilométrique
								if ($mt_kmill==0) // Pas illimité : on calcule (sinon on ne fait rien)
								{
									$diff_km=$inth_akm-$mt_kmi;
									if ($diff_km>0)
										$cout_aller=$cout_aller+($mt_kms*$diff_km);
								}
							}
							$cout_aller=$cout_aller+$mt_coutc+$inth_afrais; // Rajout des frais supplémentaires
							
							if ($mt_tpsdi==0) // Le temps de déplacement n'est pas inclus : on calcul (sinon on ne fait rien)
								$cout_aller=$cout_aller+round((($tecpr_couth/3600)*$tpsa)*100)/100;
						}
						// ...Sur site
						$cout_site=round((($tecpr_couth/3600)*$tps)*100)/100;
						// ... Retour
						$rech4 = "SELECT * FROM `" .TBMT. "` WHERE `mt_code` = '$inth_rcodemt'";
						$rs4 = mysql_query($rech4)or die(mysql_error());
						if ($row4 = mysql_fetch_array($rs4))
						{
							extract($row4);
							if ($mt_coutk>0) // C'est un coût interne
								$cout_retour=$inth_rkm*$mt_coutk; // Montant "kilométrique" retour
							else // C'est sous-traité
							{
								// Coût forfaitaire
								if ($mt_coutas==0) // A saisir ?
									$cout_retour=$mt_coutf; // Oui : Montant forfaitaire pour le retour
								else
									$cout_retour=$inth_rcd; // Non : Montant saisi pour le retour
								// Coût kilométrique
								if ($mt_kmill==0) // Pas illimité : on calcule (sinon on ne fait rien)
								{
									$diff_km=$inth_rkm-$mt_kmi;
									if ($diff_km>0)
										$cout_retour=$cout_retour+($mt_kms*$diff_km);
								}
							}
							$cout_retour=$cout_retour+$mt_coutc+$inth_rfrais; // Rajout des frais supplémentaires
							
							if ($mt_tpsdi==0) // Le temps de déplacement n'est pas inclus : on calcul (sinon on ne fait rien)
								$cout_retour=$cout_retour+round((($tecpr_couth/3600)*$tpsr)*100)/100;
						}
						$pr=$pr+$cout_site+$cout_aller+$cout_retour;
					}
				}
			}
			mysql_free_result($rs2);
		}
	}

	mysql_free_result($rs);
	if ($open==TRUE)
	{
		mysql_close($conn);
	}
	
	return $pr;
}

?>