<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
								xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
								xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
								xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
								xmlns:msxml="urn:schemas-microsoft-com:xslt">
	<xsl:output method="xml" encoding="utf-8"/>
	<xsl:decimal-format name="european" decimal-separator="." grouping-separator=","/>
	<xsl:param name="mode"/>

	<xsl:template match="bestelopdracht">
		<xsl:comment>Euro florist order.</xsl:comment>
		<xsl:element name="cXML">
			<xsl:attribute name="version">1.2.016</xsl:attribute>
			<xsl:attribute name="payloadID">O-<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+01:00</xsl:attribute>
			<xsl:attribute name="xml:lang">NL</xsl:attribute>
			<xsl:variable name="min_wpnr">
				<xsl:for-each select="bestelling/voor/werkplek">
					<xsl:sort select="volgnr" data-type="number"/>
					<xsl:if test="position()=1"><xsl:copy-of select="."/></xsl:if>
				</xsl:for-each>
			</xsl:variable>
			<Header>
				<From>
					<xsl:element name="Credential">
						<xsl:attribute name="domain">
							<xsl:value-of select="/facilitor/header/custId"/>
						</xsl:attribute>
						<Identity><xsl:value-of select="bedrijf/overeenkomst_nr"/></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>
					<Credential domain="Facilitor">
						<Identity/>
					</Credential>
					<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/datum"/>
						</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:choose>
									<xsl:when test="bestelling/kenmerk[@naam='Plaats']">
										<xsl:attribute name="addressID"/>
										<Name xml:lang="NL">
											<xsl:value-of select="bestelling/kenmerk[@naam='Bestemd voor']"/>
										</Name>
										<PostalAddress name="default">
											<DeliverTo>
												<xsl:value-of select="bestelling/kenmerk[@naam='Bestemd voor']"/> (Afgeven bij: <xsl:value-of select="bestelling/kenmerk[@naam='Afgeven bij']"/>)
											</DeliverTo>
											<Street>
												<xsl:value-of select="bestelling/kenmerk[@naam='Straat huisnummer']"/>
											</Street>
											<City>
												<xsl:value-of select="bestelling/kenmerk[@naam='Plaats']"/>
											</City>
											<State/>
											<PostalCode>
												<xsl:value-of select="bestelling/kenmerk[@naam='Postcode']"/>
											</PostalCode>
											<Phone>
												<TelephoneNumber>
													<CountryCode isoCountryCode="NL"/>
													<AreaOrCityCode/>
													<Number>
														<xsl:value-of select="bestelling/kenmerk[@naam='Telefoon']"/>
													</Number>
												</TelephoneNumber>
											</Phone>
											<Country isoCountryCode="NL"/>
										</PostalAddress>
									</xsl:when>
									<xsl:otherwise>
										<!--Als addressID is gebouwcode gewenst (door Euro Florist locatiecode genoemd)!-->
										<xsl:variable name="varNaam">
											<xsl:value-of select="afleveradres/naam"/>
										</xsl:variable>
										<xsl:attribute name="addressID">
											<xsl:value-of select="substring($varNaam,1,5)"/>
										</xsl:attribute>
										<Name xml:lang="NL">UWV (<xsl:value-of select="afleveradres/naam"/>)</Name>
										<PostalAddress>
											<DeliverTo>
												<xsl:value-of select="bestelling/kenmerk[@naam='Bestemd voor']"/> (Afgeven bij: <xsl:value-of select="bestelling/kenmerk[@naam='Afgeven bij']"/>)
											</DeliverTo>
											<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>
											<Phone>
												<TelephoneNumber>
													<CountryCode isoCountryCode="NL"/>
													<AreaOrCityCode/>
													<Number>
														<xsl:value-of select="bestelling/kenmerk[@naam='Telefoon']"/>
													</Number>
												</TelephoneNumber>
											</Phone>
											<Country isoCountryCode="NL"/>
										</PostalAddress>
									</xsl:otherwise>
								</xsl:choose>
							</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/aanvrager/naam_full"/>
							</Name>
							<Email>
								<xsl:value-of select="bestelling/aanvrager/email"/>
							</Email>
							<Phone>
								<TelephoneNumber>
									<CountryCode isoCountryCode="NL"/>
									<AreaOrCityCode/>
									<Number>
										<xsl:value-of select="bestelling/aanvrager/telefoonnr"/>
									</Number>
								</TelephoneNumber>
							</Phone>
						</Contact>
						<Extrinsic name="BezorgInfo">
							<xsl:value-of select="bestelling/kenmerk[@naam='Bezorg info']"/>
							<!--xsl:if test="bestelling/activiteit != ''">[Abonnement] </xsl:if><xsl:value-of select="bestelling/kenmerk[@naam='Bezorg info']"/-->
						</Extrinsic>
						<Extrinsic name="BloemistNummer">
							<xsl:value-of select="bestelling/kenmerk[@naam='Bloemistnummer']"/>
						</Extrinsic>
						<Extrinsic name="Kaartje">
							<xsl:value-of select="bestelling/kenmerk[@naam='Kaartje']"/>
						</Extrinsic>
						<Extrinsic name="CostCenter">
							<xsl:value-of select="bestelling/kostenplaats/nr"/>
						</Extrinsic>
						<Extrinsic name="Kavel">
							<xsl:value-of select="msxml:node-set($min_wpnr)/werkplek/plaats/regio/district/locatie/gebouw/kenmerk[@naam='Kavel']"/>
						</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="/facilitor/bestelopdracht/bestelling/leverdatum/datum"/>
							</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/>
								<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="SOAPEnvelope">
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
		  <soap:Body>
		    <ReceiveStringOrder xmlns="http://cos.euroflorist.com/uwv">
		      <xmlOrder><xsl:value-of select="/"/></xmlOrder>
		    </ReceiveStringOrder>
		  </soap:Body>
		</soap:Envelope>
	</xsl:template>

  <!-- Uitpakken SOAP envelope -->
	<xsl:template match="soap:Envelope">
	  <xml><xsl:value-of select="soap:Body"/></xml>
	</xsl:template>

  <!-- Decoderen cXML response -->
	<xsl:template match="cXML/Response/Status">
	    <Result>
			<xsl:if test="@code!='200'">
		    <xsl:value-of select="@code"/>:<xsl:value-of select="."/>
			</xsl:if>
	    </Result>
	</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:apply-templates select="SOAPEnvelope"/>
				<xsl:apply-templates select="soap:Envelope"/>
				<xsl:apply-templates select="cXML/Response"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
