<?xml version="1.0" encoding="UTF-8"?>
<!-- ===== 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., 



-->

<!-- V2026-01-06
Ispravljen bug da je hrextac:HRLegalMonetaryTotal stavljen unutar hrextac:TaxTotal

-->

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
			xmlns:hrextac="urn:mfin.gov.hr:schema:xsd:HRExtensionAggregateComponents-1" 
			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:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" 
			xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" 
			xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" 
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xmlns:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
			exclude-result-prefixes="xs xsd sbc xsi">

	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	

	
	<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:comment>
			Generated by Moj eRačun HRInvoice2EN transformation V2024-09-05
		</xsl:comment>
-->		
		<xsl:choose>
			
			<xsl:when test="/i:Invoice and normalize-space(/i:Invoice/cbc:CustomizationID) = 'urn:cen.eu:en16931:2017'">
				<xsl:call-template name="InvoiceFixEN"/>
				<xsl:comment>
					Generated by Moj eRačun ENInvoice2ENF2 - InvoiceFixEN
				</xsl:comment>
			</xsl:when>
			<xsl:when test="/i:Invoice and normalize-space(/i:Invoice/cbc:CustomizationID) = 'urn:cen.eu:en16931:2017#compliant#urn:mfin.gov.hr:cius-2025:1.0#conformant#urn:mfin.gov.hr:ext-2025:1.0' and not(exists(//hrextac:HRFISK20Data))">
				<xsl:call-template name="InvoiceFixF2"/>
				<xsl:comment>
					Generated by Moj eRačun ENInvoice2ENF2 - InvoiceFixF2
				</xsl:comment>
			</xsl:when>
			<xsl:when test="/i:Invoice and normalize-space(/i:Invoice/cbc:CustomizationID) = 'urn:cen.eu:en16931:2017#compliant#urn:mfin.gov.hr:cius-2025:1.0#conformant#urn:mfin.gov.hr:ext-2025:1.0' and exists(//hrextac:HRFISK20Data)">
				<xsl:copy-of select="/i:Invoice" />
				<xsl:comment>
					Generated by Moj eRačun ENInvoice2ENF2 - copy
				</xsl:comment>
			</xsl:when>
			<xsl:otherwise>
				<xsl:message terminate="yes">ERROR: FORMAT NOT SUPPORTED
					  <xsl:value-of select="local-name()"/> !
				</xsl:message>
			
			</xsl:otherwise>
		</xsl:choose>


		
	</xsl:template>

	<xsl:template name="InvoiceFixF2">
		<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
			xmlns:hrextac="urn:mfin.gov.hr:schema:xsd:HRExtensionAggregateComponents-1" 
			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:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" 
			xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" 
			xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" 
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			>
			
			<xsl:call-template name="HRTaxSubtotalExtension" />

			<xsl:copy-of select="//i:Invoice/cbc:UBLVersionID"/>
			<cbc:CustomizationID>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:copy-of select="//i:Invoice/cbc:ProfileID"/>
			<xsl:copy-of select="//i:Invoice/cbc:ID"/>
			<xsl:copy-of select="//i:Invoice/cbc:CopyIndicator"/>
			
			<xsl:copy-of select="//i:Invoice/cbc:UUID"/>
			<xsl:copy-of select="//i:Invoice/cbc:IssueDate"/>
			<xsl:copy-of select="//i:Invoice/cbc:IssueTime"/>
			<xsl:copy-of select="//i:Invoice/cbc:DueDate"/>
			<xsl:copy-of select="//i:Invoice/cbc:InvoiceTypeCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:Note"/>
			<xsl:copy-of select="//i:Invoice/cbc:TaxPointDate"/>
			<xsl:copy-of select="//i:Invoice/cbc:DocumentCurrencyCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:TaxCurrencyCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:PricingCurrencyCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:PaymentCurrencyCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:PaymentAlternativeCurrencyCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:AccountingCostCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:AccountingCost"/>
			<xsl:copy-of select="//i:Invoice/cbc:LineCountNumeric"/>
			<xsl:copy-of select="//i:Invoice/cbc:BuyerReference"/>
			<xsl:copy-of select="//i:Invoice/cac:InvoicePeriod"/>
			<xsl:copy-of select="//i:Invoice/cac:OrderReference"/>
			<xsl:copy-of select="//i:Invoice/cac:BillingReference"/>
			<xsl:copy-of select="//i:Invoice/cac:DespatchDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:ReceiptDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:StatementDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:OriginatorDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:ContractDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:AdditionalDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:ProjectReference"/>

			<xsl:copy-of select="//i:Invoice/cac:AccountingSupplierParty"/>
			<xsl:copy-of select="//i:Invoice/cac:AccountingCustomerParty"/>
			<xsl:copy-of select="//i:Invoice/cac:PayeeParty"/>
			<xsl:copy-of select="//i:Invoice/cac:BuyerCustomerParty"/>
			<xsl:copy-of select="//i:Invoice/cac:SellerSupplierParty"/>
			<xsl:copy-of select="//i:Invoice/cac:TaxRepresentativeParty"/>
			<xsl:copy-of select="//i:Invoice/cac:Delivery"/>
			<xsl:copy-of select="//i:Invoice/cac:DeliveryTerms"/>
			<xsl:copy-of select="//i:Invoice/cac:PaymentMeans"/>
			<xsl:copy-of select="//i:Invoice/cac:PaymentTerms"/>

			<xsl:copy-of select="//i:Invoice/cac:AllowanceCharge"/>

			<xsl:copy-of select="//i:Invoice/cac:TaxTotal"/>
			<xsl:copy-of select="//i:Invoice/cac:LegalMonetaryTotal"/>
			<xsl:copy-of select="//i:Invoice/cac:InvoiceLine"/>
			
		</Invoice>
		
	</xsl:template>

	<xsl:template name="InvoiceFixEN">
		<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
			xmlns:hrextac="urn:mfin.gov.hr:schema:xsd:HRExtensionAggregateComponents-1" 
			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:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" 
			xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" 
			xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" 
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			>
			
			<xsl:call-template name="HRTaxSubtotalExtension" />

			<xsl:copy-of select="//i:Invoice/cbc:UBLVersionID"/>
			<cbc:CustomizationID>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:copy-of select="//i:Invoice/cbc:ProfileID"/>
			<xsl:copy-of select="//i:Invoice/cbc:ID"/>
			<xsl:copy-of select="//i:Invoice/cbc:CopyIndicator"/>
			
			<xsl:copy-of select="//i:Invoice/cbc:UUID"/>
			<xsl:copy-of select="//i:Invoice/cbc:IssueDate"/>
			<xsl:copy-of select="//i:Invoice/cbc:IssueTime"/>
			<xsl:copy-of select="//i:Invoice/cbc:DueDate"/>
			<xsl:copy-of select="//i:Invoice/cbc:InvoiceTypeCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:Note"/>
			<xsl:copy-of select="//i:Invoice/cbc:TaxPointDate"/>
			<xsl:copy-of select="//i:Invoice/cbc:DocumentCurrencyCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:TaxCurrencyCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:PricingCurrencyCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:PaymentCurrencyCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:PaymentAlternativeCurrencyCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:AccountingCostCode"/>
			<xsl:copy-of select="//i:Invoice/cbc:AccountingCost"/>
			<xsl:copy-of select="//i:Invoice/cbc:LineCountNumeric"/>
			<xsl:copy-of select="//i:Invoice/cbc:BuyerReference"/>
			<xsl:copy-of select="//i:Invoice/cac:InvoicePeriod"/>
			<xsl:copy-of select="//i:Invoice/cac:OrderReference"/>
			<xsl:copy-of select="//i:Invoice/cac:BillingReference"/>
			<xsl:copy-of select="//i:Invoice/cac:DespatchDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:ReceiptDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:StatementDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:OriginatorDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:ContractDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:AdditionalDocumentReference"/>
			<xsl:copy-of select="//i:Invoice/cac:ProjectReference"/>

			<xsl:copy-of select="//i:Invoice/cac:AccountingSupplierParty"/>
			<xsl:copy-of select="//i:Invoice/cac:AccountingCustomerParty"/>
			<xsl:copy-of select="//i:Invoice/cac:PayeeParty"/>
			<xsl:copy-of select="//i:Invoice/cac:BuyerCustomerParty"/>
			<xsl:copy-of select="//i:Invoice/cac:SellerSupplierParty"/>
			<xsl:copy-of select="//i:Invoice/cac:TaxRepresentativeParty"/>
			<xsl:copy-of select="//i:Invoice/cac:Delivery"/>
			<xsl:copy-of select="//i:Invoice/cac:DeliveryTerms"/>
			<xsl:copy-of select="//i:Invoice/cac:PaymentMeans"/>
			<xsl:copy-of select="//i:Invoice/cac:PaymentTerms"/>

			<xsl:copy-of select="//i:Invoice/cac:AllowanceCharge"/>

			<xsl:copy-of select="//i:Invoice/cac:TaxTotal"/>
			<xsl:copy-of select="//i:Invoice/cac:LegalMonetaryTotal"/>
			<xsl:copy-of select="//i:Invoice/cac:InvoiceLine"/>
			
		</Invoice>
		
	</xsl:template>

    
    <xsl:template name="HRTaxSubtotalExtension">
		<xsl:if test="exists(/i:Invoice/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="/i:Invoice/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="/i:Invoice/cac:InvoiceLine/cac:Item/cac:ClassifiedTaxCategory[normalize-space(cbc:ID) = 'E' or normalize-space(cbc:ID) = 'O'] | /i:Invoice/cac:AllowanceCharge//cac:TaxCategory[normalize-space(cbc:ID) = 'E' or normalize-space(cbc:ID) = 'O']" group-by="concat(cbc:Name, '+', cbc:TaxExemptionReason, '+', cbc:TaxExemptionReasonCode)" >
					<hrextac:HRTaxSubtotal>
						<cbc:TaxableAmount>
							<xsl:attribute name="currencyID"><xsl:value-of select="//i:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>

							<xsl:value-of select="format-number(sum(/i:Invoice/cac:InvoiceLine[concat(./cac:Item/cac:ClassifiedTaxCategory/cbc:Name, '+', ./cac:Item/cac:ClassifiedTaxCategory/cbc:TaxExemptionReason, '+', ./cac:Item/cac:ClassifiedTaxCategory/cbc:TaxExemptionReasonCode) = current-grouping-key()]/cbc:LineExtensionAmount) + sum(//i:Invoice/cac:AllowanceCharge[cbc:ChargeIndicator='true'][concat(.//cac:TaxCategory/cbc:Name, '+', .//cac:TaxCategory/cbc:TaxExemptionReason, '+', .//cac:TaxCategory/cbc:TaxExemptionReasonCode) = current-grouping-key()]/cbc:Amount) - sum(//i:Invoice/cac:AllowanceCharge[cbc:ChargeIndicator='false'][concat(.//cac:TaxCategory/cbc:Name, '+', .//cac:TaxCategory/cbc:TaxExemptionReason, '+', .//cac:TaxCategory/cbc:TaxExemptionReasonCode) = current-grouping-key()]/cbc:Amount), '#########0.00;-#########0.00')" />
						</cbc:TaxableAmount>
						<cbc:TaxAmount>
							<xsl:attribute name="currencyID"><xsl:value-of select="//i: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>
							<xsl:choose>
								<xsl:when test="normalize-space(current-group()[1]/cbc:TaxExemptionReason) != ''">
									<cbc:TaxExemptionReason>
										<xsl:value-of select="current-group()[1]/cbc:TaxExemptionReason" />
									</cbc:TaxExemptionReason>								
								</xsl:when>
								<xsl:when test="normalize-space(current-group()[1]/cbc:TaxExemptionReasonCode) != ''">
									<cbc:TaxExemptionReasonCode>
										<xsl:value-of select="current-group()[1]/cbc:TaxExemptionReasonCode" />
									</cbc:TaxExemptionReasonCode>								
								</xsl:when>
							</xsl:choose>

							<!--<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>
							<xsl:choose>
								<xsl:when test="exists(/i:Invoice/cac:InvoiceLine/cac:Item/cac:ClassifiedTaxCategory/cbc:TaxExemptionReason) or exists(/i:Invoice/cac:InvoiceLine/cac:Item/cac:ClassifiedTaxCategory/cbc:TaxExemptionReasonCode) or exists(/i:Invoice/cac:AllowanceCharge//cbc:TaxExemptionReason) or exists(/i:Invoice/cac:AllowanceCharge//cbc:TaxExemptionReasonCode)">
									<hrextac:HRTaxTotal>
										<cbc:TaxAmount>
											<xsl:attribute name="currencyID"><xsl:value-of select="//i:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
											<xsl:value-of select="'0.00'" />
										
										</cbc:TaxAmount>
		
										<xsl:comment>Stavke</xsl:comment>
										<xsl:copy-of select="$HRTaxSubTotalStavke" />
									</hrextac:HRTaxTotal>
									<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="//i:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
												<xsl:value-of select="format-number(//i: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="//i: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>
									<hrextac:HRTaxTotal>
										<cbc:TaxAmount>
											<xsl:attribute name="currencyID"><xsl:value-of select="//i:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
											<xsl:value-of select="'0.00'" />
										
										</cbc:TaxAmount>
										<xsl:copy-of select="$HRTaxSubTotal" />	
									</hrextac:HRTaxTotal>

								
									<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="//i:Invoice/cbc:DocumentCurrencyCode" /></xsl:attribute>
												<xsl:value-of select="format-number(//i: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="//i: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: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:choose>
					<xsl:when test="cbc:TaxExemptionReason and normalize-space(cbc:TaxExemptionReason) != ''">
						<xsl:apply-templates select="cbc:TaxExemptionReason" />
					</xsl:when>
					<xsl:when test="cbc:TaxExemptionReasonCode and normalize-space(cbc:TaxExemptionReasonCode) != ''">
						<xsl:apply-templates select="cbc:TaxExemptionReasonCode" />
					</xsl:when>
				</xsl:choose>
				 <!--<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 name="ProfileID">
		<cbc:ProfileID>
			<xsl:choose>
				<xsl:when test="cbc:ProfileID and substring(normalize-space(cbc:ProfileID), 1, 1) = 'P'">
					<xsl:value-of select="cbc:ProfileID" />
				</xsl:when>
				<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:stylesheet>
