<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
  <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">
    <!-- Revision van deze cust.xsl -->
    <xsl:value-of select="substring(translate('$Revision$', '$ ', ''), 10)"/>
  </xsl:variable>

  <xsl:template match="opdracht">
    <xsl:choose>
      <xsl:when test="@type='response'">
        <!-- ******************************************************************* -->
        <!-- ***** RESPONSE naar LEVERANCIER nav. opdracht-insert/update       -->
        <!-- ***** => toevoegen envelop!                                         -->
        <!-- ******************************************************************* -->
        <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
          <soap:Body>
            <facilitor>
              <xsl:element name="opdracht">
                <xsl:attribute name="key">MAR<xsl:value-of select="@key"/></xsl:attribute>
                <xsl:attribute name="type"><xsl:value-of select="@type"/></xsl:attribute>
                <returncode><xsl:value-of select="returncode"/></returncode>
                <returntekst><xsl:value-of select="returntekst"/></returntekst>
                <datum><xsl:value-of select="datum"/></datum>
              </xsl:element>
            </facilitor>
          </soap:Body>
        </soap:Envelope>
      </xsl:when>
      <xsl:otherwise>
        <!-- ******************************************************************* -->
        <!-- ***** INSERT/UPDATE door LEVERANCIER                                -->
        <!-- ***** => UPDATE 1-op-1 door!                                        -->
        <!-- ******************************************************************* -->
        <!-- remove element prefix -->
        <xsl:element name="{local-name()}">
          <!-- process attributes -->
          <xsl:for-each select="@*">
            <!-- remove attribute prefix and 'MAR' from key-attribute!            -->
            <xsl:attribute name="{local-name()}">
              <xsl:choose><xsl:when test="local-name()='key'"><xsl:value-of select="translate(., 'MAR', '')"/></xsl:when><xsl:otherwise><xsl:value-of select="."/></xsl:otherwise></xsl:choose>
            </xsl:attribute>
          </xsl:for-each>
          <xsl:apply-templates/>          
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="facilitor">
    <xsl:apply-templates select="opdracht"/>
  </xsl:template>
  
  <xsl:template match="MaintenanceStatus">
	  <facilitor>
	  		<xsl:element name="opdracht">
	  				<xsl:attribute name="key"><xsl:value-of select="translate(InstructionData/InstructionNumber, 'MAR', '')"/></xsl:attribute>
            <xsl:attribute name="type">update</xsl:attribute>
            <status><xsl:apply-templates select="InstructionData/Status"/></status>
            <!-- Plandatum en tijd van opdracht  -->
            <!-- In INSBOU001 Release nov-2015 hiervoor node AppointmentDateTimeInformation gebruiken, daarvoor node CompletionDateTimeInformation	-->
            <plandatum><xsl:value-of select="InstructionData/AppointmentDateTimeInformation/RequiredDeliveryDate"/><xsl:text> </xsl:text><xsl:value-of select="InstructionData/AppointmentDateTimeInformation/RequiredDeliveryTime"/></plandatum>				  	
            <!-- Afwijkende gereeddatum (<> SYSDATE) kan via mut_datum, de werking is wel afhankelijk van  -->
            <xsl:if test="InstructionData/Status = 'GER'">
            <mut_datum><xsl:apply-templates select="InstructionData/DateReady"/></mut_datum>
            </xsl:if>
            <opmerking><xsl:apply-templates select="InstructionData/StatusDescription"/></opmerking>
	  		</xsl:element>
	  </facilitor>
  </xsl:template>
  
  <xsl:template match="Status">
  	<!-- In SidB zijn de volgende statussen mogelijk: -->
  	<!-- CODE  OMSCHRIJVING -->
  	<!-- AFM Afwachten, mail gestuurd -->
		<!-- AFR Afwachten, reactie opzichter -->
		<!-- AFW Afwachten in verband met weer -->
		<!-- ANN Opdracht geannuleerd -->
		<!-- BBT Bewoner belt terug -->
		<!-- GER Gereedmelding -->
		<!-- VER Verlenging -->
  	<xsl:choose>
  		<xsl:when test=".='GER'">ORDAFM</xsl:when>
  		<xsl:when test=".='ANN'">ORDCAN</xsl:when>
  		<xsl:when test=".='ACC'">ORDACP</xsl:when>
  		<!-- FSN#31934: Mareon kan niet goed overweg als ACC vaker dan 1x wordt gestuurd, NB: ACC is geen (optionele) expliciete statuscode voor accepteren door de leverancier. -->
  	</xsl:choose>
  </xsl:template>
  
  <xsl:template match="StatusDescription">
  	<xsl:value-of select="."/>
  	<xsl:text>&#10;</xsl:text>
  	<xsl:for-each select="../FreeText">
  		<xsl:value-of select="."/>
 		</xsl:for-each>
  </xsl:template>
  
  <xsl:template match="DateReady">
  <!-- Formaat DateReady is niet eenduidig volgens SidB specs, kan zijn CCYYMMDD of YYYY-MM-DD -->
  <!-- Vanwege deze onduidelijkheid gewoon maar beide formaten ondersteunen ... -->
  <!-- ... door eerst eventuele streepjes - te verwijderen, daarna gewoon weer toevoegen, dan is het formaat altijd YYYY-MM-DD -->
  	<!-- Eerst streepjes weg, dus altijd formaat YYYYMMDD -->
  	<xsl:variable name="l_afmeld_datum" select="translate(.,'-','')"/>
  	<xsl:value-of select="substring($l_afmeld_datum,1,4)"/>-<xsl:value-of select="substring($l_afmeld_datum,5,2)"/>-<xsl:value-of select="substring($l_afmeld_datum,7,2)"/>
  </xsl:template>

  <!--xsl:template match="soap:Body">
    <xsl:apply-templates select="facilitor"/>
  </xsl:template>

  <xsl:template match="soap:Envelope">
    <xsl:apply-templates select="soap:Body"/>
  </xsl:template-->

  <!-- Root -->
  <xsl:template match="*">
    <xsl:choose>
      <!-- Zonder prefix en facilitor-node, dan                                  -->
      <!-- als response: toevoegen envelop!                                      -->
      <!--   als update: 1-op-1 door!                                            -->
      <xsl:when test="substring-before(name(),concat(':', local-name()))='' and local-name()='facilitor'">
        <xsl:apply-templates select="facilitor"/>
      </xsl:when>
      <xsl:when test="substring-before(name(),concat(':', local-name()))='' and local-name()='MaintenanceStatus'">
        <xsl:apply-templates select="MaintenanceStatus"/>
      </xsl:when>
      <!--xsl:when test="local-name()='Envelope'">
        <xsl:apply-templates select="soap:Envelope"/>
      </xsl:when-->
      <!-- Met prefix, dus insert/update: prefix weg + evt. velden "injecteren"! -->
      <xsl:otherwise>
        <!-- remove element prefix -->
        <xsl:element name="{local-name()}">
          <!-- process attributes -->
          <xsl:for-each select="@*">
            <!-- remove attribute prefix and 'MAR' from key-attribute!            -->
            <xsl:attribute name="{local-name()}">
              <xsl:choose><xsl:when test="local-name()='key'"><xsl:value-of select="translate(., 'MAR', '')"/></xsl:when><xsl:otherwise><xsl:value-of select="."/></xsl:otherwise></xsl:choose>
            </xsl:attribute>
          </xsl:for-each>
          <xsl:apply-templates/>
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
