<?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_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_dos.inc.php");
include_once("../inc/fic_droits.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"];
$td = @$_POST["td"];
$codeadev = @$_POST["codeadev"];

$datedde = datetomysql(@$_POST["dateddef"]);
$datefinp = datetomysql(@$_POST["datefinpf"]);
$code = str_majuscules(@$_POST["code"]);
$codecli = str_majuscules(@$_POST["codecli"]);
$codeage = str_majuscules(@$_POST["codeage"]);
$codecat = str_majuscules(@$_POST["codecat"]);
$codestatut = str_majuscules(@$_POST["codestatut"]);
$urg = @$_POST["urgence"];
$proba = @$_POST["proba"];
$ave = @$_POST["ave"];
$titred = @$_POST["titred"];
$resum = @$_POST["resum"];
$codesuiv = str_majuscules(@$_POST["codesuiv"]);
$datefin = datetomysql(@$_POST["datefinf"]);

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

$msg = "";
$retourliste = TRUE;

switch ($td) {
    // Ajout de la fiche Dossier
    case "add" :
        $coded = crecode("DS", TRUE);

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "SELECT COUNT(*) FROM `" . TBDOS . "` WHERE `dos_code` = '" . $db->escape($coded) . "'";
        $nbres = $db->get_var($rech);
        if ($nbres == 0) {
            $rech2 = "INSERT INTO `" . TBDOS . "`";
            $rech2 .= " (dos_code, dos_codecli, dos_titre, dos_res, dos_datecrea,dos_codecrea,dos_datemod,dos_codemod,";
            $rech2 .= "dos_datedde,dos_datefinp,dos_codeage,dos_urg,dos_codesuiv,dos_codedosc,dos_codedoss,dos_proba,";
            $rech2 .= "dos_avance,dos_datefin,dos_codeadev)";
            $rech2 .= " VALUES";
            $rech2 .= " ('$coded','$codecli','" . $db->escape($titred) . "','" . $db->escape($resum) . "','$datea','$codeuser','$datea','$codeuser','$datedde','$datefinp',";
            $rech2 .= "'$codeage','$urg','$codesuiv','$codecat','$codestatut','$proba','0','','$codeadev')";
            $db->query($rech2);

            // Remise à niveau type client
            upd_nivcli($codecli, FALSE);

            $msg = _("Fiche ajoutée");

        } else {
            $msg = _("Ce code existe déjà");
            $retourliste = FALSE;
        }

        $db->disconnect();

        if ($retourliste == TRUE) {
            // Envoi d'email vers le webmaster
            envoi_mailwm("", "Mise en place d'un nouveau dossier de suivi (" . $coded . ")", "dos", "cre", "xxx", "age", $coded, "", "");
            // Envoi d'email vers l'agence (Si pas créé par une agence)
            if (@$_SESSION["niv_log"] <> 4)
                envoi_mail("", _("Nouveau dossier de suivi"), "dos", "cre", "xxx", "age", $coded, "", "", FALSE);
            // Envoi d'email vers le centre de gestion (Si pas créé par le CDG)
            if (@$_SESSION["niv_log"] <> 5)
                envoi_mail("", _("Nouveau dossier de suivi"), "dos", "cre", "xxx", "cdg", $coded, "", "", FALSE);
        }

        break;

    // MAJ de la fiche Dossier
    case "edit" :
        $majok = TRUE;
        // Contrôle si date de cloture
        if (datetolocal($datefin) <> "") {
            if (exist_dosd_open_dos($code, TRUE) == TRUE) {
                $majok = FALSE;
                $retourliste = FALSE;
                $msg = _("Il y a encore des détails ouverts pour ce dossier");
            }
        }

        if ($majok == TRUE) {
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

            $rech = "UPDATE `" . TBDOS . "` SET `dos_titre`='" . $db->escape($titred) . "', `dos_res`='" . $db->escape($resum) . "',`dos_datemod`='" . $datea . "',";
            $rech .= "`dos_codemod`='" . $codeuser . "',`dos_urg`='" . $urg . "',`dos_codesuiv`='" . $codesuiv . "',`dos_codedosc`='" . $codecat . "',";
            $rech .= "`dos_codedoss`='" . $codestatut . "',`dos_proba`='" . $proba . "',`dos_avance`='" . $ave . "',`dos_datefin`='" . $datefin . "',`dos_codeadev`='" . $codeadev . "'";
            $rech .= " WHERE `dos_code` = '" . $db->escape($code) . "'";
            $db->query($rech);

            // Remise à niveau type client
            upd_nivcli($codecli, FALSE);

            $db->disconnect();

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

            // On prévient le Webmaster
            envoi_mailwm("", _("Dossier de suivi N°") . $code . ' ' ._("mis à jour"), "dos", "mod", "xxx", "age", $code, "", "");
            // Envoi d'email vers l'agence
            if (@$_SESSION["niv_log"] <> 4)
                envoi_mail("", _("Dossier de suivi N°") . $code . ' ' ._("mis à jour"), "dos", "mod", "xxx", "age", $code, "", "", FALSE);
            // Envoi d'email vers le centre de gestion
            if (@$_SESSION["niv_log"] <> 5)
                envoi_mail("", _("Dossier de suivi N°") . $code . ' ' ._("mis à jour"), "dos", "mod", "xxx", "cdg", $code, "", "", FALSE);
        }
        break;


    // Suppression de la fiche Dossier
    case "del" :
        if ($codestatut <> "CLOT") {
            // Envoi d'email vers l'agence
            if (@$_SESSION["niv_log"] <> 4)
                envoi_mail("", _("Annulation de dossier de suivi"), "dos", "del", "xxx", "age", $code, "", "", FALSE);
            // Envoi d'email vers le centre de geston
            if (@$_SESSION["niv_log"] <> 5)
                envoi_mail("", _("Annulation de dossier de suivi"), "dos", "del", "xxx", "cdg", $code, "", "", FALSE);

            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            $rech = "DELETE FROM `" . TBDOS . "` WHERE `dos_code` = '" . $db->escape($code) . "'";
            $db->query($rech);

            // Remise à niveau type client
            upd_nivcli($codecli, FALSE);

            // Suppression des intervenants pour chaque détails du dossier
            $rech = "SELECT dosd_id FROM `" . TBDOSD . "` WHERE `dosd_codedos`='" . $db->escape($code) . "'";
            $row = $db->get_row($rech, ARRAY_A);
            if ($row) {
                extract($row);
                $rech = "DELETE FROM `" . TBDOSDI . "` WHERE `dosdi_iddosd` = '$dosd_id'";
                $db->query($rech);
            }

            // Suppression des détails du dossier
            $rech = "DELETE FROM `" . TBDOSD . "` WHERE `dosd_codedos` = '" . $db->escape($code) . "'";
            $db->query($rech);

            // Suppression des tâches rattachées à ce contrat
            $rech = "DELETE FROM `" . TBTAC . "` WHERE `tac_coderat` = '" . $db->escape($code) . "'";
            $db->query($rech);

            $db->disconnect();

            $msg = _("Fiche supprimée");
        } else {
            $msg = _("Suppression impossible");
            $retourliste = FALSE;
        }
        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 _("Fiche Dossier"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            <?PHP
            if ($retourliste == FALSE)
                echo "warn_txt('" . str_escape($msg) . "');";
            ?>
            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)
    aff_ret_idx_man($td, "dos_lst.php?ord=ASC&tri=Fin", _("Retour à la liste"), TRUE);
else
    pop_ret_auto($msg, 'ok', "dos_lst.php?ord=ASC&tri=Fin");
include_once("pied.php");
?>
</body>
</html>