<?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 agences actives couvrant un département  sur un pays donné */
/* Si "uneagence" est non vide, on l'exclu de la recherche, sinon, non     */
/***************************************************************************/

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

/** Récupération de la liste des agences pour un pays donné **/
function print_plain()
{
    require_once("../lib/gettext/gettext.inc");
    include_once("../inc/config.inc.php");

    header("Content-type: text/html; charset=UTF-8");
    $tab_paysdep = explode("_", $_REQUEST["paysdep"]);
    $age = get_age($tab_paysdep[0], $tab_paysdep[1], $tab_paysdep[2]);
    $result = [];
    $result[] = "=" . _("-- Faites votre choix --");
    $result[] = "-1=" . _("-- Aucune agence en particulier --");
    if (trim($tab_paysdep[2]) <> "")
        $result[] = "0=" . $tab_paysdep[2] . " " . _("uniquement (gestionnaire compte client)");
    if (sizeof($age) > 0) {
        foreach ($age as $row) {
            $result[] = $row["age_code"] . "=" . $row["age_nom"] . " (" . $row["age_code"] . ")";
        }
    }
    print implode(";", $result);
}

/**
 * @param $pays
 * @param $dep
 * @param $filtreage
 * @return array
 */
function get_age($pays, $dep, $filtreage)
{

    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 se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "";
    $result = [];
    // On récupère la liste des agences couvrant le département
    $rech2 = "SELECT zcc_code FROM `" . TBZCC . "` WHERE zcc_codedep = '" . $db->escape($dep) . "' AND zcc_codepays = '" . $db->escape($pays) . "'";
    if (trim($filtreage) <> "")
        $rech2 .= " AND zcc_code <> '" . $db->escape($filtreage) . "'";
    $rows2 = $db->get_results($rech2, ARRAY_A);
    $nb_enr = count($rows2);
    if ($rows2) {
        $idx = 0;
        $rech = "SELECT * FROM `" . TBAGE . "` WHERE `age_ouvert` = '1'";
        if ($nb_enr == 1)
            $rech .= " AND";
        else
            $rech .= " AND (";
        foreach ($rows2 as $row2) {
            extract($row2);
            if ($idx == 0)
                $rech .= " age_code='" . $zcc_code . "'";
            else
                $rech .= " OR age_code='" . $zcc_code . "'";
            $idx++;
        }
        if ($nb_enr > 1)
            $rech .= ")";
        $rech .= " ORDER BY age_nom";
    }
    if (trim($rech) <> "") {
        $rows = $db->get_results($rech, ARRAY_A);
        if ($rows) {
            foreach ($rows as $row) {
                $row = encode_str($row);
                $result[] = $row;
            }
        }
    }
    $db->disconnect();
    return $result;
}

?>