<?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/fic_age.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_mque.inc.php");
include_once("../inc/fic_prm.inc.php");
include_once("../inc/fic_ret.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_time.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("../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);

$sais_dtedblff = TRUE;
$sais_dterepf = TRUE;
$pbm_retd = FALSE;

$cpr = $_GET['cpr']; // Récupération code Produit à Retourner
$cm = '';
if (isset($_GET['cm']))
    $retd_codemque = $_GET['cm']; // Récupération code marque
$td = '';
if (isset($_GET['td']))
    $td = $_GET['td']; // Récupération type de demande
else
    $pbm_retd = TRUE;
$datemin = '';

if ($cpr <> "") {

    // Récupération des informations produit
    $row = lit_enr_retp($cpr, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        $datemin = datetolocal($retp_datecrea);

        // Si retour envoyé ou déjà traité, on force le mode "view"
        if ((exist_enr_rete($cpr, TRUE) == TRUE) OR (exist_enr_retr($cpr, TRUE) == TRUE))
            $td = "view";

        // Contrôle qu'une agence n'essaye pas de "hacker" une demande
        if ((@$_SESSION["niv_log"] == 4) AND ($retp_codeage <> @$_SESSION["code_age"]))
            $pbm_retd = TRUE;
    } else
        $pbm_retd = TRUE;

    // Récupération des informations demande de retour
    $row = lit_enr_retd($cpr, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
    } else
        $pbm_retd = TRUE;

    // Formatage des dates en français
    $retd_dateddef = datetolocal($retd_datedde);
    $retd_dblff = datetolocal($retd_dblf);
    $retd_daterepf = datetolocal($retd_daterep);
}

switch ($td) {
    case "add" :
        $titre = _("SAISIE DEMANDE DE RETOUR");
        break;
    case "edit" :
        $titre = _("MISE A JOUR DEMANDE DE RETOUR");
        break;
    case "view" :
        $sais_dtedblff = FALSE;
        $sais_dterepf = FALSE;
        $titre = _("DETAIL DEMANDE DE RETOUR");
        break;
    case "del":
        $sais_dtedblff = FALSE;
        $sais_dterepf = FALSE;
        $titre = _("SUPPRESSION DEMANDE DE RETOUR");
        break;
    default:
        $titre = _("PROBLEME");
        $pbm_retd = 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 _("Fiche Demande de Retour"); ?></title>

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

    <?PHP
    if (($td == "edit")  OR ($td == "add")) {
        ?>
        <script language="javascript">
            <!--
            var tab1_champ = ["nom:codepdtm; format:txt ; req:Y ; txt:<?PHP echo _("Code produit distributeur"); ?> ; lmx:20 ; stok:frmok ; stbad:frmbad",
                "nom:blf; format:liste ; req:Y ; txt:<?PHP echo _("Type de document"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:nblf; format:txt ; req:Y ; txt:<?PHP echo _("Numéro de document"); ?> ; lmx:20 ; stok:frmok ; stbad:frmbad",
                "nom:dblff; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date document"); ?>; stok:frmok ; stbad:frmbad",
                "nom:gar; format:liste ; req:Y ; txt:<?PHP echo _("Sous garantie"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                <?PHP if (($td =="add") OR ($td=="edit")) { ?>
                "nom:dateddef ; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date demande"); ?> ; stok:frmok ; stbad:frmbad",
                "nom:codemque; format:liste ; req:Y ; txt:<?PHP echo _("Destinataire"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:codemd; format:liste ; req:Y ; txt:<?PHP echo _("Mode de demande"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:urgence; format:liste ; req:Y ; txt:<?PHP echo _("Niveau d'urgence"); ?> ; stok:frmok ; stbad:frmbad",
                "nom:dossier; format:txt ; req:N ; txt:<?PHP echo _("Code de dossier"); ?> ; lmx:20 ; stok:frmok ; stbad:frmbad",
                <?PHP } ?>
                "nom:daterepf; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ;  req:N ; txt:<?PHP echo _("Date"); ?> de réponse ; stok:frmok ; stbad:frmbad",
                "nom:rma; format:txt ; req:N ; txt:<?PHP echo _("Numéro de retour"); ?> ; lmx:40 ; stok:frmok ; stbad:frmbad"];
            //-->
        </script>
    <?PHP
    }
    ?>

    <script type="text/javascript">
        <!--
        function init_datepicker() {
            $.datepicker.setDefaults({
                closeText: "<?PHP echo _("Fermer"); ?>",
                prevText: "<?PHP echo _("Précédent"); ?>",
                nextText: "<?PHP echo _("Suivant"); ?>",
                currentText: "<?PHP echo _("Aujourd'hui"); ?>",
                monthNames: ["<?PHP echo implode('","',$lng_tmoisf); ?>"],
                monthNamesShort: ["<?PHP echo implode('","',$lng_tmoism); ?>"],
                dayNames: ["<?PHP echo implode('","',$lng_tjourf); ?>"],
                dayNamesShort: ["<?PHP echo implode('","',$lng_tjourm); ?>"],
                dayNamesMin: ["<?PHP echo implode('","',$lng_tjours); ?>"],
                weekHeader: "<?PHP echo _("Sem."); ?>",
                dateFormat: "<?PHP echo $_SESSION['date_fmtshort']; ?>",
                firstDay: 1,
                isRTL: !1,
                changeMonth: true,
                changeYear: true,
                showMonthAfterYear: !1,
                yearSuffix: "",
                showButtonPanel: true,
                showOn: "both",
                buttonImageOnly: true,
                buttonImage: "../img/calendar.png",
                buttonText: "<?PHP echo _("Calendrier"); ?>"
            });
            <?PHP if ($sais_dtedblff == TRUE) { ?>
            $("#dblff").datepicker({
                onSelect: function (datesel) {
                    $("#dblff").blur();
                    chk_datedif(datesel, this.id, '<?PHP echo $_SESSION['date_fmtshort']; ?>', true, '', '', 'dateddef,today',false);
                }
            });
            <?PHP }

            if ($sais_dterepf == TRUE) { ?>
            $("#daterepf").datepicker({
                onSelect: function (datesel) {
                    $("#daterepf").blur();
                    chk_datedif(datesel, this.id, '<?PHP echo $_SESSION['date_fmtshort']; ?>', true, 'dateddef,today,dblff', '', '',false);
                }
            });
            <?PHP } ?>
        }

        function init_all() {
            init_Body();
            init_datepicker();
            <?PHP
            switch ($td)
            {
                case "add":
                    echo '$("#dateddef").datepicker({';
                    echo '    onSelect: function (datesel) {';
                    echo '        $("#dateddef").blur();';
                    echo "        chk_datedif(datesel, this.id, '".$_SESSION['date_fmtshort']."', true, 'today,datemin', '', '');";
                    echo '    }';
                    echo '});';
                    echo "init_autoraz('razdateddef','dateddef','click');";
                    echo "init_valid_retd('enr','retd_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "edit" :
                    echo "init_valid_retd('enr','retd_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "view" :
                    echo "readonly_all();";
                    break;
                case "del" :
                    echo "readonly_all();";
                    echo "warn_del('');";
                    break;
            }
            echo "init_autoraz('razdblff','dblff','click');";
            echo "init_autoraz('razdaterepf','daterepf','click');";
            ?>
            init_new_fen('imprim','impression','click');
            Focus_first();
        }

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

<body>

<?PHP include_once("../inc/entete.inc.php"); ?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<?PHP
if ($pbm_retd == FALSE) {
    if (nb_marques("") <> 0) {
        ?>
        <table width="100%" border="0">
            <tr>
                <td width="40%">
                    <strong><em><a
                                href="retp_lst.php?ord=DES&tri=Code"><?PHP echo _("Retour liste des produits défectueux"); ?></a></em></strong>
                    / <strong><em><a
                                href="retd_lst.php?ord=DES&tri=Demande%20le"><?PHP echo _("Liste des demandes"); ?></a></em></strong>
                </td>
                <td width="33%" align="center" class="annot_petit_it">
                    <div id="oblig">(<img src="../img/oblig.png" align="absmiddle"
                                          alt="<?PHP echo _("Champ obligatoire"); ?>">
                        : <?PHP echo _("Saisie obligatoire"); ?>)
                    </div>
                </td>
                <td width="40%" align="right">
                    <?PHP
                    if (($td == "edit") OR ($td == "view")) {
                        $urlimp = "retd_pdf.php?cpr=" . $cpr;
                        if (url_ok($urlimp) == TRUE) {
                            echo '<span id="imprim" align="right" title="' . $urlimp . '" class="falselnk">';
                            echo '<img src="../img/print.png" title="' . _("Imprimer") . '" border="0" align="absmiddle"> ' . _("Imprimer");
                            echo '</span>';
                        } else
                            echo '&nbsp;';
                    } else
                        echo '&nbsp;';
                    ?>
                </td>
            </tr>
        </table>
        <form name="retd_maj" id="retd_maj" method="post" action="retd_enr.php">
        <input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
        <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
        <input name="coderetp" type="hidden" id="coderetp" value="<?PHP echo $cpr ?>">
        <input name="today" type="hidden" id="today" value="<?PHP echo $datef; ?>">
        <input name="datemin" type="hidden" id="datemin" value="<?PHP echo $datemin; ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">

        <!-- Produit à retourner -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_retp" align="right"><?PHP echo _("Produit (N/S)"); ?></td>
                <td>
                    <span class="annot_std_gras"><?PHP echo $retp_des; ?> (<?PHP echo $retp_ns; ?>)</span>
                    <?PHP if (clos_retr($retp_code, TRUE) == TRUE) {
                        echo '<span class="alerte1">';
                        echo _("Traité");
                        echo '</span>';
                    } else {
                        if (exist_enr_rete($retp_code, TRUE) == TRUE) {
                            echo '<span class="alerte1">';
                            if (exist_enr_rete_clos($retp_code, TRUE) == TRUE)
                                echo _("Envoyé");
                            else
                                echo _("Prêt à partir");
                            echo "</span>";
                        }
                    }
                    ?>
                </td>
            </tr>
        </table>

        <!-- Destinataire -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_codemque" align="right"><?PHP echo _("Destinataire"); ?></td>
                <td>
                    <?PHP
                    // Création de la liste des destinataires
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    // Création des types de destinataires autorisés en retour produit
                    $tab_mquet_code = [];
                    $rech = "SELECT mquet_code FROM `" . TBMQUET . "`";
                    $rech .= " WHERE mquet_rpdt = 1";
                    $rows = $db->get_results($rech, ARRAY_A);
                    if ($rows)
                    {
                        foreach ($rows as $row)
                        {
                            extract($row);
                            $tab_mquet_code[] = $mquet_code;
                        }
                    }
                    // Création de la liste des codes destinataires
                    $rech = "SELECT mque_code, mque_ste FROM `" . TBMQUE . "`";
                    $rech .= " WHERE mque_code<>'*'";
                    if (count($tab_mquet_code)>0)
                    {
                        $rech .= " AND (";
                        foreach ($tab_mquet_code as $value)
                        {
                            $rech .= " mque_codemquet = '".$value."' OR";
                        }
                        // Suppression du dernier 'OR'
                        $rech = substr($rech, 0, strlen($rech) - 3);
                        $rech .= ")";
                    }
                    $rech .= " ORDER BY mque_ste";
                    $rows = $db->get_results($rech, ARRAY_A);
                    $db->disconnect();
                    cre_select_tab('codemque', 'codemque', $rows, 'mque_code', 'mque_ste', '', $retd_codemque, '', _("-- Faites votre choix --"));
                    ?>
                    <div class="annot_petit_it">(<?PHP echo _("Celui chez qui le produit devra être retourné"); ?>)</div>
                </td>
            </tr>
        </table>

        <!-- Date de demande -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_dateddef" align="right"><?PHP echo _("Date Demande"); ?></td>
                <td>
                    <input name="dateddef" type="text" id="dateddef" value="<?PHP echo $retd_dateddef ?>" size="10" maxlength="10" readonly>
                    <?PHP
                    if ($td == "add")
                    {
                        echo ' <img src="../img/raz.png" id ="razdateddef" border="0" align="absmiddle">';
                        echo '<div class="annot_petit_it">('._("Au mininimum le ").$datemin.')</div>';
                    }
                    ?>
                </td>
            </tr>
        </table>

        <!-- Mode de demande -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_codemd" align="right"><?PHP echo _("Mode de demande"); ?></td>
                <td>
                    <?PHP
                    // création de la liste des mode de demande
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    // Filtre sur agences ouvertes
                    $rech = "SELECT * FROM `" . TBMD . "` ORDER BY md_nom";
                    $rows = $db->get_results($rech, ARRAY_A);
                    $db->disconnect();
                    cre_select_tab('codemd', 'codemd', $rows, 'md_code', 'md_nom', '', $retd_codemd, '', _("-- Faites votre choix --"));
                    ?>
                </td>
            </tr>
        </table>

        <!-- Numéro de dossier -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_dossier" align="right"><?PHP echo _("N° dossier"); ?></td>
                <td>
                    <input name="dossier" type="text" id="dossier" value="<?PHP echo $retd_dossier; ?>" size="20"
                           maxlength="20">
                    <br /><span class="annot_petit_it">(<?PHP echo _("Celui donné par le distributeur"); ?>)</span>
                </td>
            </tr>
        </table>

        <!-- Code produit du distributeur -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_codepdtm" align="right"><?PHP echo _("Code produit"); ?></td>
                <td>
                    <input name="codepdtm" type="text" id="codepdtm" value="<?PHP echo $retd_codepdtm ?>" size="20"
                           maxlength="20">
                    <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
                    <br /><span class="annot_petit_it">(<?PHP echo _("Code du produit chez le distributeur"); ?>)</span>
                </td>
            </tr>
        </table>

        <!-- BL ou Facture ? -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_blf" align="right"><?PHP echo _("Type document"); ?></td>
                <td>
                    <?PHP
                    cre_select('blf', 'blf', $doc_types, intval($retd_blf), '', _("-- Faites votre choix --"));
                    echo '<br /><span class="annot_petit_it">(' . _("Type du document d'origine") . ')</span>';
                    ?>
                </td>
            </tr>
        </table>

        <!-- N° de BL ou de facture -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_nblf" align="right"><?PHP echo _("N° document"); ?></td>
                <td>
                    <input name="nblf" type="text" id="nblf" value="<?PHP echo $retd_nblf ?>" size="20" maxlength="20">
                    <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
                </td>
            </tr>
        </table>

        <!-- Date du document -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_dblf" align="right"><?PHP echo _("Date document"); ?></td>
                <td>
                    <input name="dblff" type="text" id="dblff" value="<?PHP echo $retd_dblff ?>" size="10"maxlength="10" readonly>
                    <?PHP
                    if ($sais_dtedblff == TRUE)
                        echo ' <img src="../img/raz.png" id ="razdblff" border="0" align="absmiddle">';
                    ?>
                </td>
            </tr>
        </table>

        <!-- Sous garantie ?-->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_gar" align="right"><?PHP echo _("Sous garantie ?"); ?></td>
                <td>
                    <?PHP
                    cre_select('gar', 'gar', $non_oui, intval($retd_gar), '', _("-- Faites votre choix --"));
                    ?>
                </td>
            </tr>
        </table>

        <!-- Niveau d'urgence -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_urgence" align="right"><?PHP echo _("Urgence"); ?></td>
                <td>
                    <?PHP
                    // création de la liste des Niveaux d'urgence
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    $rech = "SELECT * FROM `" . TBURG . "` ORDER BY urg_niveau";
                    $rows = $db->get_results($rech, ARRAY_A);
                    $db->disconnect();
                    cre_select_tab('urgence', 'urgence', $rows, 'urg_niveau', 'urg_des', '', $retd_nivurg);
                    ?>
                </td>
            </tr>
        </table>

        <?PHP
        if (($td == "add") OR ($td == "edit")) // Uniquement sur ajout ou édition
        {
            ?>
            <!-- Imprimer la demande ? -->
            <table width="100%" border="0">
                <tr>
                    <td width="180" class="rubfrm" id="f_imprimer" align="right"><?PHP echo _("Imprimer demande"); ?></td>
                    <td>
                        <input type="checkbox" name="imprimer" id="imprimer" value="ok">
                        <span class="annot_petit_it">(<?PHP echo _("Si coché, la demande de retour sera imprimée"); ?>)</span>
                    </td>
                </tr>
            </table>
        <?PHP } ?>

        <!-- Date de réponse -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_daterepf" align="right"><?PHP echo _("Date réponse"); ?></td>
                <td>
                    <input name="daterepf" type="text" id="daterepf" value="<?PHP echo $retd_daterepf ?>" size="10" maxlength="10" readonly>
                    <?PHP
                    if ($sais_dterepf == TRUE)
                    {
                        echo ' <img src="../img/raz.png" id ="razdaterepf" border="0" align="absmiddle">';
                        echo '<div class="annot_petit_it">('._("Au mininimum la date de demande").')</div>';
                    }
                    ?>
                </td>
            </tr>
        </table>

        <!-- N° de retour -->
        <table width="100%" border="0">
            <tr>
                <td width="180" class="rubfrm" id="f_rma" align="right"><?PHP echo _("N° de retour"); ?></td>
                <td>
                    <input name="rma" type="text" id="rma" value="<?PHP echo $retd_rma ?>" size="40" maxlength="40">
                </td>
            </tr>
        </table>

        <p align="center">
            <?PHP
            if (($td == "edit") OR ($td == "add")) {
                ?>
                <input name="RAZ" type="reset" class="bton_std" id="RAZ" value="<?PHP echo _("Réinitialiser"); ?>">
                <input name="enr" type="button" class="bton_std" id="enr" value="<?PHP echo _("Enregistrer"); ?>">
            <?PHP
            }
            if ($td == "del") // Bouton si en mode suppression
            {
                ?>
                <input name="del" type="submit" class="bton_std" id="del" value="<?PHP echo _("Supprimer"); ?>">
            <?PHP
            }
            ?>
        </p>
        </form>
    <?PHP
    } else
        pop_ret_auto(_("Aucun distributeur"), 'warn', "mque_lst.php");
} else
    pop_ret_auto(_("Problème de paramètres"), 'warn', "index.php");
include_once("pied.php");
?>
</body>
</html>