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

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 type / 1 : code technicien / 2 : code intervention
    $mess = check_tec($tab_tec[0], $tab_tec[1], $tab_tec[2]);
    print $mess;
}

/**
 * @param $typ
 * @param $code
 * @param $coder
 * @return string
 */
function check_tec($typ, $code, $coder)
{

    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 = '';
    $rech = '';
    // On recupere les enregistrements
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    switch ($typ) {
        // Interventions
        case 1 :
            $rech = 'SELECT int_daterdv, int_hrdv, int_dureem FROM `' . TBINT . "` WHERE int_code='" . $db->escape($coder) . "'";
            break;
        default :
            break;
    }
    if ($rech <> '') {
        // On se connecte a la base
        $row = $db->get_row($rech, ARRAY_A);
        // On cree le resultat
        switch ($typ) {
            // Interventions
            case 1 :
                if ($row) {
                    extract($row);
                    if (($int_daterdv <> '') AND ($int_hrdv <> '') AND ($int_dureem <> 0)) {
                        $daynum = njour_semaine($int_daterdv);
                        if ($daynum == 0) {
                            $daynum = 7;
                        }
                        // On se connecte a la base
                        $rech2 = 'SELECT tecpr_j' . $daynum . 'm1, tecpr_j' . $daynum . 'm2, tecpr_j' . $daynum . 'a1, tecpr_j' . $daynum . 'a2 FROM `' . TBTECPR . "` WHERE tecpr_codetec='" . str_majuscules($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($int_hrdv);
                            $hrdvsecfin = $hrdvsecdeb + ($int_dureem * 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') . " :\n " . _('Intervention en dehors des heures ouvrées du technicien');
                            }
                        }
                    }
                }
                break;
            default :
                break;
        }
    }
    close_database();
    return $result;
}
