<?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: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:attribute-set name="debug">
		<xsl:attribute name="border-color">red</xsl:attribute>
		<xsl:attribute name="border-width">0.5mm</xsl:attribute>
		<xsl:attribute name="border-style">solid</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="th3bold">
		<xsl:attribute name="font-weight">bold</xsl:attribute>
		<xsl:attribute name="font-size">8pt</xsl:attribute>
		<xsl:attribute name="padding">0.5mm</xsl:attribute>
		<xsl:attribute name="background-color">#cccccc</xsl:attribute>
		<xsl:attribute name="border-color">#cccccc</xsl:attribute>
		<xsl:attribute name="border-width">0.25mm</xsl:attribute>
		<xsl:attribute name="border-style">solid</xsl:attribute>

	</xsl:attribute-set>

	<xsl:attribute-set name="th1">
		<xsl:attribute name="font-size">8pt</xsl:attribute>
		<xsl:attribute name="padding">0.5mm</xsl:attribute>
		<xsl:attribute name="border-color">#cccccc</xsl:attribute>
		<xsl:attribute name="border-width">0.25mm</xsl:attribute>
		<xsl:attribute name="border-style">solid</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="th1bold">
		<xsl:attribute name="font-weight">bold</xsl:attribute>
		<xsl:attribute name="font-size">8pt</xsl:attribute>
		<xsl:attribute name="padding">0.5mm</xsl:attribute>
		<xsl:attribute name="border-color">#cccccc</xsl:attribute>
		<xsl:attribute name="border-width">0.25mm</xsl:attribute>
		<xsl:attribute name="border-style">solid</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="th1bold-left">
		<xsl:attribute name="font-weight">bold</xsl:attribute>
		<xsl:attribute name="font-size">8pt</xsl:attribute>
		<xsl:attribute name="padding">0.5mm</xsl:attribute>
		<xsl:attribute name="border-color">#cccccc</xsl:attribute>
		<xsl:attribute name="border-width">0.25mm</xsl:attribute>
		<xsl:attribute name="border-style">solid</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="th2bold">
		<xsl:attribute name="font-weight">bold</xsl:attribute>
		<xsl:attribute name="font-size">8pt</xsl:attribute>
		<xsl:attribute name="padding">0.5mm</xsl:attribute>
		<xsl:attribute name="border-color">#cccccc</xsl:attribute>
		<xsl:attribute name="border-width">0.25mm</xsl:attribute>
		<xsl:attribute name="border-style">solid</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="tf2">
		<xsl:attribute name="font-size">8pt</xsl:attribute>
		<xsl:attribute name="padding">0.5mm</xsl:attribute>
		<xsl:attribute name="border-color">#cccccc</xsl:attribute>
		<xsl:attribute name="border-width">0.25mm</xsl:attribute>
		<xsl:attribute name="border-style">solid</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="cell-style0">
		<xsl:attribute name="font-size">8pt</xsl:attribute>
		<xsl:attribute name="padding">0.5mm</xsl:attribute>
		<xsl:attribute name="border-color">#cccccc</xsl:attribute>
		<xsl:attribute name="border-width">0.25mm</xsl:attribute>
		<xsl:attribute name="border-style">solid</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="cell-style">
		<xsl:attribute name="font-size">8pt</xsl:attribute>
		<xsl:attribute name="padding">0.5mm</xsl:attribute>
		<xsl:attribute name="border-color">#cccccc</xsl:attribute>
		<xsl:attribute name="border-width">0.25mm</xsl:attribute>
		<xsl:attribute name="border-style">solid</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="text-style">
		<xsl:attribute name="font-size">8pt</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="text-small">
		<xsl:attribute name="font-size">6pt</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="text-bold">
		<xsl:attribute name="font-size">8pt</xsl:attribute>
		<xsl:attribute name="font-weight">bold</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="spacer">
		<xsl:attribute name="font-size">3pt</xsl:attribute>
		<xsl:attribute name="line-height">1mm</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="title-style">
		<xsl:attribute name="font-size">10pt</xsl:attribute>
		<xsl:attribute name="font-weight">bold</xsl:attribute>
	</xsl:attribute-set>

	<xsl:attribute-set name="report-title">
		<xsl:attribute name="font-size">10pt</xsl:attribute>

	</xsl:attribute-set>

	<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 name="prozentAbw">
	<xsl:param name="data"/>
	<xsl:param name="dataCompare"/>

	<xsl:choose>
					<xsl:when test="string(number($dataCompare)) = 'NaN' and string(number($data)) = 'NaN'">
						<xsl:value-of select="'-'"/>
					</xsl:when>
					<xsl:when test="string(number($dataCompare)) = 'NaN'">
						<xsl:value-of select="'-'"/>
					</xsl:when>
					<xsl:when test="string(number($data)) = 'NaN'">
						<xsl:value-of select="format-number(0 ,'###.##0,##' , 'percent')"/><xsl:text>%</xsl:text>
					</xsl:when>
					<xsl:when test="$dataCompare = 0">
						<xsl:value-of select="'-'" />
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="format-number((($data div $dataCompare ) -1) * 100 ,'###.##0,##' , 'percent')"/><xsl:text>%</xsl:text>
					</xsl:otherwise>
				</xsl:choose>

</xsl:template>


	<xsl:output method="text"  encoding="ISO-8859-1"  />
	<xsl:strip-space elements="*" />
	<xsl:variable name="delimiter" select="';'" />
	<csv:columns>
        <column>position<desc>Platz</desc></column>
		<column>r_department_no<desc>Marke</desc></column>
		
		<column>r_businessunit_no<desc>Salon</desc></column>
		
		<column>r_employee_nm<desc>Mitarbeiter</desc></column>
		
		<column>r_planed_week_working_hours<desc>Std. Woche</desc></column>
		<column>r_cnt_dt<desc>AT</desc></column>
		<column>r_total<desc>Umsatz</desc>
		</column>
		<column>r_total_products<desc>davon Verkauf</desc>
		</column>
		<column>r_total_per_day<desc>Umsatz pro Tag</desc></column>
		<column>r_total_product_per_day<desc>davon VK pro Tag</desc></column>
		<column>r_total_per_customer<desc>Umsatz pro Besuch</desc></column>
		<column>r_amount_customer_per_day<desc>Besuche pro Tag</desc></column>
		
	
		<column>r_total_per_day_prev_year<desc>Umsatz pro Tag (% Abweichung zum Vorjahr)</desc></column>
		<column>r_total_product_per_day_prev_year<desc>davon VK pro Tag (% Abweichung zum Vorjahr)</desc></column>
		<column>r_total_per_customer_prev_year<desc>Umsatz pro Besuch (% Abweichung zum Vorjahr)</desc></column>
		<column>r_amount_customer_per_day_prev_year<desc>Besuche pro Tag (% Abweichung zum Vorjahr)</desc></column>
		
		
		
	</csv:columns>

	<xsl:template match="/">
<xsl:text>Hitliste Mitarbeiter </xsl:text>
								<xsl:choose> 
									<xsl:when test="/report/employeemode/row/employee_mode = 'ausf'">
										<xsl:text> (ausführend) </xsl:text>
									</xsl:when>
									<xsl:otherwise>	<xsl:text> (beratend) </xsl:text>	
									</xsl:otherwise>
								</xsl:choose>	
									<xsl:text> von </xsl:text>
							<xsl:call-template name="formatdate">
								<xsl:with-param name="datestr" select="report/report-params/DATE" />
							</xsl:call-template>
							<xsl:text> bis </xsl:text><xsl:call-template name="formatdate">
								<xsl:with-param name="datestr" select="report/report-params/TODATE" />
							</xsl:call-template>
							
					<xsl:text>
</xsl:text>
	
						<xsl:text>
</xsl:text>	
		<!-- Output the CSV header -->
		<xsl:for-each select="document('')/*/csv:columns/*">
			<xsl:value-of select="./desc" />
			<xsl:if test="position() != last()">
				<xsl:value-of select="$delimiter" />
			</xsl:if>
		</xsl:for-each>
		<!-- Add a newline at the end of the record -->
		<xsl:text>
</xsl:text>



			<xsl:for-each select="report/employeeRevenue/row">
			
				<xsl:variable name="property" select="." />
		        <xsl:variable name="posCd" select="r_pos_cd" />
	
		        <xsl:variable name="companyNo" select="r_company_no" />
		      	<xsl:variable name="departmentNo" select="r_department_no" />
		        <xsl:variable name="employeeNo" select="r_employee_no" />
				<xsl:variable name="position" select="position()"></xsl:variable>
			
		
			    <xsl:variable name="prevYearRow" select="/report/employeeRevenuePrevYear/row[r_pos_cd = $posCd][r_employee_no = $employeeNo]"></xsl:variable>
			
			<xsl:variable name="buNm" select="/report/businessunits/row[pos_cd_hint = $posCd]/businessunit_nm"></xsl:variable>
			<xsl:variable name="companyNm" select="/report/companies/row[company_no = $companyNo]/company_nm"></xsl:variable>
			<xsl:variable name="departmentNm"
										select="/report/departments/row[company_no = $companyNo][department_no = $departmentNo]/department_nm"></xsl:variable>
			<!-- Loop through the columns in order -->
			<xsl:for-each select="document('')/*/csv:columns/*">
				<!-- Extract the column name and value -->
				<xsl:variable name="column" select="./text()" />
				<xsl:variable name="value" select="$property/*[name() = $column]" />
				<xsl:choose>
					<xsl:when test="$column='r_pos_cd'"></xsl:when>
				
					<xsl:when test="$column='position'"><xsl:text>"</xsl:text><xsl:value-of select="$position" /><xsl:text>"</xsl:text></xsl:when>
					<xsl:when test="$column='r_department_no'"><xsl:text>"</xsl:text><xsl:value-of select="$departmentNm" /><xsl:text>"</xsl:text></xsl:when>
					<xsl:when test="$column='r_businessunit_no'"><xsl:text>"</xsl:text><xsl:value-of select="$buNm" /><xsl:text>"</xsl:text></xsl:when>
				
					<xsl:when test="$column='r_employee_nm'"><xsl:text>"</xsl:text><xsl:value-of select="$value" /><xsl:text>"</xsl:text></xsl:when>
					<xsl:when test="$column='r_planed_week_working_hours'"><xsl:value-of select="$value" /></xsl:when>
					<xsl:when test="$column='r_cnt_dt'"><xsl:value-of select="$value" /></xsl:when>
						
					<xsl:when test="$column='r_cnt_dt_prev_year'">
						<xsl:value-of select="$prevYearRow/r_cnt_dt" />
					</xsl:when>	
					<xsl:when test="$column='r_total_per_day_prev_year'">
						<xsl:call-template name="prozentAbw">
							<xsl:with-param name="data" select="$property/r_total_per_day"/>
							<xsl:with-param name="dataCompare" select="$prevYearRow/r_total_per_day"/>
						</xsl:call-template>
					</xsl:when>	
					<xsl:when test="$column='r_total_product_per_day_prev_year'">
						<xsl:call-template name="prozentAbw">
							<xsl:with-param name="data" select="$property/r_total_product_per_day"/>
							<xsl:with-param name="dataCompare" select="$prevYearRow/r_total_product_per_day"/>
						</xsl:call-template>
					</xsl:when>	
					<xsl:when test="$column='r_total_per_customer_prev_year'">
						<xsl:call-template name="prozentAbw">
							<xsl:with-param name="data" select="$property/r_total_per_customer"/>
							<xsl:with-param name="dataCompare" select="$prevYearRow/r_total_per_customer"/>
						</xsl:call-template>
					</xsl:when>	
					<xsl:when test="$column='r_amount_customer_per_day_prev_year'">
						<xsl:call-template name="prozentAbw">
							<xsl:with-param name="data" select="$property/r_amount_customer_per_day"/>
							<xsl:with-param name="dataCompare" select="$prevYearRow/r_amount_customer_per_day"/>
						</xsl:call-template>
					</xsl:when>	
					<xsl:otherwise>
						<xsl:value-of select="format-number($value, '###.##0,00', 'money')" />
					</xsl:otherwise>
				</xsl:choose>


				<!-- Add the delimiter unless we are the last expression -->
				<xsl:if test="position() != last()">
					<xsl:value-of select="$delimiter" />
				</xsl:if>
			</xsl:for-each>
			<!-- Add a newline at the end of the record -->
			<xsl:text>
</xsl:text>
		</xsl:for-each>
		


		
		</xsl:template>

	

</xsl:stylesheet>