<?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 heures ouvrées d'un technicien */
/***************************************************/

session_start();
sleep(1);
if (array_key_exists("tec", $_REQUEST)) {
    $tomorrow = 0; //60*60*24; // en nb de secondes
    header("Cache-Control: max-age=$tomorrow");
    print_plain();
} else {
    print "Usage : $_SERVER[PHP_SELF]?tec=un-code-techinicien";
}

function print_plain()
{
    header("Content-type: text/html; charset=UTF-8");
    $tab_tec = explode("_", $_REQUEST["tec"]);
    // 0 : code technicien / 1 : date / 2 : heure / 3 : durée
    $mess = check_tec($tab_tec[0], $tab_tec[1], $tab_tec[2], $tab_tec[3]);
    print $mess;
}

/**
 * @param $code
 * @param $dte
 * @param $heure
 * @param $dur
 * @return string
 */
function check_tec($code, $dte, $heure, $dur)
{

    require_once("../lib/gettext/gettext.inc");
    include_once("../inc/config.inc.php");
    include_once("../inc/func_date.inc.php");
    include_once("../inc/func_time.inc.php");
    include_once("../inc/func_txt.inc.php");
    include_once("../classes/ezSQL/shared/ez_sql_core.php");
    include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
    include_once("../inc/func_sql.inc.php");

    $result = "";
    // On se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    if (($dte <> '') AND (chkdate($dte)) AND ($heure <> '') AND (intval($dur) <> 0)) {
        $daynum = njour_semaine(datetomysql($dte));
        if ($daynum == 0) $daynum = 7;
        $rech2 = "SELECT tecpr_j" . $daynum . "m1, tecpr_j" . $daynum . "m2, tecpr_j" . $daynum . "a1, tecpr_j" . $daynum . "a2 FROM `" . TBTECPR . "` WHERE tecpr_codetec='" . $db->escape($code) . "'";
        $row2 = $db->get_row($rech2, ARRAY_A);
        if ($row2) {
            extract($row2);
            // On transforme en secondes les heures et durées de l'intervention
            $hrdvsecdeb = heure_sec($heure);
            $hrdvsecfin = $hrdvsecdeb + ($dur * 60);
            // On transforme en secondes les tranches horaires du jour concerné
            $variable = "\$tecpr_j" . $daynum . "m1";
            eval ("\$hm1_sec = heure_sec($variable);");
            $variable = "\$tecpr_j" . $daynum . "m2";
            eval ("\$hm2_sec = heure_sec($variable);");
            $variable = "\$tecpr_j" . $daynum . "a1";
            eval ("\$ha1_sec = heure_sec($variable);");
            $variable = "\$tecpr_j" . $daynum . "a2";
            eval ("\$ha2_sec = heure_sec($variable);");

            $timeout = FALSE;
            if (($hm1_sec == $hm2_sec) OR ($ha1_sec == $ha2_sec))
                $timeout = TRUE;
            else {
                // On vérifie l'heure du RDV
                if (($hrdvsecdeb < $hm1_sec) OR (($hrdvsecdeb > $hm2_sec) AND ($hrdvsecdeb < $ha1_sec)) OR ($hrdvsecdeb > $ha2_sec))
                    $timeout = TRUE;
                // On vérifie l'heure de fin
                if (($hrdvsecfin < $hm1_sec) OR (($hrdvsecfin > $hm2_sec) AND ($hrdvsecfin < $ha1_sec)) OR ($hrdvsecfin > $ha2_sec))
                    $timeout = TRUE;
                // Contrôle si pas journée continue (voir pas "à cheval" sur la pause de mi-journée)
                if ($hm2_sec <> $ha1_sec) {
                    if (($hrdvsecdeb < $hm2_sec) AND ($hrdvsecfin > $ha1_sec))
                        $timeout = TRUE;
                }
            }

            if ($timeout == TRUE)
                $result = _("ATTENTION").' : <br />'._("En dehors des heures ouvrées du technicien");
        }
    }
    $db->disconnect();
    return $result;
}

?>