<?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>
		<!-- Define - "brute force" - variable to determine besteller-bedrijf -->
		<xsl:variable name="b4">
			<xsl:value-of select="bestelling/voor/afdeling/parent/parent/parent/parent/bedrijf/naam"/>
		</xsl:variable>
		<xsl:variable name="b3">
			<xsl:value-of select="bestelling/voor/afdeling/parent/parent/parent/bedrijf/naam"/>
		</xsl:variable>
		<xsl:variable name="b2">
			<xsl:value-of select="bestelling/voor/afdeling/parent/parent/bedrijf/naam"/>
		</xsl:variable>
		<xsl:variable name="b1">
			<xsl:value-of select="bestelling/voor/afdeling/parent/bedrijf/naam"/>
		</xsl:variable>
		<xsl:variable name="b0">
			<xsl:value-of select="bestelling/voor/afdeling/bedrijf/naam"/>
		</xsl:variable>
		<xsl:variable name="klantnaam">
			<xsl:choose>
				<xsl:when test="b4!=''">
					<xsl:value-of select="$b4"/>
				</xsl:when>
				<xsl:when test="$b3!=''">
					<xsl:value-of select="$b3"/>
				</xsl:when>
				<xsl:when test="$b2!=''">
					<xsl:value-of select="$b2"/>
				</xsl:when>
				<xsl:when test="$b1!=''">
					<xsl:value-of select="$b1"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$b0"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<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">
							<!--Of fixed Supplier???-->
							<xsl:value-of select="$klantnaam"/>
						</xsl:attribute>
						<Identity>
							<!--xsl:value-of select="substring-after(bestelling/aanvrager/afdeling/kostenplaats/nr,'-')"/-->
							<xsl:value-of select="bestelling/voor/afdeling/kenmerk[@key='1020']"/>
						</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">
									<!--xsl:value-of select="substring-after(bestelling/aanvrager/afdeling/kostenplaats/nr,'-')"/-->
									<xsl:value-of select="bestelling/voor/afdeling/kenmerk[@key='1020']"/>
								</xsl:attribute>
								<Name xml:lang="NL">
									<xsl:value-of select="bestelling/voor/naam_full"/>
								</Name>
								<PostalAddress>
									<DeliverTo>
										<xsl:value-of select="bestelling/voor/email"/>
									</DeliverTo>
									<Street>
										<xsl:value-of select="bestelling/voor/kenmerk[@key='1001']"/>
									</Street>
									<City>
										<xsl:value-of select="bestelling/voor/kenmerk[@key='1003']"/>
									</City>
									<State/>
									<PostalCode>
										<xsl:value-of select="bestelling/voor/kenmerk[@key='1002']"/>
									</PostalCode>
									<Country isoCountryCode="NL"/>
								</PostalAddress>
								<Email>
									<xsl:value-of select="bestelling/voor/email"/>
								</Email>
								<Phone>
									<TelephoneNumber>
										<CountryCode isoCountryCode="NL"/>
										<AreaOrCityCode/>
										<Number>
											<xsl:value-of select="bestelling/voor/telefoonnr"/>
										</Number>
									</TelephoneNumber>
								</Phone>
							</xsl:element>
						</ShipTo>
						<BillTo>
							<xsl:element name="Address">
								<Name xml:lang="NL"/>
							</xsl:element>
						</BillTo>
						<Comments>
							<xsl:value-of select="bestelling/kenmerk[@key='1']"/>
						</Comments>
					</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'">
										<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>
