<?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/">
   <xsl:output method="xml" encoding="utf-8"/>
   <xsl:decimal-format name="european" decimal-separator="." grouping-separator=","/>
   <xsl:param name="mode"/>

   <!-- Maken 'bon' (XML) -->
   <xsl:template match="bestelopdracht">
      <xsl:element name="Update_SalesOrders">
			<xsl:attribute name="xmlnsn">http://com.infor.portals.soap/</xsl:attribute>
         <xsl:element name="Delta">
            <xsl:element name="SalesOrder">
               <xsl:element name="TypeOfChange">Insert</xsl:element>
			   <xsl:element name="OrderText"><xsl:value-of select="bestelling/kenmerk[@key=522]"/></xsl:element>
               <xsl:element name="BusinessObjectId">0</xsl:element>
               <xsl:element name="SelectedShipToAddress">
                  <xsl:variable name="varNaam"><xsl:value-of select="afleveradres/naam"/></xsl:variable>
				      <xsl:value-of select="substring($varNaam,1,5)"/>
               </xsl:element>
			   <xsl:element name="CostCentre"><xsl:value-of select="bestelling/kostenplaats/nr"/></xsl:element>
               <xsl:element name="DelAddressCountryCode">NL</xsl:element>
               <xsl:element name="OrderDate">
                  <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:element>
               <xsl:element name="PlannedDeliveryDate">0001-01-01T00:00:00</xsl:element>
               <xsl:element name="CustomerOrderNumber">O-<xsl:value-of select="id"/></xsl:element>
               <xsl:element name="OrderStatus">0</xsl:element>
               <xsl:element name="IsEditable">false</xsl:element>
               <xsl:element name="SalesOrderLines">
               
					<xsl:for-each select="bestelopdrachtitem">
						<xsl:sort select="srtdeel/omschrijving"/>
						<xsl:sort select="posnr"/>
                  <xsl:element name="SalesOrderLine">
                     <xsl:element name="TypeOfChange">Insert</xsl:element>
                     <xsl:element name="BusinessObjectId">0</xsl:element>
                     <xsl:element name="Position">0</xsl:element>
                     <xsl:element name="ItemCode"><xsl:value-of select="bestelitem/srtdeel/artikel_nummer"/></xsl:element>
                     <xsl:element name="Quantity"><xsl:value-of select="bestelitem/aantal"/></xsl:element>
                     <xsl:element name="Price"><xsl:value-of select="bestelitem/srtdeel/prijs"/></xsl:element>
                     <xsl:element name="GrossLineAmount">0</xsl:element>
                     <xsl:element name="LineDiscount">0</xsl:element>
                     <xsl:element name="NettLineAmount">0</xsl:element>
                     <xsl:element name="OrderDiscount">0</xsl:element>
                     <xsl:element name="PlannedDeliveryDate">
                        <xsl:value-of select="../bestelling/leverdatum/jaar"/>-<xsl:value-of select="../bestelling/leverdatum/maand"/>-<xsl:value-of select="../bestelling/leverdatum/dag"/>T<xsl:value-of select="../bestelling/leverdatum/tijd"/>:00+01:00
                     </xsl:element>
                     <xsl:element name="ItemVariantCode">0</xsl:element>
                     <xsl:element name="IsEditable">false</xsl:element>
                     <xsl:element name="VatAmount">0</xsl:element>
                     <xsl:element name="ConversionFactor">0</xsl:element>
                     <xsl:element name="DiscountPercentage">0</xsl:element>
                     <xsl:element name="TotalExclusiveVat">0</xsl:element>
                     <xsl:element name="TotalInclusiveVat">0</xsl:element>
                     <xsl:element name="Original">
                        <!--<xsl:attribute name="xsi:nil">true</xsl:attribute>-->
                     </xsl:element>
                     <xsl:element name="ProcessingInformation">
                        <!--<xsl:attribute name="xsi:nil">true</xsl:attribute>-->
                     </xsl:element>
                  </xsl:element>
					</xsl:for-each>
					
               </xsl:element>
               <xsl:element name="OrderGrossAmount">0</xsl:element>
               <xsl:element name="OrderDiscount">0</xsl:element>
               <xsl:element name="InCart">false</xsl:element>
               <xsl:element name="TotalVatAmount">0</xsl:element>
               <xsl:element name="TotalAmountInclusiveVat">0</xsl:element>
               <xsl:element name="Original">
                  <!--<xsl:attribute name="xsi:nil">true</xsl:attribute>-->
               </xsl:element>
               <xsl:element name="ProcessingInformation">
                  <!--<xsl:attribute name="xsi:nil">true</xsl:attribute>-->
               </xsl:element>
            </xsl:element>
         </xsl:element>
         <xsl:element name="CustomerCode">320001968</xsl:element>
       </xsl:element>
   </xsl:template>

   <!-- Maken 'bon' (XML) -->
   <xsl:template match="facilitor">
      <xsl:apply-templates select="bestelopdracht"/>
   </xsl:template>
   
   <!-- Inpakken 'bon' in SOAP envelope -->
   <xsl:template match="SOAPEnvelope">
      <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         <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="Update_SalesOrdersResponse">
      <xsl:choose>
	  	<xsl:when test="SalesOrders/SalesOrder/TypeOfChange='Updated'">
      		<Result/>
	  	</xsl:when>
		<xsl:otherwise>
			<Result>
				<xsl:for-each select="SalesOrders/SalesOrder/SalesOrderLines/SalesOrderLine">
					<xsl:for-each select="ProcessingInformation/Info">
						<xsl:if test="Type!='Succes' and Description!=''">[<xsl:value-of select="../../ItemCode"/>] <xsl:value-of select="Type"/> (<xsl:value-of select="Code"/>): <xsl:value-of select="Description"/><xsl:text disable-output-escaping="yes">&#10;</xsl:text>
						</xsl:if>
					</xsl:for-each>
				</xsl:for-each>
				<xsl:for-each select="SalesOrders/SalesOrder/ProcessingInformation/Info">
					<xsl:if test="Type!='Succes' and Description!=''">[Order] <xsl:value-of select="Type"/> (<xsl:value-of select="Code"/>): <xsl:value-of select="Description"/><xsl:text disable-output-escaping="yes">&#10;</xsl:text>
					</xsl:if>
				</xsl:for-each>
			</Result>
		</xsl:otherwise>
	  </xsl:choose>
   </xsl:template>

   <!-- Decoderen XML response -->
   <xsl:template match="soap:Fault">
      <Result>
         <xsl:value-of select="faultstring"/>
      </Result>
   </xsl:template>


   <!-- Root -->
   <xsl:template match="/">
      <xsl:choose>
         <xsl:when test="$mode='getExtension'">
            <xsl:element name="format">
               <!-- Hotelselect gebruikt geen cXML, maar SendFile.cls verwacht wel deze parameter om een XML response in te lezen -->
               <xsl:element name="extension">cxml</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 succesvolle response (NB: <soapResult> is al door de decode gestript) -->
            <xsl:apply-templates select="Update_SalesOrdersResponse"/>
            <!-- door SendFile.cls gebruikt voor lezen van status uit gefaalde response (NB: <soapResult> is al door de decode gestript) -->
            <xsl:apply-templates select="soap:Fault"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
	
</xsl:stylesheet>