<?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:variable name="Rev">
		<xsl:value-of select="substring(translate('$Revision$', '$ ', ''), 10)"/>
	</xsl:variable>

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

	<xsl:param name="mode"/>

	<xsl:template match="bestelopdracht">
		<!--Facilitor custom XSL template for cXML bestelopdracht-->
		<sh:StandardBusinessDocument xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" xmlns:eanucc="urn:ean.ucc:2" xmlns:order="urn:ean.ucc:order:2" xmlns:gdsn="urn:ean.ucc:order:2"
		                             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		                             xsi:schemaLocation="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader                               ../Schemas/sbdh/StandardBusinessDocumentHeader.xsd urn:ean.ucc:2 ../Schemas/OrderProxy.xsd">
			<xsl:element name="sh:StandardBusinessDocumentHeader">
				<sh:HeaderVersion>2.2</sh:HeaderVersion>
				<sh:Sender><sh:Identifier Authority="DELIXL"><xsl:call-template name="delixl_klantnr"/></sh:Identifier></sh:Sender>
				<sh:Receiver><sh:Identifier Authority="EAN.UCC">8713018000000</sh:Identifier></sh:Receiver>
				<sh:DocumentIdentification>
					<sh:Standard>EAN.UCC</sh:Standard>
					<sh:TypeVersion>2.0.2</sh:TypeVersion>
					<sh:InstanceIdentifier>0000000000000</sh:InstanceIdentifier>
					<sh:Type>Order</sh:Type>
					<sh:MultipleType>false</sh:MultipleType>
					<sh:CreationDateAndTime><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</sh:CreationDateAndTime>
				</sh:DocumentIdentification>
			</xsl:element>
			<xsl:element name="eanucc:message">
				<entityIdentification>
					<uniqueCreatorIdentification>
						<xsl:value-of select="id"/>
					</uniqueCreatorIdentification>
					<contentOwner>
						<gln>0000000000000</gln>
					</contentOwner>
				</entityIdentification>
				<eanucc:transaction>
					<entityIdentification>
						<uniqueCreatorIdentification>
							<xsl:value-of select="id"/>
						</uniqueCreatorIdentification>
						<contentOwner>
							<gln>0000000000000</gln>
						</contentOwner>
					</entityIdentification>
					<command>
						<eanucc:documentCommand>
							<documentCommandHeader type="ADD">
								<entityIdentification>
									<uniqueCreatorIdentification>
										<xsl:value-of select="id"/>
									</uniqueCreatorIdentification>
									<contentOwner>
										<gln>0000000000000</gln>
									</contentOwner>
								</entityIdentification>
							</documentCommandHeader>
							<documentCommandOperand>
								<xsl:element name="order:order">
									<xsl:attribute name="documentStatus">ORIGINAL</xsl:attribute>
									<xsl:attribute name="creationDateTime"><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</xsl:attribute>
									<contentVersion><versionIdentification>2.0.2</versionIdentification></contentVersion>
									<documentStructureVersion><versionIdentification>2.0.2</versionIdentification></documentStructureVersion>
									<orderIdentification>
										<uniqueCreatorIdentification>
											<xsl:value-of select="id"/>
										</uniqueCreatorIdentification>
										<contentOwner>
											<gln>0000000000000</gln>
										</contentOwner>
									</orderIdentification>
									<orderPartyInformation>
										<seller>
											<gln>8713018000000</gln>
										</seller>
										<buyer>
											<gln>0000000000000</gln>
										</buyer>
									</orderPartyInformation>
									<orderLogisticalInformation>
										<shipToLogistics>
											<shipTo>
												<gln>0000000000000</gln>
												<additionalPartyIdentification>
													<additionalPartyIdentificationValue><xsl:call-template name="delixl_klantnr"/></additionalPartyIdentificationValue>
													<additionalPartyIdentificationType>SELLER_ASSIGNED_IDENTIFIER_FOR_A_PARTY</additionalPartyIdentificationType>
												</additionalPartyIdentification>
											</shipTo>
										</shipToLogistics>
										<orderLogisticalDateGroup>
											<requestedDeliveryDate>
												<date><xsl:value-of select="bestelling/leverdatum/jaar"/>-<xsl:value-of select="bestelling/leverdatum/maand"/>-<xsl:value-of select="bestelling/leverdatum/dag"/></date>
											</requestedDeliveryDate>
										</orderLogisticalDateGroup>
									</orderLogisticalInformation>
									<xsl:for-each select="bestelopdrachtitem">
										<xsl:sort select="bestelitem/srtdeel/artikel_nummer"/>
										<xsl:sort select="posnr"/>
										<xsl:element name="orderLineItem">
											<xsl:attribute name="unitOfMeasure"><xsl:value-of select="bestelitem/srtdeel/eenheid"/></xsl:attribute>
											<xsl:attribute name="number"><xsl:value-of select="position()"/></xsl:attribute>										
											<requestedQuantity><xsl:value-of select="aantal"/></requestedQuantity>
											<tradeItemIdentification>
												<gtin>00000000000000</gtin>
												<additionalTradeItemIdentification>
													<additionalTradeItemIdentificationValue><xsl:value-of select="bestelitem/srtdeel/artikel_nummer"/></additionalTradeItemIdentificationValue>
													<additionalTradeItemIdentificationType>SUPPLIER_ASSIGNED</additionalTradeItemIdentificationType>
												</additionalTradeItemIdentification>
											</tradeItemIdentification>
											<extension>
												<value name="portieInhoud"><xsl:value-of select="substring-before(substring-after(bestelitem/srtdeel/opmerking, 'OrPoIh:'), 'EindOrPoIh')"/></value>
												<value name="portieEenheid"><xsl:value-of select="substring-before(substring-after(bestelitem/srtdeel/opmerking, 'OrPoEh:'), 'EindOrPoEh')"/></value>
												<value name="artikelSpecificatie"><xsl:value-of select="substring-before(substring-after(bestelitem/srtdeel/opmerking, 'OrSpec:'), 'EindOrSpec')"/></value>
											</extension>
										</xsl:element>
									</xsl:for-each>
								</xsl:element>
							</documentCommandOperand>
						</eanucc:documentCommand>
					</command>
				</eanucc:transaction>
			</xsl:element>
		</sh:StandardBusinessDocument>
	</xsl:template>

	<xsl:template name="delixl_klantnr">
	    <xsl:value-of select="afleveradres/plaats/regio/district/locatie/kenmerk[@key='1040']"/>
	</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:stylesheet>
