<?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_droits.inc.php");
include_once("../inc/fic_fici.inc.php");
include_once("../inc/fic_pw.inc.php");
include_once("../inc/fic_zoc.inc.php");
include_once("../inc/func.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/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"] = "st";
?>
<!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 sous-traitants"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            init_autohref_classe('falselnkimg','');
            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 _("SOUS-TRAITANTS"); ?></span></p>
<?PHP
if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) {
    $fic_imp = "st_lsti.php";
    $url_add = "st_maj.php?td=add&code=";
} else {
    $fic_imp = "";
    $url_add = "";
}

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

$rech = cree_strrechlstst();
$rech .= crestr_triord($st_titre_col, "st_code", FALSE, $limit);

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

if ($rows) {
    $numrows = count($rows);
    include_once("../inc/formr.inc.php");

    $rech = modstr_triord($rech, $st_titre_col, "st_code", TRUE, $limit);

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

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

    // *** Pagination d'entête
    pagine_lst($numrows, $limit);

    // *** Affichage de la table

    echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";

    // création entête
    titlst($st_titre_col);
    echo "<th>" . _("Cliquer") . "</th>\n";

    $i = 0;

    foreach ($rows as $row) {
        $row = encode_str($row);
        $affok = TRUE;
        $verrou = FALSE;
        if (account_locked($row["age_code"], FALSE) == TRUE) {
            $verrou = TRUE;
            if (@$_SESSION["niv_log"] == 4)
                $affok = FALSE;
        } else {
            if ((@$_SESSION["niv_log"] == 4) AND ($row["st_ouvert"] == FALSE))
                $affok = FALSE;
        }

        if ($affok == TRUE) {
            // création de l'entête de ligne (TR)
            creetrlst($i);

            echo "<td>" . $row["st_code"];

            if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) {
                // CREATION d'icône si manque logo
                $txtalert = "";
                if (exist_logo($row["st_code"]) == FALSE)
                    $txtalert = _("Manque un logo");
                if (trim($txtalert) <> "")
                    echo ' <a href="#" class="popw"><img src="../img/warn.png" alt="'.("pbm").'" border="0" align="absmiddle"><span>' . formatetxtbr($txtalert) . '</span></a>';

                // création icône si agence rattachée
                $rech2 = "SELECT COUNT(*) FROM `" . TBAGE . "` WHERE age_code = '" . $row["st_codeage"] . "'";
                $nbres2 = $db->get_var($rech2);
                if ($nbres2 > 0)
                    echo ' <img src="../img/dp.png" title ="' . _("Egalement agence") . '" border="0" align="absmiddle">';

                // CREATION d'icône si le sous-traitant est fermé
                if ($row["st_ouvert"] == FALSE) {
                    $lst_ferm = TRUE;
                    echo " <img src=\"../img/out.png\" title =\"" . _("Est fermé depuis le ") . datetolocal($row["st_datec"]) . ".\" border=\"0\" align=\"absmiddle\">";
                }

                // Verrouillé ?
                if ($verrou == TRUE)
                    echo ' <img src="../img/locked.png" title="' . _("Compte désactivé") . '" alt="' . _("Compte désactivé") . '" border="0" align="absmiddle">';
            }

            echo "</td>\n";
            echo "<td>";
            echo $row["st_nom"];
            echo "</td>\n";
            echo "<td>" . $row["st_telf"] . "</td>\n";
            echo "<td>" . Retournecp_Id($row["st_ville"], FALSE) . "</td>\n";
            echo "<td>" . Retournepays($row["st_codepays"], FALSE) . "</td>\n";

            echo "<td>";
            $detzone1 = "";
            $detzone2 = "";
            $tab_zone = tab_zoc_st_open($row["st_code"], FALSE);
            foreach ($tab_zone as $idx => $value) {
                $rech2 = "SELECT * FROM `" . TBDEP . "` WHERE dep_code = '$value' AND dep_codepays = '" . $row["st_codepays"] . "'";
                $row2 = $db->get_row($rech2, ARRAY_A);
                if ($row2) {
                    $row2 = encode_str($row2);
                    extract($row2);
                    if ($idx < 5) {
                        if ($idx > 0) {
                            $detzone1 .= "<br />";
                            $detzone2 .= "<br />";
                        }
                        $detzone1 .= $dep_code . " (" . $dep_nom . ")";
                        $detzone2 .= $dep_code . " (" . $dep_nom . ")";
                    } else {
                        $detzone2 .= "<br />" . $dep_code . " (" . $dep_nom . ")";
                    }
                }
            }
            if ($idx >= 5) {
                $detzone1 .= '<div align="right"><a href="#" class="popa"><img src="../img/expand.png" alt="'._("Détail").'" border="0" align="absmiddle" onclick="show_hide(\''.$row["st_code"] . '_z2\',\''.$row["st_code"] . '_z1\')"><span>' . _("Détail") . '</span></a></div>';
                $detzone2 .= '<div align="right"><a href="#" class="popa"><img src="../img/reduce.png" alt="'._("Réduire").'" border="0" align="absmiddle" onclick="show_hide(\''.$row["st_code"] . '_z1\',\''.$row["st_code"] . '_z2\')"><span>' . _("Réduire") . '</span></a></div>';
                echo '<div id="' . $row["st_code"] . '_z1" style="display:block;">';
                echo $detzone1;
                echo "</div>";
                echo '<div id="' . $row["st_code"] . '_z2" style="display:none;">';
                echo $detzone2;
                echo "</div>";
            } else
                echo $detzone1;
            echo "</td>\n";
            echo "<td>";

            // On refabrique, si nécessaire, la liste des spécialités (version 0.9.9x : séparée dans autre fichier)
            if (trim($row["st_spe"]) <> "") {
                $tab_spe = explode("_", $row["st_spe"]);
                foreach ($tab_spe as $key => $value) {
                    $rech2 = "INSERT INTO `" . TBSTSP . "`";
                    $rech2 .= " (stsp_codest,stsp_codesp,stsp_niveau) VALUES";
                    $rech2 .= " ('" . $row["st_code"] . "','$value','0')";
                    $db->query($rech2);

                    $rech2 = "UPDATE `" . TBST . "` SET `st_spe`=NULL";
                    $rech2 .= "  WHERE `st_code` = '" . $row["st_code"] . "'";
                    $db->query($rech2);

                }
            }

            // on affiche la liste des spécialités
            $idx = 0;
            $rech2 = "SELECT * FROM `" . TBSTSP . "` WHERE `stsp_codest` = '" . $row["st_code"] . "'";;
            $rows2 = $db->get_results($rech2, ARRAY_A);
            if ($rows2) {
                foreach ($rows2 as $row2) {
                    $row2 = encode_str($row2);
                    extract($row2);
                    $rech3 = "SELECT * FROM `" . TBSP . "` WHERE `sp_code` = '" . $stsp_codesp . "'";;
                    $row3 = $db->get_row($rech3, ARRAY_A);
                    if ($row3) {
                        $row3 = encode_str($row3);
                        extract($row3);
                        if ($idx > 0) echo "<br />";
                        echo $sp_code . " (" . $sp_desc . ") - ";
                        echo $spec_niveau[$stsp_niveau];
                    }
                    $idx++;
                }
            }

            echo "</td>\n";

            // CREATION lien dynamique sur email
            echo "<td>";
            echo "<a href=\"mailto:" . $row["st_email"] . "\">" . $row["st_email"] . "</a>";
            echo "</td>\n";

            // CREATION lien dynamique sur icône (édition, suppression, etc...)
            echo "<td align=\"center\">";
            if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) {
                if ($row["st_ouvert"] == TRUE) {
                    $lst_maj = TRUE;
                    cre_falselnk_img('', 'st_edit_'.$row["st_id"], 'span', 'center', _("Edition"), 'lnk', 'st_maj.php?td=edit&code=' . $row["st_code"] . '&dep=' . $row["st_dep"], '../img/edit.png');
                    $lst_del = TRUE;
                    cre_falselnk_img('', 'st_del_'.$row["st_id"], 'span', 'center', _("Suppression"), 'lnk', 'st_maj.php?td=del&code=' . $row["st_code"] . '&dep=' . $row["st_dep"], '../img/del.png');
                } else {
                    $url = 'st_maj.php?td=view&code=' . $row["st_code"] . '&dep=' . $row["st_dep"];
                    if (url_ok($url, FALSE) == TRUE) {
                        $lst_view = TRUE;
                        cre_falselnk_img('', 'st_view_'.$row["st_id"], 'span', 'center', _("Visualisation"), 'lnk', 'st_maj.php?td=view&code=' . $row["st_code"] . '&dep=' . $row["st_dep"], '../img/eye.png');
                    }
                }
            } else {
                $url = 'st_maj.php?td=view&code=' . $row["st_code"] . '&dep=' . $row["st_dep"];
                if (url_ok($url, FALSE) == TRUE) {
                    $lst_view = TRUE;
                    cre_falselnk_img('', 'st_view_'.$row["st_id"], 'span', 'center', _("Visualisation"), 'lnk', $url, '../img/eye.png');
                }
            }
            $url = 'coloc_maj.php?td=add&tc=3&co=&cc=' . $row["st_code"] . '&ca=';
            if (url_ok($url, FALSE) == TRUE)
            {
                $lst_coloc = TRUE;
                cre_falselnk_img('', 'coloc_add_'.$row["st_id"], 'span', 'center', _("Ajouter contrat de location"), 'lnk', $url, '../img/coloc.png');
            }
            if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) {
                $urlimp = "st_maji.php?cs=" . $row["st_code"];
                $lst_imp = TRUE;
                cre_falselnk_img('', 'st_prn'.$row["st_id"], 'span', 'center', _("Impression"), 'lnk', $urlimp, '../img/print.png');
            }
            echo "</td>\n";

            echo "</tr>\n";

            $i++;
        }
    }
    echo "</table>\n";
    // *** Fin affichage de la table

    $db->disconnect();

    if ($locked == TRUE) {
        echo ' <img src="../img/locked.png" title="' . _("Compte désactivé") . '" alt="' . _("Compte désactivé") . '" border="0" align="absmiddle">';
        echo " : " . _("Compte désactivé");
    }

    // *** Pagination de pied
    pagine_lst($numrows, $limit);

    $taille_piedla = "";
    include_once("../inc/piedla.inc.php");
} else
    retpp();

include_once("pied.php")
?>
