<?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_int.inc.php");
include_once("../inc/fic_pdt.inc.php");
include_once("../inc/fic_taxes.inc.php");
include_once("../inc/func.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'];
$codeint = @$_GET['ci'];
$idinth = intval(@$_GET['cih']);
$codetpf = @$_GET['tpf'];

$pbm_int = FALSE;
$pbm_tpf = FALSE;

if ($codeint <> "") {
    $row = lit_enr_int($codeint);
    if (is_array($row))
        extract($row);
    else
        $pbm_int = TRUE;
}

if ($pbm_int == FALSE) {
    if ($codetpf <> "") {
        // Récupération des données de la fiche taxe
        $row = lit_enr_tpf($codetpf);
        if (is_array($row)) {
            extract($row);

            switch ($td) {
                case "add" :
                    $tpfih_codeint = $codeint;
                    $tpfih_codetpf = $tpf_code;
                    $tpfih_valeur = $tpf_valeur;
                    $tpfih_codetva = $tpf_codetva;
                    $tpfih_qte = 1;
                    $tpfih_texte = "";
                    break;

                case "edit" :
                case "del" :
                    $row = recup_tpf_tab_in_inth($codetpf);
                    if (is_array($row))
                        extract($row);
                    else
                        $pbm_tpf = TRUE;
                    break;
            }

            if ($pbm_tpf == FALSE) {
                // Récupération information TVA par client
                $cliexo = FALSE;
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT int_codecli FROM `" . TBINT . "` WHERE `int_code` = '$codeint'";
                $row = $db->get_row($rech, ARRAY_A);
                if ($row) {
                    extract($row);

                    $rech2 = "SELECT cli_modfact FROM `" . TBCLI . "` WHERE `cli_code` = '$int_codecli'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2) {
                        extract($row2);

                        if ($cli_modfact == 2) {
                            $cliexo = TRUE;
                            $tpfih_codetva = "TVA0";
                        }
                    }
                }
                $db->disconnect();

                // Récupération des informations TVA
                $row = lit_enr_tva($tpfih_codetva, TRUE);
                if (is_array($row))
                    extract($row);
                $txtva = $tva_tx / 100;

                $puht = round(($tpfih_valeur / (1 + $txtva)) * 100) / 100;
            }
        }
    } else
        $pbm_tpf = TRUE;
} else {
    if ($td == "add") {
        $tpfih_codeint = $codeint;
        $tpfih_codetpf = $tpf_code;
        $tpfih_valeur = $tpf_valeur;
        $tpfih_qte = 1;
        $tpfih_texte = "";
    } else
        $pbm_tpf = TRUE;
}

$titre = '';
switch ($td) {
    case "add" :
        $titre = _("SAISIE TAXE");
        break;
    case "edit" :
        $titre = _("EDITION TAXE");
        break;
    case "del" :
        $titre = _("SUPPRESSION TAXE");
        break;
    default :
        $pbm_tpf = 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 _("Taxe Intervention"); ?></title>

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

    <script language="javascript">
    <!--
    var tab1_champ = ["nom:qte ; format:reel_ns ; req:Y ; txt:<?PHP echo _("Quantité"); ?> ; vmn : 0.01 ; lmx:10 ; stok:frmok ; stbad:frmbad",
        <?PHP if ($tpf_pour==FALSE) { ?>
        "nom:puht; format:reel_ns ; req:Y ; txt:<?PHP echo _("PU HT"); ?>; vmn : 0.01 ; lmx:10 ; stok:frmok ; stbad:frmbad",
        <?PHP } ?>
        "nom:pu; format:reel_ns ; req:Y ; txt:<?PHP echo _("PU TTC"); ?> ; vmn : 0.01 ; lmx:10 ; stok:frmok ; stbad:frmbad",
        "nom:texte; format:txt ; req:N ; txt:<?PHP echo _("Commentaire"); ?> ; stok:frmok ; stbad:frmbad"];
    //-->
    </script>


    <?PHP if ($pbm_tec == FALSE) { ?>
        <script type="text/javascript">
            <!--
            function init_all() {
                init_Body();
                init_fdeci('qte', 2,'');
                init_fdeci('puht', <?PHP echo $_SESSION['num_nbdec']; ?>,'');
                init_calc_taxe('puht', 'pu', 'puttc', 'tva', <?PHP echo $_SESSION['num_nbdec']; ?>, 'to_ttc');
                init_fdeci('pu', <?PHP echo $_SESSION['num_nbdec']; ?>,'');
                <?PHP
                if ($tpf_pour == FALSE)
                    echo "init_calc_taxe('pu','pu','puttc','tva',".$_SESSION['num_nbdec'].",'to_ht');";
                switch ($td)
                {
                    case "add" :
                    case "edit" :
                        echo "init_valid_form('enr','tpfih_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                        break;
                    case "del" :
                        echo "readonly_all();";
                        echo "warn_del('');";
                        break;
                    case "view" :
                        echo "readonly_all();";
                        break;
                }
                ?>
                add_func_click('annul', 'sclose', '');
                Focus_first();
            }

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

<body>
<?PHP
if (($pbm_tpf == FALSE) AND ($pbm_int == FALSE)) {
    ?>
    <p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
    <p align="center"><span class="titre_gras"><?PHP echo _("Pour l'intervention"); ?>
            : <?php echo $tpfih_codeint; ?> </span></p>

    <form name="tpfih_maj" id="tpfih_maj" method="post" action="tpfih_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="codeint" type="hidden" id="codeint" value="<?PHP echo $tpfih_codeint ?>">
        <input name="inth" type="hidden" id="inth" value="<?PHP echo $idinth; ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <table width="100%" border="0">
            <tr>
                <td width="160" class="rubfrm" id="f_code" align="right"><?PHP echo _("Code"); ?></td>
                <td>
                    <input name="code" type="text" id="code" value="<?PHP echo $tpfih_codetpf ?>" size="50" maxlength="50" readonly>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_des" align="right"><?PHP echo _("Libellé"); ?></td>
                <td>
                    <input name="des" type="text" id="des" value="<?PHP echo $tpf_desc; ?>" size="50" maxlength="50" readonly>
                </td>
            </tr>
            <tr>
                <td width="30%" class="rubfrm" id="f_qte" align="right"><?PHP echo _("Quantité"); ?></td>
                <td>
                <?PHP
                cre_input_num('qte', $tpfih_qte);
                ?>
                </td>
            </tr>
            <?PHP if ($tpf_pour == FALSE) { ?>
                <tr>
                    <td width="160" class="rubfrm" id="f_puht" align="right"><?PHP echo _("PU HT"); ?></td>
                    <td>
                        <?PHP
                        if ($tpf_asaisir == TRUE)
                            cre_input_num('puht', $puht, TRUE);
                        else
                            cre_input_num('puht', $puht, TRUE, TRUE);
                        ?>
                    </td>
                </tr>
                <tr>
                    <td width="160" class="rubfrm" id="f_tva" align="right"><?PHP echo _("TVA"); ?></td>
                    <td>
                        <input name="tva" type="hidden" id="tva" value="<?PHP echo $tpfih_codetva ?>">
                        <input name="ntva" type="text" id="ntva" align="right" value="<?PHP echo $tva_desc ?>" size="40"
                               readonly>
                        <?PHP
                        // 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) {
                                $row = encode_str($row);
                                extract($row);
                                echo '<input name="' . $tva_code . '" type="hidden" id="' . $tva_code . '" value="' . $tva_tx . '">';
                            }
                        }
                        $db->disconnect();

                        if ($cliexo == TRUE)
                            echo '<div class="annot_petit_it">' . _("Ce client est exonéré de TVA") . '</div>';
                        ?>
                    </td>
                </tr>
            <?PHP } ?>
            <tr>
                <td width="160" class="rubfrm" id="f_pu" align="right"><?PHP if ($tpf_pour == FALSE) echo _("Valeur TTC"); else echo _("Pourcentage"); ?></td>
                <td>
                    <?PHP
                    $readonly = FALSE;
                    if ($tpf_asaisir == FALSE)
                        $readonly = TRUE;
                    if ($tpf_pour == TRUE)
                    {
                        cre_input_num('pu', $tpfih_valeur, FALSE, $readonly);
                        echo "%";
                    }
                    else
                        cre_input_num('pu', $tpfih_valeur, TRUE, $readonly);
                    if ($tpf_typmt == TRUE)
                        echo '<div class="annot_petit_it">'._("La valeur saisie ou calculée sera unique").'</div>';
                    else
                        echo '<div class="annot_petit_it">'._("La valeur saisie ou calculée sera proportionnelle au nombre d'unités vendues").'</div>';
                    if ($tpf_asaisir == FALSE)
                        echo '<div class="annot_petit_it">'._("Cette valeur n'est pas modifiable").'</div>';


                    if ($tpf_asaisir == TRUE) {
                        if ($tpf_pour == FALSE)
                            cre_input_num('pu', $tpfih_valeur, TRUE, FALSE);
                        else {
                            cre_input_num('pu', $tpfih_valeur, FALSE, TRUE);
                            echo ' %';
                        }
                    }
                    ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_texte" align="right"><?PHP echo _("Commentaire"); ?></td>
                <td>
                    <textarea name="texte" id="texte" rows="4"><?PHP echo $tpfih_texte; ?></textarea>
                </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('', '');
?>
</body>
</html>
