<?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/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_st.inc.php");
include_once("../inc/fic_mque.inc.php");
include_once("../inc/fic_rel.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_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"];

$code = str_majuscules(@$_POST["code"]);
$daterel = datetomysql(@$_POST["daterelf"]);
if (@$_POST["dateoutf"] <> "")
    $dateout = datetomysql(@$_POST["dateoutf"]);
else
    $dateout = "";
$coderel = str_majuscules(@$_POST["coderel"]);
$codecli = str_majuscules(@$_POST["codecli"]);
$codeage = str_majuscules(@$_POST["codeage"]);
$site = str_majuscules(@$_POST["site"]);
$codetec = str_majuscules(@$_POST["codetec"]);
$codema = str_majuscules(@$_POST["codema"]);
$modele = @$_POST["modele"];
$version = @$_POST["version"];
$codetl = str_majuscules(@$_POST["codetl"]);
$maxcli = str_majuscules(@$_POST["maxcli"]);
if ($maxcli <= 0)
    $maxcli = 1;
$codemed = str_majuscules(@$_POST["codemed"]);
$numlic = @$_POST["numlic"];
$cleflic = @$_POST["cleflic"];
$ajour = intval(@$_POST["ajour"]);
$legal = intval(@$_POST["legale"]);
if (@$_POST["datevalf"] <> "")
    $dateval = datetomysql(@$_POST["datevalf"]);
else
    $dateval = "";

$nota = @$_POST["nota"];
$signataire = @$_POST["signataire"];
$codeta = str_majuscules(@$_POST["codeta"]);
$tma = str_majuscules(@$_POST["tma"]);
$codel = str_majuscules(@$_POST["codel"]);
$tser = intval(@$_POST["tser"]);
$tlien = @$_POST["tlien"];
$tlogin = @$_POST["tlogin"];
$tmdp = @$_POST["tmdp"];
$tae = intval(@$_POST["tae"]);
$tred = intval(@$_POST["tred"]);
$codem = str_majuscules(@$_POST["codem"]);
$mper = str_majuscules(@$_POST["mper"]);
$mtype = @$_POST["mtype"];
$mlogin = @$_POST["mlogin"];
$mmdp = @$_POST["mmdp"];
if (@$_POST["mdvalf"] <> "")
    $mdval = datetomysql(@$_POST["mdvalf"]);
else
    $mdval = "";
if (@$_POST["mdlmajf"] <> "")
    $mdlmaj = datetomysql(@$_POST["mdlmajf"]);
else
    $mdlmaj = "";
$mae = intval(@$_POST["mae"]);
$mred = intval(@$_POST["mred"]);

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

$retourliste = TRUE;

/*
if (@$_POST["enrimp"] == "Enregistrer & Imprimer") {
    $imprim = TRUE;
    $retourliste = FALSE;
}
*/

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

        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "INSERT INTO `" . TBRLIC . "`";
        $rech .= " (rlic_datecrea,rlic_codecrea,rlic_datemod,rlic_codemod,rlic_coderel,rlic_code,rlic_daterel,rlic_codecli,rlic_codeage,";
        $rech .= "rlic_codetec,rlic_codema,rlic_modele,rlic_version,rlic_codetl,rlic_maxcli,rlic_codemed,rlic_numlic,rlic_cleflic,rlic_ajour,";
        $rech .= "rlic_legal,rlic_dateval,rlic_dateout,rlic_nota,rlic_signataire,rlic_idsite,";
        $rech .= "rlic_codeta,rlic_tma,rlic_codel,rlic_tser,rlic_tlien,rlic_tlogin,rlic_tmdp,rlic_tae,rlic_tred,";
        $rech .= "rlic_codem,rlic_mmode,rlic_mper,rlic_mlogin,rlic_mmdp,rlic_mdval,rlic_mdlmaj,rlic_mae,rlic_mred)";
        $rech .= " VALUES";
        $rech .= " ('$datea','$codeuser','$datea','$codeuser','$coderel','$coderl','$daterel','$codecli','$codeage',";
        $rech .= "'$codetec','$codema','".$db->escape($modele)."','" . $db->escape($version) . "','$codetl','$maxcli','$codemed','" . $db->escape($numlic) . "','".$db->escape($cleflic)."','$ajour',";
        $rech .= "'$legal','$dateval','$dateout','" . $db->escape($nota) . "','" . $db->escape($signataire) . "','$site',";
        $rech .= "'$codeta','$tma','$codel','$tser','$tlien','$tlogin','$tmdp','$tae','$tred',";
        $rech .= "'$codem','$mtype','$mper','" . $db->escape($mlogin) . "','" . $db->escape($mmdp) . "','$mdval','$mdlmaj','$mae','$mred')";
        $db->query($rech);
        $db->disconnect();

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

        // Envoi d'email vers l'agence
        if (@$_SESSION["niv_log"] <> 4)
            envoi_mail("", _("Ajout d'une fiche relevé licence"), "rlic", "cre", "xxx", "age", $coderl, "", "", FALSE);
        // Envoi d'email vers le centre de gestion
        envoi_mail("", _("Ajout d'une fiche relevé licence"), "rlic", "cre", "xxx", "cdg", $coderl, "", "", TRUE);
        break;

    // MAJ de la fiche relevé Licence
    case "edit" :
        $ok_maj = TRUE;

        if ($dateout <> "") {
            $exist_rlic_in_rpdt_actif = exist_rlic_in_rpdt_actif($codecli, $code);
            if ($exist_rlic_in_rpdt_actif == TRUE) {
                $ok_maj = FALSE;
                $retourliste = FALSE;
                $msg = _("Cette fiche est encore utilisée dans un ou des relevé(s) matériel(s) actif(s)") . '<br />' . _("Enlevez-le du (des) fiche(s) relevé(s) matériel(s) concernée(s)");
            }

            $exist_rlic_in_rpbm_actif = exist_rlic_in_rpbm_actif($code);
            if ($exist_rlic_in_rpbm_actif == TRUE) {
                $ok_maj = FALSE;
                $retourliste = FALSE;
                if ($msg == "")
                    $msg = _("Cette fiche contient des problèmes déclarés et non solutionnés") . '<br />' . _("Supprimez la (les) déclaration(s) de problème concernée(s)");
                else
                    $msg .= '<br /><br />' . _("Cette fiche contient des problèmes déclarés et non solutionnés") . '<br />' . _("Supprimez la (les) déclaration(s) de problème concernée(s)");
            }

        }

        if ($ok_maj == TRUE) {
            // MAJ de la fiche relevé Licence
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            $rech = "UPDATE `" . TBRLIC;
            $rech .= "` SET `rlic_datemod`='" . $datea . "', `rlic_codemod`='" . $codeuser . "', `rlic_code`='" . $code . "', `rlic_daterel`='" . $daterel . "', `rlic_codecli`='" . $codecli . "', `rlic_codeage`='" . $codeage . "', `rlic_codetec`='" . $codetec . "', `rlic_codema`='" . $codema . "', `rlic_modele`='" . $db->escape($modele) . "', `rlic_version`='" . $db->escape($version) . "',";
            $rech .= " `rlic_codetl`='" . $codetl . "', `rlic_maxcli`='" . $maxcli . "', `rlic_codemed`='" . $codemed . "',`rlic_numlic`='" . $db->escape($numlic) . "', `rlic_cleflic`='" . $db->escape($cleflic) . "', `rlic_ajour`='" . $ajour . "', `rlic_legal`='" . $legal . "', `rlic_dateval`='" . $dateval . "',";
            $rech .= " `rlic_dateout`='" . $dateout . "', `rlic_nota`='" . $db->escape($nota) . "', `rlic_signataire`='" . $db->escape($signataire) . "', `rlic_idsite`='" . $site . "',";
            $rech .= " `rlic_codeta`='" . $codeta . "', `rlic_tma`='" . $tma . "', `rlic_codel`='" . $codel . "', `rlic_tser`='" . $tser . "', `rlic_tlien`='" . $tlien . "', `rlic_tlogin`='" . $tlogin . "', `rlic_tmdp`='" . $tmdp . "', `rlic_tae`='" . $tae . "', `rlic_tred`='" . $tred . "',";
            $rech .= " `rlic_codem`='" . $codem . "', `rlic_mmode`='" . $mtype . "', `rlic_mper`='" . $mper . "', `rlic_mlogin`='" . $db->escape($mlogin) . "', `rlic_mmdp`='" . $db->escape($mmdp) . "', `rlic_mdval`='" . $mdval . "', `rlic_mdlmaj`='" . $mdlmaj . "', `rlic_mae`='" . $mae . "', `rlic_mred`='" . $mred . "'";
            $rech .= " WHERE `rlic_code` = '" . $db->escape($code) . "'";
            $db->query($rech);
            $db->disconnect();

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

            // Envoi d'email vers l'agence
            if (@$_SESSION["niv_log"] <> 4)
                envoi_mail("", _("Modification d'une fiche relevé licence"), "rlic", "mod", "xxx", "age", $code, "", "", FALSE);
            // Envoi d'email vers le centre de gestion
            envoi_mail("", _("Modification d'une fiche relevé licence"), "rlic", "mod", "xxx", "cdg", $code, "", "", TRUE);

        }
        break;

    // Suppression de la fiche relevé Licence
    case "del" :
        // vérification si le relevé de licence existe dans un relevé produit
        $exist_in_rpdt = exist_rlic_in_rpdt($codecli, $code);
        $exist_in_rpbm = exist_rlic_in_rpbm($code);

        if (($exist_in_rpdt == FALSE) AND ($exist_in_rpbm == FALSE)) {
            // SUPPRESSION de la fiche relevé Licence
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            $rech = "DELETE FROM `" . TBRLIC . "` WHERE `rlic_code` = '" . $db->escape($code) . "'";
            $db->query($rech);
            $db->disconnect();
            $msg = _("Fiche supprimée");
        } else {
            if ($exist_in_rpdt == TRUE) {
                $retourliste = FALSE;
                $msg = _("Impossible de supprimer un relevé de licence utilisé dans une fiche relevé de matériel") . '<br />' . _("Choisissez plutôt de le sortir du parc...ou enlevez-le des fiches relevés matériel concernées");
            }
            if ($exist_in_rpbm == TRUE) {
                $retourliste = FALSE;
                $msg = _("Impossible de supprimer un relevé de licence utilisé dans une déclaration de problème") . '<br />' . _("Choisissez plutôt de le sortir du parc...ou supprimez la déclaration de problème");
            }
        }
        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 Relevé Licence"); ?></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, "rel_maj.php?td=edit&cc=".$codecli."&cr=" . $coderel, _("Retour à la saisie"), FALSE);
} else
    pop_ret_auto($msg, 'ok', "rel_maj.php?td=edit&cc=".$codecli."&cr=" . $coderel);
include_once("pied.php");
?>
</body>
</html>