<?xml version="1.0"?>
	<!--
		@author Last Changed:$Author: theegarten $ @version $Revision: 5244 $
		@date $Date: 2010-10-26 14:12:36 +0200 (Di, 26 Okt 2010) $
	-->
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://schema.infor.com/InforOAGIS/2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://xml.apache.org/saxon/java" xmlns:xd="http://www.pnp-software.com/XSLTdoc" 
	xmlns:tfs="http://www.infor.com/tfs" exclude-result-prefixes="java tfs">
	<!---->
	<!--****************************************************************************************************-->
	<!-- retrieves the internal BillTo from the partner mapping -->
	<!--****************************************************************************************************-->
	<xd:doc>
		<xd:short>Returns the Partner mapping for a <b>Sender</b>, <b>Receiver</b> and <b>Warehouse</b>.</xd:short>
		<xd:detail>If the <b>buyer</b> is passed as an argument to this function, it will be added to the search criteria for the Partner mapping.<br/>
		If no value was found in the Partner mapping with the passed key values, the transformation is aborted and an error is raised.
		</xd:detail>
		<xd:param name="UNBSender" type="String">Sender of the EDI message.</xd:param>
		<xd:param name="UNBReceiver" type="String">Receiver of the EDI message.</xd:param>
		<xd:param name="warehouse" type="String">Warehouse of the EDI message.</xd:param>
		<xd:param name="buyer" type="String">Buyer of the EDI message.</xd:param>
	</xd:doc>
	<xsl:function name="tfs:getBillTo" as="element()*">
		<xsl:param name="MessageType"/>
		<xsl:param name="UNBSender" />
		<xsl:param name="UNBReceiver" />
		<xsl:param name="billTo" />
		<xsl:param name="buyer" />
		<!---->
		<xsl:if test="$billTo !=''">
			<xsl:choose>
				<xsl:when test="$buyer !='' ">
					<xsl:variable name="partner" select="document('partnerMappingExternalInternal.xml')/PartnerMappingExternalInternal/Partner[@MessageType=$MessageType and  @UNBSender=$UNBSender and @UNBReceiver=$UNBReceiver and Buyer/@id=$buyer and Invoicee/@id=$billTo][1]/Invoicee[@id=$billTo]" />
					<!-- check if partner is empty -->
					<xsl:choose>
						<xsl:when test="$partner !='' ">
							<xsl:sequence select="$partner" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:message
								select="tfs:raiseError('Could not find a mapping for Invoicee with:', $UNBSender, $UNBReceiver, $billTo, $buyer)" />
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>
					<xsl:variable name="partner" select="document('partnerMappingExternalInternal.xml')/PartnerMappingExternalInternal/Partner[@MessageType=$MessageType and  @UNBSender=$UNBSender and @UNBReceiver=$UNBReceiver and Invoicee/@id=$billTo]/Invoicee[@id=$billTo]" />
					<!-- check if partner is empty -->
					<xsl:choose>
						<xsl:when test="$partner !='' ">
							<xsl:sequence select="$partner" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:message
								select="tfs:raiseError('Could not find a mapping for Invoicee with:', $UNBSender, $UNBReceiver, $billTo, '')" />
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:function>
</xsl:stylesheet>
