<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:decimal-format name="european" decimal-separator='.' grouping-separator=',' />

<xsl:param name="mode"/>


<xsl:template match="bestelopdracht">
<xsl:comment>Facilitor custom XSL template for cXML bestelopdracht</xsl:comment>
<xsl:element name="cXML">
  <xsl:attribute name="payloadID"><xsl:value-of select="datum/timestamp"/>@http://mycomp.com</xsl:attribute>
  <xsl:attribute name="timestamp">2006-05-10T12:41:47+01:00</xsl:attribute>
  <xsl:attribute name="xml:lang">NL</xsl:attribute>
	<Header>
		<From>
			<Credential domain="AAFM" type="marketplace">
				<Identity><xsl:value-of select='bedrijf/overeenkomst_nr'/></Identity>
			</Credential>
		</From>
		<To>
		    <xsl:element name="Credential">
		     <xsl:attribute name="domain"><xsl:value-of select="bedrijf/naam"/></xsl:attribute>
				<Identity><xsl:value-of select='bedrijf/leverancier_nr'/></Identity>
			</xsl:element>
		</To>
		<Sender>
			<Credential domain="Facilitor">
				<Identity/>
			</Credential>
			<UserAgent></UserAgent>
		</Sender>
	</Header>
	<Request>
		<OrderRequest>
		  <xsl:element name="OrderRequestHeader">
			<xsl:attribute name="orderID"><xsl:value-of select="id"/></xsl:attribute>
			<xsl:attribute name="orderDate"><xsl:value-of select="/facilitor/bestelopdracht/datum/jaar"/>-<xsl:value-of select="/facilitor/bestelopdracht/datum/maand"/>-<xsl:value-of select="/facilitor/bestelopdracht/datum/dag"/></xsl:attribute>
			<xsl:attribute name="type">new</xsl:attribute>
			<Total>
				<Money currency="EUR"><xsl:value-of select="format-number(sum(bestelopdrachtitem/totaal), '0.00', 'european')"/></Money>
			</Total>
			<ShipTo>
  			<xsl:apply-templates select="." mode="ShipTo"/>
			</ShipTo>
			<BillTo>
				<xsl:apply-templates select="." mode="BillTo"/>
			</BillTo>
			<Contact>
				<Name xsl:lang="NL"><xsl:value-of select="bestelling/voor/naam_full"/></Name>
				<Email><xsl:value-of select="bestelling/voor/email"/></Email>
			</Contact>
			<Extrinsic name="Costcenter"><xsl:value-of select='bestelling/kostenplaats/nr'/></Extrinsic>
				<xsl:for-each select="bestelling/kenmerk">
				  <xsl:if test="@type!='Q' and @type!='L'">
				    <xsl:element name="Extrinsic">
					    <xsl:attribute name="name">
  					    <xsl:value-of select="@naam"/>
  					  </xsl:attribute>
  					    <xsl:value-of select="."/>
  			    </xsl:element>
  			  </xsl:if>
  			</xsl:for-each>
		</xsl:element>
    <xsl:for-each select="bestelopdrachtitem">
    <xsl:sort select="srtdeel/omschrijving"/>
    <xsl:sort select="posnr"/>
			<xsl:element name="ItemOut">
			<xsl:attribute name="quantity"><xsl:value-of select='aantal'/></xsl:attribute>
			<xsl:attribute name="lineNumber"><xsl:value-of select='posnr'/></xsl:attribute>
			<xsl:attribute name="requestedDeliveryDate"><xsl:value-of select="/facilitor/bestelopdracht/bestelling/leverdatum/jaar"/>-<xsl:value-of select="/facilitor/bestelopdracht/bestelling/leverdatum/maand"/>-<xsl:value-of select="/facilitor/bestelopdracht/bestelling/leverdatum/dag"/></xsl:attribute>
			<ItemID>
				<SupplierPartID><xsl:value-of select='bestelitem/srtdeel/artikel_nummer'/></SupplierPartID>
			</ItemID>
			<ItemDetail>
				<UnitPrice>
					<Money currency="EUR"><xsl:value-of select='prijs'/></Money>
				</UnitPrice>
				<Description xml:lang="NL"><xsl:value-of select='bestelitem/srtdeel/omschrijving'/></Description>
				<UnitOfMeasure><xsl:value-of select='bestelitem/srtdeel/eenheid'/></UnitOfMeasure>
				<Classification domain=""></Classification>
				<xsl:for-each select="bestelitem/kenmerk">
				  <xsl:if test="@type!='Q' and @type!='L'">
				    <xsl:element name="Extrinsic">
					    <xsl:attribute name="name">
  					    <xsl:value-of select="@naam"/>
  					  </xsl:attribute>
  					    <xsl:value-of select="."/>
  			    </xsl:element>
  			  </xsl:if>
  			</xsl:for-each>
			</ItemDetail>
		</xsl:element>
       </xsl:for-each>
		</OrderRequest>
	</Request>
</xsl:element>
</xsl:template>

<xsl:template match="facilitor">
		<xsl:apply-templates select="bestelopdracht"/>
</xsl:template>

<xsl:template match="/">
		<xsl:choose>
			<xsl:when test="$mode='getExtension'">
				<xsl:element name="format">
					<xsl:element name="extension">xml</xsl:element>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="facilitor"/>
			</xsl:otherwise>
		</xsl:choose>
</xsl:template>



<xsl:template match="bezoek_land">
		<xsl:choose>
			<xsl:when test=". = ''">The Netherlands</xsl:when>
			<xsl:otherwise><xsl:value-of select='.'/></xsl:otherwise>
		</xsl:choose>
</xsl:template>

<xsl:template match="post_land">
		<xsl:choose>
			<xsl:when test=". = ''">The Netherlands</xsl:when>
			<xsl:otherwise><xsl:value-of select='.'/></xsl:otherwise>
		</xsl:choose>
</xsl:template>

<xsl:template match="bestelopdracht" mode="ShipTo">
	<xsl:element name="Address">
		<xsl:attribute name="addressID"></xsl:attribute>
			<Name xml:lang="NL"><xsl:value-of select="afleveradres/naam"/></Name>
		<PostalAddress>
			<DeliverTo><xsl:value-of select="bestelling/voor/naam_full"/></DeliverTo>
			<DeliverTo><xsl:value-of select="bestelling/plaats"/></DeliverTo>
			<Street><xsl:value-of select="afleveradres/bezoek_adres"/></Street>
			<City><xsl:value-of select="afleveradres/bezoek_plaats"/></City>
			<State></State>
			<PostalCode><xsl:value-of select="afleveradres/bezoek_postcode"/></PostalCode>
			<Country isoCountryCode="NL"><xsl:apply-templates select="afleveradres/bezoek_land"/></Country>
		</PostalAddress>
	</xsl:element>
</xsl:template>

<xsl:template match="bestelopdracht" mode="BillTo">
		<!-- Indien het afleveradres een debiteurennummer heeft, dan die gebruiken, en anders het debiteurennummer van de besteller (dwz afdeling van besteller) gebruiken -->	
				<!-- GEEN deb.nr. bij afleveradres, dus gebruik deb.nr. en alle factuuradresgegevens van AFDELING/BU van besteller -->
				<xsl:element name="Address">
				  <xsl:attribute name="addressID">AAFM</xsl:attribute>
					<Name xml:lang="NL"><xsl:value-of select="bestelling/voor/afdeling/bedrijf/naam"/></Name>
					<PostalAddress name="">
						<Street><xsl:value-of select="factuuradres/post_adres"/></Street>
						<City><xsl:value-of select="factuuradres/post_plaats"/></City>
						<State></State>
						<PostalCode><xsl:value-of select="factuuradres/post_postcode"/></PostalCode>
						<Country isoCountryCode="NL"><xsl:apply-templates select="factuuradres/post_land"/></Country>
					</PostalAddress>
				</xsl:element>
</xsl:template>



</xsl:stylesheet>