<?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 des destinataires en fonction type destinataire */
/****************************************************************/

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

function print_plain()
{
    global
    $_SESSION;
    require_once("../lib/gettext/gettext.inc");
    header("Content-type: text/html; charset=UTF-8");
    $tab_tdest = explode("_", $_REQUEST["tdest"]);

    $nivdest = $tab_tdest[0];
    // On teste si destinataire est "moi-même"
    if ($tab_tdest[0] == -1) {
        switch (@$_SESSION["niv_log"]) {
            // Technicien agence
            case 3 :
                $nivdest = 1;
                break;
            // Agence
            case 4 :
                $nivdest = 2;
                break;
            // Centre de gestion
            case 5 :
                $nivdest = 5;
                break;
            // Administrateur
            case 9 :
                $nivdest = 6;
                break;
            // Sous-traitant
            case 10 :
                $nivdest = 4;
                break;
            // Technicien sous-traitant
            case 11 :
                $nivdest = 3;
                break;
        }
    }

    $types = get_types($nivdest, $tab_tdest[1], $tab_tdest[2]);
    $result = [];
    if (count($types) > 0) {
        foreach ($types as $row) {
            switch ($nivdest) {
                // Technicien agence
                case 1 :
                    $result[] = $row["tec_code"] . "=" . $row["tec_nom"] . " " . $row["tec_prenom"] . " (" . $row["tec_codeage"] . ")";
                    break;
                // Agence
                case 2 :
                    $result[] = $row["age_code"] . "=" . $row["age_code"] . " - " . $row["age_resp_nom"] . " " . $row["age_resp_prenom"];
                    break;
                // Technicien sous-traitant
                case 3 :
                    $result[] = $row["tec_code"] . "=" . $row["tec_nom"] . " " . $row["tec_prenom"] . " (" . $row["tec_codest"] . ")";
                    break;
                // Sous-traitant
                case 4 :
                    $result[] = $row["st_code"] . "=" . $row["st_code"] . " - " . $row["st_resp_nom"] . " " . $row["st_resp_prenom"];
                    break;
                // Centre de gestion
                case 5 :
                    $result[] = "CGE="._("Centre de gestion");
                    break;
                // Administrateur
                case 6 :
                    $result[] = "ADM="._("Administrateur");
                    break;
            }
        }
    }
    print implode(";", $result);
}

/**
 * @param $nivdest
 * @param $typrat
 * @param $coderat
 * @return array
 */
function get_types($nivdest, $typrat, $coderat)
{
    global
    $_SESSION;

    require_once("../lib/gettext/gettext.inc");
    include_once("../inc/config.inc.php");
    include_once("../inc/fic_int.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 se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    // On recupere les enregistrements
    $rech = "";
    $result = [];

    // On récupère la liste des possibles
    switch ($nivdest) {
        // Technicien agence
        case 1 :
            $rech = "SELECT * FROM `" . TBTEC . "`";

            // On filtre par niveau
            switch (@$_SESSION["niv_log"]) {
                // Technicien
                case 3 :
                    $rech .= " WHERE tec_code='" . @$_SESSION["code_log"] . "'";
                    break;
                // Agence
                case 4 :
                    $rech .= " WHERE tec_codeage='" . @$_SESSION["code_log"] . "'";
                    break;
                default :
                    break;
            }

            // Filtrage par type de rattachement
            switch ($typrat) {
                // Rien
                case -1 :
                    break;
                // Intervention
                case 1 :
                    // Si code intervention, on ne prend que les techniciens affectés à cette intervention
                    if (trim($coderat) <> "") {
                        $tab_tec = give_tab_rows_tec_toaff_int($coderat, TRUE);
                        if (count($tab_tec) > 0) {
                            // WHERE existe ?
                            if (!stristr($rech, "WHERE "))
                                $rech .= " WHERE ";
                            else
                                $rech .= " AND ";
                            if (count($tab_tec) > 1)
                                $rech .= "(";

                            foreach ($tab_tec as $value) {
                                $rech .= " " . TBTEC . ".tec_code='" . $value . "' OR";
                            }

                            // Suppression du dernier 'OR'
                            $rech = substr($rech, 0, strlen($rech) - 3);

                            if (count($tab_tec) > 1)
                                $rech .= ")";
                        }
                    }
                    break;
                // Contrat de maintenance
                case 2 :
                    break;
                // Contrat de location
                case 3 :
                    break;
                // Dossier de suivi
                case 4 :
                    break;
                // Relevé de parc
                case 5 :
                    break;
                // Bon de travail
                case 6 :
                    break;
                default :
                    break;
            }

            // On ne prend que les techniciens présents
            // WHERE existe ?
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE ";
            else
                $rech .= " AND ";
            $rech .= " tec_out=0";

            // On trie par le nom du technicien
            $rech .= " ORDER BY tec_nom";
            break;

        // Agence
        case 2 :
            $rech = "SELECT * FROM `" . TBAGE . "`";

            // On filtre par niveau
            switch (@$_SESSION["niv_log"]) {

                // Agence
                case 4 :
                    $rech .= " WHERE age_code='" . @$_SESSION["code_log"] . "'";
                    break;
                default :
                    break;
            }

            // Filtrage par type de rattachement
            if (trim($coderat) <> "") {
                switch ($typrat) {
                    // Rien
                    case -1 :
                        break;
                    // Intervention
                    case 1 :
                        // Si code intervention, on ne prend que les agences affectées à cette intervention
                        $tab_age = [];

                        $rech2 = "SELECT int_codeage, int_codeages FROM `" . TBINT . "` WHERE `int_code` = '$coderat'";
                        $row2 = $db->get_row($rech2, ARRAY_A);
                        if ($row2) {
                            extract($row2);
                            $tab_age[] = $int_codeage;
                            if ($int_codeages <> $int_codeage)
                                $tab_age[] = $int_codeages;
                        }

                        if (count($tab_age) > 0) {
                            // WHERE existe ?
                            if (!stristr($rech, "WHERE "))
                                $rech .= " WHERE ";
                            else
                                $rech .= " AND ";
                            if (count($tab_age) > 1)
                                $rech .= "(";

                            foreach ($tab_age as $value) {
                                $rech .= " " . TBAGE . ".age_code='" . $value . "' OR";
                            }

                            // Suppression du dernier 'OR'
                            $rech = substr($rech, 0, strlen($rech) - 3);

                            if (count($tab_age) > 1)
                                $rech .= ")";
                        }
                        break;
                    // Contrat de maintenance
                    case 2 :
                        break;
                    // Contrat de location
                    case 3 :
                        break;
                    // Dossier de suivi
                    case 4 :
                        break;
                    // Relevé de parc
                    case 5 :
                        break;
                    // Bon de travail
                    case 6 :
                        break;
                    default :
                        break;
                }
            }

            // Uniquement agences ouvertes
            // WHERE existe ?
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE ";
            else
                $rech .= " AND ";
            $rech .= " age_ouvert=1";

            // On trie par le code agence
            $rech .= " ORDER BY age_code";
            break;

        // Technicien sous-traitant
        case 3 :
            $rech = "SELECT * FROM `" . TBTEC . "`";

            // On filtre par niveau
            switch (@$_SESSION["niv_log"]) {
                case 11 :
                    $rech .= " WHERE tec_code='" . @$_SESSION["code_log"] . "'";
                    break;
                case 10 :
                    $rech .= " WHERE tec_codest='" . @$_SESSION["code_log"] . "'";
                    break;
                default :
                    break;
            }

            // On ne prend que les techniciens présents
            // WHERE existe ?
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE ";
            else
                $rech .= " AND ";
            $rech .= " tec_out=0";

            // On trie par le nom du technicien
            $rech .= " ORDER BY tec_nom";
            break;

        // Sous-traitant
        case 4 :
            $rech = "SELECT * FROM `" . TBST . "`";

            // On filtre par niveau
            switch (@$_SESSION["niv_log"]) {
                case 10 :
                    $rech = " WHERE st_code ='" . @$_SESSION["code_log"] . "'";
                    break;
                default :
                    break;
            }

            // On ne prend que les agences ouvertes
            // WHERE existe ?
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE ";
            else
                $rech .= " AND ";
            $rech .= " st_ouvert=1";

            // On trie par le code du sous-traitant
            $rech .= " ORDER BY st_code";
            break;

        // Centre de gestion
        case 5 :
            $result[] = "CGE="._("Centre de gestion");
            break;

        // Administrateur
        case 6 :
            $result[] = "ADM="._("Administrateur");
            break;
    }
    if ($rech <> "") {
        $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;
}

?>