<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.9

Copyright (C) 2006-2009  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://smitst.homelinux.com
****************************************************************************************/

/***************************************************************************/
/* 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_undépartement_uneagence";
}

/** Récupération de la liste des agences pour un pays donné **/
function print_plain()
{
	include_once("../lng/".$_SESSION["lng"].".inc.php"); // Paramètres de language
	include_once("../inc/config.inc.php");

	header("Content-type: text/html; charset=windows-1252");
	$tab_paysdep =explode("_",$_REQUEST["paysdep"]);
	$age = get_age($tab_paysdep[0],$tab_paysdep[1],$tab_paysdep[2]);
	$result = array();
	$result[] = "=".$sel_lst_age;
	$result[] = "-1=".$sel_lst_agedep;
	$result[] = "0=".$tab_paysdep[2]." ".$sel_lst_uniq_gc;
	if (sizeof($age)>0)
	{
		foreach ($age as $row)
		{
			$result[] = $row["age_code"]."=".$row["age_nom"]." (".$row["age_code"].")";
		}
	}
	print implode(";", $result);
}

function get_age($pays,$dep,$filtreage)
{
	if (!isset($_SESSION["lng"])) $_SESSION["lng"]="fr";
	include_once("../lng/".$_SESSION["lng"].".inc.php"); // Paramètres de language
	include_once("../inc/config.inc.php");
	include_once("../inc/func_txt.inc.php");
	// On se connecte a la base
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech="";
	$result = array();
	// On récupère la liste des agences couvrant le département
	$rech2 = "SELECT zcc_code FROM ".TBZCC." WHERE zcc_codedep = '$dep' AND zcc_codepays = '$pays'";
	if (trim($filtreage)<>"")
		$rech2.= " AND zcc_code <> '$filtreage'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nb_enr=mysql_num_rows($rs2);
	if ($nb_enr>0)
	{
		$idx=0;
		$rech = "SELECT * FROM ".TBAGE." WHERE `age_ouvert` = '1'";
		if ($nb_enr==1)
			$rech.= " AND";
		else
			$rech.= " AND (";
		while ($row2 = mysql_fetch_assoc($rs2)) 
		{         
			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)<>"")
	{
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) <> 0)
		{
			while ($row = mysql_fetch_array($rs)) 
			{
				$result[] = $row;
			}
		}
	}
	mysql_close();
	return $result;
}

?>