<?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 Dossiers */
/**************************************************/

/** Dossiers de suivi */

// Lecture d'un enregistrement dossier
function lit_enr_dos($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBDOS. "` WHERE `dos_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 dossiers
function nb_dos()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBDOS;
	$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 détails dans un dossier de suivi
function cre_js_detindos($td,$code,$from)
{
	echo "<script language=\"javascript\">\n";
	
	echo "function detindos() {\n";
	echo "var myid = ".$from."document.getElementById(\"detdosd\");\n";
	echo "myid.innerHTML = '';\n";

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

	$rech = "SELECT * FROM ".TBDOSD." WHERE dosd_codedos='".$code."' ORDER BY `dosd_dateact`";
	$rs = mysql_query($rech) or die(mysql_error());
		
	if (mysql_num_rows($rs)>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.="Date";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="De";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="A";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Type action";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Synthèse";
		$str_html.="</th>";

		$str_html.="<th>";
		$str_html.="Cliquer";
		$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\">";

			// Date
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			$str_html.=formatedate($dosd_dateact);
			$str_html.="</td>";

			// De
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			$str_html.=substr($dosd_heured,0,5);
			$str_html.="</td>";

			// A
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			$str_html.=substr($dosd_heuref,0,5);
			$str_html.="</td>";

			// Type action
			$rech2 = "SELECT * FROM ".TBDOSTA." WHERE dosta_code='".$dosd_codedosta."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			if ($row2 = mysql_fetch_assoc($rs2))
			{         
				extract($row2);
				$str_html.="<img src=\"".$dosta_img."\" border=\"0\" align=\"absmiddle\" title=\"".$dosta_desc."\"></a>";
			}
			else
				$str_html.="-";
			$str_html.="</td>";
				
			// Résumé
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			$str_html.=formatetxtbr(cesuretxt(tronquetxt($dosd_res,150),50));
			$str_html.="</td>";
				

			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			if (($td == "edit") AND ($dosd_clos==0))
			{
				$urleditdosd = "dosd_maj.php?td=edit&cd=".$code."&nd=".$dosd_id;
				$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urleditdosd."',900,650,'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 détail\"></a>";
				$str_html.=" ";

				$urldeldosd = "dosd_maj.php?td=del&cd=".$code."&nd=".$dosd_id;
				$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urldeldosd."',900,650,'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 détail\"></a>";
				$str_html.=" ";
			}
			else
			{
				$urldetdosd = "dosd_maj.php?td=view&cd=".$code."&nd=".$dosd_id;
				$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urldetdosd."',900,650,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');\")>";
				$str_html.="<img src=\"../img/eye.png\" border=\"0\" align=\"absmiddle\" title=\"Voir détail\"></a>";
			}
			$str_html.="</td>";
	
			$str_html.="</tr>";
			$i++;
		}
	
		// 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";

	}
	mysql_close($conn);
	
	echo "}\n";
	
	echo "</script>\n";
}

/** Catégories de dossiers **/

// Lecture d'un enregistrement catégorie dossier
function lit_enr_dosc($code,$open=TRUE)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBDOSC. "` WHERE `dosc_code` = '$code'";
	$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;
}

// Vérification du nombre de catégories de dossiers
function nb_dosc()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBDOSC;
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

/** Statuts de dossiers **/

// Lecture d'un enregistrement statut dossier
function lit_enr_doss($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBDOSS. "` WHERE `doss_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 statuts de dossiers
function nb_doss()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBDOSS;
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

/** Détails de dossiers **/

// Lecture d'un enregistrement détail dossier en fonction id et code dossier
function lit_enr_dosd_in_dos($id,$code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBDOSD. "` WHERE `dosd_id` = '$id' AND `dosd_codedos` = '$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érifie si un détail dossier est non clot pour un code dossier
function exist_dosd_open_dos($code,$open)
{
	$exist=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBDOSD. "` WHERE `dosd_clos` = '0' AND `dosd_codedos` = '$code'";
	$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;
}

// Vérification du nombre de détails dossiers pour un dossier donné
function nb_dosd_in_dos($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBDOSD. "` WHERE `dosd_codedos` = '$code'";
	$rs2 = mysql_query($rech2) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

/** Types actions de dossiers **/

// Lecture d'un enregistrement type action
function lit_enr_dosta($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBDOSTA. "` WHERE `dosta_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 types actions
function nb_dosta()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBDOSTA;
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

/** Intervenants sur détails dossiers **/
function exist_inter_id_in_dosdi($iddosd,$type,$idctact,$code,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM `" .TBDOSDI. "` WHERE `dosdi_iddosd` = '$iddosd' AND `dosdi_typinter` = '$type' AND `dosdi_idctact` = '$idctact'";
	if (intval($type)<>0)
		$rech2.=" AND `dosdi_codeinter` = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $row2;
}

// Donne le code Marque/Donneur d'ordres/Ss-Traitant en fonction iddosd
function donne_code_mds($iddosd,$type,$open)
{
	$dosdi_codeinter="";
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT DISTINCT dosdi_codeinter FROM `" .TBDOSDI. "` WHERE `dosdi_iddosd` = '$iddosd' AND `dosdi_typinter` = '$type'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	if ($row2 = mysql_fetch_array($rs2))
		extract($row2);
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $dosdi_codeinter;
}

// Création du code Javascript pour affichage des contacts agences
function cre_js_cage($nage,$from)
{
	echo "<script language=\"javascript\">\n";
	
	echo "function updatcage() {\n";
	echo "var myid = ".$from."document.getElementById(\"cage\");\n";
	echo 'if (myid) {';
		echo "myid.innerHTML = '';\n";
	
		if (trim($nage)<>"")
		{
			// On se connecte a la base
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);

			// Contact principal
			$rech = "SELECT * FROM ".TBAGE." WHERE `age_code` = '".$nage."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
				if ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
					$txt_cage = $age_resp_nom." ".$age_resp_prenom." (Contact principal)";
					echo 'option = '.$from.'document.createElement("option");';
					echo 'option.setAttribute("value","-1");';
					echo 'option.innerHTML ="'.$txt_cage.'";';
					echo 'myid.appendChild(option);';
				}
			}

			// Contacts secondaires
			$rech = "SELECT * FROM ".TBCAGE." WHERE `cage_codeage` = '".$nage."' ORDER BY cage_nom";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
	
					$txt_cage = $cage_nom." ".$cage_prenom;
					$row2=lit_enr_fonc($cage_codefonc,FALSE);
					if (is_array($row2))
					{
						extract($row2);
						$txt_fonc=$fonc_desc;
					}
					else
						$txt_fonc=$cage_codefonc;
					$txt_cage.=" (".$txt_fonc.")";
					echo 'option = '.$from.'document.createElement("option");';
					echo 'option.setAttribute("value","'.$cage_id.'");';
					echo 'option.innerHTML ="'.$txt_cage.'";';
					echo 'myid.appendChild(option);';
				}
			}		
			mysql_close();
		}
	echo "}\n";
	echo "}\n";
	
	echo "</script>\n";
}

// Création du code Javascript pour affichage des contacts clients
function cre_js_ccli($ncli,$from)
{
	echo "<script language=\"javascript\">\n";
	
	echo "function updatccli() {\n";
	echo "var myid = ".$from."document.getElementById(\"ccli\");\n";
	echo 'if (myid) {';
		echo "myid.innerHTML = '';\n";
	
		if (trim($ncli)<>"")
		{
			// On se connecte a la base
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);

			// Contact principal
			$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)";
					echo 'option = '.$from.'document.createElement("option");';
					echo 'option.setAttribute("value","-1");';
					echo 'option.innerHTML ="'.$txt_ccli.'";';
					echo 'myid.appendChild(option);';
				}
			}

			// Contacts secondaires
			$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.")";
					echo 'option = '.$from.'document.createElement("option");';
					echo 'option.setAttribute("value","'.$ccli_id.'");';
					echo 'option.innerHTML ="'.$txt_ccli.'";';
					echo 'myid.appendChild(option);';
				}
			}		
			mysql_close();
		}
	echo "}\n";
	echo "}\n";
	
	echo "</script>\n";
}

// Création du code Javascript pour affichage des contacts marques
function cre_js_cmque($nmque,$from)
{
	echo "<script language=\"javascript\">\n";
	
	echo "function updatcmque() {\n";
	echo "var myid = ".$from."document.getElementById(\"cmque\");\n";
	echo 'if (myid) {';
		echo "myid.innerHTML = '';\n";
	
		if (trim($nmque)<>"")
		{
			// On se connecte a la base
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);

			// Filtre sur marque
			$rech = "SELECT * FROM ".TBCMQUE." WHERE `cmque_codemque` = '".$nmque."' ORDER BY cmque_nom";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
	
					$txt_cmque = $cmque_nom." ".$cmque_prenom;
					$row2=lit_enr_fonc($cmque_codefonc,FALSE);
					if (is_array($row2))
					{
						extract($row2);
						$txt_fonc=$fonc_desc;
					}
					else
						$txt_fonc=$cmque_codefonc;
					$txt_cmque.=" (".$txt_fonc.")";
					echo 'option = '.$from.'document.createElement("option");';
					echo 'option.setAttribute("value","'.$cmque_id.'");';
					echo 'option.innerHTML ="'.$txt_cmque.'";';
					echo 'myid.appendChild(option);';
				}
			}		
			mysql_close();
		}
	echo "}\n";
	echo "}\n";
	
	echo "</script>\n";
}

// Création du code Javascript pour affichage des contacts donneurs d'ordres
function cre_js_cdo($ndo,$from)
{
	echo "<script language=\"javascript\">\n";
	
	echo "function updatcdo() {\n";
	echo "var myid = ".$from."document.getElementById(\"cdo\");\n";
	echo 'if (myid) {';
		echo "myid.innerHTML = '';\n";
	
		if (trim($ndo)<>"")
		{
			// On se connecte a la base
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);

			// Filtre sur donneur d'ordres
			$rech = "SELECT * FROM ".TBCDO." WHERE `cdo_codedo` = '".$ndo."' ORDER BY cdo_nom";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
	
					$txt_cdo = $cdo_nom." ".$cdo_prenom;
					$row2=lit_enr_fonc($cdo_codefonc,FALSE);
					if (is_array($row2))
					{
						extract($row2);
						$txt_fonc=$fonc_desc;
					}
					else
						$txt_fonc=$cdo_codefonc;
					$txt_cdo.=" (".$txt_fonc.")";
					echo 'option = '.$from.'document.createElement("option");';
					echo 'option.setAttribute("value","'.$cdo_id.'");';
					echo 'option.innerHTML ="'.$txt_cdo.'";';
					echo 'myid.appendChild(option);';
				}
			}		
			mysql_close();
		}
	echo "}\n";
	echo "}\n";
	
	echo "</script>\n";
}

// Création du code Javascript pour affichage des contacts sous-traitants
function cre_js_cst($nst,$from)
{
	echo "<script language=\"javascript\">\n";
	
	echo "function updatcst() {\n";
	echo "var myid = ".$from."document.getElementById(\"cst\");\n";
	echo 'if (myid) {';
		echo "myid.innerHTML = '';\n";
	
		if (trim($nst)<>"")
		{
			// On se connecte a la base
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);

			// Filtre sur sous-traitant
			$rech = "SELECT * FROM ".TBCST." WHERE `cst_codest` = '".$nst."' ORDER BY cst_nom";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) <> 0)
			{
				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
	
					$txt_cst = $cst_nom." ".$cst_prenom;
					$row2=lit_enr_fonc($cst_codefonc,FALSE);
					if (is_array($row2))
					{
						extract($row2);
						$txt_fonc=$fonc_desc;
					}
					else
						$txt_fonc=$cst_codefonc;
					$txt_cst.=" (".$txt_fonc.")";
					echo 'option = '.$from.'document.createElement("option");';
					echo 'option.setAttribute("value","'.$cst_id.'");';
					echo 'option.innerHTML ="'.$txt_cst.'";';
					echo 'myid.appendChild(option);';
				}
			}		
			mysql_close();
		}
	echo "}\n";
	echo "}\n";
	
	echo "</script>\n";
}


// Calcule le prix de revient d'un dossier
function px_revient_dos($dossier,$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é
	$rech = "SELECT * FROM `" .TBDOSD. "` WHERE `dosd_codedos`= '".$dossier."'";
	$rs = mysql_query($rech) or die(mysql_error());
	if (mysql_num_rows($rs) <> 0)
	{
		while ($row = mysql_fetch_assoc($rs)) 
		{
			extract($row);    
			$cout_kma=0;
			$cout_kmr=0;
			$tps = diff_time_sec($dosd_heured,$dosd_heuref); // Temps passé sur site
			$tpsa = diff_time_sec($dosd_aheured,$dosd_aheuref); // Temps pour aller
			$tpsr = diff_time_sec($dosd_rheured,$dosd_rheuref); // Temps pour retour

			// ..Coût kilométrique aller (à calculer UNE seule fois)
			$rech4 = "SELECT * FROM `" .TBMT. "` WHERE `mt_code` = '$dosd_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_kma=$dosd_akm*$mt_coutk; // Montant "kilométrique" aller
				else // C'est sous-traité
				{
					// Coût forfaitaire
					if ($mt_coutas==0) // A saisir ?
						$cout_kma=$mt_coutf; // Oui : Montant forfaitaire pour l'aller
					else
						$cout_kma=$dosd_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=$dosd_akm-$mt_kmi;
						if ($diff_km>0)
							$cout_kma=$cout_kma+($mt_kms*$diff_km);
					}
				}
				$cout_kma=$cout_kma+$mt_coutc+$dosd_afrais; // Rajout des frais supplémentaires
			}

			// ..Coût kilométrique retour (à calculer UNE seule fois)
			$rech4 = "SELECT * FROM `" .TBMT. "` WHERE `mt_code` = '$dosd_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_kmr=$dosd_rkm*$mt_coutk; // Montant "kilométrique" aller
				else // C'est sous-traité
				{
					// Coût forfaitaire
					if ($mt_coutas==0) // A saisir ?
						$cout_kmr=$mt_coutf; // Oui : Montant forfaitaire pour l'aller
					else
						$cout_kmr=$dosd_rcd; // 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=$dosd_rkm-$mt_kmi;
						if ($diff_km>0)
							$cout_kmr=$cout_kmr+($mt_kms*$diff_km);
					}
				}
				$cout_kmr=$cout_kmr+$mt_coutc+$dosd_rfrais; // Rajout des frais supplémentaires
			}

			// Calcul du coût de déplacement
			// ... Contact principal agence
			$rech2 = "SELECT dosdi_codeinter FROM `" .TBDOSDI. "` WHERE `dosdi_typinter`= '0' AND `dosdi_idctact`= '-1' AND `dosdi_iddosd`= '".$dosd_id."'";
			$rs2 = mysql_query($rech2) 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 en fonction de la date
					$rech3 = "SELECT agepr_couth FROM `" .TBAGEPR. "` WHERE `agepr_date` <= '$dosd_dateact' AND `agepr_codeage` = '".$dosdi_codeinter."' ORDER BY agepr_date DESC";
					$rs3 = mysql_query($rech3) or die(mysql_error());
					if ($row3 = mysql_fetch_array($rs3))
					{
						extract($row3);
						$cout_site=0;
						$cout_aller=0;
						$cout_retour=0;
						// ... Aller
						$rech4 = "SELECT * FROM `" .TBMT. "` WHERE `mt_code` = '$dosd_acodemt'";
						$rs4 = mysql_query($rech4)or die(mysql_error());
						if ($row4 = mysql_fetch_array($rs4))
						{
							extract($row4);
							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((($agepr_couth/3600)*$tpsa)*100)/100;
						}
						mysql_free_result($rs4);
						// ...Sur site
						$cout_site=round((($agepr_couth/3600)*$tps)*100)/100;
						// ... retour
						$rech4 = "SELECT * FROM `" .TBMT. "` WHERE `mt_code` = '$dosd_rcodemt'";
						$rs4 = mysql_query($rech4)or die(mysql_error());
						if ($row4 = mysql_fetch_array($rs4))
						{
							extract($row4);
							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((($agepr_couth/3600)*$tpsr)*100)/100;
						}
						mysql_free_result($rs4);
						$pr=$pr+$cout_site+$cout_aller+$cout_retour;
					}
					mysql_free_result($rs3);
				}
			}
			mysql_free_result($rs2);

			// ... Contacts secondaires agence
			$rech2 = "SELECT dosdi_idctact FROM `" .TBDOSDI. "` WHERE `dosdi_typinter`= '0' AND `dosdi_idctact`<>'-1' AND `dosdi_iddosd`= '".$dosd_id."'";
			$rs2 = mysql_query($rech2) 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 en fonction de la date
					$rech3 = "SELECT cagepr_couth FROM `" .TBCAGEPR. "` WHERE `cagepr_date` <= '$dosd_dateact' AND `cagepr_idcage` = '".$dosdi_idctact."' ORDER BY cagepr_date DESC";
					$rs3 = mysql_query($rech3) or die(mysql_error());
					if ($row3 = mysql_fetch_array($rs3))
					{
						extract($row3);
						$cout_site=0;
						$cout_aller=0;
						$cout_retour=0;
						// ... Aller
						$rech4 = "SELECT * FROM `" .TBMT. "` WHERE `mt_code` = '$dosd_acodemt'";
						$rs4 = mysql_query($rech4)or die(mysql_error());
						if ($row4 = mysql_fetch_array($rs4))
						{
							extract($row4);
							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((($cagepr_couth/3600)*$tpsa)*100)/100;
						}
						mysql_free_result($rs4);
						// ...Sur site
						$cout_site=round((($cagepr_couth/3600)*$tps)*100)/100;
						// ... retour
						$rech4 = "SELECT * FROM `" .TBMT. "` WHERE `mt_code` = '$dosd_rcodemt'";
						$rs4 = mysql_query($rech4)or die(mysql_error());
						if ($row4 = mysql_fetch_array($rs4))
						{
							extract($row4);
							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((($cagepr_couth/3600)*$tpsr)*100)/100;
						}
						mysql_free_result($rs4);
						$pr=$pr+$cout_site+$cout_aller+$cout_retour;
					}
					mysql_free_result($rs3);
				}
			}
			mysql_free_result($rs2);

			// ... Techniciens agence
			$rech2 = "SELECT dosdi_codeinter FROM `" .TBDOSDI. "` WHERE `dosdi_typinter`= '1' AND `dosdi_iddosd`= '".$dosd_id."'";
			$rs2 = mysql_query($rech2) 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` <= '$dosd_date' AND `tecpr_codetec` = '".$dosdi_codeinter."' ORDER BY tecpr_date DESC";
					$rs3 = mysql_query($rech3) or die(mysql_error());
					if ($row3 = mysql_fetch_array($rs3))
					{
						extract($row3);
						$cout_site=0;
						$cout_aller=0;
						$cout_retour=0;
						// ... Aller
						$rech4 = "SELECT * FROM `" .TBMT. "` WHERE `mt_code` = '$dosd_acodemt'";
						$rs4 = mysql_query($rech4)or die(mysql_error());
						if ($row4 = mysql_fetch_array($rs4))
						{
							extract($row4);
							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;
						}
						mysql_free_result($rs4);
						// ...Sur site
						$cout_site=round((($tecpr_couth/3600)*$tps)*100)/100;
						// ... retour
						$rech4 = "SELECT * FROM `" .TBMT. "` WHERE `mt_code` = '$dosd_rcodemt'";
						$rs4 = mysql_query($rech4)or die(mysql_error());
						if ($row4 = mysql_fetch_array($rs4))
						{
							extract($row4);
							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)*$tpsr)*100)/100;
						}
						mysql_free_result($rs4);
						$pr=$pr+$cout_site+$cout_aller+$cout_retour;
					}
					mysql_free_result($rs3);
				}
			}
			mysql_free_result($rs2);
			$pr=$pr+$cout_kma+$cout_kmr;
		}
	}

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

?>