<?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_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);

$pbm_retr = FALSE;
$cpr='';
$td='';
$ce = '';

if (isset($_GET['cpr']))
    $cpr = $_GET['cpr']; // Récupération code Produit à Retourner
else
    $pbm_retr = TRUE;
if (isset($_GET['td']))
    $td = $_GET['td']; // Récupération type de demande
else
    $pbm_retr = TRUE;
if (isset($_GET['tdo']))
    $tdo = $_GET['tdo']; // Récupération type de demande de la fiche envoi
else
    $pbm_retr = TRUE;
if (isset($_GET['ce']))
    $ce = $_GET['ce']; // Récupération code envoi
else
    $pbm_retr = TRUE;
$tx = "";
if (isset($_GET['tx']))
    $tx = $_GET['tx']; // Récupération source appel (tx=e -> appel depuis fiche de renvoi)

if ($cpr <> "") {
    // Récupération des informations demande de retour
    $row = lit_enr_retr($cpr, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        if ($td <> "del") {
            if ($retr_clos == 1)
                $td = "view";
        }

        // Formatage des dates en français
        $retr_dateretf = datetolocal($retr_dateret);
    } else {
        if ($td == "add") {
            $row = lit_enr_retp($cpr, TRUE);
            if (is_array($row)) {
                $row = encode_str($row);
                extract($row);
            } else
                $pbm_retr = TRUE;
        } else
            $pbm_retr = TRUE;
    }


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

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

    // Récupération fiche d'envoi
    $row = lit_enr_rete($ce, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        // Formatage des dates en français
        $rete_dateenvf = datetolocal($rete_dateenv);

        // Contrôle qu'une agence n'essaye pas de "hacker" un retour
        if ((@$_SESSION["niv_log"] == 4) AND ($rete_codeage <> @$_SESSION["code_age"]))
            $pbm_retr = TRUE;

        if ($rete_clos == 0)
            $pbm_retr = TRUE;
    } else
        $pbm_retr = TRUE;

    // Récupération fiche demande de retour
    $row = lit_enr_retd($cpr, TRUE);

    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
    } else
        $pbm_retr = TRUE;
} else
    $pbm_retr = TRUE;

switch ($td) {
    case "add" :
        $titre = _("SAISIE TRAITEMENT RETOUR");
        break;
    case "edit" :
        $titre = _("MISE A JOUR TRAITEMENT RETOUR");
        break;
    case "view" :
        $titre = _("DETAIL TRAITEMENT RETOUR");
        break;
    case "del":
        $titre = _("SUPPRESSION TRAITEMENT RETOUR");
        break;
    default:
        $titre = "PROBLEME";
        $pbm_retr = 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 Traitement de Retour"); ?></title>

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

    <?PHP
    if (($td == "edit")  OR ($td == "add")) {
        ?>
        <script language="javascript">
            <!--
            var tab1_champ = ["nom:dateretf ; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date"); ?> retour ; stok:frmok ; stbad:frmbad",
                "nom:typeope; format:liste ; req:Y ; txt:<?PHP echo _("Type opération"); ?>  ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:codeme; format:liste ; req:Y ; txt:<?PHP echo _("Moyen d'envoi"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:bord; format:txt ; req:N ; txt:<?PHP echo _("Bordereau"); ?> ; lmx:20 ; 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"); ?>"
            });
        }

        function init_all() {
            init_Body();
            init_datepicker();
            <?php
             switch ($td)
             {
                case "add" :
                case "edit" :
                        echo '$("#dateretf").datepicker({';
                        echo '    onSelect: function (datesel) {';
                        echo '        $("#dateretf").blur();';
                        echo "        chk_datedif(datesel, this.id, '".$_SESSION['date_fmtshort']."', true, 'dateenvf', '', 'today')";
                        echo '    }';
                        echo '});';
                         echo "init_fensaisie('add_me',1024,600);";
                         echo "init_valid_retr('enr','retr_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "view" :
                    echo "readonly_all();";
                    break;
                case "del" :
                    echo "readonly_all();";
                    echo "warn_del('');";
                    break;
             }
             ?>
            add_func_click('quit', 'sclose','');
            Focus_first();
        }

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

<body>
<?PHP
if ($pbm_retr == FALSE) {
    if ($tx <> "e") include_once("../inc/entete.inc.php");
    cre_ent_form($titre, 'retr_lst.php?ord=DES&tri=Date', $_SERVER['HTTP_REFERER'], '');
    ?>
    <form name="retr_maj" id="retr_maj" method="post" action="retr_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="tdo" type="hidden" id="tdo" value="<?PHP echo $tdo ?>">
    <input name="tx" type="hidden" id="tx" value="<?PHP echo $tx ?>">
    <input name="coderetp" type="hidden" id="coderetp" value="<?PHP echo $cpr ?>">
    <input name="ce" type="hidden" id="ce" value="<?PHP echo $ce ?>">
    <input name="today" type="hidden" id="today" value="<?PHP echo $datef; ?>">
    <input name="nbutton" type="hidden" id="nbutton" value="">

    <!-- Produit traité -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_retp1" align="right"><?PHP echo _("Produit (N/S)"); ?></td>
            <td>
                <span class="annot_std_gras"><?PHP echo $retp_des; ?> (<?PHP echo $retp_ns; ?>)</span>
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_retp2" align="right"><?PHP echo _("Numéro de retour"); ?></td>
            <td>
                <span class="annot_std_gras"><?PHP echo $retd_rma; ?></span>
            </td>
        </tr>
        <tr>
            <td width="160" class="rubfrm" id="f_retp3" align="right"><?PHP echo _("Renvoyé le"); ?></td>
            <td>
                <input name="dateenvf" type="text" id="dateenvf" value="<?PHP echo $rete_dateenvf ?>" readonly>
            </td>
        </tr>
    </table>

    <!-- Date renvoi -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_dateretf" align="right"><?PHP echo _("Date traitement"); ?></td>
            <td>
                <input name="dateretf" type="text" id="dateretf" value="<?PHP echo $retr_dateretf ?>" size="10" maxlength="10" readonly>
                <?PHP
                if (($td == "add") OR ($td == "edit"))
                    echo ' <img src="../img/oblig.png" align="absmiddle" alt="'._("Champ obligatoire").'">';
                ?>
            </td>
        </tr>
    </table>

    <!-- Opération effectuée -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_typeope" align="right"><?PHP echo _("Statut"); ?></td>
            <td>
                <?PHP
                cre_select('typeope', 'typeope', $ope_types, intval($retr_typeope), '', _("-- Faites votre choix --"));
                ?>
            </td>
        </tr>
    </table>

    <!-- Avoir -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_avoir" align="right"><?PHP echo _("N° avoir"); ?>
                <br />(<?PHP echo _("si avoir"); ?>)
            </td>
            <td>
                <input name="avoir" type="text" id="avoir" value="<?PHP echo $retr_avoir ?>" size="20" maxlength="20">
            </td>
        </tr>
    </table>

    <!-- Numéro de série -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_ns" align="right"><?PHP echo _("N° série"); ?>
                <br />(<?PHP echo _("si échangé"); ?>)
            </td>
            <td>
                <input name="ns" type="text" id="ns" value="<?PHP echo $retr_ns; ?>" size="20" maxlength="20">
            </td>
        </tr>
    </table>

    <!-- Motif -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_raison" align="right"><?PHP echo _("Motif"); ?>
                <br />(<?PHP echo _("si refusé"); ?><br /> <?PHP echo _("ou rien fait"); ?>)
            </td>
            <td>
                <textarea name="raison" rows="5" id="raison"><?PHP echo $retr_raison; ?></textarea>
            </td>
        </tr>
    </table>

    <!-- Effectué -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_fait" align="right"><?PHP echo _("Effectué"); ?>
                <br />(<?PHP echo _("si réparé"); ?>)
            </td>
            <td>
                <textarea name="fait" rows="5" id="fait"><?PHP echo $retr_fait; ?></textarea>
            </td>
        </tr>
    </table>

    <!-- Mode d'envoi -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_codeme" align="right"><?PHP echo _("Mode d'envoi"); ?></td>
            <td>
                <?PHP
                $tab_first_options = [];
                $tab_first_options["-1"] = _("Sans objet");

                // création de la liste des mode d'envoi
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT * FROM `" . TBME . "` ORDER BY me_nom";
                $rows = $db->get_results($rech, ARRAY_A);

                cre_select_tab('codeme', 'codeme', $rows, 'me_code', 'me_nom', '', $retr_codeme, '', _("-- Faites votre choix --"), 1, FALSE, '', '', TRUE, TRUE, FALSE, $tab_first_options);

                if (($td == "add") OR ($td == "edit"))
                {
                    $url_creeme = "me_maj.php?td=add&tx=r";
                    cre_falselnk('', 'add_me', 'span', '', _("Ajouter un mode"), $url_creeme, '../img/add.png', _("Ajouter un mode"));
                }
                ?>
            </td>
        </tr>
    </table>

    <!-- Numéro de bordereau -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_bord" align="right"><?PHP echo _("Bordereau envoi"); ?>
                <br />(<?PHP echo _("Si mode d'envoi"); ?>)
            </td>
            <td>
                <input name="bord" type="text" id="bord" value="<?PHP echo $retr_bord ?>" size="20">
            </td>
        </tr>
    </table>

    <?PHP if (($td == "add") OR ($td == "edit")) { ?>
        <!-- Cloture de fiche -->
        <table width="100%" border="0">
            <tr>
                <td width="160" class="rubfrm" id="f_clos" align="right"><?PHP echo _("Fiche close"); ?></td>
                <td>
                    <input type="checkbox" name="clos" id="clos" value="ok" <?PHP if ($retr_clos == 1) echo 'checked="checked"'; ?>>
                    <span class="annot_petit_it">(<?PHP echo _("Si coché, cette fiche sera validée et ne sera plus modifiable"); ?>)</span>
                </td>
            </tr>
        </table>
    <?PHP } else { ?>
        <input type="hidden" name="clos" id="clos" value="<?PHP if ($rete_clos == 1) echo "ok"; ?>">
    <?PHP } ?>



    <p align="center">
        <?PHP
        switch($td)
        {
            case "add" :
            case "edit" :
                echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="'._("Réinitialiser").'">';
                echo '<input name="enr" type="button" class="bton_std" id="enr" value="'._("Enregistrer").'">';
                break;
            case "del" :
                echo '<input name="del" type="submit" class="bton_std" id="del" value="'._("Supprimer").'">';
                break;
        }

        if ($tx == "e")
            echo '<input name="quit" type="button" class="bton_std" id="quit" value="'._("Quitter").'">';
        else
            include_once("pied.php");
        ?>
    </p>
    </form>
<?PHP
} else {
    if ($tx == "e")
        close_fen_auto('', '');
    else
        pop_ret_auto(_("Problème de paramètres"), 'warn', "index.php");
}
?>
