<?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_cli.inc.php');
include_once('../inc/fic_co.inc.php');
include_once('../inc/fic_cocl.inc.php');
include_once('../inc/fic_do.inc.php');
include_once('../inc/fic_droits.inc.php');
include_once('../inc/fic_hum.inc.php');
include_once('../inc/fic_int.inc.php');
include_once('../inc/fic_mque.inc.php');
include_once('../inc/fic_ot.inc.php');
include_once('../inc/fic_ret.inc.php');
include_once('../inc/fic_st.inc.php');
include_once('../inc/fic_statut.inc.php');
include_once('../inc/fic_tec.inc.php');
include_once('../inc/fic_txt.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_fic.inc.php');
include_once('../inc/func_geo.inc.php');
include_once('../inc/func_txt.inc.php');
include_once('../inc/func_txtcm.inc.php');
include_once('../inc/func_mail.inc.php');
include_once('../classes/phpmailer/class.phpmailer.php');
include_once('../classes/phpmailer/class.smtp.php');
include_once('../inc/mail.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();
$_POST = clean_str($_POST);

$titre = valpost('titre');
$td = valpost('td');

$coderetp = valpost('coderetp');
$datedde = datetomysql(valpost('dateddef'));
$codemque = valpost('codemque');
$codepdtm = valpost('codepdtm');
$blf = valpost('blf');
$nblf = valpost('nblf');
$dblf = datetomysql(valpost('dblff'));
$gar = valpost('gar');
$codemd = valpost('codemd');
$dossier = valpost('dossier');
$urgence = valpost('urgence');
$imprimer = valpost('imprimer');
$daterep = datetomysql(valpost('daterepf'));
$rma = valpost('rma');

$codeuser = valsession('code_log');

$msg = '';
$retourliste = TRUE;

if ($imprimer == 'ok') {
    $retourliste = FALSE;
}

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
switch ($td) {
    // Ajout de la fiche demande de retour
    case 'add' :
        if (exist_enr_retd($coderetp, FALSE) == FALSE) {
            $rech2 = 'INSERT INTO `' . TBRETD . '`';
            $rech2 .= ' (retd_coderetp,retd_datedde,retd_codemque,retd_codepdtm,retd_blf,retd_nblf,retd_dblf,retd_gar,retd_codemd,retd_dossier,retd_nivurg,retd_daterep,retd_rma)';
            $rech2 .= ' VALUES';
            $rech2 .= " ('$coderetp','$datedde','$codemque','" .$db->escape($codepdtm). "','$blf','" .$db->escape($nblf). "','$dblf','$gar','" .$db->escape($codemd). "','" . $db->escape($dossier) . "','$urgence','$daterep','" . $db->escape($rma) . "')";
            $db->query($rech2);

            // On met à jour la fiche produit correspondante
            $rech = 'UPDATE `' . TBRETP . '` SET';
            $rech .= " `retp_datedde`='" . $datedde . "', `retp_daterep`='" . $daterep . "' ";
            $rech .= " WHERE `retp_code` = '" . $db->escape($coderetp) . "'";
            $db->query($rech);

            $msg = _('Fiche ajoutée');
        } else {
            $msg = _('Demande déjà effectuée pour ce produit!');
            $retourliste = FALSE;
        }
        /*
                if ($retourliste == TRUE)
                {
                    // Envoi d'email vers le webmaster
                    envoi_mailwm("","Produit à retourner","retp","cre",'xxx',"age",$codepr,"","");
                    // Envoi d'email vers l'agence (Si pas créé par une agence)
                    if ((valsession('niv_log') <> 4) AND (valsession('niv_log') <> 3))
                        envoi_mail('',"Produit à retourner","retp","cre",'xxx',"age",$codepr,"","",FALSE);
                    // Envoi d'email vers le centre de gestion (Si pas créé par le CDG)
                    if (valsession('niv_log') <> 5)
                        envoi_mail('',"produit à retourner","retp","cre",'xxx',"cdg",$codepr,"","",FALSE);
                }*/
        break;

    // MAJ de la fiche demande de retour
    case 'edit' :
        if (exist_enr_rete($coderetp, FALSE) == TRUE) {
            $msg = _('Le produit a déjà été renvoyé');
            $retourliste = FALSE;
        }

        if ($retourliste == TRUE) {
            $rech = 'UPDATE `' . TBRETD . '` SET';
            $rech .= " `retd_codemd`='" . $codemd . "', `retd_dossier`='" . $db->escape($dossier) . "', `retd_codepdtm`='" . $db->escape($codepdtm) . "',";
            $rech .= " `retd_blf`='" . $blf . "', `retd_nblf`='" . $db->escape($nblf) . "', `retd_dblf`='" . $dblf . "',";
            $rech .= " `retd_gar`='" . $gar . "', `retd_nivurg`='" . $urgence . "', ";
            $rech .= " `retd_daterep`='" . $daterep . "', `retd_rma`='" . $db->escape($rma) . "'";
            $rech .= " WHERE `retd_coderetp` = '" . $db->escape($coderetp) . "'";
            $db->query($rech);

            // On met à jour la fiche produit correspondante
            $rech = 'UPDATE `' . TBRETP . '` SET';
            $rech .= " `retp_datedde`='" . $datedde . "', `retp_daterep`='" . $daterep . "' ";
            $rech .= " WHERE `retp_code` = '" . $db->escape($coderetp) . "'";
            $db->query($rech);

            $msg = _('Fiche modifiée');
        }

        break;


    // Suppression de la fiche demande de retour
    case 'del' :
        if (exist_enr_retr($code, TRUE) == TRUE) {
            $msg = _('Ce retour a déà été traité');
            $retourliste = FALSE;
        }
        if (exist_enr_rete($code, TRUE) == TRUE) {
            $msg = _('Ce retour a déà été renvoyé');
            $retourliste = FALSE;
        }

        if ($retourliste == TRUE) {
            $rech = 'DELETE FROM `' . TBRETD . "` WHERE `retd_coderetp` = '" . $db->escape($coderetp) . "'";
            $db->query($rech);

            // On met à jour la fiche produit correspondante
            $rech = 'UPDATE `' . TBRETP . "` SET `retp_datedde`='0000-00-00', ";
            $rech .= " `retp_daterep`='0000-00-00' ";
            $rech .= " WHERE `retp_code` = '" . $db->escape($coderetp) . "'";
            $db->query($rech);

            $msg = _('Fiche supprimée');
        }
        break;

    default :
        break;
}
close_database();
?>
<!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'); ?>

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            init_new_fen('imprim', 'impression', 'click');
            Focus_first();
        }

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

</head>
<body>
<?php include_once('../inc/entete.inc.php');
cre_ent_form($titre, '', '', '', FALSE);
if ($retourliste == FALSE) {
    exec_func_js("warn_txt('" . str_escape($msg) . "')");
    if ($imprimer == 'ok') {
        $urlimp = 'retd_pdf.php?cpr=' . $coderetp;
        exec_func_js("init_new_window('" . $urlimp . "','impression',0,0,'yes')");
        pop_ret_auto('', '', 'retd_lst.php?ord=DES&tri=Demande%20le');
    } else {
        aff_ret_idx_man($td, 'retd_lst.php?ord=DES&tri=Demande%20le', _('Retour à la liste'), TRUE);
    }
} else {
    pop_ret_auto($msg, 'ok', 'retd_lst.php?ord=DES&tri=Demande%20le');
}
include_once('pied.php');
?>
</body>
</html>