<?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/fic_droits.inc.php');
include_once('../inc/fic_pen.inc.php');
include_once('../inc/fic_prm.inc.php');
include_once('../inc/func.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();
$_GET = clean_str($_GET);

$td = $_GET['td'];
$coderech = $_GET['code'];

$pbm_pen = FALSE;
$penmod = TRUE;
$pendesact = TRUE;

if ($coderech <> '') {
    $row = lit_enr_pen($coderech);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
        if ((pen_modifiable($coderech, TRUE) == FALSE) AND ($td <> 'del')) {
            $td = 'view';
        }
        $pendesact = pen_desactivable($coderech, TRUE);
    } else {
        $pbm_pen = TRUE;
    }
} else {
    if ($td <> 'add') {
        $pbm_pen = TRUE;
    }
}

$nextmin = 1;
if ($pbm_pen == FALSE) {
    $codeadev = give_dev_codea_defaut($_SESSION['lng'], TRUE);

    switch ($td) {
        case 'add' :
            $titre = _('CREATION PENALITE');
            break;
        case 'edit' :
            $titre = _('MODIFICATION PENALITE');
            $nextmin = next_min_penbar($pen_code, TRUE);
            break;
        case 'del' :
            $titre = _('SUPPRESSION PENALITE');
            break;
        default :
            $pbm_pen = TRUE;
            break;
    }
}
?>
<!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 _('Fiche Pénalité Location'); ?></title>

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

    <script language="javascript">
        <!--
        var tab_champ = ["nom:code ; format:alphanum ; req:Y ; txt:<?php echo _('Code'); ?> ; lmn:1 ; lmx:10; stok:frmok ; stbad:frmbad",
            <?php if ($td<> 'add') { ?>
            "nom:valeur; format:reel_ns ; req:N ; txt:<?php echo _('Valeur'); ?>; lmx:10; stok:frmok ; stbad:frmbad",
            <?php } ?>
            "nom:desc ; format:txt ; req:Y ; txt:<?php echo _('Libellé'); ?>; lmn:1 ; lmx:40; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <script type="text/javascript">
        <!--
        <?php cre_html_penbar('detbarpen', $td, $pen_code, $pen_pour, ''); ?>

        $(function () {
            $("#code").Setcase({caseValue: 'upper', changeonFocusout: true});
            $("#desc").Setcase({caseValue: 'upper', changeonFocusout: true});
        });

        function init_all() {
            init_Body();
            init_fdeci('valeur', <?php echo valsession('num_nbdec'); ?>, '');
            chk_tbar('tbar');
            chk_pour('pour', 'sympour', 'pas_sympour');
            chk_pour('pour', 'txtpour2', 'pas_txtpour2');
            <?php
             switch ($td)
             {
                case 'add':
                    echo "init_valid_form('enr','pen_maj','".valsession('date_fmtshort')."',tab_champ,'','');";
                    echo "init_chk_tbar('tbar');";
                    echo "init_chk_pour('pour','sympour','pas_sympour');";
                    echo "init_chk_pour('pour','txtpour2','pas_txtpour2');";
                    break;
                case 'edit':
                    echo "init_valid_form('enr','pen_maj','".valsession('date_fmtshort')."',tab_champ,'','');";
                    echo "init_chk_tbar('tbar');";
                    echo "init_chk_pour('pour','sympour','pas_sympour');";
                    echo "init_chk_pour('pour','txtpour2','pas_txtpour2');";
                    echo "init_fen_penbar_pen('addpbar','add','".$pen_code."',0);";
                    break;
                case 'del':
                    echo 'readonly_all();';
                    echo "warn_del('');";
                    break;
                case 'view':
                    echo 'readonly_all();';
                    break;

             }
            ?>
            tbpenbar();
            Focus_first();
        }

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

<?php include_once('../inc/entete.inc.php');
if ($pbm_pen == FALSE) {
    cre_ent_form($titre, 'pen_lst.php?ta=m', $_SERVER['HTTP_REFERER'], '');
    ?>
    <form name="pen_maj" id="pen_maj" method="post" action="pen_enr.php">
        <input name="titre" type="hidden" id="titre" value="<?php echo $titre ?>">
        <input name="td" type="hidden" id="td" value="<?php echo $td ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <?php if ($td <> 'add') { ?>
            <input name="nextmin" type="hidden" id="nextmin" value="<?php echo $nextmin; ?>">
        <?php } ?>
        <table width="100%" border="0">
            <tr>
                <td width="10%" class="rubfrm" align="right" id="f_code"><?php echo _('Code'); ?></td>
                <td width="47%">
                    <input name="code" type="text" id="code" value="<?php echo $pen_code ?>" size="10">
                    <?php
                    if ($td == 'add') {
                        echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _('Champ obligatoire') . '">';
                    }
                    ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" align="right" id="f_desc"><?php echo _('Libellé'); ?></td>
                <td><input name="desc" type="text" id="desc" value="<?php echo $pen_desc; ?>" size="50">
                    <img src="../img/oblig.png" align="absmiddle" alt="<?php echo _('Champ obligatoire'); ?>"></td>
            </tr>
            <?php if ($td <> 'add') { ?>
                <tr>
                    <td class="rubfrm" align="right" id="f_tbar"><?php echo _('Type barême'); ?></td>
                    <td>
                        <?php
                        $tab_tbar = [];
                        $tab_tbar['0'] = _('Valeur unique');
                        $tab_tbar['1'] = _('Grille de valeurs');
                        cre_select('tbar', 'tbar', $tab_tbar, $pen_tbar);
                        echo '<div class="annot_petit_it">';
                        echo _("'Valeur unique' : La pénalité est une valeur unique");
                        echo '<br />';
                        echo _("'Grille de valeurs' : La pénalité est établie selon une grille");
                        echo '</div>';
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class="rubfrm" align="right" id="f_valeur" align="left">
                        <span id="txtpour2" style="display:none"><?php echo _('Valeur'); ?></span>
                        <span id="pas_txtpour2" style="display:block"><?php echo _('Valeur HT'); ?></span>
                    </td>
                    <td>
                        <input name="valeur" type="text" id="valeur" style="text-align:right"
                               value="<?php echo $pen_valeur; ?>" size="10">
                        <span id="sympour" style="visibility:visible" align="left"><?php echo _('%'); ?></span>
                        <span id="pas_sympour" style="visibility:visible"
                              align="left"><?php echo trim(give_dev_ISO()); ?></span>
                    </td>
                </tr>
                <tr>
                    <td class="rubfrm" align="right" id="f_pour"><?php echo _('Pourcentage ?'); ?></td>
                    <td>
                        <?php
                        cre_select('pour', 'pour', $non_oui, (int)$pen_pour);
                        echo '<div class="annot_petit_it">';
                        echo _("'Oui' : la valeur est un pourcentage du barême");
                        echo '<br />' . _("'Non' : la valeur est un montant");
                        echo '</div>';
                        ?>
                    </td>
                </tr>
                <tr>
                    <td width="160" class="rubfrm" align="right" id="f_udec"
                        align="right"><?php echo _('Unité de décompte'); ?></td>
                    <td>
                        <?php
                        cre_select('udec', 'udec', $lng_tduree, $pen_udec);
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class="rubfrm" align="right" id="f_prop"><?php echo _('Proportionnel ?'); ?></td>
                    <td>
                        <?php
                        cre_select('prop', 'prop', $non_oui, (int)$pen_prop);
                        echo '<div class="annot_petit_it">';
                        echo _("'Non' : la valeur sera valable quelque soit le nombre d'unités de retard");
                        echo '<br />';
                        echo _("'Oui' : la valeur sera proportionnelle au nombre d'unités de retard");
                        echo '</div>';
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class="rubfrm" align="right" id="f_actif"><?php echo _('Active'); ?></td>
                    <td>
                        <?php
                        cre_select('actif', 'actif', $non_oui, (int)$pen_actif);
                        echo '<div class="annot_petit_it">';
                        echo _('Vous ne pouvez pas désactiver une pénalité déjà utilisée dans une location non close');
                        echo '</div>';
                        ?>
                    </td>
                </tr>
            <?php } ?>
        </table>
        <!-- Zone de saisie de la grille tarifaire des pénalités -->
        <div id="detpenbar">
            <?php
            if (($td == 'edit') OR ($td == 'del') OR ($td == 'view')) {
                ?>
                <table width="100%" border="0">
                    <tr>
                        <td class="rubfrm" colspan="2" align="center">
                            <?php echo _('GRILLE DE TARIFICATION'); ?>
                        </td>
                    </tr>
                </table>
                <div id="detbarpen">
                </div>

                <!-- Lien d'ajout de barèmes -->
                <?php
                if ($td == 'edit') {
                    ?>
                    <div id="addbp" style="<?php if (exist_infini_penbar_code($pen_code) OR ($pen_tbar == 0)) {
                        echo 'display:none';
                    } else {
                        echo 'display:block';
                    } ?>">
                        <?php
                        cre_falselnk('addpbar', 'addpbar', 'span', '', _('Ajouter barême'), '', '../img/add.png', _('Ajouter barême'));
                        ?>
                    </div>
                    <?php
                }
            }
            ?>
        </div>
        <p align="center">
            <?php
            switch ($td) {
                case 'add' :
                case 'edit' :
                    echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="' . _('Réinitialiser') . '">';
                    echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _('Enregistrer') . '">';
                    break;
                case 'del' :
                    echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _('Supprimer') . '">';
                    break;
            }
            ?>
        </p>
    </form>
    <?php
} else {
    pop_ret_auto(_('Problème de paramètres'), 'warn', 'pen_lst.php?ta=m');
}
include_once('pied.php');
?>
</body>
</html>