<?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/func.inc.php");
include_once("../inc/func_date.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 = _("REAFFECTATION TVA");
$retourliste = FALSE;
if (@$_POST["nbutton"] == "enr") {
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $txtva1 = 0;
    $codetva1 = @$_POST["tva1"];
    $rech = "SELECT tva_tx FROM `" . TBTVA . "` WHERE `tva_code` = '" . $db->escape($codetva1) . "'";
    $row = $db->get_row($rech, ARRAY_A);
    if ($row) {
        extract($row);
        $txtva1 = $tva_tx / 100;
    }
    $txtva2 = 0;
    $codetva2 = @$_POST["tva2"];
    $rech = "SELECT tva_tx FROM `" . TBTVA . "` WHERE `tva_code` = '" . $db->escape($codetva2) . "'";
    $row = $db->get_row($rech, ARRAY_A);
    if ($row) {
        extract($row);
        $txtva2 = $tva_tx / 100;
    }
    $modeTTC = intval(@$_POST["brec"]);
    $codeuser = @$_SESSION["code_log"];

    // Recalcul des produits
    $rech = "SELECT * FROM `" . TBPDT . "` WHERE pdt_codetva = '" . $db->escape($codetva1) . "'";
    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            $puttc = $pdt_puttc;
            switch ($modeTTC) {
                // mode HT
                case 0 :
                    $puht = round($puttc / (1 + $txtva1), 2);
                    $puttc = round($puht * (1 + $txtva2), 2);
                    break;
                // Mode TTC
                case 1 :
                    break;
            }
            $rech2 = "UPDATE `" . TBPDT . "` SET `pdt_datemod`='" . $datea . "', `pdt_codemod`='" . $codeuser . "',";
            $rech2 .= " `pdt_puttc`='" . $puttc . "',";
            $rech2 .= " `pdt_codetva`='" . $db->escape($codetva2) . "'   WHERE `pdt_id` = '" . $pdt_id . "'";
            $db->query($rech2);
        }
    }

    // Recalcul des taxes parafiscales
    $rech = "SELECT * FROM `" . TBTPF . "` WHERE tpf_codetva = '" . $db->escape($codetva1) . "'";
    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            $rech2 = "UPDATE `" . TBTPF . "` SET ";
            if ($tpf_pour == FALSE) // On ne modifie la valeur que s'il ne s'agit pas d'un pourcentage
            {
                $valttc = $tpf_valeur;
                switch ($modeTTC) {
                    // mode HT
                    case 0 :
                        $valht = round($valttc / (1 + $txtva1), 2);
                        $valtc = round($valht * (1 + $txtva2), 2);
                        break;
                    // Mode TTC
                    case 1 :
                        break;
                }
                $rech2 .= " `tpf_valeur`='" . $valttc . "',";
            }
            $rech2 .= " `tpf_codetva`='" . $codetva2 . "'   WHERE `tpf_id` = '" . $tpf_id . "'";
            $db->query($rech2);
        }
    }

    // Recalcul des interventions en cours
    $modint = intval(@$_POST["modi"]);
    if ($modint == 1) {
        $rech = "SELECT int_code FROM `" . TBINT . "` WHERE int_datefin = '0000-00-00'";
        $rs = $db->get_results($rech, ARRAY_A);
        if ($rows) {
            foreach ($rows as $row) {
                extract($row);

                // Produits sur interventions
                $rech2 = "SELECT pdti_id FROM `" . TBPDTI . "` WHERE pdti_codeint = '" . $int_code . "'";
                $rows2 = $db->get_results($rech2, ARRAY_A);
                if ($rows2) {
                    foreach ($rows2 as $row2) {
                        extract($row);
                        $rech3 = "UPDATE `" . TBPDTI . "` SET ";
                        $rech3 .= " `pdti_codetva`='" . $codetva2 . "'   WHERE `pdti_id` = '" . $pdti_id . "'";
                        $db->query($rech3);
                    }
                }

                // Taxes parafiscales globales sur interventions
                $rech2 = "SELECT tpfi_id,tpfi_codetpf,tpfi_valeur FROM `" . TBTPFI . "` WHERE tpfi_codeint = '" . $int_code . "'";
                $rows2 = $db->get_results($rech2, ARRAY_A);
                if ($rows2) {
                    foreach ($rows2 as $row2) {
                        extract($row2);

                        $rech3 = "SELECT * FROM `" . TBTPF . "` WHERE tpf_code = '$tpfi_codetpf'";
                        $rows3 = $db->get_results($rech3, ARRAY_A);
                        if ($rows3) {
                            foreach ($rows3 as $row3) {
                                extract($row3);
                                $rech4 = "UPDATE `" . TBTPFI . "` SET ";
                                if ($tpf_pour == FALSE) // On ne modifie la valeur que s'il ne s'agit pas d'un pourcentage
                                {
                                    $valttc = $tpfi_valeur;
                                    switch ($modeTTC) {
                                        // mode HT
                                        case 0 :
                                            $valht = round($valttc / (1 + $txtva1), 2);
                                            $valttc = round($valht * (1 + $txtva2), 2);
                                            break;
                                        // Mode TTC
                                        case 1 :
                                            break;
                                    }
                                    $rech4 .= " `tpfi_valeur`='" . $valttc . "',";
                                }
                                $rech4 .= " `tpfi_codetva`='" . $codetva2 . "'   WHERE `tpfi_id` = '" . $tpfi_id . "'";
                                $db->query($rech4);
                            }
                        }
                    }
                }
            }
        }
    }

    // Recalcul des contrat non signés
    $modcocl = intval(@$_POST["modc"]);
    if ($modcocl == 1) {
        $rech = "SELECT cocl_id,cocl_pvht FROM `" . TBCOCL . "` WHERE cocl_datesig = '0000-00-00' AND cocl_codetva = '" . $db->escape($codetva1) . "' ";
        $rows = $db->get_results($rech, ARRAY_A);
        if ($rows) {
            foreach ($rows as $row) {
                extract($row);

                $pvht = $cocl_pvht;
                switch ($modeTTC) {
                    // mode HT
                    case 0 :
                        break;
                    // Mode TTC
                    case 1 :
                        $pvttc = round($pvht * (1 + $txtva1), 2);
                        $pvht = round($pvttc / (1 + $txtva2), 2);
                        break;
                }
                $rech2 = "UPDATE `" . TBCOCL . "` SET `cocl_datemod`='" . $datea . "', `cocl_codemod`='" . $codeuser . "',";
                $rech2 .= " `cocl_pvht`='" . $pvht . "',";
                $rech2 .= " `cocl_codetva`='" . $codetva2 . "'   WHERE `cocl_id` = '" . $cocl_id . "'";
                $db->query($rech2);
            }
        }
    }

    $db->disconnect();

    unset($_POST["val"]);
    $retourliste = TRUE;
}
?>
    <!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 _("Réaffecter TVA"); ?></title>

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

        <script language="javascript">
            <!--
            var tab1_champ = ["nom:tva1 ; format:liste ; req:Y ; txt:<?PHP echo _("TVA à réaffecter"); ?>  ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:tva2 ; format:liste ; req:Y ; txt:<?PHP echo _("Vers la TVA"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:brec ; format:liste ; req:Y ; txt:<?PHP echo _("Base de recalcul"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:modi ; format:liste ; req:Y ; txt:<?PHP echo _("Modifier interventions en cours"); ?>; stok:frmok ; stbad:frmbad",
                "nom:modc ; format:liste ; req:Y ; txt:<?PHP echo _("Modifier contrats non signés"); ?>; stok:frmok ; stbad:frmbad"];
            //-->
        </script>

        <script type="text/javascript">
            <!--
            function init_all() {
                init_Body();
                init_valid_tvarea('enr','tvarea_maj','<?PHP echo $_SESSION['date_fmtshort']; ?>',tab1_champ,'','');
                Focus_first();
            }

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

<body>
<?php include_once("../inc/entete.inc.php"); ?>
    <p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<?PHP if ($retourliste == FALSE) { ?>
    <form name="tvarea_maj" id="tvarea_maj" method="post" action="tvarea_maj.php">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <table width="60%" border="0" align="center">
            <tr>
                <td width="50%" class="rubfrm" id="f_tva1"><?PHP echo _("Réaffecter la TVA"); ?></td>
                <td width="50%">
                    <?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('tva1', 'tva1', $rows, 'tva_code', 'tva_desc', '', '', '', _("-- Faites votre choix --"));
                    ?>
                </td>
            </tr>
            <tr>
                <td width="50%" class="rubfrm" id="f_tva2"><?PHP echo _("Vers la TVA"); ?></td>
                <td width="50%">
                    <?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('tva2', 'tva2', $rows, 'tva_code', 'tva_desc', '', '', '', _("-- Faites votre choix --"));
                    ?>
                </td>
            </tr>
            <tr>
                <td width="50%" class="rubfrm" id="f_brec"><?PHP echo _("Base de recalcul"); ?></td>
                <td width="50%">
                    <?PHP
                    cre_select('brec', 'brec', $tab_txt_base_calcul, '','', _("-- Faites votre choix --"));
                    ?>
                </td>
            </tr>
            <tr>
                <td width="50%" class="rubfrm" id="f_modi"><?PHP echo _("Modifier interventions en cours"); ?></td>
                <td width="50%">
                    <?PHP
                    cre_select('modi', 'modi', $non_oui, 0);
                    ?>
                    <br /><span class="annot_petit_it"><?PHP echo _("Les produits et les taxes parafiscales sont concernés"); ?></span>
                </td>
            </tr>
            <tr>
                <td width="50%" class="rubfrm" id="f_modi"><?PHP echo _("Modifier contrats non signés"); ?></td>
                <td width="50%">
                    <?PHP
                    cre_select('modc', 'modc', $non_oui, 0);
                    ?>
                </td>
            </tr>
        </table>
        <p align="center">
            <input name="enr" type="button" class="bton_std" id="enr" value="<?PHP echo _("Valider"); ?>">
             <input name="RAZ" type="reset" class="bton_std" id="RAZ" value="<?PHP echo _("Réinitialiser"); ?>">
        </p>
    </form>
<?PHP
} else {
    exec_func_js("warn_txt('" . _("Modification effecuée") . "')");
    aff_ret_idx_man($td, "tva_lst.php", _("Retour à la liste"), FALSE);
}
include_once("pied.php");
?>