<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  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://smi.no-ip.org
 ****************************************************************************************/

// 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
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_dos($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBDOS . "` WHERE `dos_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Vérification du nombre de dossiers
 *
 * @param bool $open
 * @return int|null
 */
function nb_dos($open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBDOS . "`";
    $nbexist = $db->get_var($rexist);;
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

/**
 * Création du code Javascript pour affichage des détails dans un dossier de suivi
 *
 * @param $td
 * @param $code
 * @param string $id_html
 * @param string $from
 * @param bool $open
 */
function cre_html_detindos($td, $code, $id_html ='', $from = '', $open = TRUE)
{
    global
    $db;

    // Création de la chaine HTML
    $str_html = '';

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    $rech = "SELECT * FROM `" . TBDOSD . "` WHERE dosd_codedos='" . $db->escape($code) . "' ORDER BY `dosd_dateact`";
    $rows = $db->get_results($rech, ARRAY_A);

    if (($rows) AND (count($rows) > 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;

        foreach ($rows as $row) {
            $row = encode_str($row);
            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 .= datetolocal($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 ta_img,ta_desc FROM `" . TBTA . "` WHERE ta_code='" . $dosd_codeta . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            $str_html .= '<td class="tdlst" valign="top" align="center">';
            if ($row2) {
                extract($row2);
                $str_html .= '<img src="' . $ta_img . '" border="0" align="absmiddle" title="' . $ta_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 (intval($dosd_clos) == 0)) {
                $urleditdosd = "dosd_maj.php?td=edit&cd=" . $code . "&nd=" . $dosd_id;
                // $str_html .= cre_falselnk_img('', 'dosd_edit_'.$dosd_id, 'span', '', _("Edition"), 'lnkpop', $urleditdosd, '../img/edit.png', 900 , 650, true);
                $str_html .= "<a href=\"javascript:;\" onclick=\"init_new_window('".$urleditdosd."','dosd_edit_".$dosd_id."',900,550,'yes')\")>";
                $str_html .= '<img src="../img/edit.png" border="0" align="absmiddle" title="'._("Edition").'"></a>';
                $str_html .= ' ';

                $urldeldosd = "dosd_maj.php?td=del&cd=" . $code . "&nd=" . $dosd_id;
                // $str_html .= cre_falselnk_img('', 'dosd_del_'.$dosd_id, 'span', '', _("Suppression"), 'lnkpop', $urldeldosd, '../img/del.png', 900 , 650, true);
                $str_html .= "<a href=\"javascript:;\" onclick=\"init_new_window('".$urldeldosd."','dosd_del_".$dosd_id."',900,550,'yes')\")>";
                $str_html .= '<img src="../img/del.png" border="0" align="absmiddle" title="'._("Suppression").'"></a>';
                $str_html .= ' ';
            } else {
                $urldetdosd = "dosd_maj.php?td=view&cd=" . $code . "&nd=" . $dosd_id;
                // $str_html .= cre_falselnk_img('', 'dosd_view_'.$dosd_id, 'span', '', _("Visualisation"), 'lnkpop', $urldetdosd, '../img/eye.png', 900 , 650, true);
                $str_html .= "<a href=\"javascript:;\" onclick=\"init_new_window('".$urldetdosd."','dosd_view_".$dosd_id."',900,550,'yes')\")>";
                $str_html .= '<img src="../img/eye.png" border="0" align="absmiddle" title="'._("Visualisation").'"></a>';
            }
            $str_html .= '</td>';

            $str_html .= '</tr>';
            $i++;
        }

        // Fermeture de la table
        $str_html .= '</table>';
    }
    $db->disconnect();

    echo 'function detindos()';
    echo '{';
    echo $from . "$('#".$id_html."').html('" . str_replace("'", "\'", $str_html) . "');";
    echo '}';
}

// Catégories de dossiers

/**
 * Lecture d'un enregistrement catégorie dossier
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_dosc($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBDOSC . "` WHERE `dosc_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Vérification du nombre de catégories de dossiers
 *
 * @param bool $open
 * @return int|null
 */
function nb_dosc($open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBDOSC . "`";
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

// Statuts de dossiers

/**
 * Lecture d'un enregistrement statut dossier
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_doss($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBDOSS . "` WHERE `doss_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Vérification du nombre de statuts de dossiers
 *
 * @param bool $open
 * @return int|null
 */
function nb_doss($open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBDOSS . "`";
    $nbexist = $db->get_var($rexist);;
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

// Détails de dossiers

/**
 * Lecture d'un enregistrement détail dossier en fonction id et code dossier
 *
 * @param $id
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_dosd_in_dos($id, $code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBDOSD . "` WHERE `dosd_id` = '$id' AND `dosd_codedos` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Vérifie si un détail dossier est non clot pour un code dossier
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_dosd_open_dos($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT COUNT(*) FROM `" . TBDOSD . "` WHERE `dosd_clos` = '0' AND `dosd_codedos` = '" . $db->escape($code) . "'";
    $nbres = $db->get_var($rech2);
    if ($nbres > 0)
        $exist = TRUE;
    if ($open == TRUE)
        $db->disconnect();

    return $exist;
}

/**
 * Vérification du nombre de détails dossiers pour un dossier donné
 *
 * @param $code
 * @param bool $open
 * @return int|null
 */
function nb_dosd_in_dos($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT COUNT(*) FROM `" . TBDOSD . "` WHERE `dosd_codedos` = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rech2);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

// Types actions de dossiers

/**
 * Lecture d'un enregistrement type action
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_dosta($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBTA . "` WHERE `ta_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Vérification du nombre de types actions
 *
 * @param bool $open
 * @return int
 */
function nb_dosta($open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBTA . "`";
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

//

/**
 * @param $iddosd
 * @param $type
 * @param $idctact
 * @param $code
 * @param bool $open
 * @return array|null
 */
function exist_inter_id_in_dosdi($iddosd, $type, $idctact, $code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $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'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Donne le code Marque/Donneur d'ordres/Ss-Traitant en fonction iddosd
 *
 * @param $iddosd
 * @param $type
 * @param bool $open
 * @return string
 */
function donne_code_mds($iddosd, $type, $open = TRUE)
{
    global
    $db;
    $dosdi_codeinter = "";

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT DISTINCT dosdi_codeinter FROM `" . TBDOSDI . "` WHERE `dosdi_iddosd` = '$iddosd' AND `dosdi_typinter` = '$type'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2)
        extract($row2);
    if ($open == TRUE)
        $db->disconnect();

    return $dosdi_codeinter;
}


/**
 * Création du code Javascript pour affichage des contacts marques
 *
 * @param string $code
 * @param string $from
 * @param string $id_sel
 * @param bool $open
 * @internal param $nmque
 */
function cre_sel_ddeurs_cmque($code = '', $from = '', $id_sel = '', $open = TRUE)
{
    $tab_ddeur = give_tab_cmque($code,$open);

    echo 'function update_sel_ddeurs() {';
    echo 'var id_sel = ' . $from . '$(\'#' . $id_sel . '\');';
    echo 'var val_act = id_sel.val();';
    echo 'id_sel.empty();';
    echo 'id_sel.append(\'<option value="">' . _("-- Faites votre choix --") . '</option>\');';
    if (count($tab_ddeur) > 0) {
        foreach ($tab_ddeur as $key => $value) {
            echo 'id_sel.append(\'<option value="' . $key . '">' . str_escape($value) . '</option>\');';
        }
    }

    echo 'id_sel.val(val_act);';
    echo '}';
}

/**
 * Création du code Javascript pour affichage des contacts donneurs d'ordres
 *
 * @param string $code
 * @param string $from
 * @param string $id_sel
 * @param bool $open
 * @internal param $ndo
 */
function cre_sel_ddeurs_cdo($code = '', $from = '', $id_sel = '', $open = TRUE)
{
    $tab_ddeur = give_tab_cdo($code,$open);

    echo 'function update_sel_ddeurs() {';
    echo 'var id_sel = ' . $from . '$(\'#' . $id_sel . '\');';
    echo 'var val_act = id_sel.val();';
    echo 'id_sel.empty();';
    echo 'id_sel.append(\'<option value="">' . _("-- Faites votre choix --") . '</option>\');';
    if (count($tab_ddeur) > 0) {
        foreach ($tab_ddeur as $key => $value) {
            echo 'id_sel.append(\'<option value="' . $key . '">' . str_escape($value) . '</option>\');';
        }
    }
    echo 'id_sel.val(val_act);';
    echo '}';
}

/**
 * Création du code Javascript pour affichage des contacts donneurs d'ordres
 *
 * @param string $code
 * @param string $from
 * @param string $id_sel
 * @param bool $open
 * @internal param $ndo
 */
function cre_sel_ddeurs_cst($code = '', $from = '', $id_sel = '', $open = TRUE)
{
    $tab_ddeur = give_tab_cst($code,$open);

    echo 'function update_sel_ddeurs() {';
    echo 'var id_sel = ' . $from . '$(\'#' . $id_sel . '\');';
    echo 'var val_act = id_sel.val();';
    echo 'id_sel.empty();';
    echo 'id_sel.append(\'<option value="">' . _("-- Faites votre choix --") . '</option>\');';
    if (count($tab_ddeur) > 0) {
        foreach ($tab_ddeur as $key => $value) {
            echo 'id_sel.append(\'<option value="' . $key . '">' . str_escape($value) . '</option>\');';
        }
    }
    echo 'id_sel.val(val_act);';
    echo '}';
}

/**
 * Calcule le prix de revient d'un dossier
 *
 * @param $dossier
 * @param bool $open
 * @return int
 */
function px_revient_dos($dossier, $open = TRUE)
{
    global
    $db;
    $pr = 0;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    // Récupération prix de revient pour le travail effectué
    $rech = "SELECT * FROM `" . TBDOSD . "` WHERE `dosd_codedos`= '" . $db->escape($dossier) . "'";
    $rows = $db->get_results($rech, ARRAY_A);
    if (($rows) AND (count($rows) > 0)) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            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'";
            $row4 = $db->get_row($rech4, ARRAY_A);
            if ($row4) {
                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'";
            $row4 = $db->get_row($rech4, ARRAY_A);
            if ($row4) {
                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 . "'";
            $rows2 = $db->get_results($rech2, ARRAY_A);
            if (($rows2) AND (count($rows2) > 0)) {
                foreach ($rows2 as $row2) {
                    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";
                    $row3 = $db->get_row($rech3, ARRAY_A);
                    if ($row3) {
                        extract($row3);

                        $cout_aller = 0;
                        $cout_retour = 0;
                        // ... Aller
                        $rech4 = "SELECT * FROM `" . TBMT . "` WHERE `mt_code` = '$dosd_acodemt'";
                        $row4 = $db->get_row($rech4, ARRAY_A);
                        if ($row4) {
                            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;
                        }
                        // ...Sur site
                        $cout_site = round((($agepr_couth / 3600) * $tps) * 100) / 100;
                        // ... retour
                        $rech4 = "SELECT * FROM `" . TBMT . "` WHERE `mt_code` = '$dosd_rcodemt'";
                        $row4 = $db->get_row($rech4, ARRAY_A);
                        if ($row4) {
                            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;
                        }
                        $pr = $pr + $cout_site + $cout_aller + $cout_retour;
                    }
                }
            }

            // ... Contacts secondaires agence
            $rech2 = "SELECT dosdi_idctact FROM `" . TBDOSDI . "` WHERE `dosdi_typinter`= '0' AND `dosdi_idctact`<>'-1' AND `dosdi_iddosd`= '" . $dosd_id . "'";
            $rows2 = $db->get_results($rech2, ARRAY_A);
            if (($rows2) AND (count($rows2) > 0)) {
                foreach ($rows2 as $row2) {
                    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";
                    $row3 = $db->get_row($rech3, ARRAY_A);
                    if ($row3) {
                        extract($row3);

                        $cout_aller = 0;
                        $cout_retour = 0;
                        // ... Aller
                        $rech4 = "SELECT * FROM `" . TBMT . "` WHERE `mt_code` = '$dosd_acodemt'";
                        $row4 = $db->get_row($rech4, ARRAY_A);
                        if ($row4) {
                            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;
                        }
                        // ...Sur site
                        $cout_site = round((($cagepr_couth / 3600) * $tps) * 100) / 100;
                        // ... retour
                        $rech4 = "SELECT * FROM `" . TBMT . "` WHERE `mt_code` = '$dosd_rcodemt'";
                        $row4 = $db->get_row($rech4, ARRAY_A);
                        if ($row4) {
                            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;
                        }
                        $pr = $pr + $cout_site + $cout_aller + $cout_retour;
                    }
                }
            }

            // ... Techniciens agence
            $rech2 = "SELECT dosdi_codeinter FROM `" . TBDOSDI . "` WHERE `dosdi_typinter`= '1' AND `dosdi_iddosd`= '" . $dosd_id . "'";
            $rows2 = $db->get_results($rech2, ARRAY_A);
            if (($rows2) AND (count($rows2) > 0)) {
                foreach ($rows2 as $row2) {
                    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";
                    $row3 = $db->get_row($rech3, ARRAY_A);
                    if ($row3) {
                        extract($row3);

                        $cout_aller = 0;
                        $cout_retour = 0;
                        // ... Aller
                        $rech4 = "SELECT * FROM `" . TBMT . "` WHERE `mt_code` = '$dosd_acodemt'";
                        $row4 = $db->get_row($rech4, ARRAY_A);
                        if ($row4) {
                            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;
                        }
                        // ...Sur site
                        $cout_site = round((($tecpr_couth / 3600) * $tps) * 100) / 100;
                        // ... retour
                        $rech4 = "SELECT * FROM `" . TBMT . "` WHERE `mt_code` = '$dosd_rcodemt'";
                        $row4 = $db->get_row($rech4, ARRAY_A);
                        if ($row4) {
                            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;
                        }
                        $pr = $pr + $cout_site + $cout_aller + $cout_retour;
                    }
                }
            }
            $pr = $pr + $cout_kma + $cout_kmr;
        }
    }

    if ($open == TRUE)
        $db->disconnect();

    return $pr;
}
?>