<?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 tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], "inc.php");
if ($pos === FALSE)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");

include_once("../inc/config.inc.php");
include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_pw.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_tec.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_log.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/lst.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");
Archivelog(@$_SESSION["code_log"]);
check_modules();
$_SESSION["typ_filtres"] = "tec";
$_SESSION['tab_tec_sel'] = [];
$_SESSION['tab_sites_sel'] = [];

$pbm_tec = FALSE;

// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$tl = intval(@$_GET['tl']); // Type de liste
$td = @$_GET['td']; // Type de type d'affichage

if (($tl < 1) OR ($tl > 3))
    $pbm_tec = TRUE;

if (($td <> "std") AND ($td <> "loc"))
    $pbm_tec = TRUE;

if ($pbm_tec == FALSE) {
    switch (@$_SESSION["niv_log"]) {
        case 4 :
            $row = lit_enr_age(@$_SESSION["code_age"], TRUE);
            if (is_array($row)) {
                $row = encode_str($row);
                extract($row);
                if ($td == "std")
                    $tl = 1;
            } else
                $pbm_tec = TRUE;
            break;

        case 6 :
            $row = lit_enr_do(@$_SESSION["code_do"]);
            if (is_array($row)) {
                $row = encode_str($row);
                extract($row);
                $tl = 2;
            } else
                $pbm_tec = TRUE;
            break;

        case 10 :
            $row = lit_enr_st(@$_SESSION["code_st"]);
            if (is_array($row)) {
                $row = encode_str($row);
                extract($row);
                $tl = 3;
            } else
                $pbm_tec = TRUE;
            break;
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>SMI - <?PHP echo _("ESPACE PRIVE"); ?> : <?PHP echo _("Liste Techniciens"); ?></title>

    <?PHP include_once("../inc/header.inc.php"); ?>

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            init_autohref_classe('falselnkimg','<?PHP echo str_escape(_("Merci de confirmer<br />Sinon, cliquez sur 'Annuler'")); ?>');
            Focus_first();
        }

        $(document).ready(function () {
            init_all();
        });
        //-->
    </script>
</head>

<body>
<?php include_once("../inc/entete.inc.php"); ?>
<p align="center"><span class="titrepage">
<?PHP
echo _("TECHNICIENS");
switch ($tl) {
    case 1 :
        echo " " . _("AGENCES");
        break;
    case 2 :
        echo " " . _("DONNEURS D'ORDRES");
        break;
    case 3 :
        echo " " . _("SOUS-TRAITANTS");
        break;
    default :
        break;
}
?>
</span></p>
<?PHP
if ($td == "loc") {
    echo "<p align=\"center\"><span class=\"annot_gras_it\">";
    echo _("Pour location");
    echo "</span></p>";
}

if ($pbm_tec == FALSE) {
    if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9) OR (((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 10) OR (@$_SESSION["niv_log"] == 6)) AND ($td == "std"))) {
        $fic_imp = "tec_lsti.php?tl=" . $tl;
        $url_add = "tec_maj.php?td=add&code=&tl=" . $tl;
    } else {
        $fic_imp = "";
        $url_add = "";
    }

    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    $rech = cree_strrechlsttec(@$_SESSION["niv_log"]);

    switch ($_SESSION["niv_log"]) {
        case 4 :
            if ($td == "std") {
                if (!stristr($rech, "OR "))
                    $rech .= " WHERE tec_codeage = '" . @$_SESSION["code_age"] . "'";
                else
                    $rech .= " AND tec_codeage = '" . @$_SESSION["code_age"] . "'";
                $rech .= crestr_triord($tec_titre_col, "tec_codeage", FALSE, $limit);
            } else {
                switch ($tl) {
                    // Filtrage sur techniciens agences
                    case 1 :
                        if (!stristr($rech, "OR "))
                            $rech .= " WHERE tec_codeage <> ''";
                        else
                            $rech .= " AND tec_codeage <> ''";
                        $rech .= crestr_triord($tec_titre_col, "tec_codeage", FALSE, $limit);
                        break;
                    // Filtrage sur techniciens sous-traitant
                    case 3 :
                        if (!stristr($rech, "OR "))
                            $rech .= " WHERE tec_codest <> ''";
                        else
                            $rech .= " AND tec_codest <> ''";
                        $rech .= crestr_triord($tec3_titre_col, "tec_codest", FALSE, $limit);
                        break;
                }
            }
            break;
        case 6 :
            if (!stristr($rech, "OR "))
                $rech .= " WHERE tec_codedo = '" . @$_SESSION["code_do"] . "'";
            else
                $rech .= " AND tec_codedo = '" . @$_SESSION["code_do"] . "'";
            $rech .= crestr_triord($tec2_titre_col, "tec_codedo", FALSE, $limit);
            break;
        case 10 :
            if (!stristr($rech, "OR "))
                $rech .= " WHERE tec_codest = '" . @$_SESSION["code_st"] . "'";
            else
                $rech .= " AND tec_codest = '" . @$_SESSION["code_st"] . "'";
            $rech .= crestr_triord($tec3_titre_col, "tec_codest", FALSE, $limit);
            break;
        case 5 :
        case 9 :
            switch ($tl) {
                case 1 :
                    if (!stristr($rech, "OR "))
                        $rech .= " WHERE tec_codeage LIKE 'A%'";
                    else
                        $rech .= " AND tec_codeage LIKE 'A%'";
                    $rech .= crestr_triord($tec_titre_col, "tec_codeage", FALSE, $limit);
                    break;
                case 2 :
                    if (!stristr($rech, "OR "))
                        $rech .= " WHERE tec_codedo LIKE 'D%'";
                    else
                        $rech .= " AND tec_codedo LIKE 'D%'";
                    $rech .= crestr_triord($tec2_titre_col, "tec_codedo", FALSE, $limit);
                    break;
                case 3 :
                    if (!stristr($rech, "OR "))
                        $rech .= " WHERE tec_codest LIKE 'S%'";
                    else
                        $rech .= " AND tec_codest LIKE 'S%'";
                    $rech .= crestr_triord($tec3_titre_col, "tec_codest", FALSE, $limit);
                    break;
                default :
                    break;
            }
            break;
        default :
            break;
    }

    $rows = $db->get_results($rech, ARRAY_A);
    $db->disconnect();

    if ($rows) {
        $numrows = count($rows);
        include_once("../inc/formr.inc.php");

        switch ($_SESSION["niv_log"]) {
            case 4 :
                if ($td == "std")
                    $rech = modstr_triord($rech, $tec_titre_col, "tec_codeage", TRUE, $limit);
                else {
                    switch ($tl) {
                        // Filtrage sur techniciens agences
                        case 1 :
                            $rech = modstr_triord($rech, $tec_titre_col, "tec_codeage", TRUE, $limit);
                            break;
                        // Filtrage sur techniciens sous-traitant
                        case 3 :
                            $rech = modstr_triord($rech, $tec3_titre_col, "tec_codest", TRUE, $limit);
                            break;
                    }
                }
                break;
            case 6 :
                $rech = modstr_triord($rech, $tec2_titre_col, "tec_codedo", TRUE, $limit);
                break;
            case 10 :
                $rech = modstr_triord($rech, $tec3_titre_col, "tec_codest", TRUE, $limit);
                break;
            case 5 :
            case 9 :
                switch ($tl) {
                    case 1 :
                        $rech = modstr_triord($rech, $tec_titre_col, "tec_codeage", TRUE, $limit);
                        break;
                    case 2 :
                        $rech = modstr_triord($rech, $tec2_titre_col, "tec_codedo", TRUE, $limit);
                        break;
                    case 3 :
                        $rech = modstr_triord($rech, $tec3_titre_col, "tec_codest", TRUE, $limit);
                        break;
                    default :
                        break;
                }
                break;
            default :
                break;
        }

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

        $rows = $db->get_results($rech, ARRAY_A);

        // *** Pagination de pied
        pagine_lst($numrows, $limit);


        // *** Affichage de la table
        echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";

        // création entête
        switch ($tl) {
            case 1 :
                titlst($tec_titre_col);
                break;
            case 2 :
                titlst($tec2_titre_col);
                break;
            case 3 :
                titlst($tec3_titre_col);
                break;
            default :
                break;
        }
        echo "<th>" . _("Cliquer") . "</th>\n";

        $i = 0;

        foreach ($rows as $row) {
            $row = encode_str($row);
            // création de l'entête de ligne (TR)
            creetrlst($i);

            echo "<td>";
            switch ($tl) {
                case 1 :
                    echo $row["tec_codeage"];
                    break;
                case 2 :
                    echo $row["tec_codedo"];
                    break;
                case 3 :
                    echo $row["tec_codest"];
                    break;
                default :
                    echo "&nbsp;";
                    break;
            }
            echo "</td>\n";
            echo "<td>" . $row["tec_code"];

            // icône du planning
            if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 10)) {
                // On vérifie si le module planning est bien en place
                if ($mod_planning == TRUE) {
                    if (last_id_tecpr($row["tec_code"], FALSE) <> 0)
                    {
                        $url_planning = 'plan_maj.php?typd=1&coded=' . $row["tec_code"] . '&typr=-1&tx=tecl&from=tecl';
                        cre_falselnk_img('', 'pla_edit_'.$row["tec_id"], 'span', 'center', _("Planning/Agenda"), 'lnkpop', $url_planning , '../img/planning.png',1020,760);
                    }
                }
            }

            // CREATION d'icône si manque prix de revient
            if (((@$_SESSION["niv_log"] == 4) AND (@$_SESSION["code_age"] == $row["tec_codeage"])) OR (@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 10)) {
                if (last_id_tecpr($row["tec_code"], FALSE) == 0)
                /*if (nb_tecpr($row["tec_code"], FALSE) == 0)*/
                    echo ' <img src="../img/warn.png" title ="'._("Manque prix de revient").'" border="0" align="absmiddle">';

                // CREATION d'icône en cas de congés
                if ((comp_date($row["tec_congd"], $datea) == TRUE) AND (comp_date($datea, $row["tec_congf"]) == TRUE)) {
                    $lst_vac = TRUE;

                    echo ' <img src="../img/vac.png" title ="'._("Congés du").' ' . datetolocal($row["tec_congd"]) . ' '._("au").' ' . datetolocal($row["tec_congf"]) . '" border="0" align="absmiddle">';
                }

                // CREATION d'icône si le technicien n'est plus salarié
                if ($row["tec_out"] == TRUE) {
                    $lst_lic = TRUE;
                    echo ' <img src="../img/out.png" title ="'._("N'est plus salarié depuis le").' '  . datetolocal($row["tec_dateout"]) . '" border="0" align="absmiddle">';
                }

                // Verrouillé ?
                if (account_locked($row["tec_code"], FALSE) == TRUE) {
                    echo ' <img src="../img/locked.png" title="' . _("Compte désactivé") . '" alt="' . _("Compte désactivé") . '" border="0" align="absmiddle">';
                    $locked = TRUE;
                }
            }

            echo "</td>\n";
            echo "<td>" . $row["tec_prenom"] . "</td>\n";
            echo "<td>" . $row["tec_nom"] . "</td>\n";
            echo "<td>" . $row["tec_telf"] . "</td>\n";
            echo "<td>" . $row["tec_telp"] . "</td>\n";

            // CREATION lien dynamique sur email
            echo "<td>";
            echo "<a href=\"mailto:" . $row["tec_email"] . "\">" . $row["tec_email"] . "</a>";
            echo "</td>\n";

            // CREATION lien dynamique sur icône (édition, suppression, etc...)

            echo "<td align=\"center\">";

            if (((@$_SESSION["niv_log"] == 4) AND (@$_SESSION["code_age"] == $row["tec_codeage"])) OR ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 10) OR (@$_SESSION["niv_log"] == 6))) {
                if ($row["tec_out"] == FALSE) {
                    if (url_ok('tec_maj.php?td=edit', FALSE) == TRUE) {
                        $lst_maj = TRUE;
                        cre_falselnk_img('', 'tec_edit_'.$row["tec_id"], 'span', 'center', _("Edition"), 'lnk', 'tec_maj.php?td=edit&code=' . $row["tec_code"] . '&dep=' . $row["tec_dep"] . '&tl=' . $tl, '../img/edit.png');
                    }
                } else {
                    if (url_ok('tec_maj.php?td=view', FALSE) == TRUE) {
                        $lst_voir = TRUE;
                        cre_falselnk_img('', 'tec_view_'.$row["tec_id"], 'span', 'center', _("Visualisation"), 'lnk', 'tec_maj.php?td=edit&code=' . $row["tec_code"] . '&dep=' . $row["tec_dep"] . '&tl=' . $tl, '../img/eye.png');
                    }
                }
            }

            if (((@$_SESSION["niv_log"] == 4) AND (@$_SESSION["code_age"] == $row["tec_codeage"])) OR ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9))) {
                if (url_ok('coloc_maj.php?td=add', FALSE) == TRUE) {
                    $lst_coloc = TRUE;
                    cre_falselnk_img('', 'coloc_add_'.$row["tec_id"], 'span', 'center', _("Ajout contrat location"), 'lnk', 'coloc_maj.php?td=add&tc=2&co=&cc=' . $row["tec_code"] . '&ca=' , '../img/coloc.png');
                }
            }

            if (((@$_SESSION["niv_log"] == 4) AND (@$_SESSION["code_age"] == $row["tec_codeage"])) OR ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 10) OR (@$_SESSION["niv_log"] == 6))) {
                $urlimp = "tec_maji.php?code=" . $row["tec_code"];
                if (url_ok($urlimp, FALSE) == TRUE) {
                    $lst_imp = TRUE;
                    cre_falselnk_img('', 'tec_prn_'.$row["tec_id"], 'span', 'center', _("Impression"), 'lnkpop', $urlimp , '../img/print.png');
                }
            }

            if ($row["tec_out"] == TRUE) {
                if (@$_SESSION["niv_log"] == 9) {
                    $lst_reopen = TRUE;
                    cre_falselnk_img('', 'tec_reopen_'.$row["tec_id"], 'span', 'center', _("Réouverture compte"), 'lnkwpop', 'tec_reopen.php?ct=' . $row["tec_code"] , '../img/reopen.png',900,400);
                }
            }
            else
            {
                if (((@$_SESSION["niv_log"] == 4) AND (@$_SESSION["code_age"] == $row["tec_codeage"])) OR ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9) OR (@$_SESSION["niv_log"] == 10) OR (@$_SESSION["niv_log"] == 6))) {
                    if (url_ok('tec_maj.php?td=del', FALSE) == TRUE) {
                        $lst_del = TRUE;
                        cre_falselnk_img('', 'tec_del_'.$row["tec_id"], 'span', 'center', _("Suppression"), 'lnk', 'tec_maj.php?td=del&code=' . $row["tec_code"] . '&dep=' . $row["tec_dep"] . '&tl=' . $tl, '../img/del.png');
                    }
                }
            }


            echo "</td>\n";

            echo "</tr>\n";

            $i++;
        }
        echo "</table>\n";
        // *** Fin affichage de la table

        $db->disconnect();

        if ($locked == TRUE) {
            echo ' <img src="../img/locked.png" title="' . _("Compte désactivé") . '" alt="' . _("Compte désactivé") . '" border="0" align="absmiddle">';
            echo " : " . _("Compte désactivé");
        }

        // *** Pagination de pied
        pagine_lst($numrows, $limit);

        $taille_piedla = "";
        include_once("../inc/piedla.inc.php");
    } else
        retpp();
} else
    pop_ret_auto(_("Problème de paramètres"), 'warn', "index.php");
include_once("pied.php");
?>
</body>
</html>