<?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"];
$ncons = intval(@$_POST["ncons"]);
$idbl = @$_POST["nbl"];
$idtc = @$_POST["ntc"];

$mini = @$_POST["mini"];
$maxi = @$_POST["maxi"];
$pvht = @$_POST["puht"];

$retourliste = TRUE;

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
switch ($td) {
    // Ajout de la fiche Tarif consommable
    case "add" :
        $rech2 = "INSERT INTO `" . TBBARLOCTC . "`";
        $rech2 .= " (barloctc_idbarloc,barloctc_ncons,barloctc_min,barloctc_max,barloctc_pvht)";
        $rech2 .= " VALUES ('$idbl','$ncons','$mini','$maxi','$pvht')";
        $db->query($rech2);

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

    // MAJ de la fiche Tarif Consommable
    case "edit" :
        $majok = TRUE;

        if ($idtc <> last_id_tarcons_in_barloc($idbl, $ncons, FALSE)) {
            $msg = _("Barême impossible à modifier");
            $majok = FALSE;
        }
        /*
                    if (exist_barloc_in_coloc_ouv($idbl,FALSE)==TRUE)
                    {
                        $msg = "Barême utilisé dans une location non close";
                        $majok = FALSE;
                        $retourliste = FALSE;
                    }
            */
        if ($majok == TRUE) {
            $rech = "UPDATE `" . TBBARLOCTC . "` SET `barloctc_min`='" . $mini . "', `barloctc_max`='" . $maxi . "',";
            $rech .= " `barloctc_pvht`='" . $pvht . "'";
            $rech .= " WHERE `barloctc_id` = '" . $idtc . "'";
            $db->query($rech);

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

    // Suppression de la fiche tarif Consommable
    case "del" :
        $delok = TRUE;

        if ($idtc <> last_id_tarcons_in_barloc($idbl, $ncons, FALSE)) {
            $msg = _("Barême impossible à supprimer");
            $majok = FALSE;
        }
        /*
                    if (exist_barloc_in_coloc($idbl,FALSE)==TRUE)
                    {
                        $msg = "Barême utilisé dans une location. Désactivez le.";
                        $delok = FALSE;
                        $retourliste = FALSE;
                    }
                    */

        if ($delok == TRUE) {
            $rech = "DELETE FROM `" . TBBARLOCTC . "` WHERE `barloctc_id` = '$idtc'";
            $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 echo _("Tarif Consommable"); ?></title>

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

    <script type="text/javascript">
        <!--
        <?PHP
        if ($retourliste == TRUE)
            cre_html_tarcons($ncons,'dettarcons'.$ncons, $td, $idbl, 'window.opener.');
        ?>

        function init_all() {
            init_Body();
            <?PHP
            if ($retourliste == FALSE)
                echo "warn_txt('".$msg."',5000)";
            else
                echo "tbtarcons".$ncons."();";
            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>
