<?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/fic_droits.inc.php");
include_once("../inc/fic_mque.inc.php");
include_once("../inc/fic_ret.inc.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/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"]);
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$_SESSION["typ_filtres"] = "retp";

$td = "";
if (isset($_GET['td']))
    $td = $_GET['td']; // Récupération type demande
$tx = "";
if (isset($_GET['tx']))
    $tx = $_GET['tx']; // Récupération source appel (tx=e -> appel depuis fiche de renvoi)
$cm = "";
if (isset($_GET['cm']))
    $cm = $_GET['cm']; // Récupération code marque
$ce = "";
if (isset($_GET['ce']))
    $ce = $_GET['ce']; // Récupération code envoi
$ca = "";
if (isset($_GET['ca']))
    $ca = $_GET['ca']; // Récupération code agence
$fi = "";
if (isset($_GET['fi']) && (trim($_GET['fi']) <> ""))
    $fi = intval($_GET['fi']);

$pbm_retp = FALSE;
if (($cm == "") AND ($tx == "e"))
    $pbm_retp = TRUE;

if ($cm <> "") {
    $row = lit_enr_mque($cm, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
    } else
        $pbm_retp = TRUE;
}

if (($ce == "") AND ($tx == "e"))
    $pbm_retp = TRUE;
if (($ca == "") AND ($tx == "e"))
    $pbm_retp = TRUE;

?>
<!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 Produits à Retourner"); ?></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', '');
            Focus_first();
        }

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

<body>

<?PHP
if ($pbm_retp == FALSE) {
    if ($tx <> "e") include_once("../inc/entete.inc.php");
    echo '<p align="center"><span class="titrepage">' . _("LISTE DES PRODUITS A RETOURNER") . '</span></p>';
    if ($tx == "e") {
        echo '<div align="center" class="annot_std_gras">' . _("Pour le bon de renvoi") . ' ' . $ce . '</div>';
        echo '<br />';
    } else {
        $fic_imp = "retp_lsti.php";
        if (@$_SESSION["niv_log"] <> 3)
            $url_add = "retp_maj.php?td=add&cr=&ca=";
    }
    $tab_fi[] = _("Tous les produits");
    $tab_fi[] = _("Demandes non effectuées");
    $tab_fi[] = _("Envois non effectués");
    $tab_fi[] = _("Non traités");

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

    if ($tx == "e")
        $rech = cree_strrechlstretprma();
    else
        $rech = cree_strrechlstretp();

    if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) {
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE " . TBRETP . ".retp_codeage='" . @$_SESSION["code_age"] . "'";
        else
            $rech .= " AND " . TBRETP . ".retp_codeage='" . @$_SESSION["code_age"] . "'";
    }

    switch ($fi) {
        case "1" :
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE retp_datedde='0000-00-00'";
            else
                $rech .= " AND retp_datedde='0000-00-00'";
            break;
        case "2" :
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE retp_dateenv='0000-00-00'";
            else
                $rech .= " AND retp_dateenv='0000-00-00'";
            break;
        case "3" :
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE retp_dateret='0000-00-00'";
            else
                $rech .= " AND retp_dateret='0000-00-00'";
            break;
        default :
            break;
    }

    if ($tx == "e")
        $rech .= crestr_triord($retpe_titre_col, "retp_code", FALSE, $limit);
    else
        $rech .= crestr_triord($retp_titre_col, "retp_code", FALSE, $limit);
    $rows = $db->get_results($rech, ARRAY_A);
    $db->disconnect();

    // *** Pagination d'entête
    pagine_lst($numrows, $limit);

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

        if ($tx == "e")
            $rech = modstr_triord($rech, $retpe_titre_col, "retp_code", FALSE, $limit);
        else
            $rech = modstr_triord($rech, $retp_titre_col, "retp_code", FALSE, $limit);

        $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
        if ($tx == "e")
            titlst($retpe_titre_col);
        else
            titlst($retp_titre_col);
        echo "<th>" . _("Cliquer") . "</th>\n";


        $i = 0;


        foreach ($rows as $row) {
            $row = encode_str($row);
            // Affichage liste produit à retourner

            // création de l'entête de ligne (TR)
            creetrlst($i);

            echo "<td>";
            echo $row["retp_code"];
            echo "</td>\n";
            echo "<td>";
            echo "<div align=\"center\">";
            echo datetolocal($row["retp_datecrea"]);
            echo "<span class=\"annot_petit_it\">";
            $row2 = lit_enr_retr($row["retp_code"], FALSE);
            if (is_array($row2)) {
                $row2 = encode_str($row2);
                if ($row2["retr_clos"] == 1) {
                    $lst_retps = TRUE;
                    echo ' <img src="../img/ok.png" title ="' . _("Traité le") . '"' . datetolocal($row2["retr_dateret"]) . '" border="0" align="absmiddle">';
                }
            } else {
                if ($row["retp_codeenv"] <> "") {
                    $row2 = lit_enr_rete($row["retp_codeenv"], FALSE);
                    if (is_array($row2)) {
                        $row2 = encode_str($row2);
                        if (datetolocal($row2["rete_dateenv"]) <> "")
                            echo '<br />' . _("Envoyé le") . ' ' . datetolocal($row2["rete_dateenv"]);
                        else
                            echo '<br />' . _("Prêt à partir");
                    }
                } else {
                    if (datetolocal($row["retp_datedde"]) <> "") {
                        echo '<br />' . _("Demande le") . ' ' . datetolocal($row["retp_datedde"]);
                        $row2 = lit_enr_retd($row["retp_code"], FALSE);
                        if (is_array($row2)) {
                            $row2 = encode_str($row2);
                            if (datetolocal($row2["retd_daterep"]) <> "")
                                echo '<br />' . _("Réponse le") . ' ' . datetolocal($row2["retd_daterep"]);
                        }
                    }
                }
            }
            echo "</span>";
            echo "</td>\n";

            echo "</div>";
            echo "</td>\n";
            if ($tx <> "e") {
                echo "<td>";
                echo $row["retp_codeage"];
                echo "</td>\n";
            }
            echo "<td>";
            echo "<span class=\"annot_std_gras\">";
            echo $row["retp_des"];
            echo "</span>";
            if ($row["retp_ns"] <> "")
                echo '<br />->' . _("N/S") . ' : ' . $row["retp_ns"];
            echo "</td>\n";

            echo "<td align=\"center\">";
            if ($row["retp_codeint"] <> "-1")
                echo $row["retp_codeint"];
            else
                echo "-";
            echo "</td>\n";

            if ($tx <> "e") {
                echo "<td align=\"center\">";
                if ($row["retp_codeenv"] <> "")
                    echo $row["retp_codeenv"];
                else
                    echo "-";
                echo "</td>\n";
            }

            // CREATION lien dynamique sur icône (édition, suppression, etc...)
            echo "<td align=\"center\">";
            if ($tx <> "e") {
                $voir = FALSE;

                if (($row["retp_codeenv"] <> "") OR (exist_enr_retr($row["retp_code"], FALSE) == TRUE) OR (exist_enr_retd($row["retp_code"], FALSE) == TRUE) OR (exist_enr_rete($row["retp_code"], FALSE) == TRUE))
                    $voir = TRUE;

                if ($voir == FALSE) {
                    $lst_maj = TRUE;
                    cre_falselnk_img('', 'retp_edit_'.$row["retp_id"], 'span', 'center', _("Edition"), 'lnk', 'retp_maj.php?td=edit&cpr=' . $row["retp_code"] . '&ca=' . $row["retp_codeage"] , '../img/edit.png');
                    if (@$_SESSION["niv_log"] >= 4) {
                        $lst_retd = TRUE;
                        cre_falselnk_img('', 'retd_add_'.$row["retp_id"], 'span', 'center', _("Demande de retour"), 'lnk', 'retd_maj.php?td=add&cpr=' . $row["retp_code"] , '../img/retd.png');
                    }
                    $lst_del = TRUE;
                    cre_falselnk_img('', 'retp_del_'.$row["retp_id"], 'span', 'center', _("Suppression"), 'lnk', 'retp_maj.php?td=del&cpr=' . $row["retp_code"] . '&ca=' . $row["retp_codeage"] , '../img/del.png');
                } else {
                    $lst_voir = TRUE;
                    cre_falselnk_img('', 'retp_view_'.$row["retp_id"], 'span', 'center', _("Visualisation"), 'lnk', 'retp_maj.php?td=view&cpr=' . $row["retp_code"] . '&ca=' . $row["retp_codeage"] , '../img/eye.png');
                }
            } else {
                echo "<td align=\"center\">";
                $lst_retee = TRUE;
                cre_falselnk_img('', 'rete_add_'.$row["retp_id"], 'span', 'center', _("Demande de retour"), 'lnk', 'retep_enr.php?td=add&tdo=' . $td . '&ce=' . $ce . '&cp=' . $row["retp_code"] , '../img/rete.png');
            }
            echo "</td>\n";
            echo "</tr>\n";

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

        $db->disconnect();

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

        $taille_piedla = "";
        include_once("piedla.inc.php");
    } else {
        if ($tx == "e") {
            echo "<div align=\"center\">";
            echo '<span class="annot_std_gras">' . _("Tous les produits avec numéros de retour accordés,") . '<br />' . _("du fournisseur") . ' <span class="annot_gras_it">' . $mque_ste . '</span> ' . _("on été saisis dans des fiches de renvoi") . '</span><br />';
            echo "</div>";
        } else
            retpp();
    }

    if ($tx <> "e")
        include_once("pied.php");
    else {
        echo "<br />";
        echo "<div align=\"center\">";
        echo '<input name="quit" type="button" class="bton_std" id="quit" value="' . _("Quitter") . '">';
        echo "</div>";
    }
} else {
    if ($tx == "e")
        close_fen_auto('', '');
}
?>
