<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.4
 *
 * Copyright (C) 2006-2016  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) {
    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(valsession('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 = valget('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 ($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 ($row) {
        $row = encode_str($row);
        extract($row);

        // Contrôle qu'une agence n'essaye pas de "hacker" un retour
        if (CheckNivLog('4') AND ($retp_codeage <> valsession('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 (CheckNivLog('4') AND ($rete_codeage <> valsession('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() {
            <?php include_once ('../jscript/datepicker_prm.js.php'); ?>
            <?php if (($td == 'add') OR ($td == 'edit')) { ?>
            $("#dateretf").datepicker({
                beforeShow: function() {
                    var $dateenvf;
                    var mindate;
                    $dateenvf = $('#dateenvf');
                    if ($dateenvf.val() !== '') {
                        mindate = $dateenvf.val();
                    }
                    else
                    {
                        mindate = 0;
                    }
                    $(this).datepicker('option', 'minDate', mindate);
                    $(this).datepicker('option', 'maxDate', 0);
                },
                onSelect: function (datesel) {
                    $("#dateddef").blur();
                    chk_datedif(datesel, this.id, '<?php echo valsession('date_fmtshort'); ?>', true, 'today,datemin', '', '');
                }
            });
            <?php
            }
            ?>
        }

        function init_all() {
            init_Body();
            init_datepicker();
            <?php
             switch ($td)
             {
                case 'add' :
                case 'edit' :
                         echo "init_fensaisie('add_me',1024,600);";
                         echo "init_valid_retr('enr','retr_maj','".valsession('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 _('Type Opération'); ?></td>
                <td>
                    <?php
                    cre_select('typeope', 'typeope', $ope_types, (int)$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, 'utf8');
                    $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');
    }
}
?>
