<?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/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_txt.inc.php");
include_once("../inc/lst.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_int.inc.php");
// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);
$_GET = clean_str($_GET);

$td = '';
$str_pbm = '';

$pbm_rpbm = FALSE;

$action_to_do = '';
if (trim(@$_POST["val"]) <> '') // On a cliqué sur valider
    $action_to_do = 'valid';

if ($action_to_do == 'valid') {
    $txtmatos = "";
    $txtpbm = "";
    $lstpbm = "";
    if (count($_POST["rpbm_choisi"]) <> 0)
        $lstpbm = implode("-", $_POST["rpbm_choisi"]);
    $_SESSION["pbm_choisis"] = $lstpbm;
    $str_pbm = cre_str_pbm_int($_SESSION["pbm_choisis"]);
    $str_pbm = str_replace("'", "\'", $str_pbm);
} else {
    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"]);
    if ((isset($_GET['td'])) AND (isset($_GET['cc'])) AND (isset($_GET['ci'])) AND (isset($_GET['si']))) {
        $td = $_GET['td']; // Récupération type de demande

        if (($td <> "add") AND ($td <> "edit"))
            $pbm_rpbm = TRUE;
        $cc = $_GET['cc']; // Récupération code Client
        if ($cc <> "") {
            $row = lit_enr_cli($cc, TRUE);
            if (!is_array($row))
                $pbm_rpbm = TRUE;
        } else
            $pbm_rpbm = TRUE;
        $ci = $_GET['ci']; // Récupération code Intervention
        if ($ci <> "") {
            if ($td <> "add") {
                $row = lit_enr_int($ci);
                if (!is_array($row))
                    $pbm_rpbm = TRUE;
            }
        } else {
            if ($td <> "add")
                $pbm_rpbm = TRUE;
        }
        $si = intval($_GET['si']); // Récupération de l'id du site
        if ($si <> 0) {
            $row = lit_enr_scli_id($si, TRUE);
            if (!is_array($row))
                $pbm_rpbm = TRUE;
        }
    } else
        $pbm_rpbm = 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 _("Liste Problèmes"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            <?php
            switch ($td)
            {
                case "add" :
                    break;
                case "edit " :
                    break;
                case "del" :
                    echo "readonly_all();";
                    echo "warn_del('');";
                    break;
                case "view" :
                    echo "readonly_all();";
                    break;
            }
            if ($action_to_do == 'valid')
            {
                echo "update_detail_pbm(true,'detailpbm','".$str_pbm."','viewpbm','viewpbmfait');";
                echo "self.close();";
            }
            else
            {
                echo "chk_valbut('chx','tab_lst_pbm','"._("Tous")."','"._("Aucun")."');";
                echo "init_chk_valbut('chx','tab_lst_pbm','"._("Tous")."','"._("Aucun")."');";
                echo "init_check_all('chx','tab_lst_pbm','"._("Tous")."','"._("Aucun")."');";
                echo "add_func_click('annul','sclose','');";
                echo "Focus_first();";
            }
            ?>
        }

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

<body>
<?PHP
if ($pbm_rpbm == FALSE) {
    if (@$_POST["val"] <> _("Valider")) // Si le formulaire n'a pas été validé
    {
        ?>
        <p align="center"><span class="titrepage"><?PHP echo _("LISTE DES PROBLEMES DECLARES"); ?></span></p>
        <p align="center">
            <?PHP
            echo _("Merci de choisir les problèmes signalés que vous souhaitez affecter à l'intervention");
            if ($td == "add")
                echo ' ' . _("en cours de création") . ',';

            if ($td == "edit")
                echo ' <span class="annot_std_gras">' . $ci . '</span>,';

            if ($si == 0)
                echo '<br />' . _("et déclarés sur le") . ' <span class="annot_std_gras">' . _("site principal") . '</span>';

            if ($si > 0) {
                $row = lit_enr_scli_id($si, TRUE);
                if (is_array($row) == TRUE) {
                    $row = encode_str($row);
                    extract($row);
                    echo '<br />' . _("et déclarés sur le site") . ' : <span class="annot_std_gras">' . $scli_ste . '</span>';
                }
            }
            ?>
        </p>
        <form name="rpbm_lst" method="post" action="rpbm_lst.php">
            <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
            <?PHP
            // création de la liste des relevés de problèmes non résolus
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            if ($td == "edit") // On est édition d'intervention
                $rech = "SELECT * FROM `" . TBRPBM . "` WHERE (`rpbm_codeint` = '" . $db->escape($ci) . "' AND `rpbm_idsite`= '" . $si . "') OR (`rpbm_codecli`= '" . $db->escape($cc) . "' AND `rpbm_idsite`= '" . $si . "' AND `rpbm_codeint` IS NULL) ORDER BY `rpbm_datecrea`";
            if ($td == "add") // On est en création d'intervention
                $rech = "SELECT * FROM `" . TBRPBM . "` WHERE `rpbm_codecli`= '" . $db->escape($cc) . "' AND `rpbm_idsite`= '" . $si . "' AND `rpbm_codeint` IS NULL ORDER BY `rpbm_datecrea`";
            $rows = $db->get_results($rech, ARRAY_A);

            if ($rows) {
                echo '<div align="center">';
                echo '<input type="button" id="chx" name="chx" value="' . _("Tous") . '" class="bton_fgris_petit">';
                echo '</div>';
                echo '<br />';

                echo '<div align="center" id="tab_lst_pbm">';
                echo '<table class="MtableD" border="0" width="890" cellpadding="0" cellspacing="0" align="center">';

                // création de l'entête de la table "relevés de problèmes"
                echo "<th>";
                echo _("Déclaré le");
                echo "</th>\n";
                echo "<th>";
                echo _("Déclaré par");
                echo "</th>\n";
                echo "<th>";
                echo _("Relevé / Code Produit / Problème");
                echo "</th>\n";
                echo "<th>";
                echo _("Choisir");
                echo "</th>\n";

                $j = 0;
                $lstpbm = "";

                foreach ($rows as $row) {
                    $row = encode_str($row);
                    // création de l'entête de ligne (TR)
                    creetrlstdet($j);

                    echo "<td align=\"center\">\n";
                    echo datetolocal($row["rpbm_datecrea"]);
                    echo "</td>\n";
                    echo "<td>\n";
                    echo $row["rpbm_signale"];
                    echo "</td>\n";

                    echo "<td>\n";
                    if ($row["rpbm_typrel"] == 1) // C'est une licence
                        $rech2 = "SELECT * FROM `" . TBRLIC . "` WHERE `rlic_code`= '" . $row["rpbm_coderlp"] . "'";
                    else // C'est du matériel
                        $rech2 = "SELECT * FROM `" . TBRPDT . "` WHERE `rpdt_code`= '" . $row["rpbm_coderlp"] . "'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2) {
                        $row2 = encode_str($row2);
                        extract($row2);

                        if ($row["rpbm_typrel"] == 1) // C'est une licence
                        {
                            $rech3 = "SELECT * FROM `" . TBMQUE . "` WHERE `mque_code` = '" . $rlic_codema . "'";
                            $row3 = $db->get_row($rech3, ARRAY_A);
                            if ($row3) {
                                $row3 = encode_str($row3);
                                extract($row3);
                            }
                            $txtmatos = _("Relevé") . ' ' . $rlic_code . ' (' . _("licence") . ') :<br />' . $mque_ste . ', ' . _("modèle") . ' : ' . $rlic_modele . ', ' . _("ver.") . ' : ' . $rlic_version;
                        }
                        if ($row["rpbm_typrel"] == 2) // C'est du matériel
                        {
                            $rech3 = "SELECT * FROM `" . TBRPCAT . "` WHERE `rpcat_code` = '" . $rpdt_codecat . "'";
                            $row3 = $db->get_row($rech3, ARRAY_A);
                            if ($row3) {
                                $row3 = encode_str($row3);
                                extract($row3);
                            }
                            $rech3 = "SELECT * FROM `" . TBMQUE . "` WHERE `mque_code` = '" . $rpdt_codema . "'";
                            $row3 = $db->get_row($rech3, ARRAY_A);
                            if ($row3) {
                                $row3 = encode_str($row3);
                                extract($row3);
                            }
                            $txtmatos = _("Relevé") . ' ' . $rpdt_code . ' (' . _("matériel") . ' :<br />' . $rpcat_desc . ' / ' . $mque_ste . ', ' . _("modèle") . ' : ' . $rpdt_modele . ' (' . _("code") . ' : ' . $rpdt_codep . ')';
                        }
                    }

                    echo "<strong>" . $txtmatos . "</strong><br /><br />";
                    $tab_txtpbm = cesuretxt_tab($row["rpbm_pbm"], 100);
                    $txtpbm = "";
                    foreach ($tab_txtpbm as $index_ligne => $ligne)
                        $txtpbm .= $ligne . "<br />";
                    echo $txtpbm;
                    echo "</td>\n";

                    echo "<td align=\"center\">\n";
                    $choisi = FALSE;
                    if ($_SESSION["pbm_choisis"] <> "") {
                        if (strpos($_SESSION["pbm_choisis"], $row["rpbm_id"]) !== false)
                            $choisi = TRUE;
                    }

                    echo '<input type="checkbox" name="rpbm_choisi[]" id="chk' . $row["rpbm_id"] . '" value="' . $row["rpbm_id"] . '"';
                    if ($j % 2 == 0)
                        echo ' class="TRalter1D"';
                    else
                        echo ' class="TRalter2D"';


                    if ($choisi == TRUE)
                        echo ' checked';

                    echo '>';

                    echo "</td>\n";

                    echo "</tr>\n";

                    $j++;
                }

                echo '</table>';
                echo '</div>';
                // Fin affichage table détails licences

                echo '<p align="center">';
                echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="' . _("Remise à Zéro") . '">';
                echo '<input name="val" type="submit" class="bton_std" id="val" value="' . _("Valider") . '">';
                echo '<input name="annul" type="button" class="bton_std" id="annul" value="' . _("Annuler") . '">';
                echo '</p>';
            } else {
                echo '<div align="center">';
                echo '<span class="annot_gras_it">' . _("Aucun problème déclaré") . '</span>';
                echo '<br />';
                echo '<input name="annul" type="button" class="bton_std" id="annul" value="' . _("Quitter") . '">';
                echo '</div>';
            }
            $db->disconnect();
            ?>
        </form>
    <?PHP
    } else
        echo '<body>';
} else
    close_fen_auto('', '');
?>
</body>
</html>
