<?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., 
 -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:n="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:a="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:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:clm54217="urn:un:unece:uncefact:codelist:specification:54217:2001" xmlns:clm5639="urn:un:unece:uncefact:codelist:specification:5639:1988" xmlns:clm66411="urn:un:unece:uncefact:codelist:specification:66411:2001" xmlns:clmIANAMIMEMediaType="urn:un:unece:uncefact:codelist:specification:IANAMIMEMediaType:2003" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" version="1.0" exclude-result-prefixes="a cac cbc clm54217 clm5639 clm66411 clmIANAMIMEMediaType ext i n qdt udt xs xsi xsl">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

	<xsl:template match="/">
		<xsl:comment>
			Generated by Moj eRačun EN-2-HOLCIM transformation V2024-04-22
		</xsl:comment>
	
		<xsl:apply-templates select="/i:Invoice" />
    </xsl:template>
    
    <xsl:template match="i:Invoice">
    
 		<INVOIC_DIGITAL>
            <xsl:attribute name="xsi:noNamespaceSchemaLocation">
                <xsl:value-of select="'D:/Elektronički računi d.o.o/Holcim/LH XSD schema v22.xsd'"/>
            </xsl:attribute>
            
            <xsl:call-template name="tInvoiceType" />

			<xsl:apply-templates select="cac:AccountingCustomerParty/cac:Party" mode="CustomerVAT" />			
			<xsl:apply-templates select="cac:AccountingSupplierParty/cac:Party" mode="VendorVAT" />

			<Node>1</Node>
			
			<xsl:apply-templates select="cbc:IssueDate" />
			<xsl:apply-templates select="cbc:DocumentCurrencyCode" />
			
			<xsl:apply-templates select="cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount" />
			<xsl:apply-templates select="cac:TaxTotal/cbc:TaxAmount" />
			<xsl:apply-templates select="cac:LegalMonetaryTotal/cbc:PayableAmount" />

            <Text><xsl:value-of select="''"/></Text>
            
            <xsl:apply-templates select="cbc:ID" mode="InvoiceNumber" />
            
            <xsl:apply-templates select="cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName"  mode="VendorName" />
            <xsl:apply-templates select="cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName"  mode="CustomerName" />

            <LINES>
				<xsl:apply-templates select="cac:InvoiceLine" />
            
            </LINES>
            
            <xsl:apply-templates select="cac:AdditionalDocumentReference/cac:Attachment/cbc:EmbeddedDocumentBinaryObject" />

		</INVOIC_DIGITAL>
   
    </xsl:template>
    
    <xsl:template match="cbc:EmbeddedDocumentBinaryObject">
		<EmbeddedFilename>
			<xsl:value-of select="string(.)"/>
		</EmbeddedFilename>
    
    </xsl:template>
    
    
    <xsl:template match="cac:InvoiceLine" >
		<ART>
			<xsl:apply-templates select="cbc:ID" mode="InvoiceLine" />
			<xsl:apply-templates select="cac:Item/cac:ClassifiedTaxCategory" />
			<xsl:call-template name="tItemDescription" />
			<xsl:apply-templates select="cbc:InvoicedQuantity" />
			<xsl:call-template name="tUnitOfMeasure" />
			<xsl:call-template name="tOrderReference" />
			<xsl:call-template name="tDeliveryNote" />
			<xsl:apply-templates select="cbc:LineExtensionAmount" />
		
		
		</ART>
    
    </xsl:template>
    
    <xsl:template match="cbc:LineExtensionAmount" >
		<LineNetAmount>
			<xsl:choose>
				<xsl:when test="number(string(.)) != 'NaN'">
					<xsl:value-of select="."/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="string('0.00')"/>
				
				</xsl:otherwise>
			</xsl:choose>
        </LineNetAmount>   
		   
    </xsl:template>
    
    <xsl:template name="tDeliveryNote">
		<DeliveryNote>
			<!-- DEFINIRATI ŠTO IDE AKO NODE NE SMIJE BITI PRAZAN -->
			<xsl:value-of select="string('')" />
		</DeliveryNote>
    
    </xsl:template>
 
	<xsl:template name="tOrderReference" >
		<PurchaseOrder>
			<xsl:choose>
				<xsl:when test="/i:Invoice/cac:OrderReference/cbc:ID and normalize-space(/i:Invoice/cac:OrderReference/cbc:ID) != ''" >
					<xsl:value-of select="string(/i:Invoice/cac:OrderReference/cbc:ID)"/>
				</xsl:when>
				<xsl:otherwise>
					<!-- DEFINIRATI ŠTO IDE AKO NODE NE SMIJE BITI PRAZAN -->
					<xsl:value-of select="string('')" />
				</xsl:otherwise>
			</xsl:choose>
		
		</PurchaseOrder>
	
	</xsl:template>
     
    <xsl:template name="tUnitOfMeasure">
		<UnitOfMeasure>
			<xsl:choose>
				<xsl:when test="cac:Price/cbc:BaseQuantity and normalize-space(cac:Price/cbc:BaseQuantity/@unitCode) != '' " >
						<xsl:value-of select="cac:Price/cbc:BaseQuantity/@unitCode"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="cbc:InvoicedQuantity/@unitCode"/>
				</xsl:otherwise>
			</xsl:choose>   
		</UnitOfMeasure>
   
     </xsl:template>
  
	<xsl:template match="cbc:InvoicedQuantity">
		<Quantity>
			<xsl:value-of select="number(string(.))"/>
		</Quantity>
	
	</xsl:template>

    <xsl:template name="tItemDescription" >
		<ItemDescription>
			<xsl:choose>
				<xsl:when test="cac:Item/cbc:Name and normalize-space(cac:Item/cbc:Name) != '' ">
					<xsl:value-of select="string(cac:Item/cbc:Name)" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="string(cac:Item/cbc:Description)" />
				</xsl:otherwise>
			</xsl:choose>
    
		</ItemDescription>
    </xsl:template>    
    
    
    <xsl:template match="cac:ClassifiedTaxCategory">
		<TaxRate>
			<xsl:choose>
				<xsl:when test="cbc:Percent and (number(string(cbc:Percent)) != 'NaN')">
					<xsl:value-of select="format-number(number(cbc:Percent), '#.00')" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="string('0.00')"/>
				
				</xsl:otherwise>			
				
			</xsl:choose>	
		</TaxRate>    
    </xsl:template>

    
    <xsl:template match="cbc:ID" mode="InvoiceLine">
    
    <!-- PROVJERITI JE LI OVO OK !!!
		Dodaje 0 na broj linije (pa uvijek počinje sa 10, 20 ...)
	-->
		<LineNumber>
			<xsl:value-of select="number(string(concat(string(.), 0)))"/>
		</LineNumber>
    
    
    </xsl:template>
    
    <xsl:template match="cbc:RegistrationName" mode="CustomerName">
		<CustomerName>
			<xsl:value-of select="string(.)"/>
		</CustomerName>
     
    </xsl:template>

    <xsl:template match="cbc:RegistrationName" mode="VendorName">
		<VendorName>
			<xsl:value-of select="string(.)"/>
		</VendorName>
     
    </xsl:template>
    
    <xsl:template match="cbc:ID" mode="InvoiceNumber" >
			<InvoiceNumber>
				<xsl:value-of select="string(.)"/>
			</InvoiceNumber>
    
    </xsl:template>
     
    <xsl:template match="cbc:PayableAmount">
		<GrossAmount>
			<xsl:choose>
				<xsl:when test="number(string(.)) != 'NaN'">
					<xsl:value-of select="."/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="string('0.00')"/>
				
				</xsl:otherwise>
			</xsl:choose>
			
		</GrossAmount> 
		
    </xsl:template>
    
     
    <xsl:template match="cbc:TaxAmount">
		<TaxAmount>
			<xsl:choose>
				<xsl:when test="number(string(.)) != 'NaN'">
					<xsl:value-of select="."/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="string('0.00')"/>
				
				</xsl:otherwise>
			</xsl:choose>

		</TaxAmount> 
		
    </xsl:template>
    
   
    <xsl:template match="cbc:TaxExclusiveAmount">
		<NetAmount>
			<xsl:choose>
				<xsl:when test="number(string(.)) != 'NaN'">
					<xsl:value-of select="."/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="string('0.00')"/>
				
				</xsl:otherwise>
			</xsl:choose>

		</NetAmount> 
		
    </xsl:template>
    
    <xsl:template match="cbc:DocumentCurrencyCode">
    
		<CurrencyKey>
			<xsl:value-of select="normalize-space(.)"/>
		</CurrencyKey>
   
    </xsl:template>
    
    <xsl:template match="cbc:IssueDate" >
    
        <InvoiceDate>
            <xsl:value-of select="concat(concat(substring(string(.), 0, 5), substring(string(.), 6, 2)), substring(string(.), 9, 2))"/>
        </InvoiceDate>
   
    </xsl:template>
    
    <xsl:template match="cac:Party" mode="VendorVAT">
        <VendorVAT>
           <!-- <xsl:value-of select="concat('HR', string(n:OutgoingInvoice/n:BuyerID))"/>-->
 			<xsl:choose>
				<xsl:when test="./cac:PartyTaxScheme/cbc:CompanyID and normalize-space(./cac:PartyTaxScheme/cbc:CompanyID) != ''">
					<xsl:value-of select="./cac:PartyTaxScheme/cbc:CompanyID" />
				</xsl:when>
				<xsl:when test="./cac:PartyLegalEntity/cbc:CompanyID and normalize-space(./cac:PartyLegalEntity/cbc:CompanyID) != ''">
					<xsl:value-of select="concat('HR', string(./cac:PartyLegalEntity/cbc:CompanyID))"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="concat('HR', string(./cbc:EndpointID))"/>
				</xsl:otherwise>
			</xsl:choose>
       </VendorVAT>   
    
    </xsl:template>

    
    <xsl:template match="cac:Party" mode="CustomerVAT">
        <CustomerVAT>
           <!-- <xsl:value-of select="concat('HR', string(n:OutgoingInvoice/n:BuyerID))"/>-->
 			<xsl:choose>
				<xsl:when test="./cac:PartyTaxScheme/cbc:CompanyID and normalize-space(./cac:PartyTaxScheme/cbc:CompanyID) != ''">
					<xsl:value-of select="./cac:PartyTaxScheme/cbc:CompanyID" />
				</xsl:when>
				<xsl:when test="./cac:PartyLegalEntity/cbc:CompanyID and normalize-space(./cac:PartyLegalEntity/cbc:CompanyID) != ''">
					<xsl:value-of select="concat('HR', string(./cac:PartyLegalEntity/cbc:CompanyID))"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="concat('HR', string(./cbc:EndpointID))"/>
				</xsl:otherwise>
			</xsl:choose>
       </CustomerVAT>   
    
    </xsl:template>
    
    <xsl:template name="tInvoiceType">
    
            <InvoiceType>
                <xsl:variable name="var2_map_select_OutgoingInvoicesData">
                    <xsl:if test="string((/i:Invoice/cac:OrderReference) = (/i:Invoice/cac:OrderReference)) != 'false'">
                        <xsl:value-of select="'1'"/>
                    </xsl:if>
                </xsl:variable>
                <xsl:choose>
                    <xsl:when test="string(boolean(string($var2_map_select_OutgoingInvoicesData))) != 'false'">
                        <xsl:value-of select="'MM'"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="'FI'"/>
                    </xsl:otherwise>
                </xsl:choose>
            </InvoiceType>
   
    </xsl:template>

</xsl:stylesheet>
