<?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                          */
/* Fabrication du tableau des techniciens     */
/*  affectables                               */
/**********************************************/

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()
{
    header("Content-type: text/html; charset=UTF-8");
    $tab_codeage = explode("_", $_REQUEST["ca"]);
    $tabtec = get_tec($tab_codeage[0], $tab_codeage[1], $tab_codeage[2]);
    print $tabtec;
}

/**
 * @param $age
 * @param $ages
 * @param $td
 * @return string
 */
function get_tec($age, $ages, $td)
{
    global
        $_SESSION;

    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 = "";

    $affselect = TRUE;
    $selectable = TRUE;
    switch ($td) {
        case "del" :
            $affselect = FALSE;
            break;
        case "view" :
            $selectable = FALSE;
            break;
        default :
            break;
    }


    // On se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    // Nombre de techniciens traitants déjà affectés
    $int_publique = 0;
    $rech = "SELECT int_publique FROM `" . TBINT . "` WHERE `int_code` = '$code'";
    $row = $db->get_row($rech);
    if ($row)
        extract($row);

    $result.= '<div align="center">';

    $result .= '<span id="all_tec" class="annot_petit">';
    $result .= _("Publique") . ' ';
    $result.=  '<input type="checkbox" class="annot_petit" name="publique" id ="publique" value="1"';
    if (intval($int_publique) == 1) $result .= ' checked="checked"';
    $result.= '>';
    $result .= '</span>';

    $result.= ' / ';

    // Nombre de techniciens traitants déjà affectés
    $rexist = "SELECT COUNT(*) FROM `" . TBINTT . "` WHERE `intt_codeint` = '$code'";
    $nb_tect = $db->get_var($rexist);

    // Création de la table "choix techniciens"
    $result .= '<span id ="techq" align="center" class="annot_petit">';
    $result .= _("Technicien quelconque") . ' ';
    $result .= '<input type="checkbox" name="tecaff[]" id ="tecaff" value="-1"';
    if ($selectable == TRUE) {
        if ($nb_tect == 0) $result .= ' checked="checked"';
    } else {
        if ($nb_tect == 0) {
            $result .= ' checked="checked"';
            $result .= ' disabled="disabled"';
        }
    }
    $result .= '>';
    $result .= '</span>';
    $result .= '</div>';


    $result .= '<table class="MtableD" border="0" width="100%" cellpadding="0" cellspacing="0">';
    $result .= '<th>';
    $result .= ("Agence");
    $result .= '</th>';
    $result .= '<th>';
    $result .= _("Technicien");
    $result .= '</th>';
    $result .= '<th>';
    $result .= _("Choisir");
    $result .= '</th>';

    if (($affselect == TRUE) AND ($age <> "")) {
        // Création de la liste des Techniciens de l'agence propriétaire et de l'agence responsable du site
        switch ($_SESSION["niv_log"]) {
            case 3 :
                $rech = "SELECT tec_code, tec_codeage, tec_nom, tec_prenom FROM `" . TBTEC . "` WHERE `tec_code` = '" . $_SESSION["code_log"] . "'";
                break;
            case 4 :
                $rech = "SELECT tec_code, tec_codeage, tec_nom,tec_prenom FROM `" . TBTEC . "` WHERE `tec_codeage` = '" . $_SESSION["code_age"] . "'";
                if (($_SESSION["code_age"] <> $ages) AND ($ages <> '-1') AND ($ages <> '') AND ($td <> "add"))
                    $rech .= " OR `tec_codeage` = '" . $ages . "'";
                break;
            default :
                $rech = "SELECT tec_code, tec_codeage, tec_nom, tec_prenom FROM `" . TBTEC . "` WHERE";
                if (($ages <> '-1') AND ($ages <> ''))
                    $rech .= "(";
                $rech .= " `tec_codeage` = '" . $age . "'";
                if (($ages <> '-1') AND ($ages <> ''))
                {
                    $rech .= " OR `tec_codeage` = '" . $ages . "'";
                    $rech .= ")";
                }
                break;
        }
        $rech .= " AND `tec_out` = '0' ORDER BY tec_nom";
        $rows = $db->get_results($rech, ARRAY_A);

        if ($rows) {
            foreach ($rows as $row) {
                $row = encode_str($row);
                extract($row);
                if ($selectable == TRUE) {
                    $result .= '<tr>';
                    $result .= '<td align="center">';
                    $result .= $tec_codeage;
                    $result .= '</td>';
                    $result .= '<td>';
                    $txtdest = $tec_nom . " " . $tec_prenom;
                    $result .= $txtdest;
                    $result .= '</td>';
                    $result .= '<td>';
                    $result .= '<input type="checkbox" name="tecaff[]" id="tecaff" value="' . $tec_code . '"';
                    $rexist = "SELECT COUNT(*) FROM `" . TBINTT . "` WHERE `intt_codetec` = '$tec_code' AND `intt_codeint` = '$code'";
                    $nbres2 = $db->get_var($rexist);
                    if ($nbres2 > 0)
                        $result .= ' checked="checked"';
                    $result .= '>';
                    $result .= '</td>';
                    $result .= '</tr>';
                } else {
                    $rexist = "SELECT COUNT(*) FROM `" . TBINTT . "` WHERE `intt_codetec` = '$tec_code' AND `intt_codeint` = '$code'";
                    $nbres2 = $db->get_var($rexist);
                    if ($nbres2 > 0) {
                        $result .= '<tr>';
                        $result .= '<td align="center">';
                        $result .= $tec_codeage;
                        $result .= '</td>';
                        $result .= '<td>';
                        $txtdest = $tec_nom . " " . $tec_prenom;
                        $result .= $txtdest;
                        $result .= '</td>';
                        $result .= '<td>';
                        $result .= '<input type="checkbox" name="tecaff[]" id="tecaff" value="' . $tec_code . '"';
                        $result .= ' checked="checked"';
                        $result .= ' disabled="disabled"';
                        $result .= '>';
                        $result .= '</td>';
                        $result .= '</tr>';
                    }
                }
            }
        }
    }
    $result .= '</table>';

    $db->disconnect();
    return $result;
}

?>