<?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" indent="yes" encoding="utf-8"/>

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

	<xsl:param name="mode"/>

	<xsl:variable name="Rev">
		<xsl:value-of select="substring(translate('$Revision$', '$ ', ''), 10)"/>
	</xsl:variable>

	<xsl:template match="bestelopdracht">
		<xsl:comment>Facilitor custom XSL template for cXML bestelopdracht</xsl:comment>
		<xsl:element name="cXML">
			<xsl:attribute name="version">1.2.016</xsl:attribute>
			<xsl:attribute name="payloadID">
				<xsl:value-of select="datum/timestamp"/>-<xsl:value-of select="id"/></xsl:attribute>
			<xsl:attribute name="timestamp">
				<xsl:value-of select="datum/jaar"/>-<xsl:value-of select="datum/maand"/>-<xsl:value-of select="datum/dag"/>T<xsl:value-of select="datum/tijd"/>:00+CEST</xsl:attribute>
			<xsl:attribute name="xml:lang">NL</xsl:attribute>
			<Header>
				<From>
					<xsl:element name="Credential">
						<xsl:attribute name="domain">Stichting Sutfene</xsl:attribute>
						<Identity>LUN</Identity>
					</xsl:element>
				</From>
				<To>
					<xsl:element name="Credential">
						<xsl:attribute name="domain">GLN</xsl:attribute>
						<Identity>8713211000012</Identity>
					</xsl:element>
				</To>
				<Sender>
					<xsl:element name="Credential">
						<!--Of fixed NetworkId???-->
						<xsl:attribute name="domain">Facilitor</xsl:attribute>
						<Identity>Facilitor</Identity>
					</xsl:element>
					<UserAgent>Facilitor EDI export 1.0</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="datum/jaar"/>-<xsl:value-of select="datum/maand"/>-<xsl:value-of select="datum/dag"/><xsl:text> </xsl:text><xsl:value-of select="datum/tijd"/>:00</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:element name="Address">
								<xsl:attribute name="isoCountryCode">NL</xsl:attribute>
								<xsl:attribute name="addressID">LUN</xsl:attribute>
								<Name xml:lang="NL"><xsl:value-of select="afleveradres/naam"/></Name>
								<PostalAddress>
							      <DeliverTo><xsl:value-of select="bestelling/aanvrager/naam_full"/></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>
								<Email>
									<xsl:value-of select="bestelling/aanvrager/email"/>
								</Email>	
							</xsl:element>
						</ShipTo>
						<BillTo>
							<xsl:element name="Address">
								<xsl:attribute name="isoCountryCode">NL</xsl:attribute>
								<xsl:attribute name="addressID">LUN</xsl:attribute>
								<Name xml:lang="NL">
									<xsl:value-of select="bedrijf/factuuradres/naam"/>
								</Name>
								<PostalAddress>
									<Street><xsl:value-of select="bedrijf/factuuradres/post_adres"/></Street>
									<City><xsl:value-of select="bedrijf/factuuradres/post_plaats"/></City>
									<State></State>
									<PostalCode><xsl:value-of select="bedrijf/factuuradres/post_postcode"/></PostalCode>
									<Country isoCountryCode="NL"><xsl:apply-templates select="bedrijf/factuuradres/post_land"/></Country>
								</PostalAddress>
							</xsl:element>
						</BillTo>
						<Comments></Comments>
					<Extrinsic name="Costcenter">
							<xsl:value-of select="bestelling/kostenplaats/nr"/> - <xsl:value-of select="bestelling/kostenplaats/omschrijving"/>
					</Extrinsic>
					<Extrinsic name="Custaccount">7444102</Extrinsic>
					</xsl:element>
					<xsl:for-each select="bestelopdrachtitem">
						<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="../bestelling/leverdatum/datum"/-->
								<xsl:value-of select='../bestelling/leverdatum/jaar'/>-<xsl:value-of select='../bestelling/leverdatum/maand'/>-<xsl:value-of select='../bestelling/leverdatum/dag'/>
							</xsl:attribute>
							<ItemID>
								<SupplierPartID>
								  <xsl:value-of select="substring(bestelitem/srtdeel/artikel_nummer,1,5)"/>
								</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=""/>
								<xsl:for-each select="bestelitem/kenmerk">
                    <xsl:if test="@type!='Q' and @type!='L' and @type!='I'">
                    <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">cxml</xsl:element>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="facilitor"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
