<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 1.0.2

Copyright (C) 2006-2015  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 (Planning) */
/****************************************************************/

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;
                // Bons de travail
                case 2 :
                    $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/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");

    $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 :
                    $tab_int = give_tab_int_ok_tec($tab_prm[2], $_SESSION["code_age"], TRUE);

                    $rech = "SELECT * FROM `" . TBINT . "`";
                    if (count($tab_int) > 0) {
                        $rech .= " WHERE ";
                        if (count($tab_int) > 1)
                            $rech .= "(";

                        foreach ($tab_int as $value) {
                            $rech .= " int_code='" . $value . "' OR";
                        }

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

                        if (count($tab_int) > 1)
                            $rech .= ")";
                    }
                    $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 :
                            // On récupère le code agence du technicien
                            $rech2 = "SELECT tec_codeage FROM `" . TBTEC . "`";
                            $rech2 .= " WHERE tec_code='" . $db->escape($tab_prm[2]) . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2) {
                                extract($row2);

                                $tab_int = give_tab_int_ok_tec($tab_prm[2], $tec_codeage, TRUE);

                                if (count($tab_int) > 0) {
                                    $rech = "SELECT * FROM `" . TBINT . "`";
                                    $rech .= " WHERE ";
                                    if (count($tab_int) > 1)
                                        $rech .= "(";

                                    foreach ($tab_int as $value) {
                                        $rech .= " int_code='" . $value . "' OR";
                                    }

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

                                    if (count($tab_int) > 1)
                                        $rech .= ")";
                                    $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;
        // Bon de travail
        case 2 :
            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
        foreach ($rows as $row) {
            $row = encode_str($row);
            $rowint = $row;
            extract($row);
            $rech2 = "";
            // On ne prend que ce qui n'est pas encore planifié
            switch ($tab_prm[0]) {
                // Rien
                case -1 :
                    break;
                // Interventions
                case 1 :
                    $rech2 = "SELECT COUNT(*) FROM `" . TBPEVT . "`";
                    $rech2 .= " WHERE ";
                    $rech2 .= "(pevt_dest='" . $tab_prm[2] . "' AND pevt_coderat='" . $int_code . "')";
                    break;
                // Interventions
                case 2 :
                    break;
            }
            if ($rech2 <> "") {
                $nbres2 = $db->get_var($rech2);
                if ($nbres2 == 0)
                    $result[] = $rowint;
            }
        }
    }
    $db->disconnect();
    return $result;
}

?>