<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.2
 *
 * Copyright (C) 2006-2015  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/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/fic_age.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_tac.inc.php");
include_once("../inc/fic_tec.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"] = "int_syn";

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

$ts = '';
$codecli = '';
$date1 = '';
$date2 = '';
$pbm_intsyn = FALSE;

// Récupération type de synthèse
if (isset($_GET['ts']))
    $ts = intval($_GET['ts']);

switch ($ts) {
    // Récupération des informations clients
    case 1 :
        $codecli = '';
        if (isset($_GET['cli']))
            $codecli = $_GET['cli'];
        $row = lit_enr_cli($codecli, TRUE);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);

            switch (@$_SESSION["niv_log"]) {
                case 3 :
                case 4 :
                    if (client_age_ok($cli_code, @$_SESSION["code_age"], TRUE) == FALSE)
                        $pbm_intsyn = TRUE;
                    break;
                default :
                    break;
            }
        }
        break;

    // Récupération des informations agences
    case 2 :
    case 3 :
    case 4 :
        $codeage = '';
        if (isset($_GET['age']))
            $codeage = $_GET['age'];
        $row = lit_enr_age($codeage, TRUE);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);

            switch (@$_SESSION["niv_log"]) {
                case 3 :
                case 4 :
                    if ($age_code <> $_SESSION["code_age"])
                        $pbm_intsyn = TRUE;
                    break;
                default :
                    break;
            }
        }
        break;

    // Récupération des informations technicien
    case 5 :
    case 6 :
    case 7 :
        if (isset($_GET['tec']))
            $codetec = $_GET['tec'];
        $row = lit_enr_tec($codetec, TRUE);
        if (is_array($row)) {
            $row = encode_str($row);
            extract($row);

            switch (@$_SESSION["niv_log"]) {
                case 3 :
                    if ($tec_code <> $_SESSION["code_log"])
                        $pbm_intsyn = TRUE;
                    break;
                case 4 :
                    if ($tec_codeage <> $_SESSION["code_age"])
                        $pbm_intsyn = TRUE;
                    break;
                default :
                    break;
            }
        }
        break;

    default :
        $pbm_intsyn = TRUE;
        break;
}

if ($pbm_intsyn == FALSE) {
    // Récupération fourchette des dates
    $date1 = "";
    $date2 = "";
    if (isset($_GET["date1f"])) {
        if (@$_GET["date1f"] <> "") {
            $date1f = @$_GET["date1f"];
            $date1 = datetomysql($date1f);
        }
    }
    if (isset($_GET["date2f"])) {
        if (@$_GET["date2f"] <> "") {
            $date2f = @$_GET["date2f"];
            $date2 = datetomysql($date2f);
        }
    }
}

?>
<!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 _("Synthèse Interventions"); ?></title>

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

    <?PHP if ($pbm_intsyn == FALSE) { ?>
        <script type="text/javascript">
            <!--
            function init_all() {
                init_Body();
                warn_print(1);
            }

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

    <?PHP } ?>
</head>

<?php
if ($pbm_intsyn == FALSE) {
    echo '<body>';
    switch ($ts) {
        // Synthèse des interventions, pour un client, sur une période donnée
        case 1 :
            entimplst(_("SYNTHESE SUR UNE PERIODE DONNEE"), TRUE);
            // Affichage détail client
            $detailcli = "";
            if ($cli_ste <> "")
                $detailcli .= $cli_ste . ", ";
            $detailcli .= $cli_civilite . " " . $cli_nom;
            $detailcli .= " (" . $codecli . ")";
            echo '<div class="annot_gras_it" align="center">';
            echo $detailcli;
            echo '</div>';

            echo '<div class="annot_petit_it" align="center">';
            echo _("Du")." " . datetolocal($date1) . " "._("au")." " . datetolocal($date2);
            echo '</div>';
            echo "<br />";

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

            $aff_sfact = FALSE;
            $rech = cree_strrechlstints($_SESSION["niv_log"]);

            switch ($_SESSION["niv_log"]) {
                // Agences
                case 4 :
                    if (!stristr($rech, "WHERE "))
                        $rech .= " WHERE (" . TBINT . ".int_codeage='" . @$_SESSION["code_age"] . "' OR " . TBINT . ".int_codeages='" . @$_SESSION["code_age"] . "'";
                    else
                        $rech .= " AND (" . TBINT . ".int_codeage='" . @$_SESSION["code_age"] . "' OR " . TBINT . ".int_codeages='" . @$_SESSION["code_age"] . "'";

                    // On rajoute les interventions non affectées à une agence
                    $rech .= " OR " . TBINT . ".int_codeage='')";
                    $row = lit_enr_age($_SESSION["code_age"], FALSE);
                    if (is_array($row)) {
                        extract($row);
                        if (intval($age_fact) == 1)
                            $aff_sfact = TRUE;
                    }
                    break;

                default :
                    $aff_sfact = TRUE;
                    break;
            }
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE " . TBINT . ".int_datedde>='$date1' AND " . TBINT . ".int_datedde<='$date2'";
            else
                $rech .= " AND " . TBINT . ".int_datedde>='$date1' AND " . TBINT . ".int_datedde<='$date2'";
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE " . TBINT . ".int_codecli='$codecli'";
            else
                $rech .= " AND " . TBINT . ".int_codecli='$codecli'";
            $rech .= " ORDER BY int_code";

            $rows = $db->get_results($rech, ARRAY_A);
            $numrows = count($rows); // Nombre de résultats à la requête

            if ($numrows >0)
            {
                // *** Affichage de la table
                echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";
                titlst($ints_titre_col);

                $i = 0;

                foreach ($rows as $row) {
                    $row = encode_str($row);
                    $aff_ligne = TRUE;

                    // Au cas où l'intervention n'est pas encore affectée et que l'on est en mode technicien agence ou agence,
                    // il faut vérifier si le client fait partie de la zone de chalandise de l'agence
                    if ((($_SESSION["niv_log"] == 3) OR ($_SESSION["niv_log"] == 4)) AND (trim($row["int_codeage"]) == "")) {
                        if (trim($row["cli_prop"]) == "") {
                            $rech2 = "SELECT COUNT(*) FROM `" . TBZCC . "` WHERE `zcc_code` = '" . $_SESSION["code_age"] . "' AND `zcc_codepays` = '" . $row["cli_codepays"] . "' AND `zcc_codedep` = '" . $row["cli_dep"] . "'";
                            $nbres2 = $db->get_var($rech2);
                            if ($nbres2 == 0)
                                $aff_ligne = FALSE;
                        } else {
                            if (trim($row["cli_prop"]) <> $_SESSION["code_age"])
                                $aff_ligne = FALSE;
                        }
                    }

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

                        echo "<td>";
                        echo $row["int_code"];
                        $detfic = "<br />>>"._("Matériel")." :<br />" . formatetxtbr($row["int_mat"]);
                        $detfic .= "<br /><br />>>"._("Travail à effectuer")." :<br />" . formatetxtbr($row["int_pbm"]);
                        $detfic .= "<br /><br />>>"._("Effectué")." :<br />" . formatetxtbr($row["int_fait"]);
                        if ($detfic <> "")
                            echo $detfic;


                        if ((datetolocal($row["int_dateprev"]) <> "") AND (datetolocal($row["int_datefin"]) == "")) {
                            echo "<div class=\"annot_petit_it\" align=\"right\">";
                            echo _("Client prévenu le")." " . datetolocal($row["int_dateprev"]) . "\"";
                            echo "</div>";
                        }
                        echo "</td>\n";

                        // Catégorie
                        echo "<td align=\"center\">";
                        $cat = $row["int_codecat"];
                        $rech2 = "SELECT * FROM `" . TBINTC . "` WHERE intc_code = '$cat'";

                        $row2 = $db->get_row($rech2, ARRAY_A);

                        if ($row2) {
                            $row2 = encode_str($row2);
                            extract($row2);

                            if ($intc_img <> "")
                                $catint = $intc_desc;
                            else
                                $catint = $cat;
                            echo "<span class=\"annot_std_it\">";
                            echo $catint;
                            echo "</span>";
                        }
                        echo "</td>\n";

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

                        // RDV
                        echo "<td align=\"center\">";
                        if (datetolocal($row["int_daterdv"]) <> "") {
                            echo datetolocal($row["int_daterdv"]);
                            if ($row["int_hrdv"] <> "00:00:00") {
                                echo "<div align=\"center\" class=\"annot_petit_it\">";
                                echo substr($row["int_hrdv"], 0, 5);
                                echo "</div>";
                            }
                        } else
                            echo "&nbsp;";
                        echo "</td>\n";

                        // Affichage icône Lieu
                        echo "<td>";
                        echo "<div align=\"center\">";

                        $lieu = $row["int_codelieu"];
                        $rech3 = "SELECT * FROM `" . TBLIEU . "` WHERE lieu_code = '$lieu'";

                        $row3 = $db->get_row($rech3, ARRAY_A);

                        if ($row3) {
                            $row3 = encode_str($row3);
                            extract($row3);
                            if ($lieu_img <> "")
                                $lieuint = $lieu_desc;
                            else
                                $lieuint = $lieu;
                            echo "<span class=\"annot_std_it\">";
                            echo $lieuint;
                            echo "</span>";
                        }

                        echo "</div>";
                        echo "</td>\n";

                        // Affichage détail urgence
                        echo '<td ';
                        $couleur = give_coul_urg($row["int_urg"], FALSE);
                        if ($couleur <> "XX")
                            echo ' style="background:' . $couleur . '"';
                        echo ' align="center">';
                        echo give_nom_urg($row["int_urg"], FALSE);;
                        echo "</td>\n";
                        // Fin affichage détail urgence

                        echo "<td>";

                        // Affichage site client
                        $sitecli = "";
                        switch (trim($row["int_idsite"])) {
                            case "0" :
                                $sitecli = "Site principal";
                                break;
                            default :
                                $rech2 = "SELECT * FROM `" . TBSCLI . "` WHERE `scli_id` = '" . trim($row["int_idsite"]) . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2) {
                                    $row = encode_str($row);
                                    extract($row2);
                                    $sitecli .= $scli_ste . "<br />";
                                    $sitecli .= $scli_adr1 . "<br />";
                                    if ($scli_adr2 <> "")
                                        $sitecli .= $scli_adr2 . "<br />";
                                    $sitecli .= Retournecp_Id($scli_ville, FALSE) . " " . Retourneville_Id($scli_ville, FALSE) . "<br />";
                                }
                        }
                        if ($sitecli <> "") {
                            echo "<div class=\"annot_petit_it\" style=\"margin-left:7px;\">";
                            echo $sitecli;
                            echo "</div>";
                        }

                        // Fin affichage détail client
                        echo "</td>\n";

                        // Technicien / Agence traitants
                        echo "<td align=\"center\">";
                        if (($row["int_codetec"] <> "") AND ($row["int_codetec"] <> "-1")) {
                            echo $row["int_codetec"];
                            $row2 = lit_enr_tec($row["int_codetec"], FALSE);
                            if (is_array($row2)) {
                                $row2 = encode_str($row2);
                                extract($row2);
                                echo "<div class=\"annot_petit_it\">";
                                echo "(" . $tec_civilite . " " . $tec_prenom . " " . $tec_nom . " - " . $tec_codeage . ")";
                                echo "</div>";
                            }
                        } else
                            echo "-";
                        echo "</td>\n";

                        // Affichage icône Statut
                        echo "<td align=\"center\">";

                        $statut = $row["int_codestatut"];
                        $rech4 = "SELECT * FROM `" . TBSTA . "` WHERE statut_code = '$statut'";

                        $row4 = $db->get_row($rech4, ARRAY_A);

                        if ($row4) {
                            $row4 = encode_str($row4);
                            extract($row4);
                            $url_statut = "ints_maj.php?ci=" . $row["int_code"];
                            if ($statut_img <> "")
                                $statint = $statut_desc;
                            else
                                $statint = $statut;
                            echo '<span class="annot_std_it">';
                            echo $statint;
                            echo '</span>';
                        }

                        if (datetolocal($row["int_datefin"]) <> "") {
                            echo '<div class="annot_upetit" align="center">';
                            echo _("Le").' ' . datetolocal($row["int_datefin"]);
                            echo '</div>';
                        }

                        echo "</td>\n";

                        echo "</div>";
                        echo "</td>\n";

                        echo "</tr>\n";
                        // Fin affichage résumé intervention

                        $i++;
                    }
                }
                echo "</table>\n";
                // *** Fin affichage de la table
            } else {
                echo '<div class="annot_gras_it" align="center">';
                echo _("Aucun résultat");
                echo '</div>';
            }

            $db->disconnect();
            break;

        // Synthèse des interventions, pour une agence, sur une période donnée
        case 2 :
            // Synthèse des interventions, pour une agence gestionnaire, sur une période donnée
        case 3 :
            // Synthèse des interventions, pour une agence responsable de site, sur une période donnée
        case 4 :
            entimplst(_("SYNTHESE SUR UNE PERIODE DONNEE"), TRUE);
            // Affichage détail agence
            $detailage = "";
            $detailage .= $age_nom . ", ";
            $detailage .= " (" . $codeage . ")";
            switch ($ts) {
                case 2 :
                    $detailage .= " - "._("Gestionnaire ou responsable de site d'intervention");
                    break;
                case 3 :
                    $detailage .= " - "._("Gestionnaire d'intervention");
                    break;
                case 4 :
                    $detailage .= " - "._("Responsable de site d'intervention");
                    break;
            }
            echo '<div class="annot_gras_it" align="center">';
            echo $detailage;
            echo '</div>';

            echo '<div class="annot_petit_it" align="center">';
            echo _("Du")." " . datetolocal($date1) . " "._("au")." " . datetolocal($date2);
            echo '</div>';
            echo "<br />";

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

            $aff_sfact = FALSE;
            $rech = cree_strrechlstints($_SESSION["niv_log"]);

            switch ($_SESSION["niv_log"]) {
                // Agences
                case 4 :
                    if (!stristr($rech, "WHERE "))
                        $rech .= " WHERE (" . TBINT . ".int_codeage='" . @$_SESSION["code_age"] . "' OR " . TBINT . ".int_codeages='" . @$_SESSION["code_age"] . "'";
                    else
                        $rech .= " AND (" . TBINT . ".int_codeage='" . @$_SESSION["code_age"] . "' OR " . TBINT . ".int_codeages='" . @$_SESSION["code_age"] . "'";

                    $row = lit_enr_age($_SESSION["code_age"], FALSE);
                    if (is_array($row)) {
                        extract($row);
                        if (intval($age_fact) == 1)
                            $aff_sfact = TRUE;
                    }
                    break;

                default :
                    $aff_sfact = TRUE;
                    break;
            }
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE " . TBINT . ".int_datedde>='$date1' AND " . TBINT . ".int_datedde<='$date2'";
            else
                $rech .= " AND " . TBINT . ".int_datedde>='$date1' AND " . TBINT . ".int_datedde<='$date2'";
            switch ($ts) {
                // Synthèse des interventions, pour une agence, sur une période donnée
                case 2 :
                    if (!stristr($rech, "WHERE "))
                        $rech .= " WHERE (" . TBINT . ".int_codeage='$codeage' OR " . TBINT . ".int_codeages='$codeage')";
                    else
                        $rech .= " AND (" . TBINT . ".int_codeage='$codeage' OR " . TBINT . ".int_codeages='$codeage')";
                    break;
                // Synthèse des interventions, pour une agence gestionnaire, sur une période donnée
                case 3 :
                    if (!stristr($rech, "WHERE "))
                        $rech .= " WHERE " . TBINT . ".int_codeage='$codeage'";
                    else
                        $rech .= " AND " . TBINT . ".int_codeage='$codeage'";
                    break;
                // Synthèse des interventions, pour une agence responsable de site, sur une période donnée
                case 4 :
                    if (!stristr($rech, "WHERE "))
                        $rech .= " WHERE " . TBINT . ".int_codeages='$codeage'";
                    else
                        $rech .= " AND " . TBINT . ".int_codeages='$codeage'";
                    break;
            }

            $rows = $db->get_results($rech, ARRAY_A);
            $numrows = count($rows); // Nombre de résultats à la requête

            if ($numrows >0)
            {
                // *** Affichage de la table
                echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";
                titlst($ints_titre_col);

                $i = 0;

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

                    echo "<td>";
                    echo $row["int_code"];
                    $detfic = "<br />>>"._("Matériel")." :<br />" . formatetxtbr($row["int_mat"]);
                    $detfic .= "<br /><br />>>Travail à effectuer :<br />" . formatetxtbr($row["int_pbm"]);
                    $detfic .= "<br /><br />>>"._("Effectué")." :<br />" . formatetxtbr($row["int_fait"]);
                    if ($detfic <> "")
                        echo $detfic;


                    if ((datetolocal($row["int_dateprev"]) <> "") AND (datetolocal($row["int_datefin"]) == "")) {
                        echo "<div class=\"annot_petit_it\" align=\"right\">";
                        echo _("Client prévenu le")." " . datetolocal($row["int_dateprev"]) . "\"";
                        echo "</div>";
                    }
                    echo "</td>\n";

                    // Catégorie
                    echo "<td align=\"center\">";
                    $cat = $row["int_codecat"];
                    $rech5 = "SELECT * FROM `" . TBINTC . "` WHERE intc_code = '$cat'";

                    $row5 = $db->get_row($rech5, ARRAY_A);

                    if ($row5) {
                        $row5 = encode_str($row5);
                        extract($row5);

                        if ($intc_img <> "")
                            $catint = $intc_desc;
                        else
                            $catint = $cat;
                        echo "<span class=\"annot_std_it\">";
                        echo $catint;
                        echo "</span>";
                    }
                    echo "</td>\n";

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

                    // RDV
                    echo "<td align=\"center\">";
                    if (datetolocal($row["int_daterdv"]) <> "") {
                        echo datetolocal($row["int_daterdv"]);
                        if ($row["int_hrdv"] <> "00:00:00") {
                            echo "<div align=\"center\" class=\"annot_petit_it\">";
                            echo substr($row["int_hrdv"], 0, 5);
                            echo "</div>";
                        }
                    } else
                        echo "&nbsp;";
                    echo "</td>\n";

                    // Affichage icône Lieu
                    echo "<td>";
                    echo "<div align=\"center\">";

                    $lieu = $row["int_codelieu"];
                    $rech6 = "SELECT * FROM `" . TBLIEU . "` WHERE lieu_code = '$lieu'";

                    $row6 = $db->get_row($rech6, ARRAY_A);

                    if ($row6) {
                        $row6 = encode_str($row6);
                        extract($row6);
                        if ($lieu_img <> "")
                            $lieuint = $lieu_desc;
                        else
                            $lieuint = $lieu;
                        echo "<span class=\"annot_std_it\">";
                        echo $lieuint;
                        echo "</span>";
                    }

                    echo "</div>";
                    echo "</td>\n";

                    // Affichage détail urgence
                    echo '<td ';
                    $couleur = give_coul_urg($row["int_urg"], FALSE);
                    if ($couleur <> "XX")
                        echo ' style="background:' . $couleur . '"';
                    echo ' align="center">';
                    echo give_nom_urg($row["int_urg"], FALSE);;
                    echo "</td>\n";
                    // Fin affichage détail urgence

                    echo "<td>";

                    // Affichage site client
                    $sitecli = "";
                    switch (trim($row["int_idsite"])) {
                        case "0" :
                            $sitecli = _("Site principal");
                            break;
                        default :
                            $rech2 = "SELECT * FROM `" . TBSCLI . "` WHERE `scli_id` = '" . trim($row["int_idsite"]) . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2) {
                                $row2 = encode_str($row2);
                                extract($row2);
                                $sitecli .= $scli_ste . "<br />";
                                $sitecli .= $scli_adr1 . "<br />";
                                if ($scli_adr2 <> "")
                                    $sitecli .= $scli_adr2 . "<br />";
                                $sitecli .= Retournecp_Id($scli_ville, FALSE) . " " . Retourneville_Id($scli_ville, FALSE) . "<br />";
                            }
                    }
                    if ($sitecli <> "") {
                        echo "<div class=\"annot_petit_it\" style=\"margin-left:7px;\">";
                        echo $sitecli;
                        echo "</div>";
                    }

                    // Fin affichage détail client
                    echo "</td>\n";

                    // Technicien / Agence traitants
                    echo "<td align=\"center\">";
                    if (($row["int_codetec"] <> "") AND ($row["int_codetec"] <> "-1")) {
                        echo $row["int_codetec"];
                        $row2 = lit_enr_tec($row["int_codetec"], FALSE);
                        if (is_array($row2)) {
                            $row2 = encode_str($row2);
                            extract($row2);
                            echo "<div class=\"annot_petit_it\">";
                            echo "(" . $tec_civilite . " " . $tec_prenom . " " . $tec_nom . " - " . $tec_codeage . ")";
                            echo "</div>";
                        }
                    } else
                        echo "-";
                    echo "</td>\n";

                    // Affichage icône Statut
                    echo "<td align=\"center\">";

                    $statut = $row["int_codestatut"];
                    $rech3 = "SELECT * FROM `" . TBSTA . "` WHERE statut_code = '$statut'";

                    $row3 = $db->get_row($rech3, ARRAY_A);

                    if ($row3) {
                        $row3 = encode_str($row3);
                        extract($row3);
                        $url_statut = "ints_maj.php?ci=" . $row["int_code"];
                        if ($statut_img <> "")
                            $statint = $statut_desc;
                        else
                            $statint = $statut;
                        echo '<span class="annot_std_it">';
                        echo $statint;
                        echo '</span>';
                    }

                    if (datetolocal($row["int_datefin"]) <> "") {
                        echo '<div class="annot_upetit" align="center">';
                        echo _("Le").' ' . datetolocal($row["int_datefin"]);
                        echo '</div>';
                    }

                    echo "</td>\n";

                    echo "</div>";
                    echo "</td>\n";

                    echo "</tr>\n";
                    // Fin affichage résumé intervention

                    $i++;
                }
                echo "</table>\n";
                // *** Fin affichage de la table
            } else {
                echo '<div class="annot_gras_it" align="center">';
                echo _("Aucun résultat");
                echo '</div>';
            }

            $db->disconnect();
            break;

        // Synthèse des interventions, pour un technicien, sur une période donnée
        case 5 :
            // Synthèse des interventions, pour un technicien responsable, sur une période donnée
        case 6 :
            // Synthèse des interventions, pour un technicien intervenant, sur une période donnée
        case 7 :
            entimplst(_("SYNTHESE SUR UNE PERIODE DONNEE"), TRUE);
            // Affichage détail technicien
            $detailtec = "";
            $detailtec .= $tec_nom . " " . $tec_prenom . ", ";
            $detailtec .= " (" . $codetec . " - " . $tec_codeage . ")";
            switch ($ts) {
                case 5 :
                    $detailtec .= " - "._("Responsable ou intervenant");
                    break;
                case 6 :
                    $detailtec .= " - "._("Responsable d'intervention");
                    break;
                case 7 :
                    $detailtec .= " - "._("Intervenant");
                    break;
            }
            echo '<div class="annot_gras_it" align="center">';
            echo $detailtec;
            echo '</div>';

            echo '<div class="annot_petit_it" align="center">';
            echo _("Du")." " . datetolocal($date1) . " "._("au")." " . datetolocal($date2);
            echo '</div>';
            echo "<br />";

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

            // On fabrique le tableau des interventions
            $tab_int = [];

            if (($ts == 5) OR ($ts == 7)) {
                //...A partir des historiques interventions
                $rech = "SELECT DISTINCT inth_codeint FROM `" . TBINTH . "` WHERE inth_codetec='" . $codetec . "'";
                $rows = $db->get_results($rech, ARRAY_A);
                if ($rows) {
                    foreach ($rows as $row) {
                        extract($row);
                        $tab_int[] = $inth_codeint;
                    }
                }
            }

            if (($ts == 5) OR ($ts == 6)) {
                //...On rajoute les interventions gérées et non traitées
                $rech = "SELECT int_code FROM `" . TBINT . "` WHERE (";
                $rech .= " int_codetec='" . $codetec . "'";
                if (count($tab_int) > 0) {
                    $rech .= " AND";
                    foreach ($tab_int as $codeint) {
                        $rech .= " int_code<>'" . $codeint . "' AND";
                    }
                    // Suppression du dernier 'AND'
                    $rech = substr($rech, 0, strlen($rech) - 4);
                }
                $rech .= ")";

                $rows = $db->get_results($rech, ARRAY_A);
                if ($rows) {
                    foreach ($rows as $row) {
                        extract($row);
                        $tab_int[] = $int_code;
                    }
                }
            }

            // On filtre sur les dates
            if (count($tab_int) > 0) {
                $rech = "SELECT int_code FROM `" . TBINT . "` WHERE (";
                foreach ($tab_int as $codeint) {
                    $rech .= " int_code ='" . $codeint . "' OR";
                }
                // Suppression du dernier 'OR'
                $rech = substr($rech, 0, strlen($rech) - 3);
                $rech.=")";
                $rech .= " AND " . TBINT . ".int_datedde>='$date1' AND " . TBINT . ".int_datedde<='$date2'";
                $rows = $db->get_results($rech, ARRAY_A);
                if ($rows) {
                    foreach ($rows as $row) {
                        extract($row);
                        $tab_int[] = $int_code;
                    }
                }
            }

            if (count($tab_int) > 0) {
                $aff_sfact = FALSE;
                $rech = cree_strrechlstints($_SESSION["niv_log"]);

                if (!stristr($rech, "WHERE "))
                    $rech .= " WHERE (";
                else
                    $rech .= " AND (";
                foreach ($tab_int as $codeint) {
                    $rech .= " int_code='" . $codeint . "' OR";
                }
                // Suppression du dernier 'OR'
                $rech = substr($rech, 0, strlen($rech) - 3);
                $rech .= ")";

                switch ($_SESSION["niv_log"]) {
                    // Agences
                    case 4 :
                        if (!stristr($rech, "WHERE "))
                            $rech .= " WHERE (" . TBINT . ".int_codeage='" . @$_SESSION["code_age"] . "' OR " . TBINT . ".int_codeages='" . @$_SESSION["code_age"] . "'";
                        else
                            $rech .= " AND (" . TBINT . ".int_codeage='" . @$_SESSION["code_age"] . "' OR " . TBINT . ".int_codeages='" . @$_SESSION["code_age"] . "'";

                        $row = lit_enr_age($_SESSION["code_age"], FALSE);
                        if (is_array($row)) {
                            extract($row);
                            if (intval($age_fact) == 1)
                                $aff_sfact = TRUE;
                        }
                        break;

                    default :
                        $aff_sfact = TRUE;
                        break;
                }

                $rech .= " ORDER BY int_code";

                $rows = $db->get_results($rech, ARRAY_A);
                $numrows = count($rows); // Nombre de résultats à la requête

                if ($numrows>0)
                {
                    // *** Affichage de la table
                    echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";
                    titlst($ints_titre_col);

                    $i = 0;

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

                        echo "<td>";
                        echo $row["int_code"];
                        $detfic = "<br />>>"._("Matériel")." :<br />" . formatetxtbr($row["int_mat"]);
                        $detfic .= "<br /><br />>>"._("Travail à effectuer")." :<br />" . formatetxtbr($row["int_pbm"]);
                        $detfic .= "<br /><br />>>"._("Effectué")." :<br />" . formatetxtbr($row["int_fait"]);
                        if ($detfic <> "")
                            echo $detfic;


                        if ((datetolocal($row["int_dateprev"]) <> "") AND (datetolocal($row["int_datefin"]) == "")) {
                            echo "<div class=\"annot_petit_it\" align=\"right\">";
                            echo _("Client prévenu le")." " . datetolocal($row["int_dateprev"]) . "\"";
                            echo "</div>";
                        }
                        echo "</td>\n";

                        // Catégorie
                        echo "<td align=\"center\">";
                        $cat = $row["int_codecat"];
                        $rech2 = "SELECT * FROM `" . TBINTC . "` WHERE intc_code = '$cat'";

                        $row2 = $db->get_row($rech2, ARRAY_A);

                        if ($row2) {
                            $row2 = encode_str($row2);
                            extract($row2);

                            if ($intc_img <> "")
                                $catint = $intc_desc;
                            else
                                $catint = $cat;
                            echo "<span class=\"annot_std_it\">";
                            echo $catint;
                            echo "</span>";
                        }
                        echo "</td>\n";

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

                        // RDV
                        echo "<td align=\"center\">";
                        if (datetolocal($row["int_daterdv"]) <> "") {
                            echo datetolocal($row["int_daterdv"]);
                            if ($row["int_hrdv"] <> "00:00:00") {
                                echo "<div align=\"center\" class=\"annot_petit_it\">";
                                echo substr($row["int_hrdv"], 0, 5);
                                echo "</div>";
                            }
                        } else
                            echo "&nbsp;";
                        echo "</td>\n";

                        // Affichage icône Lieu
                        echo "<td>";
                        echo "<div align=\"center\">";

                        $lieu = $row["int_codelieu"];
                        $rech3 = "SELECT * FROM `" . TBLIEU . "` WHERE lieu_code = '$lieu'";

                        $row3 = $db->get_row($rech3, ARRAY_A);

                        if ($row3) {
                            $row3 = encode_str($row3);
                            extract($row3);
                            if ($lieu_img <> "")
                                $lieuint = $lieu_desc;
                            else
                                $lieuint = $lieu;
                            echo "<span class=\"annot_std_it\">";
                            echo $lieuint;
                            echo "</span>";
                        }

                        echo "</div>";
                        echo "</td>\n";

                        // Affichage détail urgence
                        echo '<td ';
                        $couleur = give_coul_urg($row["int_urg"], FALSE);
                        if ($couleur <> "XX")
                            echo ' style="background:' . $couleur . '"';
                        echo ' align="center">';
                        echo give_nom_urg($row["int_urg"], FALSE);;
                        echo "</td>\n";
                        // Fin affichage détail urgence

                        echo "<td>";

                        // Affichage site client
                        $sitecli = "";
                        switch (trim($row["int_idsite"])) {
                            case "0" :
                                $sitecli = _("Site principal");
                                break;
                            default :
                                $rech2 = "SELECT * FROM `" . TBSCLI . "` WHERE `scli_id` = '" . trim($row["int_idsite"]) . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2) {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                    $sitecli .= $scli_ste . "<br />";
                                    $sitecli .= $scli_adr1 . "<br />";
                                    if ($scli_adr2 <> "")
                                        $sitecli .= $scli_adr2 . "<br />";
                                    $sitecli .= Retournecp_Id($scli_ville, FALSE) . " " . Retourneville_Id($scli_ville, FALSE) . "<br />";
                                }
                        }
                        if ($sitecli <> "") {
                            echo "<div class=\"annot_petit_it\" style=\"margin-left:7px;\">";
                            echo $sitecli;
                            echo "</div>";
                        }

                        // Fin affichage détail client
                        echo "</td>\n";

                        // Technicien / Agence traitants
                        echo "<td align=\"center\">";
                        if (($row["int_codetec"] <> "") AND ($row["int_codetec"] <> "-1")) {
                            echo $row["int_codetec"];
                            $row2 = lit_enr_tec($row["int_codetec"], FALSE);
                            if (is_array($row2)) {
                                $row2 = encode_str($row2);
                                extract($row2);
                                echo "<div class=\"annot_petit_it\">";
                                echo "(" . $tec_civilite . " " . $tec_prenom . " " . $tec_nom . " - " . $tec_codeage . ")";
                                echo "</div>";
                            }
                        } else
                            echo "-";
                        echo "</td>\n";

                        // Affichage icône Statut
                        echo "<td align=\"center\">";

                        $statut = $row["int_codestatut"];
                        $rech4 = "SELECT * FROM `" . TBSTA . "` WHERE statut_code = '$statut'";

                        $row4 = $db->get_row($rech4, ARRAY_A);

                        if ($row4) {
                            $row4= encode_str($row4);
                            extract($row4);
                            $url_statut = "ints_maj.php?ci=" . $row["int_code"];
                            if ($statut_img <> "")
                                $statint = $statut_desc;
                            else
                                $statint = $statut;
                            echo '<span class="annot_std_it">';
                            echo $statint;
                            echo '</span>';
                        }
                        if (datetolocal($row["int_datefin"]) <> "") {
                            echo '<div class="annot_upetit" align="center">';
                            echo 'Le ' . datetolocal($row["int_datefin"]);
                            echo '</div>';
                        }

                        echo "</td>\n";

                        echo "</div>";
                        echo "</td>\n";

                        echo "</tr>\n";
                        // Fin affichage résumé intervention

                        $i++;
                    }
                    echo "</table>\n";
                    // *** Fin affichage de la table
                } else {
                    echo '<div class="annot_gras_it" align="center">';
                    echo _("Aucun résultat");
                    echo '</div>';
                }
            } else {
                echo '<div class="annot_gras_it" align="center">';
                echo _("Aucun résultat");
                echo '</div>';
            }

            $db->disconnect();
            break;
    }
} else
    header("Location: ../index.php");
?>
