<?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: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: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" >
		<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="/">
		
		<fis:EvidentirajERacunZahtjev xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" 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:apply-templates select="cac:TaxTotal/cac:TaxSubtotal" mode="TaxTotal"/>
			
			<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="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"/>
		
		</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="cac:CommodityClassification" mode="InvoiceLine"/>
	
	</xsl:template>
	
	<xsl:template match="cac:CommodityClassification" mode="InvoiceLine" >
		<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:template>

	<xsl:template match="@listVersionID" mode="CommodityClassification" >
		<fis:verzijaSheme>
			<xsl:value-of select="." />
		</fis:verzijaSheme>
	</xsl:template>

	
	<xsl:template match="@listID" mode="CommodityClassification" >
		<fis:identifikatorSheme>
			<xsl:value-of select="." />
		</fis:identifikatorSheme>
	</xsl:template>


	<xsl:template match="cbc:ItemClassificationCode" mode="CommodityClassification" >
		<fis:identifikatorKlasifikacije>
			<xsl:value-of select="." />
		</fis:identifikatorKlasifikacije>
	</xsl:template>
	
	<xsl:template match="cbc:Name" mode="InvoiceLine" >
		<fis:artiklNaziv>
			<xsl:value-of select="." />
		</fis:artiklNaziv>
	</xsl:template>
	
	<xsl:template match="cac:ClassifiedTaxCategory" mode="InvoiceLine" >
		<fis:artiklKategorijaPdv>
			<xsl:value-of select="cbc:ID" />
		</fis:artiklKategorijaPdv>
		
		<fis:artiklStopaPdv>
			<xsl:value-of select="cbc:Percent" />		
		</fis:artiklStopaPdv>
	
	
	</xsl:template>

	<xsl:template match="@unitCode" mode="PriceAmount">
		<fis:artiklJedinicaMjereZaOsnovnuKolicinu>
			<xsl:value-of select="." />
		</fis:artiklJedinicaMjereZaOsnovnuKolicinu>
	
	</xsl:template>
	
	<xsl:template match="cbc:BaseQuantity" mode="InvoiceLine" >
		<fis:artiklOsnovnaKolicina>
			<xsl:value-of select="." />
		</fis:artiklOsnovnaKolicina>
	</xsl:template>
	
	
	<xsl:template match="cbc:PriceAmount" mode="InvoiceLine" >
		<fis:artiklNetoCijena>
			<xsl:value-of select="." />
		</fis:artiklNetoCijena>
	</xsl:template>

	<xsl:template match="@unitCode" mode="InvoiceLine">
		<fis:jedinicaMjere>
			<xsl:value-of select="." />
		</fis:jedinicaMjere>
	
	</xsl:template>
	
	<xsl:template match="cbc:InvoicedQuantity" mode="InvoiceLine">
		<fis:kolicina>
			<xsl:value-of select="." />
		</fis:kolicina>
	
	</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"/>
		
		</fis:RaspodjelaPdv>
	
	</xsl:template>

	<xsl:template match="cbc:TaxExemptionReason" mode="TaxTotal">
		<fis:tekstRazlogaOslobodenja>
			<xsl:value-of select="." />
		</fis:tekstRazlogaOslobodenja>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxExemptionReasonCode" mode="TaxTotal">
		<fis:razlogOslobodenja>
			<xsl:value-of select="." />
		</fis:razlogOslobodenja>
	
	</xsl:template>
	
	<xsl:template match="cbc:Percent" mode="TaxTotal">
		<fis:stopa>
			<xsl:value-of select="." />
		</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: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="." />
		</fis:iznosKojiDospijevaZaPlacanje>
	
	</xsl:template>
	
	<xsl:template match="cbc:PrepaidAmount">
		<fis:placeniIznos>
			<xsl:value-of select="." />
		</fis:placeniIznos>
	
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxInclusiveAmount">
		<fis:iznosSPdv>
			<xsl:value-of select="." />
		</fis:iznosSPdv>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxAmount" mode="LegalMonetaryTotal">
		<fis:pdv>
			<xsl:value-of select="." />
		</fis:pdv>
	
	</xsl:template>
	
	<xsl:template match="cbc:TaxExclusiveAmount">
		<fis:iznosBezPdv>
			<xsl:value-of select="." />
		</fis:iznosBezPdv>

	</xsl:template>
	
	<xsl:template match="cbc:AllowanceTotalAmount">
		<fis:popust>
			<xsl:value-of select="." />
		</fis:popust>
	
	</xsl:template>
	
	<xsl:template match="cbc:LineExtensionAmount">	
		<fis:neto>
			<xsl:value-of select="." />
		</fis:neto>
	
	</xsl:template>
	
	<xsl:template match="cac:PayeeFinancialAccount">
        <fis:PrijenosSredstava>
            <fis:identifikatorRacunaZaPlacanje>
				<xsl:value-of select="cbc:ID" />
            </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">
		<fis:identifikatorPruzateljaPlatnihUsluga>
			<xsl:value-of select="cbc:ID" />
		</fis:identifikatorPruzateljaPlatnihUsluga>
	
	</xsl:template>


	
	<xsl:template match="cbc:Name" mode="PayeeFinancialAccount">
		<fis:nazivRacunaZaPlacanje>
			<xsl:value-of select="." />
		</fis:nazivRacunaZaPlacanje>
	
	</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/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>			
		</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="fn:normalize-space(/i:Invoice/cac:AccountingSupplierParty/cac:SellerContact/cbc:ID)" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$supplierCompanyId" />
				</xsl:otherwise>
			</xsl:choose>
		</fis:oibOperatera>
	</xsl:template>
	
	<xsl:template match="cac:InvoiceDocumentReference">
		<fis:PrethodniERacun>
			<fis:brojDokumenta>
				<xsl:value-of select="cbc:ID" />
			</fis:brojDokumenta>
			<fis:datumIzdavanja>
				<xsl:value-of select="cbc:IssueDate" />
			</fis:datumIzdavanja>
		
		</fis:PrethodniERacun>
	
	
	</xsl:template>
	
	<xsl:template match="cac:ContractDocumentReference">
		<fis:referencaNaUgovor>
			<xsl:value-of select="cbc:ID" />
		</fis:referencaNaUgovor>
	
	</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="." />
		</fis:datumIsporuke>
	
	</xsl:template>
	
	<xsl:template match="cbc:DueDate">
		<fis:datumDospijecaPlacanja>
			<xsl:value-of select="." />
		</fis:datumDospijecaPlacanja>
	
	</xsl:template>
	
	<xsl:template match="cbc:DocumentCurrencyCode" >
		<fis:valutaERacuna>
			<xsl:value-of select="." />
		</fis:valutaERacuna>
	
	</xsl:template>
	
	<xsl:template match="cbc:InvoiceTypeCode" >
		<fis:vrstaDokumenta>
			<xsl:value-of select="." />
		</fis:vrstaDokumenta>
	
	</xsl:template>
	
	<xsl:template match="cbc:IssueDate">
		<fis:datumIzdavanja>
			<xsl:value-of select="." />
		</fis:datumIzdavanja>
	</xsl:template>
	
	<xsl:template match="cbc:ID" mode="InvoiceID" >
		<fis:brojDokumenta>
			<xsl:value-of select="." />
		</fis:brojDokumenta>
	</xsl:template>

</xsl:stylesheet>
