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

/**********************************************/
/* Récupération technicien en fonction agence */
/* Pour intervention et relevés               */
/**********************************************/

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

function print_plain()
{
    require_once("../lib/gettext/gettext.inc");
    header("Content-type: text/html; charset=UTF-8");
    $tab_codeage = explode("_", $_REQUEST["ca"]);
    $tec = get_tec($tab_codeage[0], $tab_codeage[1]);
    $result = [];
    $result[] = "-1="._("-- Dossier non affecté --");
    foreach ($tec as $row) {
        $result[] = $row["tec_code"] . "=" . $row["tec_nom"] . " " . $row["tec_prenom"] . " (" . $row["tec_codeage"] . ")";
    }
    print implode(";", $result);
}

/**
 * @param $age
 * @param $ages
 * @return array
 */
function get_tec($age, $ages)
{

    require_once("../lib/gettext/gettext.inc");
    include_once("../inc/config.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");

    // On cree le resultat
    $result = [];
    // On se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    // On recupere les enregistrements
    switch ($_SESSION["niv_log"]) {
        case 3 :
            $rech = "SELECT * FROM `" . TBTEC . "` WHERE `tec_code` = '" . $_SESSION["code_log"] . "'";
            break;
        default :
            $rech = "SELECT * FROM `" . TBTEC . "` WHERE";
            if ((trim($ages) <> "") AND ($ages <> "-1") AND ($ages <> $age))
                $rech .= "(";
            $rech .= " tec_codeage='" . trim($age) . "'";
            if ((trim($ages) <> "") AND ($ages <> "-1") AND ($ages <> $age))
                $rech .= " OR tec_codeage='" . trim($ages) . "')";
            break;
    }

    $rech .= " AND `tec_out` = '0' ORDER BY tec_nom";
    $rows = $db->get_results($rech, ARRAY_A);
    // On cree le resultat
    foreach ($rows as $row) {
        $row = encode_str($row);
        $result[] = $row;
    }
    $db->disconnect();
    return $result;
}

?>