<?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-12
Riješen problem kad račun ima višestruke PartyTaxScheme

--> 
 
 <!-- V2026-01-09
Stavio da ne generira cijeli KPD čvor ako nema vrijednosti KPD-a

-->
 
<!-- V2026-01-04
Ispravljeno da se šalju samo KPD klasifikacije (listID0'CG')

--> 
 
<!-- V2026-01-02
Trimanje InvoiceTypeCode
Maknut komentar sa fis:artiklHrOznakaKategorijaPdv

-->
 
<!-- V2026-01-01
Naziv artikla ograničnen na 100 znakova
Stavljena ograničenja i na ostale stringove prema xsd shemi

--> 
 
<!-- V2025-12-30
Ispravljen krivi mfin.gov.hr namespace za hr ekstenziju

-->
 
 <!-- V20205-12-18
Stavljene kontrole da ne kreira prazne nodove

-->
 
 <!-- V2025-12-17
Ispravljen bug gdje stavlja prazan fis:identifikatorPruzateljaPlatnihUsluga ako postoji cac:FinancialInstitutionBranch koji nema cbc:ID

-->

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:xs="http://www.w3.org/2001/XMLSchema" 
 xmlns:fn="http://www.w3.org/2005/xpath-functions"
 xmlns:fis="http://www.porezna-uprava.gov.hr/fin/2024/types/eFiskalizacija"
 xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" 
 xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
 xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
 xmlns:hrextac="urn:mfin.gov.hr:schema:xsd:HRExtensionAggregateComponents-1"
 xmlns:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
 exclude-result-prefixes="cac cbc i fis">


	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	
	

	<xsl:param name="vrstaERacuna">I</xsl:param>

	<xsl:variable name="customerCompanyId">
		<xsl:choose>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID[@schemeID='9934'] and (string-length(normalize-space(//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID)) = 11)">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and (string-length(normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)) = 11)">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" mode="SchemeID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID[starts-with(text(), '9934:')]">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID" mode="PartyID2CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID[starts-with(text(), 'HR')]">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID" mode="TaxID2CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
			</xsl:when>

		</xsl:choose>

	</xsl:variable>
	
	<xsl:variable name="supplierCompanyId">
		<xsl:choose>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID[@schemeID='9934'] and (string-length(normalize-space(//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID)) = 11)">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and (string-length(normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)) = 11)">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" mode="SchemeID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID[starts-with(text(), '9934:')]">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID" mode="PartyID2CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID[starts-with(text(), 'HR')]">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID" mode="TaxID2CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
			</xsl:when>

		</xsl:choose>

	</xsl:variable>

	
	<xsl:variable name="fiskID" select="concat('ID-', generate-id())"/>
	
	<xsl:template match="/">
		
		<fis:EvidentirajERacunZahtjev xmlns:fis="http://www.porezna-uprava.gov.hr/fin/2024/types/eFiskalizacija">
			 <xsl:attribute name="fis:id"> 
				 <xsl:value-of select="$fiskID" /> 
			 </xsl:attribute> 
			<fis:Zaglavlje>
				<fis:datumVrijemeSlanja>
					<xsl:value-of select="format-dateTime(current-dateTime(),'[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01].[f0001]')"/>
				</fis:datumVrijemeSlanja>
				<fis:vrstaERacuna><xsl:value-of select="$vrstaERacuna" /> </fis:vrstaERacuna>
			</fis:Zaglavlje>
				
			<xsl:apply-templates select="//i:Invoice" />
		</fis:EvidentirajERacunZahtjev>
	</xsl:template>
	
	<xsl:template match="i:Invoice" >
		<fis:ERacun>
			<xsl:apply-templates select="cbc:ID" mode="InvoiceID" />
			<xsl:apply-templates select="cbc:IssueDate" />
			<xsl:apply-templates select="cbc:InvoiceTypeCode" />
			<xsl:apply-templates select="cbc:DocumentCurrencyCode" />
			<xsl:apply-templates select="cbc:DueDate" />
			
			<xsl:call-template name="ProfileID" />
			
			<xsl:apply-templates select="cac:ContractDocumentReference" />
			
			<xsl:apply-templates select="cac:Delivery/cbc:ActualDeliveryDate" />
			
			<xsl:apply-templates select="cac:BillingReference/cac:InvoiceDocumentReference" />
			
			<fis:Izdavatelj>		
				<xsl:apply-templates select="cac:AccountingSupplierParty/cac:Party" mode="TaxParty" />
				
				<xsl:call-template name="oibOperatera" />
				
			</fis:Izdavatelj>
			
			<fis:Primatelj>
				<xsl:apply-templates select="cac:AccountingCustomerParty/cac:Party" mode="TaxParty" />
			</fis:Primatelj>
			
			<xsl:apply-templates select="cac:PaymentMeans/cac:PayeeFinancialAccount" />
			
			<xsl:apply-templates select="cac:LegalMonetaryTotal" />
			
			<xsl:call-template name="raspodjelaPDV" />
			
			<xsl:apply-templates select="cac:AllowanceCharge[normalize-space(cbc:ChargeIndicator) = 'false']" mode="DocumentAllowance" />
			
			<xsl:apply-templates select="cac:AllowanceCharge[normalize-space(cbc:ChargeIndicator) = 'true']" mode="DocumentCharge" />
			
			<xsl:apply-templates select="cac:InvoiceLine" />
			
			<xsl:call-template name="CopyIndicator" />
			
		</fis:ERacun>
	
	
	</xsl:template>
	
	<xsl:template name="CopyIndicator" >
		<fis:indikatorKopije>
			<xsl:choose>
				<xsl:when test="cbc:CopyIndicator and fn:normalize-space(fn:lower-case(cbc:CopyIndicator)) = 'true'">true</xsl:when>
				<xsl:otherwise>false</xsl:otherwise>
			</xsl:choose>
		</fis:indikatorKopije>
	
	
	</xsl:template>
	
	<xsl:template match="cac:InvoiceLine">
		<fis:StavkaERacuna>
			<xsl:apply-templates select="cbc:InvoicedQuantity" mode="InvoiceLine"/>
			<xsl:apply-templates select="cbc:InvoicedQuantity/@unitCode" mode="InvoiceLine"/>
			
			<xsl:apply-templates select="cbc:LineExtensionAmount" mode="InvoiceLine"/>
			
			<xsl:apply-templates select="cac:Price/cbc:PriceAmount" mode="InvoiceLine"/>
			
			<xsl:apply-templates select="cac:Price/cac:AllowanceCharge/cbc:BaseAmount" mode="PriceAmount" />
			
			<xsl:apply-templates select="cac:Price/cbc:BaseQuantity" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:Price/cbc:BaseQuantity/@unitCode" mode="PriceAmount"/>
			
			<xsl:apply-templates select="cac:Item" mode="InvoiceLine"/>
		
		</fis:StavkaERacuna>
	</xsl:template>
	
	<xsl:template match="cac:Item" mode="InvoiceLine" >
		<xsl:apply-templates select="cac:ClassifiedTaxCategory" mode="InvoiceLine"/>
		
		<xsl:apply-templates select="cbc:Name" mode="InvoiceLine"/>
		
		<xsl:apply-templates select="cbc:Description" mode="InvoiceLine"/>
		
		<xsl:apply-templates select="cac:ClassifiedTaxCategory/cbc:Name" mode="InvoiceLineTax"/> 
		
		<xsl:apply-templates select="cac:CommodityClassification[cbc:ItemClassificationCode/@listID='CG']" mode="InvoiceLine"/>
	
	</xsl:template>
	
	<xsl:template match="cac:CommodityClassification" mode="InvoiceLine" >
		<xsl:if test="cbc:ItemClassificationCode and fn:normalize-space(cbc:ItemClassificationCode) != ''">
			<fis:ArtiklIdentifikatorKlasifikacija>
				<xsl:apply-templates select="cbc:ItemClassificationCode" mode="CommodityClassification" />
				<xsl:apply-templates select="cbc:ItemClassificationCode/@listID" mode="CommodityClassification" />
				<xsl:apply-templates select="cbc:ItemClassificationCode/@listVersionID" mode="CommodityClassification" />
			
			</fis:ArtiklIdentifikatorKlasifikacija>
		
		</xsl:if>
	
	</xsl:template>
	

	<xsl:template match="@listVersionID" mode="CommodityClassification" >
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:verzijaSheme>
				<xsl:value-of select="substring(., 1, 10)" />
			</fis:verzijaSheme>
		
		</xsl:if>
	</xsl:template>

	
	<xsl:template match="@listID" mode="CommodityClassification" >
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:identifikatorSheme>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:identifikatorSheme>
		</xsl:if>
	</xsl:template>


	<xsl:template match="cbc:ItemClassificationCode" mode="CommodityClassification" >
		<fis:identifikatorKlasifikacije>
			<xsl:value-of select="substring(normalize-space(.), 1, 10)" />
		</fis:identifikatorKlasifikacije>
	</xsl:template>

	 <xsl:template match="cbc:Name" mode="InvoiceLineTax" > 
		 <fis:artiklHrOznakaKategorijaPdv> 
			 <xsl:value-of select="fn:normalize-space(.)" />			 
		
		 </fis:artiklHrOznakaKategorijaPdv> 
	
	 </xsl:template> 	
	

	<xsl:template match="cbc:Description" mode="InvoiceLine" >
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:artiklOpis>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:artiklOpis>
		
		</xsl:if>
	</xsl:template>

	
	<xsl:template match="cbc:Name" mode="InvoiceLine" >
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:artiklNaziv>
				<xsl:value-of select="substring(fn:normalize-space(.), 1, 100)" />
			</fis:artiklNaziv>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:ClassifiedTaxCategory" mode="InvoiceLine" >
		<fis:artiklKategorijaPdv>
			<xsl:value-of select="cbc:ID" />
		</fis:artiklKategorijaPdv>
		
		<fis:artiklStopaPdv>
			<xsl:choose>
				<xsl:when test="cbc:Percent and fn:normalize-space(cbc:Percent) != ''">
					<xsl:value-of select="normalize-space(cbc:Percent)" />		
				</xsl:when>
				<xsl:otherwise>0</xsl:otherwise>
			</xsl:choose>
			
		</fis:artiklStopaPdv>
	
	
	</xsl:template>

	<xsl:template match="@unitCode" mode="PriceAmount">
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:artiklJedinicaMjereZaOsnovnuKolicinu>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:artiklJedinicaMjereZaOsnovnuKolicinu>
		</xsl:if>
	
	</xsl:template>

	<xsl:template match="cbc:BaseAmount" mode="PriceAmount" >
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:artiklBrutoCijena>
				<xsl:value-of select="fn:normalize-space(.)" />		
				
			</fis:artiklBrutoCijena>
		</xsl:if>
	
	</xsl:template>
	
	
	<xsl:template match="cbc:BaseQuantity" mode="InvoiceLine" >
		<fis:artiklOsnovnaKolicina>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:artiklOsnovnaKolicina>
	</xsl:template>
	
	
	<xsl:template match="cbc:PriceAmount" mode="InvoiceLine" >
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:artiklNetoCijena>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:artiklNetoCijena>
		</xsl:if>
	</xsl:template>

	<xsl:template match="@unitCode" mode="InvoiceLine">
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:jedinicaMjere>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:jedinicaMjere>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cbc:LineExtensionAmount" mode="InvoiceLine" >
		<fis:neto>		
			<xsl:value-of select="fn:normalize-space(.)" />

		</fis:neto>
	
	
	</xsl:template>
	
	<xsl:template match="cbc:InvoicedQuantity" mode="InvoiceLine">
		<fis:kolicina>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:kolicina>
	
	</xsl:template>


	<xsl:template match="cac:AllowanceCharge" mode="DocumentCharge" >
		<fis:DokumentTrosak>
			<fis:iznosTrosak>
				<xsl:value-of select="cbc:Amount" />
			</fis:iznosTrosak>
	
			<fis:kategorijaPdv>
				<xsl:value-of select="cac:TaxCategory/cbc:ID" />
			</fis:kategorijaPdv>
			
			<xsl:apply-templates select="cac:TaxCategory/cbc:Name" mode="DocumentCharge" />
			
			<xsl:apply-templates select="cac:TaxCategory/cbc:Percent" mode="AllowanceCharge" />
			
			<xsl:apply-templates select="cac:TaxCategory/cbc:TaxExemptionReason" mode="DocumentCharge" />
			<xsl:apply-templates select="cac:TaxCategory/cbc:TaxExemptionReasonCode" mode="DocumentCharge" />
		</fis:DokumentTrosak>
	
	</xsl:template>

	<xsl:template match="cbc:TaxExemptionReasonCode" mode="DocumentCharge" >

		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:razlogOslobodenjaPdv>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:razlogOslobodenjaPdv>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxExemptionReason" mode="DocumentCharge" >
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:tekstRazlogaOslobodenjaPdv>
				<xsl:value-of select="substring(fn:normalize-space(.), 1, 1024)" />
			</fis:tekstRazlogaOslobodenjaPdv>
		</xsl:if>
	
	</xsl:template>

	<xsl:template match="cbc:Name" mode="DocumentCharge">
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:hrOznakaPorezneKategorije>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:hrOznakaPorezneKategorije>
		</xsl:if>
	
	</xsl:template>

	
	<xsl:template match="cac:AllowanceCharge" mode="DocumentAllowance" >
		<fis:DokumentPopust>
			<fis:iznosPopust>
				<xsl:value-of select="cbc:Amount" />
			</fis:iznosPopust>
	
			<fis:kategorijaPdv>
				<xsl:value-of select="cac:TaxCategory/cbc:ID" />
			</fis:kategorijaPdv>
			
			<xsl:apply-templates select="cac:TaxCategory/cbc:Percent" mode="AllowanceCharge" />
			<xsl:apply-templates select="cbc:AllowanceChargeReason" mode="Allowance" />
			<xsl:apply-templates select="cbc:AllowanceChargeReasonCode" mode="Allowance" />
		
		</fis:DokumentPopust>
	
	</xsl:template>

	<xsl:template match="cbc:AllowanceChargeReasonCode" mode="Allowance" >

		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:razlogPopusta>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:razlogPopusta>
		</xsl:if>
	
	</xsl:template>

	
	<xsl:template match="cbc:AllowanceChargeReason" mode="Allowance" >
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:tekstRazlogaPopusta>
				<xsl:value-of select="substring(fn:normalize-space(.), 1, 1024)" />
			</fis:tekstRazlogaPopusta>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cbc:Percent" mode="AllowanceCharge" >
		<fis:stopaPdv>
			<xsl:value-of select="fn:normalize-space(.)" />
		
		</fis:stopaPdv>
	
	</xsl:template>
	
	<xsl:template name="raspodjelaPDV" >
		<xsl:apply-templates select="/*/cac:TaxTotal/cac:TaxSubtotal[cac:TaxCategory/fn:normalize-space(cbc:ID) != 'E' and cac:TaxCategory/fn:normalize-space(cbc:ID) != 'O']" mode="TaxTotal"/>
		<xsl:choose>
			<xsl:when test="exists(//hrextac:HRTaxSubtotal[hrextac:HRTaxCategory/fn:normalize-space(cbc:ID) = 'E' or hrextac:HRTaxCategory/fn:normalize-space(cbc:ID) = 'O'])">
				<xsl:apply-templates select="//hrextac:HRTaxSubtotal[hrextac:HRTaxCategory/fn:normalize-space(cbc:ID) = 'E' or hrextac:HRTaxCategory/fn:normalize-space(cbc:ID) = 'O']" mode="TaxTotal"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="/*/cac:TaxTotal/cac:TaxSubtotal[cac:TaxCategory/fn:normalize-space(cbc:ID) = 'E' or cac:TaxCategory/fn:normalize-space(cbc:ID) = 'O']" mode="TaxTotal"/>
			</xsl:otherwise>
		</xsl:choose>
	
	</xsl:template>
	
	<xsl:template match="cac:TaxSubtotal" mode="TaxTotal">
		<fis:RaspodjelaPdv>
			<fis:kategorijaPdv>
				<xsl:value-of select="cac:TaxCategory/cbc:ID" />
			</fis:kategorijaPdv>
			
			<fis:oporeziviIznos>
				<xsl:value-of select="cbc:TaxableAmount" />
			</fis:oporeziviIznos>
			
			<fis:iznosPoreza>
				<xsl:value-of select="cbc:TaxAmount" />
			</fis:iznosPoreza>
			
			<xsl:apply-templates select="cac:TaxCategory/cbc:Percent" mode="TaxTotal"/>
			<xsl:apply-templates select="cac:TaxCategory/cbc:TaxExemptionReasonCode" mode="TaxTotal"/>
			<xsl:apply-templates select="cac:TaxCategory/cbc:TaxExemptionReason" mode="TaxTotal"/>
			<xsl:apply-templates select="cac:TaxCategory/cbc:Name" mode="TaxTotal"/>
		
		</fis:RaspodjelaPdv>
	
	</xsl:template>

	<xsl:template match="hrextac:HRTaxSubtotal" mode="TaxTotal">
		<fis:RaspodjelaPdv>
			<fis:kategorijaPdv>
				<xsl:value-of select="hrextac:HRTaxCategory/cbc:ID" />
			</fis:kategorijaPdv>
			
			<fis:oporeziviIznos>
				<xsl:value-of select="cbc:TaxableAmount" />
			</fis:oporeziviIznos>
			
			<fis:iznosPoreza>
				<xsl:value-of select="cbc:TaxAmount" />
			</fis:iznosPoreza>
			
			<xsl:apply-templates select="hrextac:HRTaxCategory/cbc:Percent" mode="TaxTotal"/>
			<xsl:apply-templates select="hrextac:HRTaxCategory/cbc:TaxExemptionReasonCode" mode="TaxTotal"/>
			<xsl:apply-templates select="hrextac:HRTaxCategory/cbc:TaxExemptionReason" mode="TaxTotal"/>
			<xsl:apply-templates select="cac:TaxCategory/cbc:Name" mode="TaxTotal"/>
		
		</fis:RaspodjelaPdv>
	
	</xsl:template>
	
	<xsl:template match="cbc:Name" mode="TaxTotal">
		<fis:hrOznakaKategorijaPdv>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:hrOznakaKategorijaPdv>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxExemptionReason" mode="TaxTotal">
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:tekstRazlogaOslobodenja>
				<xsl:value-of select="substring(., 1, 1024)" />
			</fis:tekstRazlogaOslobodenja>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxExemptionReasonCode" mode="TaxTotal">
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:razlogOslobodenja>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:razlogOslobodenja>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cbc:Percent" mode="TaxTotal">
		<fis:stopa>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:stopa>
	
	</xsl:template>
	
	<xsl:template match="cac:LegalMonetaryTotal">
		<fis:DokumentUkupanIznos>
			<xsl:apply-templates select="cbc:LineExtensionAmount" />
			<xsl:apply-templates select="cbc:AllowanceTotalAmount" />
			<xsl:apply-templates select="cbc:ChargeTotalAmount" />
			<xsl:apply-templates select="cbc:TaxExclusiveAmount" />
			<xsl:apply-templates select="/i:Invoice/cac:TaxTotal/cbc:TaxAmount" mode="LegalMonetaryTotal" />
			<xsl:apply-templates select="cbc:TaxInclusiveAmount" />
			<xsl:apply-templates select="cbc:PrepaidAmount" />
			<xsl:apply-templates select="cbc:PayableAmount" />
		
		</fis:DokumentUkupanIznos>
	
	</xsl:template>
	
	<xsl:template match="cbc:PayableAmount" >
		<fis:iznosKojiDospijevaZaPlacanje>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:iznosKojiDospijevaZaPlacanje>
	
	</xsl:template>
	
	<xsl:template match="cbc:PrepaidAmount">
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:placeniIznos>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:placeniIznos>
		
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxInclusiveAmount">
		<fis:iznosSPdv>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:iznosSPdv>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxAmount" mode="LegalMonetaryTotal">
		<fis:pdv>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:pdv>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxExclusiveAmount">
		<fis:iznosBezPdv>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:iznosBezPdv>

	</xsl:template>

	<xsl:template match="cbc:ChargeTotalAmount">
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:trosak>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:trosak>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cbc:AllowanceTotalAmount">
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:popust>
				<xsl:value-of select="fn:normalize-space(.)" />
			</fis:popust>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cbc:LineExtensionAmount">	
		<fis:neto>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:neto>
	
	</xsl:template>
	
	<xsl:template match="cac:PayeeFinancialAccount">
        <fis:PrijenosSredstava>
            <fis:identifikatorRacunaZaPlacanje>
				<xsl:value-of select="substring(cbc:ID, 1, 34)" />
            </fis:identifikatorRacunaZaPlacanje>
            
            <xsl:apply-templates select="cbc:Name" mode="PayeeFinancialAccount"/>
            <xsl:apply-templates select="cac:FinancialInstitutionBranch" mode="PayeeFinancialAccount"/>
        </fis:PrijenosSredstava>
	
	</xsl:template>

	<xsl:template match="cac:FinancialInstitutionBranch" mode="PayeeFinancialAccount">
		<xsl:if test="cbc:ID and fn:normalize-space(cbc:ID) != ''"> 
			<fis:identifikatorPruzateljaPlatnihUsluga>
				<xsl:value-of select="substring(cbc:ID, 1, 25)" />
			</fis:identifikatorPruzateljaPlatnihUsluga>
		</xsl:if>
	</xsl:template>


	
	<xsl:template match="cbc:Name" mode="PayeeFinancialAccount">
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:nazivRacunaZaPlacanje>
				<xsl:value-of select="substring(., 1, 35)" />
			</fis:nazivRacunaZaPlacanje>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cac:Party" mode="TaxParty">
		<fis:ime>
			<xsl:choose>
				<xsl:when test="cac:PartyLegalEntity/cbc:RegistrationName and (fn:normalize-space(cac:PartyLegalEntity/cbc:RegistrationName) != '')"  >
					<xsl:value-of select="cac:PartyLegalEntity/cbc:RegistrationName" />
				</xsl:when>
				<xsl:when test="cac:PartyName/cbc:Name and (fn:normalize-space(cac:PartyName/cbc:Name)!= '')">
					<xsl:value-of select="cac:PartyName/cbc:Name" />
				</xsl:when>
			</xsl:choose>
		
		</fis:ime>
		
		<fis:oibPorezniBroj>
			<xsl:choose>
				<xsl:when test="cac:PartyTaxScheme[cac:TaxScheme/cbc:ID='VAT']/cbc:CompanyID and starts-with(cac:PartyTaxScheme[cac:TaxScheme/cbc:ID='VAT']/cbc:CompanyID, 'HR')">
					<xsl:value-of select="fn:substring(cac:PartyTaxScheme[cac:TaxScheme/cbc:ID='VAT']/cbc:CompanyID, 3, 23)" />
				</xsl:when>
				<xsl:when test="cac:PartyTaxScheme[cac:TaxScheme/cbc:ID='FRE']/cbc:CompanyID and (fn:normalize-space(cac:PartyTaxScheme[cac:TaxScheme/cbc:ID='FRE']/cbc:CompanyID) != '')">
					<xsl:value-of select="substring(fn:normalize-space(cac:PartyTaxScheme[cac:TaxScheme/cbc:ID='FRE']/cbc:CompanyID), 1, 20)" />
				</xsl:when>
				<xsl:when test="cbc:EndpointID[@schemeID='9934']">
					<xsl:value-of select="cbc:EndpointID"/>
				</xsl:when>
			</xsl:choose>			
		</fis:oibPorezniBroj>
						
	</xsl:template>
	
	<xsl:template name="oibOperatera">
		<fis:oibOperatera>
			<xsl:choose>
				<xsl:when test="/i:Invoice/cac:AccountingSupplierParty/cac:SellerContact/cbc:ID and fn:normalize-space(/i:Invoice/cac:AccountingSupplierParty/cac:SellerContact/cbc:ID) != ''">
					<xsl:value-of select="substring(fn:normalize-space(//cac:AccountingSupplierParty/cac:SellerContact/cbc:ID), 1, 20)" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="substring($supplierCompanyId, 1, 20)" />
				</xsl:otherwise>
			</xsl:choose>
		</fis:oibOperatera>
	</xsl:template>
	
	<xsl:template match="cac:InvoiceDocumentReference">
		<fis:PrethodniERacun>
			<fis:brojDokumenta>
				<xsl:value-of select="substring(cbc:ID, 1, 100)" />
			</fis:brojDokumenta>
			<fis:datumIzdavanja>
				<xsl:value-of select="cbc:IssueDate" />
			</fis:datumIzdavanja>
		
		</fis:PrethodniERacun>
	
	
	</xsl:template>
	
	<xsl:template match="cac:ContractDocumentReference">
		<xsl:if test="fn:normalize-space(.) != ''">
			<fis:referencaNaUgovor>
				<xsl:value-of select="substring(cbc:ID, 1, 100)" />
			</fis:referencaNaUgovor>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template name="ProfileID">
		<fis:vrstaPoslovnogProcesa>
			<xsl:choose>
				<xsl:when test="cbc:ProfileID and (fn:normalize-space(cbc:ProfileID) != '') and (fn:upper-case(fn:substring(fn:normalize-space(cbc:ProfileID), 1, 1)) = 'P')">
					<xsl:value-of select="fn:upper-case(fn:normalize-space(cbc:ProfileID))" />
				</xsl:when>
				<xsl:otherwise>P99</xsl:otherwise>
			</xsl:choose>
		
		</fis:vrstaPoslovnogProcesa>
	
	</xsl:template>
	
	<xsl:template match="cbc:ActualDeliveryDate" >
		<fis:datumIsporuke>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:datumIsporuke>
	
	</xsl:template>
	
	<xsl:template match="cbc:DueDate">
		<fis:datumDospijecaPlacanja>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:datumDospijecaPlacanja>
	
	</xsl:template>
	
	<xsl:template match="cbc:DocumentCurrencyCode" >
		<fis:valutaERacuna>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:valutaERacuna>
	
	</xsl:template>
	
	<xsl:template match="cbc:InvoiceTypeCode" >
		<fis:vrstaDokumenta>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:vrstaDokumenta>
	
	</xsl:template>
	
	<xsl:template match="cbc:IssueDate">
		<fis:datumIzdavanja>
			<xsl:value-of select="fn:normalize-space(.)" />
		</fis:datumIzdavanja>
	</xsl:template>
	
	<xsl:template match="cbc:ID" mode="InvoiceID" >
		<fis:brojDokumenta>
			<xsl:value-of select="substring(., 1, 100)" />
		</fis:brojDokumenta>
	</xsl:template>

</xsl:stylesheet>
