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

	<xsl:template match="/">
		<xsl:comment>factuurnr;factuurdatum;opdrachtnr;omschrijving;bedrag;btwperc;btwbedrag;document;debiteurnr;opmerking;btw_verlegd;betaalkenmerk</xsl:comment>
		<xsl:for-each select="batch/document">
			<xsl:variable name="l_pfd_file" select="docfile[@description='document files']/pdf_file"/>
			<xsl:variable name="verlegd" select="fields/AMOUNT/FR_MEMO"/>
      <xsl:variable name="betaalkenmerk" select="fields/DOCUMENT/EX_REFKEN"/>
      <xsl:variable name="cr">;&#xA;</xsl:variable>
			<xsl:variable name="opdracht_id"><xsl:choose>
			    <xsl:when test="fields/DOCUMENT/bes_mld_cnt_opdracht_id != ''"><xsl:choose>
    		        <xsl:when test="contains(fields/DOCUMENT/bes_mld_cnt_opdracht_id, '/')"><xsl:value-of select="fields/DOCUMENT/bes_mld_cnt_opdracht_id"/></xsl:when>
                <xsl:when test="(substring(fields/DOCUMENT/bes_mld_cnt_opdracht_id, 1,1) != 'c') and
    					                  (substring(fields/DOCUMENT/bes_mld_cnt_opdracht_id, 1,1) != 'C')">C<xsl:value-of select="fields/DOCUMENT/bes_mld_cnt_opdracht_id"/></xsl:when>
    					  <xsl:when test="(substring(fields/DOCUMENT/bes_mld_cnt_opdracht_id, 1,1) = 'c') or
    					                  (substring(fields/DOCUMENT/bes_mld_cnt_opdracht_id, 1,1) = 'C')"><xsl:value-of select="fields/DOCUMENT/bes_mld_cnt_opdracht_id"/></xsl:when>
    					 <xsl:otherwise><xsl:value-of select="fields/DOCUMENT/bes_mld_cnt_opdracht_id"/>/1</xsl:otherwise>
    		    </xsl:choose></xsl:when>
    		  <xsl:otherwise>[leeg]</xsl:otherwise>
    	</xsl:choose>
		  </xsl:variable>
			<xsl:choose>
				<xsl:when test="count(fields[@description='data fields']/TABLE[@description='Factuurregel']/EX_TABLE/row) &gt; 0">
					<xsl:for-each select="fields[@description='data fields']/TABLE[@description='Factuurregel']/EX_TABLE/row">
						<xsl:variable name="orderid">
							<xsl:call-template name="linebreaks">
								<xsl:with-param name="string" select="col[@id='fin_factuurregel_opdrachtid']"/>
							</xsl:call-template>
						</xsl:variable>
						<xsl:variable name="omschrijving">
							<xsl:call-template name="linebreaks">
								<xsl:with-param name="string" select="col[@id='fin_factuurregel_omschrijving']"/>
							</xsl:call-template>
						</xsl:variable>
						<xsl:variable name="totaal">
							<xsl:call-template name="linebreaks">
								<xsl:with-param name="string" select="col[@id='fin_factuurregel_totaal']"/>
							</xsl:call-template>
						</xsl:variable>
					<xsl:value-of select="../../../DOCUMENT/fin_factuur_nr"/>;<xsl:value-of select="../../../DOCUMENT/fin_factuur_datum"/>;<xsl:value-of select="$orderid"/><xsl:if test="$orderid=''"><xsl:value-of select="$opdracht_id"/></xsl:if>;<xsl:call-template name="puntkomma"><xsl:with-param name="p_string" select="$omschrijving"/></xsl:call-template>;<xsl:value-of select="$totaal"/>;<xsl:if test="col[@id='fin_factuurregel_btw_perc'] = ''">0</xsl:if><xsl:value-of select="col[@id='fin_factuurregel_btw_perc']"/>;<xsl:value-of select="fin_factuurregel_btw_bedrag"/>;<xsl:value-of select="$l_pfd_file"/>;<xsl:value-of select="../../../DOCUMENT/fin_factuur_debiteurnr"/>;<xsl:value-of select="fields/CORSA/Facilitor_opmerking"/>;<xsl:value-of select="$verlegd"/>;<xsl:value-of select="$betaalkenmerk"/>;<xsl:value-of select="$cr"/></xsl:for-each>
				</xsl:when>
				<xsl:otherwise>
				<xsl:variable name="omschrijving">
				<xsl:choose>
				<xsl:when test="fields/AMOUNT/EX_VAT_HIGH != '0.00'">BTW hoog tarief</xsl:when>
				<xsl:when test="fields/AMOUNT/EX_VAT_LOW != '0.00'">BTW laag tarief</xsl:when>
				<xsl:otherwise>BTW 0-tarief</xsl:otherwise>
				</xsl:choose>
				</xsl:variable>
				<xsl:variable name="btw">
				<xsl:choose>
				<xsl:when test="fields/AMOUNT/EX_VAT_HIGH != '0.00'">21</xsl:when>
				<xsl:when test="fields/AMOUNT/EX_VAT_LOW != '0.00'">6</xsl:when>
				<xsl:otherwise>0</xsl:otherwise>
				</xsl:choose>
				</xsl:variable>
				<xsl:variable name="btw_bedrag">
				<xsl:choose>
				<xsl:when test="fields/AMOUNT/EX_VAT_HIGH != '0.00'"><xsl:value-of select="fields/AMOUNT/EX_VAT_HIGH"/></xsl:when>
				<xsl:when test="fields/AMOUNT/EX_VAT_LOW != '0.00'"><xsl:value-of select="fields/AMOUNT/EX_VAT_LOW"/></xsl:when>
				<xsl:otherwise>0.00</xsl:otherwise>
				</xsl:choose>
				</xsl:variable>
			  <xsl:value-of select="fields/DOCUMENT/fin_factuur_nr"/>;<xsl:value-of select="fields/DOCUMENT/fin_factuur_datum"/>;<xsl:value-of select="$opdracht_id"/>;<xsl:value-of select="$omschrijving"/>;<xsl:value-of select="fields/AMOUNT/EX_TOTALEXCL"/>;<xsl:value-of select="$btw"/>;<xsl:value-of select="$btw_bedrag"/>;<xsl:value-of select="$l_pfd_file"/>;<xsl:value-of select="fields[@description='data fields']/DOCUMENT[@description='Factuurgegevens']/fin_factuur_debiteurnr"/>;<xsl:value-of select="fields/CORSA/Facilitor_opmerking"/>;<xsl:value-of select="$verlegd"/>;<xsl:value-of select="$betaalkenmerk"/>;<xsl:value-of select="$cr"/></xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="linebreaks">
		<xsl:param name="string"/>
		<xsl:choose>
			<xsl:when test="contains($string, '&#xA;')">
				<xsl:value-of select="substring-before($string, '&#xA;')"/>
				<xsl:call-template name="linebreaks">
					<xsl:with-param name="string" select="substring-after($string, '&#xA;')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$string"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="puntkomma">
		<xsl:param name="p_string"/>
		<xsl:value-of select="translate($p_string,';',':')"/>
	</xsl:template>
</xsl:stylesheet><!-- Stylus Studio meta-information - (c) 2004-2006. Progress Software Corporation. All rights reserved.
<metaInformation>
<scenarios ><scenario default="yes" name="Scenario1" userelativepaths="yes" externalpreview="no" url="file:///a:/Facilitor/AAFM_A/CUST/AADS/import/BACKUP2012/FACTUUR/2012&#x2D;06&#x2D;28_1241/28&#x2D;06&#x2D;2012_AV POINT_20120269_AAFMCI18_INVOICE_20120628_004_000000.XML" htmlbaseurl="" outputurl="" processortype="internal" useresolver="yes" profilemode="0" profiledepth="" profilelength="" urlprofilexml="" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext="" validateoutput="no" validator="internal" customvalidator=""/></scenarios><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/><MapperBlockPosition></MapperBlockPosition><TemplateContext></TemplateContext><MapperFilter side="source"></MapperFilter></MapperMetaTag>
</metaInformation>
--><!-- Stylus Studio meta-information - (c) 2004-2006. Progress Software Corporation. All rights reserved.
<metaInformation>
<scenarios ><scenario default="yes" name="Scenario1" userelativepaths="yes" externalpreview="no" url="file:///c:/temp/last.XML" htmlbaseurl="" outputurl="" processortype="internal" useresolver="yes" profilemode="0" profiledepth="" profilelength="" urlprofilexml="" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext="" validateoutput="no" validator="internal" customvalidator=""/></scenarios><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/><MapperBlockPosition></MapperBlockPosition><TemplateContext></TemplateContext><MapperFilter side="source"></MapperFilter></MapperMetaTag>
</metaInformation>
-->