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

$td = valget('td');
$codeint = valget('ci');
$idinth = (int)valget('cih');
$codeart = valget('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);

            switch ($td) {
                case 'add' :
                    $pdtih_codeint = $codeint;
                    $pdtih_codepdt = $pdt_code;
                    $pdtih_puttc = $pdt_puttc;
                    $pdtih_codetva = $pdt_codetva;
                    $pdtih_qte = 1;
                    $pdtih_ns = '';
                    $pdtih_texte = '';
                    break;

                case 'edit' :
                case 'del' :
                    $row = recup_pdt_tab_in_inth($codeart);
                    if ($row) {
                        $row = encode_str($row);
                        extract($row);
                    } else {
                        $pbm_pdt = TRUE;
                    }
                    break;
            }

            if ($pbm_pdt == FALSE) {
                // Récupération information TVA par client
                $cliexo = FALSE;
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                $rech = 'SELECT int_codecli FROM `' . TBINT . "` WHERE `int_code` = '$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;
                            $pdtih_codetva = 'TVA0';
                        }
                    }
                }
                close_database();

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

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

$titre = '';
switch ($td) {
    case 'add' :
        $titre = _('SAISIE ARTICLE');
        $pdtih_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 (valsession('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 (valsession('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 Historique 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",
            "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', 'pdtih_maj', '<?php valsession('date_fmtshort'); ?>', tab1_champ, '', '');
            init_fdeci('qte', 2, '');
            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');
            add_func_click('annul', 'sclose', '');
            Focus_first();
        }

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

</head>

<body>
<?php
if (($pbm_pdt == FALSE) AND ($pbm_int == FALSE)) {
    cre_ent_form($titre, '', '', '', FALSE);
    ?>
    <p align="center"><span class="titre_gras"><?php echo _("Pour l'intervention"); ?>
            : <?php echo $pdtih_codeint; ?> </span></p>

    <form id="pdtih_maj" name="pdtih_maj" method="post" action="pdtih_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 $pdtih_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 $pdtih_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($pdtih_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, $pdtih_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 $pdtih_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, 'utf8');
                    $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 . '">';
                        }
                    }
                    close_database();

                    if ($cliexo == TRUE) {
                        echo '<br /><span class="annot_petit_it">' . _('Ce client est exonéré de TVA') . '</span>';
                    }
                    ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_pu" align="right"><?php echo _('PU TTC'); ?></td>
                <td>
                    <?php cre_input_num('pu', $pdtih_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 $pdtih_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 $pdtih_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, $pdtih_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('', '');
}
?>
</body>
</html>
