<?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_pen.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_time.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);

$pbm_pdt = FALSE;
$titre = _('CONSOMMABLES FACTURABLES');
$td = valget('td');
if (($td <> 'ret') AND ($td <> 'del')) {
    $pbm_pdt = TRUE;
}
$tab_enr = [];
$didx1 = (int)valget('di1');
$didx2 = (int)valget('di2');
$ridx1 = (int)valget('ri1');
$ridx2 = (int)valget('ri2');
$qci1 = (int)valget('qc1');
$qci2 = (int)valget('qc2');

$msg = '';
$retourliste = TRUE;

if ($pbm_pdt == FALSE) {
    $diff_idx1 = 0;
    $diff_idx2 = 0;
    if ((($didx1 <> -1) && ($ridx1 <> -1)) || (($didx2 <> -1) && ($ridx2 <> -1))) {
        if (($ridx1 - $didx1) > $qci1) {
            $diff_idx1 = $ridx1 - $didx1 - $qci1;
        }
        if (($ridx2 - $didx2) > $qci2) {
            $diff_idx2 = $ridx2 - $didx2 - $qci2;
        }
    }
    switch ($td) {
        // Ajout du montant consommables pour la location
        case 'ret' :
            if (isset($_SESSION['pdtlcl']) AND isset($_SESSION['pdtlbl'])) {
                // Calcul du montant pour les consommables
                $tot_consos = calcul_consloc($_SESSION['pdtlbl'][0]['idbarloc'], $diff_idx1, $diff_idx2);
                $_SESSION['pdtlcl'][0]['valeur'] = $tot_consos;

                $msg = _('Calculs effectués');
            } else {
                $retourliste = FALSE;
                $msg = _('Problème dans les sessions');
            }
            break;

        // Suppression de la pénalité de location
        case 'del' :
            $_SESSION['pdtlcl'] = [];
            $msg = _('Consommables supprimés');
            break;

        default :
            break;
    }
} else {
    $retourliste = FALSE;
    $msg = _('Problème de paramètres');
}
?>
<!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 _('Pénalité Location'); ?></title>

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

    <script type="text/javascript">
        <!--
        <?php
        if ($retourliste == TRUE)
            {cre_html_pdtlbl('detbarloc', 'edit', $_SESSION['pdtlbl'], $_SESSION['pdtlpl'], $_SESSION['pdtlcl'], 'window.opener.');}
        ?>

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

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

</head>
<body>
<?php
cre_ent_form($titre, '', '', '', FALSE);
?>
</body>
</html>
