<?php
$price_types = array(
	"Fracht"=>"incl_freight",
	"Verpackung"=>"incl_packing",
	"Versicherung"=>"incl_assurance",
	"Zoll"=>"incl_duty",
);
$id_type = array(
	"duns",
	"iln",
	"buyer_specific",
	"supplier_specific",
);


function parseGeneralInfo($file)
{
	global $bme_general_flds;

	$data = file($file);
	$c=0;
	$general_info = array();
	foreach($data as $line)
	{
	//	$line = mb_convert_encoding( $line, "UTF-8", "UTF-16" );
		if(preg_match("/^(.*?):\t{2}(.*?)$/",$line,$match))
		{
			if(preg_match("/datum/i",$match[1]))
				$value = convertDate(fixString($match[2]));
			else
				$value = fixString($match[2]);
			$key = $bme_general_flds[$c];
			$key = str_replace("#","",$key);
			if($key=="TERRITORIES" || $key=="PRICE_FLAGS")
			{
				preg_match_all("/\w+/",$value,$array);
				$value = $array[0];
				$general_info[$key] = $value;
			}else
			{
				$general_info[$key] = $value;
			}
			$c++;
		}
	}
	return $general_info;
}

function makeHeader()
{
	global $fp;
	global $buyer;
	global $agreement;
	global $supplier;
	global $header_tpl;

	$str = $header_tpl;

	$str = str_replace("[!--catalog--]",makeCatalog(),$str);
	$str = str_replace("[!--buyer--]",makeBuyer(),$str);
	$str = str_replace("[!--agreement--]",makeAgreement(),$str);
	$str = str_replace("[!--supplier--]",makeSupplier(),$str);
	$str = preg_replace("'\n\s*\n'","\n",$str);
	fputs($fp,$str . "\n");
}

function makeCatalog()
{
	global $general_info;
	global $catalog_tpl;
	global $price_types;
	global $bme_general_flds;
	global $aErrors;
	
	$str = $catalog_tpl;
	foreach($general_info as $key=>$val)
	{
		if(!$val)
		{
//			$str = preg_replace('/^.*\[!--' . $key . '--].*$/m','',$str);
			continue;
		}
		if($key == "TERRITORIES")
		{
			$temp = "";
			if(is_array($val))foreach($val as $v)
				$temp .= "<TERRITORY>$v</TERRITORY>";
			$val = $temp;
		}
		if($key == "PRICE_FLAGS")
		{
			$temp = "";
			if(is_array($val))foreach($val as $v)
			{
				$v = str_replace(array_keys($price_types),array_values($price_types),$v);
				if(!in_array($v,$price_types))
					$aErrors[] = "$key darf nur Werte annehmen aus " . implode(",",$price_types);
				$temp .= "<PRICE_FLAG type=\"$v\">TRUE</PRICE_FLAG>";
			}
			$val = $temp;
		}
		$str = str_replace("[!--" . $key . "--]",$val,$str);
	}
	if(preg_match_all("/^.*\[!--(\w+)--].*$/m",$str,$matches,PREG_SET_ORDER))
	{
		foreach($matches as $match)
		{
			$required = in_array("#" . $match[1],$bme_general_flds);
			if($required)
			{
				$error = 1;
				$aErrors[] = "error: missing required tag $match[1]";
			}
			if($match[1] == "GENERATION_DATE")	
			{
				$str = preg_replace("/^\s*<DATETIME.*DATETIME>$/ms","",$str);
			}else
			{
				$str = str_replace($match[0],"",$str);
			}
		}
	}
	return $str;
}
function makeAgreement()
{
	global $general_info;
	global $agreement_tpl;
	global $bme_general_flds;
	global $aErrors;
	
	$makeAgreement = false;
	$str = $agreement_tpl;
	foreach($general_info as $key=>$val)
	{
		if(!$val)
		{
//			$str = preg_replace('/^.*\[!--' . $key . '--].*$/m','',$str);
			continue;
		}
		$makeAgreement = true;
		$str = str_replace("[!--" . $key . "--]",$val,$str);
	}
	if(!$makeAgreement)
		return "";
	if(preg_match_all("/^.*\[!--(\w+)--].*$/m",$str,$matches,PREG_SET_ORDER))
	{
		foreach($matches as $match)
		{
			$required = in_array("#" . $match[1],$bme_general_flds);
			if($required)
			{
				$error = 1;
				$aErrors[] = "error: missing required tag $match[1]";
			}
			$str = str_replace($match[0],"",$str);
		}
	}
	return $str;
}

function makeAddress($address)
{
	global $address_tpl;
	global $bme_address_flds;
	
	$str = $address_tpl;
	foreach($bme_address_flds as $key)
	{
		$key = str_replace("#","",$key);
		if(!$address[$key])
			continue;
		$str = str_replace("[!--" . $key . "--]",$address[$key],$str);
	}
	if(preg_match_all("/^.*\[!--(\w+)--].*$/m",$str,$matches,PREG_SET_ORDER))
	{
		foreach($matches as $match)
		{
			$required = in_array("#" . $match[1],$bme_address_flds);
			if($required)
			{
				$error = 1;
				$aErrors[] = "error: missing required tag $match[1]";
			}
			$str = str_replace($match[0],"",$str);
		}
	}
	return $str;
}

function makeBuyer()
{
	global $buyer;
	global $buyer_tpl;
	global $bme_buyer_flds;
	global $id_type;
	global $aErrors;
	
	//if(!array_sum($buyer))
		//return "";
	$str = $buyer_tpl;
	foreach($buyer as $key=>$value)
	{
		if($key == "BUYER_ID_TYPE" && !in_array($value,$id_type))
		{
			$str = str_replace(" type=\"[!--$key--]\"","",$str);
		}
		if(!$value)
		{
			continue;
		}
		if($key == "ADDRESS")
		{
			$value = makeAddress($value);
		}
		$str = str_replace("[!--$key--]", $value, $str);
	}
	if(preg_match_all("/^.*\[!--(\w+)--].*$/m",$str,$matches,PREG_SET_ORDER))
	{
		foreach($matches as $match)
		{
			$required = in_array("#" . $match[1],$bme_buyer_flds);
			if($required)
			{
				$error = 1;
				$aErrors[] = "error: missing required tag $match[1]";
			}
			$str = str_replace($match[0],"",$str);
		}
	}
	return $str;
}

function makeSupplier()
{
	global $supplier;
	global $supplier_tpl;
	global $bme_supplier_flds;
	global $id_type;
	global $mimeinfo_tpl;
	global $mime_tpl;
	global $aErrors;


	$str = $supplier_tpl;
	foreach($supplier as $key=>$value)
	{
		if($key == "SUPPLIER_ID_TYPE" && !in_array($value,$id_type))
		{
			$aErrors[] = "Kennnummertyp muss ein Wert sein aus " . implode(",",$id_type);
			$str = str_replace(" type=\"[!--$key--]\"","",$str);
		}
		if(!$value)
		{
			continue;
		}
		if($key == "ADDRESS")
		{
			$value = makeAddress($value);
		}
		if($key == "LOGO")
		{
			$mime = $mime_tpl;
			$mime = str_replace("[!--MIME_TYPE--]",getMimeType($value),$mime);
			$mime = str_replace("[!--MIME_SOURCE--]",$value,$mime);
			$mime = str_replace("[!--MIME_DESCR--]","",$mime);
			$mime = str_replace("[!--MIME_ALT--]","",$mime);
			$mime = str_replace("[!--MIME_PURPOSE--]","logo",$mime);
			$mime = str_replace("<MIME_DESCR></MIME_DESCR>","",$mime);
			$mime = str_replace("<MIME_ALT></MIME_ALT>","",$mime);
			$value = str_replace("[!--MIME--]",$mime,$mimeinfo_tpl);
		}
		$str = str_replace("[!--$key--]", $value, $str);
	}
	if(preg_match_all("/^.*\[!--(\w+)--].*$/m",$str,$matches,PREG_SET_ORDER))
	{
		foreach($matches as $match)
		{
			$required = in_array("#" . $match[1],$bme_supplier_flds);
			if($required)
			{
				$error = 1;
				$aErrors[] = "error: missing required tag $match[1]";
			}
			$str = str_replace($match[0],"",$str);
		}
	}
	return $str;
}

function parseBuyer($file)
{
	global $bme_buyer_flds;
	global $bme_address_flds;

	$data = file($file);
	$isdata = true;
	$ret = array();
	$c = 0;
	foreach($data as $line)
	{
	//	$line = mb_convert_encoding( $line, "UTF-8", "UTF-16" );
		preg_match("/^(.*?):(\t{2})?(.*)?$/",$line,$match);
		if($isdata && $match[1])
		{
			$val = fixString($match[3]);
			$key = str_replace("#","",$bme_buyer_flds[$c]);
			if(in_array($key,$bme_address_flds))
				$ret["ADDRESS"][$key] = $val;
			else
				$ret[$key] = $val;
			$c++;
		}
	}
	if(!array_sum($ret["ADDRESS"]))
		$ret["ADDRESS"] = false;
	else
		$ret["ADDRESS"]["address_type"] = "buyer";
	return $ret;
}

function parseSupplier($file)
{
	global $bme_supplier_flds;
	global $bme_address_flds;

	$data = file($file);
	$isdata = true;
	$ret = array();
	$c = 0;
	foreach($data as $line)
	{
	//	$line = mb_convert_encoding( $line, "UTF-8", "UTF-16" );
		preg_match("/^(.*?):(\t\t)?(.*)?$/",$line,$match);
		if($isdata && $match[1])
		{
			$val = fixString($match[3]);
			$key = str_replace("#","",$bme_supplier_flds[$c]);
			if(in_array($key,$bme_address_flds))
				$ret["ADDRESS"][$key] = $val;
			else
			$ret[$key] = $val;
			$c++;
		}
	}
	if(!array_sum($ret["ADDRESS"]))
		$ret["ADDRESS"] = false;
	else
		$ret["ADDRESS"]["address_type"] = "supplier";
	return $ret;
}
?>
