<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.2
 *
 * Copyright (C) 2006-2015  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)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == 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(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$td = @$_GET['td'];
$ibl = intval(@$_GET['nbl']);
$itc = intval(@$_GET['ntc']);
$nc = intval(@$_GET['nc']);
$nextmin = intval(@$_GET['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);

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

        $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
                $row = lit_enr_tcons_id($itc);
                if (is_array($row))
                {
                    $row = encode_str($row);
                    extract($row);
                }
                else
                    $pbm_tc = TRUE;
            } else
                $pbm_tc = TRUE;
        } else {
            if ($itc < 0)
                $pbm_tc = TRUE;
        }

        // Récupération des informations TVA
        $row = lit_enr_tva($barloc_tvacons1, TRUE);
        if (is_array($row))
        {
            $row = encode_str($row);
            extract($row);
        }
        $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_all() {
            init_Body();
            init_fdeci('puht', <?PHP echo $_SESSION['num_nbdec']; ?>,'');
            init_calc_taxe('puht', 'puht', 'puttc', 'tva', <?PHP echo $_SESSION['num_nbdec']; ?>, 'to_ttc');
            init_fdeci('puttc', <?PHP echo $_SESSION['num_nbdec']; ?>,'');
            init_calc_taxe('puttc', 'puht', 'puttc', 'tva', <?PHP echo $_SESSION['num_nbdec']; ?>, 'to_ht');
            <?PHP
            /*init_calc_taxe('tva', 'pu', 'puttc', 'tva', <?PHP echo $_SESSION['num_nbdec']; ?>, 'to_ttc');*/
            switch ($td)
            {
                case "add" ;
                    echo "readonly_id('tva',false);";
                    echo "init_valid_tcons_penbar('enr','barloctc_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "edit" :
                    echo "readonly_id('tva',false);";
                    echo "init_valid_tcons_penbar('enr','barloctc_maj','".$_SESSION['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) {
    ?>
    <p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
    <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 intval($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 intval($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);
                    $rech = "SELECT * FROM `" . TBTVA . "` WHERE tva_actif=1 ORDER BY tva_desc";
                    $rows = $db->get_results($rech, ARRAY_A);
                    $db->disconnect();
                    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);
                    $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 . '">';
                        }
                    }
                    $db->disconnect();
                    ?>
                </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>
