<?php


require_once("syncinclude.php");

$Response="";
$return = 3;
//Auth
if (auth())
{
	$return = 0;
	//hole einstellunegn
	$cur_query = eS_execute_query("select * from eazysales_einstellungen");
	$einstellungen = go_db_fetch_object($cur_query);
	
	//get currency
	$cur_query = eS_execute_query("select * from currencies where currencies_id=".$einstellungen->currencies_id);
	$currency = go_db_fetch_object($cur_query);
	
	//hole einen noch nicht versandten Artikel nach eS raus 
	$cur_query = eS_execute_query("select items.items_id from items LEFT JOIN eazysales_martikel ON items.items_id=eazysales_martikel.items_id where eazysales_martikel.items_id is NULL limit 1");
	if ($item_id = go_db_fetch_object($cur_query))
	{
		//hole item		
		$item_query = eS_execute_query("select * from items where items_id=".$item_id->items_id);
		if ($item = go_db_fetch_object($item_query))
		{	
			//hole beschreibung
			$item_desc_query = eS_execute_query("select * from items_description where items_id=".$item_id->items_id." and language_id=".$einstellungen->languages_id." order by items_name");
			$item_desc = go_db_fetch_object($item_desc_query);
			//mappe alles auf item
			$item->language_id=$item_desc->language_id;
			$item->items_name=$item_desc->items_name;
			$item->items_description=$item_desc->items_description;
			$item->items_short_description=$item_desc->items_short_description;
			$item->items_keywords=$item_desc->items_keywords;
			$item->items_meta_title=$item_desc->items_meta_title;
			$item->items_meta_description=$item_desc->items_meta_description;
			$item->items_meta_keywords=$item_desc->items_meta_keywords;
			$item->items_url=$item_desc->items_url;

			//bereite dieses Produkt zum Senden vor
			//hole Steuer
			$tax = get_tax($item->items_tax_class_id);
			$items_status="N";
			if ($item->items_status)	
				$items_status="Y";
			//baue Response			
			$Response=CSVkonform("P").";".			
					CSVkonform($item->items_id).";".
					CSVkonform(substr(unhtmlentities($item->items_model),0,20)).";".			
					CSVkonform(substr(unhtmlentities($item->items_name),0,255)).";".			
					CSVkonform(substr(unhtmlentities($item->items_description),0,64000)).";".			
					CSVkonform(substr(unhtmlentities($item->items_short_description),0,254)).";".				
					CSVkonform(get_preisEndkunde($item)+get_preisEndkunde($item)*$currency->value*$tax/100).";".			
					CSVkonform(get_preisEndkunde($item)*$currency->value).";".			
					CSVkonform("").";".			
					CSVkonform($tax).";".			
					CSVkonform("").";".			
					CSVkonform("Y").";".			
					CSVkonform($items_status).";".			
					CSVkonform($item->items_quantity).";".			
					CSVkonform("").";".			
					CSVkonform("1").";".			
					CSVkonform(unhtmlentities($item->items_ean)).";".			
					CSVkonform("").";".		
					CSVkonform(get_preisHaendlerKunde($item)+get_preisHaendlerKunde($item)*$currency->value*$tax/100).";".		
					CSVkonform(get_preisHaendlerKunde($item)*$currency->value).";".		
					CSVkonform($item->items_startpage).";".		
					CSVkonform("N").";".		
					CSVkonform("N").";".		
					CSVkonform($item->items_weight).";".		
					CSVkonform("N").";".		
					CSVkonform("N").";".		
					CSVkonform("N").";".		
					CSVkonform("N").";".		
					CSVkonform("0").";".		
					CSVkonform("0").";".		
					CSVkonform(unhtmlentities(getManufacturer($item->producers_id))).";".		
					CSVkonform(get_bildURL($item)).";\n";		
					
			$Response.=get_cats($item->items_id);
			$Response.=get_variationen($item->items_id);
//			$Response.=get_variationswerte($item->items_id);
			$Response.=get_attribute($item);
//			$Response.=get_staffelpreise($item->items_id,1);
//			$Response.=get_staffelpreise($item->items_id,0);

		}
	}
}
echo($return.";\n");
echo($Response);
function get_bildURL($item)
{
	$pic="";
	//hat produkt bild?
	$imageUrlPrefix="";
	if ($item->items_picture)
	{
		$path="";
		if (file_exists(FOLDER_ABSOLUT_CATALOG_PICTURES.$item->items_picture))
			$path= FOLDER_RELATIV_CATALOG . FOLDER_RELATIV_PICTURES;
			
		if (strlen($path)>1)
		{			
			//is es ein jpg?
			if (preg_match("/jpg/i",substr($item->items_picture,strlen($item->items_picture)-3)))
			{
				$pic=HTTP_SERVER.$path.$item->items_picture;
			}
			//is es ein jpeg? 
			elseif (preg_match("/jpeg/i",substr($item->items_picture,strlen($item->items_picture)-4))) 
			{ 
			   $pic=HTTP_SERVER.$path.$item->items_picture;
			}
			elseif (preg_match("/gif/i",substr($item->items_picture,strlen($item->items_picture)-3)))
			{
				if(function_exists("ImageCreateFromGIF"))
				{
					$im = @ImageCreateFromGIF (FOLDER_ABSOLUT_CATALOG_PICTURES.$item->items_picture);
					if ($im)
					{
						//erstelle dir, falls noch nicht getan
						if (!file_exists(FOLDER_ABSOLUT_CATALOG_PICTURES."es_export"))
							mkdir (FOLDER_ABSOLUT_CATALOG_PICTURES."es_export",0777);
							
						imagejpeg($im,FOLDER_ABSOLUT_CATALOG_PICTURES."es_export/".$item->items_id.".jpg");
						if (file_exists(FOLDER_ABSOLUT_CATALOG_PICTURES."es_export/".$item->items_id.".jpg"))
						{
							$pic=HTTP_SERVER.FOLDER_RELATIV_PICTURES."es_export/".$item->items_id.".jpg";
						}
					}
				}
			}
		}		
	}
	return $pic;
}

function get_attribute($item)
{
	$attribute="";
	//Lieferstatus
	if ($item->items_shippingtime>0)
	{
		//hole bezeichnung
		$cur_query = eS_execute_query("select shipping_status_name from shipping_status where language_id=".$GLOBALS['einstellungen']->languages_id." and shipping_status_id=".$item->items_shippingtime);
		$status = go_db_fetch_object($cur_query);
		if (strlen($status->shipping_status_name)>0)
		{
			//Attribut hinzuf�gen
			$attribute.=CSVkonform("T").";".			
				CSVkonform("Lieferstatus").";".
				CSVkonform($status->shipping_status_name).";\n";			
		}
	}
	//Herstellerlink gesetzt?
	if (strlen($item->items_url)>0)
	{
		//Attribut hinzuf�gen
		$attribute.=CSVkonform("T").";".			
			CSVkonform("Herstellerlink").";".
			CSVkonform($item->items_url).";\n";	
	}
	//ist vpe gesetzt?
	if ($item->items_vpe>0)
	{
		//hole vpe bezeichnung
		$cur_query = eS_execute_query("select items_vpe_name from items_vpe where language_id=".$GLOBALS['einstellungen']->languages_id." and items_vpe_id=".$item->items_vpe);
		$vpe = go_db_fetch_object($cur_query);
		if (strlen($vpe->items_vpe_name)>0)
		{
			//Attribut hinzuf�gen
			$attribute.=CSVkonform("T").";".			
				CSVkonform("Verpackungseinheit").";".
				CSVkonform($vpe->items_vpe_name).";\n";
		}
	}
	//FSK 18?
	if ($item->items_fsk18>0)
	{
		//Attribut hinzuf�gen
		$attribute.=CSVkonform("T").";".			
			CSVkonform("Altersfreigabe").";".
			CSVkonform("FSK 18").";\n";
	}
	return $attribute;
}

function get_variationswerte($items_id,$options_id)
{
	$variationswerte="";
	//existieren Variationen f�r diesen Artikel?
	$cur_query = eS_execute_query("select * from items_characteristics where options_id=".$options_id." and items_id=".$items_id);
	while ($variation = go_db_fetch_object($cur_query))
	{
		if ($variation->options_values_id>0)
		{
			//hole Variationswertnamen etc.
			$opt_query = eS_execute_query("select items_options_values_name, items_options_values_id from items_options_values where language_id=".$GLOBALS['einstellungen']->languages_id." and items_options_values_id=".$variation->options_values_id);
			$var_name = go_db_fetch_object($opt_query);
			if ($var_name->items_options_values_id>0)
			{
				if ($variation->price_prefix=="-")
					$variation->options_values_price*=-1;
				$variationswerte.=CSVkonform("W").";".			
					CSVkonform($variation->options_id).";".
					CSVkonform($variation->items_characteristics_id).";".
					CSVkonform($var_name->items_options_values_name).";".
					CSVkonform($variation->options_values_price+$variation->options_values_price*$GLOBALS['currency']->value*$GLOBALS['tax']/100).";".
					CSVkonform($variation->options_values_price).";".
					CSVkonform($variation->options_values_weight).";".
					CSVkonform(substr($variation->characteristics_model,0,30)).";".
					CSVkonform($variation->sortorder).";".
					CSVkonform($variation->characteristics_stock).";\n";
			}
		}		
	}
	return $variationswerte;
}

function get_variationen($items_id)
{
	$variationen="";
	//existieren Variationen zu diesem Artikel?
	$cur_query = eS_execute_query("select * from items_characteristics where items_id=".$items_id." group by options_id");
	while ($variation = go_db_fetch_object($cur_query))
	{
		if ($variation->options_id>0)
		{
			//hole Variationsname etc.
			$opt_query = eS_execute_query("select items_options_name, items_options_id from items_options where language_id=".$GLOBALS['einstellungen']->languages_id." and items_options_id=".$variation->options_id);
			$var_name = go_db_fetch_object($opt_query);
			if ($var_name->items_options_id>0)
			{
				$variationen.=CSVkonform("V").";".			
					CSVkonform($variation->options_id).";".
					CSVkonform($var_name->items_options_name).";".					
					CSVkonform("Y").";".
					CSVkonform(0).";\n";
			}
			$variationen.=get_variationswerte($items_id,$variation->options_id);
		}
	}
	return $variationen;
}

function get_staffelpreise($items_id, $endkunde)
{
	$staffel="";
	$anzahl = "";
	$preise = "";
	$personalOfferTable = "personal_offers_by_attendees_status_";
	$endKunden_arr = explode(";",$GLOBALS['einstellungen']->mappingEndkunde);
	$haendlerKunden_arr = explode(";",$GLOBALS['einstellungen']->mappingHaendlerkunde);	
	$table = $personalOfferTable.$endKunden_arr[0];
	if ($endkunde==0)
		$table = $personalOfferTable.$haendlerKunden_arr[0];
	$anzahlStaffelpreise=0;
	//existieren Staffelprese f�r diesen Artikel?
	$cur_query = eS_execute_query("select * from ".$table." where items_id=".$items_id." and quantity>1 order by quantity asc limit 5");
	while ($staffelpreise = go_db_fetch_object($cur_query))
	{
		$anzahlStaffelpreise++;
		$anzahl.=";".CSVkonform($staffelpreise->quantity);
		$preise.=";".CSVkonform($staffelpreise->personal_offer*$GLOBALS['currency']->value);
	}
	if (strlen($anzahl)>0)
	{
		$staffel=CSVkonform("SH");
		if ($endkunde)
			$staffel=CSVkonform("SP");
		$staffel.=$anzahl;
		for ($i=0;$i<5-$anzahlStaffelpreise;$i++)
			$staffel.=";".CSVkonform("0");	
		$staffel.=$preise;
		for ($i=0;$i<5-$anzahlStaffelpreise;$i++)
			$staffel.=";".CSVkonform("0");	
		$staffel.=";\n";	
	}
	return $staffel;
}

function get_preisEndkunde($item)
{	
	$endKunden_arr = explode(";",$GLOBALS['einstellungen']->mappingEndkunde);
	if ($endKunden_arr[0]>0)
	{
		$personalOfferTable = "personal_offers_by_attendees_status_".$endKunden_arr[0];
		$cur_query = eS_execute_query("select * from $personalOfferTable where quantity=1 and items_id=".$item->items_id);
		$staffelpreise = go_db_fetch_object($cur_query);
		if ($staffelpreise->quantity == 1)
			return $staffelpreise->personal_offer;
	}
	return $item->items_price;
}

function get_preisHaendlerKunde($item)
{
	$haendlerKunden_arr = explode(";",$GLOBALS['einstellungen']->mappingHaendlerkunde);
	if ($haendlerKunden_arr[0]>0)
	{
		$personalOfferTable = "personal_offers_by_attendees_status_".$haendlerKunden_arr[0];
		$cur_query = eS_execute_query("select * from $personalOfferTable where quantity=1 and items_id=".$item->items_id);
		$staffelpreise = go_db_fetch_object($cur_query);
		if ($staffelpreise->quantity == 1)
			return $staffelpreise->personal_offer;
	}
	return 0;
}

//get categroies 
function get_cats($items_id)
{
	$res ="";
	//get cat_id
	$glob_cat_query = eS_execute_query("select * from items_to_categories where items_id=".$items_id);
	while ($act_cat = go_db_fetch_object($glob_cat_query))
	{
		$catArr = array();
		$cur_cat_id = $act_cat->categories_id;
		if ($cur_cat_id>0)
		{
			$cat_query = eS_execute_query("select * from categories where categories_id=".$cur_cat_id);
			$current_cat = go_db_fetch_object($cat_query);	
			array_push($catArr,$cur_cat_id);
		}
		
		while ($current_cat->parent_id>0)
		{
			$cur_cat_id = $current_cat->parent_id;
			$cat_query = eS_execute_query("select * from categories where categories_id=".$cur_cat_id);
			$current_cat = go_db_fetch_object($cat_query);		
			array_push($catArr,$cur_cat_id);		
		}
		$cnt = count($catArr);
		for ($i=0;$i<$cnt;$i++)
		{
			$vor="UK";
			if ($i==0)
				$vor="K";
			$catId = array_pop($catArr);
			$cat_query = eS_execute_query("select * from categories_description where categories_id=".$catId." and language_id=".$GLOBALS['einstellungen']->languages_id);
			$current_cat = go_db_fetch_object($cat_query);
			$res.=CSVkonform($vor).";".			
				CSVkonform(unhtmlentities($current_cat->categories_name)).";".
				CSVkonform(substr(unhtmlentities($current_cat->categories_description),0,64000)).";".				
				CSVkonform($catId).";".
				CSVkonform(1).";".
				CSVkonform("").";".
				CSVkonform($current_cat->sort_order).";\n";								
		}
	}
	if ($res=="")
		$res.=CSVkonform("K").";".			
			CSVkonform("Top").";".
			CSVkonform("Wurzelkategorie").";".
			CSVkonform(0).";".
			CSVkonform(0).";;".
			CSVkonform(0).";\n";
	return $res;
}

//get hersteller
function getManufacturer($producers_id)
{
	if ($producers_id>0)
	{
		$manu_query = eS_execute_query("select * from producers where producers_id=".$producers_id);
		$manu = go_db_fetch_object($manu_query);
		return ($manu->producers_name);	
	}
	return "";
}

?>
