<!--  ===== Copyright Notice =====  -->
<!--   
 Copyright (C) Elektronički Računi d.o.o 2019. All Rights Reserved.

Elektronički Računi d.o.o  takes no position regarding the validity or scope of any 
  intellectual property or other rights that might be claimed to pertain 
  to the implementation or use of the technology described in this 
  document or the extent to which any license under such rights 
  might or might not be available; neither does it represent that it has 
  made any effort to identify any such rights. 

This document and translations of it may be copied and furnished to 
  others, in whole or in part, for the purpose of integration with
  Moj eRačun network, provided that the above copyright notice and this 
  paragraph are included on all such copies and derivative works. 
  Other usages of the document or its translations are prohibited.
  However, this document itself may not be modified in any way, 
  such as by removing the copyright notice or references to 
  OASIS or Elektronički računi d.o.o., 
  -->

<!-- V2025-01-20
Dodana provjera ako je AccountingCustomerParty/EndpointID zapravo GLN, a nema atributa - da se procesira ispravno kao GLN a ne podjedinica

-->

<!-- V2024-11-28
Hendlanje @schemeID atributa u DeliveryLocation/cbc:ID i cbc:EndpointID
Stavlja ispravne EN kodove za GLN i DUNS
Dodaje 0088 kao default ako @schemeID nije naveden

-->


<!-- V2024-10-14
Napravio da ispravno hendla DeliveryParty - sa "viškom podataka" radi mStart računa (EndpointID i ostalo - EN u DeliveryParty predviđa samo PartyName)
Dodao i komentar NA KRAJ - da potvrdimo da se zvala upravo ta transformacija


-->
  
  
  
  <!-- V2024-10-03
Ispravljen bug sa namespaceom za header
Napravljeno da prihvaća i http://ubl.moj-eracun.hr/v1.2/xsd i http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2



-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://ubl.moj-eracun.hr/v1.2/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:AttachedDocument-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:hrextac="urn:mfin.gov.hr:schema:xsd:HRExtensionAggregateComponents-1" 
xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2" xmlns:ns3="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2" version="1.1" exclude-result-prefixes="ns0 xs ns1 ns2 ns3">

	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	<xsl:param name="supplierBusinessUnit"/>
	<xsl:param name="customerBusinessUnit"/>
	<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>
	<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
	<xsl:variable name="countryPrefixList">-+
<xsl:copy-of select="document('codelist/CountryPrefixList.xml')"/>
	</xsl:variable>
	<xsl:variable name="schemeIDList">
		<xsl:copy-of select="document('codelist/SchemeIDCodeList.xml')"/>
	</xsl:variable>
	
	<xsl:variable name="supplierCountryPrefix">
		<xsl:choose>
			<xsl:when test="normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode) != ''">
				<xsl:value-of select="translate(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode,$uppercase,$lowercase)"/>
			</xsl:when>
			<xsl:when test="normalize-space(substring(//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2))!= ''">
				<xsl:value-of select="translate(substring(//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2),$uppercase,$lowercase)"/>
			</xsl:when>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="customerCountryPrefix">
		<xsl:choose>
			<xsl:when test="normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode) != ''">
				<xsl:value-of select="translate(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode,$uppercase,$lowercase)"/>
			</xsl:when>
			<xsl:when test="normalize-space(substring(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2))!= ''">
				<xsl:value-of select="translate(substring(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2),$uppercase,$lowercase)"/>
			</xsl:when>
		</xsl:choose>
	</xsl:variable>
	
	<xsl:variable name="supplierCountrySchemeID">
		<xsl:choose>
			<xsl:when test="$schemeIDList//SchemeID[@ID= $supplierCountryPrefix] != '' ">
				<xsl:value-of select="$schemeIDList//SchemeID[@ID=$supplierCountryPrefix]"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$schemeIDList//SchemeID[@ID='hr']"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="customerCountrySchemeID">
		<xsl:choose>
			<xsl:when test="$schemeIDList//SchemeID[@ID= $customerCountryPrefix] != '' ">
				<xsl:value-of select="$schemeIDList//SchemeID[@ID=$customerCountryPrefix]"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$schemeIDList//SchemeID[@ID='hr']"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	

	
	<xsl:variable name="countryCode">
		<xsl:variable name="supplierCompanyID">
			<xsl:value-of select="//ns2:Invoice/cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID"/>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="contains($supplierCompanyID,'SI')">9949</xsl:when>
			<xsl:otherwise>9934</xsl:otherwise>
		</xsl:choose>
		
	</xsl:variable>
	
		

	
	<xsl:variable name="findOIB">
		<xsl:choose>
			<xsl:when test="string-length(normalize-space(//cac:AccountingSupplierParty//cac:Party/cac:PartyLegalEntity/cbc:CompanyID)) = 11 ">
				<xsl:value-of select="//cac:AccountingSupplierParty//cac:Party/cac:PartyLegalEntity/cbc:CompanyID"/>
			</xsl:when>
			<xsl:when test="string-length(normalize-space(//ns0:Header/ns0:SupplierID)) = 11 ">
				<xsl:value-of select="//ns0:Header/ns0:SupplierID"/>
			</xsl:when>
			<xsl:when test="string-length(normalize-space(//ns3:Header/ns3:SupplierID)) = 11 ">
				<xsl:value-of select="//ns3:Header/ns3:SupplierID"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="//cac:AccountringSupplierParty//cac:Party/cac:PartyLegalEntity/cbc:CompanyID"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<xsl:variable name="isOutsideVAT">
		<xsl:choose>
			<xsl:when test="//cac:TaxCategory[normalize-space(cbc:ID/text()) != 'O']">false</xsl:when>
			<xsl:otherwise>true</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<xsl:template match="/">
		<xsl:choose>
			<xsl:when test="/ns2:CreditNote">
				<xsl:copy-of select="/ns2:CreditNote"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="//i:Invoice"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	
	<xsl:template match="i:Invoice">
		<CreditNote xmlns="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:n0="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2" xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" xmlns:hrextac="urn:mfin.gov.hr:schema:xsd:HRExtensionAggregateComponents-1" xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2">

			<xsl:call-template name="HRTaxSubtotalExtension" />

			<cbc:CustomizationID>
				<xsl:value-of select="'urn:cen.eu:en16931:2017#compliant#urn:mfin.gov.hr:cius-2025:1.0#conformant#urn:mfin.gov.hr:ext-2025:1.0'"/>
			</cbc:CustomizationID>
			<xsl:apply-templates select="cbc:ProfileID"/>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
			<xsl:apply-templates select="cbc:IssueDate"/>
			<xsl:apply-templates select="cbc:IssueTime" />
			<!-- <xsl:apply-templates select="cac:PaymentMeans/cbc:PaymentDueDate" /> -->
			<xsl:apply-templates select="cbc:TaxPointDate"/>
			<xsl:apply-templates select="cbc:InvoiceTypeCode"/>
			<!--<xsl:apply-templates select="cbc:IssueTime"/>-->
<!--			<xsl:apply-templates select="cac:AccountingSupplierParty/cac:AccountingContact/cbc:Note" mode="OznakaOperatera"/>
			<xsl:apply-templates select="cac:AccountingSupplierParty/cac:AccountingContact/cbc:Name" mode="OdgovornaOsoba"/>-->
			<xsl:apply-templates select="cbc:Note"/>
			<xsl:apply-templates select="cbc:DocumentCurrencyCode"/>
			<xsl:apply-templates select="cbc:TaxCurrencyCode"/>
			<xsl:apply-templates select="cbc:AccountingCost"/>
			<xsl:apply-templates select="cbc:BuyerReference"/>
			<xsl:apply-templates select="cac:InvoicePeriod"/>
			<xsl:apply-templates select="cac:OrderReference"/>
			<xsl:apply-templates select="cac:BillingReference"/>
			<xsl:apply-templates select="cac:DespatchDocumentReference"/>
			<xsl:apply-templates select="cac:ReceiptDocumentReference"/>
			<xsl:apply-templates select="cac:ContractDocumentReference"/>
			<xsl:apply-templates select="cac:AdditionalDocumentReference" mode="ID"/>
			<xsl:apply-templates select="cac:ProjectReference"/>
			<xsl:apply-templates select="//ns3:AttachedDocumentEnvelope/ns1:AttachedDocument" mode="Attachment"/>
			<xsl:apply-templates select="//ns0:AttachedDocumentEnvelope/ns1:AttachedDocument" mode="Attachment"/>
			<xsl:apply-templates select="cac:OriginatorDocumentReference"/>
			<xsl:apply-templates select="cac:AccountingSupplierParty"/>
			<xsl:apply-templates select="cac:AccountingCustomerParty"/>
			<xsl:apply-templates select="cac:PayeeParty"/>
			<xsl:apply-templates select="cac:TaxRepresentativeParty"/>
			<xsl:apply-templates select="cac:Delivery"/>
			<xsl:apply-templates select="cac:PaymentMeans"/>
			<!-- <xsl:apply-templates select="cac:PaymentTerms" /> -->
			<xsl:call-template name="PaymentTerms"/>
			<xsl:apply-templates select="cac:AllowanceCharge"/>
			<xsl:apply-templates select="cac:TaxTotal"/>
			<xsl:apply-templates select="cac:LegalMonetaryTotal"/>
			<xsl:apply-templates select="cac:InvoiceLine"/>
		</CreditNote>
		<xsl:comment>Generated by OutgoingDocumentsDataCreditNoteToEuropeanNormCreditNote V 2024-10-14</xsl:comment>
		
	</xsl:template>
	
	<xsl:template name="HRTaxSubtotalExtension">
		<xsl:if test="exists(cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory[normalize-space(cbc:ID) = 'E' or normalize-space(cbc:ID) = 'O'])">
			<xsl:variable name="HRTaxSubTotal">
				<xsl:apply-templates select="cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory[normalize-space(cbc:ID) = 'E' or normalize-space(cbc:ID) = 'O']" mode="HRTaxSubtotal" />
			
			
			</xsl:variable>
			
			<xsl:variable name="HRTaxSubTotalStavke">
				<xsl:for-each-group select="//ns2:Invoice/cac:InvoiceLine//cac:TaxCategory[normalize-space(cbc:ID) = 'E' or normalize-space(cbc:ID) = 'O'] | //ns2:Invoice/cac:AllowanceCharge//cac:TaxCategory[normalize-space(cbc:ID) = 'E' or normalize-space(cbc:ID) = 'O']" group-by="concat(cbc:Name, '+', cbc:TaxExemptionReason)" >
					<hrextac:HRTaxSubtotal>
						<cbc:TaxableAmount>
							<xsl:attribute name="currencyID"><xsl:value-of select="//ns2:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>

							<xsl:value-of select="format-number(sum(//ns2:Invoice/cac:InvoiceLine[concat(./cac:TaxTotal//cac:TaxCategory/cbc:Name, '+', ./cac:TaxTotal//cac:TaxCategory/cbc:TaxExemptionReason) = current-grouping-key()]/cbc:LineExtensionAmount) + sum(//ns2:Invoice/cac:AllowanceCharge[cbc:ChargeIndicator='true'][concat(.//cac:TaxCategory/cbc:Name, '+', .//cac:TaxCategory/cbc:TaxExemptionReason) = current-grouping-key()]/cbc:Amount) - sum(//ns2:Invoice/cac:AllowanceCharge[cbc:ChargeIndicator='false'][concat(.//cac:TaxCategory/cbc:Name, '+', .//cac:TaxCategory/cbc:TaxExemptionReason) = current-grouping-key()]/cbc:Amount), '#########0.00;-#########0.00')" />
						</cbc:TaxableAmount>
						<cbc:TaxAmount>
							<xsl:attribute name="currencyID"><xsl:value-of select="//ns2:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
							<xsl:value-of select="'0.00'" />
						
						</cbc:TaxAmount>
						<hrextac:HRTaxCategory>
							<!--<cbc:ID><xsl:value-of select="current-group()[1]/cbc:ID" />	</cbc:ID>--> 
							<xsl:call-template name="HRTaxCategory" >
								<xsl:with-param name="taxCategory" select="current-group()[1]" />
							</xsl:call-template>
							<cbc:Percent>0</cbc:Percent>
							<cbc:TaxExemptionReason>
								<xsl:value-of select="current-group()[1]/cbc:TaxExemptionReason" />
							</cbc:TaxExemptionReason>
							<!--<hrextac:HRTaxScheme>
								<cbc:ID>VAT</cbc:ID>
							</hrextac:HRTaxScheme>-->
										
							<xsl:call-template name="HRTaxScheme" >
								<xsl:with-param name="taxCategory" select="current-group()[1]" />
							</xsl:call-template> <!--<cbc:ID>LOC</cbc:ID>-->

						</hrextac:HRTaxCategory>




					</hrextac:HRTaxSubtotal>				
				
				</xsl:for-each-group>
			
			
			</xsl:variable>
			
			<ext:UBLExtensions>
				<ext:UBLExtension>
					<ext:ExtensionContent>
						<hrextac:HRFISK20Data>
							<hrextac:HRTaxTotal>
								<cbc:HRTaxAmount>
									<xsl:attribute name="currencyID"><xsl:value-of select="//ns2:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
									<xsl:value-of select="'0.00'" />
								
								</cbc:HRTaxAmount>
								<xsl:choose>
									<xsl:when test="exists(//ns2:Invoice/cac:InvoiceLine//cbc:TaxExemptionReason) or exists(//ns2:Invoice/cac:AllowanceCharge//cbc:TaxExemptionReason)">
										<xsl:comment>Stavke</xsl:comment>
										<xsl:copy-of select="$HRTaxSubTotalStavke" />
										<xsl:if test="exists($HRTaxSubTotalStavke//hrextac:HRTaxCategory[normalize-space(cbc:ID)='O'])">
											<hrextac:HRLegalMonetaryTotal>
												<xsl:variable name="oosVatAmount">
													<xsl:value-of select="xs:decimal( sum($HRTaxSubTotalStavke/hrextac:HRTaxSubtotal[hrextac:HRTaxCategory/normalize-space(cbc:ID)='O']/xs:decimal(cbc:TaxableAmount)))" />
												</xsl:variable>
												<cbc:TaxExclusiveAmount>
													<xsl:attribute name="currencyID"><xsl:value-of select="//ns2:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
													<xsl:value-of select="format-number(//ns2:Invoice/cac:LegalMonetaryTotal/xs:decimal(cbc:TaxExclusiveAmount) - xs:decimal($oosVatAmount), '#########0.00;-#########0.00')" />
												</cbc:TaxExclusiveAmount>
												<hrextac:OutOfScopeOfVATAmount currencyID="EUR">
													<xsl:attribute name="currencyID"><xsl:value-of select="//ns2:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
													<xsl:value-of select="format-number(/$oosVatAmount, '#########0.00;-#########0.00')" />
												</hrextac:OutOfScopeOfVATAmount>
											</hrextac:HRLegalMonetaryTotal>
										</xsl:if>
									</xsl:when>
									<xsl:when test="normalize-space($HRTaxSubTotal) != ''">
										<xsl:comment>TaxSubtotal</xsl:comment>
										<xsl:copy-of select="$HRTaxSubTotal" />									
										<xsl:if test="exists($HRTaxSubTotal//hrextac:HRTaxCategory[normalize-space(cbc:ID)='O'])">
											<hrextac:HRLegalMonetaryTotal>
												<xsl:variable name="oosVatAmount">
													<xsl:value-of select="xs:decimal( sum($HRTaxSubTotal/hrextac:HRTaxSubtotal[hrextac:HRTaxCategory/normalize-space(cbc:ID)='O']/xs:decimal(cbc:TaxableAmount)))" />
												</xsl:variable>
												<cbc:TaxExclusiveAmount>
													<xsl:attribute name="currencyID"><xsl:value-of select="//ns2:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
													<xsl:value-of select="format-number(//ns2:Invoice/cac:LegalMonetaryTotal/xs:decimal(cbc:TaxExclusiveAmount) - xs:decimal($oosVatAmount), '#########0.00;-#########0.00')" />
												</cbc:TaxExclusiveAmount>
												<hrextac:OutOfScopeOfVATAmount currencyID="EUR">
													<xsl:attribute name="currencyID"><xsl:value-of select="//ns2:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
													<xsl:value-of select="format-number(/$oosVatAmount, '#########0.00;-#########0.00')" />
												</hrextac:OutOfScopeOfVATAmount>
											</hrextac:HRLegalMonetaryTotal>
										</xsl:if>
									</xsl:when>
								</xsl:choose>
							</hrextac:HRTaxTotal>

						</hrextac:HRFISK20Data>
					</ext:ExtensionContent>
				</ext:UBLExtension>
			</ext:UBLExtensions>
		
		</xsl:if>
		
	
	</xsl:template>
	
	<xsl:template match="cac:TaxCategory" mode="HRTaxSubtotal">
		<hrextac:HRTaxSubtotal>
			<xsl:apply-templates select="../cbc:TaxableAmount" mode="HRTaxSubtotal" /> <!--<cbc:TaxableAmount currencyID="EUR">0.08</cbc:TaxableAmount>-->
			<xsl:apply-templates select="../cbc:TaxAmount" mode="HRTaxSubtotal" /> <!--<cbc:TaxAmount currencyID="EUR">0.00</cbc:TaxAmount>-->
			<hrextac:HRTaxCategory>
				<xsl:call-template name="HRTaxCategory" >
					<xsl:with-param name="taxCategory" select="." />
				</xsl:call-template> <!--<cbc:ID>O</cbc:ID>-->
				 <!--<cbc:Name>HR:PP</cbc:Name-->
				<xsl:apply-templates select="cbc:Percent" /> <!--<cbc:Percent>0</cbc:Percent>-->
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> <!--<cbc:TaxExemptionReason>Artikl ne ulazi u poreznu osnovicu i ne podliježe PDV-u - prolazna stavka čl. 33 – drugi razlozi</cbc:TaxExemptionReason>-->
				<xsl:call-template name="HRTaxScheme" >
					<xsl:with-param name="taxCategory" select="." />
				</xsl:call-template> <!--<cbc:ID>LOC</cbc:ID>-->
			</hrextac:HRTaxCategory>
		</hrextac:HRTaxSubtotal>
	</xsl:template>

	<xsl:template name="HRTaxScheme">
		<xsl:param name="taxCategory" />
		<hrextac:HRTaxScheme>
			<cbc:ID>
				<xsl:choose>
					<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'NEOPOREZIVO'">VAT</xsl:when>
					<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'HR:POVNAK'">OTH</xsl:when>
					<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'HR:PP'">LOC</xsl:when>
					<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'HR:PPMV'">CAR</xsl:when>
					<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O'">VAT</xsl:when>
					<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'OSLOBOĐENO_POREZA'">VAT</xsl:when>
					<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'HR:POVNAK'">OTH</xsl:when>
					<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'HR:PP'">LOC</xsl:when>
					<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'HR:PPMV'">CAR</xsl:when>
					<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E'">VAT</xsl:when>
					<xsl:otherwise>VAT</xsl:otherwise>
				</xsl:choose>
			
			</cbc:ID>
		</hrextac:HRTaxScheme>
	
	</xsl:template>
	
	<!-- Kod pretvaranja u HRTaxSubtotal -->
	<xsl:template name="HRTaxCategory">
		<xsl:param name="taxCategory" />
		<xsl:choose>
				<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'NEOPOREZIVO'">
					<cbc:ID>O</cbc:ID>
					<cbc:Name>HR:O</cbc:Name>
				</xsl:when>
				<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'HR:POVNAK'">
					<cbc:ID>O</cbc:ID>
					<cbc:Name>HR:POVNAK</cbc:Name>
				</xsl:when>
				<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'HR:PP'">
					<cbc:ID>O</cbc:ID>
					<cbc:Name>HR:PP</cbc:Name>
				</xsl:when>
				<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'HR:PPMV'">
					<cbc:ID>O</cbc:ID>
					<cbc:Name>HR:PPMV</cbc:Name>
				</xsl:when>
				<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O'">
					<cbc:ID>O</cbc:ID>
					<cbc:Name>HR:O</cbc:Name>
				</xsl:when>
				<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'OSLOBOĐENO_POREZA'">
					<cbc:ID>E</cbc:ID>
					<cbc:Name>HR:E</cbc:Name>
				</xsl:when>
				<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'HR:POVNAK'">
					<cbc:ID>O</cbc:ID>
					<cbc:Name>HR:POVNAK</cbc:Name>
				</xsl:when>
				<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'HR:PP'">
					<cbc:ID>O</cbc:ID>
					<cbc:Name>HR:PP</cbc:Name>
				</xsl:when>
				<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'HR:PPMV'">
					<cbc:ID>O</cbc:ID>
					<cbc:Name>HR:PPMV</cbc:Name>
				</xsl:when>
				<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E'">
					<cbc:ID>E</cbc:ID>
					<cbc:Name>HR:E</cbc:Name>
				</xsl:when>
				<xsl:otherwise>
						<cbc:ID>E</cbc:ID>
						<cbc:Name>HR:E</cbc:Name>
				</xsl:otherwise>
			</xsl:choose>
			
	</xsl:template>

	<!-- Kod pretvaranja u stavke u računu - mora biti sukladno EN pravilima -->

	<xsl:template name="ENTaxCategory" >
		<xsl:param name="taxCategory" />

		<xsl:choose>
			<xsl:when test="$isOutsideVAT = 'true'">
				<cbc:ID>O</cbc:ID>
				<xsl:apply-templates select="cbc:Name" /> <!--<cbc:Name>HR:POVNAK</cbc:Name>-->
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'NEOPOREZIVO'">
				<cbc:ID>E</cbc:ID>
				<cbc:Name>HR:O</cbc:Name>
				<cbc:Percent>0</cbc:Percent>
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'HR:POVNAK'">
				<cbc:ID>E</cbc:ID>
				<cbc:Name>HR:POVNAK</cbc:Name>
				<cbc:Percent>0</cbc:Percent>
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'HR:PP'">
				<cbc:ID>E</cbc:ID>
				<cbc:Name>HR:PP</cbc:Name>
				<cbc:Percent>0</cbc:Percent>
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O' and normalize-space($taxCategory/cbc:Name) = 'HR:PPMV'">
				<cbc:ID>O</cbc:ID>
				<cbc:Name>HR:PPMV</cbc:Name>
				<cbc:Percent>0</cbc:Percent>
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'O'">
				<cbc:ID>E</cbc:ID>
				<cbc:Name>HR:O</cbc:Name>
				<cbc:Percent>0</cbc:Percent>
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'OSLOBOĐENO_POREZA'">
				<cbc:ID>E</cbc:ID>
				<cbc:Name>HR:E</cbc:Name>
				<cbc:Percent>0</cbc:Percent>
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'HR:POVNAK'">
				<cbc:ID>E</cbc:ID>
				<cbc:Name>HR:POVNAK</cbc:Name>
				<cbc:Percent>0</cbc:Percent>
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'HR:PP'">
				<cbc:ID>E</cbc:ID>
				<cbc:Name>HR:PP</cbc:Name>
				<cbc:Percent>0</cbc:Percent>
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E' and normalize-space($taxCategory/cbc:Name) = 'HR:PPMV'">
				<cbc:ID>E</cbc:ID>
				<cbc:Name>HR:PPMV</cbc:Name>
				<cbc:Percent>0</cbc:Percent>
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:when test="normalize-space($taxCategory/cbc:ID) = 'E'">
				<cbc:ID>E</cbc:ID>
				<cbc:Name>HR:E</cbc:Name>
				<cbc:Percent>0</cbc:Percent>
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:when test="normalize-space(cbc:ID) = 'B'">
				<cbc:ID>AE</cbc:ID>
				<xsl:apply-templates select="cbc:Percent" />
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="cbc:ID" mode="TaxCategoryID" />
				<xsl:apply-templates select="cbc:Percent" />
				<xsl:apply-templates select="cbc:TaxExemptionReason" /> 
			</xsl:otherwise>
		</xsl:choose>

		<cac:TaxScheme>
			<cbc:ID>VAT</cbc:ID>
		</cac:TaxScheme>
	
	
	</xsl:template>


	<xsl:template match="cbc:TaxAmount" mode="HRTaxSubtotal">
		<cbc:TaxAmount>
			<xsl:attribute name="currencyID"><xsl:value-of select="@currencyID" /></xsl:attribute>
			<xsl:value-of select="." />
		</cbc:TaxAmount>
	</xsl:template>
	
		
	<xsl:template match="cbc:TaxableAmount" mode="HRTaxSubtotal">
		<cbc:TaxableAmount>
			<xsl:attribute name="currencyID"><xsl:value-of select="@currencyID" /></xsl:attribute>
			<xsl:value-of select="." />
		</cbc:TaxableAmount>
	</xsl:template>
	
	<xsl:template name="ProfileID">
		<cbc:ProfileID>
			<xsl:choose>
				<xsl:when test="normalize-space(cbc:InvoiceTypeCode) = '384'">P10</xsl:when>
				<xsl:when test="normalize-space(cbc:InvoiceTypeCode) = '386'">P4</xsl:when>
				<xsl:when test="normalize-space(cac:ContractDocumentReference/cbc:ID) != '' and normalize-space(cac:OrderReference/cbc:ID) != ''">P1</xsl:when>
				<xsl:when test="normalize-space(cac:OrderReference/cbc:ID) != ''">P3</xsl:when>
				<xsl:when test="normalize-space(cac:DespatchDocumentReference/cbc:ID) != '' and normalize-space(cac:ReceiptDocumentReference/cbc:ID) != ''">P8</xsl:when>
				<xsl:when test="normalize-space(cac:DespatchDocumentReference/cbc:ID) != ''">P7</xsl:when>
				<xsl:when test="normalize-space(cac:ContractDocumentReference/cbc:ID) != ''">P2</xsl:when>
				<xsl:when test="number(cac:LegalMonetaryTotal/cbc:PayableAmount) &lt; 0">P9</xsl:when>
				<xsl:when test="number(cac:LegalMonetaryTotal/cbc:PayableAmount) = 0">P5</xsl:when>
				<xsl:otherwise>P3</xsl:otherwise>
			</xsl:choose>
		
		</cbc:ProfileID>
	
	</xsl:template>
		
	<xsl:template match="cac:InvoiceLine">
		<cac:CreditNoteLine>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
			<xsl:apply-templates select="cbc:Note"/>
			<xsl:apply-templates select="cbc:InvoicedQuantity"/>
			<xsl:apply-templates select="cbc:LineExtensionAmount"/>
			<xsl:apply-templates select="cbc:AccountingCost"/>
			<xsl:apply-templates select="cac:InvoicePeriod" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:OrderLineReference" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:DespatchLineReference" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:ReceiptLineReference" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:DocumentReference" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:AllowanceCharge" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:Item" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:Price" mode="InvoiceLine"/>
		</cac:CreditNoteLine>
	</xsl:template>
	
	
	<xsl:template match="cac:Price" mode="InvoiceLine">
		<cac:Price>
			<xsl:apply-templates select="cbc:PriceAmount"/>
			<xsl:apply-templates select="cbc:BaseQuantity"/>
			<xsl:apply-templates select="cac:AllowanceCharge" mode="Price"/>
		</cac:Price>
	</xsl:template>
	<xsl:template match="cac:AllowanceCharge" mode="Price">
		<cac:AllowanceCharge>
			<cbc:ChargeIndicator>false</cbc:ChargeIndicator>
			<xsl:apply-templates select="cbc:Amount"/>
			<xsl:apply-templates select="cbc:BaseAmount"/>
		</cac:AllowanceCharge>
	</xsl:template>
	<xsl:template match="cbc:BaseQuantity">
		<cbc:BaseQuantity>
			<xsl:attribute name="unitCode" namespace=""><xsl:value-of select="@unitCode"/></xsl:attribute>
			<xsl:value-of select="format-number(number(.), '##########0.######')"/>
		</cbc:BaseQuantity>
	</xsl:template>
	
	
	<xsl:template match="cac:Item" mode="InvoiceLine">
		<cac:Item>
			<xsl:apply-templates select="cbc:Description"/>
			<xsl:apply-templates select="cbc:Name"/>
			<xsl:apply-templates select="cac:BuyersItemIdentification"/>
			<xsl:apply-templates select="cac:SellersItemIdentification"/>
			<xsl:apply-templates select="cac:ManufacturersItemIdentification"/>
			<!-- <xsl:apply-templates select="cac:StandardItemIdentification" mode="StandardItemIdentification"/> -->
			<xsl:apply-templates select="cac:AdditionalItemIdentification[cbc:ID/@schemeID]" mode="StandardItemIdentification"/>
			<xsl:if test="normalize-space(cac:AdditionalItemIdentification[not(cbc:ID/@schemeID)]/cbc:ID) != ''">
				<xsl:apply-templates select="cac:AdditionalItemIdentification[not(cbc:ID/@schemeID)]"/>
			</xsl:if>
			<xsl:apply-templates select="cac:OriginCountry"/>
			<xsl:apply-templates select="cac:CommodityClassification"/>
			<xsl:apply-templates select="../cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:AdditionalItemProperty"/>
		</cac:Item>
	</xsl:template>
	
	
	<xsl:template match="cac:ManufacturersItemIdentification">
		<cac:ManufacturersItemIdentification>
			<xsl:apply-templates select="cbc:ID"/>
		</cac:ManufacturersItemIdentification>
	</xsl:template>
	<xsl:template match="cac:AdditionalItemProperty">
		<xsl:if test="cbc:Name and cbc:Value">
			<cac:AdditionalItemProperty>
				<xsl:apply-templates select="cbc:Name"/>
				<xsl:apply-templates select="cbc:Value"/>
			</cac:AdditionalItemProperty>
		</xsl:if>
	</xsl:template>
	<xsl:template match="cac:AdditionalItemIdentification[not(cbc:ID/@schemeID)]">
		<cac:AdditionalItemIdentification>
			<xsl:apply-templates select="cbc:ID"/>
		</cac:AdditionalItemIdentification>
	</xsl:template>
	
<!--	<xsl:template match="cac:TaxCategory" mode="InvoiceLine">
		<cac:ClassifiedTaxCategory>
			<xsl:choose>
				<xsl:when test="$isOutsideVAT = 'true'">
					<cbc:ID>O</cbc:ID>
				</xsl:when>
				<xsl:when test="normalize-space(cbc:ID) = 'B'">
					<cbc:ID>AE</cbc:ID>
					<xsl:apply-templates select="cbc:Percent" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="cbc:ID" mode="TaxCategoryID"/>
					<xsl:apply-templates select="cbc:Percent"/>
				</xsl:otherwise>
			</xsl:choose>
			<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
		</cac:ClassifiedTaxCategory>
	</xsl:template>
-->	
	<xsl:template match="cac:TaxCategory" mode="InvoiceLine">
		<cac:ClassifiedTaxCategory>
			<xsl:call-template name="ENTaxCategory" >
				<xsl:with-param name="taxCategory" select="." />
			
			</xsl:call-template>
		</cac:ClassifiedTaxCategory>
	
	</xsl:template>

	
	<xsl:template match="cac:CommodityClassification">
		<cac:CommodityClassification>
			<xsl:apply-templates select="cbc:ItemClassificationCode"/>
		</cac:CommodityClassification>
	</xsl:template>
	
	<xsl:template match="cbc:ItemClassificationCode">
		<cbc:ItemClassificationCode>
			<xsl:attribute name="listID"><xsl:value-of select="@listID"/></xsl:attribute>
			<xsl:attribute name="listVersionID"><xsl:value-of select="@listVersionID"/></xsl:attribute>
			<xsl:value-of select="."/>
		</cbc:ItemClassificationCode>
	</xsl:template>
	
	
	<xsl:template match="cac:OriginCountry">
		<cac:OriginCountry>
			<xsl:apply-templates select="cbc:IdentificationCode"/>
		</cac:OriginCountry>
	</xsl:template>
	
	
	<xsl:template match="cac:StandardItemIdentification | cac:AdditionalItemIdentification[cbc:ID/@schemeID] " mode="StandardItemIdentification">
		<xsl:if test="cbc:ID and (normalize-space(cbc:ID) != '')">
			<cac:StandardItemIdentification>
				<xsl:apply-templates select="cbc:ID" mode="StandardItemID"/>
			</cac:StandardItemIdentification>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cbc:ID" mode="StandardItemID">
		<cbc:ID>
			<xsl:choose>
				<xsl:when test="@schemeID = 'GTIN'">
					<xsl:attribute name="schemeID" namespace="">0160</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="schemeID" namespace="">0017</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:value-of select="."/>
		</cbc:ID>
	</xsl:template>
	<xsl:template match="cac:SellersItemIdentification">
		<cac:SellersItemIdentification>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
		</cac:SellersItemIdentification>
	</xsl:template>
	<xsl:template match="cac:BuyersItemIdentification">
		<cac:BuyersItemIdentification>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
		</cac:BuyersItemIdentification>
	</xsl:template>
	
	
	<xsl:template match="cac:AllowanceCharge" mode="InvoiceLine">
		<cac:AllowanceCharge>
			<xsl:apply-templates select="cbc:ChargeIndicator"/>
			<xsl:apply-templates select="cbc:AllowanceChargeReasonCode"/>
			<!-- <xsl:apply-templates select="cbc:AllowanceChargeReason" /> -->
			<xsl:call-template name="AllowanceChargeReason"/>
			<xsl:apply-templates select="cbc:MultiplierFactorNumeric"/>
			<xsl:apply-templates select="cbc:Amount"/>
			<xsl:apply-templates select="cbc:BaseAmount"/>
		</cac:AllowanceCharge>
	</xsl:template>
	
	<xsl:template match="cac:DocumentReference" mode="InvoiceLine">
		<xsl:if test="cbc:ID and (normalize-space(cbc:ID) != '')">
			<cac:DocumentReference>
				<xsl:apply-templates select="cbc:ID" mode="SchemeID"/>
			</cac:DocumentReference>
		</xsl:if>
	</xsl:template>
	
<!--	<xsl:template match="cac:DespatchLineReference" mode="InvoiceLine">
		--><!-- TODO: vidjeti da li staviti neko drugo mapiranje - Vindija navodi Order/cbc:ID --><!--
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')">
			<cac:DocumentReference>
				<cbc:ID schemeID="AAK">
					<xsl:value-of select="cbc:LineID"/>
				</cbc:ID>
			</cac:DocumentReference>
		</xsl:if>
	</xsl:template>
	<xsl:template match="cac:OrderLineReference" mode="InvoiceLine">
		--><!-- TODO: vidjeti da li staviti neko drugo mapiranje - Vindija navodi Order/cbc:ID --><!--
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')">
			<cac:OrderLineReference>
				<xsl:apply-templates select="cbc:LineID"/>
			</cac:OrderLineReference>
		</xsl:if>
	</xsl:template>
-->

	<xsl:template match="cac:DespatchLineReference" mode="InvoiceLine">
	
	<!--TODO: vidjeti da li staviti neko drugo mapiranje -->
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')" >
			<cac:DespatchLineReference>
				<xsl:apply-templates select="cbc:LineID" />
				<xsl:apply-templates select="cac:DocumentReference" mode="InvoiceLine"/>
			</cac:DespatchLineReference>
		</xsl:if>
	</xsl:template>
	
	
	<xsl:template match="cac:OrderLineReference" mode="InvoiceLine">
	
	<!--TODO: vidjeti da li staviti neko drugo mapiranje -->
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')" >
			<cac:OrderLineReference>
				<xsl:apply-templates select="cbc:LineID" />
				<xsl:apply-templates select="cac:OrderReference" mode="InvoiceLine"/>
			</cac:OrderLineReference>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:OrderReference" mode="InvoiceLine">
		<cac:OrderReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
			
		</cac:OrderReference>
	
	</xsl:template>


	<xsl:template match="cac:ReceiptLineReference" mode="InvoiceLine">
	
	<!--TODO: vidjeti da li staviti neko drugo mapiranje -->
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')" >
			<cac:ReceiptLineReference>
				<xsl:apply-templates select="cbc:LineID" />
				<xsl:apply-templates select="cac:DocumentReference" mode="InvoiceLine"/>
			</cac:ReceiptLineReference>
		</xsl:if>
	</xsl:template>

	<xsl:template match="cac:InvoicePeriod" mode="InvoiceLine">
		<cac:InvoicePeriod>
			<xsl:apply-templates select="cbc:StartDate"/>
			<xsl:apply-templates select="cbc:EndDate"/>
		</cac:InvoicePeriod>
	</xsl:template>
	<xsl:template match="cbc:InvoicedQuantity">
		<cbc:CreditedQuantity>
			<xsl:attribute name="unitCode" namespace=""><xsl:value-of select="@unitCode"/></xsl:attribute>
			<xsl:value-of select="format-number(number(.), '##########0.######')"/>
		</cbc:CreditedQuantity>
	</xsl:template>
	<xsl:template match="cac:LegalMonetaryTotal">
		<cac:LegalMonetaryTotal>
			<xsl:apply-templates select="cbc:LineExtensionAmount"/>
			<xsl:apply-templates select="cbc:TaxExclusiveAmount"/>
			<xsl:apply-templates select="cbc:TaxInclusiveAmount"/>
			<xsl:apply-templates select="cbc:AllowanceTotalAmount"/>
			<xsl:apply-templates select="cbc:ChargeTotalAmount"/>
			<xsl:apply-templates select="cbc:PrepaidAmount"/>
			<xsl:apply-templates select="cbc:PayableRoundingAmount"/>
			<xsl:apply-templates select="cbc:PayableAmount"/>
		</cac:LegalMonetaryTotal>
	</xsl:template>
	<xsl:template match="cac:TaxTotal">
		<cac:TaxTotal>
			<xsl:apply-templates select="cbc:TaxAmount"/>
			<!--<xsl:apply-templates select="cac:TaxSubtotal" mode="TaxTotal"/>-->
			<xsl:apply-templates select="cac:TaxSubtotal[normalize-space(cac:TaxCategory/cbc:ID) != 'E' and normalize-space(cac:TaxCategory/cbc:ID) != 'O']" mode="TaxTotal" />
			<xsl:call-template name="TaxSubtotalExempt" />
			
		</cac:TaxTotal>
	</xsl:template>
	
	
	<xsl:template name="TaxSubtotalExempt" >
		<xsl:if test="exists(./cac:TaxSubtotal[normalize-space(cac:TaxCategory/cbc:ID) = 'E' or normalize-space(cac:TaxCategory/cbc:ID) = 'O'])" >
			<cac:TaxSubtotal>
				<cbc:TaxableAmount>
					<xsl:attribute name="currencyID"><xsl:value-of select="//ns2:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
					<xsl:value-of select="format-number(sum(cac:TaxSubtotal[normalize-space(cac:TaxCategory/cbc:ID) = 'E' or normalize-space(cac:TaxCategory/cbc:ID) = 'O']/number(cbc:TaxableAmount)), '#########0.00;-#########0.00')" />
				
				</cbc:TaxableAmount>
				<cbc:TaxAmount>
					<xsl:attribute name="currencyID"><xsl:value-of select="//ns2:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
					<xsl:value-of select="'0.00'" />
				
				</cbc:TaxAmount>
				<cac:TaxCategory>
					<cbc:ID>E</cbc:ID> 
					<cbc:Percent>0</cbc:Percent>
					<cbc:TaxExemptionReason>
						<xsl:for-each  select="distinct-values(cac:TaxSubtotal[normalize-space(cac:TaxCategory/cbc:ID) = 'E' or normalize-space(cac:TaxCategory/cbc:ID) = 'O']/cac:TaxCategory/cbc:TaxExemptionReason)" >
							<xsl:value-of select="concat(., ' ')" />
						</xsl:for-each>
					</cbc:TaxExemptionReason>
					<cac:TaxScheme>
						<cbc:ID>VAT</cbc:ID>
					</cac:TaxScheme>
				
				</cac:TaxCategory>
			
			
		
			</cac:TaxSubtotal>
		
		</xsl:if>
	
	
	</xsl:template>
	
	
	
	<xsl:template match="cac:TaxSubtotal" mode="TaxTotal">
		<cac:TaxSubtotal>
			<xsl:apply-templates select="cbc:TaxableAmount"/>
			<xsl:apply-templates select="cbc:TaxAmount"/>
			<xsl:apply-templates select="cac:TaxCategory" mode="TaxTotal"/>
		</cac:TaxSubtotal>
	</xsl:template>
	
	<xsl:template match="cac:TaxCategory" mode="TaxTotal">
		<cac:TaxCategory>
			<xsl:choose>
				<xsl:when test="$isOutsideVAT = 'true'">
					<cbc:ID>O</cbc:ID>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="cbc:ID" mode="TaxCategoryID"/>
					<xsl:apply-templates select="cbc:Percent"/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:apply-templates select="cbc:TaxExemptionReasonCode"/>
			<xsl:apply-templates select="cbc:TaxExemptionReason"/>
			<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
		</cac:TaxCategory>
	</xsl:template>
	
	<xsl:template match="cbc:TaxExemptionReason">
		<xsl:if test="normalize-space(.) != ''">
			<cbc:TaxExemptionReason>
				<xsl:value-of select="."/>
			</cbc:TaxExemptionReason>
		</xsl:if>
	</xsl:template>
	<xsl:template match="cbc:TaxExemptionReasonCode">
		<xsl:if test="normalize-space(../cbc:TaxExemptionReason) = ''">
			<cbc:TaxExemptionReasonCode>
				<xsl:value-of select="."/>
			</cbc:TaxExemptionReasonCode>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:AllowanceCharge">
		<cac:AllowanceCharge>
			<xsl:apply-templates select="cbc:ChargeIndicator"/>
			<xsl:apply-templates select="cbc:AllowanceChargeReasonCode"/>
			<!-- <xsl:apply-templates select="cbc:AllowanceChargeReason" /> -->
			<xsl:call-template name="AllowanceChargeReason"/>
			<xsl:apply-templates select="cbc:MultiplierFactorNumeric"/>
			<xsl:apply-templates select="cbc:Amount"/>
			<xsl:apply-templates select="cbc:BaseAmount"/>
			<xsl:choose>
				<xsl:when test="cac:TaxCategory">
					<xsl:apply-templates select="cac:TaxCategory" mode="AllowanceCharge"/>
				</xsl:when>
				<xsl:when test="cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory">
					<xsl:apply-templates select="cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory" mode="AllowanceCharge"/>
				</xsl:when>
			</xsl:choose>
		</cac:AllowanceCharge>
	</xsl:template>
	
	
	<xsl:template name="AllowanceChargeReason">
		<xsl:choose>
			<xsl:when test="not(cbc:AllowanceChargeReason) or (normalize-space(cbc:AllowanceChargeReason) = '')">
				<xsl:choose>
					<xsl:when test="cbc:ChargeIndicator = 'false'">
						<cbc:AllowanceChargeReason>POPUST</cbc:AllowanceChargeReason>
					</xsl:when>
					<xsl:otherwise>
						<cbc:AllowanceChargeReason>TROŠAK</cbc:AllowanceChargeReason>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="cbc:AllowanceChargeReason"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="cac:TaxCategory" mode="AllowanceCharge">
		<cac:TaxCategory>
			<xsl:call-template name="ENTaxCategory" >
				<xsl:with-param name="taxCategory" select="." />
			</xsl:call-template>

<!--			<xsl:choose>
				<xsl:when test="$isOutsideVAT = 'true'">
					<cbc:ID>O</cbc:ID>
				</xsl:when>
				<xsl:when test="normalize-space(cbc:ID) = 'B'">
					<cbc:ID>AE</cbc:ID>
					<xsl:apply-templates select="cbc:Percent" />
				</xsl:when>

				<xsl:otherwise>
					<xsl:apply-templates select="cbc:ID" mode="TaxCategoryID"/>
					<xsl:apply-templates select="cbc:Percent"/>
				</xsl:otherwise>
			</xsl:choose>
			<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
-->		</cac:TaxCategory>
	</xsl:template>
	
	<xsl:template match="cbc:ChargeIndicator">
		<xsl:variable name="chargeIndicatorValue">
			<xsl:value-of select="normalize-space(.)"/>
		</xsl:variable>
		<cbc:ChargeIndicator>
			<xsl:choose>
				<xsl:when test="$chargeIndicatorValue = 'true'">true</xsl:when>
				<xsl:when test="$chargeIndicatorValue = 'TRUE'">true</xsl:when>
				<xsl:when test="$chargeIndicatorValue = '1'">true</xsl:when>
				<xsl:when test="$chargeIndicatorValue = 'false'">false</xsl:when>
				<xsl:when test="$chargeIndicatorValue = 'FALSE'">false</xsl:when>
				<xsl:when test="$chargeIndicatorValue = '0'">false</xsl:when>
			</xsl:choose>
		</cbc:ChargeIndicator>
	</xsl:template>
	
	<xsl:template name="PaymentTerms">
		<xsl:variable name="paymentTermsContent">
			<xsl:choose>
				<xsl:when test="//i:Invoice/cbc:DueDate">
					<xsl:value-of select="concat('#Datum dospijeća plaćanja:', //i:Invoice/cbc:DueDate, '; ')"/>
				</xsl:when>
				<xsl:when test="//i:Invoice/cac:PaymentMeans/cbc:PaymentDueDate">
					<xsl:value-of select="concat('#Datum dospijeća plaćanja:', //i:Invoice/cac:PaymentMeans/cbc:PaymentDueDate, '; ')"/>
				</xsl:when>
			</xsl:choose>
			<xsl:if test="//i:Invoice/cac:PaymentTerms/cbc:Note">
				<xsl:value-of select="//i:Invoice/cac:PaymentTerms/cbc:Note"/>
			</xsl:if>
		</xsl:variable>
		<xsl:if test="normalize-space($paymentTermsContent) != ''">
			<cac:PaymentTerms>
				<cbc:Note>
					<xsl:value-of select="$paymentTermsContent"/>
				</cbc:Note>
			</cac:PaymentTerms>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:PaymentMeans">
		<cac:PaymentMeans>
			<xsl:apply-templates select="cbc:PaymentMeansCode"/>
			<xsl:apply-templates select="cbc:InstructionNote"/>
			<!--<xsl:apply-templates select="cbc:PaymentID"/>-->
			<xsl:choose>
				<xsl:when test="./cbc:InstructionID"><xsl:apply-templates select="cbc:InstructionID"/>
				</xsl:when>
				<xsl:otherwise><xsl:apply-templates select="cbc:PaymentID"/></xsl:otherwise>
			</xsl:choose>
			
			<xsl:apply-templates select="cac:CardAccount"/>
			<xsl:apply-templates select="cac:PayeeFinancialAccount" mode="PaymentMeans"/>
			<xsl:apply-templates select="cac:PaymentMandate"/>
		</cac:PaymentMeans>
	</xsl:template>
	<xsl:template match="cac:PaymentMandate">
		<cac:PaymentMandate>
			<xsl:apply-templates select="cbc:ID"/>
			<xsl:apply-templates select="cac:PayeeFinancialAccount" mode="PaymentMandate"/>
		</cac:PaymentMandate>
	</xsl:template>
	<xsl:template match="cac:PayeeFinancialAccount" mode="PaymentMandate">
		<cac:PayeeFinancialAccount>
			<xsl:apply-templates select="cbc:ID"/>
		</cac:PayeeFinancialAccount>
	</xsl:template>
	<xsl:template match="cac:PayeeFinancialAccount" mode="PaymentMeans">
		<cac:PayeeFinancialAccount>
			<xsl:apply-templates select="cbc:ID"/>
			<xsl:apply-templates select="cbc:Name"/>
			<xsl:apply-templates select="cac:FinancialInstitutionBranch"/>
		</cac:PayeeFinancialAccount>
	</xsl:template>
	<xsl:template match="cac:FinancialInstitutionBranch">
		<cac:FinancialInstitutionBranch>
			<xsl:choose>
				<xsl:when test="cbc:ID and (normalize-space(cbc:ID) != '')">
					<xsl:apply-templates select="cbc:ID"/>
				</xsl:when>
				<xsl:when test="cac:FinancialInstitution/cbc:ID and (normalize-space(cac:FinancialInstitution/cbc:ID) != '')">
					<xsl:apply-templates select="cac:FinancialInstitution/cbc:ID"/>
				</xsl:when>
			</xsl:choose>
		</cac:FinancialInstitutionBranch>
	</xsl:template>
	
	<xsl:template match="cac:CardAccount">
		<cac:CardAccount>
			<xsl:apply-templates select="cbc:PrimaryAccountNumberID"/>
			<xsl:apply-templates select="cbc:NetworkID"/>
			<xsl:apply-templates select="cbc:HolderName"/>
		</cac:CardAccount>
	</xsl:template>
	
<!--	<xsl:template match="cbc:PaymentID">
		<cbc:PaymentID>
			<xsl:value-of select="concat(., ' ', ../cbc:InstructionID)"/>
		</cbc:PaymentID>
	</xsl:template>
-->	

	
	<xsl:template match="cbc:InstructionID">
		<cbc:PaymentID>
			<xsl:choose>
				<xsl:when test="normalize-space(../cbc:PaymentID) != '' and string-length(normalize-space(../cbc:PaymentID)) = 4">
					<xsl:value-of select="normalize-space(concat(../cbc:PaymentID, ' ', .))" />
				</xsl:when>
				<xsl:when test="normalize-space(../cbc:PaymentID) != '' and string-length(normalize-space(../cbc:PaymentID)) = 2">
					<xsl:value-of select="normalize-space(concat('HR', ../cbc:PaymentID, ' ', .))" />
				</xsl:when>
				<xsl:otherwise><xsl:value-of select="normalize-space(concat('HR00 ', .))" /></xsl:otherwise>
			</xsl:choose>
			
		</cbc:PaymentID>
	
	</xsl:template>
	
	<xsl:template match="cbc:PaymentID">
		<cbc:PaymentID>
			<xsl:choose>
				<xsl:when test="normalize-space(.) != '' and string-length(normalize-space(.)) = 4">
					<xsl:value-of select="normalize-space(.)" />
				</xsl:when>
				<xsl:when test="normalize-space(.) != '' and string-length(normalize-space(../cbc:PaymentID)) = 2">
					<xsl:value-of select="normalize-space(concat('HR', .))" />
				</xsl:when>
				<xsl:otherwise><xsl:value-of select="'HR99'"/></xsl:otherwise>
			</xsl:choose>
			
		</cbc:PaymentID>
	
	
	</xsl:template>
	


	<xsl:template match="cbc:PaymentMeansCode">
		<xsl:variable name="codeVal">
			<xsl:value-of select="."/>
		</xsl:variable>
		<cbc:PaymentMeansCode>
			<xsl:choose>
				<xsl:when test="normalize-space($codeVal) = '42'">30</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="."/>
				</xsl:otherwise>
			</xsl:choose>
		</cbc:PaymentMeansCode>
	</xsl:template>
	
	
	<xsl:template match="cac:Delivery">
		<cac:Delivery>
			<xsl:apply-templates select="cbc:ActualDeliveryDate"/>
			<xsl:choose>
				<xsl:when test="cac:DeliveryLocation">
					<xsl:apply-templates select="cac:DeliveryLocation"/>
				</xsl:when>
				<xsl:when test="cac:DeliveryAddress">
					<xsl:apply-templates select="cac:DeliveryAddress"/>
				</xsl:when>
			</xsl:choose>
			<xsl:apply-templates select="cac:DeliveryParty"/>
		</cac:Delivery>
	</xsl:template>
	
	<xsl:template match="cac:DeliveryParty" >
		<cac:DeliveryParty>
			<xsl:apply-templates select="cbc:EndpointID" mode="DeliveryParty"/>
			<xsl:apply-templates select="cac:PartyIdentification" mode="DeliveryParty"/>
			<xsl:apply-templates select="cac:PartyName" />
			<xsl:apply-templates select="cac:PostalAddress" />
			<xsl:apply-templates select="cac:PartyLegalEntity" />
		</cac:DeliveryParty>
	
	</xsl:template>
	
	<xsl:template match="cbc:EndpointID" mode="DeliveryParty">
		<xsl:if test="normalize-space(.) != ''">
			<cbc:EndpointID>
				<xsl:choose>
					<xsl:when test="@schemeID and (normalize-space(@schemeID) != '')">
						<xsl:attribute name="schemeID" namespace="">
							<xsl:choose>
								<xsl:when test="@schemeID = 'GLN'">0088</xsl:when>
								<xsl:when test="@schemeID = 'DUNS'">0060</xsl:when>
								<xsl:otherwise><xsl:value-of select="@schemeID" /></xsl:otherwise>
							</xsl:choose>
						</xsl:attribute>
					</xsl:when>
					<xsl:otherwise>
						<xsl:attribute name="schemeID" namespace="">0088</xsl:attribute>
					</xsl:otherwise>
				</xsl:choose>
				
				<xsl:value-of select="." />
			
			</cbc:EndpointID>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:PartyLegalEntity">
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:RegistrationName"/>
			<xsl:apply-templates select="cbc:CompanyID"/>
		</cac:PartyLegalEntity>
	</xsl:template>

	<xsl:template match="cac:PartyIdentification" mode="DeliveryParty">
		<cac:PartyIdentification>
			<xsl:apply-templates select="cbc:ID"/>
		</cac:PartyIdentification>
	</xsl:template>

<!--	<xsl:template match="cac:DeliveryParty">
		<cac:DeliveryParty>
			<xsl:apply-templates select="cac:PartyName"/>
		</cac:DeliveryParty>
	</xsl:template>
	
-->	
	<xsl:template match="cac:DeliveryAddress">
		<cac:DeliveryLocation>
			<cac:Address>
				<xsl:apply-templates select="cbc:StreetName"/>
				<xsl:apply-templates select="cbc:AdditionalStreetName"/>
				<xsl:apply-templates select="cbc:CityName"/>
				<xsl:apply-templates select="cbc:PostalZone"/>
				<xsl:apply-templates select="cbc:CountrySubentity"/>
				<xsl:apply-templates select="cac:AddressLine"/>
				<!-- <xsl:apply-templates select="cac:Country" /> -->
				<xsl:call-template name="Country">
					<xsl:with-param name="countryCode" select="cac:Country/cbc:IdentificationCode"/>
				</xsl:call-template>
			</cac:Address>
		</cac:DeliveryLocation>
	</xsl:template>
	
	
	<xsl:template match="cac:DeliveryLocation">
		<cac:DeliveryLocation>
			<xsl:apply-templates select="cbc:ID" mode="SchemeID"/>
			<xsl:apply-templates select="cac:Address"/>
			<xsl:apply-templates select="cac:DeliveryParty"/>
		</cac:DeliveryLocation>
	</xsl:template>
	
<!--	<xsl:template match="cac:DeliveryParty">
		<cac:DeliveryParty>
			<xsl:apply-templates select="cac:PartyName"/>
		</cac:DeliveryParty>
	</xsl:template>
-->	
	
	<xsl:template match="cac:Address">
		<cac:Address>
			<xsl:apply-templates select="cbc:StreetName"/>
			<xsl:apply-templates select="cbc:AdditionalStreetName"/>
			<xsl:apply-templates select="cbc:CityName"/>
			<xsl:apply-templates select="cbc:PostalZone"/>
			<xsl:apply-templates select="cbc:CountrySubentity"/>
			<xsl:apply-templates select="cac:AddressLine"/>
			<!-- <xsl:apply-templates select="cac:Country" /> -->
			<xsl:call-template name="Country">
				<xsl:with-param name="countryCode" select="cac:Country/cbc:IdentificationCode"/>
			</xsl:call-template>
		</cac:Address>
	</xsl:template>
	<xsl:template match="cac:TaxRepresentativeParty">
		<cac:TaxRepresentativeParty>
			<xsl:apply-templates select="cac:PartyName"/>
			<xsl:apply-templates select="cac:PostalAddress"/>
			<xsl:apply-templates select="cac:PartyTaxScheme" mode="TaxRepresentativeParty"/>
		</cac:TaxRepresentativeParty>
	</xsl:template>
	
	
	<xsl:template match="cac:PartyTaxScheme" mode="TaxRepresentativeParty">
		<cac:PartyTaxScheme>
			<xsl:if test="$isOutsideVAT = 'false'">
				<xsl:apply-templates select="cbc:CompanyID"/>
			</xsl:if>
			<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
		</cac:PartyTaxScheme>
	</xsl:template>
	
	
	<xsl:template match="cac:PayeeParty">
		<xsl:if test="normalize-space(cac:PartyIdentification/cbc:ID) != '' or normalize-space(cac:PartyName/cbc:Name) != '' or normalize-space(cac:PartyLegalEntity/cbc:CompanyID) != ''" >
			<cac:PayeeParty>
				<xsl:apply-templates select="cac:PartyIdentification" mode="PayeeParty"/>
				<xsl:apply-templates select="cac:PartyName"/>
				<xsl:apply-templates select="cac:PartyLegalEntity" mode="PayeeParty"/>
			</cac:PayeeParty>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:PartyLegalEntity" mode="PayeeParty">
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:CompanyID" mode="SchemeID"/>
		</cac:PartyLegalEntity>
	</xsl:template>
	<xsl:template match="cac:PartyIdentification" mode="PayeeParty">
		<cac:PartyIdentification>
			<xsl:apply-templates select="cbc:ID" mode="SchemeID"/>
		</cac:PartyIdentification>
	</xsl:template>
	
	<xsl:template match="cac:AccountingCustomerParty">
		<cac:AccountingCustomerParty>
			<xsl:apply-templates select="cac:Party" mode="AccountingCustomerParty"/>
		</cac:AccountingCustomerParty>
	</xsl:template>
	
	<xsl:template match="cac:Party" mode="AccountingCustomerParty">
		<cac:Party>
			<!-- <xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="EndpointID" /> -->
			<xsl:call-template name="CustomerEndpointID"/>
			<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="PartyIdentification">
				<xsl:with-param name="bussinessUnit" select="$customerBusinessUnit"/>
			</xsl:apply-templates>
			<xsl:apply-templates select="cac:PartyName"/>
			<xsl:apply-templates select="cac:PostalAddress" mode="customerAddress"/>
			<xsl:call-template name="PartyTaxScheme"/>
			<xsl:apply-templates select="cac:PartyLegalEntity" mode="AccountingCustomerParty"/>
			<xsl:call-template name="PartyContact"/>
		</cac:Party>
	</xsl:template>
	
	<xsl:template match="cac:PartyLegalEntity" mode="AccountingCustomerParty">
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:RegistrationName"/>
			<xsl:apply-templates select="cbc:CompanyID" mode="SchemeID"/>
		</cac:PartyLegalEntity>
	</xsl:template>
	
	<xsl:template match="cac:AccountingSupplierParty">
		<cac:AccountingSupplierParty>
			<xsl:apply-templates select="cac:Party" mode="AccountingSupplierParty"/>
			<xsl:apply-templates select="cac:AccountingContact" mode="HRsellerContact" />
		</cac:AccountingSupplierParty>
	</xsl:template>
	
	<xsl:template match="cac:AccountingContact" mode="HRsellerContact">
		<cac:SellerContact>
			<!--<cbc:ID>12345678901</cbc:ID>
-->			<!-- OIB OPERATERA -->
			<xsl:choose>
				<xsl:when test="cbc:ID and normalize-space(cbc:ID) != ''">
					<cbc:ID><xsl:value-of select="cbc:ID" /> </cbc:ID>
				</xsl:when>
				<xsl:otherwise>
					<cbc:ID><xsl:value-of select="$findOIB" /> </cbc:ID>
				</xsl:otherwise>
			</xsl:choose>
			<cbc:Name><xsl:value-of select="cbc:Note" /></cbc:Name>	
		</cac:SellerContact>
	</xsl:template>
		
	<xsl:template match="cac:Party" mode="AccountingSupplierParty">
		<cac:Party>
			<!-- <xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="EndpointID" /> -->
			<xsl:call-template name="SupplierEndpointID"/>
			<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="SupplierPartyIdentification">
				<xsl:with-param name="bussinessUnit" select="$supplierBusinessUnit"/>
			</xsl:apply-templates>
			<xsl:apply-templates select="cbc:EndpointID[string-length(.) = 13]" mode="PartyIdentification"/>
			<xsl:apply-templates select="cac:PartyName"/>
			<xsl:apply-templates select="cac:PostalAddress" mode="supplierAddress"/>
			<xsl:call-template name="SupplierPartyTaxScheme"/>
			<xsl:apply-templates select="cac:PartyLegalEntity" mode="AccountingSupplierParty"/>
			<xsl:call-template name="PartyContact"/>
		</cac:Party>
	</xsl:template>
	
	<xsl:template match="cbc:EndpointID" mode="PartyIdentification">
		<cac:PartyIdentification>
			<cbc:ID>
				<xsl:attribute name="schemeID" namespace="">0088</xsl:attribute>
				<xsl:value-of select="."/>
			</cbc:ID>
		</cac:PartyIdentification>
	</xsl:template>
	
	<xsl:template match="cbc:CompanyID" mode="SupplierPartyIdentification">
		<xsl:param name="bussinessUnit"/>
		<cac:PartyIdentification>
			<cbc:ID>
				<xsl:choose>
					<xsl:when test="normalize-space($findOIB) != ''">
						<xsl:choose>
							<xsl:when test="normalize-space($bussinessUnit) != ''">
								<xsl:value-of select="concat($supplierCountrySchemeID,':', $findOIB,'::HR99:', $bussinessUnit)"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="concat($supplierCountrySchemeID,':', $findOIB)"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="concat($supplierCountrySchemeID,':', .)"/>
					</xsl:otherwise>
				</xsl:choose>
			</cbc:ID>
		</cac:PartyIdentification>
	</xsl:template>
	
	<xsl:template name="SupplierEndpointID">
		<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="SupplierEndpointID"/>
	</xsl:template>
	
	<xsl:template match="cbc:CompanyID" mode="SupplierEndpointID">
		<cbc:EndpointID>
			<xsl:attribute name="schemeID" namespace=""><xsl:value-of select="$supplierCountrySchemeID"/></xsl:attribute>
			<xsl:choose>
				<xsl:when test="normalize-space($findOIB) != '' ">
					<xsl:value-of select="$findOIB"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="."/>
				</xsl:otherwise>
			</xsl:choose>
		</cbc:EndpointID>
	</xsl:template>
	
	<xsl:template match="cac:PartyLegalEntity" mode="AccountingSupplierParty">
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:RegistrationName"/>
			<xsl:apply-templates select="cbc:CompanyID" mode="SchemeID"/>
			<xsl:call-template name="CompanyLegalForm"/>
		</cac:PartyLegalEntity>
	</xsl:template>
	
	<xsl:template name="CompanyLegalForm">
		<xsl:variable name="InvoiceIssuerContent">
			<xsl:value-of select="//*[local-name() = 'InvoiceIssuer']"/>
		</xsl:variable>
		<xsl:variable name="CompanyLegalFormContent">
			<xsl:choose>
				<xsl:when test="cbc:CompanyLegalForm">
					<xsl:value-of select="cbc:CompanyLegalForm"/>
				</xsl:when>
				<xsl:when test="normalize-space($InvoiceIssuerContent) != ''">
					<xsl:value-of select="$InvoiceIssuerContent"/>
				</xsl:when>
				<xsl:otherwise>?</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:if test="normalize-space($CompanyLegalFormContent) != '?'">
			<cbc:CompanyLegalForm>
				<xsl:value-of select="$CompanyLegalFormContent"/>
			</cbc:CompanyLegalForm>
		</xsl:if>
	</xsl:template>
	
	
	<xsl:template name="PartyTaxScheme">
		<cac:PartyTaxScheme>
			<xsl:choose>
				<xsl:when test="$isOutsideVAT = 'false'">
					<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="PartyTaxID"/>
					<cac:TaxScheme>
						<cbc:ID>VAT</cbc:ID>
					</cac:TaxScheme>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="PartyTaxID"/>
					<cac:TaxScheme>
						<cbc:ID>FRE</cbc:ID>
					</cac:TaxScheme>
				</xsl:otherwise>
			</xsl:choose>
		
		
<!--			<xsl:if test="$isOutsideVAT = 'false'">
				<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="PartyTaxID"/>
			</xsl:if>
			<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
-->
		</cac:PartyTaxScheme>
	</xsl:template>
	
	
	<xsl:template name="SupplierPartyTaxScheme">
		<cac:PartyTaxScheme>
			<xsl:choose>
				<xsl:when test="$isOutsideVAT = 'false'">
					<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="SupplierPartyTaxID"/>
					<cac:TaxScheme>
						<cbc:ID>VAT</cbc:ID>
					</cac:TaxScheme>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID"/>
					<cac:TaxScheme>
						<cbc:ID>FRE</cbc:ID>
					</cac:TaxScheme>
				</xsl:otherwise>
			</xsl:choose>
		
		
<!--			<xsl:if test="$isOutsideVAT = 'false'">
				<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="SupplierPartyTaxID"/>
			</xsl:if>
			<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
-->

		</cac:PartyTaxScheme>



	</xsl:template>
	
	
	<xsl:template match="cbc:CompanyID" mode="SupplierPartyTaxID">
		<cbc:CompanyID>
			<xsl:choose>
				<xsl:when test="count($countryPrefixList//CountryPrefix[. = $supplierCountryPrefix])> 0">
					<xsl:value-of select="."/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="concat('HR',$findOIB)"/>
				</xsl:otherwise>
			</xsl:choose>
		</cbc:CompanyID>
	</xsl:template>
	<xsl:template match="cac:PostalAddress">
		<cac:PostalAddress>
			<!-- <xsl:apply-templates select="cbc:StreetName" /> -->
			<xsl:apply-templates select="cbc:StreetName"/>
			<xsl:apply-templates select="cbc:AdditionalStreetName"/>
			<xsl:apply-templates select="cbc:CityName"/>
			<xsl:apply-templates select="cbc:PostalZone"/>
			<xsl:apply-templates select="cbc:CountrySubentity"/>
			<xsl:apply-templates select="cac:AddressLine"/>
			<!-- <xsl:apply-templates select="cac:Country" /> -->
			<xsl:call-template name="Country">
				<xsl:with-param name="countryCode" select="cac:Country/cbc:IdentificationCode"/>
			</xsl:call-template>
		</cac:PostalAddress>
	</xsl:template>
	<xsl:template match="cac:PostalAddress" mode="supplierAddress">
		<cac:PostalAddress>
			<xsl:apply-templates select="cbc:StreetName"/>
			<xsl:apply-templates select="cbc:AdditionalStreetName"/>
			<xsl:apply-templates select="cbc:CityName"/>
			<xsl:apply-templates select="cbc:PostalZone"/>
			<xsl:apply-templates select="cbc:CountrySubentity"/>
			<xsl:apply-templates select="cac:AddressLine"/>
			<!-- <xsl:apply-templates select="cac:Country" /> -->
			<xsl:call-template name="Country">
				<xsl:with-param name="countryCode" select="$supplierCountryPrefix"/>
			</xsl:call-template>
		</cac:PostalAddress>
	</xsl:template>
	<xsl:template match="cac:PostalAddress" mode="customerAddress">
		<cac:PostalAddress>
			<xsl:apply-templates select="cbc:StreetName"/>
			<xsl:apply-templates select="cbc:AdditionalStreetName"/>
			<xsl:apply-templates select="cbc:CityName"/>
			<xsl:apply-templates select="cbc:PostalZone"/>
			<xsl:apply-templates select="cbc:CountrySubentity"/>
			<xsl:apply-templates select="cac:AddressLine"/>
			<!-- <xsl:apply-templates select="cac:Country" /> -->
			<xsl:call-template name="Country">
				<xsl:with-param name="countryCode" select="$customerCountryPrefix"/>
			</xsl:call-template>
		</cac:PostalAddress>
	</xsl:template>
	<xsl:template match="cac:Country">
		<cac:Country>
			<xsl:apply-templates select="cbc:IdentificationCode"/>
		</cac:Country>
	</xsl:template>
	<xsl:template match="cac:AddressLine">
		<cac:AddressLine>
			<xsl:apply-templates select="cbc:Line"/>
		</cac:AddressLine>
	</xsl:template>
	
	<xsl:template match="cac:PartyName">
		<cac:PartyName>
			<xsl:choose>
				<xsl:when test="cbc:Name and (normalize-space(cbc:Name) != '')">
					<cbc:Name>
						<xsl:value-of select="cbc:Name"/>
					</cbc:Name>
				</xsl:when>
				<xsl:when test="../cac:PartyLegalEntity/cbc:RegistrationName and (normalize-space(../cac:PartyLegalEntity/cbc:RegistrationName) != '')">
					<cbc:Name>
						<xsl:value-of select="../cac:PartyLegalEntity/cbc:RegistrationName"/>
					</cbc:Name>
				</xsl:when>
			</xsl:choose>
		</cac:PartyName>
	</xsl:template>

	
	<xsl:template match="cac:PartyName" >
			<xsl:choose>
				<xsl:when test="cbc:Name and (normalize-space(cbc:Name) != '')">
					<cac:PartyName>
						<cbc:Name><xsl:value-of select="cbc:Name" /></cbc:Name>
					</cac:PartyName>
				</xsl:when>
				<xsl:when test="../cac:PartyLegalEntity/cbc:RegistrationName and (normalize-space(../cac:PartyLegalEntity/cbc:RegistrationName) != '')">
					<cac:PartyName>
				
						<cbc:Name><xsl:value-of select="../cac:PartyLegalEntity/cbc:RegistrationName" /></cbc:Name>
					</cac:PartyName>
				
				</xsl:when>
			</xsl:choose>
	
	
	</xsl:template>
	
	
	
	<xsl:template match="cac:ProjectReference">
		<cac:AdditionalDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
			<xsl:apply-templates select="cbc:IssueDate"/>
		</cac:AdditionalDocumentReference>
	</xsl:template>
	<xsl:template match="cac:AdditionalDocumentReference" mode="ID">
		<cac:AdditionalDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="SchemeID"/>
			<xsl:apply-templates select="cbc:DocumentType"/>
			<xsl:apply-templates select="cac:Attachment"/>
		</cac:AdditionalDocumentReference>
	</xsl:template>
	<xsl:template match="ns1:AttachedDocument" mode="Attachment">
		<cac:AdditionalDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
			<xsl:choose>
				<xsl:when test="position() = 1">
					<xsl:call-template name="DocumentType-Vizualizacija"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="cbc:DocumentType"/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:apply-templates select="cac:Attachment"/>
		</cac:AdditionalDocumentReference>
	</xsl:template>
	<xsl:template match="cac:Attachment">
		<cac:Attachment>
			<xsl:apply-templates select="cbc:EmbeddedDocumentBinaryObject"/>
			<xsl:apply-templates select="cac:ExternalReference"/>
		</cac:Attachment>
	</xsl:template>
	<xsl:template name="DocumentType-Vizualizacija">
		<xsl:choose>
			<xsl:when test="(not(cbc:DocumentType) or (normalize-space(cbc:DocumentType) = '')) and (cac:Attachment/cbc:EmbeddedDocumentBinaryObject/@mimeCode='application/pdf')">
				<cbc:DocumentType>VIZUALIZACIJA IZVORNIKA RAČUNA</cbc:DocumentType>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="cbc:DocumentType"/>
			</xsl:otherwise>
		</xsl:choose>
		<!-- <cbc:DocumentType>VIZUALIZACIJA IZVORNIKA RAČUNA</cbc:DocumentType> -->
	</xsl:template>
	<xsl:template match="cac:ExternalReference">
		<cac:ExternalReference>
			<xsl:apply-templates select="cbc:URI"/>
		</cac:ExternalReference>
	</xsl:template>
	<xsl:template match="cbc:EmbeddedDocumentBinaryObject">
		<cbc:EmbeddedDocumentBinaryObject>
			<xsl:attribute name="mimeCode" namespace=""><xsl:value-of select="@mimeCode"/></xsl:attribute>
			<xsl:attribute name="filename" namespace=""><xsl:value-of select="translate(@filename, ':/\&amp;', '-__ ')"/></xsl:attribute>
			<xsl:value-of select="."/>
		</cbc:EmbeddedDocumentBinaryObject>
	</xsl:template>
	
	<xsl:template match="cac:ContractDocumentReference">
		<cac:ContractDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
		</cac:ContractDocumentReference>
	</xsl:template>
	
	<xsl:template match="cac:OriginatorDocumentReference">
		<cac:OriginatorDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
		</cac:OriginatorDocumentReference>
	</xsl:template>
	
	<xsl:template match="cac:ReceiptDocumentReference">
		<cac:ReceiptDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
		</cac:ReceiptDocumentReference>
	</xsl:template>
	
	<xsl:template match="cac:DespatchDocumentReference">
		<cac:DespatchDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
		</cac:DespatchDocumentReference>
	</xsl:template>
	
	<xsl:template match="cac:BillingReference">
		<cac:BillingReference>
			<xsl:apply-templates select="cac:InvoiceDocumentReference"/>
		</cac:BillingReference>
	</xsl:template>
	
	<xsl:template match="cac:InvoiceDocumentReference">
		<!-- ISPRAVAK: V3 15.07.2019 -->
		<cac:InvoiceDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
			<xsl:apply-templates select="cbc:IssueDate"/>
		</cac:InvoiceDocumentReference>
	</xsl:template>
	
	<xsl:template match="cac:OrderReference">
		<cac:OrderReference>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
			<xsl:apply-templates select="cbc:SalesOrderReference"/>
		</cac:OrderReference>
	</xsl:template>
	
	<xsl:template match="cac:InvoicePeriod">
		<cac:InvoicePeriod>
			<xsl:apply-templates select="cbc:StartDate"/>
			<xsl:apply-templates select="cbc:EndDate"/>
			<xsl:apply-templates select="cbc:DescriptionCode"/>
		</cac:InvoicePeriod>
	</xsl:template>
	<xsl:template match="cbc:DescriptionCode">
		<!--  Vidjeti da li trebaju atributti - iskoristiti Copy-of u tom slučaju -->
		<cbc:DescriptionCode>
			<xsl:value-of select="."/>
		</cbc:DescriptionCode>
	</xsl:template>
	<xsl:template match="cbc:Name" mode="OdgovornaOsoba">
		<cbc:Note>
			<xsl:value-of select="concat('Odgovorna osoba:',. )"/>
		</cbc:Note>
	</xsl:template>
	<xsl:template match="cbc:Note" mode="OznakaOperatera">
		<cbc:Note>
			<xsl:value-of select="concat('Oznaka operatera:', . )"/>
		</cbc:Note>
	</xsl:template>
	<xsl:template match="cbc:IssueTime">
			<cbc:IssueTime><xsl:value-of select="." /> </cbc:IssueTime>

<!--		<cbc:Note>
			<xsl:value-of select="concat( 'Vrijeme izdavanja:' ,.)"/>
		</cbc:Note>
-->	</xsl:template>
	<xsl:template match="cbc:InvoiceTypeCode">
		<cbc:CreditNoteTypeCode>
			<xsl:value-of select="."/>
		</cbc:CreditNoteTypeCode>
	</xsl:template>
	<xsl:template match="cac:PaymentMeans/cbc:PaymentDueDate">
		<cbc:DueDate>
			<xsl:value-of select="."/>
		</cbc:DueDate>
	</xsl:template>
	<!--  GENERIC TEMPLATES -->
	<xsl:template name="PartyContact">
		<xsl:variable name="PartyContactNameContent">
			<xsl:choose>
				<xsl:when test="cac:Contact/cbc:Name">
					<xsl:value-of select="cac:Contact/cbc:Name"/>
				</xsl:when>
				<xsl:when test="../cac:AccountingContact/cbc:Name">
					<xsl:value-of select="../cac:AccountingContact/cbc:Name"/>
				</xsl:when>
				<xsl:when test="../cac:SellerContact/cbc:Name">
					<xsl:value-of select="../cac:SellerContact/cbc:Name"/>
				</xsl:when>
				<xsl:when test="../cac:BuyerContact/cbc:Name">
					<xsl:value-of select="../cac:BuyerContact/cbc:Name"/>
				</xsl:when>
				<xsl:otherwise>?</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="PartyContactTelephoneContent">
			<xsl:choose>
				<xsl:when test="cac:Contact/cbc:Telephone">
					<xsl:value-of select="cac:Contact/cbc:Telephone"/>
				</xsl:when>
				<xsl:when test="../cac:AccountingContact/cbc:Telephone">
					<xsl:value-of select="../cac:AccountingContact/cbc:Telephone"/>
				</xsl:when>
				<xsl:when test="../cac:SellerContact/cbc:Telephone">
					<xsl:value-of select="../cac:SellerContact/cbc:Telephone"/>
				</xsl:when>
				<xsl:when test="../cac:BuyerContact/cbc:Telephone">
					<xsl:value-of select="../cac:BuyerContact/cbc:Telephone"/>
				</xsl:when>
				<xsl:otherwise>?</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="PartyContactElectronicMailContent">
			<xsl:choose>
				<xsl:when test="cac:Contact/cbc:ElectronicMail and (normalize-space(cac:Contact/cbc:ElectronicMail) != 'fina@moj-eracun.hr') and (normalize-space(cac:Contact/cbc:ElectronicMail) != 'adresar@moj-eracun.hr')">
					<xsl:value-of select="cac:Contact/cbc:ElectronicMail"/>
				</xsl:when>
				<xsl:when test="(../cac:AccountingContact/cbc:ElectronicMail ) and (normalize-space(../cac:AccountingContact/cbc:ElectronicMail) != 'fina@moj-eracun.hr') and (normalize-space(../cac:AccountingContact/cbc:ElectronicMail) != 'adresar@moj-eracun.hr') ">
					<xsl:value-of select="../cac:AccountingContact/cbc:ElectronicMail"/>
				</xsl:when>
				<xsl:when test="../cac:SellerContact/cbc:ElectronicMail">
					<xsl:value-of select="../cac:SellerContact/cbc:ElectronicMail"/>
				</xsl:when>
				<xsl:when test="../cac:BuyerContact/cbc:ElectronicMail">
					<xsl:value-of select="../cac:BuyerContact/cbc:ElectronicMail"/>
				</xsl:when>
				<xsl:otherwise>?</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="PartyContactContent">
			<xsl:if test="normalize-space($PartyContactNameContent) != '?'">
				<cbc:Name>
					<xsl:value-of select="$PartyContactNameContent"/>
				</cbc:Name>
			</xsl:if>
			<xsl:if test="normalize-space($PartyContactTelephoneContent) != '?'">
				<cbc:Telephone>
					<xsl:value-of select="$PartyContactTelephoneContent"/>
				</cbc:Telephone>
			</xsl:if>
			<xsl:if test="normalize-space($PartyContactElectronicMailContent) != '?'">
				<cbc:ElectronicMail>
					<xsl:value-of select="$PartyContactElectronicMailContent"/>
				</cbc:ElectronicMail>
			</xsl:if>
		</xsl:variable>
		<xsl:if test="string($PartyContactContent) != ''">
			<cac:Contact>
				<xsl:copy-of select="$PartyContactContent"/>
			</cac:Contact>
		</xsl:if>
	</xsl:template>
	
	
	<xsl:template name="CustomerEndpointID">
		<xsl:choose>
			<xsl:when test="cbc:EndpointID and (normalize-space(cbc:EndpointID) != '')">
				<xsl:apply-templates select="cbc:EndpointID" mode="SchemeID"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="EndpointID"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	
	<!--  GENERIC ELEMENTS -->
	<xsl:template name="Country">
		<xsl:param name="countryCode" select="'HR'"/>
		<cac:Country>
			<xsl:choose>
				<xsl:when test="count($countryPrefixList//CountryPrefix[. = $countryCode])>0">
					<cbc:IdentificationCode>
						<xsl:value-of select="translate($countryCode,$lowercase,$uppercase)"/>
					</cbc:IdentificationCode>
				</xsl:when>
				<xsl:otherwise>
					<cbc:IdentificationCode>HR</cbc:IdentificationCode>
				</xsl:otherwise>
			</xsl:choose>
		</cac:Country>
	</xsl:template>
	<xsl:template match="cbc:Amount | cbc:BaseAmount | cbc:TaxAmount | cbc:TaxableAmount | cbc:LineExtensionAmount | cbc:TaxExclusiveAmount | cbc:TaxInclusiveAmount | cbc:AllowanceTotalAmount | cbc:ChargeTotalAmount | cbc:PrepaidAmount | cbc:PayableRoundingAmount | cbc:PayableAmount ">
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:attribute name="currencyID" namespace=""><xsl:value-of select="@currencyID"/></xsl:attribute>
			<xsl:choose>
				<xsl:when test="number(.) = 0">0.00</xsl:when>
				<xsl:when test="string(number(.))='NaN'">0.00</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="format-number(number(.), '#########0.00;-#########0.00')"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:template>
	<xsl:template match="cbc:PriceAmount">
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:attribute name="currencyID" namespace=""><xsl:value-of select="@currencyID"/></xsl:attribute>
			<xsl:choose>
				<xsl:when test="number(.) = 0">0.00</xsl:when>
				<xsl:when test="string(number(.))='NaN'">0.00</xsl:when>
				<xsl:otherwise>
					<xsl:variable name="priceAmount">
						<xsl:value-of select="format-number(number(.), '#########0.00;-#########0.00')"/>
					</xsl:variable>
					<xsl:choose>
						<xsl:when test="string-length(string(.)) >string-length(string($priceAmount))">
							<xsl:value-of select="."/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$priceAmount"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:template>
	<xsl:template match="cbc:MultiplierFactorNumeric">
		<cbc:MultiplierFactorNumeric>
			<xsl:value-of select="number(.)"/>
		</cbc:MultiplierFactorNumeric>
	</xsl:template>
	<xsl:template match="cbc:Percent">
		<cbc:Percent>
			<xsl:value-of select="number(.)"/>
		</cbc:Percent>
	</xsl:template>
	<xsl:template match="cbc:CompanyID" mode="PartyIdentification">
		<xsl:param name="bussinessUnit"/>
		<cac:PartyIdentification>
			<cbc:ID>
				<xsl:choose>
					<xsl:when test="normalize-space($bussinessUnit) != ''">
						<xsl:value-of select="concat($customerCountrySchemeID,':', .,'::HR99:',$bussinessUnit)"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="concat($customerCountrySchemeID,':', .)"/>
					</xsl:otherwise>
				</xsl:choose>
			</cbc:ID>
		</cac:PartyIdentification>
	</xsl:template>
	<xsl:template match="cbc:CompanyID" mode="PartyTaxID">
		<cbc:CompanyID>
			<xsl:choose>
				<xsl:when test="count($countryPrefixList//CountryPrefix[. = $customerCountryPrefix]) > 0">
					<xsl:value-of select="."/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="concat('HR', .)"/>
				</xsl:otherwise>
			</xsl:choose>
		</cbc:CompanyID>
	</xsl:template>
	
	<xsl:template match="cbc:CompanyID" mode="EndpointID">
		<cbc:EndpointID>
			<xsl:attribute name="schemeID" namespace=""><xsl:value-of select="$customerCountrySchemeID"/></xsl:attribute>
			<xsl:value-of select="."/>
		</cbc:EndpointID>
	</xsl:template>
	
	<xsl:template match="cbc:EndpointID" mode="SchemeID">
		<cbc:EndpointID>
			<xsl:choose>
				<xsl:when test="@schemeID and (normalize-space(@schemeID) != '')">
					<xsl:attribute name="schemeID" namespace="">
						<xsl:choose>
							<xsl:when test="@schemeID = 'GLN'">0088</xsl:when>
							<xsl:otherwise><xsl:value-of select="@schemeID"/></xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
				</xsl:when>
				<xsl:when test="(string-length(.) = 13) and (substring(., 1, 3) = '385')">
					<xsl:attribute name="schemeID" namespace="">0088</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="schemeID" namespace=""><xsl:value-of select="$customerCountrySchemeID"/></xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:value-of select="."/>
		</cbc:EndpointID>
	</xsl:template>
	<xsl:template match="cbc:ID" mode="TaxCategoryID">
		<cbc:ID>
			<xsl:choose>
				<xsl:when test="normalize-space(.) = 'O'">
					<xsl:choose>
						<xsl:when test="$isOutsideVAT = 'true'">O</xsl:when>
						<xsl:otherwise>E</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="normalize-space(.)"/>
				</xsl:otherwise>
			</xsl:choose>
		</cbc:ID>
	</xsl:template>
	<xsl:template match="cbc:ID | cbc:CompanyID " mode="SchemeID">
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:apply-templates select="@schemeID"/>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>
	<xsl:template match="@schemeID">
		<xsl:attribute name="schemeID" namespace="">
			<!--<xsl:value-of select="."/>-->
			<xsl:choose>
				<xsl:when test="contains(., 'GLN')">0088</xsl:when>
				<xsl:when test="contains(., 'DUNS')">0060</xsl:when>
				<xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
			</xsl:choose>

		</xsl:attribute>
	</xsl:template>
	<xsl:template match="cbc:ID" mode="ID">
		<cbc:ID>
			<xsl:value-of select="."/>
		</cbc:ID>
	</xsl:template>
	

	<xsl:template match="cbc:ID" mode="TaxCategoryID">
		<cbc:ID>
			<xsl:choose>
				<xsl:when test="normalize-space(.) = 'O'">
					<xsl:choose>
						<xsl:when test="$isOutsideVAT = 'true'">O</xsl:when>
						<xsl:otherwise>E</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:when test="normalize-space(.) = 'B'">AE</xsl:when>
				<xsl:otherwise><xsl:value-of select="normalize-space(.)" /></xsl:otherwise>
			</xsl:choose>
		
		</cbc:ID>
	
	</xsl:template>
	
		

	<xsl:template match="cbc:DocumentType">
		<cbc:DocumentType>
			<xsl:value-of select="."/>
		</cbc:DocumentType>
	</xsl:template>
	<xsl:template match="cbc:*">
		<!-- 		<xsl:copy>
			<xsl:value-of select="." />
		</xsl:copy>
 -->
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>
