<?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/fic_age.inc.php");
include_once("../inc/fic_hum.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_tac.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"];

$code = str_majuscules(@$_POST["code"]);
$datecrea = datetomysql(@$_POST["datecreaf"]);
$datefinp = datetomysql(@$_POST["datefinpf"]);
$urg = @$_POST["urgence"];
$titred = @$_POST["titred"];
$afaire = @$_POST["afaire"];
$codecat = str_majuscules(@$_POST["codecat"]);
$tdest = @$_POST["typdest"];
$typdest = '';
switch ($tdest) {
    case -1 :
        switch (@$_SESSION["niv_log"]) {
            case 3 :
                $typdest = 1;
                break;
            case 4 :
                $typdest = 2;
                break;
            case 5 :
                $typdest = 5;
                break;
            case 9 :
                $typdest = 6;
                break;
            case 10 :
                $typdest = 4;
                break;
            case 11 :
                $typdest = 3;
                break;
        }
        break;
    default :
        $typdest = $tdest;
        break;
}
$dest = str_majuscules(@$_POST["dest"]);
switch ($typdest) {
    case 5 :
        $dest = "CGE";
        break;
    case 6 :
        $dest = $admin_code;
        break;
}
$typrat = @$_POST["typrat"];
$coderat = str_majuscules(@$_POST["rat"]);
$ave = @$_POST["ave"];
$datefin = datetomysql(@$_POST["datefinf"]);

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

$msg = "";
$retourliste = TRUE;

switch ($td) {
    // Ajout de la fiche tâche
    case "add" :
        $codeta = crecode("TA", TRUE);

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "SELECT COUNT(*) FROM `" . TBTAC . "` WHERE `tac_code` = '$codeta'";
        $nbres = $db->get_var($rech, ARRAY_A);
        if ($nbres == 0) {
            $rech2 = "INSERT INTO `" . TBTAC . "`";
            $rech2 .= " (tac_code, tac_titre, tac_afaire, tac_datecrea,tac_codecrea,tac_datemod,tac_codemod,";
            $rech2 .= "tac_typdest,tac_codedest,tac_typrat,tac_coderat,tac_datefinp,tac_urg,tac_codetacc,";
            $rech2 .= "tac_avance,tac_datefin)";
            $rech2 .= " VALUES";
            $rech2 .= " ('$codeta','$titred','$afaire','$datecrea','$codeuser','$datea','$codeuser',";
            $rech2 .= "'$typdest','$dest','$typrat','$coderat','$datefinp','$urg','$codecat','0','')";
            $db->query($rech2);

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

        $db->disconnect();

        if ($retourliste == TRUE) {
            // Envois des emails
            switch ($typdest) {
                // Technicien agence
                case 1 :
                case 3 :
                    if (@$_SESSION["code_log"] <> $dest)
                        envoi_mail("", "Nouvelle tâche (" . $codeta . ")", "tac", "cre", "xxx", "tec", $codeta, "", "", TRUE);
                    break;
                // Agence
                case 2 :
                    if (@$_SESSION["code_log"] <> $dest)
                        envoi_mail("", "Nouvelle tâche (" . $codeta . ")", "tac", "cre", "xxx", "age", $codeta, "", "", TRUE);
                    break;
                // Sous-traitant
                case 4 :
                    if (@$_SESSION["code_log"] <> $dest)
                        envoi_mail("", "Nouvelle tâche (" . $codeta . ")", "tac", "cre", "xxx", "st", $codeta, "", "", TRUE);
                    break;
                // Centre de gestion
                case 5 :
                    if (@$_SESSION["niv_log"] <> 5)
                        envoi_mail("", "Nouvelle tâche (" . $codeta . ")", "tac", "cre", "xxx", "cdg", $codeta, "", "", TRUE);
                    break;
            }
        }
        break;

    // MAJ de la fiche Tâche
    case "edit" :
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

        $rech = "UPDATE `" . TBTAC . "` SET `tac_titre`='" . $titred . "', `tac_afaire`='" . $afaire . "',`tac_datemod`='" . $datea . "',";
        $rech .= "`tac_codemod`='" . $codeuser . "',`tac_typdest`='" . $typdest . "',`tac_codedest`='" . $dest . "',`tac_typrat`='" . $typrat . "',";
        $rech .= "`tac_datefinp`='" . $datefinp . "',`tac_urg`='" . $urg . "',`tac_codetacc`='" . $codecat . "',`tac_avance`='" . $ave . "',";
        $rech .= "`tac_datefin`='" . $datefin . "',`tac_coderat`='" . $coderat . "'";
        $rech .= " WHERE `tac_code` = '" . $code . "'";
        $db->query($rech);

        $db->disconnect();

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

        // Envois des emails
        switch ($typdest) {
            // Technicien agence
            case 1 :
            case 3 :
                if (@$_SESSION["code_log"] <> $dest)
                    envoi_mail("", "Modification tâche (" . $code . ")", "tac", "mod", "xxx", "tec", $code, "", "", TRUE);
                break;
            // Agence
            case 2 :
                if (@$_SESSION["code_log"] <> $dest)
                    envoi_mail("", "Modification tâche (" . $code . ")", "tac", "mod", "xxx", "age", $code, "", "", TRUE);
                break;
            // Sous-traitant
            case 4 :
                if (@$_SESSION["code_log"] <> $dest)
                    envoi_mail("", "Modification tâche (" . $code . ")", "tac", "mod", "xxx", "st", $code, "", "", TRUE);
                break;
            // Centre de gestion
            case 5 :
                if (@$_SESSION["niv_log"] <> 5)
                    envoi_mail("", "Modification tâche (" . $code . ")", "tac", "mod", "xxx", "cdg", $code, "", "", TRUE);
                break;
        }

        break;


    // Suppression de la fiche Dossier
    case "del" :
        if ($datefin <> "") {
            // Envois des emails
            switch ($typdest) {
                // Technicien agence
                case 1 :
                case 3 :
                    if (@$_SESSION["code_log"] <> $dest)
                        envoi_mail("", "Suppression tâche (" . $code . ")", "tac", "del", "xxx", "tec", $code, "", "", TRUE);
                    break;
                // Agence
                case 2 :
                    if (@$_SESSION["code_log"] <> $dest)
                        envoi_mail("", "Suppression tâche (" . $code . ")", "tac", "del", "xxx", "age", $code, "", "", TRUE);
                    break;
                // Sous-traitant
                case 4 :
                    if (@$_SESSION["code_log"] <> $dest)
                        envoi_mail("", "Suppression tâche (" . $code . ")", "tac", "del", "xxx", "st", $code, "", "", TRUE);
                    break;
                // Centre de gestion
                case 5 :
                    if (@$_SESSION["niv_log"] <> 5)
                        envoi_mail("", "Suppression tâche (" . $code . ")", "tac", "del", "xxx", "cdg", $code, "", "", TRUE);
                    break;
            }

            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            $rech = "DELETE FROM `" . TBTAC . "` WHERE `tac_code` = '$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"); ?> : Fiche Tâche</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, "tac_lst.php?ord=DES&tri=Code", _("Retour à la liste"), TRUE);
} else
    pop_ret_auto($msg, 'ok', "tac_lst.php?ord=DES&tri=Code");
include_once("pied.php");
?>
</body>
</html>