<?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_prm.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_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);

$td = valget('td');
$ibl = (int)valget('nbl');
$itc = (int)valget('ntc');
$nc = (int)valget('nc');
$nextmin = (int)valget('nm');
$pbm_tc = FALSE;
if (($nc <> 1) AND ($nc <> 2)) {
    $pbm_tc = TRUE;
}

if (($ibl > 0) AND ($pbm_tc == FALSE)) {
    // Récupération des données du barème
    $row = lit_enr_barloc_id($ibl, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        $row2 = lit_enr_pdtloc($barloc_codepdtloc);
        if (is_array($row2)) {
            $row2 = encode_str($row2);
            extract($row2);
        }

        $codeadev = give_dev_codea_defaut_pdtloc($barloc_codepdtloc, $_SESSION['lng']);

        if ($itc > 0) {
            if ($itc == last_id_tarcons_in_barloc($ibl, $nc, TRUE)) {
                // Récupération des données du tarif consommable
                $row2 = lit_enr_tcons_id($itc);
                if (is_array($row2)) {
                    $row2 = encode_str($row2);
                    extract($row2);
                } else {
                    $pbm_tc = TRUE;
                }
            } else {
                $pbm_tc = TRUE;
            }
        } else {
            if ($itc < 0) {
                $pbm_tc = TRUE;
            }
        }

        // Récupération des informations TVA
        $row2 = lit_enr_tva($barloc_tvacons1, TRUE);
        if (is_array($row2)) {
            $row2 = encode_str($row2);
            extract($row2);
        }
        $txtva = $tva_tx / 100;
        $puttc = round(($barloctc_pvht * (1 + $txtva)) * 100) / 100;
    } else {
        $pbm_tc = TRUE;
    }
} else {
    $pbm_tc = TRUE;
}

switch ($td) {
    case 'add' :
        $titre = _('AJOUT TARIF');
        if (exist_infini_tcons_id($ibl, $nc, TRUE) == FALSE) {
            $barloctc_idbarloc = $ibl;
            $barloctc_ncons = $nc;
            $barloctc_min = $nextmin;
            $barloctc_max = $nextmin + 1;
        } else {
            $pbm_tc = TRUE;
        }
        break;
    case 'edit' :
        $titre = _('EDITION TARIF');
        break;
    case 'del' :
        $titre = _('SUPPRESSION TARIF');
        break;
    default :
        $pbm_tc = 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 _('Tarif Consommable'); ?></title>

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

    <script language="javascript">
        <!--
        var tab1_champ = ["nom:mini; format:unsigned ; req:Y ; txt:<?php echo _('Minimum'); ?>; lmx:10 ; stok:frmok ; stbad:frmbad",
            "nom:maxi; format:unsigned ; req:Y ; txt:<?php echo _('Maximum'); ?> ; lmx:10 ; stok:frmok ; stbad:frmbad",
            "nom:puht; format:reel_ns ; req:Y ; txt:<?php echo _('PU HT'); ?> ; lmx:10 ; vmn : 0.01; stok:frmok ; stbad:frmbad",
            "nom:puttc; format:reel_ns ; req:Y ; txt:<?php echo _('PU TTC'); ?> ; lmx:10 ; vmn : 0.01; stok:frmok ; stbad:frmbad"];
        //-->
    </script>


    <script type="text/javascript">
        <!--
        <?php cre_html_tarcons($nc,'dettarcons'.$nc, 'edit', $barloc_id, 'window.opener.'); ?>

        function init_calculator() {
            <?php include_once ('../jscript/calculator_prm.js.php'); ?>
            $('#puht').calculator({
                onClose: function(value, inst) {
                    fdeci(this.id, <?php echo valsession('num_nbdec'); ?>);
                    calc_taxe(this.id, 'puttc', 'tva', <?php echo valsession('num_nbdec'); ?>, 'to_ttc');
                }
            });
            $('#puttc').calculator({
                onClose: function(value, inst) {
                    fdeci(this.id, <?php echo valsession('num_nbdec'); ?>);
                    calc_taxe('puht', this.id, 'tva', <?php echo valsession('num_nbdec'); ?>, 'to_ht');
                }
            });
        }

        function init_all() {
            init_Body();
            init_calculator();
            init_fdeci('puht', <?php echo valsession('num_nbdec'); ?>, '');
            init_calc_taxe('puht', 'puht', 'puttc', 'tva', <?php echo valsession('num_nbdec'); ?>, 'to_ttc');
            init_fdeci('puttc', <?php echo valsession('num_nbdec'); ?>, '');
            init_calc_taxe('puttc', 'puht', 'puttc', 'tva', <?php echo valsession('num_nbdec'); ?>, 'to_ht');
            <?php
            /*init_calc_taxe('tva', 'pu', 'puttc', 'tva', <?php echo valsession('num_nbdec'); ?>, 'to_ttc');*/
            switch ($td)
            {
                case 'add' ;
                    echo "readonly_id('tva',false);";
                    echo "init_valid_tcons_penbar('enr','barloctc_maj','".valsession('date_fmtshort')."',tab1_champ,'','');";
                    break;
                case 'edit' :
                    echo "readonly_id('tva',false);";
                    echo "init_valid_tcons_penbar('enr','barloctc_maj','".valsession('date_fmtshort')."',tab1_champ,'','');";
                    break;
                case 'view' :
                    echo 'readonly_all();';
                    break;
                case 'del' :
                    echo 'readonly_all();';
                    echo "warn_del('');";
                    break;
            }
            echo 'tbtarcons' .$nc. '();';
            ?>
            add_func_click('annul', 'sclose', '');
            Focus_first();
        }

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

</head>

<body>

<?php
if ($pbm_tc == FALSE) {
    cre_ent_form($titre, '', '', '', FALSE);
    ?>
    <p align="center"><?php echo('Pour le barême'); ?> : <span class="titre_gras"><?php echo $barloc_des; ?></span><br/>
        <?php echo _('Du Produit'); ?> : <span class="titre_gras"><?php echo $pdtloc_des; ?> </span></p>

    <form name="barloctc_maj" id="barloctc_maj" method="post" action="barloctc_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="ncons" type="hidden" id="ncons" value="<?php echo $nc ?>">
        <input name="nbl" type="hidden" id="nbl" value="<?php echo $barloc_id; ?>">
        <input name="ntc" type="hidden" id="ntc" value="<?php echo $itc; ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <table width="100%" border="0">
            <tr>
                <td width="25%" class="rubfrm" id="f_mini" align="right"><?php echo _('Minimum'); ?></td>
                <td>
                    <input name="mini" type="text" id="mini" style="text-align:right"
                           value="<?php echo (int)$barloctc_min; ?>" size="10" readonly="1">
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_maxi" align="right"><?php echo _('Maximum'); ?></td>
                <td>
                    <input name="maxi" type="text" id="maxi" style="text-align:right"
                           value="<?php echo (int)$barloctc_max; ?>" size="10">
                    <?php if ($td <> 'del') { ?>
                        <span class="annot_petit_it">(<?php echo _('Au moins'); ?> <?php echo $barloctc_max; ?>
                            , <?php echo _("ou '0' pour infini"); ?>)</span>
                    <?php } ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_pu" align="right"><?php echo _('Tarif HT'); ?></td>
                <td>
                    <?php
                    cre_input_num('puht', $barloctc_pvht, TRUE);
                    ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_tva" align="right"><?php echo _('TVA'); ?></td>
                <td>
                    <?php
                    // création de la liste des TVA
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                    $rech = 'SELECT * FROM `' . TBTVA . '` WHERE tva_actif=1 ORDER BY tva_desc';
                    $rows = $db->get_results($rech, ARRAY_A);
                    close_database();
                    cre_select_tab('tva', 'tva', $rows, 'tva_code', 'tva_desc', '', $barloc_tvacons1, '', _('-- Faites votre choix --'));

                    // Création des taux de TVA (champs cachés)
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                    $rech = 'SELECT * FROM `' . TBTVA . '` WHERE tva_actif=1 ORDER BY tva_desc';
                    $rows = $db->get_results($rech, ARRAY_A);

                    if ($rows) {
                        foreach ($rows as $row) {
                            extract($row);
                            echo '<input name="' . $tva_code . '" type="hidden" id="' . $tva_code . '" value="' . $tva_tx . '">';
                        }
                    }
                    close_database();
                    ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_puttc" align="right"><?php echo _('Tarif TTC'); ?></td>
                <td>
                    <?php
                    cre_input_num('puttc', $puttc, TRUE);
                    ?>
                </td>
            </tr>
        </table>
        <p align="center">
            <?php
            switch ($td) {
                case 'add' :
                case 'edit' :
                    echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _('Valider') . '">';
                    echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="' . _('Réinitialiser') . '">';
                    break;
                case 'del' :
                    echo '<input name="del" type="submit" class="bton_std" id="del" value="' . _('Supprimer') . '">';
                    break;
            }
            echo '<input name="annul" type="button" class="bton_std" id="annul" value="' . _('Annuler') . '">';
            ?>
        </p>
    </form>
    <?php
} else {
    close_fen_auto('', 'fscomp2', '');
}
?>
</body>
</html>
