<?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/fic_cli.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_mque.inc.php");
include_once("../inc/fic_rel.inc.php");
include_once("../inc/lst.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"]);
$_SESSION["typ_filtres"] = "rlp";

// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);
$_GET = clean_str($_GET);

$pbm_rlp = FALSE;
$tab_sites = [];
$tab_sites_autres = [];
$str_rlp = '';
$txtmatos = '';

$tab_rlp_choisis = explode('-', $_SESSION["rlp_choisis"]);

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

if ($action_to_do == 'valid') {
    $lstrlp = "";
    if (count($_POST["rlp_choisi"]) <> 0)
        $lstrlp = implode("-", $_POST["rlp_choisi"]);
    if (count($_POST["rl_choisi"]) <> 0) {
        if ($lstrlp == "")
            $lstrlp = implode("-", $_POST["rl_choisi"]);
        else
            $lstrlp .= "-" . implode("-", $_POST["rl_choisi"]);
    }
    $_SESSION["rlp_choisis"] = $lstrlp;
    $str_rlp = cree_txt_matos_cocl($_SESSION["rlp_choisis"]);
    $str_rlp = str_replace("'", "\'", $str_rlp);
} else {
    // Récupération lieu d'appel
    // tx = "co" => contrat
    // tx = "coa" => annexe contrat
    if (isset($_GET['tx'])) {
        $tx = $_GET['tx'];
        if (($tx <> "co") AND ($tx <> "coa"))
            $pbm_rlp = TRUE;
    } else
        $pbm_rlp = TRUE;

    // Récupération type de demande
    if (isset($_GET['td'])) {
        $td = $_GET['td'];
        if (($td <> "add") AND ($td <> "edit"))
            $pbm_rlp = TRUE;
    } else
        $pbm_rlp = TRUE;

    // Récupération code Client
    if (isset($_GET['cc'])) {
        $cc = $_GET['cc'];
        $row = lit_enr_cli($cc);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);
        } else
            $pbm_rlp = TRUE;
    } else
        $pbm_rlp = TRUE;

    // Récupération code Contrat
    if (isset($_GET['co'])) {
        $co = $_GET['co'];
        $row = lit_enr_cocl($co);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);
        } else
            $pbm_rlp = TRUE;
    } else
        $pbm_rlp = TRUE;

    // Récupération site principal ou pas
    if (isset($_GET['sp'])) {
        $sp = intval($_GET['sp']);
        if (($sp <> 0) AND ($sp <> 1))
            $pbm_rlp = TRUE;
    } else
        $pbm_rlp = TRUE;

    if (isset($_GET['coa'])) {
        $cocla = $_GET['coa']; // Récupération code Annexe Contrat
        $row = lit_enr_cocla($cocla, $co);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);
        } else
            $pbm_rlp = TRUE;
    }

    // Création du tableau des sites
    $lst_sites = $_SESSION["scli_choisis"];
    $lst_sites_autres = $_SESSION["sclia_choisis"];
    if (trim($lst_sites) <> "")
        $tab_sites = explode("-", $lst_sites);
    if (trim($lst_sites_autres) <> "")
        $tab_sites_autres = explode("-", $lst_sites_autres);
}
?>
<!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 Matériels & Licences sur relevés"); ?></title>

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

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

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

<body>
<?PHP
if ($action_to_do == '') // Si le formulaire n'a pas été validé
{
    ?>
    <p align="center"><span class="titrepage"><?PHP echo _("LISTE DES MATERIELS & LICENCES SUR RELEVES"); ?></span></p>
    <p align="center"><span class="annot_petit_it">(<?PHP echo _("En fonction des sites sélectionnés"); ?>)</span></p>
    <p align="center">
        <?PHP
        if ($td == "add")
            echo _("Merci de choisir les matériels et licences que vous souhaitez affecter");
        else
            echo _("Matériels et licences");
        echo ' ';
        if ($tx == 'coa')
            echo _("à l'annexe");
        echo ' ';
        echo _("du contrat");
        echo ' ';
        echo '<span class="annot_std_gras">' . $cocl_code . '</span>';
        echo ' ';
        echo _("pour les sites sélectionnés");
        echo '<div align="center" class="annot_petit_it">' . _("Seuls sont affichés les matériels et licences pas encore choisis dans le contrat et ses annexes") . '</div>';
        ?>
    </p>

    <form name="rlp_lst" method="post" action="rlp_lst.php">
    <?PHP
    echo '<p align="center">';
    echo '<br />';
    echo '<input type="button" name="chx" id="chx" value="Tous" class="bton_fgris_petit">';
    echo '</p>';

    echo '<div align="center" id="tab_lst_rlp">';

    // création de la liste des relevés de matériels actifs
    echo '<div class="annot_petit_it">';
    echo _("Relevés de matériels");
    echo '</div>';

    $tab_rpdt = [];
    switch ($tx) {
        case "co" :
            $tab_rpdt = cre_tab_rpdt_cocl($cocl_code, $cli_code, $sp, $tab_sites);
            break;
        case "coa" :
            $tab_rpdt = cre_tab_rpdt_aselect_coa($co,$cocla_id,$_SESSION["sclia_choisis"], $sp);
            // $tab_rpdt = cre_tab_rpdt_cocla($cocla_id, $cocl_code, $sp, $tab_sites_autres, $tab_sites);
            break;
    }

    if (count($tab_rpdt) > 0) {
        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 matériels"
        echo "<th>";
        echo _("créé le");
        echo "</th>\n";
        echo "<th>";
        echo _("Modifié le");
        echo "</th>\n";
        echo "<th>";
        echo _("détail");
        echo "</th>\n";
        echo "<th>";
        echo _("Choisir");
        echo "</th>\n";

        $j = 0;

        foreach ($tab_rpdt as $key=>$value) {
            $row = lit_enr_rpdt($value);
            if (is_array($row)) {
                $row = encode_str($row);
                // création de l'entête de ligne (TR)
                creetrlstdet($j);

                echo '<td align="center">';
                echo datetolocal($row["rpdt_datecrea"]);
                echo '</td>';
                echo '<td align="center">';
                echo datetolocal($row["rpdt_datemod"]);
                echo '</td>';

                echo '<td>';
                echo '<span class="annot_petit_gras">';
                $nom_site = "";
                switch ($row["rpdt_idsite"]) {
                    case -1 :
                        $nom_site = _("Tous les sites");
                        break;
                    case 0 :
                        $nom_site = _("Site principal");
                        break;
                    default :
                        $row2 = lit_enr_scli_id($row["rpdt_idsite"], TRUE);
                        if (is_array($row2)) {
                            $row2 = encode_str($row2);
                            extract($row2);
                            $nom_site .= $scli_ste;
                            $nom_site .= '<br />' . _("Site") . ' : (' . Retournecp_Id($scli_ville, TRUE) . ' ' . Retourneville_Id($scli_ville, TRUE) . ')';
                        }
                }
                echo $nom_site . '<br />';
                echo '</span>';
                $row2 = lit_enr_mque($row["rpdt_codema"], TRUE);
                if (is_array($row2)) {
                    $row2 = encode_str($row2);
                    extract($row2);
                }
                $txtmatos = _("Relevé") . ' ' . $row["rpdt_code"] . ' : ' . $mque_ste . ' ' . $row["rpdt_modele"] . ' ' . $row["rpdt_version"];

                echo $txtmatos . '<br />';
                echo '</td>';

                echo '<td align="center">';
                echo '<input type="checkbox" name="rlp_choisi[]" id="chkp' . $row["rpdt_id"] . '" value="' . $row["rpdt_code"] . '"';
                if ($j % 2 == 0)
                    echo ' class="TRalter1D"';
                else
                    echo ' class="TRalter2D"';


                if (exist_in_array($row["rpdt_code"], $tab_rlp_choisis))
                    echo " checked";
                echo '>';

                echo "</td>\n";

                echo "</tr>\n";

                $j++;
            }
        }

        echo "</table>\n";
        // Fin affichage table relevés matériels
        if ($j == 0) {
            echo "<span class=\"annot_petit_it\">";
            echo _("Pas de matériel sur les sites sélectionnés") . "<br />";
            echo "</span>";
        }

    } else
        echo _("Aucun relevé de matériel") . "<br />";

    // création de la liste des relevés de licences actifs

    echo '<br />';
    echo '<div class="annot_petit_it">';
    echo _("Relevés de licences");
    echo '</div>';

    $tab_rlic = [];
    switch ($tx) {
        case "co" :
            $tab_rlic = cre_tab_rlic_cocl($cocl_code, $cli_code, $sp, $tab_sites);
            break;
        case "coa" :
            $tab_rlic = cre_tab_rlic_aselect_coa($cocl_code,$cocla_id,$_SESSION["sclia_choisis"], $sp);
            // $tab_rlic = cre_tab_rlic_cocla($cocla_id, $cocl_code, $sp, $tab_sites_autres, $tab_sites);
            break;
    }

    if (count($tab_rlic) > 0) {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

        echo "<table class=\"MtableD\" border=\"0\" width=\"890\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";

        // création de l'entête de la table "relevés de licences"
        echo "<th>";
        echo _("créé le");
        echo "</th>\n";
        echo "<th>";
        echo _("Modifié le");
        echo "</th>\n";
        echo "<th>";
        echo _("détail");
        echo "</th>\n";
        echo "<th>";
        echo _("Choisir");
        echo "</th>\n";

        $j = 0;
        foreach ($tab_rlic as $value) {
            $row = lit_enr_rlic($value, FALSE);
            if (is_array($row)) {
                $row = encode_str($row);
                // création de l'entête de ligne (TR)
                creetrlstdet($j);

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

                echo "<td>\n";
                echo "<span class=\"annot_petit_gras\">";
                $nom_site = "";
                switch ($row["rlic_idsite"]) {
                    case -1 :
                        $nom_site = _("Tous les sites");
                        break;
                    case 0 :
                        $nom_site = _("Site principal");
                        break;
                    default :
                        $rech2 = "SELECT * FROM `" . TBSCLI . "` WHERE `scli_id` = '" . $row["rlic_idsite"] . "'";
                        $row2 = $db->get_row($rech2, ARRAY_A);
                        if ($row2) {
                            $row2 = encode_str($row2);
                            extract($row2);
                            $nom_site .= $scli_ste;
                            $nom_site .= _("Site") . " : (" . Retournecp_Id($scli_ville, FALSE) . " " . Retourneville_Id($scli_ville, FALSE) . ")";
                        }
                }

                echo $nom_site . "<br />";
                echo "</span>";
                $rech2 = "SELECT * FROM `" . TBMQUE . "` WHERE `mque_code` = '" . $row["rlic_codema"] . "'";
                $row2 = $db->get_row($rech2, ARRAY_A);
                if ($row2) {
                    $row2 = encode_str($row2);
                    extract($row2);
                }
                $txtmatos = _("relevé") . " " . $row["rlic_code"] . " : " . $mque_ste . " " . $row["rlic_modele"] . " " . $row["rlic_version"];

                echo $txtmatos . "<br />";
                echo "</td>\n";

                echo "<td align=\"center\">\n";

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

                if (exist_in_array($row["rlic_code"], $tab_rlp_choisis))
                    echo " checked";
                echo '>';

                echo "</td>\n";

                echo "</tr>\n";

                $j++;
            }
        }

        echo "</table>\n";

        $db->disconnect();
        // Fin affichage table relevés licences
        if ($j == 0) {
            echo "<span class=\"annot_petit_it\">";
            echo _("Pas de licences sur les sites sélectionnés") . "<br />";
            echo "</span>";
        }
    } else
        echo _("Aucun relevé de licence") . "<br />";

    echo '</div>';
    ?>
    <p align="center">
        <input name="RAZ" type="reset" class="bton_std" id="RAZ" value="<?PHP echo _("Remise à Zéro"); ?>">
        <input name="val" type="submit" class="bton_std" id="val" value="<?PHP echo _("Valider"); ?>">
        <input name="annul" type="button" class="bton_std" id="annul" value="<?PHP echo _("Annuler"); ?>">
    </p>
    </form>
<?PHP
}
?>
</body>
</html>
