<?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_droits.inc.php");
include_once("../inc/func.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(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);

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

$type = @$_POST["type"];
$code = str_majuscules(@$_POST["code"]);
$ste = str_majuscules(@$_POST["ste"]);
$rcs = str_majuscules(@$_POST["rcs"]);
$ape = str_majuscules(@$_POST["ape"]);
$tvai = str_majuscules(@$_POST["tvai"]);
$adr1 = str_majuscules(@$_POST["adr1"]);
$adr2 = str_majuscules(@$_POST["adr2"]);
$ville = @$_POST["ville"];
$dep = str_majuscules(@$_POST["dep"]);
$pays = str_majuscules(@$_POST["pays"]);
$telf = @$_POST["telf"];
$fax = @$_POST["fax"];
$notaa = @$_POST["notaa"];
$notat = @$_POST["notat"];
$notap = @$_POST["notap"];

$codeuser = @$_SESSION["code_log"];
if (@$_SESSION["niv_log"] == 3)
    $codeuser = @$_SESSION["code_age"];

$retourliste = TRUE;

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

// vérification de la validité de la ville choisie
$rech = "SELECT COUNT(*) FROM `" . TBVILLES . "` WHERE villes_id = '" . $db->escape($ville) . "' AND villes_codepays = '" . $db->escape($pays) . "'";
$nbres = $db->get_var($rech);
if ($nbres == 0)
    $ville = 0;
switch ($td) {
    // Ajout de la fiche Marque
    case "add" :
        $rech = "SELECT COUNT(*) FROM `" . TBMQUE . "` WHERE mque_code = '" . $db->escape($code) . "'";
        $nbres = $db->get_var($rech);
        if ($nbres == 0) {
            // Fiche Marque
            $rech = "INSERT INTO `" . TBMQUE . "` (mque_code,mque_ste,mque_rcs,mque_ape,mque_tvai,mque_codemquet,mque_adr1,mque_adr2,mque_ville,mque_dep,mque_codepays,mque_telf,mque_fax,mque_notaa,mque_notat)";
            $rech .= " VALUES ('" . $db->escape($code) . "','" . $db->escape($ste) . "','$rcs','$ape','$tvai','" . $db->escape($type) . "','" . $db->escape($adr1) . "',";
            $rech .= "'" . $db->escape($adr2) . "','$ville','$dep','$pays','$telf','$fax','";
            $rech .= $db->escape($notaa) . "','" . $db->escape($notat) . "')";
            $db->query($rech);
            // Notes privées
            $rech = "INSERT INTO `" . TBMQUEN . "` (mquen_codemque,mquen_codeu,mquen_nota)";
            $rech .= " VALUES ('" . $db->escape($code) . "','$codeuser','" . $db->escape($notap) . "')";
            $db->query($rech);
            $msg = _("Fiche ajoutée");
        } else {
            $msg = _("Ce code existe déjà");
            $retourliste = FALSE;
        }
        break;

    // Modification de la fiche Marque
    case "edit" :
        // Fiche Marque
        if (@$_SESSION["niv_log"] >= 5) {
            $rech = "UPDATE `" . TBMQUE . "`";
            $rech .= " SET `mque_ste`='" . $db->escape($ste) . "', `mque_rcs`='" . $rcs . "', `mque_ape`='" . $ape . "', `mque_tvai`='" . $tvai . "',";
            $rech .= " `mque_codemquet`='" . $db->escape($type) . "', `mque_adr1`='" . $db->escape($adr1) . "', `mque_adr2`='" . $db->escape($adr2) . "',";
            $rech .= " `mque_ville`='" . $ville . "', `mque_dep`='" . $dep . "', `mque_codepays`='" . $pays . "', `mque_telf`='" . $telf . "',";
            $rech .= " `mque_fax`='" . $fax . "', `mque_notaa`='" . $db->escape($notaa) . "', `mque_notat`='" . $db->escape($notat) . "'";
            $rech .= " WHERE `mque_code` = '" . $db->escape($code) . "'";
        } else {
            $rech = "UPDATE `" . TBMQUE . "`";
            $rech .= " SET `mque_ste`='" . $ste . "', `mque_rcs`='" . $rcs . "', `mque_ape`='" . $ape . "', `mque_tvai`='" . $tvai . "',";
            $rech .= " `mque_codemquet`='" . $db->escape($type) . "', `mque_adr1`='" . $db->escape($adr1) . "', `mque_adr2`='" . $db->escape($adr2) . "',";
            $rech .= " `mque_ville`='" . $ville . "', `mque_dep`='" . $dep . "', `mque_codepays`='" . $pays . "', `mque_telf`='" . $telf . "',";
            $rech .= " `mque_fax`='" . $fax . "', `mque_notat`='" . $db->escape($notat) . "'";
            $rech .= " WHERE `mque_code` = '" . $db->escape($code) . "'";
        }
        $db->query($rech);
        // Notes privées
        $rech = "SELECT COUNT(*) FROM `" . TBMQUEN . "` WHERE `mquen_codemque` = '$code' AND `mquen_codeu` = '$codeuser'";
        $nbres = $db->get_var($rech, ARRAY_A);
        if ($nbres) {
            $rech = "UPDATE `" . TBMQUEN . "`";
            $rech .= " SET `mquen_nota`='" . $db->escape($notap) . "'";
            $rech .= " WHERE (`mquen_codemque` = '" . $db->escape($code) . "' AND `mquen_codeu` = '" . $codeuser . "')";
        } else {
            $rech = "INSERT INTO `" . TBMQUEN . "` (mquen_codemque,mquen_codeu,mquen_nota)";
            $rech .= " VALUES ('" . $db->escape($code) . "','$codeuser','" . $db->escape($notap) . "')";
        }
        $db->query($rech);
        $msg = _("Fiche modifiée");
        break;

    // Suppression de la fiche Marque
    case "del" :
        $ok_del = TRUE;
        $msg = "";

        // Vérifie si utilisé dans une demande de retour produit
        $rexist = "SELECT COUNT(*) FROM `" . TBRETD . "` WHERE `retd_codemque` = '" . $db->escape($code) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0) {
            $msg = _("Cette marque est utilisée dans une demande de retour produit");
            $ok_del = FALSE;
            $retourliste = FALSE;
        }

        // Vérifie si utilisé dans un envoi de retour produit
        $rexist = "SELECT COUNT(*) FROM `" . TBRETE . "` WHERE `rete_codemque` = '" . $db->escape($code) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0) {
            if ($msg == "")
                $msg .= "<br /><br />";
            $msg = _("Cette marque est utilisée dans un envoi de retour produit");
            $ok_del = FALSE;
            $retourliste = FALSE;
        }

        // Vérifie si utilisé dans un relevé de matériel
        $rexist = "SELECT COUNT(*) FROM `" . TBRPDT . "` WHERE `rpdt_codema` = '" . $db->escape($code) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0) {
            if ($msg == "")
                $msg .= "<br /><br />";
            $msg = _("Cette marque est utilisée dans un Relevé de matériel");
            $ok_del = FALSE;
            $retourliste = FALSE;
        }

        // Vérifie si utilisé dans un relevé de licence
        $rexist = "SELECT COUNT(*) FROM `" . TBRLIC . "` WHERE `rlic_codema` = '" . $db->escape($code) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0) {
            if ($msg == "")
                $msg .= "<br /><br />";
            $msg .= _("Cette marque est utilisée dans un Relevé de licence");
            $ok_del = FALSE;
            $retourliste = FALSE;
        }

        // Vérifie si utilisé dans un type de licence
        $rexist = "SELECT COUNT(*) FROM `" . TBRLTL . "` WHERE `rltl_codema` = '" . $db->escape($code) . "'";
        $nbres = $db->get_var($rexist);
        if ($nbres > 0) {
            if ($msg == "")
                $msg .= "<br /><br />";
            $msg .= _("Cette marque est utilisée dans un type de licence");
            $ok_del = FALSE;
            $retourliste = FALSE;
        }

        if ($ok_del == TRUE) {
            // SUPPRESSION de la fiche Marque
            $rech = "DELETE FROM `" . TBMQUE . "` WHERE `mque_code` = '" . $db->escape($code) . "'";
            $db->query($rech);

            // SUPPRESSION des fiches contacts
            $rech = "DELETE FROM `" . TBCMQUE . "` WHERE `cmque_codemque` = '" . $db->escape($code) . "'";
            $db->query($rech);

            // SUPPRESSION des liens webs
            $rexist = "SELECT wmque_id FROM `" . TBWMQUE . "` WHERE `wmque_codemque` = '" . $db->escape($code) . "'";
            $row = $db->get_row($rexist, ARRAY_A);
            if ($row) {
                extract($row);

                $rech2 = "DELETE FROM `" . TBWMQUEN . "` WHERE `wmquen_idwmque` = '$wmque_id'";
                $db->query($rech2);
            }

            $rech = "DELETE FROM `" . TBWMQUE . "` WHERE `wmque_codemque` = '" . $db->escape($code) . "'";
            $db->query($rech);

            // SUPPRESSION des fiches Notes privées
            $rexist = "SELECT cmque_id FROM `" . TBCMQUE . "` WHERE `cmque_codemque` = '" . $db->escape($code) . "'";
            $row = $db->get_row($rexist, ARRAY_A);
            if ($row) {
                extract($row);

                $rech2 = "DELETE FROM `" . TBCMQUEN . "` WHERE `cmquen_idcmque` = '$cmque_id'";
                $db->query($rech2);
            }

            $rech = "DELETE FROM `" . TBMQUEN . "` WHERE `mquen_codemque` = '" . $db->escape($code) . "'";
            $db->query($rech);

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

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            <?PHP
            if ($retourliste == FALSE)
                echo "warn_txt('" . str_escape($msg) . "');";
            ?>
            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)
    aff_ret_idx_man($td, "mque_lst.php", _("Retour à la liste"), TRUE);
else
    pop_ret_auto($msg, 'ok', "mque_lst.php");
include_once("pied.php");
?>
</body>
</html>