<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.4
 *
 * Copyright (C) 2006-2016  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) {
    header('Location: ../index.php');
}

/**
 * Création de la chaîne de recherche
 * des listes écrans et impressions des interventions
 * $qte : si TRUE, on ne retourne que la quantité (COUNT)
 *
 * @param $nivutil
 * @param bool $qte
 * @param bool $export
 * @return string
 */

function cree_strrechlstint($nivutil, $qte = FALSE, $export = FALSE)
{
    global
    $db,
    $_SESSION,
    $_GET;

    if ($qte == FALSE) {
        $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 ($export == TRUE) {
            $rech .= TBINT . '.int_fait, ';
        }
        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 ';
    } else {
        $rech = 'SELECT COUNT(*) ';
    }
    if ($nivutil <> 1) {
        $rech .= 'FROM `' . TBINT . '`, `' . TBCLI . '`, `' . TBSTA . '`, `' . TBINTC . '`';
    } else {
        $rech .= 'FROM `' . TBINT . '`, `' . TBSTA . '`, `' . TBINTC . '`';
    }

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

        // trouvé ?
        if (!stristr($rech, 'WHERE ')) {
            $rech .= ' WHERE (';
        } else {
            $rech .= ' AND (';
        }

        // Construction de la chaîne de recherche
        $rech .= TBINT . ".int_code LIKE '%" . $db->escape($search) . "%' OR";
        if ((int)trim($_SESSION['niv_util']) <> 1) {
            $rech .= ' ' . TBINT . ".int_codecat LIKE '%" . $db->escape($search) . "%' OR";
            $rech .= ' ' . TBINTC . ".intc_code LIKE '%" . $db->escape($search) . "%' OR";
            $rech .= ' ' . TBINTC . ".intc_desc LIKE '%" . $db->escape($search) . "%' OR";
        }
        $rech .= ' ' . TBINT . ".int_codeman LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= ' ' . TBINT . ".int_codeocli LIKE '%" . $db->escape($search) . "%' OR";
        if (chkdate(trim($_GET['search'])) == TRUE) {
            $daterech = datetomysql(trim($_GET['search']));
            $rech .= ' ' . TBINT . ".int_datedde LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= ' ' . TBINT . ".int_daterdv LIKE '%" . $db->escape($daterech) . "%' OR";
            $rech .= ' ' . TBINT . ".int_datefinp LIKE '%" . $db->escape($daterech) . "%' OR";
        }
        $rech .= ' ' . TBINT . ".int_codeage LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= ' ' . TBINT . ".int_codelieu LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= ' ' . TBINT . ".int_codetec LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= ' ' . TBINT . ".int_codestatut LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= ' ' . TBINT . ".int_mat LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= ' ' . TBINT . ".int_pbm LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= ' ' . TBINT . ".int_hrdv LIKE '%" . $db->escape($search) . "%' OR";
        if ($nivutil <> 1) {
            $rech .= ' ' . TBINT . ".int_codedo LIKE '%" . $db->escape($search) . "%' OR";
            $rech .= ' ' . TBINT . ".int_codeext LIKE '%" . $db->escape($search) . "%' OR";
            $rech .= ' ' . TBINT . ".int_codecli 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 .= ' ' . TBSTA . ".statut_code LIKE '%" . $db->escape($search) . "%' OR";
        $rech .= ' ' . TBSTA . ".statut_desc LIKE '%" . $db->escape($search) . "%' OR";

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

    $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;
    }

    $_GET = clean_str($_GET);

    // Filtres complémentaires
    switch ($_SESSION['niv_log']) {
        case 1 :
            break;
        default :
            if (isset($_GET['cat'])) {
                $cat = trim($_GET['cat']);
                if ($cat <> '') {
                    $rech .= ' AND ' . TBINT . ".int_codecat = '" . $db->escape($cat) . "'";
                }
            }
            if (isset($_GET['urg'])) {
                $urg = trim($_GET['urg']);
                if ($urg <> '') {
                    $rech .= ' AND ' . TBINT . ".int_urg = '" . $db->escape($urg) . "'";
                }
            }
            if (isset($_GET['dateddef1'])) {
                if (datetolocal($_GET['dateddef1']) <> '') {
                    $datefiltre = datetomysql($_GET['dateddef1']);
                    $rech .= ' AND ' . TBINT . ".int_datedde >= '" . $db->escape($datefiltre) . "'";
                }
            }
            if (isset($_GET['dateddef2'])) {
                if (datetolocal($_GET['dateddef2']) <> '') {
                    $datefiltre = datetomysql($_GET['dateddef2']);
                    $rech .= ' AND ' . TBINT . ".int_datedde <= '" . $db->escape($datefiltre) . "'";
                }
            }
            if (isset($_GET['dateddefp1'])) {
                if (datetolocal($_GET['dateddefp1']) <> '') {
                    $datefiltre = datetomysql($_GET['dateddefp1']);
                    $rech .= ' AND ' . TBINT . ".int_dateprev >= '" . $db->escape($datefiltre) . "'";
                }
            }
            if (isset($_GET['dateddefp2'])) {
                if (datetolocal($_GET['dateddefp2']) <> '') {
                    $datefiltre = datetomysql($_GET['dateddefp2']);
                    $rech .= ' AND ' . TBINT . ".int_dateprev <= '" . $db->escape($datefiltre) . "'";
                }
            }
            break;
    }

    if ($where_mode == true) {
        $rech .= ')';
    }

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

/**
 * Retourne une chaine représentant la recherche à effectuer sur un affichage de liste
 * $qte : si TRUE, on ne retourne que la quantité (COUNT)
 * $export : si TRUE, on rajoute les champs complémentaires à l'export (et non présents dans la liste)
 *
 * @param bool $qte
 * @param bool $export
 * @return string
 */
function cree_strrechlstintfi($qte = FALSE, $export = FALSE)
{
    global
    $mod_planning,
    $fi,
    $tab_cli,
    $tab_int,
    $datea,
    $_SESSION;

    $rech = cree_strrechlstint($_SESSION['niv_log'], $qte, $export);
    switch ($_SESSION['niv_log']) {
        case 1 :
            if (count($tab_cli) <= 1) {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ';
                } else {
                    $rech .= ' AND ';
                }
                $rech .= ' ' . TBINT . ".int_codecli='" . $_SESSION['code_log'] . "'";
                if ((int)$_SESSION['id_ccli'] > 0) {
                    $rech .= ' AND ';
                    $rech .= ' ' . TBINT . ".int_ddeur='" . $_SESSION['id_ccli'] . "'";
                }
            } else {
                // On rajoute les comptes complémentaires (cas des regroupements de clients)
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ';
                } else {
                    $rech .= ' AND ';
                }
                $rech .= '(';
                foreach ($tab_cli as $value) {
                    // Si session contact client, on ne prend que les interventions du contact
                    if ((int)$_SESSION['id_ccli'] > 0) {
                        $rech .= ' (';
                    }
                    $rech .= ' ' . TBINT . ".int_codecli='" . $value . "'";
                    if ((int)$_SESSION['id_ccli'] > 0) {
                        $rech .= ' AND ' . TBINT . ".int_ddeur='" . $_SESSION['id_ccli'] . "')";
                    }
                    $rech .= ' OR';
                }
                // Suppression du dernier 'OR'
                $rech = substr($rech, 0, -3);

                $rech .= ')';
            }
            break;
        default :
            switch ($_SESSION['niv_log']) {
                // Techniciens
                case 3 :
                    if (count($tab_int) > 0) {
                        if (!stristr($rech, 'WHERE ')) {
                            $rech .= ' WHERE ';
                        } else {
                            $rech .= ' AND ';
                        }
                        if (count($tab_int) > 1) {
                            $rech .= '(';
                        }

                        foreach ($tab_int as $value) {
                            $rech .= ' ' . TBINT . ".int_code='" . $value . "' OR";
                        }

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

                        if (count($tab_int) > 1) {
                            $rech .= ')';
                        }
                    }
                    break;
                // Agences
                case 4 :
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= ' WHERE (' . TBINT . ".int_codeage='" . valsession('code_age') . "' OR " . TBINT . ".int_codeages='" . valsession('code_age') . "'";
                    } else {
                        $rech .= ' AND (' . TBINT . ".int_codeage='" . valsession('code_age') . "' OR " . TBINT . ".int_codeages='" . valsession('code_age') . "'";
                    }

                    // On rajoute les interventions non affectées à une agence
                    $rech .= ' OR ' . TBINT . ".int_codeage='')";
                    break;

                // Donneurs d'Ordres et leurs Techniciens
                case 6 :
                case 7 :
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= ' WHERE ' . TBINT . ".int_codedo='" . valsession('code_do') . "'";
                    } else {
                        $rech .= ' AND ' . TBINT . ".int_codedo='" . valsession('code_do') . "'";
                    }
                    break;
                default :
                    break;
            }
            break;
    }

    switch ($fi) {
        case '1' :
            if (!stristr($rech, 'WHERE ')) {
                $rech .= ' WHERE ' . TBINT . ".int_datefin='0000-00-00'";
            } else {
                $rech .= ' AND ' . TBINT . ".int_datefin='0000-00-00'";
            }
            break;
        case '2' :
            if (!stristr($rech, 'WHERE ')) {
                $rech .= ' WHERE ' . TBINT . ".int_datefin<>'0000-00-00'";
            } else {
                $rech .= ' AND ' . TBINT . ".int_datefin<>'0000-00-00'";
            }
            break;
        case '3' :
            if (!stristr($rech, 'WHERE ')) {
                $rech .= ' WHERE ' . TBINT . ".int_dateprev<>'0000-00-00' AND " . TBINT . ".int_datefin='0000-00-00'";
            } else {
                $rech .= ' AND ' . TBINT . ".int_dateprev<>'0000-00-00' AND " . TBINT . ".int_datefin='0000-00-00'";
            }
            break;
        case '4' :
            if (valsession('niv_log') <> 1) {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE (' . TBINT . ".int_codetec='-1' OR " . TBINT . ".int_codetec='')";
                } else {
                    $rech .= ' AND (' . TBINT . ".int_codetec='-1' OR " . TBINT . ".int_codetec='')";
                }
            }
            break;
        case '5' :
            if (valsession('niv_log') <> 1) {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ' . TBINT . ".int_codeage=''";
                } else {
                    $rech .= ' AND ' . TBINT . ".int_codeage=''";
                }
            }
            break;
        case '6' :
            if (valsession('niv_log') <> 1) {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ' . TBINT . ".int_fact='0'";
                } else {
                    $rech .= ' AND ' . TBINT . ".int_fact='0'";
                }
            }
            break;
        case '7' :
            if (valsession('niv_log') <> 1) {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ' . TBINT . ".int_fact='0' AND " . TBINT . ".int_dateprev<>'0000-00-00' AND " . TBINT . ".int_datefin='0000-00-00'";
                } else {
                    $rech .= ' AND ' . TBINT . ".int_fact='0' AND " . TBINT . ".int_dateprev<>'0000-00-00' AND " . TBINT . ".int_datefin='0000-00-00'";
                }
            }
            break;
        case '8' :
            if (valsession('niv_log') <> 1) {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ' . TBINT . ".int_fact='0' AND " . TBINT . ".int_datefin<>'0000-00-00'";
                } else {
                    $rech .= ' AND ' . TBINT . ".int_fact='0' AND " . TBINT . ".int_datefin<>'0000-00-00'";
                }
            }
            break;
        case '9' :
            if (valsession('niv_log') <> 1) {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ' . TBINT . ".int_fact='1'";
                } else {
                    $rech .= ' AND ' . TBINT . ".int_fact='1'";
                }
            }
            break;
        case '10' :
            if (valsession('niv_log') <> 1) {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ' . TBINT . ".int_fact='1' AND " . TBINT . ".int_dateprev<>'0000-00-00' AND " . TBINT . ".int_datefin='0000-00-00'";
                } else {
                    $rech .= ' AND ' . TBINT . ".int_fact='1' AND " . TBINT . ".int_dateprev<>'0000-00-00' AND " . TBINT . ".int_datefin='0000-00-00'";
                }
            }
            break;
        case '11' :
            if (valsession('niv_log') <> 1) {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ' . TBINT . ".int_fact='1' AND " . TBINT . ".int_datefin<>'0000-00-00'";
                } else {
                    $rech .= ' AND ' . TBINT . ".int_fact='1' AND " . TBINT . ".int_datefin<>'0000-00-00'";
                }
            }
            break;
        case '12' :
        case '13' :
            if (valsession('niv_log') <> 1) {
                if ($fi == '12') {
                    $date2 = nouv_date_plus_jours($datea, 7);
                } else {
                    $date2 = nouv_date_plus_jours($datea, 14);
                }
                /*
                if(!stristr($rech,"WHERE "))
                    $rech .= " WHERE (".TBINT.".int_datedde>='".$datea."' AND ".TBINT.".int_datedde<='".$date2."')";
                else
                    $rech .= " AND (".TBINT.".int_datedde>='".$datea."' AND ".TBINT.".int_datedde<='".$date2."')";
                */

                // On trouve les RDV supérieurs ou égaux à la date du jour
                // et inférieur à la date butoir
                $rech2 = 'SELECT rdv_coderat FROM `' . TBRDV . '`';
                $rech2 .= ' WHERE rdv_typrat=1';
                $rech2 .= " AND rdv_date>='" . $datea . "' AND rdv_date<='" . $date2 . "'";

                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ' . TBINT . '.int_code IN (' . $rech2 . ')';
                } else {
                    $rech .= ' AND ' . TBINT . '.int_code IN (' . $rech2 . ')';
                }
            }
            break;
        case '14' :
            if (valsession('niv_log') <> 1) {
                $date2 = nouv_date_plus_jours($datea, 2);
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE (' . TBINT . ".int_datefinp>='" . $datea . "' AND " . TBINT . ".int_datefinp<='" . $date2 . "' AND " . TBINT . ".int_datefinp<>'0000-00-00' AND " . TBINT . ".int_dateprev='0000-00-00')";
                } else {
                    $rech .= ' AND (' . TBINT . ".int_datefinp>='" . $datea . "' AND " . TBINT . ".int_datefinp<='" . $date2 . "' AND " . TBINT . ".int_datefinp<>'0000-00-00' AND " . TBINT . ".int_dateprev='0000-00-00')";
                }
            }
            break;
        case '15' :
            if (valsession('niv_log') <> 1) {
                $date2 = nouv_date_plus_jours($datea, 7);
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE (' . TBINT . ".int_datefinp>='" . $datea . "' AND " . TBINT . ".int_datefinp<='" . $date2 . "' AND " . TBINT . ".int_datefinp<>'0000-00-00' AND " . TBINT . ".int_dateprev='0000-00-00')";
                } else {
                    $rech .= ' AND (' . TBINT . ".int_datefinp>='" . $datea . "' AND " . TBINT . ".int_datefinp<='" . $date2 . "' AND " . TBINT . ".int_datefinp<>'0000-00-00' AND " . TBINT . ".int_dateprev='0000-00-00')";
                }
            }
            break;
        case '16' :
            if (valsession('niv_log') <> 1) {
                $date2 = nouv_date_plus_jours($datea, 15);
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE (' . TBINT . ".int_datefinp>='" . $datea . "' AND " . TBINT . ".int_datefinp<='" . $date2 . "' AND " . TBINT . ".int_datefinp<>'0000-00-00' AND " . TBINT . ".int_dateprev='0000-00-00')";
                } else {
                    $rech .= ' AND (' . TBINT . ".int_datefinp>='" . $datea . "' AND " . TBINT . ".int_datefinp<='" . $date2 . "' AND " . TBINT . ".int_datefinp<>'0000-00-00' AND " . TBINT . ".int_dateprev='0000-00-00')";
                }
            }
            break;
        case '17' :
            if (valsession('niv_log') <> 1) {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE (' . TBINT . ".int_datefinp<'" . $datea . "'  AND " . TBINT . ".int_datefinp<>'0000-00-00' AND " . TBINT . ".int_dateprev='0000-00-00')";
                } else {
                    $rech .= ' AND (' . TBINT . ".int_datefinp<'" . $datea . "'  AND " . TBINT . ".int_datefinp<>'0000-00-00' AND " . TBINT . ".int_dateprev='0000-00-00')";
                }
            }
            break;
        case '18' :
            if (valsession('niv_log') <> 1) {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ' . TBINT . ".int_dateprev='0000-00-00' AND " . TBINT . ".int_datefin='0000-00-00'";
                } else {
                    $rech .= ' AND ' . TBINT . ".int_dateprev='0000-00-00' AND " . TBINT . ".int_datefin='0000-00-00'";
                }

                $rech2 = 'SELECT rdv_coderat FROM `' . TBRDV . '`';
                $rech2 .= ' WHERE rdv_typrat=1';

                $rech .= ' AND ' . TBINT . '.int_code NOT IN (' . $rech2 . ')';
            }
            break;
        case '99' :
            // On vérifie si le module planning est bien en place
            if ($mod_planning == TRUE) {
                if (valsession('niv_log') <> 1) {
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= ' WHERE (' . TBINT . ".int_dateprev='0000-00-00' AND " . TBINT . ".int_datefin='0000-00-00')";
                    } else {
                        $rech .= ' AND (' . TBINT . ".int_dateprev='0000-00-00' AND " . TBINT . ".int_datefin='0000-00-00')";
                    }

                    $rech2 = 'SELECT pevt_coderat FROM `' . TBPEVT . '`';
                    $rech2 .= ' WHERE pevt_typrat=1';

                    $rech .= ' AND ' . TBINT . '.int_code NOT IN (' . $rech2 . ')';
                }
            }
            break;
        case '100' :
        case '101' :
            // On vérifie si le module planning est bien en place
            if ($mod_planning == TRUE) {
                if (valsession('niv_log') <> 1) {
                    if ($fi == '100') {
                        $date2 = nouv_date_plus_jours($datea, 7);
                    } else {
                        $date2 = nouv_date_plus_jours($datea, 14);
                    }
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= ' WHERE (' . TBINT . ".int_datefinp>='" . $datea . "' AND " . TBINT . ".int_datefinp<='" . $date2 . "' AND " . TBINT . ".int_datefinp<>'0000-00-00' AND " . TBINT . ".int_dateprev='0000-00-00')";
                    } else {
                        $rech .= ' AND (' . TBINT . ".int_datefinp>='" . $datea . "' AND " . TBINT . ".int_datefinp<='" . $date2 . "' AND " . TBINT . ".int_datefinp<>'0000-00-00' AND " . TBINT . ".int_dateprev='0000-00-00')";
                    }
                    $rech .= ' AND (' . TBINT . ".int_dateprev='0000-00-00' AND " . TBINT . ".int_datefin='0000-00-00')";

                    $rech2 = 'SELECT pevt_coderat FROM `' . TBPEVT . '`';
                    $rech2 .= ' WHERE pevt_typrat=1';

                    $rech .= ' AND ' . TBINT . '.int_code NOT IN (' . $rech2 . ')';
                }
            }
            break;
        default :
            break;
    }

    return $rech;
}

/**
 * Retourne une chaine représentant la recherche à effectuer sur un affichage de liste sur la base des filtres complémentaires
 * $restreint : si TRUE, on restreint à $limit
 *
 * @param $rech
 * @param bool $restreint
 * @return string
 */
function cree_strrechlstintfi2($rech, $restreint = FALSE)
{
    global
    $mod_planning,
    $limit,
    $db,
    $fi,
    $datea,
    $_SESSION;

    $tab_intlst = [];

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

    $next_row = TRUE;
    $nrow = 0;

    if (($fi == '12') OR ($fi == '13') OR ($fi == '18') OR ($fi == '99') OR ($fi == '100') OR ($fi == '101')) {
        while ($next_row == TRUE) {
            $row = $db->get_row($rech, ARRAY_A, $nrow);
            if ($row) {
                extract($row);

                $enr_ok = TRUE;

                switch ($fi) {
                    case '12' :
                    case '13' :
                        if (valsession('niv_log') <> 1) {
                            if ($fi == '12') {
                                $date2 = nouv_date_plus_jours($datea, 7);
                            } else {
                                $date2 = nouv_date_plus_jours($datea, 14);
                            }
                            // On trouve les RDV supérieurs ou égaux à la date du jour
                            // et inférieur à la date butoir
                            $rech2 = 'SELECT * FROM `' . TBRDV . '`';
                            $rech2 .= " WHERE rdv_typrat=1 AND rdv_coderat='" . $int_code . "'";
                            $rech2 .= " AND rdv_date>='" . $datea . "' AND rdv_date<='" . $date2 . "'";
                            $rows2 = $db->get_results($rech2, ARRAY_A);
                            if (count($rows2) > 0) {
                                $enr_ok = FALSE;
                            }
                        }
                        break;
                    case '18' :
                        if (valsession('niv_log') <> 1) {
                            $rech2 = 'SELECT * FROM `' . TBRDV . '`';
                            $rech2 .= " WHERE rdv_typrat=1 AND rdv_coderat='" . $int_code . "'";
                            $rows2 = $db->get_results($rech2, ARRAY_A);
                            if (count($rows2) > 0) {
                                $enr_ok = FALSE;
                            }
                        }
                        break;
                    case '99' :
                        if ($mod_planning == TRUE) {
                            if (valsession('niv_log') <> 1) {
                                // On vérifie si intervention non planifiée
                                $rech2 = 'SELECT rdv_id FROM `' . TBRDV . '`';
                                $rech2 .= " WHERE rdv_typrat=1 AND rdv_coderat='" . $int_code . "'";
                                $rows2 = $db->get_results($rech2, ARRAY_A);
                                if ($rows2) {
                                    foreach ($rows2 as $row2) {
                                        extract($row2);
                                        $rech3 = 'SELECT COUNT(pevt_id) FROM `' . TBPEVT . '`';
                                        $rech3 .= ' WHERE pevt_idrdv=' . $rdv_id;
                                        $nbres3 = $db->get_var($rech3);
                                        if ($nbres3 == 0) {
                                            $enr_ok = FALSE;
                                        }
                                    }
                                } else {
                                    $enr_ok = FALSE;
                                }
                            }
                        }
                        break;
                    case '100' :
                    case '101' :
                        if ($mod_planning == TRUE) {
                            if (valsession('niv_log') <> 1) {
                                if ($fi == '100') {
                                    $date2 = nouv_date_plus_jours($datea, 7);
                                } else {
                                    $date2 = nouv_date_plus_jours($datea, 14);
                                }
                                // On trouve les RDV supérieurs ou égaux à la date du jour
                                // et inférieur à la date butoir
                                $rech2 = 'SELECT * FROM `' . TBRDV . '`';
                                $rech2 .= " WHERE rdv_typrat=1 AND rdv_coderat='" . $int_code . "'";
                                $rech2 .= " AND rdv_honore=0 AND rdv_date>='" . $datea . "' AND rdv_date<='" . $date2 . "'";
                                $rows2 = $db->get_results($rech2, ARRAY_A);
                                if ($rows2) {
                                    foreach ($rows2 as $row2) {
                                        extract($row2);
                                        $rech3 = 'SELECT COUNT(*) FROM `' . TBPEVT . '`';
                                        $rech3 .= ' WHERE pevt_idrdv=' . $rdv_id;
                                        $nbres3 = $db->get_var($rech3, ARRAy_A);
                                        if ($nbres3 == 0) {
                                            $enr_ok = FALSE;
                                        }
                                    }
                                } else {
                                    $enr_ok = FALSE;
                                }
                            }
                        }
                        break;
                    default :
                        break;
                }
                if ($enr_ok == true) {
                    $tab_intlst[] = $int_id;
                }
            } else {
                $next_row = FALSE;
            }
            $nrow++;
            // Si restreint, on ne dépasse pas la limite du nombre de lignes à afficher
            if ($restreint AND ($nrow > ($limit - 1))) {
                $next_row = FALSE;
            }
        }
        if (count($tab_intlst) > 0) {
            if (!stristr($rech, 'WHERE ')) {
                $rech .= ' WHERE (';
            } else {
                $rech .= ' AND (';
            }
            foreach ($tab_intlst as $value) {
                $rech .= ' ' . TBINT . '.int_id=' . $value . ' OR';
            }
            // Suppression du dernier 'OR'
            $rech = substr($rech, 0, -3);
            $rech .= ')';
        }
    }


    return $rech;
}
