<?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");

/*********************************************************/
/* Diverses fonctions pour les listes affichées dans SMI */
/*********************************************************/

include_once("../inc/lst_int.inc.php");
include_once("../inc/lst_tri.inc.php");

/**
 * Création des titres (th) des listes écrans
 * 
 * @param $tableau
 * @param bool $triok
 */
function titlst($tableau, $triok = TRUE)
{
    global
    $_SESSION,
    $tab_comp_libelle,
    $tab_comp_symbole;

    // Récupération paramètres actuels		
    $tri = @$_GET["tri"];
    $ord = @$_GET["ord"];
    $tableau = encode_str($tableau);
    // On accepte les tris sur colonne
    if ($triok == TRUE) {
        foreach ($tableau as $nom_col => $code_champ) {
            echo "<th>";

            if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) {
                if (count($_SESSION["tri_col"]) > 0) {
                    if (array_key_exists($code_champ, $_SESSION["tri_col"])) {
                        $tab_tmp = $_SESSION["tri_col"][$code_champ];

                        echo '<div id="ico_rech_' . $code_champ . '"';
                        if (($tab_tmp[1] == 'null') AND (trim($tab_tmp[2]) == ''))
                            echo ' class="ico_rech_col_std"';
                        else
                            echo ' class="ico_rech_col_sel"';
                        echo '>';
                        echo '<img src="../img/rech_col.gif" border="0" align="absmiddle" title="' . _("Filtrer") . '" alt="' . _("Filtrer") . '">';
                        echo '</div>';
                        echo '<form id="form_rech_col_' . $code_champ . '">';
                        echo '<div id="rech_col_' . $code_champ . '" class="rech_col">';
                        echo '<div id="prm_rech_col_' . $code_champ . '" class="prm_rech_col">';
                        echo '<div style="float:right;">';
                        echo ' <img id="cancel_rech_' . $code_champ . '" src="../img/rech_close_small.png" border="0" align="absmiddle" title="' . _("Annuler") . '" alt="' . _("Annuler") . '">';
                        echo '</div>';
                        echo $nom_col;
                        echo '</br>';
                        echo ' <a href="#" class="popr">';
                        echo ' <input name=="prm_rech_' . $code_champ . '" type="button" id="prm_rech_' . $code_champ . '" class="bton_rech_std" value="' . $tab_comp_symbole[$tab_tmp[1]] . '" size="2">';
                        echo '<span name="lib_rech_' . $code_champ . '" id="lib_rech_' . $code_champ . '">' . formatetxtbr($tab_comp_libelle[$tab_tmp[1]]) . '<div class="annot_petit_it">(' . _("cliquez pour changer") . ')</div></span></a>';
                        echo ' ';
                        echo '<input name="' . $code_champ . '" type="text" id="' . $code_champ . '" value="' . $tab_tmp[2] . '" size="20" style="margin-right:25px;">';
                        echo '</br>';
                        echo '<img id="valid_rech_' . $code_champ . '" src="../img/rech_add.png" border="0" align="absmiddle" title="' . _("Valider") . '" alt="' . _("Valider") . '" style="margin-right:10px;">';
                        echo ' <img id="reset_rech_' . $code_champ . '" src="../img/rech_init.png" border="0" align="absmiddle" title="' . ("Réinitialiser") . '" alt="' . _("Réinitialiser") . '">';
                        echo '</div>';
                        echo "</div>";
                        echo '</form>';
                    } else
                        echo '</ br>';
                }
            }

            $urltria = $_SERVER["PHP_SELF"] . "?ord=ASC";
            $urltrid = $_SERVER["PHP_SELF"] . "?ord=DES";

            foreach ($_GET as $key => $value) {
                if (($key <> "ord") && ($key <> "tri")) {
                    $urltria .= "&" . $key . "=" . $value;
                    $urltrid .= "&" . $key . "=" . $value;
                }
            }

            $urltria .= "&tri=" . $nom_col;
            $urltrid .= "&tri=" . $nom_col;

            if (trim($code_champ) <> "") {
                if ($nom_col == $tri) {
                    if ($ord == "ASC") {
                        echo '<img src="../img/up.gif" align="absmiddle" title="' . _("Trié en ascendant") . '"> ';
                        echo '<a href="' . $urltrid . '" title="' . _("Tri descendant sur") . ' ' . $nom_col . '">';
                    } else {
                        echo '<img src="../img/dn.gif" align="absmiddle" title="Trié en descendant"> ';
                        echo '<a href="' . $urltria . '" title="' . _("Tri ascendant sur") . ' ' . $nom_col . '">';
                    }
                } else
                    echo '<a href="' . $urltria . '" title="' . _("Tri ascendant sur") . ' ' . $nom_col . '">';
            }
            echo '<span class="annot_petit_gras3">';
            echo $nom_col;
            echo '</span>';
            if (trim($code_champ) <> "")
                echo "</a>\n";
            echo "</th>\n";
        }
    } else // On n'accepte pas les tris sur colonne
    {
        foreach ($tableau as $nom_col => $code_champ) {
            echo "<th>";
            echo '<span class="annot_petit_gras3">';
            echo $nom_col;
            echo '</span>';
            echo "</th>\n";
        }
    }
}

/**
 * Création du menu de navigation
 * 
 * @param $numrows
 * @param $nligpage
 */
function pagine_lst($numrows, $nligpage)
{
    // Nb Maximum de liens de pages à afficher avant et après la page courante
    $nbmaxlpages = 9;

    // Nombre total de pages
    $pagestot = intval($numrows / $nligpage);
    if ($numrows % $nligpage) $pagestot++;

    //Numéro d'enregistrement courant
    $enrcour = 1;
    if (!empty($_GET['page']))
        $enrcour = intval($_GET['page']);

    //Numéro de page courante courant
    $pagecour = intval($enrcour / $nligpage);
    if ($enrcour % $nligpage) $pagecour++;
    // Calcul du premier et du dernier numéro de lien à afficher
    $firstnumpage = max(1, $pagecour - $nbmaxlpages);
    $lastnumpage = min($pagestot, $pagecour + $nbmaxlpages);
    // Affichage de la pagination
    echo '<div align="center">';
    /*
    echo '<table width="100%" border="0">';
    echo '<tr>';
    echo '<td align="center">';
    */
    // Affichage de l'icône première page
    if ($firstnumpage > 1) {
        $url = $_SERVER["PHP_SELF"] . "?page=1";

        foreach ($_GET as $key => $value) {
            if ($key <> "page")
                $url .= "&" . $key . "=" . $value;
        }

        echo '<a href="' . $url . '"><img src="../img/deb.png" border="0" align="absmiddle" title="' . _("Premmière page") . '"></a> ';
    }
    // Affichage de l'icône de page précédente
    if (($pagecour > 1) && ($pagestot > 1)) {
        $back_page = $enrcour - $nligpage;
        $url = $_SERVER["PHP_SELF"] . "?page=$back_page";

        foreach ($_GET as $key => $value) {
            if ($key <> "page")
                $url .= "&" . $key . "=" . $value;
        }

        echo '<a href="' . $url . '"><img src="../img/prec.png" border="0" align="absmiddle" title="' . _("Précédent") . '"></a> ';
    }
    // Affichage d'un lien direct sur première page
    if ($firstnumpage > 1) {
        $url = $_SERVER["PHP_SELF"] . "?page=1";

        foreach ($_GET as $key => $value) {
            if ($key <> "page")
                $url .= "&" . $key . "=" . $value;
        }
        echo '<span class="pnsel"><a href="' . $url . '">1</a></span> ';
        echo ' ... ';
    }
    // Boucle sur chaque page et CREATION d'un lien sur celle-ci
    // Pas de page s'il n'y a seulement qu'une page
    if ($pagestot > 1) {
        for ($i = $firstnumpage; $i <= $lastnumpage; $i++) {
            $ppage = ($nligpage * ($i - 1)) + 1;
            if ($i == $pagecour)
                echo '<span class="psel">' . $i . '</span> ';
            else {
                $url = $_SERVER["PHP_SELF"] . "?page=$ppage";

                foreach ($_GET as $key => $value) {
                    if ($key <> "page")
                        $url .= "&" . $key . "=" . $value;
                }

                echo '<span class="pnsel"><a href="' . $url . '">' . $i . '</a></span> ';
            }
        }
    }
    // Affichage d'un lien direct sur dernière page
    if ($lastnumpage < $pagestot) {
        echo "... \n";
        $last_page = ($nligpage * ($pagestot - 1)) + 1;
        $url = $_SERVER["PHP_SELF"] . "?page=$last_page";

        foreach ($_GET as $key => $value) {
            if ($key <> "page")
                $url .= "&" . $key . "=" . $value;
        }
        $pagemax = intval($numrows / $nligpage);
        if ($numrows % $nligpage) $pagemax++;
        echo '<span class="pnsel"><a href="' . $url . '">' . $pagemax . '</a></span> ';
    }

    // Affichage de l'icône de page suivante
    if ($pagecour < $pagestot) {
        $next_page = $enrcour + $nligpage; // Si c'est la dernière page, pas de page suivante
        $url = $_SERVER["PHP_SELF"] . "?page=$next_page";

        foreach ($_GET as $key => $value) {
            if ($key <> "page")
                $url .= "&" . $key . "=" . $value;
        }

        echo ' <a href="' . $url . '"><img src="../img/suiv.png" border="0" align="absmiddle" title="' . _("Page suivante") . '"></a>';
    }
    // Affichage de l'icône de dernière page
    if ($lastnumpage < $pagestot) {
        $last_page = ($nligpage * ($pagestot - 1)) + 1;
        $url = $_SERVER["PHP_SELF"] . "?page=$last_page";

        foreach ($_GET as $key => $value) {
            if ($key <> "page")
                $url .= "&" . $key . "=" . $value;
        }

        echo ' <a href="' . $url . '"><img src="../img/fin.png" border="0" align="absmiddle" title="' . _("Dernière page") . '"></a>';
    }
    /*
    echo '</td>';
    echo '</tr>';
    echo '</table>';
    */
    echo '</div>';
}

/**
 * Création des lignes TR des listes écrans avec effet au passage de la souris
 * 
 * @param $index
 * @param bool $defaut
 */
function creetrlst($index, $defaut = FALSE)
{
    $complement_classe = '';

    if ($defaut == TRUE)
        $complement_classe = 'defaut';

    // Couleurs alternées
    if ($index % 2 == 0)
        echo "<tr onmouseover=\"this.className='TRsel" . $complement_classe . "'\" onmouseout=\"this.className='TRalter1" . $complement_classe . "'\" class=\"TRalter1" . $complement_classe . "\">\n";
    else
        echo "<tr onmouseover=\"this.className='TRsel" . $complement_classe . "'\" onmouseout=\"this.className='TRalter2" . $complement_classe . "'\" class=\"TRalter2" . $complement_classe . "\">\n";
}

/**
 * Création des lignes TR des listes écrans (détails) avec effet au passage de la souris
 * 
 * @param $index
 */
function creetrlstdet($index)
{
    // Couleurs alternées
    if ($index % 2 == 0)
        echo "<tr onmouseover=\"this.style.backgroundColor='Whitesmoke'\" onmouseout=\"this.style.backgroundColor='#999999'\" class=\"TRalter1D\">\n";
    else
        echo "<tr onmouseover=\"this.style.backgroundColor='Whitesmoke'\" onmouseout=\"this.style.backgroundColor='#CCCCCC'\" class=\"TRalter2D\">\n";
}

/**
 * Retour Page Précédente si liste vide 
 */
function retpp()
{
    global
    $url_add,
    $_SESSION;

    echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">';
    echo '<tr>';
    echo '<td class="annot_std_gras">';
    echo _("Aucun enregistrement trouvé");
    echo "<br /><br />";
    echo "<a href=\"javascript:history.back();\">";
    echo '<img src="../img/prec.png" border="0" align="absmiddle">';
    echo " " . _("Retour à la page précédente");
    echo "</a>";
    if (isset($_SESSION["lnk_lst"])) {
        if (trim($_SESSION["lnk_lst"]) <> '') {
            echo '<a href="#" id="reset_rech_lst">';
            echo ' / '._("Réinitialiser la liste");
            echo '</a>';
        }
    }
    echo '</td>';
    echo '<td align="right">';
    if (($url_add <> "") AND (@$_GET['ta'] <> "i")) {
        echo "<br />";
        echo '<a href="' . $url_add . '"><img src="../img/add.png" title ="'._("Ajouter enregistrement").'" border="0" align="absmiddle"></a>';
        echo '<a href="' . $url_add . '"> '._("Ajouter").'</a>';
    }
    echo '</tr>';
    echo "</table>";
}

/*********************************************************/
/* Diverses fonctions pour les listes imprimées dans SMI */
/*********************************************************/

/**
 * Entête des impressions des listes
 * 
 * @param $titre
 * @param bool $logo
 */
function entimplst($titre, $logo = FALSE)
{
    global
    $chemin_images,
    $chemin_logos;

    if ($logo == TRUE) {
        $lnkimg = $chemin_logos . "/logcge.gif";

        // Logo
        if (file_exists(trim($lnkimg))) {
            $infos_img = @getimagesize($lnkimg);
            $largeur = $infos_img[0];
            $hauteur = $infos_img[1];
            $coeffl = 1;
            $coeffh = 1;
            if ($lmax > 0) {
                if ($largeur > $lmax)
                    $coeffl = ($largeur / $lmax);
            }
            if ($hmax > 0) {
                if ($hauteur > $hmax)
                    $coeffh = ($hauteur / $hmax);
            }
            if ($coeffl > $coeffh)
                $coeff = $coeffl;
            else
                $coeff = $coeffh;
            $hauteur = floor($hauteur / $coeff);
            $largeur = floor($largeur / $coeff);
            echo '<img src="' . $lnkimg . '"  title="' . $title . '" id="' . $id . '" alt="' . $alt . '" width="' . $largeur . '" height="' . $hauteur . '" border="0" align="absmiddle">';
            echo "\n";
            echo "<br />";
        }

        $urlcache = $chemin_images . "/cache.gif";

        // Affichage coordonnées centre de gestion
        $row = lit_enr_cdg();
        if (is_array($row)) {
            extract($row);
            echo "<span class=\"annot_petit_gras\">";
            echo $cge_adr1;
            if ($cge_adr2 <> "")
                echo ", " . $cge_adr2;
            echo ", " . Retournecp_Id($cge_ville, TRUE) . " " . Retourneville_Id($cge_ville, TRUE) . "<br />";
            echo "<br />";
            echo "</span>";
            echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"2\" class=\"annot_petit_gras\">\n";
            echo "<tr>\n";
            echo "<td>\n";
            $chemin_imagesco = $chemin_images . "/telc.png";
            echo "<img src=\"" . $chemin_imagesco . "\" align=\"absmiddle\">";
            echo " " . $cge_telf;
            if (trim($cge_txttelf) <> "") {
                echo "<span class=\"annot_petit_it\">";
                echo " (" . $cge_txttelf . ")";
                echo "</span>";
            }
            echo "</td>\n";
            echo "<td style=\"padding-left:10px;\">\n";
            $chemin_imagesco = $chemin_images . "/faxc.png";
            echo "<img src=\"" . $chemin_imagesco . "\" align=\"absmiddle\">";
            echo " " . $cge_fax;
            if (trim($cge_txtfax) <> "") {
                echo "<span class=\"annot_petit_it\">";
                echo " (" . $cge_txtfax . ")";
                echo "</span>";
            }
            echo "</td>\n";
            echo "</tr>\n";
            echo "<tr>\n";
            echo "<td>\n";
            $chemin_imagesco = $chemin_images . "/webc.png";
            echo "<img src=\"" . $chemin_imagesco . "\" align=\"absmiddle\">";
            echo " " . $cge_web;
            echo "</td>\n";
            echo "<td style=\"padding-left:10px;\">\n";
            $chemin_imagesco = $chemin_images . "/atc.png";
            echo "<img src=\"" . $chemin_imagesco . "\" align=\"absmiddle\">";
            echo " " . $cge_email . "<br />";
            echo "</td>\n";
            echo "</tr>\n";
            echo "</table>";
        } else {
            echo "<br />";
            echo "<br />";
            $chemin_imagesco = $chemin_images . "/telc.png";
            echo "<img src=\"" . $chemin_imagesco . "\" align=\"absmiddle\">";
            echo "<img src=\"" . $urlcache . "\" align=\"absmiddle\">";
            $chemin_imagesco = $chemin_images . "/faxc.png";
            echo "<img src=\"" . $chemin_imagesco . "\" align=\"absmiddle\">";
            echo "<br />";
            $chemin_imagesco = $chemin_images . "/webc.png";
            echo "<img src=\"" . $chemin_imagesco . "\" align=\"absmiddle\">";
            $chemin_imagesco = $chemin_images . "/atc.png";
            echo "<img src=\"" . $chemin_imagesco . "\" align=\"absmiddle\">";
            echo "<br />";

            $chemin_imagesco = $chemin_images . "/telc.png";
            echo "<img src=\"" . $chemin_imagesco . "\" align=\"absmiddle\">";
            echo "<br />";
            $chemin_imagesco = $chemin_images . "/faxc.png";
            echo "<img src=\"" . $chemin_imagesco . "\" align=\"absmiddle\">";
            echo "<br />";
            $chemin_imagesco = $chemin_images . "/webc.png";
            echo "<img src=\"" . $chemin_imagesco . "\" align=\"absmiddle\">";
            echo "<br />";
            echo "<br />";
        }
    }

    // Etat
    echo "<p align=\"center\"><span class=\"titre_impression\">$titre</span></p>";
    echo "<span class=\"annot_petit_it\">";
    echo "Imprimée le : ";
    echo "</span>";
    echo "<span class=\"annot_gras_it\">";
    echo date("d-m-Y H:i:s") . "<br />";
    echo "</span>";
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        echo "<span class=\"annot_petit_it\">";
        echo "Filtrée sur : ";
        echo "</span>";
        echo "<span class=\"annot_gras_it\">";
        echo trim($_GET['search']);
        echo "</span>";
        echo "<span class=\"annot_petit_it\">";
        echo ". ";
        echo "</span>";
    }
    if (isset($_GET['tri']) && !empty($_GET['tri'])) {
        echo "<span class=\"annot_petit_it\">";
        echo "Triée par : ";
        echo "</span>";
        echo "<span class=\"annot_gras_it\">";
        echo $_GET['tri'];
        echo "</span>";
        echo "<span class=\"annot_petit_it\">";
        if ((@$_GET['ord'] == "ASC") OR (@$_GET['ord'] == "")) {
            echo " (";
            echo "</span>";
            echo "<span class=\"annot_gras_it\">";
            echo "Ascendant";
            echo "</span>";
            echo "<span class=\"annot_petit_it\">";
            echo ").";
        } else {
            echo " (";
            echo "</span>";
            echo "<span class=\"annot_gras_it\">";
            echo "Descendant";
            echo "</span>";
            echo "<span class=\"annot_petit_it\">";
            echo ").";
        }
        echo "</span>";
    }
    if (isset($_GET['fi']) && !empty($_GET['fi'])) {
        echo "<span class=\"annot_gras_it\">";
        echo " + Filtre type appliqué";
        echo "</span>";
    }
    if ((trim(@$_GET['search']) <> "") OR (@$_GET['ord'] <> "")) {
        echo "<br />";
    }
}

/**
 * Création des titres (th) des listes ou fiches imprimées
 * 
 * @param $tableau
 */
function titimplst($tableau)
{
    foreach ($tableau as $nom_col => $code_champ) {
        echo "<th>";
        echo ucfirst("$nom_col");
        echo "</th>\n";
    }
}

/***************************************************************/
/* Diverses fonctions pour les traitements des listes dans SMI */
/***************************************************************/

/**
 *  Création de la chaîne de recherche des listes écrans et impressions des logs de connexions
 * 
 * @return string
 */
function cree_strrechlstlog()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBLOG . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " log_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($search) == TRUE) {
            $daterech = datetomysql($search);
            $rech .= " log_date LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " log_heure LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " log_urlprec LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " log_url LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " log_ip LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}


/**
 *  Création de la chaîne de recherche
 * des listes écrans et impressions des logs de fichiers
 * 
 * @return string
 */
function cree_strrechlstflog()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBFICLOG . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " ficlog_date LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " ficlog_heure LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ficlog_ip LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ficlog_ref LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ficlog_fic LIKE '%" . $db->escape($search) . "%' OR";
        $search = strtolower($_GET['search']);
        if (($search == "ok") OR ($search == "hack") OR ($search == "leech") OR ($search == "inconnu")) {
            switch ($search) {
                case "ok" :
                    $rech .= " ficlog_type = '0' OR";
                    break;
                case "hack" :
                    $rech .= " ficlog_type = '1' OR";
                    break;
                case "leech" :
                    $rech .= " ficlog_type = '2' OR";
                    break;
                case "inconnu" :
                    $rech .= " ficlog_type = '3' OR";
                    break;
                default :
                    break;
            }
        }

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 *  Création de la chaîne de recherche
 * des listes écrans et impressions des agences
 * 
 * @return string
 */

function cree_strrechlstage()
{
    global
    $db;

    $rech = "SELECT DISTINCT * FROM `" . TBAGE . "`, `" . TBACAT . "`, `" . TBVILLES . "`, `" . TBPAYS . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBAGE . ".age_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBAGE . ".age_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBAGE . ".age_telf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBAGE . ".age_ville LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBAGE . ".age_codepays LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBAGE . ".age_email LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBACAT . ".acat_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBVILLES . ".villes_cp LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPAYS . ".pays_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE (" . TBAGE . ".age_cat=" . TBACAT . ".acat_code";
        $rech .= " AND " . TBAGE . ".age_ville=" . TBVILLES . ".villes_id AND " . TBAGE . ".age_codepays=" . TBPAYS . ".pays_code)";
    } else {
        $rech .= " AND (" . TBAGE . ".age_cat=" . TBACAT . ".acat_code";
        $rech .= " AND " . TBAGE . ".age_ville=" . TBVILLES . ".villes_id AND " . TBAGE . ".age_codepays=" . TBPAYS . ".pays_code)";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 *  Création de la chaîne de recherche
 * des listes écrans et impressions des catégories agences
 * 
 * @return string
 */
function cree_strrechlstacat()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBACAT . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " acat_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " acat_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 *  Création de la chaîne de recherche des listes écrans et impressions des catégories clients
 * 
 * @return string
 */
function cree_strrechlstcat()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBCCAT . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " cat_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cat_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 *  Création de la chaîne de recherche des listes écrans et impressions des civilités
 * 
 * @return string
 */

function cree_strrechlstciv()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBCIV . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " civ_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " civ_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 *  Création de la chaîne de recherche des listes écrans et impressions des champs de check-list
 * 
 * @return string
 */
function cree_strrechlstclc()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBCLC . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " clc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " clc_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 *  Création de la chaîne de recherche des listes écrans et impressions des groupements de champs de check-list
 * 
 * @return string
 */
function cree_strrechlstclcg()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBCLCG . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " clcg_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " clcg_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 *  Création de la chaîne de recherche des listes écrans et impressions des donneurs d'ordres
 * 
 * @return string
 */
function cree_strrechlstdo()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBDO . "`, `" . TBVILLES . "`, `" . TBPAYS . "`";
    $rech .= " WHERE " . TBDO . ".do_ville=" . TBVILLES . ".villes_id AND " . TBDO . ".do_codepays=" . TBPAYS . ".pays_code";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBDO . ".do_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDO . ".do_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDO . ".do_telf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDO . ".do_ville LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDO . ".do_codepays LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDO . ".do_email LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBVILLES . ".villes_cp LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPAYS . ".pays_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 *  Création de la chaîne de recherche des listes écrans et impressions des modèles de droits utilisateurs
 * 
 * @return string
 */
function cree_strrechlstdroitsm()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBDROITSM . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " droitsm_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " droitsm_des LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 *  Création de la chaîne de recherche des listes écrans et impressions des fonctions
 * 
 * @return string
 */
function cree_strrechlstfonc()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBFONC . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " fonc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " fonc_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/** 
 *  Création de la chaîne de recherche des listes écrans et impressions des clients
 * 
 * @param $nivutil
 * @param $codeage
 * @param $codedo
 * @param bool $open
 * @param bool $qte
 * @return string
 */

function cree_strrechlstcli($nivutil, $codeage, $codedo, $open = TRUE, $qte = FALSE)
{
    global
    $db;

    if ($qte == FALSE) {
        $rech = "SELECT " . TBCLI . ".cli_id, " . TBCLI . ".cli_prop, " . TBCLI . ".cli_codedo, " . TBCLI . ".cli_code, " . TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, ";
        $rech .= TBCLI . ".cli_prenom, " . TBCLI . ".cli_nom, " . TBCLI . ".cli_dep, " . TBCLI . ".cli_telf, " . TBCLI . ".cli_telp, ";
        $rech .= TBCLI . ".cli_email, " . TBCLI . ".cli_cpta ";
    } else
        $rech = "SELECT COUNT(*) ";
    $rech .= "FROM `" . TBCLI . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " cli_prop LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_codedo LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_prenom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_telf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_telp LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_email LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }

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

    switch ($nivutil) {
        // Agences ou Techniciens
        case 4 :
        case 3 :
            $rech2 = "SELECT * FROM `" . TBAGE . "` WHERE age_code = '" . $db->escape($codeage) . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2)
                extract($row2);
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE (" . TBCLI . ".cli_prop='" . $db->escape($codeage) . "' OR (" . TBCLI . ".cli_prop IS NULL OR " . TBCLI . ".cli_prop=''";
            else
                $rech .= " AND (" . TBCLI . ".cli_prop='" . $db->escape($codeage) . "' OR (" . TBCLI . ".cli_prop IS NULL OR " . TBCLI . ".cli_prop=''";
            $rech3 = "SELECT * FROM `" . TBZCC . "` WHERE zcc_code = '" . $db->escape($codeage) . "'";
            $rows3 = $db->get_results($rech3, ARRAY_A);
            $nb_enr = count($rows3);
            if ($nb_enr > 0) {
                $idx = 0;
                if ($nb_enr == 1)
                    $rech .= " AND";
                else
                    $rech .= " AND (";
                foreach ($rows3 as $row3) {
                    extract($row3);
                    if ($idx == 0)
                        $rech .= " " . TBCLI . ".cli_dep='" . $zcc_codedep . "'";
                    else
                        $rech .= " OR " . TBCLI . ".cli_dep='" . $zcc_codedep . "'";
                    $idx++;
                }
                if ($nb_enr > 1)
                    $rech .= ")";
            }
            $rech .= " AND cli_codepays='" . $age_codepays . "'";
            $rech .= ")";
            $rech .= ")";
            break;

        // Donneurs d'Ordres et leurs Techniciens
        case 6 :
        case 7 :
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE " . TBCLI . ".cli_codedo='" . $db->escape($codedo) . "'";
            else
                $rech .= " AND " . TBCLI . ".cli_codedo='" . $db->escape($codedo) . "'";
            break;
        default :
            break;
    }
    // *** Fin de Recherche

    if ($open == TRUE)
        $db->disconnect();

    return $rech;
}

/** 
 *  Création de la chaîne de recherche des listes écrans des sites clients
 * @param $nivutil
 * @param $codeage
 * @param $codedo
 * @param bool $qte
 * @return string
 */

function cree_strrechlstscli($nivutil, $codeage, $codedo, $qte = FALSE)
{
    global
    $db;

    if ($qte == FALSE) {
        $rech = "SELECT " . TBSCLI . ".scli_id, " . TBSCLI . ".scli_prop, " . TBSCLI . ".scli_codecli, " . TBSCLI . ".scli_id, " . TBSCLI . ".scli_ste, " . TBSCLI . ".scli_rcivil, ";
        $rech .= TBSCLI . ".scli_rprenom, " . TBSCLI . ".scli_rnom, " . TBSCLI . ".scli_dep, " . TBSCLI . ".scli_telf, " . TBSCLI . ".scli_fax, ";
        $rech .= TBSCLI . ".scli_rtelf, " . TBSCLI . ".scli_rtelp, " . TBSCLI . ".scli_datecrea, ";
        $rech .= TBSCLI . ".scli_email, scli_actif, ";
        $rech .= TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, " . TBCLI . ".cli_prenom, " . TBCLI . ".cli_nom ";
    } else
        $rech = "SELECT COUNT(*) ";
    $rech .= "FROM `" . TBSCLI . "`, `" . TBCLI . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " " . TBSCLI . ".scli_prop LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_codecli LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_rcivil LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_rprenom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_rnom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_telf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_fax LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_rtelf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_rfax LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_email LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate(trim($_GET['search'])) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " scli_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBCLI . ".cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_prenom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }

    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBSCLI . ".scli_codecli=" . TBCLI . ".cli_code";
    else
        $rech .= " AND " . TBSCLI . ".scli_codecli=" . TBCLI . ".cli_code";

    switch ($nivutil) {
        // Agences ou Techniciens
        case 4 :
        case 3 :
            $tab_zcc = tab_zcc_age_open_full($codeage, FALSE);
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE (";
            else
                $rech .= " AND (";
            $rech .= TBSCLI . ".scli_prop='" . $db->escape($codeage) . "' OR " . TBCLI . ".cli_prop='" . $db->escape($codeage) . "'";
            if (count($tab_zcc) > 0) {
                foreach ($tab_zcc as $key => $value) {
                    $tab_zone = explode('_', $key);
                    $rech .= " OR (" . TBSCLI . ".scli_prop='-1' AND " . TBSCLI . ".scli_dep='" . $tab_zone[0] . "' AND " . TBSCLI . ".scli_codepays='" . $tab_zone[1] . "')";
                }
            }
            $rech .= ")";
            break;

        // Donneurs d'Ordres et leurs Techniciens
        case 6 :
        case 7 :
            if (!stristr($rech, "WHERE "))
                $rech .= " WHERE " . TBCLI . ".cli_codedo='" . $db->escape($codedo) . "'";
            else
                $rech .= " AND " . TBCLI . ".cli_codedo='" . $codedo . "'";
            break;

        default :
            break;
    }
    // *** Fin de Recherche

    return $rech;
}

/** 
 *  Création de la chaîne de recherche des listes écrans et impressions des clients depuis l'espace client
 * 
 * @param $tab_rcli
 * @param bool $qte
 * @return string
 */

function cree_strrechlstclicli($tab_rcli, $qte = FALSE)
{
    global
    $db;

    if ($qte == FALSE) {
        $rech = "SELECT " . TBCLI . ".cli_id," . TBCLI . ".cli_prop, " . TBCLI . ".cli_codedo, " . TBCLI . ".cli_code, " . TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, ";
        $rech .= TBCLI . ".cli_prenom, " . TBCLI . ".cli_nom, " . TBCLI . ".cli_dep, " . TBCLI . ".cli_telf, " . TBCLI . ".cli_telp, ";
        $rech .= TBCLI . ".cli_email, " . TBCLI . ".cli_cpta ";
    } else
        $rech = "SELECT COUNT(*) ";
    $rech .= "FROM `" . TBCLI . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " cli_prop LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_codedo LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_prenom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_telf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_telp LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_email LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }

    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE ";
    else
        $rech .= " AND ";
    if (count($tab_rcli) > 1)
        $rech .= "(";
    foreach ($tab_rcli as $value) {
        $rech .= " " . TBCLI . ".cli_code='" . $value . "' OR";
    }
    // Suppression du dernier 'OR'
    $rech = substr($rech, 0, strlen($rech) - 3);

    if (count($tab_rcli) > 1)
        $rech .= ")";
    // *** Fin de Recherche

    return $rech;
}

/**  Création de la chaîne de recherche des listes écrans des sites clients depuis l'espace clients
 * 
 * @param $tab_rcli
 * @param bool $qte
 * @return string
 */

function cree_strrechlstsclicli($tab_rcli, $qte = FALSE)
{
    global
    $db;

    if ($qte == FALSE) {
        $rech = "SELECT " . TBSCLI . ".scli_id, " . TBSCLI . ".scli_prop, " . TBSCLI . ".scli_codecli, " . TBSCLI . ".scli_id, " . TBSCLI . ".scli_ste, " . TBSCLI . ".scli_rcivil, ";
        $rech .= TBSCLI . ".scli_rprenom, " . TBSCLI . ".scli_rnom, " . TBSCLI . ".scli_dep, " . TBSCLI . ".scli_telf, " . TBSCLI . ".scli_fax, ";
        $rech .= TBSCLI . ".scli_rtelf, " . TBSCLI . ".scli_rtelp, " . TBSCLI . ".scli_datecrea, ";
        $rech .= TBSCLI . ".scli_email, ";
        $rech .= TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, " . TBCLI . ".cli_prenom, " . TBCLI . ".cli_nom ";
    } else
        $rech = "SELECT COUNT(*) ";
    $rech .= "FROM `" . TBSCLI . "`, `" . TBCLI . "`";


    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " " . TBSCLI . ".scli_prop LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_codecli LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_rcivil LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_rprenom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_rnom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_telf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_fax LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_rtelf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_rfax LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_email LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " scli_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBCLI . ".cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_prenom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }

    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBSCLI . ".scli_codecli=" . TBCLI . ".cli_code";
    else
        $rech .= " AND " . TBSCLI . ".scli_codecli=" . TBCLI . ".cli_code";

    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE ";
    else
        $rech .= " AND ";
    if (count($tab_rcli) > 1)
        $rech .= "(";
    foreach ($tab_rcli as $value) {
        $rech .= " " . TBSCLI . ".scli_codecli='" . $value . "' OR";
    }
    // Suppression du dernier 'OR'
    $rech = substr($rech, 0, strlen($rech) - 3);

    if (count($tab_rcli) > 1)
        $rech .= ")";

    // *** Fin de Recherche

    return $rech;
}

/** 
 *  Création de la chaîne de recherche
 * des listes écrans et impressions des clients pour les opérateurs téléphoniques
 * 
 * @param bool $qte
 * @return string
 */

function cree_strrechlstcliope($qte = FALSE)
{
    global
    $db;

    if ($qte == FALSE) {
        $rech = "SELECT 
					cli_prop,
					cli_code,
					cli_ste,
					cli_civilite,
					cli_prenom,
					cli_nom,
					cli_dep,
					cli_cpta";
    } else
        $rech = "SELECT COUNT(*) ";

    $rech .= " FROM `" . TBCLI . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " cli_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_prenom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " cli_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/** 
 *  Création de la chaîne de recherche
 * des listes écrans des sites clients (affichage opérateurs téléphoniques)
 * 
 * @param bool $qte
 * @return string
 */

function cree_strrechlstscliope($qte = FALSE)
{
    global
    $db;

    if ($qte == FALSE) {
        $rech = "SELECT " . TBSCLI . ".scli_codecli, " . TBSCLI . ".scli_id, " . TBSCLI . ".scli_ste, ";
        $rech .= TBSCLI . ".scli_datecrea, ";
        $rech .= TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, " . TBCLI . ".cli_prenom, " . TBCLI . ".cli_nom ";
    } else
        $rech = "SELECT COUNT(*) ";
    $rech .= "FROM `" . TBSCLI . "`, `" . TBCLI . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " " . TBSCLI . ".scli_codecli LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSCLI . ".scli_ste LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " scli_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBCLI . ".cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_prenom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }

    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBSCLI . ".scli_codecli=" . TBCLI . ".cli_code";
    else
        $rech .= " AND " . TBSCLI . ".scli_codecli=" . TBCLI . ".cli_code";

    // *** Fin de Recherche

    return $rech;
}

/** 
 *  Création de la chaîne de recherche
 * des listes écrans et impressions des contrats types
 */

function cree_strrechlstco()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBCO . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " co_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " co_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " co_desc LIKE '%" . $db->escape($search) . "%' OR";
        // $rech .= " co_cgv LIKE '%".$db->escape($search)."%' OR";
        $search = strtolower($_GET['search']);
        if (($search == "actif") OR ($search == "inactif")) {
            if ($search == "actif") {
                $rech .= " co_actif = '1' OR";
            } else {
                $rech .= " co_actif = '0' OR";
            }
        }


        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/** 
 *  Création de la chaîne de recherche
 * des listes écrans et impressions des contrats clients
 */

function cree_strrechlstcocl()
{
    global
    $db,
    $typ_horaire;

    $rech = "SELECT " . TBCOCL . ".cocl_code, " . TBCOCL . ".cocl_codeco, " . TBCOCL . ".cocl_codeage, " . TBCOCL . ".cocl_codecli, " . TBCOCL . ".cocl_datecrea, ";
    $rech .= TBCOCL . ".cocl_datesig, " . TBCOCL . ".cocl_dated, " . TBCOCL . ".cocl_datef, " . TBCOCL . ".cocl_datefin, " . TBCOCL . ".cocl_daterel, " . TBCOCL . ".cocl_typh, ";
    $rech .= TBCOCL . ".cocl_renouv, " . TBCOCL . ".cocl_rnum, " . TBCOCL . ".cocl_nbs, ";
    $rech .= TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, " . TBCLI . ".cli_nom, " . TBCLI . ".cli_prenom, " . TBCLI . ".cli_dep, ";
    $rech .= TBCO . ".co_code, " . TBCO . ".co_nom ";
    $rech .= "FROM `" . TBCOCL . "`, `" . TBCLI . "`, `" . TBCO . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBCOCL . ".cocl_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCOCL . ".cocl_codeage LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCOCL . ".cocl_codeco LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCOCL . ".cocl_codecli LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBCOCL . ".cocl_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_datesig LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_dated LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_datef LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_datefin LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_daterel LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $search = strtolower($_GET['search']);
        if (($search == _("oui")) OR ($search == _("non"))) {
            if ($search == _("oui")) {
                $rech .= " " . TBCOCL . ".cocl_renouv = '1' OR";
            } else {
                $rech .= " " . TBCOCL . ".cocl_renouv = '0' OR";
            }
        }
        foreach ($typ_horaire as $key => $value) {
            if (stristr($value, $_GET['search']))
                $rech .= " " . TBCOCL . ".cocl_typh = '" . $key . "' OR";
        }
        $rech .= " " . TBCOCL . ".cocl_rnum LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_prenom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCO . ".co_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBCOCL . ".cocl_codecli=" . TBCLI . ".cli_code AND " . TBCOCL . ".cocl_codeco=" . TBCO . ".co_code";
    else
        $rech .= " AND " . TBCOCL . ".cocl_codecli=" . TBCLI . ".cli_code AND " . TBCOCL . ".cocl_codeco=" . TBCO . ".co_code";
    // *** Fin de Recherche

    return $rech;
}

/** 
 *  Création de la chaîne de recherche
 * des listes écrans et impressions des annexes contrats clients
 * 
 * @param string $codecocl
 * @return string
 */

function cree_strrechlstcocla($codecocl = "")
{
    global
    $db;

    $rech = "SELECT " . TBCOCLA . ".cocla_code, " . TBCOCLA . ".cocla_datecrea, " . TBCOCLA . ".cocla_codecocl,";
    $rech .= TBCOCLA . ".cocla_datesig, " . TBCOCLA . ".cocla_daterel, ";
    $rech .= TBCOCL . ".cocl_codeage, " . TBCOCL . ".cocl_codecli ";
    $rech .= "FROM `" . TBCOCLA . "`, `" . TBCOCL . "`";
    if (trim($codecocl) <> "")
        $rech .= " WHERE " . TBCOCLA . ".cocla_codecocl='" . $db->escape($codecocl) . "'";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBCOCLA . ".cocla_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBCOCLA . ".cocl_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCLA . ".cocl_datesig LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCLA . ".cocl_daterel LIKE '%" . $db->escape($daterech) . "%' OR";
        }

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBCOCL . ".cocl_code=" . TBCOCLA . ".cocla_codecocl";
    else
        $rech .= " AND " . TBCOCL . ".cocl_code=" . TBCOCLA . ".cocla_codecocl";
    // *** Fin de Recherche

    return $rech;
}

/**  Création de la chaîne de recherche
 * des listes écrans et impressions des contrats clients (pour l'affichage clients) **/

function cree_strrechlstcoclc()
{
    global
    $db,
    $typ_horaire;

    $rech = "SELECT " . TBCOCL . ".cocl_code, " . TBCOCL . ".cocl_codeage, " . TBCOCL . ".cocl_codecli, " . TBCOCL . ".cocl_datecrea, " . TBCOCL . ".cocl_datesig, ";
    $rech .= TBCOCL . ".cocl_codeco, " . TBCOCL . ".cocl_dated, " . TBCOCL . ".cocl_datef, " . TBCOCL . ".cocl_daterel, " . TBCOCL . ".cocl_datefin, ";
    $rech .= TBCOCL . ".cocl_typh, " . TBCOCL . ".cocl_renouv, " . TBCOCL . ".cocl_rnum, ";
    $rech .= TBCOCL . ".cocl_nbs, ";
    $rech .= TBCO . ".co_code, " . TBCO . ".co_nom ";
    $rech .= "FROM `" . TBCOCL . "`, `" . TBCLI . "`, `" . TBCO . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBCOCL . ".cocl_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($search) == TRUE) {
            $daterech = datetomysql($search);
            $rech .= " " . TBCOCL . ".cocl_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_datesig LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_dated LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_datef LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_daterel LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_datefin LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $search = strtolower($search);
        if (($search == _("oui")) OR ($search == _("non"))) {
            if ($search == _("oui")) {
                $rech .= " " . TBCOCL . ".cocl_renouv = '1' OR";
            } else {
                $rech .= " " . TBCOCL . ".cocl_renouv = '0' OR";
            }
        }
        foreach ($typ_horaire as $key => $value) {
            if (stristr($value, $search))
                $rech .= " " . TBCOCL . ".cocl_typh = '" . $key . "' OR";
        }
        $rech .= " " . TBCOCL . ".cocl_rnum LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBCOCL . ".cocl_codecli=" . TBCLI . ".cli_code AND " . TBCOCL . ".cocl_codeco=" . TBCO . ".co_code";
    else
        $rech .= " AND " . TBCOCL . ".cocl_codecli=" . TBCLI . ".cli_code AND " . TBCOCL . ".cocl_codeco=" . TBCO . ".co_code";
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des historiques interventions sur contrats
 */

function cree_strrechlstcoclh()
{
    global
    $db;

    $rech = "SELECT " . TBINT . ".int_code, " . TBINT . ".int_datedde, " . TBINT . ".int_codecli, " . TBINT . ".int_codecocl, ";
    $rech .= TBINT . ".int_codeage, " . TBINT . ".int_daterdv, " . TBINT . ".int_durees, " . TBINT . ".int_codestatut, ";
    $rech .= TBINT . ".int_datefin, " . TBCOCLH . ".coclh_codecli, " . TBCOCLH . ".coclh_codecocl, " . TBCOCLH . ".coclh_codeint ";
    $rech .= "FROM `" . TBINT . "`, `" . TBCOCLH . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        $rech .= " WHERE " . TBINT . ".int_codecocl = '" . $db->escape($search) . "'";
    }
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBINT . ".int_code = " . TBCOCLH . ".coclh_codeint";
    else
        $rech .= " AND " . TBINT . ".int_code = " . TBCOCLH . ".coclh_codeint";
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des relances contrats clients
 */

function cree_strrechlstcoclr()
{
    global
    $db;

    $rech = "SELECT " . TBCOCL . ".cocl_code, " . TBCOCL . ".cocl_codeco, " . TBCOCL . ".cocl_codecli, " . TBCOCL . ".cocl_datecrea, " . TBCOCL . ".cocl_daterec, " . TBCOCL . ".cocl_dated, ";
    $rech .= TBCOCL . ".cocl_datef, " . TBCOCL . ".cocl_preavis, " . TBCOCL . ".cocl_datefin, " . TBCOCL . ".cocl_daterel, " . TBCOCL . ".cocl_typh, ";
    $rech .= TBCOCL . ".cocl_nivrel, " . TBCOCL . ".cocl_renouv, " . TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, " . TBCLI . ".cli_nom ";
    $rech .= "FROM `" . TBCOCL . "`, `" . TBCLI . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBCOCL . ".cocl_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCOCL . ".cocl_codecli LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBCOCL . ".cocl_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_dated LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_datef LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBCOCL . ".cocl_daterel LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBCOCL . ".cocl_nivrel LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // ne pas rechercher les contrats déjà renouvelés
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBCOCL . ".cocl_renouv = '0'";
    else
        $rech .= " AND " . TBCOCL . ".cocl_renouv = '0'";
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBCOCL . ".cocl_codecli=" . TBCLI . ".cli_code";
    else
        $rech .= " AND " . TBCOCL . ".cocl_codecli=" . TBCLI . ".cli_code";
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des contrats de location
 *
 * @param $fi
 * @param $tab_rcli
 * @return string
 */

function cree_strrechlstcoloc($fi, $tab_rcli)
{
    global
    $db,
    $_SESSION;

    // Clients
    $rech1 = "SELECT " . TBCOLOC . ".coloc_code, " . TBCOLOC . ".coloc_codeage, " . TBCOLOC . ".coloc_codecli, " . TBCOLOC . ".coloc_datecrea, ";
    $rech1 .= TBCOLOC . ".coloc_datedeb, " . TBCOLOC . ".coloc_datefin, " . TBCOLOC . ".coloc_dateclos, " . TBCOLOC . ".coloc_typcli, ";
    $rech1 .= TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, " . TBCLI . ".cli_nom, " . TBCLI . ".cli_dep ";
    $rech1 .= "FROM `" . TBCOLOC . "`, `" . TBCLI . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech1, "WHERE "))
            $rech1 .= " WHERE (";
        else
            $rech1 .= " AND (";

        // Construction de la chaîne de recherche
        $rech1 .= TBCOLOC . ".coloc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech1 .= " " . TBCOLOC . ".coloc_codeage LIKE '%" . $db->escape($search) . "%' OR";
        $rech1 .= " " . TBCOLOC . ".coloc_codecli LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech1 .= " " . TBCOLOC . ".coloc_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech1 .= " " . TBCOLOC . ".coloc_datefin LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech1 .= " " . TBCOLOC . ".coloc_dateclos LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $search = strtolower($_GET['search']);
        $rech1 .= " " . TBCLI . ".cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech1 .= " " . TBCLI . ".cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech1 .= " " . TBCLI . ".cli_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech1 = substr($rech1, 0, strlen($rech1) - 3);
        $rech1 .= ")";
    }
    if (!stristr($rech1, "WHERE "))
        $rech1 .= " WHERE " . TBCOLOC . ".coloc_codecli=" . TBCLI . ".cli_code AND " . TBCOLOC . ".coloc_typcli=0";
    else
        $rech1 .= " AND " . TBCOLOC . ".coloc_codecli=" . TBCLI . ".cli_code AND " . TBCOLOC . ".coloc_typcli=0";

    switch ($_SESSION["niv_log"]) {
        case 1 :
            if (count($tab_rcli) > 0) {
                if (!stristr($rech1, "WHERE "))
                    $rech1 .= " WHERE ";
                else
                    $rech1 .= " AND ";
                if (count($tab_rcli) > 1)
                    $rech1 .= "(";
                foreach ($tab_rcli as $value) {
                    $rech1 .= " " . TBCOLOC . ".coloc_codecli='" . $value . "' OR";
                }
                // Suppression du dernier 'OR'
                $rech1 = substr($rech1, 0, strlen($rech1) - 3);

                if (count($tab_rcli) > 1)
                    $rech1 .= ")";
            }
            break;
        case 3 :
        case 4 :
            $rech1 .= " AND " . TBCOLOC . ".coloc_codeage='" . @$_SESSION["code_age"] . "'";
            break;
    }

    switch ($fi) {
        case 1 :
            if (!stristr($rech1, "WHERE "))
                $rech1 .= " WHERE " . TBCOLOC . ".coloc_dateclos='0000-00-00'";
            else
                $rech1 .= " AND " . TBCOLOC . ".coloc_dateclos='0000-00-00'";
            break;
        case 2 :
            if (!stristr($rech1, "WHERE "))
                $rech1 .= " WHERE " . TBCOLOC . ".coloc_dateclos<>'0000-00-00'";
            else
                $rech1 .= " AND " . TBCOLOC . ".coloc_dateclos<>'0000-00-00'";
            break;
        default :
            break;
    }

    // Agences
    $rech2 = "SELECT " . TBCOLOC . ".coloc_code, " . TBCOLOC . ".coloc_codeage, " . TBCOLOC . ".coloc_codecli, " . TBCOLOC . ".coloc_datecrea, ";
    $rech2 .= TBCOLOC . ".coloc_datedeb, " . TBCOLOC . ".coloc_datefin, " . TBCOLOC . ".coloc_dateclos, " . TBCOLOC . ".coloc_typcli, ";
    $rech2 .= TBAGE . ".age_nom, " . TBAGE . ".age_resp_civilite, " . TBAGE . ".age_resp_nom, " . TBAGE . ".age_dep ";
    $rech2 .= "FROM `" . TBCOLOC . "`, `" . TBAGE . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        // WHERE existe ?
        if (!stristr($rech2, "WHERE "))
            $rech2 .= " WHERE (";
        else
            $rech2 .= " AND (";

        // Construction de la chaîne de recherche
        $rech2 .= TBCOLOC . ".coloc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech2 .= " " . TBCOLOC . ".coloc_codeage LIKE '%" . $db->escape($search) . "%' OR";
        $rech2 .= " " . TBCOLOC . ".coloc_codecli LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech2 .= " " . TBCOLOC . ".coloc_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech2 .= " " . TBCOLOC . ".coloc_datefin LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech2 .= " " . TBCOLOC . ".coloc_dateclos LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech2 .= " " . TBAGE . ".age_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech2 .= " " . TBAGE . ".age_resp_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech2 .= " " . TBAGE . ".age_resp_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech2 = substr($rech2, 0, strlen($rech2) - 3);
        $rech2 .= ")";
    }
    if (!stristr($rech2, "WHERE "))
        $rech2 .= " WHERE " . TBCOLOC . ".coloc_codecli=" . TBAGE . ".age_code AND " . TBCOLOC . ".coloc_typcli=1";
    else
        $rech2 .= " AND " . TBCOLOC . ".coloc_codecli=" . TBAGE . ".age_code AND " . TBCOLOC . ".coloc_typcli=1";
    switch ($_SESSION["niv_log"]) {
        case 1 :
            if (count($tab_rcli) > 0) {
                if (!stristr($rech2, "WHERE "))
                    $rech2 .= " WHERE ";
                else
                    $rech2 .= " AND ";
                if (count($tab_rcli) > 1)
                    $rech2 .= "(";
                foreach ($tab_rcli as $value) {
                    $rech2 .= " " . TBCOLOC . ".coloc_codecli='" . $value . "' OR";
                }
                // Suppression du dernier 'OR'
                $rech2 = substr($rech2, 0, strlen($rech2) - 3);

                if (count($tab_rcli) > 1)
                    $rech2 .= ")";
            }
            break;
        case 3 :
        case 4 :
            $rech2 .= " AND " . TBCOLOC . ".coloc_codeage='" . @$_SESSION["code_age"] . "'";
            break;
    }

    switch ($fi) {
        case 1 :
            if (!stristr($rech2, "WHERE "))
                $rech2 .= " WHERE " . TBCOLOC . ".coloc_dateclos='0000-00-00'";
            else
                $rech2 .= " AND " . TBCOLOC . ".coloc_dateclos='0000-00-00'";
            break;
        case 2 :
            if (!stristr($rech2, "WHERE "))
                $rech2 .= " WHERE " . TBCOLOC . ".coloc_dateclos<>'0000-00-00'";
            else
                $rech2 .= " AND " . TBCOLOC . ".coloc_dateclos<>'0000-00-00'";
            break;
        default :
            break;
    }

    // Technicien agence
    $rech3 = "SELECT " . TBCOLOC . ".coloc_code, " . TBCOLOC . ".coloc_codeage, " . TBCOLOC . ".coloc_codecli, " . TBCOLOC . ".coloc_datecrea, ";
    $rech3 .= TBCOLOC . ".coloc_datedeb, " . TBCOLOC . ".coloc_datefin, " . TBCOLOC . ".coloc_dateclos, " . TBCOLOC . ".coloc_typcli, ";
    $rech3 .= TBTEC . ".tec_prenom, " . TBTEC . ".tec_civilite, " . TBTEC . ".tec_nom, " . TBTEC . ".tec_dep ";
    $rech3 .= "FROM `" . TBCOLOC . "`, `" . TBTEC . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        // WHERE existe ?
        if (!stristr($rech3, "WHERE "))
            $rech3 .= " WHERE (";
        else
            $rech3 .= " AND (";

        // Construction de la chaîne de recherche
        $rech3 .= TBCOLOC . ".coloc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech3 .= " " . TBCOLOC . ".coloc_codeage LIKE '%" . $db->escape($search) . "%' OR";
        $rech3 .= " " . TBCOLOC . ".coloc_codecli LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech3 .= " " . TBCOLOC . ".coloc_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech3 .= " " . TBCOLOC . ".coloc_datefin LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech3 .= " " . TBCOLOC . ".coloc_dateclos LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $search = strtolower($_GET['search']);
        $rech3 .= " " . TBTEC . ".tec_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech3 .= " " . TBTEC . ".tec_civilite LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech3 = substr($rech3, 0, strlen($rech3) - 3);
        $rech3 .= ")";
    }
    if (!stristr($rech3, "WHERE "))
        $rech3 .= " WHERE " . TBCOLOC . ".coloc_codecli=" . TBTEC . ".tec_code AND " . TBCOLOC . ".coloc_typcli=2";
    else
        $rech3 .= " AND " . TBCOLOC . ".coloc_codecli=" . TBTEC . ".tec_code AND " . TBCOLOC . ".coloc_typcli=2";
    switch ($_SESSION["niv_log"]) {
        case 1 :
            if (count($tab_rcli) > 0) {
                if (!stristr($rech3, "WHERE "))
                    $rech3 .= " WHERE ";
                else
                    $rech3 .= " AND ";
                if (count($tab_rcli) > 1)
                    $rech3 .= "(";
                foreach ($tab_rcli as $value) {
                    $rech3 .= " " . TBCOLOC . ".coloc_codecli='" . $value . "' OR";
                }
                // Suppression du dernier 'OR'
                $rech3 = substr($rech3, 0, strlen($rech3) - 3);

                if (count($tab_rcli) > 1)
                    $rech3 .= ")";
            }
            break;
        case 3 :
        case 4 :
            $rech3 .= " AND " . TBCOLOC . ".coloc_codeage='" . @$_SESSION["code_age"] . "'";
            break;
    }

    switch ($fi) {
        case 1 :
            if (!stristr($rech3, "WHERE "))
                $rech3 .= " WHERE " . TBCOLOC . ".coloc_dateclos='0000-00-00'";
            else
                $rech3 .= " AND " . TBCOLOC . ".coloc_dateclos='0000-00-00'";
            break;
        case 2 :
            if (!stristr($rech3, "WHERE "))
                $rech3 .= " WHERE " . TBCOLOC . ".coloc_dateclos<>'0000-00-00'";
            else
                $rech3 .= " AND " . TBCOLOC . ".coloc_dateclos<>'0000-00-00'";
            break;
        default :
            break;
    }

    // *** Fin de Recherche

    // Sous-traitants
    $rech4 = "SELECT " . TBCOLOC . ".coloc_code, " . TBCOLOC . ".coloc_codeage, " . TBCOLOC . ".coloc_codecli, " . TBCOLOC . ".coloc_datecrea, ";
    $rech4 .= TBCOLOC . ".coloc_datedeb, " . TBCOLOC . ".coloc_datefin, " . TBCOLOC . ".coloc_dateclos, " . TBCOLOC . ".coloc_typcli, ";
    $rech4 .= TBST . ".st_nom, " . TBST . ".st_resp_civilite, " . TBST . ".st_resp_nom, " . TBST . ".st_dep ";
    $rech4 .= "FROM `" . TBCOLOC . "`, `" . TBST . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        // WHERE existe ?
        if (!stristr($rech4, "WHERE "))
            $rech4 .= " WHERE (";
        else
            $rech4 .= " AND (";

        // Construction de la chaîne de recherche
        $rech4 .= TBCOLOC . ".coloc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech4 .= " " . TBCOLOC . ".coloc_codeage LIKE '%" . $db->escape($search) . "%' OR";
        $rech4 .= " " . TBCOLOC . ".coloc_codecli LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech4 .= " " . TBCOLOC . ".coloc_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech4 .= " " . TBCOLOC . ".coloc_datefin LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech4 .= " " . TBCOLOC . ".coloc_dateclos LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $search = strtolower($_GET['search']);
        $rech4 .= " " . TBST . ".st_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech4 .= " " . TBST . ".st_resp_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech4 .= " " . TBST . ".st_resp_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech4 = substr($rech4, 0, strlen($rech4) - 3);
        $rech4 .= ")";
    }
    if (!stristr($rech4, "WHERE "))
        $rech4 .= " WHERE " . TBCOLOC . ".coloc_codecli=" . TBST . ".st_code AND " . TBCOLOC . ".coloc_typcli=3";
    else
        $rech4 .= " AND " . TBCOLOC . ".coloc_codecli=" . TBST . ".st_code AND " . TBCOLOC . ".coloc_typcli=3";
    switch ($_SESSION["niv_log"]) {
        case 1 :
            if (count($tab_rcli) > 0) {
                if (!stristr($rech4, "WHERE "))
                    $rech4 .= " WHERE ";
                else
                    $rech4 .= " AND ";
                if (count($tab_rcli) > 1)
                    $rech4 .= "(";
                foreach ($tab_rcli as $value) {
                    $rech4 .= " " . TBCOLOC . ".coloc_codecli='" . $value . "' OR";
                }
                // Suppression du dernier 'OR'
                $rech4 = substr($rech4, 0, strlen($rech4) - 3);

                if (count($tab_rcli) > 1)
                    $rech4 .= ")";
            }
            break;
        case 3 :
        case 4 :
            $rech4 .= " AND " . TBCOLOC . ".coloc_codeage='" . @$_SESSION["code_age"] . "'";
            break;
    }

    switch ($fi) {
        case 1 :
            if (!stristr($rech4, "WHERE "))
                $rech4 .= " WHERE " . TBCOLOC . ".coloc_dateclos='0000-00-00'";
            else
                $rech4 .= " AND " . TBCOLOC . ".coloc_dateclos='0000-00-00'";
            break;
        case 2 :
            if (!stristr($rech4, "WHERE "))
                $rech4 .= " WHERE " . TBCOLOC . ".coloc_dateclos<>'0000-00-00'";
            else
                $rech4 .= " AND " . TBCOLOC . ".coloc_dateclos<>'0000-00-00'";
            break;
        default :
            break;
    }

    // *** Fin de Recherche

    $rech = $rech1 . " UNION " . $rech2 . " UNION " . $rech3 . " UNION " . $rech4;

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des catégories de dossiers
 */

function cree_strrechlstdosc()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBDOSC . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " dosc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " dosc_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des statuts de dossiers
 */

function cree_strrechlstdoss()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBDOSS . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " doss_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " doss_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des types actions de dossiers
 */

function cree_strrechlstta()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBTA . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " ta_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ta_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des catégories de demandeurs
 */

function cree_strrechlstddec()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBDDEC . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " ddec_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ddec_desc LIKE '%" . $db->escape($search) . "%' OR";
        $search = strtolower($_GET['search']);
        if (($search == _("oui")) OR ($search == _("non"))) {
            if ($search == _("oui")) {
                $rech .= " ddec_pub = '1' OR";
            } else {
                $rech .= " ddec_pub = '0' OR";
            }
        }

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}


/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des destinataires des demandes
 */

function cree_strrechlstdded()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBDDED . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " dded_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " dded_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " dded_email LIKE '%" . $db->escape($search) . "%' OR";
        $search = strtolower($_GET['search']);
        if (($search == _("oui")) OR ($search == _("non"))) {
            if ($search == _("oui")) {
                $rech .= " dded_pub = '1' OR";
                $rech .= " dded_cli = '1' OR";
                $rech .= " dded_age = '1' OR";
                $rech .= " dded_tec = '1' OR";
                $rech .= " dded_ope = '1' OR";
            } else {
                $rech .= " dded_pub = '0' OR";
                $rech .= " dded_cli = '0' OR";
                $rech .= " dded_age = '0' OR";
                $rech .= " dded_tec = '0' OR";
                $rech .= " dded_ope = '0' OR";
            }
        }

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des types de demandes
 */

function cree_strrechlstddet()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBDDET . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " ddet_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ddet_desc LIKE '%" . $db->escape($search) . "%' OR";
        $search = strtolower($_GET['search']);
        if (($search == _("oui")) OR ($search == _("non"))) {
            if ($search == _("oui")) {
                $rech .= " ddet_pub = '1' OR";
                $rech .= " ddet_cli = '1' OR";
                $rech .= " ddet_age = '1' OR";
                $rech .= " ddet_tec = '1' OR";
                $rech .= " ddet_tec = '0' OR";
            } else {
                $rech .= " ddet_pub = '0' OR";
                $rech .= " ddet_cli = '0' OR";
                $rech .= " ddet_age = '0' OR";
                $rech .= " ddet_tec = '0' OR";
                $rech .= " ddet_ope = '0' OR";
            }
        }

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des dépôts de location
 */

function cree_strrechlstdloc()
{
    global
    $db;

    $rech = "SELECT DISTINCT * FROM `" . TBDLOC . "`, `" . TBVILLES . "`, `" . TBPAYS . "`";
    $rech .= " WHERE " . TBDLOC . ".dloc_ville=" . TBVILLES . ".villes_id AND " . TBDLOC . ".dloc_codepays=" . TBPAYS . ".pays_code";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBDLOC . ".dloc_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDLOC . ".dloc_telf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDLOC . ".dloc_ville LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDLOC . ".dloc_codepays LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDLOC . ".dloc_email LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBVILLES . ".villes_cp LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPAYS . ".pays_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des départements
 */

function cree_strrechlstdep()
{
    global
    $db,
    $tab_lngdisp;

    $rech = "SELECT * FROM `" . TBDEP . "`, `" . TBPAYS . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBDEP . ".dep_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDEP . ".dep_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPAYS . ".pays_nom LIKE '%" . $db->escape($search) . "%' OR";
        foreach ($tab_lngdisp as $index => $value) {
            if (stristr($value, $db->escape($search)))
                $rech .= " " . TBDEP . ".dep_lng='" . $index . "' OR";
        }

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBDEP . ".dep_codepays=" . TBPAYS . ".pays_code";
    else
        $rech .= " AND " . TBDEP . ".dep_codepays=" . TBPAYS . ".pays_code";

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des devises
 */

function cree_strrechlstdev()
{
    global
    $db,
    $tab_lngdisp;

    $rech = "SELECT * FROM `" . TBDEV . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBDEV . ".dev_codea LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDEV . ".dev_coden LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDEV . ".dev_nom LIKE '%" . $db->escape($search) . "%' OR";
        foreach ($tab_lngdisp as $index => $value) {
            if (stristr($value, $db->escape($search)))
                $rech .= " " . TBDEV . ".dev_lng='" . $index . "' OR";
        }

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des dossiers
 */

function cree_strrechlstdos()
{
    global
    $db;

    $rech = "SELECT " . TBDOS . ".dos_code, " . TBDOS . ".dos_datedde, " . TBDOS . ".dos_datefinp, " . TBDOS . ".dos_datefin, ";
    $rech .= TBDOS . ".dos_codeage, " . TBDOS . ".dos_codecli, ";
    $rech .= TBDOS . ".dos_urg, " . TBDOS . ".dos_codesuiv, " . TBDOS . ".dos_codedoss, " . TBDOS . ".dos_codedosc, ";
    $rech .= TBDOS . ".dos_proba, " . TBDOS . ".dos_avance, " . TBDOS . ".dos_datefin, " . TBDOS . ".dos_res, ";
    $rech .= TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, " . TBCLI . ".cli_nom, ";
    $rech .= TBDOSS . ".doss_code, " . TBDOSS . ".doss_desc, ";
    $rech .= TBDOSC . ".dosc_code, " . TBDOSC . ".dosc_desc, ";
    $rech .= TBURG . ".urg_des ";
    $rech .= "FROM `" . TBDOS . "`, `" . TBCLI . "`, `" . TBDOSS . "`, `" . TBDOSC . "`, `" . TBURG . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBDOS . ".dos_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBDOS . ".dos_datedde LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBDOS . ".dos_datefinp LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBDOS . ".dos_codecli LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDOS . ".dos_codeage LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDOS . ".dos_proba LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDOS . ".dos_avance LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDOS . ".dos_res LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDOS . ".dos_codesuiv LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDOS . ".dos_codedoss LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDOSS . ".doss_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDOSS . ".doss_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDOSC . ".dosc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDOSC . ".dosc_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBURG . ".urg_des LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE (" . TBDOS . ".dos_codecli=" . TBCLI . ".cli_code";
        $rech .= " AND " . TBDOS . ".dos_codedoss=" . TBDOSS . ".doss_code";
        $rech .= " AND " . TBDOS . ".dos_urg=" . TBURG . ".urg_niveau";
        $rech .= " AND " . TBDOS . ".dos_codedosc=" . TBDOSC . ".dosc_code)";
    } else {
        $rech .= " AND " . TBDOS . ".dos_codecli=" . TBCLI . ".cli_code";
        $rech .= " AND " . TBDOS . ".dos_codedoss=" . TBDOSS . ".doss_code";
        $rech .= " AND " . TBDOS . ".dos_urg=" . TBURG . ".urg_niveau";
        $rech .= " AND " . TBDOS . ".dos_codedosc=" . TBDOSC . ".dosc_code";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des villes
 */

function cree_strrechlstvil()
{
    global
    $db,
    $tab_lngdisp;

    $rech = "SELECT * FROM `" . TBVILLES . "`, `" . TBDEP . "`, `" . TBPAYS . "`";
    $rech .= " WHERE (" . TBVILLES . ".villes_dep=" . TBDEP . ".dep_code AND " . TBVILLES . ".villes_lng=" . TBDEP . ".dep_lng)";
    $rech .= " AND (" . TBVILLES . ".villes_codepays=" . TBPAYS . ".pays_code AND " . TBVILLES . ".villes_lng=" . TBPAYS . ".pays_lng)";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBVILLES . ".villes_cp LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBVILLES . ".villes_nom LIKE '%" . $db->escape($search) . "%' OR";
        foreach ($tab_lngdisp as $index => $value) {
            if (stristr($value, $_GET['search']))
                $rech .= " " . TBDEP . ".dep_lng='" . $index . "' OR";
        }
        $rech .= " " . TBDEP . ".dep_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPAYS . ".pays_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des pays
 */

function cree_strrechlstpays()
{
    global
    $db,
    $tab_lngdisp;

    $rech = "SELECT * FROM `" . TBPAYS . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBPAYS . ".pays_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPAYS . ".pays_nom LIKE '%" . $db->escape($search) . "%' OR";
        foreach ($tab_lngdisp as $index => $value) {
            if (stristr($value, $_GET['search']))
                $rech .= " " . TBPAYS . ".pays_lng='" . $index . "' OR";
        }

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des catégories évènements plannings
 */

function cree_strrechlstpcat()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBPCAT . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " pcat_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " pcat_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des statuts évènements planning
 */

function cree_strrechlstpsta()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBPSTA . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " psta_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " psta_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}


/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des familles
 */

function cree_strrechlstfam()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBFAM . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " fam_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " fam_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";

    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des indices horaires
 */

function cree_strrechlstind()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBIND . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " ind_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ind_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ind_coeff LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des catégories d'interventions
 */

function cree_strrechlstintc()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBINTC . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " intc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " intc_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des factures sur interventions
 */

function cree_strrechlstintf()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBINTF . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " intf_numf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " intf_datefac LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des synthèses interventions
 *
 * @param $nivutil
 * @return string
 */

function cree_strrechlstints($nivutil)
{
    $rech = "SELECT " . TBINT . ".int_id, " . TBINT . ".int_codecrea, " . TBINT . ".int_code, " . TBINT . ".int_codeext, " . TBINT . ".int_codeman, ";
    $rech .= TBINT . ".int_codecat, " . TBINT . ".int_codeocli, " . TBINT . ".int_datedde, ";
    $rech .= TBINT . ".int_daterdv, " . TBINT . ".int_datefinp, " . TBINT . ".int_codeage, " . TBINT . ".int_codedo, " . TBINT . ".int_codecli, ";
    $rech .= TBINT . ".int_urg, " . TBINT . ".int_codetec, " . TBINT . ".int_codestatut, ";
    $rech .= TBINT . ".int_idsite, " . TBINT . ".int_dateprev, " . TBINT . ".int_datefin, " . TBINT . ".int_codeages, ";
    $rech .= TBINT . ".int_mat, " . TBINT . ".int_pbm, " . TBINT . ".int_fact, " . TBINT . ".int_forfait, " . TBINT . ".int_hrdv, " . TBINT . ".int_planifie, ";
    if ($nivutil <> 1) {
        $rech .= TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, " . TBCLI . ".cli_prenom, " . TBCLI . ".cli_nom, ";
        $rech .= TBCLI . ".cli_dep, " . TBCLI . ".cli_codepays, " . TBCLI . ".cli_prop, ";
    }
    $rech .= TBSTA . ".statut_code, " . TBSTA . ".statut_desc, ";
    $rech .= TBINTC . ".intc_code, " . TBINTC . ".intc_desc ";

    if ($nivutil <> 1)
        $rech .= "FROM `" . TBINT . "`, `" . TBCLI . "`, `" . TBSTA . "`, `" . TBINTC . "`";
    else
        $rech .= "FROM `" . TBINT . "`, `" . TBSTA . "`, `" . TBINTC . "`";

    $where_mode = false;
    if (!stristr($rech, "WHERE ")) {
        $where_mode = true;
        $rech .= " WHERE (";
    } else
        $rech .= " AND ";
    // Jonctions
    $rech .= TBINT . ".int_codestatut=" . TBSTA . ".statut_code";
    switch ($_SESSION["niv_log"]) {
        case 1 :
            $rech .= " AND " . TBINT . ".int_codecat=" . TBINTC . ".intc_code";
            break;
        default :
            $rech .= " AND " . TBINT . ".int_codecat=" . TBINTC . ".intc_code";
            $rech .= " AND " . TBINT . ".int_codecli=" . TBCLI . ".cli_code";
            break;
    }
    if ($where_mode == true)
        $rech .= ")";

    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des imports spécifiques
 */

function cree_strrechlstiprm()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBIPRM . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " iprm_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " iprm_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";

    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des lieux
 */

function cree_strrechlstlieu()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBLIEU . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " lieu_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " lieu_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des moyens d'envoi
 */

function cree_strrechlstme()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBME . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " me_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " me_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " me_delai LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des types de demandes
 */

function cree_strrechlstmd()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBMD . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " md_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " md_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}


/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des types marques
 */

function cree_strrechlstmquet()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBMQUET . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " mquet_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " mquet_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des moyens de transport
 */

function cree_strrechlstmt()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBMT . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " mt_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " mt_des LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des opérateurs
 */

function cree_strrechlstope()
{
    global
    $db;

    $rech = "SELECT 
				ope_congd,
				ope_congf,
				ope_code,
				ope_prenom,
				ope_nom,
				ope_dep,
				ope_telf,
				ope_telp,
				ope_out,
				ope_dateout
			 FROM `" . TBOPE . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " ope_congd LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " ope_congf LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " ope_dateout LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " ope_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ope_prenom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ope_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ope_telf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " ope_telp LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche	

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des ordres
 */

function cree_strrechlstot()
{
    global
    $db;

    $rech = "SELECT " . TBOT . ".ot_code, " . TBOT . ".ot_datedde, " . TBOT . ".ot_daterdv, " . TBOT . ".ot_hrdv, " . TBOT . ".ot_codest, " . TBOT . ".ot_codeint, ";
    $rech .= TBOT . ".ot_codelieu, " . TBOT . ".ot_urg, " . TBOT . ".ot_codestatut, " . TBOT . ".ot_codetec, " . TBOT . ".ot_codecli, ";
    $rech .= TBOT . ".ot_idsite, " . TBOT . ".ot_mat, " . TBOT . ".ot_pbm, " . TBOT . ".ot_fait,  " . TBOT . ".ot_datefin, ";
    $rech .= TBST . ".st_nom, " . TBST . ".st_resp_civilite, " . TBST . ".st_resp_nom, ";
    $rech .= TBSTA . ".statut_code, " . TBSTA . ".statut_desc, ";
    $rech .= TBLIEU . ".lieu_code, " . TBLIEU . ".lieu_desc, ";
    $rech .= TBINT . ".int_code, " . TBINT . ".int_codeage, " . TBINT . ".int_codedo, " . TBINT . ".int_codecli, " . TBINT . ".int_codeman, " . TBINT . ".int_codeext, ";
    $rech .= TBINT . ".int_codedo, " . TBINT . ".int_codetec, " . TBINT . ".int_idsite, " . TBINT . ".int_datefin, " . TBINT . ".int_dateprev, ";
    $rech .= TBURG . ".urg_des ";
    $rech .= "FROM `" . TBOT . "`, `" . TBST . "`, `" . TBSTA . "`, `" . TBLIEU . "`, `" . TBINT . "`, `" . TBURG . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBOT . ".ot_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBOT . ".ot_datedde LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBOT . ".ot_daterdv LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBOT . ".ot_datefin LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBOT . ".ot_codest LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codelieu LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codetec LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codestatut LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_mat LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_pbm LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_hrdv LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_resp_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_resp_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSTA . ".statut_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSTA . ".statut_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBLIEU . ".lieu_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBLIEU . ".lieu_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBINT . ".int_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBINT . ".int_codecli LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBINT . ".int_codeman LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBINT . ".int_codedo LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBINT . ".int_codeext LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBURG . ".urg_des LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE (" . TBOT . ".ot_codest=" . TBST . ".st_code";
        $rech .= " AND " . TBOT . ".ot_codelieu=" . TBLIEU . ".lieu_code";
        $rech .= " AND " . TBOT . ".ot_codestatut=" . TBSTA . ".statut_code";
        $rech .= " AND " . TBOT . ".ot_urg=" . TBURG . ".urg_niveau";
        $rech .= " AND " . TBOT . ".ot_codeint=" . TBINT . ".int_code)";
    } else {
        $rech .= " AND " . TBOT . ".ot_codest=" . TBST . ".st_code";
        $rech .= " AND " . TBOT . ".ot_codelieu=" . TBLIEU . ".lieu_code";
        $rech .= " AND " . TBOT . ".ot_codestatut=" . TBSTA . ".statut_code";
        $rech .= " AND " . TBOT . ".ot_urg=" . TBURG . ".urg_niveau";
        $rech .= " AND " . TBOT . ".ot_codeint=" . TBINT . ".int_code";
    }

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des ordres à régulariser
 *
 * @param bool $qte
 * @return string
 */

function cree_strrechlstot_ar($qte= FALSE)
{
    global
    $db;

    if ($qte == FALSE)
    {
        $rech = "SELECT " . TBOT . ".ot_code, " . TBOT . ".ot_datedde, " . TBOT . ".ot_daterdv, " . TBOT . ".ot_hrdv, " . TBOT . ".ot_codest, " . TBOT . ".ot_codeint, ";
        $rech .= TBOT . ".ot_codelieu, " . TBOT . ".ot_urg, " . TBOT . ".ot_codestatut, " . TBOT . ".ot_codetec, " . TBOT . ".ot_codecli, ";
        $rech .= TBOT . ".ot_idsite, " . TBOT . ".ot_mat, " . TBOT . ".ot_pbm,  " . TBOT . ".ot_datefin, ";
        $rech .= TBST . ".st_nom, " . TBST . ".st_resp_civilite, " . TBST . ".st_resp_nom, ";
        $rech .= TBSTA . ".statut_code, " . TBSTA . ".statut_desc, ";
        $rech .= TBLIEU . ".lieu_code, " . TBLIEU . ".lieu_desc, ";
        $rech .= TBCLI . ".cli_prop, ";
        $rech .= TBURG . ".urg_des ";
    }
    else
        $rech = "SELECT COUNT(*) ";

    $rech .= "FROM `" . TBOT . "`, `" . TBST . "`, `" . TBSTA . "`, `" . TBLIEU . "`, `" . TBCLI . "`, `" . TBURG . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBOT . ".ot_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBOT . ".ot_datedde LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBOT . ".ot_daterdv LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBOT . ".ot_datefin LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBOT . ".ot_codest LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codelieu LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codetec LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codecli LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codestatut LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_mat LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_pbm LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_hrdv LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_resp_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_resp_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSTA . ".statut_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSTA . ".statut_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBLIEU . ".lieu_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBLIEU . ".lieu_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBURG . ".urg_des LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE (" . TBOT . ".ot_codest=" . TBST . ".st_code";
        $rech .= " AND " . TBOT . ".ot_codelieu=" . TBLIEU . ".lieu_code";
        $rech .= " AND " . TBOT . ".ot_codestatut=" . TBSTA . ".statut_code";
        $rech .= " AND " . TBOT . ".ot_urg=" . TBURG . ".urg_niveau";
        // $rech .= " AND ".TBOT.".ot_codeint='--AR--'";
        $rech .= " AND " . TBOT . ".ot_codecli=" . TBCLI . ".cli_code)";
    } else {
        $rech .= " AND " . TBOT . ".ot_codest=" . TBST . ".st_code";
        $rech .= " AND " . TBOT . ".ot_codelieu=" . TBLIEU . ".lieu_code";
        $rech .= " AND " . TBOT . ".ot_codestatut=" . TBSTA . ".statut_code";
        $rech .= " AND " . TBOT . ".ot_urg=" . TBURG . ".urg_niveau";
        // $rech .= " AND ".TBOT.".ot_codeint='--AR--'";
        $rech .= " AND " . TBOT . ".ot_codecli=" . TBCLI . ".cli_code";
    }

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des ordres régularisés
 *
 * @param bool $qte
 * @return string
 */

function cree_strrechlstot_r($qte= FALSE)
{
    global
    $db;

    if ($qte == FALSE)
    {
        $rech = "SELECT " . TBOT . ".ot_code, " . TBOT . ".ot_datedde, " . TBOT . ".ot_daterdv, " . TBOT . ".ot_hrdv, " . TBOT . ".ot_codest, " . TBOT . ".ot_codeint, ";
        $rech .= TBOT . ".ot_codelieu, " . TBOT . ".ot_urg, " . TBOT . ".ot_codestatut, " . TBOT . ".ot_codetec, " . TBOT . ".ot_codecli, ";
        $rech .= TBOT . ".ot_idsite, " . TBOT . ".ot_mat, " . TBOT . ".ot_pbm,  " . TBOT . ".ot_datefin, ";
        $rech .= TBST . ".st_nom, " . TBST . ".st_resp_civilite, " . TBST . ".st_resp_nom, ";
        $rech .= TBSTA . ".statut_code, " . TBSTA . ".statut_desc, ";
        $rech .= TBLIEU . ".lieu_code, " . TBLIEU . ".lieu_desc, ";
        $rech .= TBCLI . ".cli_prop, ";
        $rech .= TBURG . ".urg_des ";
    }
    else
        $rech = "SELECT COUNT(*) ";

    $rech .= "FROM `" . TBINT . "`,`" . TBOT . "`, `" . TBST . "`, `" . TBSTA . "`, `" . TBLIEU . "`, `" . TBCLI . "`, `" . TBURG . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBOT . ".ot_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBOT . ".ot_datedde LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBOT . ".ot_daterdv LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBOT . ".ot_datefin LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBOT . ".ot_codest LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codelieu LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codetec LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codecli LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codestatut LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_mat LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_pbm LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_hrdv LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_resp_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_resp_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSTA . ".statut_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSTA . ".statut_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBLIEU . ".lieu_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBLIEU . ".lieu_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBURG . ".urg_des LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE (" . TBOT . ".ot_codest=" . TBST . ".st_code";
        $rech .= " AND " . TBOT . ".ot_codelieu=" . TBLIEU . ".lieu_code";
        $rech .= " AND " . TBOT . ".ot_codestatut=" . TBSTA . ".statut_code";
        $rech .= " AND " . TBOT . ".ot_urg=" . TBURG . ".urg_niveau";
        $rech .= " AND " . TBOT . ".ot_codeint=" . TBINT . ".int_code";
        // $rech .= " AND ".TBOT.".ot_codeint='--AR--'";
        $rech .= " AND " . TBOT . ".ot_codecli=" . TBCLI . ".cli_code)";
    } else {
        $rech .= " AND " . TBOT . ".ot_codest=" . TBST . ".st_code";
        $rech .= " AND " . TBOT . ".ot_codelieu=" . TBLIEU . ".lieu_code";
        $rech .= " AND " . TBOT . ".ot_codestatut=" . TBSTA . ".statut_code";
        $rech .= " AND " . TBOT . ".ot_urg=" . TBURG . ".urg_niveau";
        $rech .= " AND " . TBOT . ".ot_codeint=" . TBINT . ".int_code";
        // $rech .= " AND ".TBOT.".ot_codeint='--AR--'";
        $rech .= " AND " . TBOT . ".ot_codecli=" . TBCLI . ".cli_code";
    }

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des ordres  pour les sous-traitants
 *
 * @param bool $qte
 * @return string
 */

function cree_strrechlstotst($qte = FALSE)
{
    global
    $db;

    if ($qte == FALSE)
    {
        $rech = "SELECT " . TBOT . ".ot_code, " . TBOT . ".ot_datedde, " . TBOT . ".ot_daterdv, " . TBOT . ".ot_hrdv, " . TBOT . ".ot_codest, " . TBOT . ".ot_codeint, ";
        $rech .= TBOT . ".ot_codelieu, " . TBOT . ".ot_urg, " . TBOT . ".ot_codestatut, " . TBOT . ".ot_codetec, " . TBOT . ".ot_codecli, ";
        $rech .= TBOT . ".ot_idsite, " . TBOT . ".ot_mat, " . TBOT . ".ot_pbm,  " . TBOT . ".ot_datefin, ";
        $rech .= TBST . ".st_nom, " . TBST . ".st_resp_civilite, " . TBST . ".st_resp_nom, ";
        $rech .= TBSTA . ".statut_code, " . TBSTA . ".statut_desc, ";
        $rech .= TBLIEU . ".lieu_code, " . TBLIEU . ".lieu_desc, ";
        $rech .= TBURG . ".urg_des ";
    }
    else
        $rech = "SELECT COUNT(*) ";

    $rech .= "FROM `" . TBOT . "`, `" . TBSTA . "`, `" . TBLIEU . "`, `" . TBST . "`, `" . TBURG . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBOT . ".ot_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql(trim($_GET['search']));
            $rech .= " " . TBOT . ".ot_datedde LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBOT . ".ot_daterdv LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBOT . ".ot_codest LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codelieu LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codetec LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_codestatut LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBOT . ".ot_hrdv LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSTA . ".statut_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBSTA . ".statut_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBLIEU . ".lieu_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBLIEU . ".lieu_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBURG . ".urg_des LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE (" . TBOT . ".ot_codest=" . TBST . ".st_code";
        $rech .= " AND " . TBOT . ".ot_codelieu=" . TBLIEU . ".lieu_code";
        $rech .= " AND " . TBOT . ".ot_urg=" . TBURG . ".urg_niveau";
        $rech .= " AND " . TBOT . ".ot_codestatut=" . TBSTA . ".statut_code)";
    } else {
        $rech .= " AND " . TBOT . ".ot_codest=" . TBST . ".st_code";
        $rech .= " AND " . TBOT . ".ot_codelieu=" . TBLIEU . ".lieu_code";
        $rech .= " AND " . TBOT . ".ot_urg=" . TBURG . ".urg_niveau";
        $rech .= " AND " . TBOT . ".ot_codestatut=" . TBSTA . ".statut_code";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des origines
 */

function cree_strrechlstori()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBORI . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " orig_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " orig_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des périodicités
 */

function cree_strrechlstper()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBPER . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " per_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " per_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " per_nbj LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des périodes de location
 */

function cree_strrechlstperloc()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBPERLOC . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " perloc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " perloc_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des demandes de retour
 */

function cree_strrechlstretd()
{
    global
    $db;

    $rech = "SELECT " . TBRETD . ".retd_coderetp, " . TBRETD . ".retd_datedde, " . TBRETD . ".retd_daterep, " . TBRETD . ".retd_rma, ";
    $rech .= TBRETD . ".retd_codemque, ";
    $rech .= TBRETP . ".retp_code, " . TBRETP . ".retp_des, " . TBRETP . ".retp_ns, " . TBRETP . ".retp_codeenv, ";
    $rech .= TBMQUE . ".mque_code, " . TBMQUE . ".mque_ste ";
    $rech .= "FROM `" . TBRETD . "`, `" . TBRETP . "`, `" . TBMQUE . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBRETD . ".retd_coderetp LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql(trim($_GET['search']));
            $rech .= " " . TBRETD . ".retd_datedde LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBRETD . ".retd_daterep LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBRETD . ".retd_rma LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_des LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_ns LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_codeenv LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBMQUE . ".mque_ste LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE (" . TBRETD . ".retd_coderetp=" . TBRETP . ".retp_code";
        $rech .= " AND " . TBRETD . ".retd_codemque=" . TBMQUE . ".mque_code)";
    } else {
        $rech .= " AND " . TBRETD . ".retd_coderetp=" . TBRETP . ".retp_code";
        $rech .= " AND " . TBRETD . ".retd_codemque=" . TBMQUE . ".mque_code";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des renvois de produité
 */

function cree_strrechlstrete()
{
    global
    $db;

    $rech = "SELECT " . TBRETE . ".rete_codeage, " . TBRETE . ".rete_code, " . TBRETE . ".rete_dateenv, " . TBRETE . ".rete_codeme, " . TBRETE . ".rete_bord, " . TBRETE . ".rete_clos, ";
    $rech .= TBME . ".me_nom, ";
    $rech .= TBMQUE . ".mque_code, " . TBMQUE . ".mque_ste ";
    $rech .= "FROM `" . TBRETE . "`, `" . TBME . "`, `" . TBMQUE . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBRETE . ".rete_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETE . ".rete_codeage LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBRETE . ".rete_dateenv LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBRETE . ".rete_bord LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBME . ".me_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBMQUE . ".mque_ste LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE ((" . TBRETE . ".rete_codeme=" . TBME . ".me_code";
        $rech .= " OR " . TBRETE . ".rete_codeme='-1')";
        $rech .= " AND " . TBRETE . ".rete_codemque=" . TBMQUE . ".mque_code)";
    } else {
        $rech .= " AND (" . TBRETE . ".rete_codeme=" . TBME . ".me_code";
        $rech .= " OR " . TBRETE . ".rete_codeme='-1')";
        $rech .= " AND " . TBRETE . ".rete_codemque=" . TBMQUE . ".mque_code";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des produits à retourner
 */

function cree_strrechlstretp()
{
    global
    $db;

    $rech = "SELECT " . TBRETP . ".retp_code, " . TBRETP . ".retp_datecrea, " . TBRETP . ".retp_codeage, " . TBRETP . ".retp_des, ";
    $rech .= TBRETP . ".retp_nblf, " . TBRETP . ".retp_ns, " . TBRETP . ".retp_datedde, " . TBRETP . ".retp_dateenv, " . TBRETP . ".retp_dateret, ";
    $rech .= TBRETP . ".retp_codeint, " . TBRETP . ".retp_codeenv, " . TBRETP . ".retp_daterep ";
    $rech .= "FROM `" . TBRETP . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBRETP . ".retp_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBRETP . ".retp_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBRETP . ".retp_datedde LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBRETP . ".retp_daterep LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBRETP . ".retp_dateenv LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBRETP . ".retp_dateret LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBRETP . ".retp_codeage LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_des LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_nblf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_ns LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_codeint LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_codeenv LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des produits à retourner et possédant un RMA pour un constructeur en particulier
 */

function cree_strrechlstretprma()
{
    global
    $db;

    $rech = "SELECT " . TBRETP . ".retp_code, " . TBRETP . ".retp_datecrea, " . TBRETP . ".retp_codeage, " . TBRETP . ".retp_des, ";
    $rech .= TBRETP . ".retp_nblf, " . TBRETP . ".retp_ns, " . TBRETP . ".retp_datedde, " . TBRETP . ".retp_dateenv, " . TBRETP . ".retp_dateret, ";
    $rech .= TBRETP . ".retp_codeint, " . TBRETP . ".retp_codeenv, " . TBRETP . ".retp_daterep ";
    $rech .= "FROM `" . TBRETP . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBRETP . ".retp_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBRETP . ".retp_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBRETP . ".retp_datedde LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBRETP . ".retp_daterep LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBRETP . ".retp_dateenv LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBRETP . ".retp_dateret LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBRETP . ".retp_codeage LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_des LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_nblf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_ns LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_codeint LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_codeenv LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE (" . TBRETP . ".retp_codeenv IS NULL";
        $rech .= " AND " . TBRETP . ".retp_daterep<>'0000-00-00')";
    } else {
        $rech .= " AND " . TBRETP . ".retp_codeenv IS NULL";
        $rech .= " AND " . TBRETP . ".retp_daterep<>'0000-00-00'";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des traitements de retour
 */

function cree_strrechlstretr()
{
    global
    $db,
    $ope_types;

    $rech = "SELECT " . TBRETR . ".retr_coderetp, " . TBRETR . ".retr_dateret, " . TBRETR . ".retr_typeope, " . TBRETR . ".retr_clos, ";
    $rech .= TBRETP . ".retp_code, " . TBRETP . ".retp_des, " . TBRETP . ".retp_ns, " . TBRETP . ".retp_codeenv,  " . TBRETP . ".retp_codeage ";
    $rech .= "FROM `" . TBRETR . "`, `" . TBRETP . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBRETR . ".retr_coderetp LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBRETR . ".retr_dateret LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        foreach ($ope_types as $key => $value) {
            if (!stristr($_GET['search'], $value))
                $rech .= " " . TBRETR . ".retr_typeope LIKE '%" . $key . "%' OR";
        }
        $rech .= " " . TBRETP . ".retp_des LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRETP . ".retp_ns LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE " . TBRETR . ".retr_coderetp=" . TBRETP . ".retp_code";
    } else {
        $rech .= " AND " . TBRETR . ".retr_coderetp=" . TBRETP . ".retp_code";
    }

    // *** Fin de Recherche
    return $rech;

}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des produits
 */

function cree_strrechlstrpdt()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBPDT . "`, `" . TBFAM . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBPDT . ".pdt_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDT . ".pdt_codeb = '" . $db->escape($search) . "' OR";
        $rech .= " " . TBPDT . ".pdt_fam LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDT . ".pdt_des LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDT . ".pdt_detail LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDT . ".pdt_puttc LIKE '%" . $db->escape($search) . "%' OR";
        $search = strtolower($_GET['search']);
        if (($search == _("oui")) OR ($search == _("non"))) {
            if ($search == _("oui")) {
                $rech .= " " . TBPDT . ".pdt_vendu = '1' OR";
            } else {
                $rech .= " " . TBPDT . ".pdt_vendu = '0' OR";
            }
        }
        $rech .= " " . TBFAM . ".fam_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";

    }
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBPDT . ".pdt_fam=" . TBFAM . ".fam_code";
    else
        $rech .= " AND " . TBPDT . ".pdt_fam=" . TBFAM . ".fam_code";
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des produits en saisie intervention
 */

function cree_strrechlstrpdti()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBPDT . "`, `" . TBFAM . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBPDT . ".pdt_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDT . ".pdt_codeb = '" . $db->escape($search) . "' OR";
        $rech .= " " . TBPDT . ".pdt_fam LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDT . ".pdt_des LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDT . ".pdt_detail LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDT . ".pdt_puttc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBFAM . ".fam_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";

    }
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBPDT . ".pdt_fam=" . TBFAM . ".fam_code";
    else
        $rech .= " AND " . TBPDT . ".pdt_fam=" . TBFAM . ".fam_code";
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBPDT . ".pdt_vendu='1'";
    else
        $rech .= " AND " . TBPDT . ".pdt_vendu='1'";
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des produits en location
 */

function cree_strrechlstrpdtl()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBPDTLOC . "`, `" . TBFAM . "`, `" . TBDLOC . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBPDTLOC . ".pdtloc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDTLOC . ".pdtloc_codeb = '" . $db->escape($search) . "' OR";
        $rech .= " " . TBPDTLOC . ".pdtloc_fam LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDTLOC . ".pdtloc_des LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDTLOC . ".pdtloc_ns LIKE '%" . $db->escape($search) . "%' OR";
        $search = strtolower($_GET['search']);
        if (($search == _("oui")) OR ($search == _("non"))) {
            if ($search == _("oui")) {
                $rech .= " " . TBPDTLOC . ".pdtloc_louable = '1' OR";
                $rech .= " " . TBPDTLOC . ".pdtloc_loue = '1' OR";
            } else {
                $rech .= " " . TBPDTLOC . ".pdtloc_louable = '0' OR";
                $rech .= " " . TBPDTLOC . ".pdtloc_loue = '0' OR";
            }
        }
        $rech .= " " . TBFAM . ".fam_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDLOC . ".dloc_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBDLOC . ".dloc_codeage LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";

    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE (" . TBPDTLOC . ".pdtloc_fam=" . TBFAM . ".fam_code";
        $rech .= " AND " . TBPDTLOC . ".pdtloc_iddloc=" . TBDLOC . ".dloc_id)";
    } else {
        $rech .= " AND " . TBPDTLOC . ".pdtloc_fam=" . TBFAM . ".fam_code";
        $rech .= " AND " . TBPDTLOC . ".pdtloc_iddloc=" . TBDLOC . ".dloc_id";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des produits en saisie location
 */

function cree_strrechlstrpdtcl()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBPDTLOC . "`, `" . TBFAM . "`, `" . TBDLOC . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBPDTLOC . ".pdtloc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDTLOC . ".pdtloc_codeb = '" . $db->escape($search) . "' OR";
        $rech .= " " . TBPDTLOC . ".pdtloc_fam LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDTLOC . ".pdtloc_des LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDTLOC . ".pdtloc_ns LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBFAM . ".fam_desc LIKE '%" . $db->escape($search) . "%' OR";
        if (isset($_GET['fi']) && (trim($_GET['fi']) == "0")) {
            $rech .= " " . TBDLOC . ".dloc_nom LIKE '%" . $db->escape($search) . "%' OR";
            $rech .= " " . TBDLOC . ".dloc_codeage LIKE '%" . $db->escape($search) . "%' OR";
        }

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";

    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE (" . TBPDTLOC . ".pdtloc_fam=" . TBFAM . ".fam_code";
        $rech .= " AND " . TBPDTLOC . ".pdtloc_iddloc=" . TBDLOC . ".dloc_id)";
    } else {
        $rech .= " AND " . TBPDTLOC . ".pdtloc_fam=" . TBFAM . ".fam_code";
        $rech .= " AND " . TBPDTLOC . ".pdtloc_iddloc=" . TBDLOC . ".dloc_id";
    }
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBPDTLOC . ".pdtloc_louable='1'";
    else
        $rech .= " AND " . TBPDTLOC . ".pdtloc_louable='1'";
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des historiques produits par intervention
 */

function cree_strrechlstpdtih()
{
    global
    $db;

    $rech = "SELECT " . TBPDTI . ".pdti_codepdt, " . TBPDTI . ".pdti_codeint, " . TBPDTI . ".pdti_qte, " . TBPDTI . ".pdti_puttc, " . TBPDTI . ".pdti_ns, ";
    $rech .= TBINT . ".int_code, " . TBINT . ".int_codeage, " . TBINT . ".int_codecli, " . TBINT . ".int_datedde, " . TBINT . ".int_datefin, " . TBINT . ".int_codestatut, ";
    $rech .= TBPDT . ".pdt_code, " . TBPDT . ".pdt_des, ";
    $rech .= TBCLI . ".cli_code, " . TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, " . TBCLI . ".cli_nom ";
    $rech .= "FROM `" . TBPDTI . "`, `" . TBINT . "`, `" . TBPDT . "`, `" . TBCLI . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " " . TBPDTI . ".pdti_codepdt LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDTI . ".pdti_codeint LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDTI . ".pdti_qte LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDTI . ".pdti_puttc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPDTI . ".pdti_ns LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBINT . ".int_codecli LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBINT . ".int_datedde LIKE '%" . $db->escape($daterech) . "%' OR";
            $daterech = datetomysql(trim($_GET['search']));
            $rech .= " " . TBINT . ".int_datefin LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBPDT . ".pdt_des LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBPDTI . ".pdti_codeint=" . TBINT . ".int_code";
    else
        $rech .= " AND " . TBPDTI . ".pdti_codeint=" . TBINT . ".int_code";
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBPDTI . ".pdti_codepdt=" . TBPDT . ".pdt_code";
    else
        $rech .= " AND " . TBPDTI . ".pdti_codepdt=" . TBPDT . ".pdt_code";
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBINT . ".int_codecli=" . TBCLI . ".cli_code";
    else
        $rech .= " AND " . TBINT . ".int_codecli=" . TBCLI . ".cli_code";
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des unités de vente
 */

function cree_strrechlstpdtu()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBPDTU . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " pdtu_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " pdtu_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " pdtu_abr LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des pénalités (locations)
 */

function cree_strrechlstpen()
{
    global
    $db,
    $lng_tduree,
    $non_oui;

    $rech = "SELECT * FROM `" . TBPEN . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " pen_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " pen_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " pen_valeur LIKE '%" . $db->escape($search) . "%' OR";
        $search = strtolower($_GET['search']);
        foreach ($lng_tduree as $key => $value) {
            if (stristr($value, $search))
                $rech .= " pen_udec = '" . $key . "' OR";
        }
        if (stristr("Grille de valeurs (%)", $search))
            $rech .= " pen_tbar = '1' OR";
        else
            $rech .= " pen_tbar = '0' OR";
        if (stristr("%", $search))
            $rech .= " pen_pour = '1' OR";
        else
            $rech .= " pen_pour = '0' OR";
        foreach ($non_oui as $key => $value) {
            if (stristr($value, $search))
                $rech .= " pen_asaisir = '" . $key . "' OR";
        }

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des règlements
 */

function cree_strrechlstreg()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBREG . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " reg_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " reg_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des statuts
 */

function cree_strrechlststa()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBSTA . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " statut_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " statut_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des tâches
 */

function cree_strrechlsttac()
{
    global
    $db,
    $admin_code;

    $rech = "SELECT " . TBTAC . ".tac_code, " . TBTAC . ".tac_datecrea, " . TBTAC . ".tac_codecrea, " . TBTAC . ".tac_datefinp, " . TBTAC . ".tac_datefin, ";
    $rech .= TBTAC . ".tac_typdest, " . TBTAC . ".tac_codedest, " . TBTAC . ".tac_typrat, " . TBTAC . ".tac_coderat, ";
    $rech .= TBTAC . ".tac_urg, " . TBTAC . ".tac_codetacc, " . TBTAC . ".tac_avance, " . TBTAC . ".tac_datefin, " . TBTAC . ".tac_titre, " . TBTAC . ".tac_afaire, ";
    $rech .= TBTACC . ".tacc_code, " . TBTACC . ".tacc_desc, ";
    $rech .= TBURG . ".urg_des ";
    $rech .= "FROM `" . TBTAC . "`, `" . TBTACC . "`, `" . TBURG . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBTAC . ".tac_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBTAC . ".tac_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBTAC . ".tac_datefinp LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        if (stristr("Centre de gestion", $_GET['search'])) {
            $rech .= " " . TBTAC . ".tac_codecrea LIKE 'CGE' OR";
            $rech .= " " . TBTAC . ".tac_codedest LIKE 'CGE' OR";
        }
        if (stristr($admin_code, $_GET['search'])) {
            $rech .= " " . TBTAC . ".tac_codecrea LIKE '" . $admin_code . "' OR";
            $rech .= " " . TBTAC . ".tac_codedest LIKE '" . $admin_code . "' OR";
        }
        $rech .= " " . TBTAC . ".tac_codecrea LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTAC . ".tac_codedest LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTAC . ".tac_coderat LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTAC . ".tac_avance LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTAC . ".tac_titre LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTAC . ".tac_afaire LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTACC . ".tacc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTACC . ".tacc_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBURG . ".urg_des LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE " . TBTAC . ".tac_codetacc=" . TBTACC . ".tacc_code";
        $rech .= " AND " . TBTAC . ".tac_urg=" . TBURG . ".urg_niveau";
    } else {
        $rech .= " AND " . TBTAC . ".tac_codetacc=" . TBTACC . ".tacc_code";
        $rech .= " AND " . TBTAC . ".tac_urg=" . TBURG . ".urg_niveau";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des catégories de tâches
 */

function cree_strrechlsttacc()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBTACC . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " tacc_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " tacc_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des techniciens
 *
 * @param $niv_util
 * @return string
 */

function cree_strrechlsttec($niv_util)
{
    global
    $db;

    $rech = "SELECT 
				tec_congd,
				tec_congf,
				tec_codeage,
				tec_codedo,
				tec_codest,
				tec_code,
				tec_prenom,
				tec_nom,
				tec_dep,
				tec_telf,
				tec_telp,
				tec_email,
				tec_out,
				tec_dateout
			 FROM `" . TBTEC . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " tec_congd LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " tec_congf LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " tec_dateout LIKE '%" . $db->escape($daterech) . "%' OR";
        }

        switch ($niv_util) {
            case 4 :
                $rech .= " tec_codeage LIKE '%" . $db->escape($search) . "%' OR";
                break;
            case 6 :
                $rech .= " tec_codedo LIKE '%" . $db->escape($search) . "%' OR";
                break;
            case 10 :
                $rech .= " tec_codest LIKE '%" . $db->escape($search) . "%' OR";
                break;
            default :
                $rech .= " tec_codeage LIKE '%" . $db->escape($search) . "%' OR";
                $rech .= " tec_codedo LIKE '%" . $db->escape($search) . "%' OR";
                $rech .= " tec_codest LIKE '%" . $db->escape($search) . "%' OR";
                break;
        }
        $rech .= " tec_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " tec_prenom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " tec_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " tec_telf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " tec_telp LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " tec_email LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des traçabilités
 *
 * @param $tx
 * @return string
 */

// $tx : type liste ('r' : depuis pièce rattachée / autre : classique)

function cree_strrechlsttra($tx)
{
    global
    $db;

    switch ($tx) {
        case 'r' :
            $rech = "SELECT " . TBTRA . ".tra_id, " . TBTRA . ".tra_date, " . TBTRA . ".tra_heure, ";
            $rech .= TBTRA . ".tra_codeta, " . TBTRA . ".tra_codeutil, " . TBTRA . ".tra_titre, " . TBTRA . ".tra_desc, ";
            $rech .= TBTA . ".ta_code, " . TBTA . ".ta_desc ";
            $rech .= "FROM `" . TBTRA . "`, `" . TBTA . "`";
            break;
        default :
            $rech = "SELECT " . TBTRA . ".tra_id, " . TBTRA . ".tra_coderat, " . TBTRA . ".tra_date, " . TBTRA . ".tra_heure, ";
            $rech .= TBTRA . ".tra_codeta, " . TBTRA . ".tra_codeutil, " . TBTRA . ".tra_titre, " . TBTRA . ".tra_desc, ";
            $rech .= TBTA . ".ta_code, " . TBTA . ".ta_desc ";
            $rech .= "FROM `" . TBTRA . "`, `" . TBTA . "`";
            break;
    }

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        switch ($tx) {
            case 'r' :
                break;
            default :
                $rech .= TBTRA . ".tra_coderat LIKE '%" . $db->escape($search) . "%' OR";
                break;
        }
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBTRA . ".tra_date LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBTRA . ".tra_heure LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTRA . ".tra_codeta LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTRA . ".tra_codeutil LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTRA . ".tra_titre LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTRA . ".tra_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTA . ".ta_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }

    // WHERE Trouvé ?
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE (";
    else
        $rech .= " AND (";

    $rech .= TBTRA . ".tra_codeta=" . TBTA . ".ta_code";
    $rech .= ")";

    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des textes types*
 */

function cree_strrechlsttxt()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBTXT . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " txt_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " txt_entete LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " txt_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " txt_texte LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des niveaux d'urgence
 */

function cree_strrechlsturg()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBURG . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " urg_niveau LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " urg_des LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des patrimoines
 */

function cree_strrechlstrpat()
{
    global
    $db;

    $rech = "SELECT " . TBRPAT . ".rpat_code, " . TBRPAT . ".rpat_datecrea, " . TBRPAT . ".rpat_datemod, " . TBRPAT . ".rpat_codecli, " . TBRPAT . ".rpat_codeage, ";
    $rech .= TBCLI . ".cli_code, " . TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, " . TBCLI . ".cli_nom ";
    $rech .= "FROM `" . TBRPAT . "`, `" . TBCLI . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBRPAT . ".rpat_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBRPAT . ".rpat_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBRPAT . ".rpat_datemod LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBRPAT . ".rpat_codecli LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRPAT . ".rpat_codeage LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBRPAT . ".rpat_codecli=" . TBCLI . ".cli_code";
    else
        $rech .= " AND " . TBRPAT . ".rpat_codecli=" . TBCLI . ".cli_code";
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des catégories matériels pour les relevés
 */

function cree_strrechlstrrpcat()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBRPCAT . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " rpcat_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " rpcat_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des états pour les relevés
 */

function cree_strrechlstrrpet()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBRPET . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " rpet_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " rpet_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des types d'accès pour les relevés
 */

function cree_strrechlstrrlptmta()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBRLPTMTA . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " rlptmta_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " rlptmta_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des logiciels de télémaintenance pour les relevés*
 */

function cree_strrechlstrrlptml()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBRLPTML . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " rlptml_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " rlptml_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des modes de mises à jour pour les relevés
 */

function cree_strrechlstrrlpmm()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBRLPMM . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " rlpmm_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " rlpmm_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des marques
 */

function cree_strrechlstrrlpma()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBMQUE . "`, `" . TBMQUET . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBMQUE . ".mque_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= TBMQUE . ".mque_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= TBMQUET . ".mquet_desc LIKE '%" . $db->escape($search) . "%' OR";
        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // Jointure
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE (" . TBMQUE . ".mque_codemquet = " . TBMQUET . ".mquet_code)";
    else
        $rech .= " AND " . TBMQUE . ".mque_codemquet = " . TBMQUET . ".mquet_code";

    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des marques (uniquement si retour en attente)
 */

function cree_strrechlstrrlpmar()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBMQUE . "`, `" . TBMQUET . "`,`" . TBRETP . "`, `" . TBRETD . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBMQUE . ".mque_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= TBMQUE . ".mque_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= TBMQUET . ".mquet_desc LIKE '%" . $db->escape($search) . "%' OR";
        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // Jointure
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE (" . TBMQUE . ".mque_codemquet = " . TBMQUET . ".mquet_code)";
    else
        $rech .= " AND " . TBMQUE . ".mque_codemquet = " . TBMQUET . ".mquet_code";

    if (!stristr($rech, "WHERE ")) {
        $rech .= " WHERE (" . TBRETP . ".retp_codeenv IS NULL";
        $rech .= " AND " . TBRETD . ".retd_rma<>''";
        $rech .= " AND " . TBRETD . ".retd_rma IS NOT NULL";
        $rech .= " AND " . TBRETP . ".retp_code=" . TBRETD . ".retd_coderetp";
        $rech .= " AND " . TBRETD . ".retd_codemque=" . TBMQUE . ".mque_code)";
    } else {
        $rech .= " AND " . TBRETP . ".retp_codeenv IS NULL";
        $rech .= " AND " . TBRETD . ".retd_rma<>''";
        $rech .= " AND " . TBRETD . ".retd_rma IS NOT NULL";
        $rech .= " AND " . TBRETP . ".retp_code=" . TBRETD . ".retd_coderetp";
        $rech .= " AND " . TBRETD . ".retd_codemque=" . TBMQUE . ".mque_code";
    }

    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des relevés
 */

function cree_strrechlstrrel()
{
    global
    $db;

    $rech = "SELECT " . TBREL . ".rel_code, " . TBREL . ".rel_datecrea, " . TBREL . ".rel_datemod, " . TBREL . ".rel_codecli, " . TBREL . ".rel_codeage, ";
    $rech .= TBCLI . ".cli_code, " . TBCLI . ".cli_ste, " . TBCLI . ".cli_civilite, " . TBCLI . ".cli_nom ";
    $rech .= "FROM `" . TBREL . "`, `" . TBCLI . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBREL . ".rel_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBREL . ".rel_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBREL . ".rel_datemod LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= " " . TBREL . ".rel_codecli LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBREL . ".rel_codeage LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_ste LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_civilite LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBCLI . ".cli_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBREL . ".rel_codecli=" . TBCLI . ".cli_code";
    else
        $rech .= " AND " . TBREL . ".rel_codecli=" . TBCLI . ".cli_code";
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des modèles de réplication
 */

function cree_strrechlstrreplm()
{
    global
    $db,
    $typ_repli;

    $rech = "SELECT " . TBREPLM . ".replm_nom, " . TBREPLM . ".replm_datecrea, " . TBREPLM . ".replm_datemod, " . TBREPLM . ".replm_typ, ";
    $rech .= TBREPLM . ".replm_id ";
    $rech .= "FROM `" . TBREPLM . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBREPLM . ".rel_code LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate($_GET['search']) == TRUE) {
            $daterech = datetomysql($_GET['search']);
            $rech .= " " . TBREPLM . ".replm_datecrea LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= " " . TBREPLM . ".replm_datemod LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        foreach ($typ_repli as $key => $value) {
            if (stristr($value, $search))
                $rech .= " replm_typ = '" . $key . "' OR";
        }

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des types de licences pour les relevés
 */

function cree_strrechlstrrltl()
{
    global
    $db;

    $rech = "SELECT " . TBRLTL . ".rltl_code, " . TBRLTL . ".rltl_desc ";
    $rech .= "FROM `" . TBRLTL . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " " . TBRLTL . ".rltl_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRLTL . ".rltl_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des types de médias pour les relevés
 */

function cree_strrechlstrrlmed()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBRLMED . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " rlmed_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " rlmed_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des objets pour les patrimoines
 */

function cree_strrechlstrrobj()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBROBJ . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " " . TBROBJ . ".robj_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBROBJ . ".robj_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des types d'acquisitions pour les patrimoines
 */

function cree_strrechlstrrpta()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBRTA . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " " . TBRTA . ".rta_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRTA . ".rta_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des types d'objets pour les patrimoines
 */

function cree_strrechlstrrpto()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBRTO . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " " . TBRTO . ".rto_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBRTO . ".rto_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des spécialités
 */

function cree_strrechlstsp()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBSP . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " sp_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " sp_desc LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des sous-traitants
 */

function cree_strrechlstst()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBST . "`, `" . TBVILLES . "`, `" . TBPAYS . "`";
    $rech .= " WHERE " . TBST . ".st_ville=" . TBVILLES . ".villes_id AND " . TBST . ".st_codepays=" . TBPAYS . ".pays_code";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBST . ".st_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_nom LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_telf LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_ville LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_codepays LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_email LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBST . ".st_spe LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBVILLES . ".villes_cp LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBPAYS . ".pays_nom LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche

    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des Taxes Parafiscales
 */

function cree_strrechlsttpf()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBTPF . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // WHERE existe ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= TBTPF . ".tpf_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " " . TBTPF . ".tpf_desc LIKE '%" . $db->escape($search) . "%' OR";
        $search = strtolower($_GET['search']);
        if (($search == _("oui")) OR ($search == _("non"))) {
            if ($search == _("oui")) {
                $rech .= " " . TBTPF . ".tpf_pour = '1' OR";
                $rech .= " " . TBTPF . ".tpf_asaisir = '1' OR";
            } else {
                $rech .= " " . TBTPF . ".tpf_pour = '0' OR";
                $rech .= " " . TBTPF . ".tpf_asaisir = '0' OR";
            }
        }
        $rech .= " " . TBTPF . ".tpf_valeur LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche
    return $rech;
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des TVA
 */

function cree_strrechlsttva()
{
    global
    $db;

    $rech = "SELECT * FROM `" . TBTVA . "`";

    // *** Recherche
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $search = clean_str($_GET['search']);

        // Trouvé ?
        if (!stristr($rech, "WHERE "))
            $rech .= " WHERE (";
        else
            $rech .= " AND (";

        // Construction de la chaîne de recherche
        $rech .= " tva_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " tva_desc LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= " tva_tx LIKE '%" . $db->escape($search) . "%' OR";

        // Suppression du dernier 'OR'
        $rech = substr($rech, 0, strlen($rech) - 3);
        $rech .= ")";
    }
    // *** Fin de Recherche
    return $rech;
}
?> 
