<?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/config.inc.php");
include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_rel.inc.php");
include_once("../inc/fic_txt.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_mail.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.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"];
$td = @$_POST["td"];
$typrel = @$_POST["typrel"];
$coderel = @$_POST["coderel"];
$id = @$_POST["id"];
$codecli = str_majuscules(@$_POST["codecli"]);
$site = str_majuscules(@$_POST["site"]);
$cap = @$_POST["cap"];
$coderlp = str_majuscules(@$_POST["code"]);

$pbm = @$_POST["pbm"];
$datecrea = datetomysql(@$_POST["datecreaf"]);
$signale = @$_POST["signale"];

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

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

$retourliste = TRUE;

switch ($td) {
    // Ajout de la fiche Déclaration de problème du site principal
    case "add" :
        // Si tous les sites, on génère autant de fiches problèmes que de sites actifs
        if ($site == -1) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

            $site = 0;
            $rech = "INSERT INTO `" . TBRPBM . "`";
            $rech .= " (rpbm_datecrea,rpbm_codecrea,rpbm_datemod,rpbm_codemod,rpbm_coderlp,rpbm_typrel,rpbm_codecli,rpbm_signale,";
            $rech .= "rpbm_pbm,rpbm_idsite)";
            $rech .= " VALUES";
            $rech .= " ('$datecrea','$codeuser','$datea','$codeuser','$coderlp','$typrel','$codecli','" . $db->escape($signale) . "',";
            $rech .= "'" . $db->escape($pbm) . "','$site')";
            $db->query($rech);
            $tab_id[] = $db->insert_id;

            $rech2 = "SELECT * FROM `" . TBSCLI . "` WHERE `scli_actif` = '1' AND `scli_codecli` = '" . $codecli . "'";
            $rows = $db->get_results($rech2, ARRAY_A);
            $nb_enr = 0;

            if ($rows) {
                $nb_enr = count($rows);

                foreach ($rows as $row) {
                    extract($row);

                    // Ajout de la fiche Déclaration de problème sur les sites secondaires
                    $rech = "INSERT INTO `" . TBRPBM . "`";
                    $rech .= " (rpbm_datecrea,rpbm_codecrea,rpbm_datemod,rpbm_codemod,rpbm_coderlp,rpbm_typrel,rpbm_codecli,rpbm_signale,";
                    $rech .= "rpbm_pbm,rpbm_idsite)";
                    $rech .= " VALUES";
                    $rech .= " ('$datecrea','$codeuser','$datea','$codeuser','$coderlp','$typrel','$codecli','".$db->escape($signale)."',";
                    $rech .= "'".$db->escape($pbm)."','$scli_id')";
                    $rs = $db->get_row($rech, ARRAY_A);
                    $tab_id[] = $db->insert_id;
                }
            }
            $db->disconnect();

            if ($nb_enr <> 0) {
                // Envoi des différents emails
                if (count($tab_id) <> 0) {
                    for ($i = 0; $i < count($tab_id); $i++) {
                        if ($tab_id[$i] <> "") {
                            // Envoi d'email vers le client
                            envoi_mail("", _("Déclaration d'un problème licence ou matériel"), "rpbm", "cre", "xxx", "cli", $tab_id[$i], "", "", FALSE);
                            // Envoi d'email vers l'agence
                            if (@$_SESSION["niv_log"] <> 4)
                                envoi_mail("", _("Déclaration d'un problème licence ou matériel"), "rpbm", "cre", "xxx", "age", $tab_id[$i], "", "", FALSE);
                            // Envoi d'email vers le centre de gestion
                            envoi_mail("", _("Déclaration d'un problème licence ou matériel"), "rpbm", "cre", "xxx", "cdg", $tab_id[$i], "", "", TRUE);
                        }
                    }
                }
            }

            $msg = _("Déclarations de problèmes enregistrées");
        } else {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

            // Ajout de la fiche Déclaration de problème
            $rech = "INSERT INTO `" . TBRPBM . "`";
            $rech .= " (rpbm_datecrea,rpbm_codecrea,rpbm_datemod,rpbm_codemod,rpbm_coderlp,rpbm_typrel,rpbm_codecli,rpbm_signale,";
            $rech .= "rpbm_pbm,rpbm_idsite)";
            $rech .= " VALUES";
            $rech .= " ('$datecrea','$codeuser','$datea','$codeuser','$coderlp','$typrel','$codecli','".$db->escape($signale)."',";
            $rech .= "'".$db->escape($pbm)."','$site')";
            $db->query($rech);
            $id = $db->insert_id;
            $db->disconnect();

            // Envoi d'email vers le client
            envoi_mail("", _("Déclaration d'un problème licence ou matériel"), "rpbm", "cre", "xxx", "cli", $id, "", "", FALSE);
            // Envoi d'email vers l'agence
            if (@$_SESSION["niv_log"] <> 4)
                envoi_mail("", _("Déclaration d'un problème licence ou matériel"), "rpbm", "cre", "xxx", "age", $id, "", "", FALSE);
            // Envoi d'email vers le centre de gestion
            envoi_mail("", _("Déclaration d'un problème licence ou matériel"), "rpbm", "cre", "xxx", "cdg", $id, "", "", TRUE);

            $msg = _("Déclaration de problème enregistrée");
        }
        break;

    // MAJ de la fiche relevé
    case "edit" :
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "UPDATE `" . TBRPBM;
        $rech .= "` SET `rpbm_datemod`='" . $datea . "', `rpbm_codemod`='" . $codeuser . "',`rpbm_coderlp`='" . $coderlp . "',`rpbm_typrel`='" . $typrel . "',";
        $rech .= "  `rpbm_codecli`='" . $codecli . "', `rpbm_signale`='" . $db->escape($signale) . "', `rpbm_pbm`='" . $db->escape($pbm) . "', `rpbm_idsite`='" . $site . "'";
        $rech .= " WHERE `rpbm_id` = '" . $id . "'";
        $db->query($rech);
        $db->disconnect();

        $msg = _("Déclaration de problème modifiée");

        // Envoi d'email vers le client
        envoi_mail("", _("Modification d'un problème déjà déclaré"), "rpbm", "cre", "xxx", "cli", $id, "", "", FALSE);
        // Envoi d'email vers l'agence
        if (@$_SESSION["niv_log"] <> 4)
            envoi_mail("", _("Modification d'un problème déjà déclaré"), "rpbm", "cre", "xxx", "age", $id, "", "", FALSE);
        // Envoi d'email vers le centre de gestion
        envoi_mail("", _("Modification d'un problème déjà déclaré"), "rpbm", "cre", "xxx", "cdg", $id, "", "", TRUE);
        break;

    // Suppression de la fiche relevé
    case "del" :
        if ($codeint == "") {
            // SUPPRESSION de la fiche relevé
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            $rech = "DELETE FROM `" . TBRPBM . "` WHERE `rpbm_id` = '$id'";
            $db->query($rech);
            $db->disconnect();
            $msg = _("Déclaration de problème supprimée");
        } else {
            $retourliste = FALSE;
            $msg = _("Impossible de supprimer une déclaration de problème rattachée à une intervention").'<br />'._("Sortez-la de l'intervention avant de la supprimer");
        }
        break;

    default :
        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 _("Déclaration de Problème"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            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>

<p align="center">&nbsp;</p>
<?PHP
if ($retourliste == FALSE) {
    exec_func_js("warn_txt('" . str_escape($msg) . "')");
    aff_ret_idx_man($td, "rel_maj.php?td=edit&cc= '.$codecli.'&cr=" . $coderel, _("Retour à la saisie"), FALSE);
} else {
    if ($cap == 0) {
        if (@$_SESSION["niv_log"] == 1)
            pop_ret_auto($msg, 'ok', "rel_maj.php?cc=" . $codecli);
        else
            pop_ret_auto($msg, 'ok', "rel_maj.php?td=edit&cc=".$codecli."&cr=" . $coderel);
    } else {
        if (@$_SESSION["niv_log"] == 1)
            $td = "view";
        else
            $td = "edit";
        if ($typrel == 1)
            $url_ret = "rlic_maj.php?td=edit&cc=".$codecli."&cr=".$coderel."&crl=" . $coderlp;
        else
            $url_ret = "rpdt_maj.php?td=edit&cc=".$codecli."&cr=".$coderel."&crp=" . $coderlp;

        pop_ret_auto($msg, 'ok', $url_ret);
    }
}
include_once("pied.php");
?>
</body>
</html>