<?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_int.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_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();
$_POST = clean_str($_POST);

$titre = @$_POST["titre"];
$td = @$_POST["td"];
$codeint = @$_POST["codeint"];

$code = str_majuscules(@$_POST["code"]);
$des = str_majuscules(@$_POST["des"]);
$qte = floatval(@$_POST["qte"]);
$tva = @$_POST["tva"];
$pu = floatval(@$_POST["pu"]);
$ns = @$_POST["ns"];
$texte = @$_POST["texte"];
$facture = intval(@$_POST["facture"]);

$retourliste = TRUE;

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
switch ($td) {
    // Ajout de la fiche Produit Intervention
    case "add" :
        $rech = "SELECT COUNT(*) FROM `" . TBPDTI . "` WHERE pdti_codeint = '" . $db->escape($codeint) . "' AND pdti_codepdt = '" . $db->escape($code) . "'";
        $nbres = $db->get_var($rech);
        if ($nbres == 0) {
            $rech2 = "INSERT INTO `" . TBPDTI . "`";
            $rech2 .= " (pdti_codeint,pdti_codepdt,pdti_frompdt,pdti_puttc,pdti_codetva,pdti_qte,pdti_ns,pdti_texte,pdti_facture) VALUES";
            $rech2 .= " ('$codeint','" . $db->escape($code) . "',NULL,'$pu','$tva','$qte','" . $db->escape($ns) . "','" . $db->escape($texte) . "','$facture')";
            $db->query($rech2);

            // Ajout des taxes parafiscales rattachées
            $rech2 = "SELECT * FROM `" . TBTPFA . "` WHERE tpfa_codepdt = '$code' ORDER BY tpfa_id";
            $rows2 = $db->get_results($rech2, ARRAY_A);
            if ($rows2) {
                foreach ($rows2 as $row2) {
                    $row2 = encode_str($row2);
                    extract($row2);

                    $rech3 = "SELECT * FROM `" . TBTPF . "` WHERE tpf_code = '$tpfa_codetpf'";
                    $row3 = $db->get_row($rech3, ARRAY_A);
                    if ($row3) {
                        $row3 = encode_str($row3);
                        extract($row3);

                        $txtcompl = "";

                        // Calcul du code TVA
                        $codetva_tpf = "";
                        switch ($tpf_codetva) {
                            // -2 : Pas de TVA
                            case "-2" :
                                $codetva_tpf = "TVA0";
                                break;
                            // -1 : TVA du produit
                            case "-1" :
                                $codetva_tpf = $tva;
                                break;
                            // Autre : TVA définie
                            default :
                                $codetva_tpf = $tpf_codetva;
                                break;
                        }

                        // Calcul du taux de TVA
                        $txtva_tpf = 0;
                        $rech4 = "SELECT * FROM `" . TBTVA . "` WHERE tva_code = '$codetva_tpf'";
                        $row4 = $db->get_row($rech4, ARRAY_A);
                        if ($row4) {
                            extract($row4);
                            $txtva_tpf = $tva_tx;
                        }

                        // Calcul du nombre de taxes
                        $qte_tpf = 0;
                        switch ($tpf_typmt) {
                            // 0 : Proportionnel au nombre de produits
                            case "0" :
                                $qte_tpf = $qte;
                                $txtcompl .= ' ' . _("Taxe unitaire");
                                break;
                            // 1 : Fixe quel que soit le nombre de produits
                            case "1" :
                                $qte_tpf = 1;
                                $txtcompl .= ' ' . _("Taxe forfaitaire");
                                break;
                        }

                        // Calcul du montant TTC unitaire de la taxe
                        $putttc_tpf = 0;
                        switch ($tpf_pour) {
                            // 0 : Valeur
                            case "0" :
                                $puttc_tpf = $tpfa_mt;
                                break;
                            // 1 : Pourcentage
                            case "1" :
                                $puttc_tpf = $pu * round($tpfa_mt / 100, 2);
                                $txtcompl .= ' ' . full_num_format($tpfa_mt, '', '', FALSE, '', FALSE) . _("% sur prix unitaire");
                                if ($codetva_tpf == "TVA0")
                                    $txtcompl .= ' ' . _("TTC");
                                break;
                        }

                        $rech4 = "INSERT INTO `" . TBPDTI . "`";
                        $rech4 .= " (pdti_codeint,pdti_codepdt,pdti_frompdt,pdti_puttc,pdti_codetva,pdti_qte,pdti_ns,pdti_texte) VALUES";
                        $rech4 .= " ('$codeint','$tpf_code','" . $db->escape($code) . "','$puttc_tpf','$codetva_tpf','$qte_tpf','','" . $db->escape($txtcompl) . "')";
                        $db->query($rech4);
                    }
                }
            }
            $msg = _("Produit ajouté");
        } else {
            $msg = _("Ce produit est déjà saisi dans cette intervention");
            $retourliste = FALSE;
        }
        break;

    // MAJ de la fiche Produit Intervention
    case "edit" :
        $rech = "UPDATE `" . TBPDTI . "` SET `pdti_codeint`='" . $db->escape($codeint) . "', `pdti_codepdt`='" . $db->escape($code) . "',";
        $rech .= " `pdti_puttc`='" . $pu . "', `pdti_qte`='" . $qte . "', `pdti_ns`='" . $db->escape($ns) . "', `pdti_texte`='" . $db->escape($texte) . "', `pdti_facture`='" . $facture . "'";
        $rech .= " WHERE `pdti_codeint` = '" . $codeint . "' AND `pdti_codepdt` = '" . $db->escape($code) . "'";
        $db->query($rech);

        // On supprime d'abord les taxes parafiscales rattachées à ce produit, dans cette intervention
        $rech = "DELETE FROM `" . TBPDTI . "` WHERE `pdti_codeint` = '$codeint' AND `pdti_frompdt` = '" . $db->escape($code) . "'";
        $db->query($rech);

        // Ajout des taxes parafiscales rattachées
        $rech2 = "SELECT * FROM `" . TBTPFA . "` WHERE tpfa_codepdt = '$code' ORDER BY tpfa_id";
        $rows2 = $db->get_results($rech2, ARRAY_A);
        if ($rows2) {
            foreach ($rows2 as $row2) {
                $row2 = encode_str($row2);
                extract($row2);

                $rech3 = "SELECT * FROM `" . TBTPF . "` WHERE tpf_code = '$tpfa_codetpf'";
                $row3 = $db->get_row($rech3, ARRAY_A);
                if ($row3) {
                    $row3 = encode_str($row3);
                    extract($row3);

                    $txtcompl = "";

                    // Calcul du code TVA
                    $codetva_tpf = "";
                    switch ($tpf_codetva) {
                        // -2 : Pas de TVA
                        case "-2" :
                            $codetva_tpf = "TVA0";
                            break;
                        // -1 : TVA du produit
                        case "-1" :
                            $codetva_tpf = $tva;
                            break;
                        // Autre : TVA définie
                        default :
                            $codetva_tpf = $tpf_codetva;
                            break;
                    }

                    // Calcul du taux de TVA
                    $txtva_tpf = 0;
                    $rech4 = "SELECT * FROM `" . TBTVA . "` WHERE tva_code = '$codetva_tpf'";
                    $row4 = $db->get_row($rech4, ARRAY_A);
                    if ($row4) {
                        extract($row4);
                        $txtva_tpf = $tva_tx;
                    }

                    // Calcul du nombre de taxes
                    $qte_tpf = 0;
                    switch ($tpf_typmt) {
                        // 0 : Proportionnel au nombre de produits
                        case "0" :
                            $qte_tpf = $qte;
                            $txtcompl .= ' ' . _("Taxe unitaire");
                            break;
                        // 1 : Fixe quel que soit le nombre de produits
                        case "1" :
                            $qte_tpf = 1;
                            $txtcompl .= ' ' . _("Taxe forfaitaire");
                            break;
                    }

                    // Calcul du montant TTC unitaire de la taxe
                    $putttc_tpf = 0;
                    switch ($tpf_pour) {
                        // 0 : Valeur
                        case "0" :
                            $puttc_tpf = $tpfa_mt;
                            break;
                        // 1 : Pourcentage
                        case "1" :
                            $puttc_tpf = $pu * round($tpfa_mt / 100, 2);
                            $txtcompl .= ' ' . full_num_format($tpfa_mt, '', '', FALSE, '', FALSE) . _("% sur prix unitaire");
                            if ($codetva_tpf == "TVA0")
                                $txtcompl .= ' ' . _("TTC");
                            break;
                    }

                    $rech4 = "INSERT INTO `" . TBPDTI . "`";
                    $rech4 .= " (pdti_codeint,pdti_codepdt,pdti_frompdt,pdti_puttc,pdti_codetva,pdti_qte,pdti_ns,pdti_texte) VALUES";
                    $rech4 .= " ('$codeint','$tpf_code','" . $db->escape($code) . "','$puttc_tpf','$codetva_tpf','$qte_tpf','','" . $db->escape($txtcompl) . "')";
                    $db->query($rech4);
                }
            }
        }

        $msg = _("Produit mis à jour");
        break;

    // Suppression de la fiche Produit de l'intervention
    case "del" :
        $rech = "DELETE FROM `" . TBPDTI . "` WHERE `pdti_codeint` = '$codeint' AND `pdti_codepdt` = '" . $db->escape($code) . "'";
        $db->query($rech);

        $rech = "DELETE FROM `" . TBPDTI . "` WHERE `pdti_codeint` = '$codeint' AND `pdti_frompdt` = '" . $db->escape($code) . "'";
        $db->query($rech);

        $msg = _("Produit supprimé");
        break;

    default :
        break;
}
$db->disconnect();
?>
<!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 type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            <?PHP
            if ($retourliste == FALSE)
                echo "warn_txt('".str_escape($msg)."');";
            else
            {
                $row = lit_enr_int($codeint);
                if (is_array($row))
                    extract($row);
                cre_html_pdti('detpdti','edit',$codeint,$int_acpte,'window.opener.');
                echo "updatpdti();";
                echo "pop_txt('".str_escape($msg)."');";
                echo "self.close();";
            }
            ?>
            Focus_first();
        }

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

</head>
<body>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>

<p align="center">&nbsp;</p>
</body>
</html>
