<?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();
$_GET = clean_str($_GET);

$pbm_pen = FALSE;
$titre = _('PENALITE SUR LOCATION');
$td = valget('td');
if (($td <> 'ret') AND ($td <> 'del')) {
    $pbm_pen = TRUE;
}
$tab_enr = [];
$dp = '';
$hp = '';
$dr = '';
$hr = '';
$dp = valget('dp');
$hp = valget('hp');
$dr = valget('dr');
$hr = valget('hr');

if ((chkdate($dp) == FALSE) OR (chkdate($dr) == FALSE)) {
    $pbm_pen = TRUE;
}

if ((heurecorrecte($hp . ':00') == FALSE) OR (heurecorrecte($hr . ':00') == FALSE)) {
    $pbm_pen = TRUE;
}

$msg = '';
$retourliste = TRUE;
if ($pbm_pdt == FALSE) {
    $diff_secondes = diff_dateheure_sec($dp . ' ' . $hp . ':00', $dr . ' ' . $hr . ':00');
    switch ($td) {
        // Ajout de la pénalité de location
        case 'ret' :
            if (isset($_SESSION['pdtlpl'])) {
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');

                // Calculs du total HT pour le barème choisi
                // Récupération enregistrement barème location
                if ($_SESSION['pdtlbl'][0]['idbarloc'] <> 0) {
                    $rech2 = 'SELECT * FROM `' . TBBARLOC . "` WHERE barloc_id='" . $_SESSION['pdtlbl'][0]['idbarloc'] . "'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2) {
                        extract($row2);
                    }
                }
                close_database();

                $qte = $_SESSION['pdtlbl'][0]['qte'];

                // Calculs divers
                //... Montant location
                $pvht = $_SESSION['pdtlbl'][0]['pvht'];
                $tothtloc = round(($qte * $pvht) * 100) / 100;
                //... Caution
                $pvht = $_SESSION['pdtlbl'][0]['caution'];
                $tothtcaut = round($pvht * 100) / 100;
                //... Assurance
                $tothtass = 0;
                if ($_SESSION['pdtlbl'][0]['avecass'] == 1) {
                    $pvht = $_SESSION['pdtlbl'][0]['mtass'];
                    $tothtass = round(($qte * $pvht) * 100) / 100;
                }
                //...Consommables
                //... Consommable 1
                $pvht = $_SESSION['pdtlbl'][0]['puconsi1'];
                $tothtcons1 = round(($qte * $pvht) * 100) / 100;
                //... Consommable 2
                $pvht = $_SESSION['pdtlbl'][0]['puconsi2'];
                $tothtcons2 = round(($qte * $pvht) * 100) / 100;

                // Total HT
                $totbarht = $tothtloc + $tothtcaut + $tothtass + $tothtcons1 + $tothtcons2;

                // Calcul du montant de la pénalité
                $tot_penalite = calcul_penalite($_SESSION['pdtlpl'][0]['penposs'], $totbarht, $diff_secondes);
                $_SESSION['pdtlpl'][0]['codepen'] = $_SESSION['pdtlpl'][0]['penposs'];
                $_SESSION['pdtlpl'][0]['valeur'] = $tot_penalite;

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

        // Suppression de la pénalité de location
        case 'del' :
            $_SESSION['pdtlpl'][0]['codepen'] = '-1';
            $_SESSION['pdtlpl'][0]['valeur'] = 0;
            $msg = _('Pénalité supprimée');
            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>
