<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:ScriptNS0="http://schemas.microsoft.com/BizTalk/2003/ScriptNS0"
                xmlns:ScriptNS1="http://schemas.microsoft.com/BizTalk/2003/ScriptNS1"
                
                xmlns:DateTime="TM.Shared.Components.DateTime"
                xmlns:SSO="TM.Shared.Components.SSO"
                xmlns:String="TM.Shared.Components.String"

                xmlns:s0="http://TM.KLG.com/KLG/OrderStatus"
                xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006"
                 
                exclude-result-prefixes="s0 ns0 ScriptNS0 ScriptNS1 DateTime SSO String">
  
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" indent="yes" />

  <!-- Region global variable initialization -->

  <!-- region connectionString -->
  <xsl:variable name="connectionString" select="SSO:GetValue('TM.Bosman', 'ESBLookupConnectionString2016')"/>
  <!-- end region -->

  <!-- end region -->

  <!-- region templates -->

  <xsl:template match="/">
    <xsl:apply-templates select="/s0:StatusReport" />
  </xsl:template>

  <xsl:template match="/s0:StatusReport">

    <ns0:EFACT_D96A_IFTSTA_BOSMAN>
      <UNH>
        <UNH1>
          <xsl:value-of select="DateTime:FormatDateTime(TimeStamp, 'yyyy-MM-ddTHH:mm:ss', 'yyyyMMddHHmmss')" />
        </UNH1>
        <UNH2>
          <UNH2.1>IFTSTA</UNH2.1>
          <UNH2.2>D</UNH2.2>
          <UNH2.3>96A</UNH2.3>
          <UNH2.4>UN</UNH2.4>
          <UNH2.5>EAN002</UNH2.5>
        </UNH2>
      </UNH>
      <ns0:BGM>
        <!-- Max length of BGM02 is 35 -->
        <BGM02>
          <xsl:value-of select="substring(/s0:StatusReport/Status/ShipmentReference,1,35)"/>
        </BGM02>
      </ns0:BGM>
      <ns0:DTM>
        <ns0:C507>
          <C50701>137</C50701>
          <C50702>
            <xsl:value-of select="DateTime:FormatDateTime(TimeStamp, 'yyyy-MM-ddTHH:mm:ss', 'yyyyMMdd')" />
          </C50702>
          <C50703>102</C50703>
        </ns0:C507>
      </ns0:DTM>
      <ns0:CNILoop1>
        <ns0:CNI>
          <!-- Momenteel bevat Shared.Status maximaal 1 bericht -->
          <CNI01>1</CNI01>
          <ns0:C503>
            <C50301>
              <xsl:choose>
                <xsl:when test="contains(/s0:StatusReport/Status/ShipmentReference,'/')">
                  <xsl:value-of select="normalize-space(substring-after(/s0:StatusReport/Status/ShipmentReference,'/'))"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="/s0:StatusReport/Status/ShipmentReference"/>
                </xsl:otherwise>
              </xsl:choose>
            </C50301>
          </ns0:C503>
        </ns0:CNI>
        <ns0:STSLoop1>
          <ns0:STS>
            <ns0:C601>
              <C60101>1</C60101>
            </ns0:C601>
            <ns0:C555>
              <C55501>
                <xsl:variable name="status">
                  <xsl:choose>
                    <xsl:when test="ScriptNS0:DBValueExtract(string(ScriptNS0:DBLookup(0, /s0:StatusReport/Status/StatusCode, $connectionString, 'TM_Bosman_StatusCode', 'KLG')), 'Bosman') != ''">
                      <xsl:value-of select="ScriptNS0:DBValueExtract(string(ScriptNS0:DBLookup(0, /s0:StatusReport/Status/StatusCode, $connectionString, 'TM_Bosman_StatusCode', 'KLG')), 'Bosman')" />
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="/s0:StatusReport/Status/StatusCode"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:value-of select="$status"/>
              </C55501>
            </ns0:C555>
          </ns0:STS>
          <ns0:DTM_3>
            <ns0:C507_3>
              <C50701>334</C50701>
              <xsl:choose>
                <xsl:when test="String:Left(Status/StatusTimeStamp,1)>'0'">
                  <C50702>
                    <xsl:value-of select="DateTime:FormatDateTime(Status/StatusTimeStamp, 'yyyy-MM-ddTHH:mm:ss', 'yyyyMMddHHmm')" />
                  </C50702>
                  <C50703>203</C50703>
                </xsl:when>
                <xsl:otherwise>
                  <C50702>000101010000</C50702>
                  <C50703>102</C50703>
                </xsl:otherwise>
              </xsl:choose>
            </ns0:C507_3>
          </ns0:DTM_3>
        </ns0:STSLoop1>
      </ns0:CNILoop1>
      <UNT>
        <UNT1>0</UNT1>
        <UNT2>
          <xsl:value-of select="DateTime:FormatDateTime(TimeStamp, 'yyyy-MM-ddTHH:mm:ss', 'yyyyMMddHHmmss')" />
        </UNT2>
      </UNT>
      
    </ns0:EFACT_D96A_IFTSTA_BOSMAN>
  </xsl:template>
  

  <!-- end region -->

  <xsl:variable name="dbClose" select="ScriptNS0:DBLookupShutdown()"/>

</xsl:stylesheet>