<?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_droits.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_plan.inc.php");
include_once("../inc/fic_rdv.inc.php");
include_once("../inc/fic_tra.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_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(@$_SESSION["code_log"]);
check_modules();

// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);

$titre = @$_POST["titre"];
$td = @$_POST["td"];
$codeint = @$_POST["codeint"];
$idrdv = intval(@$_POST["idrdv"]);
$cre_int = intval(@$_POST["creint"]);

$daterdv = datetomysql(@$_POST["daterdvf"]);
$hrdv = @$_POST["hrdv"];
$hrdv .= ":00";
$dureem = intval(@$_POST["dureem"]);
$codelieu = @$_POST["codelieu"];
$urg = @$_POST["urgence"];

$codeuser = @$_SESSION["code_log"];
$msg = '';
$retourliste = TRUE;
$titre_tra = _("Modification de l'intervention") . " " . $codeint;

$db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

switch ($td) {
    // Ajout du RDV
    case "add" :
        // On vérifie d'abord si le RDV n'a pas déjà été saisi
        $rech = "SELECT COUNT(*) FROM `" . TBRDV . "`";
        $rech .= " WHERE rdv_typrat='1' AND rdv_coderat='" . $db->escape($codeint) . "'";
        $rech .= " AND rdv_date='" . $daterdv . "' AND rdv_heure='" . $hrdv . "' AND rdv_dureem=" . $dureem;
        $nbres = $db->get_var($rech);
        if ($nbres == 0) {
            // Pas encore saisi : on ajoute
            $rech2 = "INSERT INTO `" . TBRDV . "`";
            $rech2 .= " (rdv_datecrea,rdv_codecrea,rdv_datemod,rdv_codemod,rdv_typrat,rdv_coderat,rdv_date,rdv_codelieu,";
            $rech2 .= "rdv_heure,rdv_dureem,rdv_nivurg)";
            $rech2 .= " VALUES";
            $rech2 .= " ('$datea','$codeuser','$datea','$codeuser','1','" . $db->escape($codeint) . "','$daterdv','" . $db->escape($codelieu) . "',";
            $rech2 .= "'$hrdv','$dureem','$urg')";

            $db->query($rech2);

            // Traçabilité
            // Si en mode création (intervention pas encore enregistrée) : pas de traçabilité détaillée
            if ($cre_int == 0) {
                $desc_tra = _("Ajout d'une date de rendez-vous");
                $desc_tra .= chr(13) . chr(10);
                $desc_tra .= "(" . _("Le") . " " . datetolocal($daterdv) . " " . _("à") . " " . substr($hrdv, 0, 5) . ")";
                add_trace(1, $codeint, "CRER", $titre_tra, $desc_tra, FALSE);
            }

            $msg = _("RDV ajouté");
        } else {
            $msg = _("Ce RDV a déjà été saisi !");
            $retourliste = FALSE;
        }
        break;

    // MAJ du RDV
    case "edit" :
        // On récupère l'enregistrement précédent (traçabilité)
        $detailsaisi = "";
        $ex_enrrdv = lit_enr_rdv_id($idrdv, FALSE, "assoc");

        $rech = "UPDATE `" . TBRDV . "` SET `rdv_datemod`='" . $datea . "', `rdv_codemod`='" . $codeuser . "',";
        $rech .= " `rdv_date`='" . $daterdv . "',`rdv_codelieu`='" . $codelieu . "', `rdv_heure`='" . $hrdv . "',";
        $rech .= " `rdv_dureem`='" . $dureem . "', `rdv_nivurg`='" . $urg . "'";
        $rech .= " WHERE `rdv_id` = '" . $idrdv . "'";
        $db->query($rech);

        // Mise à jour de l'évènement associé (si existe)
        $rech = "UPDATE `" . TBPEVT . "` SET `pevt_datemod`='" . $datea . "', `pevt_codemod`='" . $codeuser . "',";
        $rech .= " `pevt_date`='" . $daterdv . "', `pevt_heure`='" . $hrdv . "', `pevt_dureem`='" . $dureem . "',";
        $rech .= " `pevt_codelieu`='" . $db->escape($codelieu) . "',";
        $rech .= " `pevt_nivurg`='" . $urg . "'";
        $rech .= " WHERE `pevt_idrdv` = '" . $idrdv . "'";
        $db->query($rech);

        // On récupère l'enregistrement actuel (traçabilité)
        $nouv_enrrdv = lit_enr_rdv_id($idrdv, FALSE, "assoc");

        // On compare les éléments d'enregistrement pour traçabilité
        $trace_added = analyse_trace("rdv", $ex_enrrdv, $nouv_enrrdv, $detailsaisi, FALSE);

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

    // Suppression du RDV
    case "del" :
        // On récupère déjà les données du RDV (traçabilité)
        $row = lit_enr_rdv_id($idrdv, FALSE);
        if ($row) {
            extract($row);

            $rech = "DELETE FROM `" . TBRDV . "` WHERE `rdv_id` = '$idrdv'";
            $db->query($rech);

            // Supression de l'évènement associé
            $rech = "DELETE FROM `" . TBPEVT . "` WHERE `pevt_idrdv` = '$idrdv'";
            $db->query($rech);

            // traçabilité
            $desc_tra = _("Suppression d'une date de rendez-vous");
            $desc_tra .= chr(13) . chr(10);
            $desc_tra .= "(" . _("Le") . " " . datetolocal($rdv_date) . " " . _("à") . " " . substr($rdv_heure, 0, 5) . ")";
            add_trace(1, $codeint, "SUPR", $titre_tra, $desc_tra, FALSE);

            $msg = _("RDV 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 _("RDV Intervention"); ?></title>

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

    <script type="text/javascript">
        <!--
        <?PHP
        if ($retourliste == TRUE)
        {
            cre_html_trdv('detmrdv','edit',$codeint,'window.opener.');
            cre_html_nextrdv('nextrdv','edit',$codeint,'window.opener.');
        }
        ?>

        function init_all() {
            init_Body();
            <?PHP
            if ($retourliste == TRUE)
            {
                echo "updatmrdv();";
                echo "updatnextrdv();";
            }
            else
                echo "warn_txt('".str_escape($msg)."')";
            ?>
            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>
<?PHP
if ($retourliste == TRUE) {
    // echo '<p align="center" class="annot_std_it">' . $msg . '...</p>';
    close_fen_auto($msg, '', '');
}
?>
</body>
</html>
