<?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_do.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_hum.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_droits.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_ot.inc.php");
include_once("../inc/fic_ret.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();
$_GET = clean_str($_GET);
$titre = @$_POST["titre"];
$td = @$_POST["td"];

$datecrea = datetomysql(@$_POST["datecreaf"]);
$dateenv = datetomysql(@$_POST["dateenvf"]);
$clos = @$_POST["clos"];
$clore = 0;
if ($clos == "ok")
    $clore = 1;
$code = str_majuscules(@$_POST["code"]);
$codemque = str_majuscules(@$_POST["codemque"]);
$codeage = str_majuscules(@$_POST["codeage"]);
$codeme = str_majuscules(@$_POST["codeme"]);
$bord = str_majuscules(@$_POST["bord"]);
$nbcol = @$_POST["nbcol"];
if ($nbcol == 0) $nbcol = 1;

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

$msg = "";
$retourliste = TRUE;

if (@$_POST["nbutton"] == 'enrimp') {
    $retourliste = FALSE;
    $imprim = TRUE;
}

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
switch ($td) {
    // Ajout de la fiche envoi
    case "add" :
        $codee = crecode("ER", FALSE);

        $rech = "SELECT COUNT(*) FROM `" . TBRETE . "` WHERE `rete_code` = '" . $db->escape($codee) . "'";
        $nbres = $db->get_var($rech);
        if ($nbres == 0) {
            $rech2 = "INSERT INTO `" . TBRETE . "`"
                . " (rete_code,rete_datecrea,rete_dateenv,rete_codemque,rete_codeme,rete_bord,rete_nbcol,rete_codeage)"
                . " VALUES"
                . " ('" . $db->escape($codee) . "','$datecrea','$dateenv','$codemque','$codeme','" . $db->escape($bord) . "','$nbcol','$codeage')";
            $db->query($rech2);

            $msg = _("Bon de renvoi enregistré");
        } else {
            $msg = _("Ce code existe déjà");
            $retourliste = FALSE;
            $imprim = FALSE;
        }

        break;

    // MAJ de la fiche envoi
    case "edit" :
        $rech = "UPDATE `" . TBRETE . "` SET `rete_dateenv`='" . $dateenv . "', `rete_codemque`='" . $codemque . "',";
        $rech .= " `rete_codeme`='" . $codeme . "', `rete_bord`='" . $db->escape($bord) . "', `rete_nbcol`='" . $nbcol . "', `rete_codeage`='" . $codeage . "',";
        $rech .= " `rete_clos`='" . $clore . "'";
        $rech .= " WHERE `rete_code` = '" . $db->escape($code) . "'";
        $db->query($rech);

        // Si clot, on met à jour les fiches produits correspondantes
        if ($clore == 1) {
            $rech = "SELECT * FROM `" . TBRETEP . "`";
            $rech .= " WHERE `retep_codeenv` = '" . $db->escape($code) . "'";
            $rows = $db->get_results($rech, ARRAY_A);
            if ($rows) {
                foreach ($rows as $row) {
                    $row = encode_str($row);
                    extract($row);

                    $rech2 = "UPDATE `" . TBRETP . "` SET `retp_dateenv`='" . $dateenv . "' ";
                    $rech2 .= " WHERE `retp_code` = '" . $retep_coderetp . "'";
                    $db->query($rech2);
                }
            }
        }
        $msg = _("Bon de renvoi mis à jour");

        break;

    // Suppression de la fiche envoi
    case "del" :
        if ($clore == 0) {
            if (nb_art_traites_in_envoi($code, FALSE) == 0) {
                $rech = "DELETE FROM `" . TBRETE . "` WHERE `rete_code` = '" . $db->escape($code) . "'";
                $db->query($rech);

                $msg = _("Bon de renvoi supprimé");
            } else {
                $msg = _("Il y déjà eu des articles traités par le fournisseur. Impossible de supprimer");
                $retourliste = FALSE;
                $imprim = FALSE;
            }
        } else {
            $msg = _("Impossible de supprimer une fiche close");
            $retourliste = FALSE;
            $imprim = FALSE;
        }
        break;

    default :
        break;
}
$db->disconnect();
?>
<!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 envoi de produits"); ?></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"); ?>
<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) . "')");
    if ($imprim == TRUE) {
        if ($td == "add")
            $urlimp = "rete_pdf.php?ce=" . $codee;
        else
            $urlimp = "rete_pdf.php?ce=" . $code;

        if (url_ok($urlimp) == TRUE) {
            echo '<div id="imprim" align="center" title="' . $urlimp . '" class="falselnk">';
            echo '<img src="../img/print.png" title="' . _("Imprimer") . '" border="0" align="absmiddle"> ' . _("Imprimer");
            echo '</div>';
        }
    }
    aff_ret_idx_man($td, "rete_lst.php?ord=DES&tri=Code", _("Retour à la liste"), TRUE);
} else {
    if ($td <> "add")
        pop_ret_auto($msg, 'ok', "rete_lst.php?ord=DES&tri=Code");
    else
        pop_ret_auto($msg, 'ok', "rete_maj.php?td=edit&ce=" . $codee);
}
include_once("pied.php");
?>
</body>
</html>