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


$td = $_GET['td']; // Récupération type demande
$cpr = $_GET['cpr']; // Récupération code Produit à Retourner
$ca = $_GET['ca']; // Récupération code Agence
$cm = $_GET['cm']; // Récupération code Marque

$pbm_retp = FALSE;
$nbartint = 0;
$lst_id_readonly = '';

if (@$_SESSION["niv_log"] == 3)
    $td= "view";

if ($ca <> "") {
    // Contrôle qu'une agence n'essaye pas de "hacker" une fiche
    if (((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) AND ($ca <> @$_SESSION["code_age"]))
        $pbm_retp = TRUE;
    $retp_codeage = $ca;
}

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

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

        // Formatage des dates en français
        $retp_datecreaf = datetolocal($retp_datecrea);
        $retp_datemodf = datetolocal($retp_datemod);
        $retp_dblff = datetolocal($retp_dblf);

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

        if ($td == "edit")
            $ca = $retp_codeage;

        if ($td == "add") {
            $retp_datecreaf = $datef;
            $retp_code = $cpr;
            $retp_codeage = $ca;
            $retp_codeenv = "";
        }
    } else
        $pbm_retp = TRUE;
} else {
    if ($td == "add") {
        $retp_datecreaf = $datef;
        $retp_code = crecodesession("PR");
        $cpr = $retp_code;
        $retp_blf = "";
        $retp_codeenv = "";
    } else
        $pbm_retp = TRUE;
}

switch ($td) {
    case "add" :
        $titre = _("SAISIE PRODUIT A RETOURNER");
        $retp_blf = '';
        break;
    case "edit" :
        $titre = _("MISE A JOUR PRODUIT A RETOURNER");
        break;
    case "view" :
        $titre = _("DETAIL PRODUIT A RETOURNER");
        break;
    case "del":
        $titre = _("SUPPRESSION PRODUIT A RETOURNER");
        break;
    default:
        $titre = _("PROBLEME");
        $pbm_retp = TRUE;
        break;
}
if ((@$_SESSION["niv_log"] <> 5) AND (@$_SESSION["niv_log"] <> 9)) {
    $lst_id_readonly = 'codeage';
    $retp_codeage = @$_SESSION["code_age"];
}

?>
<!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 Produit à Retourner"); ?></title>

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

    <?PHP
    if (($td == "edit")  OR ($td == "add")) {
        ?>
        <script language="javascript">
            <!--
            var tab1_champ = ["nom:datecreaf ; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date demande"); ?> ; stok:frmok ; stbad:frmbad",
                <?PHP if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) { ?>
                "nom:codeage; format:liste ; req:Y ; txt:<?PHP echo _("Agence demandeuse"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                <?PHP } ?>
                "nom:des; format:txt ; req:Y ; txt:<?PHP echo _("Code produit distributeur"); ?> ; lmx:50 ; stok:frmok ; stbad:frmbad",
                "nom:ns; format:txt ; req:Y ; txt:<?PHP echo _("Numéro de série"); ?>; lmx:40 ; stok:frmok ; stbad:frmbad",
                "nom:nint; format:txt ; req:N ; txt:<?PHP echo _("Numéro interne"); ?>; lmx:50 ; stok:frmok ; stbad:frmbad",
                "nom:gar; format:liste ; req:Y ; txt:<?PHP echo _("Sous garantie"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:pbm; format:txt ; req:Y ; txt:<?PHP echo _("Problème"); ?> ; 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:dblf; format:date ; masque:<?PHP echo $_SESSION['date_fmtfull']; ?> ; req:Y ; txt:<?PHP echo _("Date document"); ?>; stok:frmok ; stbad:frmbad",
                "nom:codeint; format:liste ; req:Y ; txt:<?PHP echo _("Intervention concernée"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad"];
            //-->
        </script>
    <?PHP
    }
    ?>

    <script type="text/javascript">
        <!--
        $(function () {
            $("#des").Setcase({caseValue: 'upper', changeonFocusout: true});
        });

        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" :
                    echo '$("#datecreaf").datepicker();';
                    echo '$("#dblf").datepicker({';
                    echo '    onSelect: function (datesel) {';
                    echo '        $("#dblf").blur();';
                    echo "chk_datedif(datesel, this.id, '".$_SESSION['date_fmtshort']."', true, '', '', 'today');";
                    echo '},';
                    echo '});';
                    echo "init_valid_form('enr','retp_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    echo "init_valid_form('enrd','retp_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "edit" :
                    echo '$("#datecreaf").datepicker();';
                    echo '$("#dblf").datepicker({';
                    echo '    onSelect: function (datesel) {';
                    echo '        $("#dblf").blur();';
                    echo "chk_datedif(datesel, this.id, '".$_SESSION['date_fmtshort']."', true, '', '', 'today');";
                    echo '},';
                    echo '});';
                    echo "init_valid_form('enr','retp_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    echo "init_valid_form('enrd','retp_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "del ":
                    echo "readonly_all();";
                    echo "warn_del('');";
                    break;
                case "view" :
                    echo "readonly_all();";
                    break;
            }
            if ($lst_id_readonly <> '')
                echo "readonly_id('".$lst_id_readonly."',false);";
            ?>
            init_ajax_int();
            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>
<!-- affiché uniquement s'il y a au moins un distributeur -->
<?PHP
if ($pbm_retp == FALSE) {
    ?>
    <table width="100%" border="0">
        <tr>
            <td width="40%"
                class="annot_gras_it"><?PHP aff_lnk_lst_rech('retp_lst.php?ord=DES&tri=Code', _("Retour à la liste"), $_SERVER['HTTP_REFERER'], _("Retour à la page précédente")); ?></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 (((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) AND ($td == "edit")) { ?>
                    <a href="retd_maj.php?td=add&cpr=<?PHP echo $retp_code; ?>"><?PHP echo _("Demande de retour"); ?></a>
                <?PHP } else echo "&nbsp;" ?>
            </td>
        </tr>
    </table>
    <form name="retp_maj" id="retp_maj" method="post" action="retp_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="codeenv" type="hidden" id="codeenv" value="<?PHP echo $retp_codeenv ?>">
    <input name="cm" type="hidden" id="cm" value="<?PHP echo $cm ?>">
    <input name="today" type="hidden" id="today" value="<?PHP echo $datef; ?>">
    <input name="nbutton" type="hidden" id="nbutton" value="">
    <?PHP if ($td == "add") { ?>
        <input name="code" type="hidden" id="code" value="<?PHP echo $retp_code ?>">
    <?PHP } ?>
    <!-- Code de de la fiche (affiché uniquement si édition ou suppression) -->
    <?PHP if ($td <> "add") { ?>
        <table width="100%" border="0">
            <tr>
                <td width="160" class="rubfrm" id="f_coderetp" align="right"><?PHP echo _("Code"); ?></td>
                <td>
                    <input name="code" type="text" id="code" value="<?PHP echo $retp_code ?>" size="20" maxlength="20"
                           readonly>
                    <?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>";
                        } else {
                            if (exist_enr_retd($retp_code, TRUE) == TRUE) {
                                echo "<span class=\"alerte1\">";
                                echo _("Demande envoyée");
                                echo "</span>";
                            }
                        }
                    }
                    ?>
                </td>
            </tr>
        </table>
    <?PHP } ?>

    <!-- Date de demande -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_datecreaf" align="right"><?PHP echo _("Date Demande"); ?></td>
            <td>
                <input name="datecreaf" type="text" id="datecreaf" value="<?PHP echo $retp_datecreaf ?>" size="10"
                       maxlength="10" readonly>
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
            </td>
        </tr>
    </table>

    <!-- Code de l'agence (choisi uniquement si création, affiché uniquement si édition ou suppression) -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_agence" align="right"><?PHP echo _("Agence demandeuse"); ?></td>
            <td>
                <?PHP
                $tab_age = give_tab_age_cli($cli_code);
                cre_select('codeage', 'codeage', $tab_age, $retp_codeage, '', _("-- Faites votre choix --"), 1, FALSE, '', _("-- Pas d'agence --"));
                ?>
            </td>
        </tr>
    </table>

    <!-- Désignation du produit -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_des" align="right"><?PHP echo _("Libellé"); ?></td>
            <td>
                <input name="des" type="text" id="des" value="<?PHP echo $retp_des ?>" size="50" maxlength="50">
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
            </td>
        </tr>
    </table>

    <!-- N° de série -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_ns" align="right"><?PHP echo _("N° série"); ?></td>
            <td>
                <input name="ns" type="text" id="ns" value="<?PHP echo $retp_ns ?>" size="40" maxlength="40">
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
            </td>
        </tr>
    </table>

    <!-- N° interne -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_nint" align="right"><?PHP echo _("N° interne"); ?></td>
            <td>
                <input name="nint" type="text" id="nint" value="<?PHP echo $retp_nint ?>" size="50" maxlength="50">
                <br /><span class="annot_petit_it">(<?PHP echo _("Code interne de marquage du produit"); ?>)</span>
            </td>
        </tr>
    </table>

    <!-- Problème -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_pbm" align="right"><?PHP echo _("Problème"); ?></td>
            <td>
                <?PHP
                if (($td == "add") OR ($td == "edit")) {
                    ?>
                    <textarea name="pbm" rows="5" id="pbm"><?PHP echo $retp_pbm; ?></textarea>
                    <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
                <?PHP
                } else {
                    ?>
                    <textarea name="pbm" rows="5" id="pbm" readonly><?PHP echo $retp_pbm; ?></textarea>
                <?PHP
                }
                ?>
            </td>
        </tr>
    </table>

    <!-- BL ou Facture ? -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_blf" align="right"><?PHP echo _("Document client"); ?></td>
            <td>
                <?PHP
                cre_select('blf', 'blf', $doc_types, intval($retp_blf), '', _("-- Faites votre choix --"));
                ?>
            </td>
        </tr>
    </table>

    <!-- N° de BL ou de facture -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_nblf" align="right"><?PHP echo _("N° document"); ?></td>
            <td>
                <input name="nblf" type="text" id="nblf" value="<?PHP echo $retp_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="160" class="rubfrm" id="f_dblf" align="right"><?PHP echo _("Date document"); ?></td>
            <td>
                <input name="dblf" type="text" id="dblf" value="<?PHP echo $retp_dblff ?>" size="10" maxlength="10"
                       readonly>
                <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
            </td>
        </tr>
    </table>

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

    <!-- Intervention éventuellement concernée (choisi uniquement si création, affiché uniquement si édition ou suppression) -->
    <table width="100%" border="0">
        <tr>
            <td width="160" class="rubfrm" id="f_codeint" align="right"><?PHP echo _("Intervention"); ?></td>
            <td>
                <?PHP
                $tab_first_options = [
                    "-1" => _("-- Pas d'intervention concernée --")
                ];
                // création de la liste des interventions non terminées
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT * FROM `" . TBINT . "` WHERE `int_datefin` = '0000-00-00' AND `int_codeage` = '" . $retp_codeage . "'";
                // Filtre sur l'agence (si nécessaire)
                if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
                    $rech .= " AND `int_codeage` = '" . @$_SESSION["code_age"] . "'";
                $rech .= " ORDER BY int_code";
                $rows = $db->get_results($rech, ARRAY_A);
                $db->disconnect();
                cre_select_tab('codeint', 'codeint', $rows, 'int_code', 'int_code', 'int_codecli', $retp_codeint, '', _("-- Choisissez l'intervention rattachée à ce problème --"), 1, FALSE, '', '', TRUE, TRUE, FALSE, $tab_first_options);
                ?>
            </td>
        </tr>
    </table>

    <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") . '">';
                if (trim($cm) <> "")
                    echo '<input name="enrd" type="button" class="bton_std" id="enrd" value="' . _("Enregistrer & Demander un retour") . '">';
                break;
            case "del" :
                echo '<input name="del" type="submit" class="bton_std" id="del" value="' . _("Supprimer") . '">';
                break;
        }
        ?>
    </p>
    </form>
<?PHP
} else
    pop_ret_auto(_("Problème de paramètres"), 'warn', "index.php");
include_once("pied.php");
?>
</body>
</html>