<?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
 ****************************************************************************************/

/**********************************************/
/* MAJ Planning sur Drag & Drop				  */
/**********************************************/

session_start();
sleep(1);
if (array_key_exists("id", $_REQUEST)) {
    $tomorrow = 0; //60*60*24; // en nb de secondes
    header("Cache-Control: max-age=$tomorrow");
    upd_plan();
} else
    print "Usage : $_SERVER[PHP_SELF]?id=idorigine-iddestination";

function upd_plan()
{
    global
        $datea,
        $db,
        $_SESSION;

    header("Content-type: text/html; charset=UTF-8");
    $tab_codeid = explode("-", $_REQUEST["id"]);

    $idorig = intval($tab_codeid[0]);

    $tab_codedest = explode("_", $tab_codeid[1]);
    $codedest = $tab_codedest[1];

    require_once("../lib/gettext/gettext.inc");
    include_once("../inc/config.inc.php");
    include_once("../inc/func_date.inc.php");
    include_once("../inc/func_time.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");
    include_once("../inc/fic_int.inc.php");
    include_once("../inc/fic_plan.inc.php");
    include_once("../inc/fic_tec.inc.php");

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


    $newdate_j = substr($codedest, 1, 2);
    $newdate_m = substr($codedest, 3, 2);
    $newdate_a = substr($codedest, 5, 4);
    $new_date = $newdate_a . "-" . $newdate_m . "-" . $newdate_j;
    $dayweek = njour_semaine($new_date)+1;
    // $dayweek = date( "w", strtotime(date('Y-m-d', strtotime($new_date))));

    $newheure_h = substr($codedest, 10, 2);
    $newheure_m = substr($codedest, 12, 2);
    $new_heure = $newheure_h . ":" . $newheure_m . ":00";

    if (evt_modifiable($idorig) == TRUE) {

        // On récupère les données de l'enregistrement courant
        $row = lit_enr_id_evt($idorig);
        if ($row)
        {
            extract($row);

            $mess_err = chk_evt_tec($pevt_typdest,$pevt_dest,$dayweek,$new_heure,$pevt_dureem);

            if ($mess_err == '')
            {
                // On se connecte a la base
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

                // MAJ évènement, pièce rattachée
                // et évènements impactés (si intervention ou bon de travail)
                $rech2 = "";
                $cree_rdv = FALSE;
                // MAJ du RDV
                switch (intval($pevt_typrat)) {
                    // Rien
                    case -1 :
                        break;
                    // Interventions
                    case 1 :
                        if ($pevt_coderat <> "") {
                            // Pas encore de RDV saisi : on crée
                            if (intval($pevt_idrdv) == 0) {
                                $cree_rdv = TRUE;

                                $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','$pevt_coderat','$new_date','$pevt_codelieu',";
                                $rech2 .= "'$new_heure','$pevt_dureem','$pevt_nivurg')";
                            } else // RDV déjà saisi : on met à jour
                            {
                                // Si RDV pas encore affecté, on peut directement mettre à jour ce RDV
                                $rech2 = "UPDATE `" . TBRDV . "` SET `rdv_datemod`='" . $datea . "', `rdv_codemod`='" . $codeuser . "',";
                                $rech2 .= " `rdv_date`='" . $new_date . "', `rdv_heure`='" . $new_heure . "'";
                                $rech2 .= " WHERE `rdv_id` = '" . $pevt_idrdv . "'";
                            }
                        }
                        break;
                    // Bon de travail
                    case 2 :
                        break;
                }

                if ($rech2 <> "")
                    $db->query($rech2);

                // MAJ de l'évènement

                // Si on a créé un nouveau RDV
                // Il faut remplacer l'évènement rattaché à l'ancien RDV, par
                if ($cree_rdv == TRUE) {
                    // On récupère l'ID du RDV créé
                    $pevt_idrdv = $db->insert_id;

                    // On met à jour en rattachant le nouveau RDV dans l'événement déplacé
                    $rech2 = "UPDATE `" . TBPEVT . "` SET `pevt_datemod`='" . $datea . "', `pevt_codemod`='" . $codeuser . "',";
                    $rech2 .= " `pevt_date`='" . $new_date . "', `pevt_heure`='" . $new_heure . "',`pevt_idrdv`='" . $pevt_idrdv . "'";
                    $rech2 .= " WHERE `pevt_id` = '" . $idorig . "'";
                }
                else
                {
                    // On n'a pas créé de RDV : On met à jour l'évènement au planning
                    $rech2 = "UPDATE `" . TBPEVT . "` SET `pevt_datemod`='" . $datea . "', `pevt_codemod`='" . $codeuser . "',";
                    $rech2 .= " `pevt_date`='" . $new_date . "', `pevt_heure`='" . $new_heure . "'";
                    $rech2 .= " WHERE `pevt_id` = '" . $idorig . "'";
                }
                $db->query($rech2);

                // Traçabilité
                $titre = _("Modification de l'évènement planning du")." " . datetolocal($pevt_date) . " "._("à")." " . substr($pevt_heure, 0, 5);
                $desc = $pevt_dest . " : "._("Déplacé au")." " . datetolocal($new_date) . " "._("à")." " . substr($new_heure, 0, 5);

                if ($_SESSION["niv_log"] == 9)
                    $code_session = _("Administrateur");
                else
                    $code_session = $_SESSION["code_log"];
                $datecourante = date("Y-m-d");
                $heurecourante = date("H:i:s");

                $rech2 = "INSERT INTO `" . TBTRA . "`";
                $rech2 .= " (tra_codeutil,tra_date,tra_heure,tra_codeta,tra_titre,tra_desc,tra_typrat,tra_coderat) VALUES";
                $rech2 .= " ('$code_session','$datecourante','$heurecourante','MOEP','" . $db->escape($titre) . "','" . $db->escape($desc) . "','$pevt_typrat','$pevt_coderat')";
                $db->query($rech2);
                $db->disconnect();
            }
        } else
            $mess_err =  _("Problème de lecture événement");
    } else
        $mess_err = _("Vous n'avez pas le droit de modifier cet événement");

    if ($mess_err <> '')
        print($mess_err);
}

?>