<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

	<xsl:decimal-format name="european" decimal-separator="." grouping-separator=","/>

	<xsl:param name="mode"/>

	<xsl:template match="bestelopdracht">
		<!--Facilitor custom XSL template for cXML bestelopdracht-->
		<xsl:element name="cXML">
			<xsl:attribute name="version">1.2.016</xsl:attribute>
			<xsl:attribute name="payloadID">
				<xsl:value-of select="key"/>
			</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>
			<Header>
				<From>
					<xsl:element name="Credential">
						<xsl:attribute name="domain">
							<xsl:value-of select="/facilitor/header/custId"/>
						</xsl:attribute>
            <!--Klantcode zoals door DRDV aan ALLI toegekend-->
            <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:element>
				</To>
				<Sender>
					<Credential domain="Facilitor">
						<Identity>order@facilityplace.nl</Identity>
					</Credential>
					<UserAgent/>
				</Sender>
			</Header>
			<Request>
				<OrderRequest>
					<xsl:element name="OrderRequestHeader">
						<xsl:attribute name="orderID">
							<xsl:value-of select="id"/>
						</xsl:attribute>
						<xsl:attribute name="orderDate">
							<xsl:value-of select="datum/jaar"/>-<xsl:value-of select="datum/maand"/>-<xsl:value-of select="datum/dag"/>
						</xsl:attribute>
						<Total>
							<Money currency="EUR">
								<xsl:value-of select="format-number(sum(bestelopdrachtitem/totaal), '0.00', 'european')"/>
							</Money>
						</Total>
						<ShipTo>
						    <xsl:choose>
            			<xsl:when test="bestelling/kenmerk[@key=241]='Ja' or
            			                bestelling/kenmerk[@key=461]='Ja' or
            			                bestelling/kenmerk[@key=462]='Ja' or
            			                bestelling/kenmerk[@key=463]='Ja' or
            			                bestelling/kenmerk[@key=464]='Ja' or
            			                bestelling/kenmerk[@key=465]='Ja' or
            			                bestelling/kenmerk[@key=466]='Ja' or
            			                bestelling/kenmerk[@key=467]='Ja' or
            			                bestelling/kenmerk[@key=468]='Ja' or
            			                bestelling/kenmerk[@key=469]='Ja' or
            			                bestelling/kenmerk[@key=470]='Ja'">
            			<xsl:comment>Nu is het een drukopdracht/inkoopbestelling</xsl:comment>
            				<xsl:element name="Address">
        								<xsl:attribute name="addressID"></xsl:attribute>
        								<Name xml:lang="NL"></Name>
        								<PostalAddress>
        							      <DeliverTo></DeliverTo>
        								    <Street></Street>
                				    <City></City>
                				    <State></State>
                				    <PostalCode></PostalCode>
                				    <Country isoCountryCode="NL"></Country>
        								</PostalAddress>
        						</xsl:element>
            			</xsl:when>
            			<xsl:otherwise>
            			  <xsl:comment>Nu is het een afroepopdracht/bestelling uit de voorraad</xsl:comment>
                    <xsl:element name="Address">
        								<xsl:attribute name="addressID"></xsl:attribute>
        								<Name xml:lang="NL"><xsl:value-of select="bestelling/aanvrager/naam_full"/></Name>
        								<PostalAddress>
        							      <DeliverTo><xsl:value-of select="bestelling/aanvrager/naam_full"/></DeliverTo>
        								    <Street><xsl:value-of select="afleveradres/bezoek_adres"/></Street>
                				    <City><xsl:value-of select="afleveradres/bezoek_plaats"/></City>
                				    <State></State>
                				    <PostalCode><xsl:value-of select="afleveradres/bezoek_postcode"/></PostalCode>
                				    <Country isoCountryCode="NL"><xsl:apply-templates select="afleveradres/bezoek_land"/></Country>
        								</PostalAddress>
        						</xsl:element>
            			</xsl:otherwise>
            		</xsl:choose>						  
						</ShipTo>
						<BillTo>
							<xsl:element name="Address">
								<xsl:attribute name="addressID"><xsl:value-of select="bedrijf/overeenkomst_nr"/></xsl:attribute>
								<Name xml:lang="NL">
									<xsl:value-of select="bedrijf/factuuradres/naam"/>
								</Name>
								<PostalAddress>
									<Street><xsl:value-of select="bedrijf/factuuradres/post_adres"/></Street>
									<City><xsl:value-of select="bedrijf/factuuradres/post_plaats"/></City>
									<State></State>
									<PostalCode><xsl:value-of select="bedrijf/factuuradres/post_postcode"/></PostalCode>
									<Country isoCountryCode="NL"><xsl:apply-templates select="bedrijf/factuuradres/post_land"/></Country>
								</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>
						</Contact>
						<Extrinsic name="Costcenter">
							<xsl:value-of select="bestelling/kostenplaats/nr"/>
						</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="../bestelling/leverdatum/jaar"/>-<xsl:value-of select="../bestelling/leverdatum/maand"/>-<xsl:value-of select="../bestelling/leverdatum/dag"/>
							</xsl:attribute>
							<ItemID>
								<SupplierPartID>
									<xsl:value-of select="bestelitem/srtdeel/artikel_nummer"/>
								</SupplierPartID>
							</ItemID>
							<ItemDetail>
								<UnitPrice>
									<Money currency="EUR">
										<xsl:value-of select="format-number(prijs, '0.00', 'european')"/>
									</Money>
								</UnitPrice>
								<Description xml:lang="NL">
									<xsl:value-of select="bestelitem/srtdeel/omschrijving"/>
								</Description>
								<UnitOfMeasure>
									<xsl:value-of select="bestelitem/srtdeel/eenheid"/>
								</UnitOfMeasure>
								<Classification domain=""/>
								<xsl:for-each select="bestelitem/kenmerk">
                    <xsl:if test="@type!='Q' and @type!='L' and @type!='I'">
                    <xsl:element name="Extrinsic">
                      <xsl:attribute name="id">
                        <xsl:value-of select="@key"/>
                      </xsl:attribute>
                      <xsl:attribute name="name">
                        <xsl:value-of select="translate(@naam,' ','')"/>
                      </xsl:attribute>
                        <xsl:value-of select="."/>
                    </xsl:element>
                    </xsl:if>
  			        </xsl:for-each>
  			        <xsl:apply-templates select="bestelitem" mode="Bepaaljuistetelefoon"/>
  			        <xsl:apply-templates select="bestelitem" mode="Bepaaljuistemail"/>
  			        <xsl:apply-templates select="bestelitem" mode="BepaalVvEVestiging"/>
							</ItemDetail>
						</xsl:element>
					</xsl:for-each>
				</OrderRequest>
			</Request>
		</xsl:element>
	</xsl:template>
	
	
  <xsl:template match="bestelitem" mode="Bepaaljuistetelefoon">
    <xsl:variable name="l_visitekaartje_key">10501</xsl:variable>
		<xsl:variable name="l_visitekaartje_vve_key">10502</xsl:variable>
		<xsl:variable name="l_visitekaartje_ssc_key">10503</xsl:variable>
    
    <xsl:choose>
        <xsl:when test="./srtdeel/key=$l_visitekaartje_key">
            <xsl:choose>
                <xsl:when test="./kenmerk[@key=261]='Ja' and kenmerk[@key=213] != ''">
                    <xsl:element name="Extrinsic">
                      <xsl:attribute name="id">0</xsl:attribute>
                      <xsl:attribute name="name">GewensteTelefoonnr</xsl:attribute>
                        <xsl:value-of select="substring(translate(kenmerk[@key=213],' ',''),1,2)"/>-<xsl:value-of select="substring(translate(kenmerk[@key=213],' ',''),3)"/>
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:element name="Extrinsic">
                      <xsl:attribute name="id">0</xsl:attribute>
                      <xsl:attribute name="name">GewensteTelefoonnr</xsl:attribute>
                    </xsl:element>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:when test="./srtdeel/key=$l_visitekaartje_vve_key">
    	    <xsl:choose>
                <xsl:when test="./kenmerk[@key=282]='Ja' and kenmerk[@key=214] != ''">
                    <xsl:element name="Extrinsic">
                      <xsl:attribute name="id">0</xsl:attribute>
                      <xsl:attribute name="name">GewensteTelefoonnr</xsl:attribute>
                        <xsl:value-of select="substring(translate(kenmerk[@key=214],' ',''),1,2)"/>-<xsl:value-of select="substring(translate(kenmerk[@key=214],' ',''),3)"/>
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:element name="Extrinsic">
                      <xsl:attribute name="id">0</xsl:attribute>
                      <xsl:attribute name="name">GewensteTelefoonnr</xsl:attribute>
                    </xsl:element>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:when test="./srtdeel/key=$l_visitekaartje_ssc_key">
            <xsl:choose>
                <xsl:when test="kenmerk[@key=281]='Ja' and kenmerk[@key=215] != ''">
                    <xsl:element name="Extrinsic">
                      <xsl:attribute name="id">0</xsl:attribute>
                      <xsl:attribute name="name">GewensteTelefoonnr</xsl:attribute>
                        <xsl:value-of select="substring(translate(kenmerk[@key=215],' ',''),1,2)"/>-<xsl:value-of select="substring(translate(kenmerk[@key=215],' ',''),3)"/>
                    </xsl:element>
                </xsl:when>
                <xsl:when test="./kenmerk[@key=301]='Ja' and kenmerk[@key=302] != ''">
                    <xsl:element name="Extrinsic">
                      <xsl:attribute name="id">0</xsl:attribute>
                      <xsl:attribute name="name">GewensteTelefoonnr</xsl:attribute>
                        <xsl:value-of select="substring(translate(kenmerk[@key=302],' ',''),1,3)"/>-<xsl:value-of select="substring(translate(kenmerk[@key=302],' ',''),4)"/>
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:element name="Extrinsic">
                      <xsl:attribute name="id">0</xsl:attribute>
                      <xsl:attribute name="name">GewensteTelefoonnr</xsl:attribute>
                    </xsl:element>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
    </xsl:choose>
  </xsl:template>
	
	<xsl:template match="bestelitem" mode="Bepaaljuistemail">
        <xsl:element name="Extrinsic">
            <xsl:attribute name="id">-1</xsl:attribute>
            <xsl:attribute name="name">GewensteMailAdres</xsl:attribute>
            <xsl:choose>
                <xsl:when test="./kenmerk[@naam='Opties voor afdrukken e-mailadres']='Geen e-mail afdrukken'"></xsl:when>
                <xsl:when test="./kenmerk[@naam='Opties voor afdrukken e-mailadres']='Het algemene mailadres afdrukken'"><xsl:value-of select="kenmerk[@naam='Standaard mailadres']"/></xsl:when>
                <xsl:when test="./kenmerk[@naam='Opties voor afdrukken e-mailadres']='Uw eigen mailadres afdrukken'"><xsl:value-of select="kenmerk[@naam='Email']"/></xsl:when>
            </xsl:choose>
        </xsl:element>
  </xsl:template>
  
  
  <xsl:template match="bestelitem" mode="BepaalVvEVestiging">
    <xsl:variable name="l_visitekaartje_vve_key">10502</xsl:variable>
		<xsl:if test="./srtdeel/key=$l_visitekaartje_vve_key">
        <xsl:element name="Extrinsic">
            <xsl:attribute name="id">-2</xsl:attribute>
            <xsl:attribute name="name">GewensteVvEVestiging</xsl:attribute>
                <xsl:value-of select="kenmerk[@key=367]"/>            
        </xsl:element>
    </xsl:if>   
  </xsl:template>
	
	<xsl:template match="facilitor">
		<xsl:apply-templates select="bestelopdracht"/>
	</xsl:template>

	<xsl:template match="/">
		<xsl:choose>
			<xsl:when test="$mode='getExtension'">
				<xsl:element name="format">
					<xsl:element name="extension">xml</xsl:element>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>

				<xsl:apply-templates select="facilitor"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
</xsl:stylesheet>