<?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');
$tdo = valpost('tdo');
$tx = valpost('tx');
$ce = valpost('ce');

$coderetp = valpost('coderetp');
$dateret = datetomysql(valpost('dateretf'));
$typeope = valpost('typeope');
$avoir = valpost('avoir');
$ns = valpost('ns');
$raison = valpost('raison');
$fait = valpost('fait');
$codeme = valpost('codeme');
$bord = valpost('bord');
$clos = valpost('clos');
$clore = 0;
if ($clos == 'ok') {
    $clore = 1;
}

$codeuser = valsession('code_log');

$msg = '';
$retourliste = TRUE;

switch ($td) {
    // Ajout de la fiche traitement de retour
    case 'add' :
        if (exist_enr_retr($coderetp, TRUE) == FALSE) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
            $rech2 = 'INSERT INTO `' . TBRETR . '`';
            $rech2 .= ' (retr_coderetp,retr_dateret,retr_typeope,retr_ns,retr_avoir,retr_raison,retr_fait,retr_codeme,retr_bord,retr_clos)';
            $rech2 .= ' VALUES';
            $rech2 .= " ('$coderetp','$dateret','$typeope','" . $db->escape($ns) . "','" . $db->escape($avoir) . "','" . $db->escape($raison) . "','" . $db->escape($fait) . "','$codeme','" . $db->escape($bord) . "','$clore')";
            $db->query($rech2);

            // Si clot, on met à jour la fiche produit correspondante
            if ($clore == 1) {
                $rech = 'UPDATE `' . TBRETP . "` SET `retp_dateret`='" . $dateret . "' ";
                $rech .= " WHERE `retp_code` = '" . $coderetp . "'";
                $db->query($rech);
            }
            close_database();

            $msg = _('Fiche ajoutée');
        } else {
            $msg = _('Demande déjà effectuée pour ce produit');
            $retourliste = FALSE;
        }

        if (($retourliste == TRUE) AND ($clore == 1)) {
            // Envoi d'email vers le webmaster
            envoi_mailwm('', _('Retour traité'), 'retr', 'cre', 'xxx', 'age', $coderetp, '', '');
            // Envoi d'email vers l'agence (Si pas créé par une agence)
            if ((valsession('niv_log') <> 4) AND (valsession('niv_log') <> 3)) {
                envoi_mail('', _('Retour traité'), 'retr', 'cre', 'xxx', 'age', $coderetp, '', '', FALSE);
            }
            // Envoi d'email vers le centre de gestion (Si pas créé par le CDG)
            if (valsession('niv_log') <> 5) {
                envoi_mail('', _('Retour traité'), 'retr', 'cre', 'xxx', 'cdg', $coderetp, '', '', FALSE);
            }
        }
        break;

    // MAJ de la fiche traitement de retour
    case 'edit' :

        $maj_retr_ok = TRUE;

        if (clos_retr($coderetp, TRUE) == TRUE) {
            $maj_retr_ok = FALSE;
            $msg = _('Fiche déjà close');
            $retourliste = FALSE;
        }

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
        if ($maj_retr_ok == TRUE) {
            $rech = 'UPDATE `' . TBRETR . '` SET';
            $rech .= " `retr_dateret`='" . $dateret . "', `retr_typeope`='" . $typeope . "',";
            $rech .= " `retr_ns`='" . $db->escape($ns) . "', `retr_avoir`='" . $db->escape($avoir) . "', `retr_raison`='" . $db->escape($raison) . "',";
            $rech .= " `retr_fait`='" . $db->escape($fait) . "', `retr_codeme`='" . $codeme . "', ";
            $rech .= " `retr_bord`='" . $db->escape($bord) . "', `retr_clos`='" . $clore . "'";
            $rech .= " WHERE `retr_coderetp` = '" . $db->escape($coderetp) . "'";
            $db->query($rech);

            // Si clot, on met à jour la fiche produit correspondante
            if ($clore == 1) {
                $rech = 'UPDATE `' . TBRETP . "` SET `retp_dateret`='" . $dateret . "' ";
                $rech .= " WHERE `retp_code` = '" . $coderetp . "'";
                $db->query($rech);
            }

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

        close_database();

        if (($retourliste == TRUE) AND ($clore == 1)) {
            // Envoi d'email vers le webmaster
            envoi_mailwm('', _(_('Retour traité')), 'retr', 'cre', 'xxx', 'age', $coderetp, '', '');
            // Envoi d'email vers l'agence (Si pas créé par une agence)
            if ((valsession('niv_log') <> 4) AND (valsession('niv_log') <> 3)) {
                envoi_mail('', _(_('Retour traité')), 'retr', 'cre', 'xxx', 'age', $coderetp, '', '', FALSE);
            }
            // Envoi d'email vers le centre de gestion (Si pas créé par le CDG)
            if (valsession('niv_log') <> 5) {
                envoi_mail('', _(_('Retour traité')), 'retr', 'cre', 'xxx', 'cdg', $coderetp, '', '', FALSE);
            }
        }
        break;


    // Suppression de la fiche traitement de retour
    case 'del' :
        if (clos_retr($code, TRUE) == FALSE) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
            $rech = 'DELETE FROM `' . TBRETR . "` WHERE `retr_coderetp` = '" . $db->escape($coderetp) . "'";
            $db->query($rech);

            // Si clot, on met à jour la fiche produit correspondante
            if ($clore == 1) {
                $rech = 'UPDATE `' . TBRETP . "` SET `retp_dateret`='0000-00-00' ";
                $rech .= " WHERE `retp_code` = '" . $coderetp . "'";
                $db->query($rech);
            }

            close_database();

            $msg = _('Fiche supprimée');
        } else {
            $msg = _('Fiche déjà close');
            $retourliste = FALSE;
        }
        break;

    default :
        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'); ?>

    <script type="text/javascript">
        <!--
        <?php
        if ($retourliste == TRUE)
        {
            switch ($tx)
            {
                case 'e' :
                    cre_html_pdte('detpdte', $tdo, $ce, 'window.opener.');
                    break;
            }
        }
        ?>

        function init_all() {
            init_Body();
            <?php
            if ($retourliste == FALSE) {
                echo "warn_txt('" . str_escape($msg) . "',5000)";
            } else {
                switch ($tx)
                {
                    case 'e' :
                        echo 'updatenv();';
                        echo 'self.close();';
                        break;
                    default :
                        pop_ret_auto($msg, 'ok', 'retr_lst.php?ord=DES&tri=Date');
                        break;
                }
            }
            ?>

            Focus_first();
        }

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

</head>
<body>
<?php
if ($tx <> 'e') {
    include_once('../inc/entete.inc.php');
}
cre_ent_form($titre, '', '', '', FALSE);
if ($tx <> 'e') {
    include_once('pied.php');
}
?>
</body>
</html>