<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet	version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"  
xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:100" 
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100" 
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:gaeb="http://www.gaeb.de/GAEB_DA_XML/DA89/3.3"
xmlns:itcrafts="itcrafts"
exclude-result-prefixes="gaeb itcrafts fn xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

	<!-- =====================================================================================
	include fuer XRechnung und ZUGFeRD CII-Formate
	GAEB.x89 mit it-crafts-Erweiterungen in Rechnung konvertieren
		
	2024-11-16 gs
		- Ursprungsversion zu SourceSave hinzugefuegt
		
	2024-12-20 gs
		- ram:ExemptionReasonCode entfernt.
		- Skonto muss auf eigener Zeile ausgegeben werden.
		- Skonto wird nur ausgegeben wenn Skonto Prozentsatz gegeben ist.
		
	2024-12-27 sa
		- SpecifiedTradePaymentTerms:   
			Skonto Formatierung geaendert
		- IncludedSupplyChainTradeLineItem:
			Feld Name bilden
			Menge und Einzelpreis Vorzeichen normalisiert
		- ApplicableHeaderTradeAgreement
			BT-11 Herkunft geaendert
			BT-17/BT-18/BT-19 ergaenzt
		- ApplicableHeaderTradeDelivery
			Pfad der Quellangaben geaendert
		- SpecifiedTaxRegistration
			ID-Unterelement fuer Steuernummer

	2025-09-03 sa			
		- SellerTradeParty
		    BT-34 Pflicht nach neuen Kosit Regeln
		- BuyerTradeParty
		    BT-49 Pflicht nach neuen Kosit Regeln
		- Preisfíndung der Positionen in Gaeb (itcrafts) verlegt
	
	2026-03-13 gs
		- SupplyChainTradeLineItem
			Im Falle von Rabatten werden Vorzeichen korrekt ausgelassen
		- ApplicableHeaderTradeDelivery
			tradeparty template entfernt und mit manuelle Ausgabe der Daten ersetzt.
			Zuerst wird versucht, Daten aus CnstSite zu ermitteln.
			Dann wird versucht, Daten aus InvoiceRecipient zu ermitteln.
			Sonst werden Standardwerte ausgegeben.
		- tradeParty template
			Bei PostalTradeAddress if-checks entfernt und mit Rückfallwerten ersetzt
		- ApplicableHeaderTradeAgreement > BuyerReference
			if-check mit Rückfallwert ersetzt
	
	===================================================================================== -->

	<xsl:include href="itcrafts_shared_Helpers.xsl"/>
	
	<xsl:variable name="UNITCODE_LIST">
		<xsl:apply-templates select="document('UnitCodes.xml')/UnitCodeList/*"/>
	</xsl:variable>
	<xsl:function name="itcrafts:getUnitcode">
		<xsl:param name="match"/>
		<xsl:value-of select="($UNITCODE_LIST/*[Match = $match][1]/@iso,'C62')[1]"/>
	</xsl:function>
	
	<!-- -->
	
	<xsl:template match="/gaeb:GAEB">
		<rsm:CrossIndustryInvoice>
			<xsl:call-template name="ExchangedDocumentContext"/>
			<xsl:call-template name="ExchangedDocument"/>
			<xsl:call-template name="SupplyChainTradeTransaction"/>
		</rsm:CrossIndustryInvoice>
	</xsl:template>
	
	<xsl:template name="ExchangedDocumentContext">
		<rsm:ExchangedDocumentContext>
				<!-- (BusinessProcessSpecifiedDocumentContextParameter) -->
				<ram:BusinessProcessSpecifiedDocumentContextParameter>
					<ram:ID><xsl:value-of select="$OutputSettings/businessID"/></ram:ID>
				</ram:BusinessProcessSpecifiedDocumentContextParameter>

				<!-- GuidelineSpecifiedDocumentContextParameter -->
				<ram:GuidelineSpecifiedDocumentContextParameter>
					<ram:ID><xsl:value-of select="$OutputSettings/guidelineID"/></ram:ID>
				</ram:GuidelineSpecifiedDocumentContextParameter>
			</rsm:ExchangedDocumentContext>
	</xsl:template>

	<xsl:template name="ExchangedDocument">
		<rsm:ExchangedDocument>
			<!-- ID -->
			<ram:ID><xsl:value-of select="gaeb:Invoice/gaeb:InvoiceHeader/gaeb:InvoiceNo"/></ram:ID>

			<!-- TypeCode -->
			<ram:TypeCode><xsl:value-of select="gaeb:Invoice/gaeb:InvoiceHeader/itcrafts:TypeCode"/></ram:TypeCode>
			
			<!-- IssueDateTime -->
			<ram:IssueDateTime><xsl:apply-templates select="gaeb:Invoice/gaeb:InvoiceHeader/gaeb:InvoiceDate" mode="date-102"/></ram:IssueDateTime>

			<!-- (IncludedNote*) -->
			<xsl:apply-templates select="gaeb:Invoice/gaeb:AddText" mode="IncludedNote"/>
		</rsm:ExchangedDocument>
	</xsl:template>

	<xsl:template name="SupplyChainTradeTransaction">
		<rsm:SupplyChainTradeTransaction>
			<!-- SupplyChainTradeLineItem* -->
			<xsl:apply-templates select="gaeb:Invoice/gaeb:BoQ/gaeb:BoQBody/gaeb:Itemlist/gaeb:Item" mode="SupplyChainTradeLineItem"/>
			
			<!-- HeaderTradeAgreement -->
			<xsl:apply-templates select="gaeb:Invoice" mode="HeaderTradeAgreement"/>
			
			<!-- ApplicableHeaderTradeDelivery -->
			<ram:ApplicableHeaderTradeDelivery>
			
				<xsl:choose>
					<!-- Falls CnstSite vorhanden ist, übernimm die Daten -->
					<xsl:when test="gaeb:Invoice/gaeb:CnstSite">
						<ram:ShipToTradeParty>
							<xsl:if test="gaeb:Invoice/gaeb:CnstSite/gaeb:CnstSiteIDNo">
								<ram:ID><xsl:value-of select="gaeb:Invoice/gaeb:CnstSite/gaeb:CnstSiteIDNo"/></ram:ID>
							</xsl:if>
							<ram:PostalTradeAddress>
								<ram:PostcodeCode><xsl:value-of select="(gaeb:Invoice/gaeb:CnstSite/gaeb:Address/gaeb:PCode, 'n.a.')[1]"/></ram:PostcodeCode>
								<ram:LineOne><xsl:value-of select="(gaeb:Invoice/gaeb:CnstSite/gaeb:Address/gaeb:Street, 'n.a.')[1]"/></ram:LineOne>
								<ram:CityName><xsl:value-of select="(gaeb:Invoice/gaeb:CnstSite/gaeb:Address/gaeb:City, 'n.a.')[1]"/></ram:CityName>
								<ram:CountryID><xsl:value-of select="(gaeb:Invoice/gaeb:CnstSite/itcrafts:CountryID,'DE')[1]"/></ram:CountryID>
							</ram:PostalTradeAddress>
						</ram:ShipToTradeParty>
					</xsl:when>
					
					<!-- Falls möglich benötigte Felder von InvoiceRecipient übernehmen -->
					<xsl:otherwise>
						<ram:ShipToTradeParty>
							<ram:PostalTradeAddress>
								<ram:PostcodeCode><xsl:value-of select="(gaeb:Invoice/gaeb:InvoiceRecipient/gaeb:Address/gaeb:PCode, 'n.a.')[1]"/></ram:PostcodeCode>
								<ram:LineOne><xsl:value-of select="(gaeb:Invoice/gaeb:InvoiceRecipient/gaeb:Address/gaeb:Street, 'n.a.')[1]"/></ram:LineOne>
								<ram:CityName><xsl:value-of select="(gaeb:Invoice/gaeb:InvoiceRecipient/gaeb:Address/gaeb:City, 'n.a.')[1]"/></ram:CityName>
								<ram:CountryID><xsl:value-of select="(gaeb:Invoice/gaeb:InvoiceRecipient/itcrafts:CountryID,'DE')[1]"/></ram:CountryID>
							</ram:PostalTradeAddress>
						</ram:ShipToTradeParty>
					</xsl:otherwise>
				</xsl:choose>
				
				<!--ActualDeliverySupplyChainEvent-->
				<!-- BT-72 -->
				<ram:ActualDeliverySupplyChainEvent>
					<ram:OccurrenceDateTime>
						<xsl:apply-templates select="gaeb:Invoice/gaeb:InvoiceHeader/gaeb:ServiceProvisionEndDate" mode="date-102"/>
					</ram:OccurrenceDateTime>
				</ram:ActualDeliverySupplyChainEvent>
				
				<!--DespatchAdviceReferencedDocument-->
				<!--ReceivingAdviceReferencedDocument-->
			</ram:ApplicableHeaderTradeDelivery>
			
			<!-- ApplicableHeaderTradeSettlement -->
			<xsl:apply-templates select="." mode="ApplicableHeaderTradeSettlement"/>
		</rsm:SupplyChainTradeTransaction>
	</xsl:template>
	
	<xsl:template match="gaeb:AddText" mode="IncludedNote">
		<ram:IncludedNote>
			<ram:Content>
				<xsl:apply-templates select="gaeb:DetailAddText" mode="clean-text"/>
			</ram:Content>
		</ram:IncludedNote>
	</xsl:template>

	<!-- =========================================================================== Item -->
	
	<xsl:template match="gaeb:Invoice/gaeb:BoQ/gaeb:BoQBody/gaeb:Itemlist/gaeb:Item" mode="SupplyChainTradeLineItem">
		<ram:IncludedSupplyChainTradeLineItem>
		
			<!-- AssociatedDocumentLineDocument -->
			<ram:AssociatedDocumentLineDocument>
				<ram:LineID><xsl:value-of select="@ID"/></ram:LineID>
			</ram:AssociatedDocumentLineDocument>

			<!-- Inhalt fuer Element Name bilden -->
			<xsl:variable name="positionNumber">
				<xsl:if test="itcrafts:PosNr">
					<xsl:value-of select="itcrafts:PosNr"/>
				</xsl:if>
			</xsl:variable>
			<xsl:variable name="headLine">
				<xsl:if test="gaeb:Description/gaeb:CompleteText/gaeb:OutlineText/gaeb:OutlTxt/gaeb:TextOutlTxt">
					<xsl:apply-templates select="gaeb:Description/gaeb:CompleteText/gaeb:OutlineText/gaeb:OutlTxt/gaeb:TextOutlTxt" mode="clean-text"/>
				</xsl:if>
			</xsl:variable>
			<xsl:variable name="itemName">
				<xsl:value-of select="$positionNumber"/>
				<xsl:if test="$positionNumber != '' and $headLine != ''"><xsl:text> </xsl:text></xsl:if>
				<xsl:value-of select="$headLine"/>
			</xsl:variable>

			<!-- SpecifiedTradeProduct -->
			<ram:SpecifiedTradeProduct>
			    <xsl:choose>
					<xsl:when test="$itemName != ''">
						<ram:Name><xsl:value-of select="$itemName"/></ram:Name>
					</xsl:when>
					<xsl:otherwise>
						<ram:Name><xsl:text>n.a.</xsl:text></ram:Name>
					</xsl:otherwise>
			    </xsl:choose>
				
				<xsl:if test="gaeb:Description/gaeb:CompleteText/gaeb:DetailTxt/gaeb:Text">
					<ram:Description><xsl:apply-templates select="gaeb:Description/gaeb:CompleteText/gaeb:DetailTxt/gaeb:Text" mode="clean-text"/></ram:Description>
				</xsl:if>
			</ram:SpecifiedTradeProduct>

			<!-- SpecifiedLineTradeAgreement (Einzelpreis) -->
			<ram:SpecifiedLineTradeAgreement>
				<ram:NetPriceProductTradePrice>
					<ram:ChargeAmount><xsl:value-of select="itcrafts:Price/itcrafts:EP"/></ram:ChargeAmount>
				</ram:NetPriceProductTradePrice>
			</ram:SpecifiedLineTradeAgreement>
			
			<!-- SpecifiedLineTradeDelivery (Menge mit Einheit) -->
			<ram:SpecifiedLineTradeDelivery>
				<ram:BilledQuantity unitCode="{itcrafts:getUnitcode(gaeb:QU)}">
					<xsl:value-of select="itcrafts:Price/itcrafts:Qty"/>
				</ram:BilledQuantity>
			</ram:SpecifiedLineTradeDelivery>
		
			<!-- SpecifiedLineTradeSettlement -->
			<ram:SpecifiedLineTradeSettlement>
				<!-- Umsatzsteuer -->
				<xsl:apply-templates select="." mode="ApplicableTradeTax"/>

				<!-- Rabatt ausgeben, wenn vorhanden -->
				<xsl:if test="itcrafts:Charge">
					<ram:SpecifiedTradeAllowanceCharge>
					
						<ram:ChargeIndicator>
							<udt:Indicator><xsl:value-of select="itcrafts:Charge/itcrafts:ChargeIndicator"/></udt:Indicator>
						</ram:ChargeIndicator>
					
						<xsl:choose>
							<!-- Aufschlag -->
							<xsl:when test="itcrafts:Charge/itcrafts:ChargeIndicator = 'true'">
								<ram:CalculationPercent><xsl:value-of select="itcrafts:Charge/itcrafts:ChargePercent"/></ram:CalculationPercent>
								<ram:BasisAmount><xsl:value-of select="itcrafts:Price/itcrafts:GP"/></ram:BasisAmount>					
								<ram:ActualAmount><xsl:value-of select="itcrafts:Charge/itcrafts:ChargeAmount"/></ram:ActualAmount>
							</xsl:when>

							<!-- Rabatt -->
							<xsl:otherwise>
								<ram:CalculationPercent><xsl:value-of select="substring(itcrafts:Charge/itcrafts:ChargePercent,2)"/></ram:CalculationPercent>
								<ram:BasisAmount><xsl:value-of select="itcrafts:Price/itcrafts:GP"/></ram:BasisAmount>					
								<ram:ActualAmount><xsl:value-of select="substring(itcrafts:Charge/itcrafts:ChargeAmount,2)"/></ram:ActualAmount>
							</xsl:otherwise>
						</xsl:choose>

						<ram:Reason><xsl:value-of select="itcrafts:Charge/itcrafts:ChargeReason"/></ram:Reason>
					</ram:SpecifiedTradeAllowanceCharge>
				</xsl:if>

				<!-- Zeilenbetrag -->
				<ram:SpecifiedTradeSettlementLineMonetarySummation>
					<ram:LineTotalAmount><xsl:value-of select="gaeb:IT"/></ram:LineTotalAmount>
				</ram:SpecifiedTradeSettlementLineMonetarySummation>

			</ram:SpecifiedLineTradeSettlement>
		</ram:IncludedSupplyChainTradeLineItem>
	</xsl:template>
	
	<!-- ================================================================================= -->

	<xsl:template match="gaeb:Invoice" mode="HeaderTradeAgreement">
		<ram:ApplicableHeaderTradeAgreement>
		    <!-- BT-10 -->
			<ram:BuyerReference><xsl:value-of select="(gaeb:InvoiceRecipient/itcrafts:LeitwegID, 'n.a.')[1]"/></ram:BuyerReference>

			<!-- SellerTradeParty -->
			<xsl:choose>
				<!-- BT-34 wurde in der GAEB-Datei angegeben -->
				<xsl:when test="gaeb:InvoiceCreator/gaeb:Address/itcrafts:uriValue">
					<xsl:apply-templates select="gaeb:InvoiceCreator" mode="tradeParty">
						<xsl:with-param name="header" select="'ram:SellerTradeParty'"/>
						<xsl:with-param name="id" select="gaeb:CTR/gaeb:AcctRecNo"/>
						<xsl:with-param name="uriValue" select="gaeb:InvoiceCreator/gaeb:Address/itcrafts:uriValue"/>
						<xsl:with-param name="uriScheme" select="gaeb:InvoiceCreator/gaeb:Address/itcrafts:uriScheme"/>
					</xsl:apply-templates>
				</xsl:when>
				<!-- BT-34 Scheme=9930 German VA -->
				<xsl:when test="gaeb:InvoiceCreator/gaeb:Address/gaeb:VATID">
					<xsl:apply-templates select="gaeb:InvoiceCreator" mode="tradeParty">
						<xsl:with-param name="header" select="'ram:SellerTradeParty'"/>
						<xsl:with-param name="id" select="gaeb:CTR/gaeb:AcctRecNo"/>
						<xsl:with-param name="uriValue" select="gaeb:InvoiceCreator/gaeb:Address/gaeb:VATID"/>
						<xsl:with-param name="uriScheme" select="'9930'"/>
					</xsl:apply-templates>
				</xsl:when>
				<!-- BT-34 nicht ausgeben -->
				<xsl:otherwise>
					<xsl:apply-templates select="gaeb:InvoiceCreator" mode="tradeParty">
						<xsl:with-param name="header" select="'ram:SellerTradeParty'"/>
						<xsl:with-param name="id" select="gaeb:CTR/gaeb:AcctRecNo"/>
					</xsl:apply-templates>
				</xsl:otherwise>
			</xsl:choose>

			<!-- BuyerTradeParty -->
			<xsl:choose>
				<!-- BT-49 wurde in der GAEB-Datei angegeben -->
				<xsl:when test="gaeb:InvoiceRecipient/gaeb:Address/itcrafts:uriValue">
					<xsl:apply-templates select="gaeb:InvoiceRecipient" mode="tradeParty">
						<xsl:with-param name="header" select="'ram:BuyerTradeParty'"/>
						<xsl:with-param name="id" select="gaeb:OWN/gaeb:AcctRecNo"/>
						<xsl:with-param name="uriValue" select="gaeb:InvoiceRecipient/gaeb:Address/itcrafts:uriValue"/>
						<xsl:with-param name="uriScheme" select="gaeb:InvoiceRecipient/gaeb:Address/itcrafts:uriScheme"/>
					</xsl:apply-templates>
				</xsl:when>
				<!-- BT-49 Scheme=9930 German VA -->
				<xsl:when test="gaeb:InvoiceRecipient/gaeb:Address/gaeb:VATID">
					<xsl:apply-templates select="gaeb:InvoiceRecipient" mode="tradeParty">
						<xsl:with-param name="header" select="'ram:BuyerTradeParty'"/>
						<xsl:with-param name="id" select="gaeb:OWN/gaeb:AcctRecNo"/>
						<xsl:with-param name="uriValue" select="gaeb:InvoiceRecipient/gaeb:Address/gaeb:VATID"/>
						<xsl:with-param name="uriScheme" select="'9930'"/>
					</xsl:apply-templates>
				</xsl:when>
				<!-- BT-49 Scheme=0204 Leitweg-ID -->
				<xsl:when test="gaeb:InvoiceRecipient/itcrafts:LeitwegID">
					<xsl:apply-templates select="gaeb:InvoiceRecipient" mode="tradeParty">
						<xsl:with-param name="header" select="'ram:BuyerTradeParty'"/>
						<xsl:with-param name="id" select="gaeb:OWN/gaeb:AcctRecNo"/>
						<xsl:with-param name="uriValue" select="gaeb:InvoiceRecipient/itcrafts:LeitwegID"/>
						<xsl:with-param name="uriScheme" select="'0204'"/>
					</xsl:apply-templates>
				</xsl:when>
				<!-- BT-49 nicht angegeben -->
				<xsl:otherwise>
					<xsl:apply-templates select="gaeb:InvoiceRecipient" mode="tradeParty">
						<xsl:with-param name="header" select="'ram:BuyerTradeParty'"/>
						<xsl:with-param name="id" select="gaeb:OWN/gaeb:AcctRecNo"/>
					</xsl:apply-templates>
				</xsl:otherwise>
			</xsl:choose>

			<!-- BT-14 -->
			<xsl:if test="gaeb:InvoiceHeader/itcrafts:AssignmentNo and gaeb:InvoiceHeader/itcrafts:AssignmentNo != ''">
				<ram:SellerOrderReferencedDocument>
					<ram:IssuerAssignedID><xsl:value-of select="gaeb:InvoiceHeader/itcrafts:AssignmentNo"/></ram:IssuerAssignedID>
				</ram:SellerOrderReferencedDocument>
			</xsl:if>

			<!-- BT-13 -->
			<xsl:if test="gaeb:InvoiceHeader/itcrafts:OrderNo and gaeb:InvoiceHeader/itcrafts:OrderNo != ''">
				<ram:BuyerOrderReferencedDocument>
					<ram:IssuerAssignedID><xsl:value-of select="gaeb:InvoiceHeader/itcrafts:OrderNo"/></ram:IssuerAssignedID>
				</ram:BuyerOrderReferencedDocument>
			</xsl:if>

			<!-- BT-12 -->
			<xsl:if test="gaeb:InvoiceHeader/itcrafts:ContractNo and gaeb:InvoiceHeader/itcrafts:ContractNo != ''">
				<ram:ContractReferencedDocument>
					<ram:IssuerAssignedID><xsl:value-of select="gaeb:InvoiceHeader/itcrafts:ContractNo"/></ram:IssuerAssignedID>
				</ram:ContractReferencedDocument>
			</xsl:if>

			<!-- BT-17 -->
			<xsl:if test="gaeb:InvoiceHeader/itcrafts:AwardNo and gaeb:InvoiceHeader/itcrafts:AwardNo != ''">
				<ram:AdditionalReferencedDocument>
					<ram:IssuerAssignedID><xsl:value-of select="gaeb:InvoiceHeader/itcrafts:AwardNo"/></ram:IssuerAssignedID>
					<ram:TypeCode>50</ram:TypeCode>
				</ram:AdditionalReferencedDocument>
			</xsl:if>

			<!-- BT-18 -->
			<xsl:if test="gaeb:InvoiceHeader/itcrafts:ObjectNo and gaeb:InvoiceHeader/itcrafts:ObjectNo != ''">
				<ram:AdditionalReferencedDocument>
					<ram:IssuerAssignedID><xsl:value-of select="gaeb:InvoiceHeader/itcrafts:ObjectNo"/></ram:IssuerAssignedID>
					<ram:TypeCode>130</ram:TypeCode>
				</ram:AdditionalReferencedDocument>
			</xsl:if>

			<!-- Attachments -->
			<xsl:apply-templates select="gaeb:InvoiceHeader/itcrafts:AdditionalDocument" mode="AdditionalReferencedDocument"/>

			<!-- BT-11 -->
			<xsl:if test="gaeb:InvoiceHeader/itcrafts:ProjectID and gaeb:InvoiceHeader/itcrafts:ProjectID != ''">
				<ram:SpecifiedProcuringProject>
					<ram:ID><xsl:value-of select="gaeb:InvoiceHeader/itcrafts:ProjectID"/></ram:ID>
					<ram:Name><xsl:value-of select="gaeb:InvoiceHeader/itcrafts:ProjectID"/></ram:Name>
				</ram:SpecifiedProcuringProject>
			</xsl:if>
		</ram:ApplicableHeaderTradeAgreement>
	</xsl:template>
	
	<xsl:template match="gaeb:GAEB" mode="ApplicableHeaderTradeSettlement">
		<ram:ApplicableHeaderTradeSettlement>
			<ram:InvoiceCurrencyCode><xsl:value-of select="(gaeb:PrjInfo/gaeb:Cur,'EUR')[1]"/></ram:InvoiceCurrencyCode>

			<xsl:apply-templates select="gaeb:Invoice/gaeb:InvoiceCreator/gaeb:Address/gaeb:Bank/gaeb:IBAN" mode="SpecifiedTradeSettlementPaymentMeans"/>

			<xsl:apply-templates select="gaeb:Invoice/gaeb:BoQ/gaeb:BoQInfo/gaeb:Totals/gaeb:VATPart" mode="ApplicableTradeTax"/>

			<xsl:if test="gaeb:Invoice/gaeb:InvoiceHeader/gaeb:ServiceProvisionStartDate or gaeb:Invoice/gaeb:InvoiceHeader/gaeb:ServiceProvisionEndDate">
				<ram:BillingSpecifiedPeriod>
					<ram:StartDateTime><xsl:apply-templates select="gaeb:Invoice/gaeb:InvoiceHeader/gaeb:ServiceProvisionStartDate" mode="date-102"/></ram:StartDateTime>
					<ram:EndDateTime><xsl:apply-templates select="gaeb:Invoice/gaeb:InvoiceHeader/gaeb:ServiceProvisionEndDate" mode="date-102"/></ram:EndDateTime>
				</ram:BillingSpecifiedPeriod>
			</xsl:if>

			<xsl:apply-templates select="gaeb:Invoice/gaeb:InvoiceHeader" mode="SpecifiedTradePaymentTerms"/>

			<xsl:variable name="TotalPrepaidAmount">
				<xsl:value-of select="itcrafts:formatAmount(gaeb:Invoice/gaeb:InvoiceShare[itcrafts:SubType = 'TotalPrepaidAmount']/gaeb:Total)"/>	
			</xsl:variable>

			<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
				<ram:LineTotalAmount><xsl:value-of select="itcrafts:formatAmount(gaeb:Invoice/gaeb:InvoiceShare[itcrafts:SubType = 'LineTotalAmount']/gaeb:Total)"/></ram:LineTotalAmount>
				<ram:TaxBasisTotalAmount><xsl:value-of select="itcrafts:formatAmount(gaeb:Invoice/gaeb:InvoiceShare[itcrafts:SubType = 'TaxBasisTotalAmount']/gaeb:Total)"/></ram:TaxBasisTotalAmount>
				<ram:TaxTotalAmount currencyID="{(gaeb:PrjInfo/gaeb:Cur,'EUR')[1]}"><xsl:value-of select="itcrafts:formatAmount(gaeb:Invoice/gaeb:InvoiceShare[itcrafts:SubType = 'TaxTotalAmount']/gaeb:Total)"/></ram:TaxTotalAmount>
				<ram:GrandTotalAmount><xsl:value-of select="itcrafts:formatAmount(gaeb:Invoice/gaeb:InvoiceShare[itcrafts:SubType = 'GrandTotalAmount']/gaeb:Total)"/></ram:GrandTotalAmount>
				<xsl:if test="$TotalPrepaidAmount != '0'">
					<ram:TotalPrepaidAmount><xsl:value-of select="$TotalPrepaidAmount"/></ram:TotalPrepaidAmount>
				</xsl:if>
				<ram:DuePayableAmount><xsl:value-of select="itcrafts:formatAmount(gaeb:Invoice/gaeb:TotalGross)"/></ram:DuePayableAmount>
			</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
			
			<!-- BT-19 -->
			<xsl:if test="gaeb:Invoice/gaeb:InvoiceHeader/itcrafts:CostCenter and gaeb:Invoice/gaeb:InvoiceHeader/itcrafts:CostCenter != ''">
				<ram:ReceivableSpecifiedTradeAccountingAccount>
					<ram:ID><xsl:value-of select="gaeb:Invoice/gaeb:InvoiceHeader/itcrafts:CostCenter"/></ram:ID>
				</ram:ReceivableSpecifiedTradeAccountingAccount>
			</xsl:if>
			
		</ram:ApplicableHeaderTradeSettlement>
	</xsl:template>

	<xsl:template match="gaeb:Invoice/gaeb:InvoiceCreator/gaeb:Address/gaeb:Bank/gaeb:IBAN" mode="SpecifiedTradeSettlementPaymentMeans">
		<ram:SpecifiedTradeSettlementPaymentMeans>
			<ram:TypeCode>58</ram:TypeCode>

			<ram:PayeePartyCreditorFinancialAccount>
				<ram:IBANID><xsl:value-of select="."/></ram:IBANID>
				<!-- AccountName := itcrafts:AccHolder (abw. Kontoinhaber) -->
				<xsl:if test="../itcrafts:AccHolder">
					<ram:AccountName><xsl:value-of select="../itcrafts:AccHolder"/></ram:AccountName>
				</xsl:if>
			</ram:PayeePartyCreditorFinancialAccount>
			
		</ram:SpecifiedTradeSettlementPaymentMeans>
	</xsl:template>
	
	<xsl:template match="itcrafts:AdditionalDocument" mode="AdditionalReferencedDocument">
		<ram:AdditionalReferencedDocument>
			<ram:IssuerAssignedID><xsl:value-of select="itcrafts:ID"/></ram:IssuerAssignedID>
			
			<xsl:if test="itcrafts:URIID and itcrafts:URIID != ''">
				<ram:URIID><xsl:value-of select="itcrafts:URIID"/></ram:URIID>
			</xsl:if>
			
			<ram:TypeCode>916</ram:TypeCode>
			<ram:Name><xsl:value-of select="itcrafts:FileName"/></ram:Name>
			
			<xsl:if test="itcrafts:ContentBase64 and itcrafts:ContentBase64 != ''">
				<ram:AttachmentBinaryObject>
					<xsl:attribute name="mimeCode" select="itcrafts:MimeCode"/>
					<xsl:attribute name="filename" select="itcrafts:FileName"/>
					<xsl:value-of select="itcrafts:ContentBase64"/>
				</ram:AttachmentBinaryObject>
			</xsl:if>
		</ram:AdditionalReferencedDocument>
	</xsl:template>
	
	<xsl:template match="gaeb:InvoiceHeader" mode="SpecifiedTradePaymentTerms">
		<xsl:variable name="desc">
			<xsl:text>&#10;&#9;&#9;&#9;&#9;</xsl:text>
			<xsl:value-of select="'#SKONTO#TAGE='"/>
			<xsl:value-of select="itcrafts:CashDiscountDays"/>
			<xsl:value-of select="'#PROZENT='"/>
			<xsl:value-of select="format-number(itcrafts:CashDiscountPercent, '0.00')"/>
			<!-- Basisbetrag immer angeben, da er immer vom Rechnungsbetrag abweichend vereinbart werden kann -->
			<xsl:value-of select="'#BASISBETRAG='"/>
			<xsl:value-of select="format-number(itcrafts:CashDiscountBaseAmount, '0.00')"/>
			<xsl:value-of select="'#'"/>
			<!-- xrechnung dokumentation gibt an dass der basisbetrag nur angegeben soll, falls es sich um eine teilrechnung handelt -->			
			<!--
			<xsl:if test="//gaeb:Invoice/gaeb:InvoiceShare[itcrafts:SubType = 'TotalPrepaidAmount']/gaeb:Total &gt; 0"> 
				<xsl:value-of select="'BASISBETRAG='"/>
				<xsl:value-of select="format-number(itcrafts:CashDiscountBaseAmount, '0.00')"/>
				<xsl:value-of select="'#'"/>
			</xsl:if>
			-->
			<xsl:text>&#10;&#9;&#9;&#9;</xsl:text>
		</xsl:variable>

		<ram:SpecifiedTradePaymentTerms>
			<xsl:if test="itcrafts:CashDiscountPercent and itcrafts:CashDiscountPercent != ''">
				<ram:Description>
					<xsl:value-of select="$desc"/>
				</ram:Description>
			</xsl:if>
			<ram:DueDateDateTime><xsl:apply-templates select="itcrafts:DueDate" mode="date-102"/></ram:DueDateDateTime>
		</ram:SpecifiedTradePaymentTerms>
	</xsl:template>

	<xsl:template match="*" mode="tradeParty">
		<xsl:param name="header"/>
		<xsl:param name="id"/>
		<xsl:param name="uriValue"/>
		<xsl:param name="uriScheme"/>
		
		<xsl:variable name="combinedNames">
			<xsl:for-each select="gaeb:Address/*[substring(name(), 1, 4) = 'Name']">
				<xsl:value-of select="if(position() = last()) then . else concat(., '&#10;')"/>
			</xsl:for-each>
		</xsl:variable>
		
		<xsl:element name="{$header}">		
			<!-- ID -->
			<xsl:if test="$id">
			<ram:ID><xsl:value-of select="$id"/></ram:ID>
			</xsl:if>
			
			<!-- GlobalID -->
			<xsl:if test="gaeb:Address/gaeb:ILN">
			<ram:GlobalID schemeID='0088'><xsl:value-of select="gaeb:Address/gaeb:ILN"/></ram:GlobalID>
			</xsl:if>
			
			<!-- kombinierte Namen -->
			<xsl:if test="$combinedNames">
			<ram:Name><xsl:value-of select="$combinedNames"/></ram:Name>
			</xsl:if>
			
			<!-- Beschreibung -->
			<xsl:if test="itcrafts:Pflichtangaben">
				<ram:Description><xsl:value-of select="itcrafts:Pflichtangaben"/></ram:Description>
			</xsl:if>
			
			<!-- DefinedTradeContact -->
			<xsl:if test="concat(gaeb:Address/gaeb:Contact,gaeb:Address/gaeb:Phone,gaeb:Address/gaeb:Email) != ''">
				<ram:DefinedTradeContact>
					<ram:PersonName><xsl:value-of select="(gaeb:Address/gaeb:Contact,'n.a.')[1]"/></ram:PersonName>
					<ram:TelephoneUniversalCommunication>
						<ram:CompleteNumber><xsl:value-of select="(gaeb:Address/gaeb:Phone,'n.a.')[1]"/></ram:CompleteNumber>
					</ram:TelephoneUniversalCommunication>
					<ram:EmailURIUniversalCommunication>
						<ram:URIID><xsl:value-of select="(gaeb:Address/gaeb:Email,'n.a.')[1]"/></ram:URIID>
					</ram:EmailURIUniversalCommunication>
				</ram:DefinedTradeContact>
			</xsl:if>
			
			<!-- PostalTradeAddress -->
			<ram:PostalTradeAddress>
				<ram:PostcodeCode><xsl:value-of select="(gaeb:Address/gaeb:PCode, 'n.a.')[1]"/></ram:PostcodeCode>
				<ram:LineOne><xsl:value-of select="(gaeb:Address/gaeb:Street, 'n.a.')[1]"/></ram:LineOne>
				<ram:CityName><xsl:value-of select="(gaeb:Address/gaeb:City, 'n.a.')[1]"/></ram:CityName>
				<ram:CountryID><xsl:value-of select="(itcrafts:CountryID,'DE')[1]"/></ram:CountryID>
			</ram:PostalTradeAddress>

            <!-- BT-34 bzw. BT-49 -->
			<xsl:if test="string-length($uriValue) != 0">
				<ram:URIUniversalCommunication>
					<ram:URIID>
						<xsl:attribute name="schemeID">
						   <xsl:value-of select="$uriScheme"/>
						</xsl:attribute>
						<xsl:value-of select="$uriValue"/>
					</ram:URIID>
				</ram:URIUniversalCommunication>
			</xsl:if>

			<!-- UStID oder Steuernummer nicht beides -->
			<xsl:choose>
				<xsl:when test="gaeb:Address/gaeb:VATID and gaeb:Address/gaeb:VATID != ''">
					<ram:SpecifiedTaxRegistration>
						<ram:ID schemeID='VA'><xsl:value-of select="gaeb:Address/gaeb:VATID"/></ram:ID>
					</ram:SpecifiedTaxRegistration>
				</xsl:when>
				<xsl:when test="gaeb:TaxNo and gaeb:TaxNo != ''">
					<ram:SpecifiedTaxRegistration>
						<ram:ID schemeID='FC'><xsl:value-of select="gaeb:TaxNo"/></ram:ID>
					</ram:SpecifiedTaxRegistration>
				</xsl:when>
			</xsl:choose>
		</xsl:element>
	</xsl:template>

	<xsl:template match="*" mode="ApplicableTradeTax">
		<ram:ApplicableTradeTax>
			<xsl:if test="gaeb:VATAmount">
				<ram:CalculatedAmount><xsl:value-of select="itcrafts:formatAmount(gaeb:VATAmount)"/></ram:CalculatedAmount>
			</xsl:if>
			
			<ram:TypeCode>VAT</ram:TypeCode>
			
			<xsl:if test="itcrafts:RateApplicablePercent = 0">
				<ram:ExemptionReason><xsl:value-of select="//gaeb:Invoice/gaeb:BoQ/gaeb:BoQInfo/gaeb:Totals/gaeb:VATPart/itcrafts:MWSt_Description"/></ram:ExemptionReason>
			</xsl:if>
			
			<xsl:if test="gaeb:TotalNetPart">
				<ram:BasisAmount><xsl:value-of select="itcrafts:formatAmount(gaeb:TotalNetPart)"/></ram:BasisAmount>
			</xsl:if>

			<ram:CategoryCode><xsl:value-of select="itcrafts:CategoryCode"/></ram:CategoryCode>
			
			<ram:RateApplicablePercent><xsl:value-of select="itcrafts:formatAmount(itcrafts:RateApplicablePercent)"/></ram:RateApplicablePercent>
		</ram:ApplicableTradeTax>
	</xsl:template>
	
</xsl:stylesheet>