<?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/fic_droits.inc.php");
include_once("../inc/fic_pdt.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_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(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

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

$pbm_pdt = FALSE;
$puht = 0;

if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
    $td = "view";

if ($coderech <> "") {
    // Récupération des informations produit
    $row = lit_enr_pdt($coderech);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
    }

    // Récupération des informations TVA
    $row = lit_enr_tva($pdt_codetva, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
    }

    $txtva = $tva_tx / 100;
    $puht = round(($pdt_puttc / (1 + $txtva)) * 100) / 100;
} else {
    if ($td == "add")
        $pdt_vendu = TRUE;
    else
        $pbm_pdt = TRUE;
}

$codeadev = give_dev_codea_defaut($_SESSION["lng"], TRUE);

switch ($td) {
    case "add" :
        $titre = _("CREATION ARTICLE");
        break;
    case "edit" :
        $titre = _("MISE A JOUR ARTICLE");
        break;
    case "view" :
        $titre = _("DETAIL ARTICLE");
        break;
    case "del" :
        $titre = "SUPPRESSION ARTICLE";
        break;
    default :
        $pbm_pdt = 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 Article"); ?></title>

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

    <script language="javascript">
        <!--
        var tab1_champ = ["nom:code ; format:txt ; req:Y ; txt:<?PHP echo _("Code"); ?> ; lmx:20 ; stok:frmok ; stbad:frmbad",
            "nom:codeb ; format:txt ; req:N ; txt:<?PHP echo _("Code-barre"); ?> ; lmx:20 ; stok:frmok ; stbad:frmbad",
            "nom:fam ; format:liste ; req:Y ; txt:<?PHP echo _("Famille"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:fam ; format:txt ; req:Y ; txt:<?PHP echo _("Famille"); ?> ; stok:frmok ; stbad:frmbad",
            "nom:des; format:txt ; req:Y ; txt:<?PHP echo _("Libellé"); ?> ; lmx:50 ; stok:frmok ; stbad:frmbad",
            "nom:det; format:txt ; req:N ; txt:<?PHP echo _("détail");  ?> ; lmx:70 ; stok:frmok ; stbad:frmbad",
            "nom:tva; format:liste ; req:Y ; txt:<?PHP echo _("TVA"); ?> ; vmn:1 ; 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:pu; format:reel_ns ; req:Y ; txt:<?PHP echo _("PU TTC"); ?> ; lmx:10 ; vmn:0.01 ; stok:frmok ; stbad:frmbad",
            "nom:uv; format:liste ; req:Y ; txt:<?PHP echo _("unité de vente"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <script type="text/javascript">

        <?PHP cre_html_tpfa('dettpfa', $td, $pdt_code, ''); ?>

        <!--
        $(function () {
            $("#code").Setcase({caseValue: 'upper', changeonFocusout: true});
            $("#des").Setcase({caseValue: 'upper', changeonFocusout: true});
        });

        function init_all() {
            init_Body();
            init_fdeci('puht', <?PHP echo $_SESSION['num_nbdec']; ?>);
            init_calc_taxe('puht', 'puht', 'pu', 'tva', <?PHP echo $_SESSION['num_nbdec']; ?>, 'to_ttc');
            init_fdeci('pu', <?PHP echo $_SESSION['num_nbdec']; ?>);
            init_calc_taxe('pu', 'puht', 'pu', 'tva', <?PHP echo $_SESSION['num_nbdec']; ?>, 'to_ht');
            init_calc_taxe('tva', 'puht', 'pu', 'tva', <?PHP echo $_SESSION['num_nbdec']; ?>, 'to_ttc');
            <?php
            switch ($td)
            {
                case "add" :
                    echo "init_valid_form('enr','pdt_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "edit" :
                    echo "init_valid_form('enr','pdt_maj','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    echo "readonly_id('code',false);";
                    if ((@$_SESSION["niv_log"] <> 2) AND (@$_SESSION["niv_log"] <> 7) AND (@$_SESSION["niv_log"] <> 6))
                        echo "init_fensaisie('chx_tpf',900,500);";
                    echo "tbtpfa();";
                    break;
                case "view" :
                    echo "readonly_all();";
                    break;
                case "del" :
                    echo "readonly_all();";
                    echo "warn_del('');";
                    break;
            }
            ?>
            init_back('annul', 'click');
            Focus_first();
        }

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

</head>

<body>
<?PHP
include_once("../inc/entete.inc.php");
if ($pbm_pdt == FALSE) {
    ?>
    <!-- affiché uniquement s'il y a au moins famille -->
    <?PHP
    if ((nb_fam() > 0) AND (nb_pdtu() > 0)) {
        cre_ent_form($titre, 'pdt_lst.php?ta=m', $_SERVER['HTTP_REFERER'], '');
        ?>
        <form name="pdt_maj" id="pdt_maj" method="post" action="pdt_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="">
            <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 $pdt_code ?>" size="30"
                               maxlength="20">
                        <?PHP
                        if ($td == "add")
                            echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _("Champ obligatoire") . '">';
                        ?>
                    </td>
                </tr>
                <tr>
                    <td width="160" class="rubfrm" id="f_codeb" align="right"><?PHP echo _("Code-barre"); ?></td>
                    <td>
                        <input name="codeb" type="text" id="codeb" value="<?PHP echo $pdt_codeb ?>" size="30"
                               maxlength="20">
                    </td>
                </tr>
                <tr>
                    <td width="160" class="rubfrm" id="f_fam" align="right"><?PHP echo _("Famille"); ?></td>
                    <td>
                        <?PHP
                        // création de la liste des familles
                        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                        $rech = "SELECT * FROM `" . TBFAM . "` ORDER BY fam_desc";
                        $rows = $db->get_results($rech, ARRAY_A);
                        $db->disconnect();

                        cre_select_tab('fam', 'fam', $rows, 'fam_code', 'fam_desc', '', $pdt_fam, '', _("-- Faites votre choix --"));
                        ?>
                    </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 $pdt_des ?>" size="50">
                        <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
                    </td>
                </tr>
                <tr>
                    <td width="160" class="rubfrm" id="f_det" align="right"><?PHP echo _("Détail"); ?></td>
                    <td>
                        <input name="det" type="text" id="det" value="<?PHP echo $pdt_detail ?>" size="70">
                    </td>
                </tr>
                <tr>
                    <td width="160" class="rubfrm" id="f_pu" align="right"><?PHP echo _("PU HT"); ?></td>
                    <td>
                        <?PHP cre_input_num('puht', $puht, TRUE, FALSE); ?>
                    </td>
                </tr>
                <tr>
                    <td width="160" 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', '', $pdt_codetva, '', _("-- Faites votre choix --"));

                        // création des taux de TVA (champs cachés)
                        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 . '">';
                            }
                        }
                        ?>
                    </td>
                </tr>
                <tr>
                    <td width="160" class="rubfrm" id="f_pu" align="right"><?PHP echo _("PU TTC"); ?></td>
                    <td>
                        <?PHP cre_input_num('pu', $pdt_puttc, TRUE, FALSE); ?>
                    </td>
                </tr>
                <tr>
                    <td width="160" class="rubfrm" id="f_uv" align="right"><?PHP echo _("Unité de vente"); ?></td>
                    <td>
                        <?PHP
                        // création de la liste des unités
                        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                        $rech = "SELECT * FROM `" . TBPDTU . "` ORDER BY pdtu_desc";
                        $rows = $db->get_results($rech, ARRAY_A);
                        $db->disconnect();

                        // echo '>' . $pdtu_desc . ' (' . $pdtu_abr . ')</option>';
                        cre_select_tab('uv', 'uv', $rows, 'pdtu_code', 'pdtu_desc', '', $pdt_codepdtu, '', _("-- Faites votre choix --"));
                        ?>
                    </td>
                </tr>
                <tr>
                    <td class="rubfrm" id="f_vente" align="right"><?PHP echo _("Commercialisé"); ?></td>
                    <td>
                        <?PHP cre_select('vendu', 'vendu', $non_oui, (int)$pdt_vendu); ?>
                    </td>
                </tr>
            </table>

            <!-- Zone de saisie des taxes parafiscales -->
            <?PHP
            if (($td == "edit") OR ($td == "del") OR ($td == "view")) {
                if ((@$_SESSION["niv_log"] <> 2) AND (@$_SESSION["niv_log"] <> 7) AND (@$_SESSION["niv_log"] <> 6)) {
                    $urlchoixtpf = "tpf_lst.php?ta=p&td=" . $td . "&cp=" . $coderech;
                    ?>
                    <table width="100%" border="0">
                        <tr>
                            <td class="rubfrm" colspan="2" align="center"><?PHP echo _("TAXES"); ?></td>
                        </tr>
                    </table>
                    <div id="dettpfa">
                    </div>

                    <!-- Lien d'ajout de taxes-->
                    <?PHP
                    if ($td <> "del") {
                        if (($td == "edit") AND (@$_SESSION["niv_log"] <> 2) AND (@$_SESSION["niv_log"] <> 7) AND (@$_SESSION["niv_log"] <> 6)) {
                            cre_falselnk('', 'chx_tpf', 'div', '', _("Ajouter une taxe parafiscale"), $urlchoixtpf, '../img/taxe.png', _("Ajouter une taxe parafiscale"));
                        }
                    }
                }
            }
            ?>
            <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="submit" class="bton_std" id="del" value="'._("Supprimer").'">';
                    break;
            }
            echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="'._("Réinitialiser").'">';
            echo '<input name="annul" type="button" class="bton_std" id="annul" value="'._("Retour liste").'">';
            ?>
            </p>
        </form>
        <!-- Fin affiché uniquement s'il y a au moins une famille article -->
    <?PHP
    } else {
        $txt_err = "";
        if (nb_fam() == 0)
            $txt_err = _("Merci de créer au moins une famille.");
        if (nb_pdtu() == 0)
            $txt_err .= "<br />" . _("Merci de créer au moins une unité de vente.");
        pop_ret_auto($txt_err, 'warn', "index.php");
    }
} else
    pop_ret_auto(_("Problème de paramètres"), 'warn', "pdt_lst.php");
include_once("pied.php");
?>
</body>
</html>