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

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

	<xsl:param name="mode"/>

	<xsl:template match="bestelopdracht">
		<!--Facilitor custom XSL template for cXML bestelopdracht-->
		<xsl:element name="cXML">
			<xsl:attribute name="version">1.2.016</xsl:attribute>
			<xsl:attribute name="payloadID"><xsl:value-of select="key"/></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+01:00</xsl:attribute>
			<xsl:attribute name="xml:lang">NL</xsl:attribute>
			<Header>
				<From>
					<xsl:element name="Credential">
						<xsl:attribute name="domain"><xsl:value-of select="/facilitor/header/custId"/></xsl:attribute>
						<Identity/>
					</xsl:element>
				</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>
					<xsl:element name="Credential">
						<xsl:attribute name="domain">FACILITOR</xsl:attribute>
						<Identity/>
					</xsl:element>
					<UserAgent/>
				</Sender>
			</Header>
			<Request>
				<OrderRequest>
					<xsl:element name="OrderRequestHeader">
						<xsl:attribute name="orderID">O-<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: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="addressID"></xsl:attribute>
								<Name xml:lang="NL">UWV (<xsl:value-of select="afleveradres/naam"/>)</Name>
								<PostalAddress>
									<DeliverTo><xsl:value-of select="bestelling/voor/naam_full"/></DeliverTo>
									<xsl:if test="bestelling/plaats != ''">
										<DeliverTo><xsl:value-of select="bestelling/plaats"/></DeliverTo>
									</xsl:if>
									<Street><xsl:value-of select="afleveradres/bezoek_adres"/></Street>
									<City><xsl:value-of select="afleveradres/bezoek_plaats"/></City>
									<State/>
									<PostalCode><xsl:value-of select="afleveradres/bezoek_postcode"/></PostalCode>
									<Country isoCountryCode="NL"/>
								</PostalAddress>
							</xsl:element>
						</ShipTo>
						<BillTo>
							<xsl:element name="Address">
								<xsl:attribute name="addressID">AMSG3</xsl:attribute>
								<Name xml:lang="NL"><xsl:value-of select="factuuradres/naam"/></Name>
								<PostalAddress>
									<Street><xsl:value-of select="factuuradres/post_adres"/></Street>
									<City><xsl:value-of select="factuuradres/post_plaats"/></City>
									<State/>
									<PostalCode><xsl:value-of select="factuuradres/post_postcode"/></PostalCode>
									<Country isoCountryCode="NL"/>
								</PostalAddress>
							</xsl:element>
						</BillTo>
						<Contact>
							<Name xml:lang="NL"><xsl:value-of select="bestelling/voor/naam_full"/></Name>
							<!--Email><xsl:value-of select="bestelling/voor/email"/></Email-->
							<Email>servicecenterfb@uwv.nl</Email>
							<Phone>
								<TelephoneNumber>
									<CountryCode isoCountryCode="NL"/>
									<AreaOrCityCode/>
									<Number><xsl:value-of select="bestelling/voor/telefoonnr"/></Number>
								</TelephoneNumber>
							</Phone>
						</Contact>
						<Extrinsic name="Costcenter"><xsl:value-of select="bestelling/kostenplaats/nr"/></Extrinsic>
					</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="../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="bestelitem/srtdeel/artikel_nummer"/></SupplierPartID>
							</ItemID>
							<ItemDetail>
								<UnitPrice>
									<Money currency="EUR"><xsl:value-of select="format-number(prijs, '0.00', 'european')"/></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=""/>
							</ItemDetail>
						</xsl:element>
					</xsl:for-each>
				</OrderRequest>
			</Request>
		</xsl:element>
	</xsl:template>

	<xsl:template match="opdracht">
		<ns:selectOfferRequestMessage xmlns:ns="http://services.worldmeetings.com/services/v1.0/Order">
			<bookerExternalId><xsl:value-of select="melding/voor/key"/></bookerExternalId>
			<reservationId><xsl:value-of select="melding/externnr"/></reservationId>
			<offerId><xsl:value-of select="kenmerk[@naam='OFFER_ID']"/></offerId>
			<orderNumber>VO<xsl:value-of select="melding/key"/>/<xsl:value-of select="bedrijfopdr_volgnr"/></orderNumber>
			<costCenter><xsl:value-of select="kostenplaats/nr"/></costCenter>
			<memo><xsl:value-of select="omschrijving"/></memo>
		</ns:selectOfferRequestMessage>
	</xsl:template>

	<!-- Maken 'bon' (XML) -->
	<xsl:template match="facilitor">
		<xsl:apply-templates select="bestelopdracht"/>
		<xsl:apply-templates select="opdracht"/>
	</xsl:template>

	<!-- Inpakken 'bon' in SOAP envelope -->
	<xsl:template match="SOAPEnvelope">
		<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
			<soap:Body>
        <xsl:copy-of select="./node()"/>				
			</soap:Body>
		</soap:Envelope>
	</xsl:template>

	<!-- Uitpakken SOAP envelope -->
	<xsl:template match="soap:Envelope">
		<soapResult>
			<xsl:copy-of select="soap:Body/node()"/>
		</soapResult>
	</xsl:template>

	<!-- Decoderen XML response -->
	<xsl:template match="cXML">
		<Result>
			<xsl:if test="substring(Response/@code, 1)!='2'"><xsl:value-of select="Response/@text"/></xsl:if>
		</Result>
	</xsl:template>

	<!-- Root -->
	<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>
				<!-- door SendFile.cls gebruikt voor maken 'bon' (XML die via SOAP verstuurd wordt) -->
				<xsl:apply-templates select="facilitor"/>
				<!-- door SendSOAP.cls gebruikt voor inpakken van 'bon' in SOAP envelope -->
				<xsl:apply-templates select="SOAPEnvelope"/>
				<!-- door SendSOAP.cls gebruikt voor uitpakken van response uit SOAP envelope -->
				<xsl:apply-templates select="soap:Envelope"/>
				<!-- door SendFile.cls gebruikt voor lezen van status uit gefaalde response (NB: <soapResult> is al door de decode gestript) -->
				<xsl:apply-templates select="soapResult/cXML"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
