<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.4
 *
 * Copyright (C) 2006-2016  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) {
    header('Location: ../index.php');
}

/*****************************************************/
/* Fonctions pour les accès aux fichiers techniciens */
/* et fichiers rattachés                             */
/*****************************************************/

/**
 * Vérification du nombre d'interventions sur un technicien
 *
 * @param $tec
 * @param bool $open
 * @return null
 */
function nb_int_tec($tec, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT COUNT(*) FROM `' . TBINT . "` WHERE `int_codetec` = '" . $db->escape($tec) . "'";
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Vérification du nombre de techniciens d'agence
 *
 * @param $agence
 * @param bool $open
 * @return null
 */
function nb_tec($agence, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    if ($agence <> '') {
        $rexist = 'SELECT COUNT(*) FROM `' . TBTEC . "` WHERE `tec_codeage` = '" . $db->escape($agence) . "'";
    } else {
        $rexist = 'SELECT COUNT(*) FROM `' . TBTEC . "` WHERE `tec_codeage` <> ''";
    }
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Vérification du nombre de techniciens chez un donneur d'ordres
 *
 * @param $code
 * @param bool $open
 * @return null
 */
function nb_tec_do($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    if ($code <> '') {
        $rexist = 'SELECT COUNT(*) FROM `' . TBTEC . "` WHERE `tec_codedo` = '" . $db->escape($code) . "'";
    } else {
        $rexist = 'SELECT COUNT(*) FROM `' . TBTEC . "` WHERE `tec_codedo` <> ''";
    }
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Vérification du nombre de techniciens chez un sous-traitant
 *
 * @param $code
 * @param bool $open
 * @return null
 */
function nb_tec_st($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    if ($code <> '') {
        $rexist = 'SELECT COUNT(*) FROM `' . TBTEC . "` WHERE `tec_codest` = '" . $db->escape($code) . "'";
    } else {
        $rexist = 'SELECT COUNT(*) FROM `' . TBTEC . "` WHERE `tec_codest` <> ''";
    }
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Vérification du nombre de techniciens actifs
 *
 * @param bool $open
 * @return null
 */
function nb_tec_actifs($open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT COUNT(*) FROM `' . TBTEC . "`  WHERE `tec_out` = '0'";
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

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

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBTEC . "` WHERE `tec_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Vérification de la présence d'un technicien dans une fiche intervention non close
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_tec_in_int_ouv($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }

    // On vérifie si le technicien est responsable d'une intervention
    $rexist = 'SELECT COUNT(*) FROM `' . TBINT . "` WHERE `int_codetec` = '" . $db->escape($code) . "' AND `int_datefin` = '0000-00-00'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0) {
        $exist = TRUE;
    }

    // On vérifie si le technicien est affecté à une intervention
    $rexist = 'SELECT ' . TBINT . '.int_id FROM `' . TBINTT . '`, `' . TBINT . '` WHERE ' . TBINTT . ".intt_codetec = '" . $db->escape($code) . "' AND " . TBINT . ".int_datefin = '0000-00-00' AND " . TBINT . '.int_code=' . TBINTT . '.intt_codeint';
    $rows = $db->get_results($rexist, ARRAY_A);
    if (count($rows) > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Vérification de la présence d'un technicien dans une fiche intervention
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_tec_in_int($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }

    // On vérifie si le technicien est responsable d'une intervention
    $rexist = 'SELECT COUNT(*) FROM `' . TBINT . "` WHERE `int_codetec` = '" . $db->escape($code) . "'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0) {
        $exist = TRUE;
    }

    // On vérifie si le technicien est affecté à une intervention
    $rexist = 'SELECT COUNT(*) FROM `' . TBINTT . "` WHERE `intt_codetec` = '" . $db->escape($code) . "'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Vérification de la présence d'un technicien dans une fiche Relevé licence
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_tec_in_rl($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT COUNT(*) FROM `' . TBRLIC . "` WHERE `rlic_codetec` = '" . $db->escape($code) . "'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Vérification de la présence d'un technicien dans une fiche Relevé produit
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_tec_in_rp($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT COUNT(*) FROM `' . TBRPDT . "` WHERE `rpdt_codetec` = '" . $db->escape($code) . "'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Vérification de la présence d'un technicien dans un bon de travail
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_tec_in_ot($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT COUNT(*) FROM `' . TBOT . "` WHERE `ot_codetec` = '" . $db->escape($code) . "'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Vérification de la présence d'un technicien dans un bon de travail non clos
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_tec_in_ot_ouv($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rexist = 'SELECT COUNT(*) FROM `' . TBOT . "` WHERE `ot_codetec` = '" . $db->escape($code) . "' AND `ot_datefin` = '0000-00-00'";
    $nbres = $db->get_var($rexist);
    if ($nbres > 0) {
        $exist = TRUE;
    }
    if ($open == TRUE) {
        close_database();
    }

    return $exist;
}

/**
 * Retourne les enregistrements de techniciens possibles pour une intervention
 *
 * @param $codeage
 * @param $codeages
 * @param $tab_codeages
 * @param bool $open
 * @return array
 */
function give_tab_rows_tec($codeage, $codeages, $tab_codeages, $open = TRUE)
{
    global
    $_SESSION,
    $db;

    $rows = [];

    if ($codeage <> '') {
        if ($open == TRUE) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
        }

        switch ($_SESSION['niv_log']) {
            case 3 :
                /*
                $rech = "SELECT * FROM `" . TBTEC . "` WHERE `tec_code` = '" . $_SESSION['code_log'] . "'";
                break;
                */
            case 4 :
                $rech = 'SELECT * FROM `' . TBTEC . "` WHERE `tec_codeage` = '" . $codeage . "'";
                /*if (($_SESSION['code_age'] <> $codeages))*/
                if ((trim($codeages) <> '') AND ($codeage <> $codeages)) {
                    $rech .= " OR `tec_codeage` = '" . $codeages . "'";
                }
                break;
            default :
                $rech = 'SELECT * FROM `' . TBTEC . "` WHERE `tec_codeage` = '" . $codeage . "'";
                // $rech = "SELECT * FROM `" . TBTEC . "` WHERE";
                if (trim($codeages) <> '') {
                    if ($codeage <> $codeages) {
                        $rech .= " OR `tec_codeage` = '" . $codeages . "'";
                    }
                } else {
                    if (count($tab_codeages) > 0) {
                        $rech .= ' AND (';
                        if (array_key_exists('-1', $tab_codeages) AND count($tab_codeages) == 1) {
                            $rech .= " `tec_codeage` = '" . $codeage . "' OR";
                        } else {
                            foreach ($tab_codeages as $key => $value) {
                                $rech .= " `tec_codeage` = '" . $key . "' OR";
                            }
                        }
                        // Suppression du dernier 'OR'
                        $rech = substr($rech, 0, -3);
                        $rech .= ')';
                    }
                }
                break;
        }
        $rech .= " AND `tec_out` = '0' ORDER BY tec_nom";
        $rows = $db->get_results($rech, ARRAY_A);

        if ($open == TRUE) {
            close_database();
        }
    }

    return $rows;
}

/**
 * Retourne un tableau des techniciens
 *
 *
 * @param string $typ
 * @param string $except
 * @param bool $open
 *
 * @return array
 */
function give_tab_tec($typ = 'age', $except = '', $open = TRUE)
{
    global
    $db;

    $tab_res = [];

    if (($typ == 'age') OR ($typ == 'do') OR ($typ == 'st')) {
        // Création de la liste des techniciens présents
        if ($open == TRUE) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
        }

        $rech = 'SELECT tec_code,tec_nom,tec_prenom,tec_codeage,tec_codedo,tec_codest FROM `' . TBTEC . '`';

        $rech .= ' WHERE `tec_code' . $typ . "` <> ''";
        $rech .= " AND `tec_out` = '0'";
        if ($except <> '') {
            $rech .= " AND `tec_code` <> '" . $except . "'";
        }
        $rech .= ' ORDER BY tec_nom';

        $rows = $db->get_results($rech, ARRAY_A);
        if ($rows) {
            foreach ($rows as $row) {
                $row = encode_str($row);
                extract($row);
                $str = $tec_nom . ' ' . $tec_prenom . ' (' . $tec_code;
                switch ($typ) {
                    case 'age' :
                        $str .= ' - AGENCE : ' . $tec_codeage;
                        break;
                    case 'do' :
                        $str .= ' - D.O. : ' . $tec_codedo;
                        break;
                    case 'st' :
                        $str .= ' - S.T. : ' . $tec_codest;
                        break;
                }
                $str .= ')';
                $tab_res[$tec_code] = $str;
            }
        }

        if ($open == TRUE) {
            close_database();
        }
    }
    return $tab_res;
}

/**
 * Retourne un tableau des techniciens pour les plannings
 *
 * @param string $typ
 * @param string $code
 * @param string $except
 * @param bool $open
 * @return array
 */
function give_tab_tec_plan($typ = 'age', $code = '', $except = '', $open = TRUE)
{
    global
    $db;

    $tecprm = [];
    $tab_res = [];

    if (($typ == 'age') OR ($typ == 'do') OR ($typ == 'st')) {
        // Création de la liste des techniciens présents
        if ($open == TRUE) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
        }

        $rech = 'SELECT tec_code,tec_couleur,tec_nom,tec_prenom,tec_codeage,tec_codedo,tec_codest FROM `' . TBTEC . '`';

        $rech .= ' WHERE `tec_code' . $typ . "` = '" . $code . "'";
        $rech .= " AND `tec_out` = '0'";
        if ($except <> '') {
            $rech .= " AND `tec_code` <> '" . $except . "'";
        }
        $rech .= ' ORDER BY tec_nom';

        $rows = $db->get_results($rech, ARRAY_A);
        if ($rows) {
            foreach ($rows as $row) {
                $row = encode_str($row);
                extract($row);
                if (last_id_tecpr($tec_code, FALSE) <> 0) {
                    $str = $tec_nom . ' ' . $tec_prenom . ' (' . $tec_code;
                    switch ($typ) {
                        case 'age' :
                            $str .= ' - AGENCE : ' . $tec_codeage;
                            break;
                        case 'do' :
                            $str .= ' - D.O. : ' . $tec_codedo;
                            break;
                        case 'st' :
                            $str .= ' - S.T. : ' . $tec_codest;
                            break;
                    }
                    $str .= ')';
                    $tecprm['code'] = $tec_code;
                    $tecprm['coul'] = $tec_couleur;
                    $tecprm['detail'] = $str;
                    $tab_res[] = $tecprm;
                }
            }
        }

        if ($open == TRUE) {
            close_database();
        }
    }
    return $tab_res;
}

/**
 * @param $codest
 * @param bool $open
 * @return array
 */
function give_tab_rows_tec_st($codest, $open = TRUE)
{
    global
    $_SESSION,
    $db;

    $rows = [];

    if ($codest <> '') {
        if ($open == TRUE) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
        }

        $rech = 'SELECT * FROM `' . TBTEC . "` WHERE `tec_codest` = '" . $codest . "'";
        $rech .= " AND `tec_out` = '0' ORDER BY tec_nom";
        $rows = $db->get_results($rech, ARRAY_A);

        if ($open == TRUE) {
            close_database();
        }
    }

    return $rows;
}

// Prix de revient

/**
 * Vérification du nombre de prix de revient enregistrés pour un technicien
 *
 * @param $tec
 * @param bool $open
 * @return null
 */
function nb_tecpr($tec, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    if ($tec <> '') {
        $rexist = 'SELECT COUNT(*) FROM `' . TBTECPR . "` WHERE `tecpr_codetec` = '" . $db->escape($tec) . "'";
    } else {
        $rexist = 'SELECT COUNT(*) FROM `' . TBTECPR . '`';
    }
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE) {
        close_database();
    }

    return $nbexist;
}

/**
 * Vérification s'il y a un problème de prix de revient enregistrés pour les techniciens d'une agence
 *
 * @param $agence
 * @param bool $open
 * @return int
 */
function pbm_tecspr($agence, $open = TRUE)
{
    global
    $db;
    $pbm = 0;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    if ($agence <> '') {
        $rech2 = 'SELECT tec_code FROM `' . TBTEC . "` WHERE `tec_codeage` = '" . $db->escape($agence) . "'";
    } else {
        $rech2 = 'SELECT tec_code FROM `' . TBTEC . '`';
    }
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2) {
        extract($row2);
        $rech3 = 'SELECT COUNT(*) FROM `' . TBTECPR . "` WHERE `tecpr_codetec` = '" . $tec_code . "'";
        $nbres = $db->get_var($rech3);
        if ($nbres == 0) {
            $pbm = 1;
            return $pbm;
        }
    }
    if ($open == TRUE) {
        close_database();
    }

    return $pbm;
}

/**
 * Retourne la liste des horaires, pour un technicien donné
 *
 * @param $code
 * @param bool $retour
 * @param bool $open
 * @return string
 */
function str_tecspr($code, $retour = TRUE, $open = TRUE)
{
    global
    $db,
    $lng_tjourf;
    $str_tecpr = '';

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }

    $rech2 = 'SELECT * FROM `' . TBTECPR . "` WHERE `tecpr_codetec` = '" . $db->escape($code) . "'";
    $rech2 .= ' ORDER BY tecpr_id DESC';
    $rech2 .= ' LIMIT 1';
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2) {
        extract($row2);
        foreach ($lng_tjourf as $key => $value) {
            $str_tecpr .= $value . ' : AM ';
            $variable = "\$tecpr_j" . $key . 'm1';
            eval ("\$valeur = substr($variable,0,5);");
            $str_tecpr .= $valeur;
            $str_tecpr .= ' - ';
            $variable = "\$tecpr_j" . $key . 'm2';
            eval ("\$valeur = substr($variable,0,5);");
            $str_tecpr .= $valeur;
            $str_tecpr .= ' / PM ';
            $variable = "\$tecpr_j" . $key . 'a1';
            eval ("\$valeur = substr($variable,0,5);");
            $str_tecpr .= $valeur;
            $str_tecpr .= ' - ';
            $variable = "\$tecpr_j" . $key . 'a2';
            eval ("\$valeur = substr($variable,0,5);");
            $str_tecpr .= $valeur;
            if ($retour == TRUE) {
                $str_tecpr .= '<br />';
            }
        }
    }
    if ($open == TRUE) {
        close_database();
    }

    return $str_tecpr;
}

/**
 * Lecture d'un enregistrement prix de revient technicien
 *
 * @param $id
 * @param bool $open
 * @return array|null
 */
function lit_enr_tecpr($id, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT * FROM `' . TBTECPR . "` WHERE `tecpr_id` = '$id'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE) {
        close_database();
    }

    return $row2;
}

/**
 * Retourne le dernier enregistrement d'un prix de revient technicien
 *
 * @param $code
 * @param bool $open
 * @param int $except_id
 * @return int
 */
function last_id_tecpr($code, $open = TRUE, $except_id = 0)
{
    global
    $db;
    $tecpr_id = 0;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT tecpr_id FROM `' . TBTECPR . "` WHERE `tecpr_codetec` = '" . $db->escape($code) . "'";
    if ($except_id <> 0) {
        $rech2 .= ' AND `tecpr_id` != ' . $except_id;
    }
    $rech2 .= ' ORDER BY tecpr_id DESC';
    $rech2 .= ' LIMIT 1';
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2) {
        extract($row2);
    }
    if ($open == TRUE) {
        close_database();
    }

    return $tecpr_id;
}

/**
 * Retourne la dernière date de modif d'un prix de revient technicien
 *
 * @param $id
 * @param bool $open
 * @return bool|string
 */
function last_date_tecpr($id, $open = TRUE)
{
    global
    $db,
    $datea;
    $tecpr_date = $datea;

    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }
    $rech2 = 'SELECT tecpr_date FROM `' . TBTECPR . "` WHERE `tecpr_id` = '$id'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($row2) {
        extract($row2);
    }
    if ($open == TRUE) {
        close_database();
    }

    return $tecpr_date;
}


/**
 * @param $typd
 * @param $coded
 * @param $dayweek
 * @param $heured
 * @param $dureem
 * @param bool $open
 * @return string
 */
function chk_evt_tec($typd, $coded, $dayweek, $heured, $dureem, $open = TRUE)
{
    global
    $db;

    $mess_err = _('Evénement en dehors des plages horaires du technicien');

    // On se connecte a la base
    if ($open == TRUE) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    }

    // Lecture des paramètres planning
    $row = lit_enr_pprm(FALSE);
    if ($row) {
        $row = encode_str($row);
        extract($row);
    }

    // Récupération derniers paramètres horaires du technicien
    if ((int)$typd == 1) {
        $rech2 = 'SELECT tec_codeage FROM `' . TBTEC . "` WHERE `tec_code` = '" . $db->escape($coded) . "'";
        $row2 = $db->get_row($rech2, ARRAY_A);
        if (is_array($row2)) {
            extract($row2);
            // Récupération derniers paramètres horaires
            // Si technicien agence
            if ($tec_codeage <> '') {
                $id_tecpr = last_id_tecpr($coded, FALSE);
                $row3 = lit_enr_tecpr($id_tecpr, FALSE);
                if (is_array($row3)) {
                    extract($row3);

                    $heure_between = TRUE;
                    $heured_ok = FALSE;
                    $heuref_ok = FALSE;
                    $heured_sec = heure_sec($heured);
                    $heuref_sec = heure_sec(heure_fin($heured, $dureem));

                    // On récupère les horaires (en secondes) du jour
                    $hm1_sec = 0;
                    $hm2_sec = 0;
                    $ha1_sec = 0;
                    $ha2_sec = 0;

                    $variable = "\$tecpr_j" . $dayweek . 'm1';
                    eval ("\$hm1_sec = heure_sec($variable);");
                    $variable = "\$tecpr_j" . $dayweek . 'm2';
                    eval ("\$hm2_sec = heure_sec($variable);");
                    $variable = "\$tecpr_j" . $dayweek . 'a1';
                    eval ("\$ha1_sec = heure_sec($variable);");
                    $variable = "\$tecpr_j" . $dayweek . 'a2';
                    eval ("\$ha2_sec = heure_sec($variable);");


                    // On vérifie l'heure de début
                    if ((($heured_sec >= $hm1_sec) AND ($heured_sec <= $hm2_sec) AND ($hm1_sec <> $hm2_sec)) OR
                        ((($heured_sec >= $ha1_sec) AND ($heured_sec <= $ha2_sec)) AND ($ha1_sec <> $ha2_sec))
                    ) {
                        $heured_ok = TRUE;
                    }

                    // On vérifie l'heure de fin
                    if ((($heuref_sec >= $hm1_sec) AND ($heuref_sec <= $hm2_sec) AND ($hm1_sec <> $hm2_sec)) OR
                        ((($heuref_sec >= $ha1_sec) AND ($heuref_sec <= $ha2_sec)) AND ($ha1_sec <> $ha2_sec))
                    ) {
                        $heuref_ok = TRUE;
                    }

                    if (($heured_ok == TRUE) AND ($heuref_ok == TRUE)) {
                        if (($heured_sec <= $hm2_sec) AND ($heuref_sec >= $ha1_sec) AND ($hm2_sec <> $ha1_sec)) {
                            $heure_between = FALSE;
                        }
                    }

                    // print($heured.'('.$heured_sec.') / '.sec_heure($heuref_sec).' ('.$heuref_sec.')/ '.$hm1_sec.' - '.$hm2_sec.' - '.$ha1_sec.' - '.$ha2_sec.' > '.$heured_ok.'> '.$heuref_ok.'> '.$heure_between);

                    // Les heures de début et de fin sont ok : on vérifie si la période entre ces 2 heures est valable
                    if (($heured_ok == TRUE) AND ($heuref_ok == TRUE) AND ($heure_between == TRUE)) {
                        $mess_err = '';
                    }
                } else {
                    $mess_err = _("Pas d'horaires saisis");
                }
            }
        } else {
            $mess_err = _('Problème de code technicien');
        }
    } else {
        $mess_err = '';
    }

    if ($open == TRUE) {
        close_database();
    }

    return $mess_err;
}
