<?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/func.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_loc.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"];

$iddloc = intval(@$_POST["dloc"]);
$code = str_majuscules(@$_POST["code"]);
$codeb = str_majuscules(@$_POST["codeb"]);
$des = str_majuscules(@$_POST["des"]);
$ns = @$_POST["ns"];
$fam = str_majuscules(@$_POST["fam"]);
$valneuf = floatval(@$_POST["valneuf"]);
$valnet = floatval(@$_POST["valnet"]);
$caution = floatval(@$_POST["caution"]);
$tvacaut = @$_POST["tva"];
$note = @$_POST["note"];
$louable = intval(@$_POST["louable"]);
$ucons1 = str_majuscules(@$_POST["ucons1"]);
$idxcons1 = @$_POST["idxcons1"];
$tvacons1 = @$_POST["tvacons1"];
$ucons2 = str_majuscules(@$_POST["ucons2"]);
$idxcons2 = @$_POST["idxcons2"];
$tvacons2 = @$_POST["tvacons2"];

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

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
switch ($td) {
    // Ajout de la fiche Produit
    case "add" :
        $rech = "SELECT COUNT(*) FROM `" . TBPDTLOC . "` WHERE pdtloc_code = '" . $db->escape($code) . "'";
        $nbres = $db->get_var($rech);
        if ($nbres == 0) {
            $rech2 = "INSERT INTO `" . TBPDTLOC . "`";
            $rech2 .= " (pdtloc_code,pdtloc_codeb,pdtloc_des,pdtloc_ns,pdtloc_datecrea,pdtloc_fam,pdtloc_iddloc,pdtloc_valneuf,pdtloc_valnet,";
            $rech2 .= "pdtloc_caution,pdtloc_tvacaut,pdtloc_note,pdtloc_louable,pdtloc_pdtucons1,pdtloc_idxcons1,pdtloc_tvacons1,";
            $rech2 .= "pdtloc_pdtucons2,pdtloc_idxcons2,pdtloc_tvacons2) VALUES";
            $rech2 .= " ('" . $db->escape($code) . "','" . $db->escape($codeb) . "','" . $db->escape($des) . "','" . $db->escape($ns) . "','$datea','" . $db->escape($fam) . "','$iddloc','$valneuf','$valnet','$caution','$tvacaut','" . $db->escape($note) . "',";
            $rech2 .= "'$louable','$ucons1','$idxcons1','$tvacons1','$ucons2','$idxcons2','$tvacons2')";
            $db->query($rech2);

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

    // MAJ de la fiche Produit
    case "edit" :
        $existecoloc = FALSE;
        $majok = TRUE;

        // Vérifie si utilisé dans une fiche intervention non close
        if (exist_pdt_in_coloc_ouv($code, FALSE) == TRUE) {
            $existecoloc = TRUE;
            if ($louable == 0) {
                $majok = FALSE;
                $msg = _("Ce produit est déjà utilisé dans une location");
                $retourliste = FALSE;
            }
        }

        if ($majok == TRUE) {
            // MAJ de la fiche Produit
            $rech = "UPDATE `" . TBPDTLOC . "` SET `pdtloc_codeb`='" . $db->escape($codeb) . "', `pdtloc_iddloc`='" . $iddloc . "',";
            $rech .= " `pdtloc_fam`='" . $db->escape($fam) . "', `pdtloc_des`='" . $db->escape($des) . "', `pdtloc_ns`='" . $db->escape($ns) . "', `pdtloc_valneuf`='" . $valneuf . "',";
            $rech .= "`pdtloc_valnet`='" . $valnet . "', `pdtloc_caution`='" . $caution . "',`pdtloc_tvacaut`='" . $tvacaut . "',`pdtloc_note`='" . $db->escape($note) . "',";
            $rech .= "`pdtloc_louable`='" . $louable . "',`pdtloc_pdtucons1`='" . $ucons1 . "',`pdtloc_idxcons1`='" . $idxcons1 . "',";
            $rech .= "`pdtloc_tvacons1`='" . $tvacons1 . "',`pdtloc_pdtucons2`='" . $ucons2 . "',`pdtloc_idxcons2`='" . $idxcons2 . "',";
            $rech .= "`pdtloc_tvacons2`='" . $tvacons2 . "'";
            $rech .= " WHERE `pdtloc_code` = '" . $db->escape($code) . "'";
            $db->query($rech);

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

    // Suppression de la fiche Produit
    case "del" :
        $existcoloc = FALSE;

        // Vérifie si utilisée dans une fiche intervention
        if (exist_pdt_in_coloc($code, FALSE) == TRUE) {
            $existcoloc = TRUE;
            $msg = _("Ce produit est déjà utilisé dans une location").'<br />'._("Passez-le en mode 'non louable'");
            $retourliste = FALSE;
        }

        if ($existcoloc == FALSE) {
            // SUPPRESSION de la fiche Produit
            $rech = "DELETE FROM `" . TBPDTLOC . "` WHERE `pdtloc_code` = '" . $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 Produit à Louer"); ?></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, "pdtloc_lst.php?ta=m", _("Retour à la liste"), TRUE);
} else
    pop_ret_auto($msg, 'ok', "pdtloc_lst.php?ta=m&ca=" . @$_SESSION["code_age"]);
include_once("pied.php");
?>
</body>
</html>