<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  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_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'];
$codetpf = @$_GET['tpf'];

$pbm_tpf = FALSE;

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

        switch ($td)
        {
            case "add" :
                $tpfi_codeint = $codeint;
                $tpfi_codetpf = $tpf_code;
                $tpfi_valeur = $tpf_valeur;
                $tpfi_codetva = $tpf_codetva;
                $tpfi_qte = 1;
                $tpfi_texte = "";
                break;
            case "edit" :
                $row = lit_enr_tpf_in_int($codetpf, $codeint);
                if (is_array($row))
                    extract($row);
                else
                    $pbm_tpf = TRUE;
                break;
            case "del" :
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                $rech = "SELECT * FROM `" . TBTPFI . "` WHERE `tpfi_codetpf` = '$codetpf' AND `tpfi_codeint` = '" . $db->escape($codeint) . "'";
                $row = $db->get_row($rech, ARRAY_A);
                if ($row)
                    extract($row);
                else
                    $pbm_tpf = TRUE;
                $db->disconnect();
                break;
        }

        // 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` = '" . $db->escape($codeint) . "'";
        $row = $db->get_row($rech, ARRAY_A);
        if ($row) {
            extract($row);

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

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


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

        $puht = round(($tpfi_valeur / (1 + $txtva)) * 100) / 100;

    }
} else {
    if ($td == "add") {
        $tpfi_codeint = $codeint;
        $tpfi_codetpf = $tpf_code;
        $tpfi_valeur = $tpf_valeur;
        $tpfi_qte = 1;
        $tpfi_texte = "";
    } else
        $pbm_tpf = TRUE;
}

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');";
                echo "readonly_id('code-des',false);";
                 switch ($td)
                 {
                    case "add":
                        echo "init_valid_form('enr','tpfi_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                        break;
                    case "edit":
                        echo "init_valid_form('enr','tpfi_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                        break;
                    case "del":
                        echo "init_valid_form('del','tpfi_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                        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) {
    ?>
    <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 $tpfi_codeint; ?> </span></p>

    <form name="tpfi_maj" id="tpfi_maj"  method="post" action="tpfi_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 $tpfi_codeint ?>">
        <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 $tpfi_codetpf ?>" size="50" maxlength="50">
                </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">
                </td>
            </tr>
            <tr>
                <td width="30%" class="rubfrm" id="f_qte" align="right"><?PHP echo _("Quantité"); ?></td>
                <td>
                    <input name="qte" type="text" id="qte" style="text-align:right"
                           value="<?PHP echo full_num_format($tpfi_qte); ?>" size="10">
                    <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
                </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 $tpfi_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) {
                                extract($row);
                                echo '<input name="' . $tva_code . '" type="hidden" id="' . $tva_code . '" value="' . $tva_tx . '">';
                            }
                        }
                        $db->disconnect();

                        if ($cliexo == TRUE)
                            echo '<br /><span class="annot_petit_it">' . _("Ce client est exonéré de TVA") . '</span>';
                        ?>
                    </td>
                </tr>
            <?PHP } ?>
            <tr>
                <td width="160" class="rubfrm" id="f_pu"
                    align="right"><?PHP if ($tpf_pour == FALSE) echo _("PU TTC"); else echo _("Pourcentage"); ?></td>
                <td>
                    <?PHP
                    if ($tpf_asaisir == TRUE) {
                        if ($tpf_pour == FALSE)
                            cre_input_num('pu', $tpfi_valeur, TRUE, FALSE);
                        else {
                            cre_input_num('pu', $tpfi_valeur, FALSE, TRUE);
                            echo ' %';
                        }
                    }
                    echo '<span class="annot_petit_it">' . _("Du montant total des articles") . '</span>';
                    ?>
                </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 $tpfi_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").'">';
                    break;
                case "del" :
                    echo '<input name="del" type="button" class="bton_std" id="del" value="'._("Supprimer").'">';
                    break;
            }
            echo '<input name="annul" type="button" class="bton_std" id="annul" value="'._("Annuler").'">';
            echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="'._("Réinitialiser").'">';
            ?>
        </p>
    </form>
<?PHP
} else
    close_fen_auto('', '');
?>
</body>
</html>
