<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  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_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_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(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);

$titre = @$_POST["titre"];
$td = @$_POST["td"];
$cm = @$_POST["cm"];

$code = str_majuscules(@$_POST["code"]);
$datecrea = datetomysql(@$_POST["datecreaf"]);
$codeage = str_majuscules(@$_POST["codeage"]);
$des = str_majuscules(@$_POST["des"]);
$nint = @$_POST["nint"];
$blf = @$_POST["blf"];
$nblf = str_majuscules(@$_POST["nblf"]);
$dblf = datetomysql(@$_POST["dblf"]);
$gar = @$_POST["gar"];
$ns = @$_POST["ns"];
$pbm = @$_POST["pbm"];
$codeint = str_majuscules(@$_POST["codeint"]);
$codeenv = str_majuscules(@$_POST["codeenv"]);

$codeuser = @$_SESSION["code_log"];

$msg = "";
$retourliste = TRUE;

switch ($td) {
    // Ajout de la fiche produit à retourner
    case "add" :
        $codepr = crecode("PR", TRUE);

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "SELECT COUNT(*) FROM `" . TBRETP . "` WHERE `retp_code` = '" . $db->escape($codepr) . "'";
        $nbres = $db->get_var($rech);
        if ($nbres == 0) {
            $rech2 = "INSERT INTO `" . TBRETP . "`"
                . " (retp_datecrea,retp_codecrea,retp_datemod,retp_codemod,retp_code,retp_codeage,retp_des,retp_nint,retp_blf,retp_nblf,retp_dblf,retp_gar,retp_ns,retp_pbm,retp_codeint)"
                . " VALUES"
                . " ('$datecrea','$codeuser','$datea','$codeuser','" . $db->escape($codepr) . "','$codeage','$des','$nint','$blf','$nblf','$dblf','$gar','" . $db->escape($ns) . "','" . $db->escape($pbm) . "','$codeint')";
            $db->query($rech2);

            $msg = _("Fiche ajoutée");
        } else {
            $msg = _("Ce code existe déjà");
            $retourliste = FALSE;
        }

        $db->disconnect();

        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 ((@$_SESSION["niv_log"] <> 4) AND (@$_SESSION["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 (@$_SESSION["niv_log"] <> 5)
                envoi_mail("", "produit à retourner", "retp", "cre", "xxx", "cdg", $codepr, "", "", FALSE);
        }
        break;

    // MAJ de la fiche Produit à retourner
    case "edit" :
        if ($codeenv <> "") {
            $msg = _("Le produit a déjà été envoyé");
            $retourliste = FALSE;
        }

        if (exist_enr_retd($code, TRUE) == TRUE) {
            $msg = _("La demande a déjà été effectuée");
            $retourliste = FALSE;
        }

        if ($retourliste == TRUE) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            $rech = "UPDATE `" . TBRETP . "` SET `retp_datemod`='" . $datea . "', `retp_codemod`='" . $codeuser . "', `retp_codeage`='" . $codeage . "',";
            $rech .= " `retp_des`='" . $des . "', `retp_nint`='" . $nint . "', `retp_blf`='" . $blf . "', `retp_nblf`='" . $nblf . "', `retp_dblf`='" . $dblf . "',";
            $rech .= " `retp_gar`='" . $gar . "', `retp_ns`='" . $db->escape($ns) . "', `retp_pbm`='" . $db->escape($pbm) . "', `retp_codeint`='" . $codeint . "'";
            $rech .= " WHERE `retp_code` = '" . $db->escape($code) . "'";
            $db->query($rech);
            $db->disconnect();

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

    // Suppression de la fiche produit à retourner
    case "del" :
        if (exist_enr_retr($code, TRUE) == TRUE) {
            $msg = _("Ce retour a déà été traité");
            $retourliste = FALSE;
        }
        if (exist_enr_retd($code, TRUE) == TRUE) {
            $msg = _("La demande a déjà été effectuée");
            $retourliste = FALSE;
        }
        if (exist_enr_rete($code, TRUE) == TRUE) {
            $msg = _("Ce retour a déà été renvoyé");
            $retourliste = FALSE;
        }

        if ($retourliste == TRUE) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            $rech = "DELETE FROM `" . TBRETP . "` WHERE `retp_code` = '" . $db->escape($code) . "'";
            $db->query($rech);
            $db->disconnect();

            $msg = _("Fiche supprimée");
        }
        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 Produit à Retourner"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            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>

<p align="center">&nbsp;</p>
<?PHP
if ($retourliste == FALSE) {
    exec_func_js("warn_txt('" . str_escape($msg) . "')");
    aff_ret_idx_man($td, "retp_lst.php?ord=DES&tri=Code", _("Retour à la liste"), TRUE);
} else {
    if (trim($cm) == "")
        pop_ret_auto($msg, 'ok', "retp_lst.php?ord=DES&tri=Code");
    else
        pop_ret_auto($msg, 'ok', 'retd_maj.php?td=add&cpr=' . $codepr . '&cm=' . $cm);
}
include_once("pied.php");
?>
</body>
</html>