<?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/fic_age.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_co.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_ot.inc.php");
include_once("../inc/fic_statut.inc.php");
include_once("../inc/fic_tec.inc.php");
include_once("../inc/fic_txt.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_time.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("../inc/func_txtcm.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../classes/phpmailer/class.phpmailer.php");
include_once("../classes/phpmailer/class.smtp.php");
include_once("../inc/mail.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"];
$tx = @$_POST["tx"];
$td = @$_POST["td"];
$ci = @$_POST["ci"];
$cf = @$_POST["cf"];

$fnulle = intval(@$_POST["fnulle"]);
$datefac = datetomysql(@$_POST["datefacf"]);
$numf = @$_POST["numf"];
$mt = @$_POST["mt"];
$nomf = @$_POST["nomf"];
$codeme = str_majuscules(@$_POST["codeme"]);
$nota = @$_POST["nota"];

$codeuser = @$_SESSION["code_log"];

$msg = "";
$retourliste = FALSE;

switch ($_POST["nbutton"]) {
    case "del" :
        $td = "del";
        break;
    default :
        break;
}

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
switch ($td) {
    // Ajout de la fiche suivi facturation
    case "add" :
        $rech = "SELECT COUNT(*) FROM `" . TBINTF . "` WHERE `intf_codeint` = '" . $db->escape($ci) . "' AND `intf_numf` = '" . $db->escape($numf) . "'";
        $nbres = $db->get_var($rech);
        if ($nbres == 0) {
            $rech2 = "INSERT INTO `" . TBINTF . "`";
            $rech2 .= " (intf_datecrea,intf_codecrea,intf_datemod,intf_codemod,intf_fnulle,intf_datefac,intf_numf,intf_mt,intf_nomf,";
            $rech2 .= "intf_codeint,intf_codeme,intf_nota)";
            $rech2 .= " VALUES";
            $rech2 .= " ('$datea','$codeuser','$datea','$codeuser','$fnulle','$datefac','" . $db->escape($numf) . "','" . $db->escape($mt) . "','" . $db->escape($nomf) . "',";
            $rech2 .= "'" . $db->escape($ci) . "','$codeme','" . $db->escape($nota) . "')";
            $db->query($rech2);

            // MAJ de la fiche intervention
            $rech2 = "UPDATE `" . TBINT . "` SET `int_fact`='1'";
            $rech2 .= " WHERE `int_code` = '" . $db->escape($ci) . "'";
            $db->query($rech2);

            $msg = _("Fiche ajoutée");
            $retourliste = TRUE;
        } else
            $msg = _("Ce code existe déjà");

        break;

    // MAJ de la fiche suivi facturation
    case "edit" :
        $rech = "UPDATE `" . TBINTF . "` SET `intf_datemod`='" . $datea . "', `intf_codemod`='" . $codeuser . "', `intf_fnulle`='" . $fnulle . "',";
        $rech .= " `intf_datefac`='" . $datefac . "', `intf_numf`='" . $numf . "', `intf_mt`='" . $mt . "', `intf_nomf`='" . $db->escape($nomf) . "', `intf_codeme`='" . $codeme . "',";
        $rech .= "  `intf_nota`='" . $db->escape($nota) . "'";
        $rech .= " WHERE `intf_codeint` = '" . $db->escape($ci) . "' AND `intf_numf` = '" . $db->escape($cf) . "'";
        $db->query($rech);

        // MAJ de la fiche intervention
        $rexist = "SELECT COUNT(*) FROM `" . TBINTF . "` WHERE `intf_codeint` = '$ci'";
        $nbres2 = $db->get_var($rexist);
        if ($nbres2 == 0) {
            $rech2 = "UPDATE `" . TBINT . "` SET `int_fact`='0'";
            $rech2 .= " WHERE `int_code` = '" . $db->escape($ci) . "'";
        } else {
            $rech2 = "UPDATE `" . TBINT . "` SET `int_fact`='1'";
            $rech2 .= " WHERE `int_code` = '" . $db->escape($ci) . "'";
        }
        $db->query($rech2);

        $msg = _("Fiche modifiée");
        $retourliste = TRUE;

        break;


    // Suppression de la fiche suivi facturation
    case "del" :
        $rech = "DELETE FROM `" . TBINTF . "` WHERE `intf_codeint` = '" . $db->escape($ci) . "' AND `intf_numf` = '" . $db->escape($cf) . "'";
        $db->query($rech);

        // MAJ de la fiche intervention
        $rexist = "SELECT COUNT(*) FROM `" . TBINTF . "` WHERE `intf_codeint` = '$ci'";
        $nbres2 = $db->get_var($rexist);
        if ($nbres2 == 0) {
            $rech2 = "UPDATE `" . TBINT . "` SET `int_fact`='0'";
            $rech2 .= " WHERE `int_code` = '" . $db->escape($ci) . "'";
        } else {
            $rech2 = "UPDATE `" . TBINT . "` SET `int_fact`='1'";
            $rech2 .= " WHERE `int_code` = '" . $db->escape($ci) . "'";
        }
        $db->query($rech2);

        $msg = _("Fiche supprimée");
        $retourliste = TRUE;

        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 _("Suivi facturation"); ?></title>

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

    <script type="text/javascript">
        <!--
        <?PHP
        if ($tx=="i")
            cre_detail_factint('txtsfact',$ci);
        ?>

        function init_all() {
            init_Body();
            <?PHP
            if ($tx=="i")
                echo "update_detail_factint()";
            if ($retourliste == FALSE)
                echo "warn_txt('" . str_escape($msg) . "');";
            ?>
            Focus_first();
        }

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

</head>
<body>
<?php
if ($tx == "m")
    include_once("../inc/entete.inc.php");
?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>

<p align="center">&nbsp;</p>
<?PHP
if ($retourliste == FALSE)
    aff_ret_idx_man($td, "intf_lst.php?tx=" . $tx . "&ci=" . $ci . "&ord=ASC&tri=Facture", _("Retour à la liste"), FALSE);
else
    pop_ret_auto($msg, 'ok', "intf_lst.php?tx=" . $tx . "&ci=" . $ci . "&ord=ASC&tri=Facture");

if ($tx == "m")
    include_once("pied.php");
?>
</body>
</html>