<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
	xmlns:date="http://exslt.org/dates-and-times" xmlns:exsl="http://exslt.org/common"
	extension-element-prefixes="date exsl" xmlns:csv="csv:csv">

	<xsl:output method="text" encoding="ISO-8859-1" />
	<xsl:strip-space elements="*" />
	<xsl:variable name="delimiter" select="';'" />

	<xsl:decimal-format name="money" decimal-separator="," grouping-separator="." NaN="0,00" />
	<xsl:decimal-format name="amount" decimal-separator="," grouping-separator="." NaN="0" />
	<xsl:decimal-format name="percent" decimal-separator="," grouping-separator="." NaN="0" />

	<xsl:template name="formatdate">
		<xsl:param name="datestr" />

		<xsl:variable name="yyyy">
			<xsl:value-of select="substring($datestr,1,4)" />
		</xsl:variable>

		<xsl:variable name="mm">
			<xsl:value-of select="substring($datestr,6,2)" />
		</xsl:variable>

		<xsl:variable name="dd">
			<xsl:value-of select="substring($datestr,9,2)" />
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="$datestr != ''">
				<xsl:value-of select="$dd" />
				<xsl:value-of select="'.'" />
				<xsl:value-of select="$mm" />
				<xsl:value-of select="'.'" />
				<xsl:value-of select="$yyyy" />
			</xsl:when>
			<xsl:otherwise>
				&#160;
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="formattime">
		<xsl:param name="timestr" />

		<xsl:variable name="time">
			<xsl:value-of select="substring($timestr,12,8)" />
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="$timestr != ''">
				<xsl:value-of select="$time" />
			</xsl:when>
			<xsl:otherwise>
				&#160;
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="formatdatetime">
		<xsl:param name="datetimestr" />

		<xsl:variable name="yyyy">
			<xsl:value-of select="substring($datetimestr,1,4)" />
		</xsl:variable>

		<xsl:variable name="mm">
			<xsl:value-of select="substring($datetimestr,6,2)" />
		</xsl:variable>

		<xsl:variable name="dd">
			<xsl:value-of select="substring($datetimestr,9,2)" />
		</xsl:variable>

		<xsl:variable name="time">
			<xsl:value-of select="substring($datetimestr,12,8)" />
		</xsl:variable>


		<xsl:choose>
			<xsl:when test="$datetimestr != ''">
				<xsl:value-of select="$dd" />
				<xsl:value-of select="'.'" />
				<xsl:value-of select="$mm" />
				<xsl:value-of select="'.'" />
				<xsl:value-of select="$yyyy" />
				<xsl:text>, </xsl:text>
				<xsl:value-of select="$time" />
			</xsl:when>
			<xsl:otherwise>
				&#160;
			</xsl:otherwise>
		</xsl:choose>

	</xsl:template>



	<xsl:template match="/">


		<!-- HEADER -->
		<xsl:text>&#xa;</xsl:text>
		<xsl:text>&#xa;</xsl:text>

		<xsl:text>Von</xsl:text><xsl:value-of select="$delimiter" />

		<xsl:call-template name="formatdate">
			<xsl:with-param name="datestr" select="report/report-params/DATE" />
		</xsl:call-template>
		<xsl:value-of select="$delimiter" />

		<xsl:text>Bis</xsl:text><xsl:value-of select="$delimiter" />
		<xsl:call-template name="formatdate">
			<xsl:with-param name="datestr" select="report/report-params/TODATE" />
		</xsl:call-template>
		<xsl:value-of select="$delimiter" />

		<!-- Ausgegeben stonierte Gutscheine-->
		<xsl:text>&#xa;</xsl:text>
		<xsl:text>&#xa;</xsl:text>
		<xsl:text>Ausgegebene stornierte Gutscheine</xsl:text>
		<xsl:text>&#xa;</xsl:text>
		<xsl:text>&#xa;</xsl:text>
		<xsl:text>Ausgabedatum</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Stornodatum</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Kassierer</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Gutschein Nr</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Betrag</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Beleg Nr</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Kunde</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Telefon</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Handy</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>EMail</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>PLZ</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Ort</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Straße</xsl:text>
		<xsl:value-of select="$delimiter" />

		<xsl:text>&#xa;</xsl:text>

		<xsl:for-each select="report/sales_voucher_out_storno/row">

			<xsl:call-template name="formatdate">
				<xsl:with-param name="datestr" select="voucher_ts" />
			</xsl:call-template>
			<xsl:value-of select="$delimiter" />
			<xsl:call-template name="formatdate">
				<xsl:with-param name="datestr" select="voucher_use_ts" />
			</xsl:call-template>
			<xsl:value-of select="$delimiter" />

			<xsl:value-of select="employee_nm" />
			<xsl:value-of select="$delimiter" />

			<xsl:value-of select="voucher_cd" />
			<xsl:value-of select="$delimiter" />

				<xsl:value-of select="format-number(voucher_value, '###.##0,00', 'money')" />
				<xsl:value-of select="$delimiter" />
				<xsl:value-of select="sales_inv_cd" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="customer_nm" />
				<xsl:value-of select="$delimiter" />

				<xsl:text>#</xsl:text><xsl:value-of select="phone" />
				<xsl:value-of select="$delimiter" />

				<xsl:text>#</xsl:text><xsl:value-of select="mobile" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="email" />
				<xsl:value-of select="$delimiter" />

				<xsl:text>#</xsl:text><xsl:value-of select="postal_cd" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="city" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="street" />
				<xsl:value-of select="$delimiter" />


			<xsl:text>&#xa;</xsl:text>
		</xsl:for-each>

		<xsl:value-of select="$delimiter" />
		<xsl:value-of select="$delimiter" />
		<xsl:value-of select="$delimiter" />
		<xsl:text>Summe</xsl:text>
		<xsl:value-of select="$delimiter" />

		<xsl:value-of select="format-number(sum(report/sales_voucher_out_storno/row/voucher_value), '###.##0,00', 'money')" />
		<xsl:value-of select="$delimiter" />
		<xsl:text>&#xa;</xsl:text>


		<!-- Eingelöste stonierte Gutscheine-->
		<xsl:text>&#xa;</xsl:text>
		<xsl:text>&#xa;</xsl:text>
		<xsl:text>Eingelöste stornierte Gutscheine</xsl:text>
		<xsl:text>&#xa;</xsl:text>
		<xsl:text>&#xa;</xsl:text>

		<xsl:text>Ausgabedatum</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Stornodatum</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Kassierer</xsl:text>
<xsl:value-of select="$delimiter" />
		<xsl:text>Gutschein Nr</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Betrag</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Beleg Nr</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Kunde</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Telefon</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Handy</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>EMail</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>PLZ</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Ort</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Straße</xsl:text>
		<xsl:value-of select="$delimiter" />

		<xsl:text>&#xa;</xsl:text>

		<xsl:for-each select="report/sales_voucher_in_storno/row">

			<xsl:call-template name="formatdate">
				<xsl:with-param name="datestr" select="voucher_ts" />
			</xsl:call-template>
			<xsl:value-of select="$delimiter" />
			<xsl:call-template name="formatdate">
				<xsl:with-param name="datestr" select="voucher_use_ts" />
			</xsl:call-template>
			<xsl:value-of select="$delimiter" />

			<xsl:value-of select="employee_nm" />
			<xsl:value-of select="$delimiter" />

			<xsl:value-of select="voucher_cd" />
			<xsl:value-of select="$delimiter" />

			<xsl:value-of select="format-number(voucher_value, '###.##0,00', 'money')" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="sales_inv_cd" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="customer_nm" />
				<xsl:value-of select="$delimiter" />

				<xsl:text>#</xsl:text><xsl:value-of select="phone" />
				<xsl:value-of select="$delimiter" />

				<xsl:text>#</xsl:text><xsl:value-of select="mobile" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="email" />
				<xsl:value-of select="$delimiter" />

				<xsl:text>#</xsl:text><xsl:value-of select="postal_cd" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="city" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="street" />
				<xsl:value-of select="$delimiter" />

			<xsl:text>&#xa;</xsl:text>
		</xsl:for-each>

		<xsl:value-of select="$delimiter" />
		<xsl:value-of select="$delimiter" />
		<xsl:value-of select="$delimiter" />
		<xsl:text>Summe</xsl:text>
		<xsl:value-of select="$delimiter" />

		<xsl:value-of select="format-number(sum(report/sales_voucher_in_storno/row/voucher_value), '###.##0,00', 'money')" />
		<xsl:value-of select="$delimiter" />
		<xsl:text>&#xa;</xsl:text>
	


		<!-- Deaktivierte Gutscheine-->
		<xsl:text>&#xa;</xsl:text>
		<xsl:text>&#xa;</xsl:text>
		<xsl:text>Deaktivierte Gutscheine</xsl:text>
		<xsl:text>&#xa;</xsl:text>
		<xsl:text>&#xa;</xsl:text>

		<xsl:text>Ausgabedatum</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Deakt.-Datum</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Kassierer</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Gutschein Nr</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Betrag</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Beleg Nr</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Kunde</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Telefon</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Handy</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>EMail</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>PLZ</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Ort</xsl:text>
		<xsl:value-of select="$delimiter" />
		<xsl:text>Straße</xsl:text>
		<xsl:value-of select="$delimiter" />

		<xsl:text>&#xa;</xsl:text>

		<xsl:for-each select="report/sales_voucher_out_disabled/row">

			<xsl:call-template name="formatdate">
				<xsl:with-param name="datestr" select="voucher_ts" />
			</xsl:call-template>
			<xsl:value-of select="$delimiter" />
			<xsl:call-template name="formatdate">
				<xsl:with-param name="datestr" select="voucher_use_ts" />
			</xsl:call-template>
			<xsl:value-of select="$delimiter" />

			<xsl:value-of select="employee_nm" />
			<xsl:value-of select="$delimiter" />

			<xsl:value-of select="voucher_cd" />
			<xsl:value-of select="$delimiter" />

			<xsl:value-of select="format-number(voucher_value, '###.##0,00', 'money')" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="sales_inv_cd" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="customer_nm" />
				<xsl:value-of select="$delimiter" />

				<xsl:text>#</xsl:text><xsl:value-of select="phone" />
				<xsl:value-of select="$delimiter" />

				<xsl:text>#</xsl:text><xsl:value-of select="mobile" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="email" />
				<xsl:value-of select="$delimiter" />

				<xsl:text>#</xsl:text><xsl:value-of select="postal_cd" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="city" />
				<xsl:value-of select="$delimiter" />

				<xsl:value-of select="street" />
				<xsl:value-of select="$delimiter" />

			<xsl:text>&#xa;</xsl:text>
		</xsl:for-each>

		<xsl:value-of select="$delimiter" />
		<xsl:value-of select="$delimiter" />
		<xsl:value-of select="$delimiter" />
		<xsl:text>Summe</xsl:text>
		<xsl:value-of select="$delimiter" />

		<xsl:value-of select="format-number(sum(report/sales_voucher_out_disabled/row/voucher_value), '###.##0,00', 'money')" />
		<xsl:value-of select="$delimiter" />
		<xsl:text>&#xa;</xsl:text>


	</xsl:template>

</xsl:stylesheet>