<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.4
 *
 * Copyright (C) 2006-2016  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) {
    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(valsession('code_log'));
// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);

$titre = valpost('titre');
$td = valpost('td');
$codepdt = valpost('codepdt');
$idbl = valpost('nbl');

$code = str_majuscules(valpost('code'));
$des = str_majuscules(valpost('desbl'));
$datev1f = valpost('dateval1f');
$dateval1 = $datev1f;
if ($datev1f <> '') {
    $dateval1 = datetomysql($datev1f);
}
$datev2f = valpost('dateval2f');
$dateval2 =$datev2f;
if ($datev2f <> '') {
    $dateval2 = datetomysql($datev2f);
}
$puht = (float)valpost('puht');
$tva = valpost('tva');
$mtass = (float)valpost('mtass');
$tvaass = valpost('tvaass');
$assobl = (int)valpost('assobl');
$mult = (int)valpost('mult');
$actif = (int)valpost('actif');
$pen = valpost('pen');
$qconsi1 = valpost('qconsi1');
$puconsi1 = (float)valpost('puconsi1');
$tvacons1 = valpost('tvaconsi1');
$qconsi2 = valpost('qconsi2');
$puconsi2 = (float)valpost('puconsi2');
$tvacons2 = valpost('tvaconsi2');

$msg = '';
$retourliste = TRUE;

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
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;
}
close_database();
?>
<!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>
<?php
cre_ent_form($titre, '', '', '', FALSE);
?>
</body>
</html>
