<?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/config.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_loc.inc.php");
include_once("../inc/fic_taxes.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_log.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"];
$codepdt = @$_POST["codepdt"];
$idbl = @$_POST["nbl"];

$code = str_majuscules(@$_POST["code"]);
$des = str_majuscules(@$_POST["desbl"]);
$datev1f = @$_POST["dateval1f"];
if ($datev1f <> "")
    $dateval1 = datetomysql($datev1f);
else
    $dateval1 = $datev1f;
$datev2f = @$_POST["dateval2f"];
if ($datev2f <> "")
    $dateval2 = datetomysql($datev2f);
else
    $dateval2 = $datev2f;
$puht = floatval(@$_POST["puht"]);
$tva = @$_POST["tva"];
$mtass = floatval(@$_POST["mtass"]);
$tvaass = @$_POST["tvaass"];
$assobl = intval(@$_POST["assobl"]);
$mult = intval(@$_POST["mult"]);
$actif = intval(@$_POST["actif"]);
$pen = @$_POST["pen"];
$qconsi1 = @$_POST["qconsi1"];
$puconsi1 = floatval(@$_POST["puconsi1"]);
$tvacons1 = @$_POST["tvacons1"];
$qconsi2 = @$_POST["qconsi2"];
$puconsi2 = floatval(@$_POST["puconsi2"]);
$tvacons2 = @$_POST["tvacons2"];


$retourliste = TRUE;

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
switch ($td) {
    // Ajout de la fiche barème
    case "add" :
        $rech = "SELECT * FROM `" . TBBARLOC . "` WHERE barloc_codepdtloc = '" . $db->escape($codepdt) . "' AND barloc_codeperloc = '" . $db->escape($code) . "'";
        $nb_rs = $db->get_var($rech);
        if ($nb_rs == 0) {
            $rech2 = "INSERT INTO `" . TBBARLOC . "`";
            $rech2 .= " (barloc_codepdtloc,barloc_codeperloc,barloc_des,barloc_dateval1,barloc_dateval2,";
            $rech2 .= " barloc_pvht,barloc_codetva,barloc_mtass,barloc_codetvaass,barloc_assobl,barloc_mult,barloc_actif,barloc_codepen,";
            $rech2 .= "barloc_qconsi1,barloc_puconsi1,barloc_tvacons1,barloc_qconsi2,barloc_puconsi2,barloc_tvacons2)";
            $rech2 .= " VALUES ('$codepdt','" . $db->escape($code) . "','" . $db->escape($des) . "','$dateval1','$dateval2','$puht','$tva','$mtass','$tvaass','$assobl','$mult',";
            $rech2 .= "'$actif','$pen','$qconsi1','$puconsi1','$tvacons1','$qconsi2','$puconsi2','$tvacons2')";
            $db->query($rech2);

            $msg = _("Fiche ajoutée");
        } else {
            $msg = _("Ce code est déjà saisi pour ce produit");
            $retourliste = FALSE;
        }
        break;

    // MAJ de la fiche barème
    case "edit" :
        $majok = TRUE;

        if ($actif == 0) {
            if (exist_barloc_in_coloc_ouv($idbl, FALSE) == TRUE) {
                $msg = _("Barême utilisé dans une location non close");
                $majok = FALSE;
            }
        }

        if ($majok == TRUE) {
            $rech = "UPDATE `" . TBBARLOC . "` SET `barloc_codepdtloc`='" . $codepdt . "', `barloc_codeperloc`='" . $db->escape($code) . "',";
            $rech .= " `barloc_dateval1`='" . $dateval1 . "', `barloc_dateval2`='" . $dateval2 . "', `barloc_pvht`='" . $puht . "',";
            $rech .= " `barloc_codetva`='" . $tva . "', `barloc_actif`='" . $actif . "', `barloc_codepen`='" . $pen . "', `barloc_des`='" . $db->escape($des) . "',";
            $rech .= " `barloc_mtass`='" . $mtass . "', `barloc_codetvaass`='" . $tvaass . "', `barloc_assobl`='" . $assobl . "', `barloc_mult`='" . $mult . "',";
            $rech .= " `barloc_qconsi1`='" . $qconsi1 . "',`barloc_puconsi1`='" . $puconsi1 . "',`barloc_tvacons1`='" . $tvacons1 . "',";
            $rech .= " `barloc_qconsi2`='" . $qconsi2 . "',`barloc_puconsi2`='" . $puconsi2 . "',`barloc_tvacons2`='" . $tvacons2 . "'";
            $rech .= " WHERE `barloc_codepdtloc` = '" . $db->escape($codepdt) . "' AND `barloc_codeperloc` = '" . $db->escape($code) . "'";
            $db->query($rech);

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

    // Suppression de la fiche barème
    case "del" :
        $delok = TRUE;

        if ($actif == 0) {
            if (exist_barloc_in_coloc($idbl, FALSE) == TRUE) {
                $msg = _("Barême utilisé dans une location. Désactivez le.");
                $delok = FALSE;
            }
        }

        if ($delok == TRUE) {
            $rech = "DELETE FROM `" . TBBARLOC . "` WHERE `barloc_codepdtloc` = '" . $db->escape($codepdt) . "' AND `barloc_codeperloc` = '" . $db->escape($code) . "'";
            $db->query($rech);

            $msg = _("Fiche supprimée");
        } else
            $retourliste = 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 _("Barème Location"); ?></title>

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

    <script type="text/javascript">
        <!--
        <?PHP
        if ($retourliste == TRUE)
            cre_html_barloc('detbarloc', 'edit', $codepdt, 'window.opener.');
        ?>

        function init_all() {
            init_Body();
            <?PHP
            if ($retourliste == FALSE)
                echo "warn_txt('".$msg."',5000)";
            else
                echo "tbbarloc();";
            echo 'self.close();';
            ?>
            Focus_first();
        }

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

</head>
<body>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
</body>
</html>
