<?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 codes état en fonction type état (Tâches) */
/**************************************************************/

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

function print_plain()
{
    global
    $_SESSION;
    require_once("../lib/gettext/gettext.inc");
    include_once("../inc/func_date.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");

    header("Content-type: text/html; charset=UTF-8");
    $types = get_types($_REQUEST["trat"]);
    $result = [];
    if (count($types) > 0) {
        // On se connecte a la base
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        foreach ($types as $row) {
            switch ($_REQUEST["trat"]) {
                // Interventions
                case 1 :
                    $rech2 = "SELECT cli_ste,cli_nom,cli_prenom FROM `" . TBCLI . "`";
                    $rech2 .= " WHERE cli_code='" . $row["int_codecli"] . "'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2)
                    {
                        $row2 = encode_str($row2);
                        extract($row2);
                    }
                    if (trim($cli_ste) <> "")
                        $txtcli = $cli_ste;
                    else
                        $txtcli = $cli_nom . " " . $cli_prenom;
                    $result[] = $row["int_code"] . "=" . $row["int_code"] . " "._("du")." " . datetolocal($row["int_datedde"]) . " (" . $txtcli . ")";
                    break;
                // Contrats de maintenance
                case 2 :
                    $rech2 = "SELECT cli_ste,cli_nom,cli_prenom FROM `" . TBCLI . "`";
                    $rech2 .= " WHERE cli_code='" . $row["cocl_codecli"] . "'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2)
                    {
                        $row2 = encode_str($row2);
                        extract($row2);
                    }
                    if (trim($cli_ste) <> "")
                        $txtcli = $cli_ste;
                    else
                        $txtcli = $cli_nom . " " . $cli_prenom;
                    $result[] = $row["cocl_code"] . "=" . $row["cocl_code"] . " "._("du")." " . datetolocal($row["cocl_datecrea"]) . " (" . $txtcli . ")";
                    break;
                // Contrats de location
                case 3 :
                    $rech2 = "SELECT cli_ste,cli_nom,cli_prenom FROM `" . TBCLI . "`";
                    $rech2 .= " WHERE cli_code='" . $row["coloc_codecli"] . "'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2)
                    {
                        $row2 = encode_str($row2);
                        extract($row2);
                    }
                    if (trim($cli_ste) <> "")
                        $txtcli = $cli_ste;
                    else
                        $txtcli = $cli_nom . " " . $cli_prenom;
                    $result[] = $row["coloc_code"] . "=" . $row["coloc_code"] . " "._("du")." " . datetolocal($row["coloc_datecrea"]) . " (" . $txtcli . ")";
                    break;
                // Dossiers de suivi
                case 4 :
                    $rech2 = "SELECT cli_ste,cli_nom,cli_prenom FROM `" . TBCLI . "`";
                    $rech2 .= " WHERE cli_code='" . $row["dos_codecli"] . "'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2)
                    {
                        $row2 = encode_str($row2);
                        extract($row2);
                    }
                    if (trim($cli_ste) <> "")
                        $txtcli = $cli_ste;
                    else
                        $txtcli = $cli_nom . " " . $cli_prenom;
                    $result[] = $row["dos_code"] . "=" . $row["dos_code"] . " "._("du")." " . datetolocal($row["dos_datecrea"]) . " (" . $txtcli . ")";
                    break;
                // Relevés de parc
                case 5 :
                    $rech2 = "SELECT cli_ste,cli_nom,cli_prenom FROM `" . TBCLI . "`";
                    $rech2 .= " WHERE cli_code='" . $row["rel_codecli"] . "'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2)
                    {
                        $row2 = encode_str($row2);
                        extract($row2);
                    }
                    if (trim($cli_ste) <> "")
                        $txtcli = $cli_ste;
                    else
                        $txtcli = $cli_nom . " " . $cli_prenom;
                    $result[] = $row["rel_code"] . "=" . $row["rel_code"] . " "._("du")." " . datetolocal($row["rel_datecrea"]) . " (" . $txtcli . ")";
                    break;
                // Bons de travail
                case 6 :
                    $rech2 = "SELECT int_codecli FROM `" . TBINT . "`";
                    $rech2 .= " WHERE int_code='" . $row["ot_codeint"] . "'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2)
                    {
                        $row2 = encode_str($row2);
                        extract($row2);
                    }
                    $rech2 = "SELECT cli_ste,cli_nom,cli_prenom FROM `" . TBCLI . "`";
                    $rech2 .= " WHERE cli_code='" . $int_codecli . "'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2)
                    {
                        $row2 = encode_str($row2);
                        extract($row2);
                    }
                    if (trim($cli_ste) <> "")
                        $txtcli = $cli_ste;
                    else
                        $txtcli = $cli_nom . " " . $cli_prenom;
                    $result[] = $row["ot_code"] . "=" . $row["ot_code"] . " "._("du")." " . datetolocal($row["ot_datedde"]) . " (" . $txtcli . ")";
                    break;
            }
        }
        $db->disconnect();
    }
    print implode(";", $result);
}

/**
 * @param $trat
 * @return array
 */
function get_types($trat)
{
    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");

    $tab_prm = explode("_", $trat);
    // On se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    // On recupere les enregistrements
    $rech = "";
    $result = [];
    switch ($tab_prm[0]) {
        // Rien
        case -1 :
            break;
        // Interventions
        case 1 :
            switch (@$_SESSION["niv_log"]) {
                case 3 :
                case 4 :
                    $rech = "SELECT * FROM `" . TBINT . "`";
                    $rech .= " WHERE (int_codeage='" . @$_SESSION["code_age"] . "' OR int_codeages='" . @$_SESSION["code_age"] . "')";
                    $rech .= " AND int_datefin='0000-00-00'";
                    $rech .= " ORDER BY int_code DESC";
                    break;
                case 10 :
                case 11 :
                    break;
                default :
                    // On filtre en fonction du choix de destinataire
                    switch ($tab_prm[1]) {
                        // Technicien agence
                        case 1 :
                            $rech = "SELECT * FROM `" . TBINT . "`";
                            // On récupère le code agence du technicien
                            $rech2 = "SELECT tec_codeage FROM `" . TBTEC . "`";
                            $rech2 .= " WHERE tec_code='" . $tab_prm[2] . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                                extract($row2);
                            $rech .= " WHERE (int_codeage='" . $tec_codeage . "' OR int_codeages='" . $tec_codeage . "')";
                            $rech .= " AND int_datefin='0000-00-00'";
                            $rech .= " ORDER BY int_code DESC";
                            break;
                        // Agence
                        case 2 :
                            $rech = "SELECT * FROM `" . TBINT . "`";
                            $rech .= " WHERE (int_codeage='" . $tab_prm[2] . "' OR int_codeages='" . $tab_prm[2] . "')";
                            $rech .= " AND int_datefin='0000-00-00'";
                            $rech .= " ORDER BY int_code DESC";
                            break;
                        // Technicien sous-traitant
                        // Sous-traitant
                        case 3 :
                        case 4 :
                            break;
                        // Centre de gestion ou administrateur
                        default :
                            $rech = "SELECT * FROM `" . TBINT . "`";
                            $rech .= " WHERE int_datefin='0000-00-00'";
                            $rech .= " ORDER BY int_code DESC";
                            break;
                    }
                    break;
            }
            break;
        // Contrat de maintenance
        case 2 :
            switch (@$_SESSION["niv_log"]) {
                case 3 :
                case 4 :
                    $rech = "SELECT * FROM `" . TBCOCL . "`";
                    $rech .= " WHERE cocl_codeage='" . @$_SESSION["code_age"] . "'";
                    $rech .= " AND cocl_datefin='0000-00-00'";
                    $rech .= " ORDER BY cocl_code DESC";
                    break;
                case 10 :
                case 11 :
                    break;
                default :
                    // On filtre en fonction du choix de destinataire
                    switch ($tab_prm[1]) {
                        // Technicien agence
                        case 1 :
                            $rech = "SELECT * FROM `" . TBCOCL . "`";
                            // On récupère le code agence du technicien
                            $rech2 = "SELECT tec_codeage FROM `" . TBTEC . "`";
                            $rech2 .= " WHERE tec_code='" . $tab_prm[2] . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                                extract($row2);
                            $rech .= " WHERE cocl_codeage='" . $tec_codeage . "'";
                            $rech .= " AND cocl_datefin='0000-00-00'";
                            $rech .= " ORDER BY cocl_code DESC";
                            break;
                        // Agence
                        case 2 :
                            $rech = "SELECT * FROM `" . TBCOCL . "`";
                            $rech .= " WHERE cocl_codeage='" . $tab_prm[2] . "'";
                            $rech .= " AND cocl_datefin='0000-00-00'";
                            $rech .= " ORDER BY cocl_code DESC";
                            break;
                        // Technicien sous-traitant
                        // Sous-traitant
                        case 3 :
                        case 4 :
                            break;
                        // Centre de gestion ou administrateur
                        default :
                            $rech = "SELECT * FROM `" . TBCOCL . "`";
                            $rech .= " WHERE cocl_datefin='0000-00-00'";
                            $rech .= " ORDER BY cocl_code DESC";
                            break;
                    }
                    break;
            }
            break;
        // Contrat de location
        case 3 :
            switch (@$_SESSION["niv_log"]) {
                case 3 :
                case 4 :
                    $rech = "SELECT * FROM `" . TBCOLOC . "`";
                    $rech .= " WHERE coloc_codeage='" . @$_SESSION["code_age"] . "'";
                    $rech .= " AND coloc_dateclos='0000-00-00'";
                    $rech .= " ORDER BY coloc_code DESC";
                    break;
                case 10 :
                case 11 :
                    break;
                default :
                    // On filtre en fonction du choix de destinataire
                    switch ($tab_prm[1]) {
                        // Technicien agence
                        case 1 :
                            $rech = "SELECT * FROM `" . TBCOLOC . "`";
                            // On récupère le code agence du technicien
                            $rech2 = "SELECT tec_codeage FROM `" . TBTEC . "`";
                            $rech2 .= " WHERE tec_code='" . $tab_prm[2] . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                                extract($row2);
                            $rech .= " WHERE coloc_codeage='" . $tec_codeage . "'";
                            $rech .= " AND coloc_dateclos='0000-00-00'";
                            $rech .= " ORDER BY coloc_code DESC";
                            break;
                        // Agence
                        case 2 :
                            $rech = "SELECT * FROM `" . TBCOLOC . "`";
                            $rech .= " WHERE coloc_codeage='" . $tab_prm[2] . "'";
                            $rech .= " AND coloc_dateclos='0000-00-00'";
                            $rech .= " ORDER BY coloc_code DESC";
                            break;
                        // Technicien sous-traitant
                        // Sous-traitant
                        case 3 :
                        case 4 :
                            break;
                        // Centre de gestion ou administrateur
                        default :
                            $rech = "SELECT * FROM `" . TBCOLOC . "`";
                            $rech .= " WHERE coloc_dateclos='0000-00-00'";
                            $rech .= " ORDER BY coloc_code DESC";
                            break;
                    }
                    break;
            }
            break;
        // Dossier de suivi
        case 4 :
            switch (@$_SESSION["niv_log"]) {
                case 3 :
                case 4 :
                    $rech = "SELECT * FROM `" . TBDOS . "`";
                    $rech .= " WHERE dos_codeage='" . @$_SESSION["code_age"] . "'";
                    $rech .= " AND dos_datefin='0000-00-00'";
                    $rech .= " ORDER BY dos_code DESC";
                    break;
                case 10 :
                case 11 :
                    break;
                default :
                    // On filtre en fonction du choix de destinataire
                    switch ($tab_prm[1]) {
                        // Technicien agence
                        case 1 :
                            $rech = "SELECT * FROM `" . TBDOS . "`";
                            // On récupère le code agence du technicien
                            $rech2 = "SELECT tec_codeage FROM `" . TBTEC . "`";
                            $rech2 .= " WHERE tec_code='" . $tab_prm[2] . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                                extract($row2);
                            $rech .= " WHERE dos_codeage='" . $tec_codeage . "'";
                            $rech .= " AND dos_datefin='0000-00-00'";
                            $rech .= " ORDER BY dos_code DESC";
                            break;
                        // Agence
                        case 2 :
                            $rech = "SELECT * FROM `" . TBDOS . "`";
                            $rech .= " WHERE dos_codeage='" . $tab_prm[2] . "'";
                            $rech .= " AND dos_datefin='0000-00-00'";
                            $rech .= " ORDER BY dos_code DESC";
                            break;
                        // Technicien sous-traitant
                        // Sous-traitant
                        case 3 :
                        case 4 :
                            break;
                        // Centre de gestion ou administrateur
                        default :
                            $rech = "SELECT * FROM `" . TBDOS . "`";
                            $rech .= " WHERE dos_datefin='0000-00-00'";
                            $rech .= " ORDER BY dos_code DESC";
                            break;
                    }
                    break;
            }
            break;
        // Relevé de parc
        case 5 :
            switch (@$_SESSION["niv_log"]) {
                case 3 :
                case 4 :
                    $rech = "SELECT * FROM `" . TBREL . "`";
                    $rech .= " WHERE rel_codeage='" . @$_SESSION["code_age"] . "'";
                    $rech .= " ORDER BY rel_code DESC";
                    break;
                case 10 :
                case 11 :
                    break;
                default :
                    // On filtre en fonction du choix de destinataire
                    switch ($tab_prm[1]) {
                        // Technicien agence
                        case 1 :
                            $rech = "SELECT * FROM `" . TBREL . "`";
                            // On récupère le code agence du technicien
                            $rech2 = "SELECT tec_codeage FROM `" . TBTEC . "`";
                            $rech2 .= " WHERE tec_code='" . $tab_prm[2] . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                                extract($row2);
                            $rech .= " WHERE rel_codeage='" . $tec_codeage . "'";
                            $rech .= " ORDER BY rel_code DESC";
                            break;
                        // Agence
                        case 2 :
                            $rech = "SELECT * FROM `" . TBREL . "`";
                            $rech .= " WHERE rel_codeage='" . $tab_prm[2] . "'";
                            $rech .= " ORDER BY rel_code DESC";
                            break;
                        // Technicien sous-traitant
                        // Sous-traitant
                        case 3 :
                        case 4 :
                            break;
                        // Centre de gestion ou administrateur
                        default :
                            $rech = "SELECT * FROM `" . TBREL . "`";
                            $rech .= " ORDER BY rel_code DESC";
                            break;
                    }
                    break;
            }
            break;
        // Bon de travail
        case 6 :
            switch (@$_SESSION["niv_log"]) {
                case 3 :
                case 4 :
                    $rech = "SELECT * FROM `" . TBOT . "`, `" . TBINT . "`";
                    $rech .= " WHERE (" . TBOT . ".ot_codeint=" . TBINT . ".int_code";
                    $rech .= " AND " . TBINT . ".int_codeage='" . @$_SESSION["code_age"] . "'";
                    $rech .= " AND " . TBOT . ".ot_datefin='0000-00-00')";
                    $rech .= " ORDER BY ot_code DESC";
                    break;
                case 10 :
                case 11 :
                    $rech = "SELECT * FROM `" . TBOT . "`";
                    $rech .= " WHERE ot_codest='" . @$_SESSION["code_st"] . "'";
                    $rech .= " AND ot_datefin='0000-00-00'";
                    $rech .= " ORDER BY ot_code DESC";
                    break;
                default :
                    // On filtre en fonction du choix de destinataire
                    switch ($tab_prm[1]) {
                        // Technicien agence
                        case 1 :
                            // $rech = "SELECT * FROM `" . TBOT . "`";
                            // On récupère le code agence du technicien
                            $rech2 = "SELECT tec_codeage FROM `" . TBTEC . "`";
                            $rech2 .= " WHERE tec_code='" . $tab_prm[2] . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                                extract($row2);
                            $rech = "SELECT * FROM `" . TBOT . "`, `" . TBINT . "`";
                            $rech .= " WHERE (" . TBOT . ".ot_codeint=" . TBINT . ".int_code";
                            $rech .= " AND " . TBINT . ".int_codeage='" . $tec_codeage . "'";
                            $rech .= " AND " . TBOT . ".ot_datefin='0000-00-00')";
                            $rech .= " ORDER BY ot_code DESC";
                            break;
                        // Agence
                        case 2 :
                            $rech = "SELECT * FROM `" . TBOT . "`, `" . TBINT . "`";
                            $rech .= " WHERE (" . TBOT . ".ot_codeint=" . TBINT . ".int_code";
                            $rech .= " AND " . TBINT . ".int_codeage='" . $tab_prm[2] . "'";
                            $rech .= " AND " . TBOT . ".ot_datefin='0000-00-00')";
                            $rech .= " ORDER BY ot_code DESC";
                            break;
                        // Technicien sous-traitant
                        case 3 :
                            $rech = "SELECT * FROM `" . TBOT . "`";
                            // On récupère le code sous-traitant du technicien
                            $rech2 = "SELECT tec_codest FROM `" . TBTEC . "`";
                            $rech2 .= " WHERE tec_code='" . $tab_prm[2] . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                                extract($row2);
                            $rech .= " WHERE ot_codest='" . $tec_codest . "'";
                            $rech .= " AND ot_datefin='0000-00-00'";
                            $rech .= " ORDER BY ot_code DESC";
                            break;
                        // Sous-traitant
                        case 4 :
                            $rech = "SELECT * FROM `" . TBOT . "`";
                            $rech .= " WHERE ot_codest='" . $tab_prm[2] . "'";
                            $rech .= " AND ot_datefin='0000-00-00'";
                            $rech .= " ORDER BY ot_code DESC";
                            break;
                        // Centre de gestion ou administrateur
                        default :
                            $rech = "SELECT * FROM `" . TBOT . "`";
                            $rech .= " WHERE ot_datefin='0000-00-00'";
                            $rech .= " ORDER BY ot_code DESC";
                            break;
                    }
                    break;
            }
            break;
    }
    if ($rech <> "") {
        $rows = $db->get_results($rech, ARRAY_A);
        // On cree le resultat
        $result = [];
        foreach ($rows as $row) {
            $row = encode_str($row);
            $result[] = $row;
        }
    }
    $db->disconnect();
    return $result;
}

?>