<?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_do.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_co.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_ot.inc.php");
include_once("../inc/fic_statut.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();
$_GET = clean_str($_GET);

$retourok = TRUE;

if (@$_SESSION["niv_log"] == 9) {
    $msg = "";

    // Variables Formulaire
    $code = str_majuscules(@$_GET["ci"]);
    $codeuser = @$_GET["code_log"];

    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT * FROM `" . TBINT . "` WHERE `int_code` = '" . $db->escape($code) . "'";
    $row = $db->get_row($rech, ARRAY_A);
    if ($row) {
        extract($row);

        $reopen_ok = TRUE;
        $cocl_typh = -1;

        // Si intervention rattachée à un contrat, manipulations complémentaires
        if ($int_codecocl <> "") {
            $rech2 = "SELECT * FROM `" . TBCOCL . "` WHERE `cocl_code` = '" . $int_codecocl . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2) {
                extract($row2);

                // Si la date du jour est supérieure à la date de fin du contrat,
                // il n'est pas possible de réouvrir l'intervention
                if (datetolocal($cocl_datef) <> "") {
                    if (comp_date($cocl_datef, $datea) == TRUE) {
                        $reopen_ok = FALSE;
                        $msg = _("Le contrat rattaché à cette intervention est en échéance dépassée") . "<br />" . _("Impossible de réouvrir cette intervention");
                        $retourok = FALSE;
                    }
                }
            }
        }

        if ($reopen_ok == TRUE) {
            // On supprime la date de cloture de cette intervention
            $rech2 = "UPDATE `" . TBINT . "` SET `int_datemod`='" . $datea . "', `int_codemod`='" . $codeuser . "',";
            $rech2 .= " `int_datefin`='', `int_codestatut`='EC'";
            $rech2 .= " WHERE `int_code` = '" . $db->escape($code) . "'";
            $db->query($rech2);
            $msg = _("Intervention") . " " . $code . " " . _("réouverte");

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

            // Réouverture des problèmes affectés à l'intervention
            $rech2 = "SELECT COUNT(*) FROM `" . TBRPBM . "` WHERE `rpbm_codeint` = '" . $db->escape($code) . "'";
            $nbres2 = $db->get_var($rech2);
            if ($nbres2 > 0) {
                $rech2 = "UPDATE `" . TBRPBM . "` SET `rpbm_datett`= '0000-00-00' WHERE `rpbm_codeint`= '" . $db->escape($code) . "'";
                $db->query($rech2);
                $msg .= "<br /><br />" . _("Problèmes attachés réouverts");
            }

            // Suppression des reports
            $rech2 = "DELETE FROM `" . TBCOCLR . "` WHERE `coclr_codeint` = '" . $db->escape($code) . "'";
            $db->query($rech2);

            // Réouverture du contrat
            $rech2 = "UPDATE `" . TBCOCL;
            $rech2 .= "` SET `cocl_datefin`='0000-00-00'";
            $rech2 .= " WHERE `cocl_code` = '" . $cocl_code . "'";
            $db->query($rech2);

            if ($cocl_typh == 1) // Contrat 'Quota Horaire'
                $msg .= "<br /><br />" . ("Solde contrat") . " " . $cocl_code . " " . _("remis à jour") . "<br />( " . _("Contrat réouvert") . ")";
            else
                $msg .= "<br /><br />" . _("Contrat") . " " . $cocl_code . " " . _("réouvert");
        }
    } else {
        $msg = _("Cette intervention n'existe pas");
        $retourok = FALSE;
    }
    $db->disconnect();
} else {
    $msg = _("Vous n'avez pas le droit de réouvrir une intervention");
    $retourok = FALSE;
}
?>
<!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 _("Réouverture Intervention"); ?></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) . "');";
            else
                echo "pop_txt('" . str_escape($msg) . "');";
            ?>
        }

        $(document).ready(function () {
            init_all();
        });
        //-->
    </script>

</head>
<body>
<p align="center"><span class="titrepage"><?PHP echo _("Réouverture Intervention"); ?></span></p>

<p align="center">&nbsp;</p>
<?PHP
if ($retourok == FALSE)
    close_fen_auto(_("Retour à la liste des interventions"), 'fscomp', 'window.opener.', 5000);
else
    close_fen_auto(_("Retour à la liste des interventions"), 'fscomp', 'window.opener.');
?>
</body>
</html>
