<?xml version="1.0" encoding="UTF-8"?>
<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:ns1="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:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
 exclude-result-prefixes="cac cbc i ns1">


	<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" >
		<xsl:choose>
			<xsl:when test="$vrstaERacuna = 'U'">
				<xsl:value-of select="fn:concat($customerCompanyId, ':', format-dateTime(current-dateTime(),'[Y0001][M01][D01]T[H01][m01][s][f]'))" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="fn:concat($supplierCompanyId, ':', format-dateTime(current-dateTime(),'[Y0001][M01][D01]T[H01][m01][s][f]'))" />
			</xsl:otherwise>
		</xsl:choose>
		
	</xsl:variable>
	
	<xsl:template match="/">
		
		<ns1:EvidentirajERacunZahtjev xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" xmlns:ns1="http://www.porezna-uprava.gov.hr/fin/2024/types/eFiskalizacija">
			<xsl:attribute name="ns1:id">
				<xsl:value-of select="$fiskID" />
			</xsl:attribute>
			<ns1:Zaglavlje>
				<ns1:datumVrijemeSlanja>
					<xsl:value-of select="format-dateTime(current-dateTime(),'[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01].[f0001]')"/>
				</ns1:datumVrijemeSlanja>
				<ns1:vrstaERacuna><xsl:value-of select="$vrstaERacuna" /> </ns1:vrstaERacuna>
			</ns1:Zaglavlje>
				
			<xsl:apply-templates select="//i:Invoice" />
			
			<dsig:Signature>

				<xsl:comment>
					TODO - ovdje dodati potpis
				</xsl:comment>		
			</dsig:Signature>
		
		</ns1:EvidentirajERacunZahtjev>

		<xsl:comment>
			Generated by Moj eRačun EuropeanNormInvoice2F2.0 transformation V2025-08-18
		</xsl:comment>
			
	</xsl:template>


	<xsl:template match="i:Invoice" >
		<ns1: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" />
			
			<ns1:Izdavatelj>		
				<xsl:apply-templates select="cac:AccountingSupplierParty/cac:Party" mode="TaxParty" />
				
				<xsl:call-template name="oibOperatera" />
				
			</ns1:Izdavatelj>
			
			<ns1:Primatelj>
				<xsl:apply-templates select="cac:AccountingCustomerParty/cac:Party" mode="TaxParty" />
			</ns1:Primatelj>
			
			<xsl:apply-templates select="cac:PaymentMeans/cac:PayeeFinancialAccount" />
			
			<xsl:apply-templates select="cac:LegalMonetaryTotal" />
			
			<xsl:apply-templates select="cac:TaxTotal/cac:TaxSubtotal" mode="TaxTotal"/>
			
			<xsl:apply-templates select="cac:InvoiceLine" />
			
			<xsl:call-template name="CopyIndicator" />
			
		</ns1:ERacun>
	
	
	</xsl:template>
	
	<xsl:template name="CopyIndicator" >
		<ns1: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>
		</ns1:indikatorKopije>
	
	
	</xsl:template>
	
	<xsl:template match="cac:InvoiceLine">
		<ns1:StavkaERacuna>
			<xsl:apply-templates select="cbc:InvoicedQuantity" mode="InvoiceLine"/>
			<xsl:apply-templates select="cbc:InvoicedQuantity/@unitCode" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:Price/cbc:PriceAmount" mode="InvoiceLine"/>
			<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"/>
		
		</ns1: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="cac:CommodityClassification" mode="InvoiceLine"/>
	
	</xsl:template>
	
	<xsl:template match="cac:CommodityClassification" mode="InvoiceLine" >
		<ns1: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" />
		
		</ns1:ArtiklIdentifikatorKlasifikacija>
	
	</xsl:template>

	<xsl:template match="@listVersionID" mode="CommodityClassification" >
		<ns1:verzijaSheme>
			<xsl:value-of select="." />
		</ns1:verzijaSheme>
	</xsl:template>

	
	<xsl:template match="@listID" mode="CommodityClassification" >
		<ns1:identifikatorSheme>
			<xsl:value-of select="." />
		</ns1:identifikatorSheme>
	</xsl:template>


	<xsl:template match="cbc:ItemClassificationCode" mode="CommodityClassification" >
		<ns1:identifikatorKlasifikacije>
			<xsl:value-of select="." />
		</ns1:identifikatorKlasifikacije>
	</xsl:template>
	
	<xsl:template match="cbc:Name" mode="InvoiceLine" >
		<ns1:artiklNaziv>
			<xsl:value-of select="." />
		</ns1:artiklNaziv>
	</xsl:template>
	
	<xsl:template match="cac:ClassifiedTaxCategory" mode="InvoiceLine" >
		<ns1:artiklKategorijaPdv>
			<xsl:value-of select="cbc:ID" />
		</ns1:artiklKategorijaPdv>
		
		<ns1:artiklStopaPdv>
			<xsl:value-of select="cbc:Percent" />		
		</ns1:artiklStopaPdv>
	
	
	</xsl:template>

	<xsl:template match="@unitCode" mode="PriceAmount">
		<ns1:artiklJedinicaMjereZaOsnovnuKolicinu>
			<xsl:value-of select="." />
		</ns1:artiklJedinicaMjereZaOsnovnuKolicinu>
	
	</xsl:template>
	
	<xsl:template match="cbc:BaseQuantity" mode="InvoiceLine" >
		<ns1:artiklOsnovnaKolicina>
			<xsl:value-of select="." />
		</ns1:artiklOsnovnaKolicina>
	</xsl:template>
	
	
	<xsl:template match="cbc:PriceAmount" mode="InvoiceLine" >
		<ns1:artiklNetoCijena>
			<xsl:value-of select="." />
		</ns1:artiklNetoCijena>
	</xsl:template>

	<xsl:template match="@unitCode" mode="InvoiceLine">
		<ns1:jedinicaMjere>
			<xsl:value-of select="." />
		</ns1:jedinicaMjere>
	
	</xsl:template>
	
	<xsl:template match="cbc:InvoicedQuantity" mode="InvoiceLine">
		<ns1:kolicina>
			<xsl:value-of select="." />
		</ns1:kolicina>
	
	</xsl:template>
	
	<xsl:template match="cac:TaxSubtotal" mode="TaxTotal">
		<ns1:RaspodjelaPdv>
			<ns1:kategorijaPdv>
				<xsl:value-of select="cac:TaxCategory/cbc:ID" />
			</ns1:kategorijaPdv>
			
			<ns1:oporeziviIznos>
				<xsl:value-of select="cbc:TaxableAmount" />
			</ns1:oporeziviIznos>
			
			<ns1:iznosPoreza>
				<xsl:value-of select="cbc:TaxAmount" />
			</ns1: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"/>
		
		</ns1:RaspodjelaPdv>
	
	</xsl:template>

	<xsl:template match="cbc:TaxExemptionReason" mode="TaxTotal">
		<ns1:tekstRazlogaOslobodenja>
			<xsl:value-of select="." />
		</ns1:tekstRazlogaOslobodenja>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxExemptionReasonCode" mode="TaxTotal">
		<ns1:razlogOslobodenja>
			<xsl:value-of select="." />
		</ns1:razlogOslobodenja>
	
	</xsl:template>
	
	<xsl:template match="cbc:Percent" mode="TaxTotal">
		<ns1:stopa>
			<xsl:value-of select="." />
		</ns1:stopa>
	
	</xsl:template>
	
	<xsl:template match="cac:LegalMonetaryTotal">
		<ns1:DokumentUkupanIznos>
			<xsl:apply-templates select="cbc:LineExtensionAmount" />
			<xsl:apply-templates select="cbc:AllowanceTotalAmount" />
			<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" />
		
		</ns1:DokumentUkupanIznos>
	
	</xsl:template>
	
	<xsl:template match="cbc:PayableAmount" >
		<ns1:iznosKojiDospijevaZaPlacanje>
			<xsl:value-of select="." />
		</ns1:iznosKojiDospijevaZaPlacanje>
	
	</xsl:template>
	
	<xsl:template match="cbc:PrepaidAmount">
		<ns1:placeniIznos>
			<xsl:value-of select="." />
		</ns1:placeniIznos>
	
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxInclusiveAmount">
		<ns1:iznosSPdv>
			<xsl:value-of select="." />
		</ns1:iznosSPdv>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxAmount" mode="LegalMonetaryTotal">
		<ns1:pdv>
			<xsl:value-of select="." />
		</ns1:pdv>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxExclusiveAmount">
		<ns1:iznosBezPdv>
			<xsl:value-of select="." />
		</ns1:iznosBezPdv>

	</xsl:template>
	
	<xsl:template match="cbc:AllowanceTotalAmount">
		<ns1:popust>
			<xsl:value-of select="." />
		</ns1:popust>
	
	</xsl:template>
	
	<xsl:template match="cbc:LineExtensionAmount">	
		<ns1:neto>
			<xsl:value-of select="." />
		</ns1:neto>
	
	</xsl:template>
	
	<xsl:template match="cac:PayeeFinancialAccount">
        <ns1:PrijenosSredstava>
            <ns1:identifikatorRacunaZaPlacanje>
				<xsl:value-of select="cbc:ID" />
            </ns1:identifikatorRacunaZaPlacanje>
            
            <xsl:apply-templates select="cbc:Name" mode="PayeeFinancialAccount"/>
            <xsl:apply-templates select="cac:FinancialInstitutionBranch" mode="PayeeFinancialAccount"/>
        </ns1:PrijenosSredstava>
	
	</xsl:template>

	<xsl:template match="cac:FinancialInstitutionBranch" mode="PayeeFinancialAccount">
		<ns1:identifikatorPruzateljaPlatnihUsluga>
			<xsl:value-of select="cbc:ID" />
		</ns1:identifikatorPruzateljaPlatnihUsluga>
	
	</xsl:template>


	
	<xsl:template match="cbc:Name" mode="PayeeFinancialAccount">
		<ns1:nazivRacunaZaPlacanje>
			<xsl:value-of select="." />
		</ns1:nazivRacunaZaPlacanje>
	
	</xsl:template>
	
	<xsl:template match="cac:Party" mode="TaxParty">
		<ns1: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>
		
		</ns1:ime>
		
		<ns1:oibPorezniBroj>
			<xsl:choose>
				<xsl:when test="cac:PartyTaxScheme/cbc:CompanyID and starts-with(cac:PartyTaxScheme/cbc:CompanyID, 'HR')">
					<xsl:value-of select="fn:substring(cac:PartyTaxScheme/cbc:CompanyID, 3)" />
				</xsl:when>
				<xsl:when test="cac:PartyTaxScheme/cbc:CompanyID and (fn:normalize-space(cac:PartyTaxScheme/cbc:CompanyID) != '')">
					<xsl:value-of select="fn:normalize-space(cac:PartyTaxScheme/cbc:CompanyID)" />
				</xsl:when>
				<xsl:when test="cbc:EndpointID[@schemeID='9934']">
					<xsl:value-of select="cbc:EndpointID"/>
				</xsl:when>
			</xsl:choose>			
		</ns1:oibPorezniBroj>
						
	</xsl:template>
	
	<xsl:template name="oibOperatera">
		<ns1: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="fn:normalize-space(/i:Invoice/cac:AccountingSupplierParty/cac:SellerContact/cbc:ID)" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$supplierCompanyId" />
				</xsl:otherwise>
			</xsl:choose>
		</ns1:oibOperatera>
	</xsl:template>
	
	<xsl:template match="cac:InvoiceDocumentReference">
		<ns1:PrethodniERacun>
			<ns1:brojDokumenta>
				<xsl:value-of select="cbc:ID" />
			</ns1:brojDokumenta>
			<ns1:datumIzdavanja>
				<xsl:value-of select="cbc:IssueDate" />
			</ns1:datumIzdavanja>
		
		</ns1:PrethodniERacun>
	
	
	</xsl:template>
	
	<xsl:template match="cac:ContractDocumentReference">
		<ns1:referencaNaUgovor>
			<xsl:value-of select="cbc:ID" />
		</ns1:referencaNaUgovor>
	
	</xsl:template>
	
	<xsl:template name="ProfileID">
		<ns1: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>
		
		</ns1:vrstaPoslovnogProcesa>
	
	</xsl:template>
	
	<xsl:template match="cbc:ActualDeliveryDate" >
		<ns1:datumIsporuke>
			<xsl:value-of select="." />
		</ns1:datumIsporuke>
	
	</xsl:template>
	
	<xsl:template match="cbc:DueDate">
		<ns1:datumDospijecaPlacanja>
			<xsl:value-of select="." />
		</ns1:datumDospijecaPlacanja>
	
	</xsl:template>
	
	<xsl:template match="cbc:DocumentCurrencyCode" >
		<ns1:valutaERacuna>
			<xsl:value-of select="." />
		</ns1:valutaERacuna>
	
	</xsl:template>
	
	<xsl:template match="cbc:InvoiceTypeCode" >
		<ns1:vrstaDokumenta>
			<xsl:value-of select="." />
		</ns1:vrstaDokumenta>
	
	</xsl:template>
	
	<xsl:template match="cbc:IssueDate">
		<ns1:datumIzdavanja>
			<xsl:value-of select="." />
		</ns1:datumIzdavanja>
	</xsl:template>
	
	<xsl:template match="cbc:ID" mode="InvoiceID" >
		<ns1:brojDokumenta>
			<xsl:value-of select="." />
		</ns1:brojDokumenta>
	</xsl:template>

</xsl:stylesheet>
