<?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
 ****************************************************************************************/

/**********************************************/
/* Récupération client en fonction d'un texte */
/* recherché                                  */
/**********************************************/

session_start();
sleep(1);
if (array_key_exists("txt", $_REQUEST)) {
    $tomorrow = 0; //60*60*24; // en nb de secondes
    header("Cache-Control: max-age=$tomorrow");
    print_plain();
} else {
    print "Usage : $_SERVER[PHP_SELF]?txt=untexte_clientsdejachoisis";
}

function print_plain()
{
    require_once("../lib/gettext/gettext.inc");
    include_once("../inc/config.inc.php");
    include_once("../inc/func_geo.inc.php");
    header("Content-type: text/html; charset=UTF-8");
    $tab_txtrech = explode("_", $_REQUEST["txt"]);
    $cli = get_cli($tab_txtrech[0], $tab_txtrech[1]);
    $result = [];
    if (count($cli) > 0) {
        foreach ($cli as $row) {
            $txtres = $row["cli_code"] . "=" . $row["cli_ste"] . ", " . $row["cli_adr1"];
            if (trim($row["cli_adr2"]) <> "")
                $txtres .= ", " . $row["cli_adr2"];
            $txtres .= ", " . Retournecp_Id($row["cli_ville"], TRUE) . " " . Retourneville_Id($row["cli_ville"], TRUE);
            $result[] = $txtres;

        }
    }
    print implode(";", $result);
}

/**
 * @param $txtrech
 * @param $exclu
 * @return array
 */
function get_cli($txtrech, $exclu)
{

    require_once("../lib/gettext/gettext.inc");
    include_once("../inc/config.inc.php");
    include_once("../inc/func_txt.inc.php");
    include_once("../classes/ezSQL/shared/ez_sql_core.php");
    include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
    include_once("../inc/func_sql.inc.php");

    // On cree le resultat
    $result = [];
    // On se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT * FROM `" . TBCLI . "`";
    // trouvé ?
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE (";
    else
        $rech .= " AND (";

    // Construction de la chaîne de recherche
    $rech .= " cli_ste LIKE '%" . $db->escape($txtrech) . "%' OR";
    $rech .= " cli_adr1 LIKE '%" . $db->escape($txtrech) . "%' OR";
    $rech .= " cli_adr2 LIKE '%" . $db->escape($txtrech) . "%' OR";

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

    // Ajout des codes à exclure
    if (trim($exclu) <> '') {
        $tab_exclu = explode("-", $exclu);
        if (count($tab_exclu) > 0) {
            $rech .= " AND (";
            for ($i = 0; $i < count($tab_exclu); $i++) {
                if (trim($tab_exclu[$i]) <> '')
                    $rech .= " cli_code <> '" . $db->escape($tab_exclu[$i]) . "' AND";
            }
            // Suppression du dernier 'AND'
            $rech = substr($rech, 0, strlen($rech) - 4);
            $rech .= ")";
        }
    }
    $rech .= " ORDER BY cli_ste";

    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            // On ne prend que les comptes actifs
            $rech2 = "SELECT COUNT(*) FROM `" . TBPW . "` WHERE `pw_code` = '" . $row["cli_code"] . "' AND `pw_actif` = '0'";
            $nbres2 = $db->get_var($rech2);
            if ($nbres2 == 0)
                $result[] = $row;
        }
    }
    $db->disconnect();
    return $result;
}

?>