<?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
 ****************************************************************************************/

session_start();

require_once("../lib/gettext/gettext.inc");
include_once("../inc/config.inc.php");
include_once("../inc/func_log.inc.php");
if (CheckURLLog(TRUE) == FALSE) header("Location: ../index.php");
if (CheckNivLog("1") == FALSE) header("Location: ../index.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_txt.inc.php");
include_once("../inc/func.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("../inc/func_date.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);

$pbm_dpbm = FALSE;
$pbm_expw = FALSE;
$nouvpwok = FALSE;

$cc = $_GET['cc']; // Récupération code Client
$tab_cli = [];

$row = lit_enr_cli($cc, TRUE);
if (is_array($row)) {
    $row = encode_str($row);
    extract($row);

    if ($_SESSION["niv_log"] == 1) {
        $tab_cli = give_tab_rcli($_SESSION["code_log"], TRUE);
        if (in_array($cli_code, $tab_cli) == FALSE)
            $pbm_dpbm = TRUE;
    }
} else
    $pbm_dpbm = TRUE;
?>
<!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 _("Déclaration de problème"); ?></title>

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

    <script language="javascript">
        <!--
        var tab_champ = new Array(
            "nom:chxml ; format:liste ; req:Y ; txt:<?PHP echo _("matériel ou licence concerné"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad");
        //-->
    </script>

    <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");
if ($pbm_dpbm == FALSE) {
    ?>
    <p align="center"><span class="titrepage"><?PHP echo _("DECLARATION DE PROBLEME"); ?></span></p>
    <br />
    <form name="dpbm" id="dpbm" action="" method="post">
        <table width="100%" border="0" align="center">
            <tr>
                <td class="rubfrm" id="f_chxml">
                    <div align="center">
                        <?PHP echo _("Merci de choisir le matériel ou la licence ayant un problème"); ?>
                    </div>
                </td>
            </tr>
            <tr>
                <td align="center">
	<span class="annot_petit_it">
	<?PHP echo _("Licences = Relevé (Site utilisateur, Marque Modèle Version)"); ?><br />
	<?PHP echo _("Matériels = Relevé (Site utilisateur, Inventaire, Marque Modèle Numéro de série)"); ?>
	</span>
                    <br /><br />
                    <select name="chxml" id="chxml" onChange="self.location.href=this.value">
                        <?PHP
                        echo '<option value="">'._("-- Faites votre choix --").'</option>';

                        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

                        // Création de la liste des licences actives
                        switch ($_SESSION["niv_log"]) {
                            case 1 :
                                $rech = "SELECT * FROM `" . TBRLIC . "` WHERE";
                                if (count($tab_cli) <= 1)
                                    $rech .= " `rlic_codecli` = '" . $_SESSION["code_log"] . "'";
                                else {
                                    $rech .= "(";
                                    foreach ($tab_cli as $key => $value) {
                                        $rech .= " `rlic_codecli` = '" . $value . "' OR";
                                    }
                                    // Suppression du dernier 'OR'
                                    $rech = substr($rech, 0, strlen($rech) - 3);

                                    $rech .= ")";
                                }
                                $rech .= " AND `rlic_dateout` = '0000-00-00' ORDER BY rlic_code";
                                break;
                            default :
                                $rech = "SELECT * FROM `" . TBRLIC . "` WHERE `rlic_codecli` = '" . $cc . "' AND `rlic_dateout` = '0000-00-00' ORDER BY rlic_code";
                                break;
                        }

                        $rows = $db->get_results($rech, ARRAY_A);

                        if ($rows) {
                            echo '<option value="" disabled="disabled"></option>';
                            echo '<option value="" disabled="disabled">'._("-- Licences actives--").'</option>';

                            foreach ($rows as $resultat) {
                                encode_str($resultat);
                                extract($resultat);

                                $lien = "rpbm_maj.php?td=add&tr=1&cr=" . $rlic_code . "&cap=0";
                                $nom = $rlic_code . " (" . $rlic_codecli . ", ";
                                if ($rlic_idsite <> -1) {
                                    if ($rlic_idsite <> 0) {
                                        $rech2 = "SELECT * FROM `" . TBSCLI . "` WHERE `scli_codecli` = '" . $cc . "' AND `scli_id` = '" . $rlic_idsite . "'";
                                        $row2 = $db->get_row($rech2, ARRAY_A);
                                        if ($row2) {
                                            $row2 = encode_str($row2);
                                            extract($row2);
                                            $nom .= $scli_ste . " - " . Retourneville_Id($scli_ville, FALSE) . ", ";
                                        }
                                    } else
                                        $nom .= _("Site principal").', ';
                                } else
                                    $nom .= _("Tous les sites").', ';

                                $rech2 = "SELECT * FROM `" . TBMQUE . "` WHERE `mque_code` = '" . $rlic_codema . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2) {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                    $nom .= $mque_ste . " ";
                                }

                                $nom .= $rlic_modele . " " . $rlic_version . ")";

                                echo "<option value=\"" . $lien . "\"";
                                echo ">" . $nom . "</option>\n";
                            }
                        } else
                            echo '<option value="" disabled="disabled">'._("-- Aucune licence active --").'</option>';

                        // création de la liste des matériels actifs
                        switch ($_SESSION["niv_log"]) {
                            case 1 :
                                $rech = "SELECT * FROM `" . TBRPDT . "` WHERE";
                                if (count($tab_cli) <= 1)
                                    $rech .= " `rpdt_codecli` = '" . $_SESSION["code_log"] . "'";
                                else {
                                    $rech .= "(";
                                    foreach ($tab_cli as $key => $value) {
                                        $rech .= " `rpdt_codecli` = '" . $value . "' OR";
                                    }
                                    // Suppression du dernier 'OR'
                                    $rech = substr($rech, 0, strlen($rech) - 3);

                                    $rech .= ")";
                                }
                                $rech .= " AND `rpdt_dateout` = '0000-00-00' ORDER BY rpdt_code";
                                break;
                            default :
                                $rech = "SELECT * FROM `" . TBRPDT . "` WHERE `rpdt_codecli` = '" . $cc . "' AND `rpdt_dateout` = '0000-00-00' ORDER BY rpdt_code";
                                break;
                        }

                        $rows = $db->get_results($rech, ARRAY_A);

                        if ($rows) {
                            echo '<option value="" disabled="disabled"></option>';
                            echo '<option value="" disabled="disabled">'._("-- matériels actifs --").'</option>';

                            foreach ($rows as $resultat) {
                                encode_str($resultat);
                                extract($resultat);

                                $lien = "rpbm_maj.php?td=add&tr=2&cr=" . $rpdt_code . "&cap=0";
                                $nom = $rpdt_code . " (" . $rpdt_codecli . ", ";
                                if ($rpdt_idsite <> -1) {
                                    if ($rpdt_idsite <> 0) {
                                        $rech2 = "SELECT * FROM `" . TBSCLI . "` WHERE `scli_codecli` = '" . $cc . "' AND `scli_id` = '" . $rpdt_idsite . "'";
                                        $row2 = $db->get_row($rech2, ARRAY_A);
                                        if ($row2) {
                                            $row2 = encode_str($row2);
                                            extract($row2);
                                            $nom .= $scli_ste . " - " . Retourneville_Id($scli_ville, FALSE) . ", ";
                                        }
                                    } else
                                        $nom .= _("Site principal").', ';
                                } else
                                    $nom .= _("Tous les sites").', ';

                                $nom .= $rpdt_codep . ", ";

                                $rech2 = "SELECT * FROM `" . TBMQUE . "` WHERE `mque_code` = '" . $rpdt_codema . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2) {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                    $nom .= $mque_ste . " ";
                                }

                                $nom .= $rpdt_modele . " " . $rpdt_ns . ")";

                                echo "<option value=\"" . $lien . "\"";
                                echo ">" . $nom . "</option>\n";
                            }
                        } else
                            echo '<option value="" disabled="disabled">'._("-- Aucune licence active --").'</option>';

                        $db->disconnect();
                        echo '</select>';
                        echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _("Champ obligatoire") . '">';
                        ?>
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>
    </form>
<?PHP
} else
    pop_ret_auto(_("Problème de paramètres"), 'warn', "index.php");
include_once("pied.php");
?>
</body>
</html>