<?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/fic_droits.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/config.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_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'];
$codeint = @$_GET['ci'];
$codeart = @$_GET['ca'];

$pbm_int = FALSE;
$pbm_pdt = FALSE;

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

if ($pbm_int == FALSE) {
    if ($codeart <> "") {
        // Récupération des données de la fiche article
        $row = lit_enr_pdt($codeart);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);

            if ($td == "add") {
                $pdti_codeint = $codeint;
                $pdti_codepdt = $pdt_code;
                $pdti_puttc = $pdt_puttc;
                $pdti_codetva = $pdt_codetva;
                $pdti_qte = 1;
                $pdti_ns = "";
                $pdti_texte = "";
            }

            if (($td == "edit") OR ($td == "del")) {
                $row = lit_enr_pdt_in_int($codeart, $codeint);
                if (is_array($row)) {
                    $row = encode_str($row);
                    extract($row);
                } else
                    $pbm_pdt = TRUE;
            }

            // 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) {
                $row = encode_str($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;
                        $pdti_codetva = "TVA0";
                    }
                }
            }
            $db->disconnect();

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

            $puht = round(($pdti_puttc / (1 + $txtva)) * 100) / 100;
        }
    } else
        $pbm_pdt = TRUE;
}

$titre = '';
switch ($td) {
    case "add" :
        $titre = _("SAISIE ARTICLE");
        $pdti_facture = 1;
        break;
    case "edit" :
        $titre = _("EDITION ARTICLE");
        break;
    case "del" :
        $titre = _("SUPPRESSION ARTICLE");
        break;
    default :
        $pbm_pdt = TRUE;
        break;
}

if (($pbm_int == FALSE) AND ($pbm_pdt == FALSE)) {
    // En cas de création, la devise par défaut de l'intervention dossier est celle du compte qui saisi
    if ($td == "add") {
        switch (@$_SESSION["niv_log"]) {
            case 3 :
            case 4 :
                $int_codeadev = give_dev_codea_defaut_age($_SESSION["code_age"], $_SESSION["lng"], TRUE);
                break;
            default :
                $int_codeadev = give_dev_codea_defaut($_SESSION["lng"], TRUE);
                break;
        }
    } else
        // Sinon, on récupère le code devise de l'intervention déjà créée
        $int_codeadev = give_dev_codea_defaut_int($int_code, $_SESSION["lng"], TRUE);

    // On récupère le code devise du compte qui visualise
    switch (@$_SESSION["niv_log"]) {
        case 3 :
        case 4 :
            $codeadev_local = give_dev_codea_defaut_age($_SESSION["code_age"], $_SESSION["lng"], TRUE);
            break;
        default :
            $codeadev_local = give_dev_codea_defaut($_SESSION["lng"], 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 _("Article Intervention"); ?></title>

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

    <script language="javascript">
        <!--
        var tab1_champ = ["nom:puht; format:reel_ns ; req:Y ; txt:<?PHP echo _("PU HT"); ?>; vmn : 0.01 ; lmx:10 ; stok:frmok ; stbad:frmbad",
            "nom:pu; format:reel_ns ; req:Y ; txt:<?PHP echo _("PU TTC"); ?> ; vmn : 0.01 ; lmx:10 ; stok:frmok ; stbad:frmbad",
            "nom:ns; format:txt ; req:N ; txt:<?PHP echo _("Numéro de série"); ?> ; lmx:50 ; stok:frmok ; stbad:frmbad",
            "nom:texte; format:txt ; req:N ; txt:<?PHP echo _("Commentaire"); ?> ; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            init_valid_form('enr','pdti_maj','<?PHP $_SESSION['date_fmtshort']; ?>',tab1_champ,'','');
            init_fdeci('qte', 2,'');
            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');
            add_func_click('annul', 'sclose','');
            Focus_first();
        }

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

</head>

<body>
<?PHP
if ($pbm_pdt == 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 $pdti_codeint; ?> </span></p>

    <form id="pdti_maj" name="pdti_maj" method="post" action="pdti_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 $pdti_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 $pdti_codepdt ?>" 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 $pdt_des; ?>" size="50" maxlength="50"
                           readonly>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_qte" align="right"><?PHP echo _("Quantité"); ?></td>
                <td>
                    <?PHP
                    if ($pdt_codepdtu <> "TEMPS")
                        echo '<input name="qte" type="text" id="qte" style="text-align:right" value="' . full_num_format($pdti_qte) . '" size="10">';
                    else {
                        $tab_qte = [];
                        // création de la liste des heures (mode décimal)
                        for ($i = 300; $i <= 345600; $i += 300) {
                            $heuredeci = format_num($i / 3600, 2);
                            $heure = heure_to_hhmm(sec_heure($i));
                            $tab_qte[$heuredeci] = $heure;
                        }
                        cre_select('qte', 'qte', $tab_qte, $pdti_qte);
                    }
                    ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_puht" 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>
                    <input name="tva" type="hidden" id="tva" value="<?PHP echo $pdti_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) {
                        ?>
                        <br /><span class="annot_petit_it"><?PHP echo _("Ce client est exonéré de TVA"); ?></span>
                    <?PHP } ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_pu" align="right"><?PHP echo _("PU TTC"); ?></td>
                <td>
                    <?PHP cre_input_num('pu', $pdti_puttc, TRUE, FALSE); ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_ns" align="right"><?PHP echo _("N° Série"); ?></td>
                <td>
                    <input name="ns" type="text" id="ns" value="<?PHP echo $pdti_ns; ?>" size="40" maxlength="100">
                </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 $pdti_texte; ?></textarea>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_facture" align="right"><?PHP echo _("Facturé au client"); ?></td>
                <td>
                    <?PHP cre_select('facture', 'facture', $non_oui, intval($pdti_facture)); ?>
                </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="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="'._("Annuler").'">';
        ?>
        </p>
    </form>
<?PHP
} else
    close_fen_auto('', 'fscomp');
?>
</body>
</html>
