<?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>woconr;leveranciernr;factuurnr;factuurdatum;onderwerp;opdrachtnr;omschrijving;bedrag;btwperc;btwbedrag;btw_verlegd;document;iban;G_iban;G_bedrag</xsl:comment>
<xsl:for-each select="Invoice">
<xsl:for-each select="InvoiceLine">
<xsl:value-of select="../Invoicee/GLN"/>;<xsl:value-of select="../Invoicer/GLN"/>;<xsl:value-of select="../InvoiceNumber"/>;<xsl:value-of select="../InvoiceDate"/>;<xsl:call-template name="vrije_tekst"><xsl:with-param name="p_string" select="../FreeText"/></xsl:call-template>;<xsl:value-of select="../BuyersOrderNumber"/>;<xsl:call-template name="vrije_tekst"><xsl:with-param name="p_string" select="FreeText"/></xsl:call-template>;<xsl:value-of select="NetLineAmount"/>;<xsl:apply-templates select="VATInformation"/>;<xsl:value-of select="../Attachment/FileName"/>;<xsl:value-of select="../Supplier/AccountInformation/IBAN"/>;<xsl:value-of select="../PaymentOnBlockedAccount/IBAN"/>;<xsl:value-of select="../PaymentOnBlockedAccount/Amount"/>;&#10;</xsl:for-each></xsl:for-each>
</xsl:template>

<xsl:template match="VATInformation">
	<!-- Deze template levert het gedeelte [btwperc;btwbedrag;btw_verlegd] op -->
	<!-- Let even op: geen voorgaande ; en zeker ook geen ; op einde, dat gebeurt in de aanroepende regel (hierboven dus) -->
	<!-- Let op: btw bedrag wordt niet uit SidB uit de node InvoiceLine aangeboden, dus deze is hier altijd leeg. -->
	<xsl:choose>
		<xsl:when test="VATRate = 'E'">
			<!-- BTW verlegd = Ja, en neem BTW-percentage over uit XML, indien deze NIET(!) staat ingevuld (hoeft kennelijk niet in SidB/volgens wet, zet dan hard 21% hier in -->
   		<xsl:choose>
   			<xsl:when test="VATPercentage != ''"><xsl:value-of select="VATPercentage"/>;;Ja</xsl:when>
   			<xsl:otherwise>21;;Ja</xsl:otherwise>
   		</xsl:choose>	
   	</xsl:when>
   	<!-- BTW verlegd = Nee (Vatrate is dan S), veld btw_verlegd in CSV gewoon leeg laten, en neem BTW-percentage over uit XML -->
   	<xsl:otherwise><xsl:value-of select="VATPercentage"/>;;</xsl:otherwise>
  </xsl:choose>	
</xsl:template>

<xsl:template name="vrije_tekst">
<xsl:param name="p_string"/>
<xsl:variable name="l_string_esc_quotes"><xsl:call-template name="escape_quote"><xsl:with-param name="string" select="$p_string"/></xsl:call-template></xsl:variable>
<xsl:variable name="l_string_esc_linebreaks"><xsl:call-template name="escape_linebreaks"><xsl:with-param name="string" select="$l_string_esc_quotes"/></xsl:call-template></xsl:variable>"<xsl:value-of select="$l_string_esc_linebreaks"/>"</xsl:template>

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


<xsl:template name="escape_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="escape_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:stylesheet>
