<?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/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(valsession('code_log'));
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

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

$pbm_pdt = FALSE;
$puht = 0;

if (CheckNivLog('3-4')) {
    $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);

$titre = '';
switch ($td) {
    case 'add' :
        $titre = _('CREATION ARTICLE');
        $pdt_codetva = give_code_tva_default(TRUE);
        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 valsession('num_nbdec'); ?>);
            init_calc_taxe('puht', 'puht', 'pu', 'tva', <?php echo valsession('num_nbdec'); ?>, 'to_ttc');
            init_fdeci('pu', <?php echo valsession('num_nbdec'); ?>);
            init_calc_taxe('pu', 'puht', 'pu', 'tva', <?php echo valsession('num_nbdec'); ?>, 'to_ht');
            init_calc_taxe('tva', 'puht', 'pu', 'tva', <?php echo valsession('num_nbdec'); ?>, 'to_ttc');
            <?php
            switch ($td)
            {
                case 'add' :
                    echo "init_valid_form('enr','pdt_maj','".valsession('date_fmtshort')."',tab1_champ,'','');";
                    break;
                case 'edit' :
                    echo "init_valid_form('enr','pdt_maj','".valsession('date_fmtshort')."',tab1_champ,'','');";
                    echo "readonly_id('code',false);";
                    if ((valsession('niv_log') <> 2) AND (valsession('niv_log') <> 7) AND (valsession('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, 'utf8');
                        $rech = 'SELECT * FROM `' . TBFAM . '` ORDER BY fam_desc';
                        $rows = $db->get_results($rech, ARRAY_A);
                        close_database();

                        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, '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', '', $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, 'utf8');
                        $rech = 'SELECT * FROM `' . TBPDTU . '` ORDER BY pdtu_desc';
                        $rows = $db->get_results($rech, ARRAY_A);
                        close_database();

                        // 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 ((valsession('niv_log') <> 2) AND (valsession('niv_log') <> 7) AND (valsession('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 (valsession('niv_log') <> 2) AND (valsession('niv_log') <> 7) AND (valsession('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>