namespace TM.EDIBroker.TransSmart.Transforms {
    
    
    [Microsoft.XLANGs.BaseTypes.SchemaReference(@"TM.EDIBroker.TransSmart.Schemas.Shipment", typeof(global::TM.EDIBroker.TransSmart.Schemas.Shipment))]
    [Microsoft.XLANGs.BaseTypes.SchemaReference(@"TM.Shared.Schemas.Order", typeof(global::TM.Shared.Schemas.Order))]
    public sealed class TM_EDIBroker_TransSmart_Shipment_to_TM_Shared_Order : global::Microsoft.XLANGs.BaseTypes.TransformBase {
        
        private const string _strMap = @"<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.EDIBroker.TransSmart.Shipment""
                xmlns:ns0=""http://TM.KLG.com/KLG/Order""
                
                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 -->

  <xsl:key name=""groepje"" match=""s0:shipmentLine"" use=""concat(generate-id(..), '|', s0:lineNo)"" />


  <xsl:variable name=""connectionString"" select=""SSO:GetValue('TM.EDIBroker.TransSmart', 'ESBLookupConnectionString')""/>
  <xsl:variable name=""sender"" select=""'TUUCIOISTE01'""/>
  <xsl:variable name=""receiver"" select=""'KLGEUVENLO01'""/>
  <xsl:variable name=""currentDate"" select=""DateTime:GetCurrentDate()"" />

  <!-- end region -->

  <!-- region templates -->

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

  <xsl:template match=""/s0:carrierBooking"">
    <ns0:Order>
      <Sender>
        <xsl:value-of select=""$sender""/>
      </Sender>
      <Receiver>
        <xsl:value-of select=""$receiver""/>
      </Receiver>
      <GroupName>
        <xsl:text>N/A</xsl:text>
      </GroupName>
      <Consolidation>
        <xsl:text>N/A</xsl:text>
      </Consolidation>
      <Reference>
        <xsl:value-of select=""s0:shipments[1]/s0:shipmentInfo[1]/s0:reference""/>
      </Reference>
      <TimeStamp>
        <xsl:value-of select=""DateTime:GetCurrentDateTime()"" />
      </TimeStamp>
      <Status>NEW</Status>
      <Date>
        <xsl:value-of select=""$currentDate"" />
      </Date>
      <FullTruckLoad>false</FullTruckLoad>
      <xsl:for-each select=""s0:shipments/s0:shipmentInfo"">
        <Shipment>
          <xsl:call-template name=""Shipment""/>
        </Shipment>
      </xsl:for-each>
    </ns0:Order>
  </xsl:template>

  <xsl:template name=""Shipment"">

    <Consignor>
      <xsl:value-of select=""$sender""/>
    </Consignor>
    <ConsignorReference>
      <xsl:value-of select=""s0:reference/text()""/>
    </ConsignorReference>

    <Debtor>
      <xsl:value-of select=""$sender""/>
    </Debtor>

    <DebtorReference>
      <xsl:value-of select=""s0:reference/text()""/>
    </DebtorReference>

    <Incoterm>
      <xsl:choose>
        <xsl:when test=""s0:incoterms/text() = 'DDU'"">
          <xsl:value-of select=""'DAP'""/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select=""s0:incoterms/text()""/>
        </xsl:otherwise>
      </xsl:choose>
    </Incoterm>
    <IncotermCity/>
    <IncotermCountryCode/>

    <COD>0</COD>
    <CODCurrency>EUR</CODCurrency>

    <xsl:call-template name=""Address_SF"">
    </xsl:call-template>

    <xsl:call-template name=""Address_ST"">
    </xsl:call-template>

    <xsl:for-each select=""s0:shipmentLines/s0:shipmentLine[generate-id() = generate-id(key('groepje', concat(generate-id(..), '|', s0:lineNo))[1])]"">
      <Good>
        <xsl:call-template name=""Good"">
          <xsl:with-param name=""reference"" select = ""REPLACE"" />
        </xsl:call-template>
      </Good>
    </xsl:for-each>

    <Parametergroup>
      <Qualifier>
        <xsl:text>Scenario</xsl:text>
      </Qualifier>

      <Parameter>
        <Qualifier>
          <xsl:text>Scenario</xsl:text>
        </Qualifier>
        <Value>
          <xsl:text>21</xsl:text>
        </Value>
      </Parameter>
    </Parametergroup>
  </xsl:template>

  <xsl:template name=""Address_SF"">

    <xsl:variable name=""collectionDate"" select=""concat(substring(s0:pickupDate/text(), 1, 4), '-', substring(s0:pickupDate/text(), 5, 2), '-', substring(s0:pickupDate/text(), 7, 2))""/>
    <xsl:variable name=""collectionTimeFrom"" select=""concat(substring(s0:pickupTime/text(), 1, 2), ':', substring(s0:pickupTime/text(), 3, 2), ':00')""/>
    <xsl:variable name=""collectionTimeUntill"" select=""concat(substring(s0:pickupTimeTo/text(), 1, 2), ':', substring(s0:pickupTimeTo/text(), 3, 2), ':00')""/>

    <xsl:variable name=""loadingDateTimeFrom"">
      <xsl:choose>
        <xsl:when test=""not($collectionDate = '00000000') and not(normalize-space($collectionDate)='') and string-length($collectionDate) = 10"">
          <xsl:choose>
            <xsl:when test=""not(normalize-space($collectionTimeFrom)='') and string-length($collectionTimeFrom) = 8"">
              <xsl:value-of select=""concat($collectionDate, 'T', $collectionTimeFrom)"" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select=""concat($collectionDate, 'T07:00:00')"" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select=""concat($currentDate, 'T07:00:00')""/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name=""loadingDateTimeTo"">
      <xsl:choose>
        <xsl:when test=""not($collectionDate = '00000000') and not(normalize-space($collectionDate)='') and string-length($collectionDate) = 10"">
          <xsl:choose>
            <xsl:when test=""not(normalize-space($collectionTimeUntill)='') and string-length($collectionTimeUntill) = 8"">
              <xsl:value-of select=""concat($collectionDate, 'T', $collectionTimeUntill)"" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select=""concat($collectionDate, 'T16:00:00')"" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select=""concat($currentDate, 'T16:00:00')""/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <Address>
      <xsl:call-template name=""Address"">
        <xsl:with-param name=""addressType"" select = ""'SF'"" />
        <xsl:with-param name=""name1"" select = ""s0:addresses/s0:address[s0:type = 'SEND']/s0:name"" />
        <xsl:with-param name=""name2"" select = ""''"" />
        <xsl:with-param name=""address1"" select = ""s0:addresses/s0:address[s0:type = 'SEND']/s0:street1"" />
        <xsl:with-param name=""address2"" select = ""s0:addresses/s0:address[s0:type = 'SEND']/s0:street2"" />
        <xsl:with-param name=""postalCode"" select = ""s0:addresses/s0:address[s0:type = 'SEND']/s0:zipCode"" />
        <xsl:with-param name=""city"" select = ""s0:addresses/s0:address[s0:type = 'SEND']/s0:city"" />
        <xsl:with-param name=""region"" select = ""s0:addresses/s0:address[s0:type = 'SEND' and s0:countryCode = 'IE']/s0:state"" />
        <xsl:with-param name=""countryCode"" select = ""s0:addresses/s0:address[s0:type = 'SEND']/s0:countryCode"" />
        <xsl:with-param name=""start"" select = ""$loadingDateTimeFrom"" />
        <xsl:with-param name=""end"" select = ""$loadingDateTimeTo"" />
        <xsl:with-param name=""reference"" select = ""s0:reference"" />
        <xsl:with-param name=""externalRemark1"" select = ""''"" />
        <xsl:with-param name=""externalRemark2"" select = ""''"" />
        <xsl:with-param name=""internalRemark1"" select = ""''"" />
        <xsl:with-param name=""internalRemark2"" select = ""''"" />
      </xsl:call-template>
      <xsl:call-template name=""ContactPerson"">
        <xsl:with-param name=""name"" select = ""s0:addresses/s0:address[s0:type = 'SEND']/s0:contact"" />
        <xsl:with-param name=""phone"" select = ""s0:addresses/s0:address[s0:type = 'SEND']/s0:telNo"" />
        <xsl:with-param name=""fax"" select = ""''"" />
        <xsl:with-param name=""email"" select = ""s0:addresses/s0:address[s0:type = 'SEND']/s0:email"" />
      </xsl:call-template>
    </Address>
  </xsl:template>

  <xsl:template name=""Address_ST"">
    <xsl:param name=""reference"" />

    <xsl:variable name=""deliveryDate"" select=""concat(substring(s0:requestedDeliveryDate/text(), 1, 4), '-', substring(s0:requestedDeliveryDate/text(), 5, 2), '-', substring(s0:requestedDeliveryDate/text(), 7, 2))""/>
    <xsl:variable name=""deliveryTimeFrom"" select=""concat(substring(s0:requestedDeliveryTime/text(), 1, 2), ':', substring(s0:requestedDeliveryTime/text(), 3, 2), ':00')""/>
    <xsl:variable name=""deliveryTimeUntill"" select=""concat(substring(s0:requestedDeliveryTimeTo/text(), 1, 2), ':', substring(s0:requestedDeliveryTimeTo/text(), 3, 2), ':00')""/>

    <xsl:variable name=""deliveryDateTimeFrom"">
      <xsl:choose>
        <xsl:when test=""not($deliveryDate = '00000000') and not(normalize-space($deliveryDate)='') and string-length($deliveryDate) = 10"">
          <xsl:choose>
            <xsl:when test=""not(normalize-space($deliveryTimeFrom)='') and string-length($deliveryTimeFrom) = 8"">
              <xsl:value-of select=""concat($deliveryDate, 'T', $deliveryTimeFrom)"" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select=""concat($deliveryDate, 'T07:00:00')"" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select=""concat($currentDate, 'T07:00:00')""/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name=""deliveryDateTimeTo"">
      <xsl:choose>
        <xsl:when test=""not($deliveryDate = '00000000') and not(normalize-space($deliveryDate)='') and string-length($deliveryDate) = 10"">
          <xsl:choose>
            <xsl:when test=""not(normalize-space($deliveryTimeUntill)='') and string-length($deliveryTimeUntill) = 8"">
              <xsl:value-of select=""concat($deliveryDate, 'T', $deliveryTimeUntill)"" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select=""concat($deliveryDate, 'T16:00:00')"" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select=""concat($currentDate, 'T16:00:00')""/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <Address>
      <xsl:call-template name=""Address"">
        <xsl:with-param name=""addressType"" select = ""'ST'"" />
        <xsl:with-param name=""name1"" select = ""s0:addresses/s0:address[s0:type = 'RECV']/s0:name"" />
        <xsl:with-param name=""name2"" select = ""''"" />
        <xsl:with-param name=""address1"" select = ""s0:addresses/s0:address[s0:type = 'RECV']/s0:street1"" />
        <xsl:with-param name=""address2"" select = ""s0:addresses/s0:address[s0:type = 'RECV']/s0:street2"" />
        <xsl:with-param name=""postalCode"" select = ""s0:addresses/s0:address[s0:type = 'RECV']/s0:zipCode"" />
        <xsl:with-param name=""city"" select = ""s0:addresses/s0:address[s0:type = 'RECV']/s0:city"" />
        <xsl:with-param name=""region"" select = ""s0:addresses/s0:address[s0:type = 'RECV' and s0:countryCode = 'IE']/s0:state"" />
        <xsl:with-param name=""countryCode"" select = ""s0:addresses/s0:address[s0:type = 'RECV']/s0:countryCode"" />
        <xsl:with-param name=""start"" select = ""$deliveryDateTimeFrom"" />
        <xsl:with-param name=""end"" select = ""$deliveryDateTimeTo"" />
        <xsl:with-param name=""reference"" select = ""s0:reference"" />
        <xsl:with-param name=""externalRemark1"" select = ""''"" />
        <xsl:with-param name=""externalRemark2"" select = ""s0:instruction"" />
        <xsl:with-param name=""internalRemark1"" select = ""''"" />
        <xsl:with-param name=""internalRemark2"" select = ""''"" />
      </xsl:call-template>
      <xsl:call-template name=""ContactPerson"">
        <xsl:with-param name=""name"" select = ""s0:addresses/s0:address[s0:type = 'RECV']/s0:contact"" />
        <xsl:with-param name=""phone"" select = ""s0:addresses/s0:address[s0:type = 'RECV']/s0:telNo"" />
        <xsl:with-param name=""fax"" select = ""''"" />
        <xsl:with-param name=""email"" select = ""s0:addresses/s0:address[s0:type = 'RECV']/s0:email"" />
      </xsl:call-template>
    </Address>
  </xsl:template>

  <xsl:template name=""Address"">
    <xsl:param name=""addressType"" />
    <xsl:param name=""name1"" />
    <xsl:param name=""name2"" />
    <xsl:param name=""address1"" />
    <xsl:param name=""address2"" />
    <xsl:param name=""postalCode"" />
    <xsl:param name=""city"" />
    <xsl:param name=""region"" />
    <xsl:param name=""countryCode"" />
    <xsl:param name=""start"" />
    <xsl:param name=""end"" />
    <xsl:param name=""reference"" />
    <xsl:param name=""externalRemark1"" />
    <xsl:param name=""externalRemark2"" />
    <xsl:param name=""internalRemark1"" />
    <xsl:param name=""internalRemark2"" />
    <AddressType>
      <xsl:value-of select=""$addressType"" />
    </AddressType>
    <Name1>
      <xsl:value-of select=""$name1"" />
    </Name1>
    <Name2>
      <xsl:value-of select=""$name2"" />
    </Name2>
    <Address1>
      <xsl:value-of select=""$address1"" />
    </Address1>
    <Address2>
      <xsl:value-of select=""$address2"" />
    </Address2>
    <PostalCode>
      <xsl:value-of select=""$postalCode"" />
    </PostalCode>
    <City>
      <xsl:value-of select=""$city"" />
    </City>
    <Region>
      <xsl:value-of select=""$region"" />
    </Region>
    <CountryCode>
      <xsl:value-of select=""$countryCode"" />
    </CountryCode>
    <Start>
      <xsl:value-of select=""$start"" />
    </Start>
    <End>
      <xsl:value-of select=""$end"" />
    </End>
    <Reference>
      <xsl:value-of select=""$reference"" />
    </Reference>
    <ExternalRemark1>
      <xsl:value-of select=""$externalRemark1"" />
    </ExternalRemark1>
    <ExternalRemark2>
      <xsl:value-of select=""$externalRemark2"" />
    </ExternalRemark2>
    <InternalRemark1>
      <xsl:value-of select=""$internalRemark1"" />
    </InternalRemark1>
    <InternalRemark2>
      <xsl:value-of select=""$internalRemark2"" />
    </InternalRemark2>
  </xsl:template>

  <xsl:template name=""ContactPerson"">
    <xsl:param name=""name"" />
    <xsl:param name=""phone"" />
    <xsl:param name=""fax"" />
    <xsl:param name=""email"" />
    <ContactPerson>
      <Name>
        <xsl:value-of select=""$name"" />
      </Name>
      <Phone>
        <xsl:value-of select=""$phone"" />
      </Phone>
      <Fax>
        <xsl:value-of select=""$fax"" />
      </Fax>
      <Email>
        <xsl:value-of select=""$email"" />
      </Email>
    </ContactPerson>
  </xsl:template>

  <xsl:template name=""Good"">
    <xsl:param name=""reference"" />

    <xsl:variable name=""currentLineNo"" select=""s0:lineNo"" />
    <xsl:variable name=""quantity"" select=""sum(../s0:shipmentLine[s0:lineNo = $currentLineNo]/s0:quantity)""/>
    
    <PackingQty>
      <xsl:value-of select=""$quantity""/>
    </PackingQty>
    <PackingType>
      <xsl:value-of select=""s0:packageType""/>
    </PackingType>

    <PackageQty>0</PackageQty>
    <xsl:variable name=""DbLookupPackageCode"" select=""ScriptNS0:DBLookup(2 ,s0:packageType , connectionString , 'TM_TransSmart_Package' , 'TransSmart')"" />
    <xsl:variable name=""PackageType"" select=""ScriptNS0:DBValueExtract($DbLookupPackageCode , 'KLG')"" />
    <PackageType>
      <xsl:value-of select=""$PackageType""/>
    </PackageType>

    <ProductDescription>
      <xsl:value-of select=""s0:description/text()""/>
    </ProductDescription>

    <Brand>
    </Brand>

    <BrutoWeight>
      <xsl:value-of select=""round(sum(s0:measurements/s0:weight/text()))""/>
    </BrutoWeight>
    <NettoWeight>
      <xsl:value-of select=""round(sum(s0:measurements/s0:weight/text()))""/>
    </NettoWeight>

    <LoadingMeter>
      <xsl:value-of select=""format-number(((((round(sum(s0:measurements/s0:length/text())) * round(sum(s0:measurements/s0:width/text()))) div 24000) * $quantity) * 100) div 100,'##0.00' )""/>
    </LoadingMeter>
    
    <Volume>
      <xsl:value-of select=""((round(sum(s0:measurements/s0:length/text())) * round(sum(s0:measurements/s0:width/text())) * round(sum(s0:measurements/s0:height/text()))) * $quantity) div 1000000""/>
    </Volume>
    <PalletPlaces>
      <xsl:text>0</xsl:text>
    </PalletPlaces>
    
    <PackingLength>
      <xsl:value-of select=""round(sum(s0:measurements/s0:length/text()))""/>
    </PackingLength>
    <PackingWidth>
      <xsl:value-of select=""round(sum(s0:measurements/s0:width/text()))""/>
    </PackingWidth>
    <PackingHeight>
      <xsl:value-of select=""round(sum(s0:measurements/s0:height/text()))""/>
    </PackingHeight>

    <GoodsValue>
      <xsl:text>0</xsl:text>
    </GoodsValue>
    <GoodsValueCurrency>
      <xsl:text>EUR</xsl:text>
    </GoodsValueCurrency>
  </xsl:template>

  <!-- end region -->

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

</xsl:stylesheet>
";
        
        private const string _strArgList = @"<ExtensionObjects>
  <ExtensionObject Namespace=""http://schemas.microsoft.com/BizTalk/2003/ScriptNS0"" 
                   AssemblyName=""Microsoft.BizTalk.BaseFunctoids, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" 
                   ClassName=""Microsoft.BizTalk.BaseFunctoids.FunctoidScripts"" />
  <ExtensionObject Namespace=""http://schemas.microsoft.com/BizTalk/2003/ScriptNS1"" 
                   AssemblyName=""Microsoft.BizTalk.CrossReferencing, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" 
                   ClassName=""Microsoft.BizTalk.CrossReferencing.CrossReferencing"" />
  <ExtensionObject Namespace=""TM.Shared.Components.DateTime""
                   AssemblyName=""TM.Shared.Components, Version=1.0.7.0, Culture=neutral, PublicKeyToken=9cbf569b151cb1d4""
                   ClassName=""TM.Shared.Components.DateTimeFunctions"" />
  <ExtensionObject Namespace=""TM.Shared.Components.KeyList""
                   AssemblyName=""TM.Shared.Components, Version=1.0.7.0, Culture=neutral, PublicKeyToken=9cbf569b151cb1d4""
                   ClassName=""TM.Shared.Components.KeyListFunctions"" />
  <ExtensionObject Namespace=""TM.Shared.Components.SSO""
                   AssemblyName=""TM.Shared.Components, Version=1.0.7.0, Culture=neutral, PublicKeyToken=9cbf569b151cb1d4""
                   ClassName=""TM.Shared.Components.SSOFunctions"" />
  <ExtensionObject Namespace=""TM.Shared.Components.String""
                   AssemblyName=""TM.Shared.Components, Version=1.0.7.0, Culture=neutral, PublicKeyToken=9cbf569b151cb1d4""
                   ClassName=""TM.Shared.Components.StringFunctions"" />
</ExtensionObjects>";
        
        private const string _strSrcSchemasList0 = @"TM.EDIBroker.TransSmart.Schemas.Shipment";
        
        private const global::TM.EDIBroker.TransSmart.Schemas.Shipment _srcSchemaTypeReference0 = null;
        
        private const string _strTrgSchemasList0 = @"TM.Shared.Schemas.Order";
        
        private const global::TM.Shared.Schemas.Order _trgSchemaTypeReference0 = null;
        
        public override string XmlContent {
            get {
                return _strMap;
            }
        }
        
        public override string XsltArgumentListContent {
            get {
                return _strArgList;
            }
        }
        
        public override string[] SourceSchemas {
            get {
                string[] _SrcSchemas = new string [1];
                _SrcSchemas[0] = @"TM.EDIBroker.TransSmart.Schemas.Shipment";
                return _SrcSchemas;
            }
        }
        
        public override string[] TargetSchemas {
            get {
                string[] _TrgSchemas = new string [1];
                _TrgSchemas[0] = @"TM.Shared.Schemas.Order";
                return _TrgSchemas;
            }
        }
    }
}
