<?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_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"];

$datecrea = datetomysql(@$_POST["datecreaf"]);
$code = str_majuscules(@$_POST["code"]);
$codecli = str_majuscules(@$_POST["codecli"]);
$codeage = str_majuscules(@$_POST["codeage"]);
$nota = @$_POST["nota"];

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

$msg = '';
$retourliste = TRUE;

if (@$_POST["nbutton"] == "enrimp") {
    $imprim = TRUE;
    $retourliste = FALSE;
}

switch ($td) {
    // Ajout de la fiche relevé
    case "add" :
        $coder = crecode("P", TRUE);

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "INSERT INTO `" . TBRPAT . "`";
        $rech .= " (rpat_datecrea,rpat_codecrea,rpat_datemod,rpat_codemod,rpat_code,rpat_codecli,rpat_codeage,";
        $rech .= "rpat_nota)";
        $rech .= " VALUES";
        $rech .= " ('$datecrea','$codeuser','$datea','$codeuser','$coder','$codecli','$codeage',";
        $rech .= "'" . $db->escape($nota) . "')";
        $db->query($rech);

        // MAJ de la fiche Client (pour rendre l'agence ayant effectué le relevé "propriétaire" du client)
        $rech2 = "UPDATE `" . TBCLI . "` SET `cli_datemod`='" . $datea . "', `cli_codemod`='" . $codeuser . "', `cli_prop`='" . $codeage . "',";
        $rech2 .= " `cli_type`='2'  WHERE `cli_code` = '" . $codecli . "'";
        $db->query($rech2);

        $db->disconnect();

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

        /*
                // Envoi d'email vers le client
                envoi_mail("","création de votre dossier de relevé de parc","rel","cre","xxx","cli",$coder,"","",FALSE);
                // Envoi d'email vers l'agence
                if (@$_SESSION["niv_log"] <> 4)
                    envoi_mail("","création de dossier relevé de parc","rel","cre","xxx","age",$coder,"","",FALSE);
                // Envoi d'email vers le centre de gestion
                envoi_mail("","création de dossier de relevé de parc","rel","cre","xxx","cdg",$coder,"","",TRUE);
        */
        break;

    // MAJ de la fiche relevé
    case "edit" :
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "UPDATE `" . TBRPAT;
        $rech .= "` SET `rpat_datemod`='" . $datea . "', `rpat_codemod`='" . $codeuser . "',`rpat_code`='" . $code . "', `rpat_codecli`='" . $codecli . "', `rpat_codeage`='" . $codeage . "',";
        $rech .= " `rpat_nota`='" . $db->escape($nota) . "'";
        $rech .= " WHERE `rpat_code` = '" . $db->escape($code) . "'";
        $db->query($rech);
        $db->disconnect();

        $msg = _("Fiche modifiée");
        /*
                // Envoi d'email vers le client
                envoi_mail("","Modification de votre dossier de relevé de parc","rel","mod","xxx","cli",$code,"","",FALSE);
                // Envoi d'email vers l'agence
                if (@$_SESSION["niv_log"] <> 4)
                    envoi_mail("","Modification de dossier de relevé de parc","rel","mod","xxx","age",$code,"","",FALSE);
                // Envoi d'email vers le centre de gestion
                envoi_mail("","Modification de dossier de relevé de parc","rel","mod","xxx","cdg",$code,"","",TRUE);
        */
        break;

    // Suppression de la fiche relevé
    case "del" :
        $ok_del = TRUE;
        /*
                $exist_rpat_in_rlic = exist_rpat_in_rlic($code);
                if ($exist_rpat_in_rlic == TRUE)
                {
                    $ok_del = FALSE;
                    $msg = "Ce dossier relevé contient des fiches de relevés licences. Supprimez-les.";
                }

                $exist_rpat_in_rpdt = exist_rpat_in_rpdt($code);
                if ($exist_rpat_in_rpdt == TRUE)
                {
                    $ok_del = FALSE;
                    if ($msg == "")
                        $msg = "Ce dossier relevé contient des fiches de relevés matériels. Supprimez-les.";
                    else
                        $msg .= "<br /><br />Ce dossier relevé contient des fiches de relevés matériels. Supprimez-les.";
                }
        */
        if ($ok_del == TRUE) {
            // SUPPRESSION de la fiche relevé
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            $rech = "DELETE FROM `" . TBRPAT . "` WHERE `rpat_code` = '" . $db->escape($code) . "'";
            $db->query($rech);
            /*
                        // Suppression des tâches rattachées à ce relevé
                        $rech = "DELETE FROM `" .TBTAC. "` WHERE `tac_coderat` = '".$db->escape($code)."'";
                        $db->query($rech);
            */
            $db->disconnect();

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

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

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            init_new_fen('imprim','impression','click');
            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) . "')");
    if ($imprim == TRUE) {
        if ($td == "add")
            $urlimp = "rpat_pdf.php?code=" . $codeco;
        else
            $urlimp = "rpat_pdf.php?code=" . $code;

        if (url_ok($urlimp) == TRUE)
            cre_falselnk('imprim', 'imprim', 'div', 'center', $urlimp, '', '../img/print.png', _("Imprimer"));

    }
    aff_ret_idx_man($td, "rpat_lst.php", _("Retour à la liste"), TRUE);
} else
    pop_ret_auto($msg, 'ok', "rpat_lst.php");
include_once("pied.php");
?>
</body>
</html>