<?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" 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:param name="srtnotificatiecode"/>

	<xsl:variable name="Rev">
		<!-- Revision van deze hout2sys.xsl -->
		<xsl:value-of select="substring(translate('$Revision$', '$ ', ''), 10)"/>
	</xsl:variable>

	<xsl:template match="melding">
		<facilitor>
			<xsl:element name="opdracht">
				<xsl:attribute name="key">
					<xsl:value-of select="externnr"/>
				</xsl:attribute>
				<xsl:attribute name="type">update</xsl:attribute>
				<xsl:choose>
					<xsl:when test="$srtnotificatiecode='MLDACP' or status_key='4'">
						<datum>
							<xsl:value-of select="tracking/track[@code='MLDACP']/datum/jaar"/>-<xsl:value-of select="tracking/track[@code='MLDACP']/datum/maand"/>-<xsl:value-of select="tracking/track[@code='MLDACP']/datum/dag"/>
							<xsl:text> </xsl:text>
							<xsl:value-of select="tracking/track[@code='MLDACP']/datum/tijd"/>:00</datum>
						<xsl:if test="opmerking!=''">
							<opmerking>
								<xsl:value-of select="opmerking"/>
							</opmerking>
						</xsl:if>
						<datumgereed>
							<xsl:value-of select="datum_gepland/jaar"/>-<xsl:value-of select="datum_gepland/maand"/>-<xsl:value-of select="datum_gepland/dag"/>
							<xsl:text> </xsl:text>
							<xsl:value-of select="datum_gepland/tijd"/>:00</datumgereed>
						<kosten>
							<xsl:value-of select="format-number(sum(opdracht/kosten), '0,00', 'european')"/>
						</kosten>
					</xsl:when>
					<xsl:when test="$srtnotificatiecode='MLDAFM' or status_key='5'">
						<datum>
							<xsl:value-of select="tracking/track[@code='MLDAFM']/datum/jaar"/>-<xsl:value-of select="tracking/track[@code='MLDAFM']/datum/maand"/>-<xsl:value-of select="tracking/track[@code='MLDAFM']/datum/dag"/>
							<xsl:text> </xsl:text>
							<xsl:value-of select="tracking/track[@code='MLDAFM']/datum/tijd"/>:00</datum>
						<status>ORDAFM</status>
						<xsl:if test="opmerking!=''">
							<opmerking>
								<xsl:value-of select="opmerking"/>
							</opmerking>
						</xsl:if>
						<datumgereed>
							<xsl:value-of select="datum_gepland/jaar"/>-<xsl:value-of select="datum_gepland/maand"/>-<xsl:value-of select="datum_gepland/dag"/>
							<xsl:text> </xsl:text>
							<xsl:value-of select="datum_gepland/tijd"/>:00</datumgereed>
						<kosten>
							<xsl:value-of select="format-number(sum(opdracht/kosten), '0,00', 'european')"/>
						</kosten>
					</xsl:when>
					<xsl:when test="$srtnotificatiecode='MLDREJ' or status_key='1'">
						<datum>
							<xsl:value-of select="tracking/track[@code='MLDREJ']/datum/jaar"/>-<xsl:value-of select="tracking/track[@code='MLDREJ']/datum/maand"/>-<xsl:value-of select="tracking/track[@code='MLDREJ']/datum/dag"/>
							<xsl:text> </xsl:text>
							<xsl:value-of select="tracking/track[@code='MLDREJ']/datum/tijd"/>:00</datum>
						<status>ORDAFM</status>
						<opmerking>Afgewezen.<xsl:if test="opmerking!=''">
								<xsl:value-of select="opmerking"/></xsl:if></opmerking>
						<datumgereed>
							<xsl:value-of select="datum_gepland/jaar"/>-<xsl:value-of select="datum_gepland/maand"/>-<xsl:value-of select="datum_gepland/dag"/>
							<xsl:text> </xsl:text>
							<xsl:value-of select="datum_gepland/tijd"/>:00</datumgereed>
						<kosten>
							<xsl:value-of select="format-number(sum(opdracht/kosten), '0,00', 'european')"/>
						</kosten>
					</xsl:when>
					<xsl:otherwise>
						<xsl:variable name="maxtrack">
							<xsl:for-each select="tracking/track/datum">
								<xsl:sort select="timestamp" data-type="number" order="descending"/>
								<xsl:if test="position()=1">
									<xsl:copy-of select="."/>
								</xsl:if>
							</xsl:for-each>
						</xsl:variable>
						<datum>
							<xsl:value-of select="msxml:node-set($maxtrack)/datum/jaar"/>-<xsl:value-of select="msxml:node-set($maxtrack)/datum/maand"/>-<xsl:value-of select="msxml:node-set($maxtrack)/datum/dag"/>
							<xsl:text> </xsl:text>
							<xsl:value-of select="msxml:node-set($maxtrack)/datum/tijd"/>:00</datum>
						<xsl:if test="opmerking!=''">
							<opmerking>
								<xsl:value-of select="opmerking"/>
							</opmerking>
						</xsl:if>
						<datumgereed>
							<xsl:value-of select="datum_gepland/jaar"/>-<xsl:value-of select="datum_gepland/maand"/>-<xsl:value-of select="datum_gepland/dag"/>
							<xsl:text> </xsl:text>
							<xsl:value-of select="datum_gepland/tijd"/>:00</datumgereed>
						<kosten>
							<xsl:value-of select="format-number(sum(opdracht/kosten), '0,00', 'european')"/>
						</kosten>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:element>
		</facilitor>
	</xsl:template>

	<xsl:template match="facilitor">
		<xsl:apply-templates select="melding"/>
	</xsl:template>

	<!-- Root -->
	<xsl:template match="/">
		<xsl:apply-templates select="facilitor"/>
	</xsl:template>
</xsl:stylesheet>
