<?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
 ****************************************************************************************/


// 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/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_tra.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"]);
$_SESSION["typ_filtres"] = "tra";

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

$pbm_tra = FALSE;

// Récupération origine appel
// 'm' : depuis menu 
// 'r' : depuis un rattachement
$tx='';
if (isset($_GET["tx"]))
    $tx = $_GET['tx'];
if (($tx <> "m") AND ($tx <> "r"))
    $pbm_tra = TRUE;

// Récupération type liste
// 'i' : interventions
// 'c' : clients
// 'd' : donneurs d'ordres
$cr = '';
$tl = $_GET['tl'];
if (($tl <> "i") AND ($tl <> "c") AND ($tl <> "d"))
    $pbm_tra = TRUE;
else {
    // Récupération code pièce rattachée
    if ($tx == "r") {
        if (isset($_GET["cr"]))
            $cr = $_GET["cr"];
        else
            $pbm_tra = TRUE;
    }
}

if ($pbm_tra == FALSE) {
    // Récupération de la pièce rattachée en fonction du code pièce rattachée
    if ($tx == "r") {
        switch ($tl) {
            // Interventions
            case "i" :
                $row = lit_enr_int($cr);
                if (is_array($row)) {
                    $row = encode_str($row);
                    extract($row);
                } else
                    $pbm_tra = TRUE;
                break;
            // Clients
            case "c" :
                $row = lit_enr_cli($cr, TRUE);
                if (is_array($row)) {
                    $row = encode_str($row);
                    extract($row);
                } else
                    $pbm_tra = TRUE;
                break;
            // Donneurs d'ordres
            case "d" :
                $row = lit_enr_do($cr, TRUE);
                if (is_array($row)) {
                    $row = encode_str($row);
                    extract($row);
                } else
                    $pbm_tra = TRUE;
                break;
            default :
                break;
        }
    }
}

switch (@$_SESSION["niv_log"]) {
    case 1 :
    case 3 :
    case 4 :
    case 6 :
    case 7 :
        $pbm_tra = 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 _("Traçabilité"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            init_autohref_classe('falselnkimg','');
            add_func_click('quit', 'sclose', '');
            add_func_click('quit2', 'sclose', '');
            Focus_first();
        }

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

</head>

<body>

<?php
if ($tx == "m")
    include_once("../inc/entete.inc.php");

if ($pbm_tra == FALSE) {
    $nblig_aff = 3;
    $nbcarac_lig = 50;
    $titre = "";
    if ($cr == "") {
        switch ($tl) {
            case "i" :
                $titre = _("TRACABILITE INTERVENTIONS");
                break;
            case "c" :
                $titre = _("TRACABILITE CLIENTS");
                break;
            case "d" :
                $titre = _("TRACABILITE DONNEURS D'ORDRES");
                break;
            default :
                break;
        }
    } else {
        switch ($tl) {
            case "i" :
                $titre = _("TRACABILITE INTERVENTION");
                break;
            case "c" :
                $titre = _("TRACABILITE CLIENT");
                break;
            case "d" :
                $titre = _("TRACABILITE DONNEUR D'ORDRES");
                break;
            default :
                break;
        }
        $titre .= " " . $cr;
    }
    echo '<p align="center"><span class="titrepage">' . $titre . '</span></p>';

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

    $rech = cree_strrechlsttra($tx);

    // Si depuis code : on filtre sur code
    if (trim($cr) <> "") {
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE ";
        else
            $rech .= " AND ";
        $rech .= " " . TBTRA . ".tra_coderat='" . $cr . "'";
    }
    // Si listing général : on filtre sur type
    if (trim($tl) <> "") {
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE ";
        else
            $rech .= " AND ";
        switch ($tl) {
            case "i" :
                $rech .= " " . TBTRA . ".tra_typrat=1";
                break;
            case "c" :
                $rech .= " " . TBTRA . ".tra_typrat=2";
                break;
            case "d" :
                $rech .= " " . TBTRA . ".tra_typrat=3";
                break;
        }
    }

    switch ($tx) {
        case "r" :
            $rech .= crestr_triord($tra_titre_col_rat, "tra_date DESC, tra_heure DESC", FALSE, $limit);
            break;
        default :
            $rech .= crestr_triord($tra_titre_col, "tra_coderat, tra_date DESC, tra_heure DESC", FALSE, $limit);
            break;
    }

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

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

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

        switch ($tx) {
            case "r" :
                $rech = modstr_triord($rech, $tra_titre_col_rat, "tra_date DESC, tra_heure DESC", TRUE, $limit);
                break;
            default :
                $rech = modstr_triord($rech, $tra_titre_col, "tra_coderat, tra_date DESC, tra_heure DESC", TRUE, $limit);
                break;
        }

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

        if ($tx <> "m") {
            echo '<div align="center">';
            echo '<input name="quit" type="button" class="bton_std" id="quit" value="' . _("Quitter") . '">';
            echo '</div>';
            echo '<br />';
        }

        // *** Pagination de début
        pagine_lst($numrows, $limit);

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

        switch ($tx) {
            case "r" :
                titlst($tra_titre_col_rat);
                break;
            default :
                titlst($tra_titre_col);
                break;
        }

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

            switch ($tx) {
                case "r" :
                    break;
                default :
                    // Code pièce rattachée
                    echo '<td align="center">';
                    echo $row["tra_coderat"];
                    echo '</td>';
                    break;
            }

            // Date
            echo '<td align="center">';
            echo datetolocal($row["tra_date"]);
            echo '</td>';

            // Heure
            echo '<td align="center">';
            echo $row["tra_heure"];
            echo '</td>';

            // Opération
            echo '<td align="center">';
            $typeaction = $row["tra_codeta"];

            $rech2 = "SELECT * FROM `" . TBTA . "` WHERE ta_code = '" . $typeaction . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);

            if ($row2) {
                $row2 = encode_str($row2);
                extract($row2);
                $title = $ta_desc . " (" . $typeaction . ")";
                echo '<img src="' . $ta_img . '" title="' . $title . '" border="0" align="absmiddle"';
            }
            echo '</td>';

            // Titre et détail
            echo '<td>';
            echo '<div>';
            echo '<span class="annot_petit_gras_soul">';
            echo $row["tra_titre"];
            echo '</span>';
            $row2 = lit_trace_mail($row["tra_id"], FALSE);
            if (is_array($row2)) {
                $row2 = encode_str($row2);
                $urldetail = 'mailint_detail.php?id=' . $row["tra_id"];
                if (url_ok($urldetail, FALSE) == TRUE)
                    cre_falselnk_img('', 'tra_det_'.$row["tra_id"], 'span', 'center', _("Détail"), 'lnkpop', $urldetail, '../img/eye.png',700,500);
            }
            echo '</div>';
            $txt_detail1 = "";
            $txt_detail2 = "";

            // On calcule le nombre de lignes totales du détail
            $tab_lig = cesuretxt_tab($row["tra_desc"], $nbcarac_lig);
            if (count($tab_lig) > $nblig_aff) {
                foreach ($tab_lig as $index => $value) {
                    if ($index < 3) {
                        $txt_detail1 .= $value;
                        if ($index <> 2)
                            $txt_detail1 .= "<br />";
                    }
                    $txt_detail2 .= $value;
                    if ($index <> (count($tab_lig) - 1))
                        $txt_detail2 .= "<br />";
                }
                $txt_detail1 .= '<div align="right"><a href="#" class="popa"><img src="../img/expand.png" alt="'._("Détail").'" border="0" align="absmiddle" onclick="show_hide(\''.$row["tra_id"] . '_z2\',\''.$row["tra_id"] . '_z1\')"><span>' . _("Détail") . '</span></a></div>';
                $txt_detail2 .= '<div align="right"><a href="#" class="popa"><img src="../img/reduce.png" alt="'._("Réduire").'" border="0" align="absmiddle" onclick="show_hide(\''.$row["tra_id"] . '_z1\',\''.$row["tra_id"] . '_z2\')"><span>' . _("Réduire") . '</span></a></div>';

                echo '<div id="' . $row["tra_id"] . '_z1" class="annot_petit" style="display:block;margin-left:5px;padding-top:2px;">';
                echo $txt_detail1;
                echo "</div>";
                echo '<div id="' . $row["tra_id"] . '_z2" class="annot_petit" style="display:none;margin-left:5px;padding-top:2px;">';
                echo $txt_detail2;
                echo "</div>";

            } else {
                foreach ($tab_lig as $index => $value) {
                    echo $value;
                    if ($index <> 2)
                        echo "<br />";
                }
            }
            echo '</td>';

            // Utilisateur
            echo '<td align="center">';
            echo $row["tra_codeutil"];
            echo '</td>';

            echo "</tr>\n";

            $i++;
        }

        echo "</table>\n";
        // *** Fin affichage de la table

        $db->disconnect();

        if ($tx <> "m") {
            echo '<br /><div align="center">';
            echo '<input name="quit2" type="button" class="bton_fgris_petit" id="quit2" value="' . _("Quitter") . '">';
            echo '</div>';
        }
    } else
        retpp();
} else
    pop_ret_auto(_("Problème de paramètres"), 'warn', "index.php");

if ($tx == "m")
    include_once("pied.php");
?>
</body>
</html>