<?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-22
Dodan fix za STSI-INA popustu cbc:Note

-->
 
<!-- 2026-01-11 
Dodan Obračun po naplaćenoj naknadi u cbc:Note polje

--> 
 
 <!-- V2025-12-17
CopyIndikator podrška
PartyIdentification - uzima iz računa (i za pošiljatelja stavlja samo dio nakon HR99 ako postoji)
-->
 
 <!-- V2025-10-20
Usklađeno sa EN F.2.0

-->
 
 <!-- V2025-01-28

Hendlanje Model/PNB kad nisu upisani konzistentni podaci
Podrška za velike brojeve (da ih ne stavlja u scientific notation)

-->
 
 <!-- V2024-10-15
Dodatne dorade procesiranja odgovorne osobe na zahtjev Ine

-->
 
 <!-- V2024-09-29
Sređena oznaka operatera, odgovorna osoba i vrijeme izdavanja

-->
 
 <!-- V2024-09-24
Zamjena SAP broja i Invoice/cbc:ID za CROSCO i STSI račune za INU

EUR umjesto HRK kao default valuta

-->
 <!-- 2024-05-21
1. 
Izmijenio redosljed Attachmenta - prvo stavlja Attachmente koji su .PDF (sa idejom da je to zapravo vizualizacija), a onda ostale attachmente
Nije moguće utjecati na redosljed .PDF attachmenta

2. 
Komentirao template 
<xsl:call-template name="OriginalXmlAttachment"/>
... jer se oslanjao na parametar originalXML - koji (izgleda) ne dobiva
Otkomentirati liniju 183 ako fajl treba imati originalni xml (pretpostavljam base64 enkodiran)

-->

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="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:ns2="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
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:hrextac="urn:mfin.gov.hr:schema:xsd:HRExtensionAggregateComponents-1" 
exclude-result-prefixes="ns2">
	<!--<xsl:output method="xml" encoding="UTF-8" byte-order-mark="yes" indent="yes"/>-->
	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	
	 <xsl:param name="originalXml"/> 
<!--	<xsl:variable name="originalXml">
		<xsl:copy-of select="/" />
	
	</xsl:variable>-->
	
	<xsl:variable name="isOutsideVAT">false</xsl:variable>
    <xsl:variable name="supplierCompanyId">
        <xsl:choose>
            <xsl:when test="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID[@schemeID='9934']">
                <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="customerCompanyId">
        <xsl:choose>
            <xsl:when test="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID[@schemeID='9934']">
                <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:AccountingCustomerParty/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="issueTime">
        <xsl:choose>
            <xsl:when test="/ns2:Invoice/cbc:IssueTime">
                <xsl:value-of select="substring(/ns2:Invoice/cbc:IssueTime, 1, 8)" />
            </xsl:when>
            <xsl:when test="/ns2:Invoice/cbc:Note" >
                <xsl:for-each select="/ns2:Invoice/cbc:Note">
                    <xsl:variable name="vrijeme1" select="substring-after(., 'Vrijeme izdavanja:')" />
                    <xsl:variable name="vrijeme2" select="substring-before($vrijeme1, '#AAI#')" />
                    <xsl:variable name="vrijeme3" select="substring-before(., '#Vrijeme izdavanja')" />
                    <xsl:variable name="vrijeme4" select="substring-after(., 'Issue Time:')" />
                    <xsl:variable name="vrijeme5" select="substring-after(., 'Vrijeme izdavanja#')" />
                    <xsl:choose>
                        <xsl:when test="normalize-space($vrijeme3) != ''">
                            <xsl:value-of select="normalize-space($vrijeme3)" />
                        </xsl:when>						
                        <xsl:when test="normalize-space($vrijeme2) != ''">
                            <xsl:value-of select="normalize-space($vrijeme2)" />
                        </xsl:when>
                        <xsl:when test="normalize-space($vrijeme1) != ''">
                            <xsl:value-of select="normalize-space($vrijeme1)" />
                        </xsl:when>										
                        <xsl:when test="normalize-space($vrijeme4) != ''">
                            <xsl:value-of select="normalize-space($vrijeme4)" />
                        </xsl:when>
                        <xsl:when test="normalize-space($vrijeme5) != ''">
                            <xsl:value-of select="normalize-space($vrijeme5)" />
                        </xsl:when>	
                        
                        <!--				<xsl:when test="normalize-space(substring-before(., '#Vrijeme izdavanja')) != ''">
							<xsl:value-of select="normalize-space(substring-before(., '#Vrijeme izdavanja'))" /
						</xsl:when>
		>-->			
                    </xsl:choose>
                </xsl:for-each>
            </xsl:when>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="noteSum">
        <xsl:variable name="sum" />
        <xsl:for-each select="//ns2:Invoice/cbc:Note">
            
            <xsl:choose>
				<xsl:when test="normalize-space(substring-before(., '#AAI#')) != ''" />
				<xsl:when test="normalize-space(substring-before(., '#Vrijeme izdavanja')) != ''" />
				<xsl:when test="normalize-space(substring-before(., '#Oznaka operatera')) != ''" />
				<xsl:when test="normalize-space(substring-before(., '#Odgovorna osoba')) != ''" />
				<xsl:when test="contains(., 'Vrijeme') and contains(., 'izdavanja') and contains (., ':')" />
				<xsl:when test="contains(., 'Issue') and contains(., 'ime') and contains (., ':')" />
				<xsl:when test="contains(., 'Oznaka') and contains(., 'peratera') and contains (., ':')" />
				<xsl:when test="contains(., 'Operater') and contains (., ':')" />
				<xsl:when test="contains(., 'Operator') and contains (., ':')" />
				<xsl:when test="contains(., 'Odgovorna') and contains(., 'soba') and contains (., ':')" />
				<xsl:when test="contains(., 'Responsible') and contains(., 'erson') and contains (., ':')" />
                <xsl:when test="contains(., 'Blagajnik') and contains(., ':')" />
                <xsl:otherwise><xsl:value-of select="concat($sum,., ';')"/></xsl:otherwise>
            </xsl:choose>
            
        </xsl:for-each>
        
        
    </xsl:variable>
    
	<xsl:template match="/">
	    <xsl:apply-templates select="/ns2:Invoice" />
		
		
	</xsl:template>
	
	<xsl:template match="ns2:Invoice">
		<Invoice
		    xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
		    xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
		    xmlns:ns3="urn:un:unece:uncefact:codelist:specification:IANAMIMEMediaType:2003"
		    xmlns:ns4="urn:un:unece:uncefact:codelist:specification:5639:1988" 
		    xmlns:ns1="urn:un:unece:uncefact:codelist:specification:54217:2001" 
		    xmlns:ns2="urn:un:unece:uncefact:codelist:specification:66411:2001" 
		    xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" 
		    xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
		    xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
		    xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2">
			
			
			
			<ext:UBLExtensions>
			
				<!--<xsl:apply-templates select="ext:UBLExtensions/ext:UBLExtension[not(.//sig:UBLDocumentSignatures)]" />-->

				<xsl:call-template name="UBLExtensionPlace" />
				
				<xsl:apply-templates select="cac:AdditionalDocumentReference/cac:Attachment[normalize-space(cbc:EmbeddedDocumentBinaryObject)!= '' and  contains(cbc:EmbeddedDocumentBinaryObject/@filename, '.pdf')]" mode="Attachment" />
				
<!--				<xsl:apply-templates select="ext:UBLExtensions/ext:UBLExtension[normalize-space(ext:ExtensionContent)!= '' and  contains(cbc:Name, 'application/pdf')]" mode="Attachment" />-->

				<xsl:apply-templates select="cac:AdditionalDocumentReference/cac:Attachment[normalize-space(cbc:EmbeddedDocumentBinaryObject)!= '' and  not( contains(cbc:EmbeddedDocumentBinaryObject/@filename, '.pdf'))]" mode="Attachment" />
                <!--<xsl:call-template name="OriginalXmlAttachment"/>-->

			</ext:UBLExtensions>

			<cbc:UBLVersionID>2.0</cbc:UBLVersionID>
			<cbc:CustomizationID>HRUBL-1.0</cbc:CustomizationID>
			<!--<xsl:apply-templates select="cbc:ProfileID" />-->
			<!--<cbc:ProfileID>MojEracunInvoice</cbc:ProfileID>-->
			<xsl:apply-templates select="cbc:ID" mode="FiscalNumber" />
			
		    <!--<cbc:CopyIndicator>false</cbc:CopyIndicator>-->
		    <xsl:apply-templates select="cbc:CopyIndicator" />

			<xsl:apply-templates select="cbc:IssueDate" />
			<xsl:call-template name="IssueTime"/>
<!--		    <xsl:choose>
		        <xsl:when test="normalize-space(cbc:IssueTime) !=''">
		            <xsl:apply-templates select="cbc:IssueTime"/>
		        </xsl:when>
		        <xsl:otherwise>
		            <xsl:call-template name="IssueTime"/>
		        </xsl:otherwise>
		    </xsl:choose>
-->			

			<xsl:apply-templates select="cbc:InvoiceTypeCode" />
		    <xsl:if test="normalize-space($noteSum) != ''">
		        <cbc:Note><xsl:value-of select="$noteSum" /></cbc:Note>
		    </xsl:if>
		    <xsl:apply-templates select="//hrextac:HRObracunPDVPoNaplati" />
			<xsl:apply-templates select="cbc:TaxPointDate" />
			<xsl:copy-of select="cbc:DocumentCurrencyCode" />
			<xsl:copy-of select="cbc:TaxCurrencyCode" />
			<xsl:apply-templates select="cbc:AccountingCost" />
			<xsl:apply-templates select="cbc:BuyerReference" />
			<xsl:apply-templates select="cac:InvoicePeriod" />
			
			<xsl:apply-templates select="cac:OrderReference" />
			<xsl:apply-templates select="cac:BillingReference" />
			<xsl:copy-of select="cac:DespatchDocumentReference" />			
			<xsl:copy-of select="cac:ReceiptDocumentReference" />
			<xsl:copy-of select="cac:OriginatorDocumentReference" />
			<xsl:copy-of select="cac:ContractDocumentReference" />
			<!--<xsl:apply-templates select="cac:AdditionalDocumentReference[cbc:DocumentType != 'SAPTypeID']" />-->
			<xsl:apply-templates select="cac:AdditionalDocumentReference[contains(cbc:DocumentType,  'SAP broj')]" mode="FiscalAdditionalDocumentReference" />
			<xsl:apply-templates select="cbc:ID" mode="AdditionalDocumentReference" />
						
			<xsl:apply-templates select="cac:AccountingSupplierParty" />
			<xsl:apply-templates select="cac:AccountingCustomerParty" />
			
			<xsl:copy-of select="cac:PayeeParty" />
			<xsl:copy-of select="cac:TaxRepresentativeParty" />
			
			<!--<xsl:apply-templates select="cac:Delivery" />-->
			<xsl:call-template name="cac:Delivery" />
			
			<xsl:apply-templates select="cac:PaymentMeans" />
			<xsl:apply-templates select="cac:PaymentTerms" />
			
			<xsl:apply-templates select="cac:AllowanceCharge" />
			
			<xsl:apply-templates select="cac:TaxTotal" />
			<xsl:apply-templates select="cac:LegalMonetaryTotal" />

			<xsl:apply-templates select="cac:InvoiceLine" />
		</Invoice>
	
	</xsl:template>
	
	<xsl:template match="hrextac:HRObracunPDVPoNaplati">
		<xsl:if test="normalize-space(.) != ''">
			<cbc:Note>
				<xsl:value-of select="normalize-space(.)" />
			</cbc:Note>
		
		</xsl:if>
	
	</xsl:template>
	
    <xsl:template name="OriginalXmlAttachment">
        <ext:UBLExtension>
            <cbc:Name>application/xml</cbc:Name>
            <ext:ExtensionContent>
                <xsl:value-of select="$originalXml" />
            </ext:ExtensionContent>
        </ext:UBLExtension>
    </xsl:template>
    
	<xsl:template name="IssueTime">
	    <cbc:IssueTime>
	        <xsl:value-of select="substring($issueTime, 1, 8)"/>
	    </cbc:IssueTime>
	</xsl:template>
	
	<xsl:template match="cac:Attachment" mode="Attachment">
		<ext:UBLExtension>
			<cbc:Name>
				<xsl:choose>
					<xsl:when test="normalize-space(cbc:EmbeddedDocumentBinaryObject/@mimeCode) != ''">
						<xsl:value-of select="normalize-space(cbc:EmbeddedDocumentBinaryObject/@mimeCode)" />
					</xsl:when>
					<xsl:otherwise>application/pdf</xsl:otherwise>
				</xsl:choose>
			</cbc:Name>
			<ext:ExtensionContent>
				<xsl:value-of select="cbc:EmbeddedDocumentBinaryObject" />
			</ext:ExtensionContent>
		</ext:UBLExtension>
	</xsl:template>
	
	
	
	<xsl:template match="ext:UBLExtension" mode="Attachment">
		<ext:UBLExtension>
			<cbc:Name>
				<xsl:choose>
					<xsl:when test="normalize-space(cbc:EmbeddedDocumentBinaryObject/@mimeCode) != ''">
						<xsl:value-of select="normalize-space(cbc:EmbeddedDocumentBinaryObject/@mimeCode)" />
					</xsl:when>
					<xsl:otherwise>application/pdf</xsl:otherwise>
				</xsl:choose>
			</cbc:Name>
			<ext:ExtensionContent>
				<xsl:value-of select="ext:ExtensionContent" />
			</ext:ExtensionContent>
		</ext:UBLExtension>
	</xsl:template>
		
	<xsl:template name="UBLExtensionPlace">
		<ext:UBLExtension>
			<cbc:ID>HRINVOICE1</cbc:ID>
			<cbc:Name>InvoiceIssuePlaceData</cbc:Name>
			<ext:ExtensionAgencyID>hr</ext:ExtensionAgencyID>
			<ext:ExtensionAgencyName>Hrvatska</ext:ExtensionAgencyName>
			<ext:ExtensionReasonCode>MandatoryField</ext:ExtensionReasonCode>
			<ext:ExtensionReason> Mjesto izdavanja racun prema Pravilniku o PDV-u</ext:ExtensionReason>
			<ext:ExtensionContent>
				<ihi:IssuePlace xmlns:ihi="urn:invoice:hr:issueplace">
					<xsl:choose>
						<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName and (normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName) != '') "><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName" /></xsl:when>
						<xsl:otherwise><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:Line" /></xsl:otherwise>
					</xsl:choose>
				</ihi:IssuePlace>
				<ihc:ChannelID xmlns:ihc="urn:invoice:hr:channelID">1</ihc:ChannelID>
				<xsl:if test="//ns2:Invoice/cac:AdditionalDocumentReference[cbc:DocumentType='SAPTypeID']/cbc:ID != ''">
					<iht:SAPTypeID xmlns:iht="urn:invoice:hr:saptypeID"><xsl:value-of select="//ns2:Invoice/cac:AdditionalDocumentReference[cbc:DocumentType='SAPTypeID']/cbc:ID"/></iht:SAPTypeID>
				</xsl:if>
			</ext:ExtensionContent>
		</ext:UBLExtension>	
	</xsl:template>
	
	<xsl:template match="cac:InvoiceLine">
		<cac:InvoiceLine>
			<xsl:copy-of select="cbc:ID" />
			<xsl:copy-of select="cbc:Note" />
			<xsl:copy-of select="cbc:InvoicedQuantity" />
			<xsl:copy-of select="cbc:LineExtensionAmount" />
			<xsl:copy-of select="cbc:AccountingCost" />
			<xsl:copy-of select="cac:InvoicePeriod" />
			<xsl:copy-of select="cac:OrderLineReference" />
			<xsl:copy-of select="cac:DocumentReference"/>
			<xsl:copy-of select="cac:DespatchLineReference" />
			<xsl:apply-templates select="cac:AllowanceCharge" mode="InvoiceLine" />
		    <xsl:variable name="taxCategoryNode">
		        <xsl:apply-templates select="cac:Item/cac:ClassifiedTaxCategory" mode="AllowanceChargeLine" />
		    </xsl:variable>
		    
		    <xsl:variable name="currencyID"><xsl:value-of select="cbc:LineExtensionAmount/@currencyID" /></xsl:variable>
		    <xsl:variable name="taxableAmount"><xsl:value-of select="number(cbc:LineExtensionAmount)" /></xsl:variable>
		    <xsl:variable name="percent"><xsl:value-of select="number(cac:Item/cac:ClassifiedTaxCategory/cbc:Percent)" /></xsl:variable>
		    <xsl:call-template name="HRTaxTotal">
		        <xsl:with-param name="currencyID" select="$currencyID" />
		        <xsl:with-param name="taxableAmount" select="$taxableAmount"/>
		        <xsl:with-param name="percent" select="$percent"/>		
		        <xsl:with-param name="taxCategoryNode" select="$taxCategoryNode" />
		    </xsl:call-template>
			<xsl:apply-templates select="cac:Item" mode="InvoiceLine" />
			<xsl:copy-of select="cac:Price"  />
			

		</cac:InvoiceLine>
		
	</xsl:template>	
    
    <xsl:template match="cac:Item" mode="InvoiceLine">
        <cac:Item>
            <xsl:apply-templates select="cbc:Description" />
            <xsl:apply-templates select="cbc:Name" />
            <xsl:apply-templates select="cac:BuyersItemIdentification" />
            <xsl:apply-templates select="cac:SellersItemIdentification" />
            <xsl:choose>
                <xsl:when test="cac:ManufacturersItemIdentification">
                    <xsl:apply-templates select="cac:ManufacturersItemIdentification"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="cac:AdditionalItemProperty[cbc:Name/text() = 'ManufacturersItemIdentification']" mode="ManufacturersItemIdentification" />
                </xsl:otherwise>
            </xsl:choose>
            <xsl:apply-templates select="cac:StandardItemIdentification" />
            <xsl:apply-templates select="cac:AdditionalItemIdentification" />
            <xsl:apply-templates select="cac:AdditionalItemProperty[cbc:Name/text() = 'AdditionalItemIdentification']" mode="AdditionalItemIdentification" />
            
            <xsl:apply-templates select="cac:OriginCountry" />
            <xsl:apply-templates select="cac:CommodityClassification" />
            <!--<xsl:apply-templates select="../cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory" mode="InvoiceLine" />AllowanceChargeLine-->
            <xsl:apply-templates select="cac:AdditionalItemProperty[not((cbc:Name/text() = 'ManufacturersItemIdentification') or (cbc:Name/text() = 'AdditionalItemIdentification'))]" />
            
            
            
        </cac:Item>
        
    </xsl:template>
    <xsl:template match="cac:AdditionalItemProperty">
        <cac:AdditionalItemProperty>
            <xsl:apply-templates select="cbc:Name" />
            <xsl:apply-templates select="cbc:Value" />
            
        </cac:AdditionalItemProperty>
        
    </xsl:template>
    
    <xsl:template match="cac:StandardItemIdentification | cac:AdditionalItemIdentification">
        <xsl:if test="cbc:ID and (normalize-space(cbc:ID) != '')" >
            <cac:AdditionalItemIdentification>
                <xsl:apply-templates select="cbc:ID" mode="SchemeID" />
            </cac:AdditionalItemIdentification>
        </xsl:if>
    </xsl:template>
    <xsl:template match="cac:AdditionalItemProperty" mode="ManufacturersItemIdentification">
        <cac:ManufacturersItemIdentification>
            <cbc:ID><xsl:value-of select="cbc:Value" /></cbc:ID>
        </cac:ManufacturersItemIdentification>
    </xsl:template>
    <xsl:template match="cac:ManufacturersItemIdentification">
        <cac:ManufacturersItemIdentification>
            <xsl:apply-templates select="cbc:ID" mode="ID"/>
        </cac:ManufacturersItemIdentification>
    </xsl:template>
    
    <xsl:template match="cac:AdditionalItemProperty" mode="AdditionalItemIdentification">
        <cac:AdditionalItemIdentification>
            <cbc:ID><xsl:value-of select="cbc:Value" /></cbc:ID>
            
        </cac:AdditionalItemIdentification>
    </xsl:template>
    <xsl:template match="cac:SellersItemIdentification">
        <cac:SellersItemIdentification>
            <xsl:apply-templates select="cbc:ID" mode="ID" />
        </cac:SellersItemIdentification>
    </xsl:template>
    <xsl:template match="cac:BuyersItemIdentification">
        <cac:BuyersItemIdentification>
            <xsl:apply-templates select="cbc:ID" mode="ID" />
        </cac:BuyersItemIdentification>
    </xsl:template>
    
    <xsl:template match="cac:ClassifiedTaxCategory" mode="AllowanceChargeLine">
        <cac:TaxCategory>
            <xsl:variable name="taxCategoryID">
                <xsl:call-template name="HRTaxCategoyIDValue">
                    <xsl:with-param name="taxCategoryID" select="cbc:ID" />
                </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="HRTaxCategoryID">
                <xsl:with-param name="taxCategoryID" select="$taxCategoryID" />
            </xsl:call-template>
            <!--<xsl:apply-templates select="cbc:Percent" />-->
            <xsl:choose>
                <xsl:when test="cbc:Percent and (string(number(cbc:Percent)) != 'NaN')">
                    <xsl:apply-templates select="cbc:Percent" />
                </xsl:when>
                <xsl:otherwise>
                    <cbc:Percent>0</cbc:Percent>
                </xsl:otherwise>
            </xsl:choose>
            
            <xsl:apply-templates select="/ns2:Invoice/cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory[cbc:ID/text() = $taxCategoryID]/cbc:TaxExemptionReason" />
            
            <xsl:call-template name="HRTaxScheme">
                <xsl:with-param name="percent" select="cbc:Percent" />			
            </xsl:call-template>
            
        </cac:TaxCategory>
        
    </xsl:template>
    <xsl:template name="HRTaxCategoryID">
        <xsl:param name="taxCategoryID" />
        <cbc:ID schemeID="UN/ECE 5305" schemeAgencyID="6" schemeURI="http://www.unece.org/trade/untdid/d07a/tred/tred5305.htm">
            <xsl:value-of select="$taxCategoryID" />
        </cbc:ID>
        <cbc:Name>
            <xsl:choose>
                <xsl:when test="$taxCategoryID = 'S'">PDV</xsl:when>
                <xsl:when test="$taxCategoryID = 'O'">NEOPOREZIVO</xsl:when>
                <xsl:when test="$taxCategoryID = 'AE'">PPO</xsl:when>
                <xsl:when test="$taxCategoryID = 'E'">OSLOBOĐENO_POREZA</xsl:when>
                <xsl:when test="$taxCategoryID = 'Z'">OSLOBOĐENO_POREZA</xsl:when>
            </xsl:choose>
        </cbc:Name>
    </xsl:template>
    
    <xsl:template name="HRTaxCategoyIDValue">
        <xsl:param name="taxCategoryID" />
        <xsl:choose>
            <!--Podrška za mapping ako bude potreban -->
            <xsl:when test="normalize-space($taxCategoryID) = 'O'">O</xsl:when>
            <xsl:when test="normalize-space($taxCategoryID) = 'E'">E</xsl:when>
            <xsl:otherwise><xsl:value-of select="$taxCategoryID" /></xsl:otherwise>
        </xsl:choose>	
    </xsl:template>
	
    <xsl:template name="HRTaxTotal">
        <xsl:param name="currencyID" select="'EUR'" />
        <xsl:param name="taxableAmount" select="0"/>
        <xsl:param name="percent" select="0"/>
        <xsl:param name="taxCategoryNode" />
        <!--<xsl:value-of select="concat('cID', $currencyID , 'taAm', $taxableAmount, 'per', $percent)" />-->
        <cac:TaxTotal>
            <xsl:variable name="taxAmount">
                <xsl:choose>
                    <xsl:when test="string(number(number($taxableAmount) * number($percent) div 100))='NaN'">0.00</xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="format-number( number($taxableAmount) * number($percent) div 100, '#########0.00;-#########0.00') " />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <cbc:TaxAmount>
                <xsl:attribute name="currencyID"><xsl:value-of select="$currencyID" /></xsl:attribute>
                <xsl:value-of select="format-number($taxAmount,'#########0.00;-#########0.00')" />
            </cbc:TaxAmount>
            <cac:TaxSubtotal>
                <cbc:TaxableAmount>
                    <xsl:attribute name="currencyID"><xsl:value-of select="$currencyID" /></xsl:attribute>
                    <!--<xsl:value-of select="$taxableAmount" />	-->
                    <xsl:value-of select="format-number($taxableAmount,'#########0.00;-#########0.00')" />
				 
                </cbc:TaxableAmount>
                <cbc:TaxAmount>
                    <xsl:attribute name="currencyID"><xsl:value-of select="$currencyID" /></xsl:attribute>
                    <xsl:value-of select="format-number($taxAmount,'#########0.00;-#########0.00')" />
                </cbc:TaxAmount>
                <cbc:CalculationSequenceNumeric>1</cbc:CalculationSequenceNumeric>
                <!--<xsl:apply-templates select="cac:TaxCategory" mode="AllowanceChargeLine" />-->
                <xsl:copy-of select="$taxCategoryNode" />
            </cac:TaxSubtotal>
        </cac:TaxTotal>
        
    </xsl:template>
	
	<xsl:template match="cac:AllowanceCharge" mode="InvoiceLine" >
		<cac:AllowanceCharge>
			<xsl:apply-templates select="cbc:ChargeIndicator" />
			<xsl:apply-templates select="cbc:AllowanceChargeReasonCode" />
			<xsl:apply-templates select="cbc:AllowanceChargeReason" />
			<xsl:apply-templates select="cbc:MultiplierFactorNumeric" />
			<xsl:apply-templates select="cbc:Amount" />
			<xsl:apply-templates select="cbc:BaseAmount" />

			</cac:AllowanceCharge>
			

	</xsl:template>
	<xsl:template match="cac:TaxSubtotal" mode="InvoiceLine" >
		<cac:TaxSubtotal>
			<xsl:apply-templates select="cbc:TaxableAmount" />
			<xsl:apply-templates select="cbc:TaxAmount" />
			<xsl:apply-templates select="cbc:CalculationSequenceNumeric" />
			<xsl:apply-templates select="cac:TaxCategory" />
		
		</cac:TaxSubtotal>
	
	</xsl:template>
	
	<xsl:template match="cac:TaxCategory">
		<cac:TaxCategory>
			<xsl:copy-of select="cbc:ID" />
			<xsl:copy-of select="cbc:Name" />
			<xsl:copy-of select="cbc:Percent" />
			<xsl:apply-templates select="cbc:TaxExemptionReason"/>
			<xsl:call-template name="HRTaxScheme">
				<xsl:with-param name="percent" select="cbc:Percent" />			
			
			</xsl:call-template>
		
		</cac:TaxCategory>
	
	</xsl:template>
	
	
	
	<xsl:template name="HRTaxScheme">
		<xsl:param name="percent" select="0"/>
		<cac:TaxScheme>
			 <cbc:ID>VAT</cbc:ID>
		</cac:TaxScheme>	 
	</xsl:template>

	
	<xsl:template match="cac:DocumentReference" mode="InvoiceLine">
		<xsl:if test="cbc:ID and (normalize-space(cbc:ID) != '')" >
			<cac:DocumentReference>
				<xsl:apply-templates select="cbc:ID" mode="SchemeID"/>
			</cac:DocumentReference>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:DespatchLineReference" mode="InvoiceLine">
	
	<!--TODO: vidjeti da li staviti neko drugo mapiranje - Vindija navodi Order/cbc:ID-->
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')" >
			<cac:DocumentReference>
				<cbc:ID schemeID="AAK">
					<xsl:value-of select="cbc:LineID" />
				</cbc:ID>
			</cac:DocumentReference>
		</xsl:if>
	</xsl:template>
	
	
	<xsl:template match="cac:OrderLineReference" mode="InvoiceLine">
	
	<!--TODO: vidjeti da li staviti neko drugo mapiranje - Vindija navodi Order/cbc:ID-->
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')" >
			<cac:OrderLineReference>
				<xsl:apply-templates select="cbc:LineID" />
			</cac:OrderLineReference>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:InvoicePeriod" mode="InvoiceLine">
		<cac:InvoicePeriod>
			<xsl:apply-templates select="cbc:StartDate" />
			<xsl:apply-templates select="cbc:EndDate" />
		
		</cac:InvoicePeriod>
	
	</xsl:template>

	
	<xsl:template match="cbc:InvoicedQuantity">
		<cbc:InvoicedQuantity>
			<xsl:attribute name="unitCode" namespace="">
				<xsl:value-of select="@unitCode" />
			</xsl:attribute>
			<xsl:value-of select="number(.)" />
			
		</cbc:InvoicedQuantity>
	</xsl:template>



	<xsl:template match="cac:LegalMonetaryTotal">
		<cac:LegalMonetaryTotal>
			<xsl:choose>
				<xsl:when test="cbc:LineExtensionAmount"><xsl:apply-templates select="cbc:LineExtensionAmount" /></xsl:when>
				<xsl:otherwise>
					<cbc:LineExtensionAmount>
						<xsl:copy-of select="cbc:TaxExclusiveAmount/@currencyID" />
						<xsl:value-of select="cbc:TaxExclusiveAmount" />
					</cbc:LineExtensionAmount>
				</xsl:otherwise>
			</xsl:choose>
			
			<xsl:apply-templates select="cbc:TaxExclusiveAmount" />
			<xsl:apply-templates select="cbc:TaxInclusiveAmount" />
			<!--<xsl:apply-templates select="cbc:AllowanceTotalAmount" />-->
			<xsl:call-template name="STSI-INA-AllowanceTotalAmount" />
			<xsl:apply-templates select="cbc:ChargeTotalAmount" />
			<xsl:apply-templates select="cbc:PrepaidAmount" />
			<xsl:apply-templates select="cbc:PayableRoundingAmount" />
			<xsl:apply-templates select="cbc:PayableAmount" />
		
		</cac:LegalMonetaryTotal>
	
	</xsl:template>
	
	<xsl:template name="STSI-INA-AllowanceTotalAmount">
		<xsl:choose>
			<xsl:when test="normalize-space($supplierCompanyId) = '99172175603' and normalize-space($customerCompanyId) = '27759560625' and exists(/*/cbc:Note[contains(lower-case(text()), 'popust:')])">
				<cbc:AllowanceTotalAmount>
					<xsl:attribute name="currencyID">
						<xsl:value-of select="/*/cbc:DocumentCurrencyCode" />
					</xsl:attribute>
					<xsl:value-of select="replace(/*/cbc:Note[contains(lower-case(text()), 'popust:')], '\s*.*:\s*([0-9.]+)\s*.*', '$1')"/>
				</cbc:AllowanceTotalAmount>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="cbc:AllowanceTotalAmount" />
			</xsl:otherwise>
		</xsl:choose>
	
	</xsl:template>
	
	
	<xsl:template match="cac:TaxTotal">
	
		<cac:TaxTotal>
			<xsl:apply-templates select="cbc:TaxAmount" />
			<xsl:apply-templates select="cac:TaxSubtotal" mode="TaxTotal" />
		
		</cac:TaxTotal>
	
	
	</xsl:template>
	
	
	
	<xsl:template match="cac:TaxSubtotal" mode="TaxTotal" >
		<cac:TaxSubtotal>
			<xsl:apply-templates select="cbc:TaxableAmount" />
			<xsl:apply-templates select="cbc:TaxAmount" />
			<xsl:apply-templates select="cac:TaxCategory" mode="TaxTotal" />
		
		</cac:TaxSubtotal>
	
	</xsl:template>
	
	<xsl:template match="cac:TaxCategory" mode="TaxTotal" >
		<cac:TaxCategory>
			
		    <xsl:call-template name="HRTaxCategoryID">
		        <xsl:with-param name="taxCategoryID" select="cbc:ID" />
		    </xsl:call-template>
		    <xsl:choose>
		        <xsl:when test="cbc:Percent and (string(number(cbc:Percent)) != 'NaN')">
		            <xsl:apply-templates select="cbc:Percent" />
		        </xsl:when>
		        <xsl:otherwise>
		            <cbc:Percent>0</cbc:Percent>
		        </xsl:otherwise>
		    </xsl:choose>
		    
		    
		    <xsl:apply-templates select="cbc:TaxExemptionReasonCode" />
		    <xsl:apply-templates select="cbc:TaxExemptionReason" />
		    
		    <xsl:call-template name="HRTaxScheme">
		        <xsl:with-param name="percent" select="cbc:Percent" />			
		    </xsl:call-template>
		</cac:TaxCategory>

	</xsl:template>
	
	<xsl:template match="cbc:TaxExemptionReason">
		<cbc:TaxExemptionReason>
			<xsl:choose>
				<xsl:when test="normalize-space(.) !=''">
					<xsl:value-of select="."/>
				</xsl:when>
				<xsl:otherwise>Porez 0</xsl:otherwise>
			</xsl:choose>
		</cbc:TaxExemptionReason>
	</xsl:template>
	
	<xsl:template match="cbc:TaxExemptionReasonCode" >
		<xsl:if test="normalize-space(../cbc:TaxExemptionReason) = ''">
			<cbc:TaxExemptionReasonCode>
				<xsl:value-of select="." />
			</cbc:TaxExemptionReasonCode>
		</xsl:if>
	</xsl:template>
	

	
	<xsl:template match="cac:AllowanceCharge">
		<cac:AllowanceCharge>
			<xsl:apply-templates select="cbc:ChargeIndicator" />
			<xsl:apply-templates select="cbc:AllowanceChargeReasonCode" />
			<!--<xsl:apply-templates select="cbc:AllowanceChargeReason" />-->
			<xsl:call-template name="AllowanceChargeReason" />
			<xsl:apply-templates select="cbc:MultiplierFactorNumeric" />
			<xsl:apply-templates select="cbc:Amount" />
			<xsl:apply-templates select="cbc:BaseAmount" />
			
		    <xsl:variable name="taxCategoryNode">
		        <xsl:apply-templates select="cac:TaxCategory" mode="AllowanceCharge" />
		    </xsl:variable>
		    
		    <xsl:variable name="currencyID"><xsl:value-of select="cbc:Amount/@currencyID" /></xsl:variable>
		    <xsl:variable name="taxableAmount"><xsl:value-of select="number(cbc:Amount)" /></xsl:variable>
		    <xsl:variable name="percent"><xsl:value-of select="number(cac:TaxCategory/cbc:Percent)" /></xsl:variable>
		    <xsl:call-template name="HRTaxTotal">
		        <xsl:with-param name="currencyID" select="$currencyID" />
		        <xsl:with-param name="taxableAmount" select="$taxableAmount"/>
		        <xsl:with-param name="percent" select="$percent"/>	
		        <xsl:with-param name="taxCategoryNode" select="$taxCategoryNode" />
		    </xsl:call-template>
			
		
		</cac:AllowanceCharge>
	
	</xsl:template>
	
	<xsl:template name="AllowanceChargeReason">
		<xsl:choose>
			<xsl:when test="not(cbc:AllowanceChargeReason) or (normalize-space(cbc:AllowanceChargeReason) = '')">
				<xsl:choose>
					<xsl:when test="cbc:ChargeIndicator = 'false'">
						<cbc:AllowanceChargeReason>POPUST</cbc:AllowanceChargeReason>
					</xsl:when>
					<xsl:otherwise>
						<cbc:AllowanceChargeReason>TROŠAK</cbc:AllowanceChargeReason>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="cbc:AllowanceChargeReason" />
			</xsl:otherwise>
		</xsl:choose>
	
	</xsl:template>
	
	<xsl:template match="cbc:ChargeIndicator" >
		<xsl:variable name="chargeIndicatorValue"><xsl:value-of select="normalize-space(.)" /></xsl:variable>
		<cbc:ChargeIndicator>
			<xsl:choose>
				<xsl:when test="$chargeIndicatorValue = 'true'">true</xsl:when>
				<xsl:when test="$chargeIndicatorValue = 'TRUE'">true</xsl:when>
				<xsl:when test="$chargeIndicatorValue = '1'">true</xsl:when>
				<xsl:when test="$chargeIndicatorValue = 'false'">false</xsl:when>
				<xsl:when test="$chargeIndicatorValue = 'FALSE'">false</xsl:when>
				<xsl:when test="$chargeIndicatorValue = '0'">false</xsl:when>
			</xsl:choose>
		</cbc:ChargeIndicator>
	</xsl:template>
	
	<xsl:template match="cac:PaymentTerms" >
		<cac:PaymentTerms>
			<xsl:apply-templates select="cbc:Note" />
		</cac:PaymentTerms>
	
	</xsl:template>
	
	<xsl:template match="cac:PaymentMeans" >
		<cac:PaymentMeans>
			<xsl:apply-templates select="cbc:PaymentMeansCode"/>
		    <xsl:choose>
		        <xsl:when test="normalize-space(/ns2:Invoice/cbc:DueDate) != ''">
		            <xsl:apply-templates select="/ns2:Invoice/cbc:DueDate" mode="PaymentDueDate" />
		        </xsl:when>
		        <xsl:otherwise><xsl:apply-templates select="cbc:PaymentDueDate" mode="PaymentMeans"/></xsl:otherwise>
		    </xsl:choose>
		    <cbc:PaymentChannelCode listID="Payment Channel" listAgencyID="BII">IBAN</cbc:PaymentChannelCode>
		    
			<xsl:apply-templates select="cbc:PaymentID" mode="InstructionID"/>
			<xsl:apply-templates select="cbc:InstructionNote" />
		    <xsl:apply-templates select="cbc:PaymentID" mode="PaymentID"/>
			<xsl:apply-templates select="cac:CardAccount" />
			<xsl:apply-templates select="cac:PayeeFinancialAccount" mode="PaymentMeans" />
			<xsl:apply-templates select="cac:PaymentMandate" />
			
		
		</cac:PaymentMeans>
	
	</xsl:template>
    <xsl:template match="cbc:PaymentMeansCode" >
        <xsl:variable name="codeVal">
            <xsl:value-of select="." />
        </xsl:variable>
        <cbc:PaymentMeansCode listID="UN/ECE 4461" listAgencyID="6" listName="PaymentMeansCode" listVersionID="D10B" listURI="http://docs.oasis-open.org/ubl/os-UBL-2.1/cl/gc/default/PaymentMeansCode-2.1.gc" listSchemeURI="urn:un:unece:uncefact:codelist:standard:UNECE:PaymentMeansCode:D10B">
            <xsl:choose>
                <xsl:when test="normalize-space($codeVal) = '30'">42</xsl:when>
                <xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
            </xsl:choose>
            
        </cbc:PaymentMeansCode>
        
    </xsl:template>	
    <xsl:template match="cbc:DueDate" mode="PaymentDueDate" >
        <cbc:PaymentDueDate>
            <xsl:value-of select="." />
        </cbc:PaymentDueDate>
    </xsl:template>
	<xsl:template match="cbc:PaymentChannelCode">
		<cbc:PaymentChannelCode listID="Payment Channel" listAgencyID="BII"><xsl:value-of select="." /></cbc:PaymentChannelCode>
	</xsl:template>
	
	<xsl:template match="cac:PaymentMandate" >
		<cac:PaymentMandate>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="cac:PayeeFinancialAccount" mode="PaymentMandate" />
		
		</cac:PaymentMandate>	
	
	</xsl:template>
	
	<xsl:template match="cac:PayeeFinancialAccount" mode="PaymentMandate" >
		<cac:PayeeFinancialAccount>
			<xsl:apply-templates select="cbc:ID" />
		</cac:PayeeFinancialAccount>
	
	</xsl:template>
	
	<xsl:template match="cac:PayeeFinancialAccount" mode="PaymentMeans">
		<cac:PayeeFinancialAccount>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cac:FinancialInstitutionBranch" />
		
		</cac:PayeeFinancialAccount>
	
	</xsl:template>
	
	<xsl:template match="cac:FinancialInstitutionBranch">
		<cac:FinancialInstitutionBranch>
			<xsl:choose>
				<xsl:when test="cbc:ID and (normalize-space(cbc:ID) != '')">
					<xsl:apply-templates select="cbc:ID" />
				</xsl:when>
				<xsl:when test="cac:FinancialInstitution/cbc:ID and (normalize-space(cac:FinancialInstitution/cbc:ID) != '')" >
					<xsl:apply-templates select="cac:FinancialInstitution/cbc:ID" />
				</xsl:when>
			</xsl:choose>
		
		</cac:FinancialInstitutionBranch>
	
	</xsl:template>
	
	<xsl:template match="cac:CardAccount" >
		<cac:CardAccount>
			<xsl:apply-templates select="cbc:PrimaryAccountNumberID" />
			<xsl:apply-templates select="cbc:NetworkID" />
			<xsl:apply-templates select="cbc:HolderName" />
		
		</cac:CardAccount>
	
	</xsl:template>

    <xsl:template match="cbc:PaymentID" mode="InstructionID" >
        <xsl:variable name="instructionID">
			<xsl:choose>
				<xsl:when test="normalize-space(substring-after(substring-before(., ' '),'HR')) != ''">
					<xsl:value-of select="normalize-space(substring-after(substring-before(., ' '),'HR'))" />
				</xsl:when>
				<xsl:when test="string-length(normalize-space(substring-before(., ' '))) = 2">
					<xsl:value-of select="normalize-space(substring-before(., ' '))" />
				</xsl:when>
				<xsl:when test="string-length(normalize-space(.)) = 4 and substring(normalize-space(.), 1, 2) = 'HR'">
					<xsl:value-of select="substring(normalize-space(.), 3, 2)" />
				</xsl:when>
				<xsl:when test="string-length(normalize-space(.)) = 2">
					<xsl:value-of select="normalize-space(.)" />
				</xsl:when>
			</xsl:choose>
        
        </xsl:variable>
        <cbc:InstructionID>
            <xsl:choose>
                <xsl:when test="string($instructionID) != ''">
                    <xsl:value-of select="$instructionID" />
                </xsl:when>
<!--
                <xsl:otherwise>
                    <xsl:value-of select="normalize-space(.)" />
                </xsl:otherwise>
-->
            </xsl:choose>
        </cbc:InstructionID>
        
    </xsl:template>
    
    <xsl:template match="cbc:PaymentID" mode="PaymentID" >
        <xsl:variable name="paymentID">
			<xsl:choose>
				<xsl:when test="substring-after(normalize-space(.), ' ') != ''">
					<xsl:value-of select="substring-after(normalize-space(.), ' ')" />
				</xsl:when>
				<xsl:when test="string-length(normalize-space(.)) &gt;  4">
					<xsl:value-of select="normalize-space(.)" />
				</xsl:when>
				<xsl:when test="substring(normalize-space(.), 1, 2) != 'HR'">
					<xsl:value-of select="normalize-space(.)" />
				</xsl:when>
			</xsl:choose>
        
        </xsl:variable> 
        <cbc:PaymentID>
            <xsl:choose>
                <xsl:when test="string($paymentID) != ''">
                    <xsl:value-of select="$paymentID" />
                </xsl:when>
<!--
                <xsl:otherwise>
                    <xsl:value-of select="normalize-space(.)" />
                </xsl:otherwise>
-->
            </xsl:choose>
        </cbc:PaymentID>
        
    </xsl:template>
	
	
	<xsl:template name="cac:Delivery" >
		<cac:Delivery>
			<xsl:choose>
				<xsl:when test="//ns2:Invoice/cac:Delivery/cbc:ActualDeliveryDate"><xsl:apply-templates select="//ns2:Invoice/cac:Delivery/cbc:ActualDeliveryDate" /></xsl:when>
				<xsl:otherwise>
					<cbc:ActualDeliveryDate>
						<xsl:value-of select="//ns2:Invoice/cbc:IssueDate" />
					</cbc:ActualDeliveryDate>
				
				</xsl:otherwise>
			</xsl:choose>
			
			<xsl:choose>
				<xsl:when test="//ns2:Invoice/cac:Delivery/cac:DeliveryLocation">
					<xsl:apply-templates select="//ns2:Invoice/cac:Delivery/cac:DeliveryLocation" />
				</xsl:when>
				<xsl:when test="//ns2:Invoice/cac:Delivery/cac:DeliveryAddress">
					<xsl:apply-templates select="cac:DeliveryAddress" />
				</xsl:when>
			</xsl:choose>
			
			<xsl:apply-templates select="//ns2:Invoice/cac:Delivery/cac:DeliveryParty" />
		
		</cac:Delivery>
	
	</xsl:template>
	
	<xsl:template match="cac:DeliveryParty" >
		<cac:DeliveryParty>
			<xsl:apply-templates select="cac:PartyName" />
		
		</cac:DeliveryParty>
	
	</xsl:template>
	
	<xsl:template match="cac:DeliveryAddress">
		<cac:DeliveryLocation>
			<cac:Address>
				<!--<xsl:apply-templates select="cbc:StreetName" />-->
				<xsl:call-template name="StreetName" />
				<xsl:apply-templates select="cbc:AdditionalStreetName" />
				<xsl:apply-templates select="cbc:CityName" />
				<xsl:apply-templates select="cbc:PostalZone" />
				<xsl:apply-templates select="cbc:CountrySubentity" />
				<xsl:apply-templates select="cac:AddressLine" />
				<!--<xsl:apply-templates select="cac:Country" />-->
				<xsl:call-template name="Country">
					<xsl:with-param name="countryCode" select="cac:Country/cbc:IdentificationCode" />
				</xsl:call-template>

			
			</cac:Address>
		</cac:DeliveryLocation>
	
	</xsl:template>
	
	<xsl:template match="cac:DeliveryLocation" >
		<cac:DeliveryLocation>
			<xsl:apply-templates select="cbc:ID" mode="SchemeID" />
			<xsl:apply-templates select="cac:Address" />
			<xsl:apply-templates select="cac:DeliveryParty" />
			
		</cac:DeliveryLocation>
	
	</xsl:template>
	
<!--	<xsl:template match="cac:DeliveryParty" >
		<cac:DeliveryParty>
			<xsl:apply-templates select="cac:PartyName" />
		
		</cac:DeliveryParty>
	
	</xsl:template>
-->	
	<xsl:template match="cac:Address">
		<cac:Address>
			<!--<xsl:apply-templates select="cbc:StreetName" />-->
			<cbc:AddressFormatCode listID="UN/ECE 3477" listAgencyID="6">1</cbc:AddressFormatCode>
			<xsl:apply-templates select="cbc:StreetName" />
			<xsl:apply-templates select="cbc:BuildingNumber" />
			<xsl:apply-templates select="cbc:CityName" />
			<xsl:apply-templates select="cbc:PostalZone" />
			<xsl:apply-templates select="cbc:CountrySubentity" />
			<xsl:call-template name="AddressLine" />
			<!--<xsl:apply-templates select="cac:Country" />-->
			<xsl:copy-of select="cac:Country" />
		
		</cac:Address>	
	</xsl:template>
	
	
	
	<xsl:template match="cac:AccountingCustomerParty">
		<cac:AccountingCustomerParty>
		
			<xsl:apply-templates select="cac:Party" mode="AccountingCustomerParty" />
			<xsl:apply-templates select="cac:Party/cac:Contact" />
		
		</cac:AccountingCustomerParty>
	
	</xsl:template>
    <xsl:template match="cac:Contact">
        <cac:AccountingContact>
            <xsl:apply-templates select="cbc:Name" />
            <xsl:apply-templates select="cbc:ElectronicMail" />
            <xsl:apply-templates select="cbc:Note" />
        </cac:AccountingContact>
        
        
    </xsl:template>
	
		<xsl:template match="cac:Party" mode="AccountingCustomerParty" >
		<cac:Party>
			<!--<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="EndpointID" />-->
		    <xsl:apply-templates select="cbc:EndpointID" mode="SchemeID"/>
		    
<!--		    <xsl:call-template name="CustomerPartyIdentification" />-->
			<cac:PartyIdentification>
				<cbc:ID><xsl:value-of select="$customerCompanyId" /></cbc:ID>
			</cac:PartyIdentification>
	
			
			<xsl:call-template name="PartyName" />
<!--
		    <xsl:apply-templates select="cac:PartyIdentification" />
			<xsl:copy-of select="cac:PartyName" />
-->
			<xsl:apply-templates select="cac:PostalAddress" />
			<!--<xsl:call-template name="PartyTaxScheme" />-->
		    <xsl:call-template name="AccountingCustomerPartyLegalEntity" />
			<xsl:copy-of select="cac:Contact" />
		
		</cac:Party>
	
	</xsl:template>

	
	<xsl:template name="CustomerPartyIdentification" >
		<xsl:choose>
			<xsl:when test="cac:PartyIdentification/cbc:ID and normalize-space(cac:PartyIdentification/cbc:ID) != ''">
				<xsl:apply-templates select="cac:PartyIdentification" />
			</xsl:when>
			<xsl:otherwise>
				<cac:PartyIdentification>
					<cbc:ID><xsl:value-of select="$customerCompanyId" /></cbc:ID>
				</cac:PartyIdentification>

			</xsl:otherwise>
		</xsl:choose>
    
    </xsl:template>
	
	
	<xsl:template name="PartyName" >
		<xsl:choose>
			<xsl:when test="normalize-space(cac:PartyName) != ''">
				<xsl:apply-templates select="cac:PartyName" mode="copy" />
			</xsl:when>
			<xsl:otherwise>
				<cac:PartyName>
					<cbc:Name><xsl:value-of select="cac:PartyLegalEntity/cbc:RegistrationName" /></cbc:Name>
				</cac:PartyName>
			
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>	
	
	
    <xsl:template name="AccountingCustomerPartyLegalEntity" >
        <cac:PartyLegalEntity>
            <xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName" />
            <cbc:CompanyID>
                <xsl:value-of select="$customerCompanyId" />
            </cbc:CompanyID>
            
            
        </cac:PartyLegalEntity>
        
    </xsl:template>
	
	<xsl:template match="cac:PartyLegalEntity" mode="AccountingCustomerParty" >
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:RegistrationName" />
			<xsl:apply-templates select="cbc:CompanyID" mode="SchemeID" />
		
		</cac:PartyLegalEntity>
	
	</xsl:template>
    <xsl:template match="cbc:EndpointID" mode="CompanyID">
        
        <cbc:CompanyID>
            <xsl:value-of select="." />
        </cbc:CompanyID>
    </xsl:template>
	
	<xsl:template match="cac:AccountingSupplierParty" >
		<cac:AccountingSupplierParty>
			<xsl:apply-templates select="cac:Party" mode="AccountingSupplierParty" />
			<xsl:call-template name="AccountingContact"/>
		
		</cac:AccountingSupplierParty>
	
	</xsl:template>
    <xsl:template match="cbc:ID" mode="PartyID2CompanyID">
        <xsl:variable name="id1"><xsl:value-of select="substring-after(., ':')" /></xsl:variable>
        <xsl:variable name="id2"><xsl:value-of select="substring-before($id1, '::HR99')" /></xsl:variable>
        
        <cbc:CompanyID>
            <xsl:choose>
                <xsl:when test="normalize-space($id2) != ''"><xsl:value-of select="$id2" /></xsl:when>
                <xsl:when test="normalize-space($id1) != ''"><xsl:value-of select="$id1" /></xsl:when>
                <xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
            </xsl:choose>
        </cbc:CompanyID>
    </xsl:template>
    <xsl:template match="cbc:CompanyID" mode="TaxID2CompanyID">
        
        <cbc:CompanyID>
            <xsl:value-of select="substring-after(., 'HR')" />
        </cbc:CompanyID>
    </xsl:template>
	
	<xsl:template match="cac:Party" mode="AccountingSupplierParty" >
		<cac:Party>
			<!--<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="EndpointID" />-->
		    <xsl:apply-templates select="cbc:EndpointID" mode="SchemeID"/>
		    <xsl:call-template name="SupplierPartyIdentification" />
<!--			<cac:PartyIdentification>
				<cbc:ID><xsl:value-of select="$supplierCompanyId" /></cbc:ID>
			</cac:PartyIdentification>
-->
			<xsl:call-template name="PartyName" />

<!--
			<xsl:apply-templates select="cac:PartyIdentification" mode="AccountingSupplierParty"/>
			<xsl:copy-of select="cac:PartyName" />
-->
			<xsl:apply-templates select="cac:PostalAddress" />
			<!--<xsl:call-template name="PartyTaxScheme" />-->
			<xsl:call-template name="AccountingSupplierPartyLegalEntity"/>
			<xsl:copy-of select="cac:Contact" />
		
		</cac:Party>
	
	</xsl:template>


    <xsl:template name="SupplierPartyIdentification" >
		<xsl:choose>
			<xsl:when test="cac:PartyIdentification/cbc:ID and normalize-space(cac:PartyIdentification/cbc:ID) != ''">
				<!--<xsl:apply-templates select="cac:PartyIdentification" />-->
				<xsl:apply-templates select="cac:PartyIdentification" mode="AccountingSupplierParty"/>
			</xsl:when>
			<xsl:otherwise>
				<cac:PartyIdentification>
					<cbc:ID><xsl:value-of select="$supplierCompanyId" /></cbc:ID>
				</cac:PartyIdentification>

			</xsl:otherwise>
		</xsl:choose>
    
    </xsl:template>
	
    <xsl:template match="cac:PartyIdentification" mode="AccountingSupplierParty">
        <xsl:if test="cbc:ID and (normalize-space(cbc:ID) != '')">
            
            <xsl:variable name="id1" select="substring-after(cbc:ID, ':')" />
            <xsl:variable name="id2" select="substring-after($id1, '::HR99:')" />
            <xsl:choose>
                <xsl:when test="normalize-space($id2) != ''">
                    <cac:PartyIdentification>
                        <cbc:ID><xsl:value-of select="$id2" /></cbc:ID>
                    </cac:PartyIdentification>	
                </xsl:when>
                <xsl:when test="normalize-space($id1) != ''"><cbc:ID><xsl:value-of select="$id1" /></cbc:ID></xsl:when>
				<!--<xsl:otherwise><cbc:ID><xsl:value-of select="cbc:ID" /></cbc:ID></xsl:otherwise>-->
				<xsl:otherwise>
					<cac:PartyIdentification>
						<cbc:ID><xsl:value-of select="$supplierCompanyId" /></cbc:ID>
					</cac:PartyIdentification>
	
				</xsl:otherwise>
           </xsl:choose>
            
            
        </xsl:if>
    </xsl:template>
    
    
    <xsl:template match="cac:PartyIdentification" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:PartyIdentification>
				<xsl:apply-templates select="cbc:ID" mode="SchemeID" />
			</cac:PartyIdentification>
		
		</xsl:if>
	
	</xsl:template>

    <xsl:template name="AccountingSupplierPartyLegalEntity" >
        <cac:PartyLegalEntity>
            <xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName" />
            <!--<xsl:apply-templates select="cbc:CompanyID" mode="SchemeID" />-->
            
            <cbc:CompanyID>
                <xsl:value-of select="$supplierCompanyId" />
            </cbc:CompanyID>
            
            
        </cac:PartyLegalEntity>
        
    </xsl:template>
    <xsl:template match="cbc:EndpointID" mode="SchemeID">
        <xsl:if test="@schemeID and (normalize-space(@schemeID) = '0088')">
            <cbc:EndpointID schemeID="GLN">
                <xsl:value-of select="." />
            </cbc:EndpointID>
            
        </xsl:if>
    </xsl:template>
	
	
	<xsl:template name="AccountingContact">
		<xsl:variable name="odgovornaOsoba">
			<xsl:apply-templates select="/ns2:Invoice/cbc:Note" mode="OdgovornaOsoba" />
		</xsl:variable>
		<xsl:variable name="oznakaOperatera">
<!--			<xsl:apply-templates select="/ns2:Invoice/cbc:Note" mode="OznakaOperatera" />-->
			<xsl:value-of select="normalize-space(//cac:AccountingSupplierParty/cac:SellerContact/cbc:Name)" />
		</xsl:variable>
		<cac:AccountingContact>
			<xsl:choose>
				<xsl:when test="normalize-space($odgovornaOsoba) != ''">
					<cbc:Name>
						<xsl:value-of select="$odgovornaOsoba" />
					</cbc:Name>
				</xsl:when>
				<xsl:when test="normalize-space($oznakaOperatera) != ''">
					<cbc:Name>
						<xsl:value-of select="$oznakaOperatera" />
					</cbc:Name>
				</xsl:when>
				<xsl:otherwise>
					<!--<xsl:apply-templates select="/ns2:Invoice/cbc:Note" mode="OdgovornaOsoba"/>-->
				</xsl:otherwise>
			</xsl:choose>
			<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ElectronicMail" />
			
			
			<cbc:Note>
				<xsl:choose>
					<xsl:when test="normalize-space($oznakaOperatera) != ''">
						<!--<xsl:apply-templates select="/ns2:Invoice/cbc:Note" mode="OznakaOperatera"/>-->
						<!--Oznaka operatera: --><xsl:value-of select="$oznakaOperatera"/>
					</xsl:when>
					<xsl:when test="normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:Name) != ''">
						<xsl:value-of select="normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:Name)" />
					</xsl:when>
					<xsl:otherwise>
						<!--<xsl:apply-templates select="/ns2:Invoice/cbc:Note" mode="OznakaOperatera"/>-->
					</xsl:otherwise>
				</xsl:choose>
			 </cbc:Note>
			
		</cac:AccountingContact>
		
		
	</xsl:template>
	
    <xsl:template match="cbc:Note" mode="OznakaOperatera">
    
		<xsl:choose>
                                                                        
			<xsl:when test="(normalize-space(substring-before(., '#AAI#')) != '') and contains(., 'perater') and contains(., ':')" >
				<!-- Ako ima #AAI# i tekst Operater sa dvotočkom - malo ili veliko slovo -->
				<xsl:value-of	select="normalize-space(substring-after(substring-before(., '#AAI#'), ':'))" />			
			</xsl:when>
			<xsl:when test="contains(., 'Oznaka operatera:')" >
				<!-- Ako ima Oznaka operatera: -->
				<xsl:value-of	select="normalize-space(substring-after(., 'Oznaka operatera:'))" />			
			</xsl:when>
			<xsl:when test="contains(., 'Operater:')" >
				<!-- Ako ima tekst Operater sa dvotočkom  -->
				<xsl:value-of	select="normalize-space(substring-after(., 'Operater:'))" />			
			</xsl:when>
			<xsl:when test="contains(., 'Operator:')" >
				<!-- Ako ima tekst Operator sa dvotočkom  -->
				<xsl:value-of	select="normalize-space(substring-after(., 'Operator:'))" />			
			</xsl:when>
			<xsl:when test="contains(., '#Oznaka operatera') " >
				<!-- Ako ima tekst #Oznaka operatera - oznaka je prije toga -->
				<xsl:value-of	select="normalize-space(substring-before(., '#Oznaka operatera'))" />			
			</xsl:when>
			<xsl:when test="contains(., 'Blagajnik:')" >
				<!-- Ako ima tekst Blagajnik -->
				<xsl:value-of	select="normalize-space(substring-after(., 'Blagajnik:'))" />			
			</xsl:when>
		</xsl:choose>
 <!--       <xsl:variable name="oznaka">
            <xsl:value-of select="normalize-space(substring-before(., '#Oznaka operatera'))" />
        </xsl:variable>
        <xsl:variable name="oznaka2">
            <xsl:value-of select="normalize-space(substring-after(., 'Oznaka operatera:'))" />
        </xsl:variable>
        <xsl:variable name="oznaka3">
            <xsl:value-of select="normalize-space(substring-after(., 'Operater:'))" />
        </xsl:variable>
        <xsl:variable name="oznaka4">
            <xsl:value-of select="normalize-space(substring-after(., 'Operator:'))" />
        </xsl:variable>
        <xsl:variable name="oznaka5">
            <xsl:value-of select="normalize-space(substring-after(., 'Oznaka operatera#'))" />
        </xsl:variable>
        <xsl:if test="normalize-space($oznaka) != ''">
            <cbc:Note><xsl:value-of select="$oznaka"/></cbc:Note>
        </xsl:if>
        <xsl:if test="normalize-space($oznaka2) != ''">
            <cbc:Note><xsl:value-of select="$oznaka2"/></cbc:Note>
        </xsl:if>
        <xsl:if test="normalize-space($oznaka3) != ''">
            <cbc:Note><xsl:value-of select="$oznaka3"/></cbc:Note>
        </xsl:if>
        <xsl:if test="normalize-space($oznaka4) != ''">
            <cbc:Note><xsl:value-of select="$oznaka4"/></cbc:Note>
        </xsl:if>
        <xsl:if test="normalize-space($oznaka5) != ''">
            <cbc:Note><xsl:value-of select="$oznaka5"/></cbc:Note>
        </xsl:if>
-->    </xsl:template>
    
    
    <xsl:template match="cbc:Note" mode="OdgovornaOsoba">
		<xsl:choose>
			<xsl:when test="(normalize-space(substring-before(., '#AAI#')) != '') and contains(., 'dgovorna osoba') and contains(., ':')" >
				<!-- Ako ima #AAI# i tekst Operater sa dvotočkom - malo ili veliko slovo -->
				<xsl:value-of	select="normalize-space(substring-after(substring-before(., '#AAI#'), ':'))" />			
			</xsl:when>
			<xsl:when test="contains(., 'dgovorna') and contains(.,  'soba') and contains(., ':')" >
				<!-- Ako ima Oznaka operatera: -->
				<xsl:value-of	select="normalize-space(substring-after(., ':'))" />			
			</xsl:when>
			<xsl:when test="contains(., '#Odgovorna osoba') " >
				<!-- Ako ima tekst #Oznaka operatera - oznaka je prije toga -->
				<xsl:value-of	select="normalize-space(substring-before(., '#Odgovorna osoba'))" />			
			</xsl:when>
			<xsl:when test="contains(., 'dgovorna osoba') and contains(., '#')" >
				<!-- Ako ima Oznaka operatera: -->
				<xsl:value-of	select="normalize-space(substring-before(., '#'))" />			
			</xsl:when>
			<xsl:when test="contains(., 'dgovorna osoba') " >
				<!-- Ako ima Oznaka operatera: -->
				<xsl:value-of	select="normalize-space(substring-after(., 'dgovorna osoba'))" />			
			</xsl:when>
			<xsl:when test="contains(., 'Responsible') and contains(., 'erson:')" >
				<!-- Ako ima tekst Responsible Person: - person malo ili veliko -->
				<xsl:value-of	select="normalize-space(substring-after(., 'erson:'))" />			
			</xsl:when>
		</xsl:choose>

<!--        <xsl:variable name="oznaka">
            <xsl:value-of select="normalize-space(substring-before(., '#Odgovorna osoba'))" />
        </xsl:variable>
        <xsl:variable name="oznaka2">
            <xsl:value-of select="normalize-space(substring-after(., 'Odgovorna osoba:'))" />
        </xsl:variable>
        <xsl:variable name="oznaka3">
            <xsl:value-of select="normalize-space(substring-after(., 'Responsible Person:'))" />
        </xsl:variable>
        <xsl:variable name="oznaka4">
            <xsl:value-of select="normalize-space(substring-after(., 'Odgovorna osoba#'))" />
        </xsl:variable>
        <xsl:if test="normalize-space($oznaka) != ''">
            <cbc:Name><xsl:value-of select="$oznaka"/></cbc:Name>
        </xsl:if>
        <xsl:if test="normalize-space($oznaka2) != ''">
            <cbc:Name><xsl:value-of select="$oznaka2"/></cbc:Name>
        </xsl:if>
        <xsl:if test="normalize-space($oznaka3) != ''">
            <cbc:Name><xsl:value-of select="$oznaka3"/></cbc:Name>
        </xsl:if>
        <xsl:if test="normalize-space($oznaka4) != ''">
            <cbc:Name><xsl:value-of select="$oznaka4"/></cbc:Name>
        </xsl:if>
-->    </xsl:template>
	
	<xsl:template match="cac:AccountingContact">
		<cac:AccountingContact>
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cbc:ElectronicMail" />
			<xsl:apply-templates select="cbc:Note" />
		</cac:AccountingContact>
	
	
	</xsl:template>
	
	<xsl:template match="cac:PostalAddress" >
		<cac:PostalAddress>
			<cbc:AddressFormatCode listID="UN/ECE 3477" listAgencyID="6">1</cbc:AddressFormatCode>
			<xsl:apply-templates select="cbc:StreetName" />
			<xsl:apply-templates select="cbc:BuildingNumber" />
			<xsl:apply-templates select="cbc:CityName" />
			<xsl:apply-templates select="cbc:PostalZone" />
			<xsl:apply-templates select="cbc:CountrySubentity" />
			<xsl:call-template name="AddressLine" />
			<!--<xsl:apply-templates select="cac:Country" />-->
			<xsl:copy-of select="cac:Country" />
				
		</cac:PostalAddress>
	
	</xsl:template>
	
	<xsl:template match="cac:Country" >
		<cac:Country>
			<xsl:apply-templates select="cbc:IdentificationCode" />
		
		</cac:Country>
	
	</xsl:template>
	
	<xsl:template name="AddressLine">
		<xsl:choose>
			<xsl:when test="cac:AddressLine"><xsl:copy-of select="cac:AddressLine" /></xsl:when>
			<xsl:otherwise>
				<cac:AddressLine>
					<cbc:Line>
						<xsl:value-of select="normalize-space(concat(cbc:StreetName, ' ', cbc:BuildingNumber))" />
					</cbc:Line>
				</cac:AddressLine>
			</xsl:otherwise>
		
		</xsl:choose>
	
	</xsl:template>
	
	<xsl:template match="cac:AddressLine">
		<cac:AddressLine>
			<xsl:apply-templates select="cbc:Line" />
		</cac:AddressLine>
	
	</xsl:template>
	
	<xsl:template name="StreetName">
		<xsl:choose>
			<xsl:when test="cbc:StreetName and (normalize-space(cbc:StreetName) != '')">
				<cbc:StreetName>
					<xsl:value-of select="cbc:StreetName" />
					<xsl:if test="cbc:BuildingNumber and (normalize-space(cbc:BuildingNumber) != '')" >
						<xsl:value-of select="concat(' ', normalize-space(cbc:BuildingNumber))" />
					</xsl:if>
				</cbc:StreetName>
			</xsl:when>
			<xsl:when test="cbc:AddressLine and (normalize-space(cbc:AddressLine) != '')">
				<cbc:StreetName>
					<xsl:value-of select="cbc:AddressLine" />
				</cbc:StreetName>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="cac:PartyName" >
		<cac:PartyName>
			<xsl:choose>
				<xsl:when test="cbc:Name and (normalize-space(cbc:Name) != '')">
					<cbc:Name><xsl:value-of select="cbc:Name" /></cbc:Name>
				</xsl:when>
				<xsl:when test="../cac:PartyLegalEntity/cbc:RegistrationName and (normalize-space(../cac:PartyLegalEntity/cbc:RegistrationName) != '')">
					<cbc:Name><xsl:value-of select="../cac:PartyLegalEntity/cbc:RegistrationName" /></cbc:Name>
				
				</xsl:when>
			</xsl:choose>
		</cac:PartyName>
	
	</xsl:template>
	
	
	<xsl:template match="cac:ProjectReference" >
		<cac:ProjectReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
		</cac:ProjectReference>
	
	</xsl:template>
	
	<xsl:template match="cac:AdditionalDocumentReference" mode="FiscalAdditionalDocumentReference">
		<cac:AdditionalDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="FiscalAdditionalDocumentReference" />
			<xsl:apply-templates select="cbc:IssueDate"/>
			<xsl:apply-templates select="cbc:DocumentType" />
		</cac:AdditionalDocumentReference>
		
	</xsl:template>
	<xsl:template match="cbc:ID" mode="FiscalAdditionalDocumentReference">
		<cbc:ID>
			<xsl:choose>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID = '15538072333'">
					<xsl:choose>
						<xsl:when test="contains(.,'RIZ')">
							<xsl:value-of select="."/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
								<xsl:when test="contains(../../cbc:ID,'RIZ')">
									<xsl:value-of select="../../cbc:ID"/>
								</xsl:when>	
								<xsl:otherwise>
									<xsl:value-of select="."/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID = '99172175603'">
					<xsl:choose>
						<xsl:when test="contains(.,'STSI')">
							<xsl:value-of select="."/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
								<xsl:when test="contains(../../cbc:ID,'STSI')">
									<xsl:value-of select="../../cbc:ID"/>
								</xsl:when>	
								<xsl:otherwise>
									<xsl:value-of select="."/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="."/>
				</xsl:otherwise>
			</xsl:choose>
		</cbc:ID>
	</xsl:template>
	
	
	<xsl:template match="cbc:ID" mode="AdditionalDocumentReference">
		<cac:AdditionalDocumentReference>
			<cbc:ID><xsl:value-of select="." /></cbc:ID>
					
		</cac:AdditionalDocumentReference>
	
	</xsl:template>
	

	<xsl:template match="cbc:ID" mode="FiscalNumber">
		<cbc:ID>
			<xsl:choose>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID = '15538072333'">
					<xsl:choose>
						<xsl:when test="not(contains(.,'RIZ'))">
							<xsl:value-of select="."/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
								<!--<xsl:when test="../cac:AdditionalDocumentReference/cbc:ID[contains(text(),'RIZ')]">-->
								<xsl:when test="../cac:AdditionalDocumentReference[contains(cbc:DocumentType/text(),'SAP broj')]">
									<xsl:value-of select="../cac:AdditionalDocumentReference[contains(cbc:DocumentType/text(),'SAP broj')]/cbc:ID"/>

<!--								<xsl:when test="not(contains(../cac:AdditionalDocumentReference/cbc:ID,'RIZ'))">
									<xsl:value-of select="../cac:AdditionalDocumentReference/cbc:ID"/>
-->								</xsl:when>	
								<xsl:otherwise>
									<xsl:value-of select="."/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID = '99172175603'">
					<xsl:choose>
						<xsl:when test="not(contains(.,'STSI'))">
							<xsl:value-of select="."/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
<!--								<xsl:when test="not(contains(../cac:AdditionalDocumentReference/cbc:ID,'STSI'))">
									<xsl:value-of select="../cac:AdditionalDocumentReference/cbc:ID"/>
-->
							<!--<xsl:when test="../cac:AdditionalDocumentReference/cbc:ID[contains(text(),'STSI')]">-->
							<xsl:when test="../cac:AdditionalDocumentReference[contains(cbc:DocumentType/text(),'SAP broj')]">
									<xsl:value-of select="../cac:AdditionalDocumentReference[contains(cbc:DocumentType/text(),'SAP broj')]/cbc:ID"/>
							</xsl:when>	
								<xsl:otherwise>
									<xsl:value-of select="."/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="."/>
				</xsl:otherwise>
			</xsl:choose>
		</cbc:ID>
	</xsl:template>
	


	
	<xsl:template match="cac:ExternalReference" >
		<cac:ExternalReference>
			<xsl:apply-templates select="cbc:URI" />
		
		</cac:ExternalReference>
	
	</xsl:template>
	
	
	<xsl:template match="cbc:EmbeddedDocumentBinaryObject">
		<cbc:EmbeddedDocumentBinaryObject>
			<xsl:attribute name="mimeCode" namespace="">
				<xsl:value-of select="@mimeCode"/>
			</xsl:attribute>
		
			<xsl:attribute name="filename" namespace="">
				<xsl:value-of select="translate(@filename, ':/\&amp;', '-__ ')"/>
			</xsl:attribute>

			<xsl:value-of select="."/>
			
		</cbc:EmbeddedDocumentBinaryObject>
	
	</xsl:template>
	
	<xsl:template match="cac:ContractDocumentReference" >
		<cac:ContractDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
		</cac:ContractDocumentReference>
	
	</xsl:template>

	<xsl:template match="cac:OriginatorDocumentReference" >
		<cac:OriginatorDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
		</cac:OriginatorDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="cac:ReceiptDocumentReference" >
		<cac:ReceiptDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
		</cac:ReceiptDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="cac:DespatchDocumentReference" >
		<cac:DespatchDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
		</cac:DespatchDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="cac:BillingReference" >
		<cac:BillingReference>
			<xsl:apply-templates select="cac:InvoiceDocumentReference" />
		
		</cac:BillingReference>	
	
	</xsl:template>
	
	<xsl:template match="cac:InvoiceDocumentReference" >
	<!--ISPRAVAK: V3 15.07.2019-->
		<cac:InvoiceDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
			<xsl:apply-templates select="cbc:IssueDate" />
		</cac:InvoiceDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="cac:OrderReference" >
		<cac:OrderReference>
			<xsl:apply-templates select="cbc:ID" mode="OrderReferenceID" />
			<xsl:apply-templates select="cbc:SalesOrderID" />		
			<xsl:apply-templates select="cbc:IssueDate"/>
			<xsl:apply-templates select="cbc:IssueTime"/>
		</cac:OrderReference>
	
	</xsl:template>
	
	
	<xsl:template match="cac:InvoicePeriod" >
		<cac:InvoicePeriod>
			<xsl:apply-templates select="cbc:StartDate" />
			<xsl:apply-templates select="cbc:EndDate" />
			<xsl:apply-templates select="cbc:DescriptionCode" />
		
		</cac:InvoicePeriod>
	
	</xsl:template>
	<xsl:template match="cbc:IssueDate">
		<cbc:IssueDate>
			<xsl:value-of select="."/>
		</cbc:IssueDate>
	</xsl:template>

	<xsl:template match="cbc:SalesOrderID">
		<cbc:IssueTime>
			<xsl:value-of select="."/>
		</cbc:IssueTime>
	</xsl:template>
	
	<xsl:template match="cbc:DescriptionCode">
	<!-- Vidjeti da li trebaju atributti - iskoristiti Copy-of u tom slučaju-->
		<cbc:DescriptionCode>
			<xsl:value-of select="." />
		</cbc:DescriptionCode>
		
	
	</xsl:template>
	

	<xsl:template match="cbc:InvoiceTypeCode">
		<cbc:InvoiceTypeCode>
			<xsl:choose>
				<xsl:when test="normalize-space(.) != '394'">
					<xsl:value-of select="."/></xsl:when>
				<xsl:otherwise>380</xsl:otherwise>
			</xsl:choose>
		</cbc:InvoiceTypeCode>
	</xsl:template>
	
	<xsl:template match="cac:PaymentMeans/cbc:PaymentDueDate">
		<cbc:DueDate>
			<xsl:value-of select="."/>
		</cbc:DueDate>
	</xsl:template>
	
	<xsl:template match="cbc:PaymentDueDate" mode="PaymentMeans">
		<cbc:PaymentDueDate>
			<xsl:value-of select="."/>
		</cbc:PaymentDueDate>
	</xsl:template>
	
	
	
	<!-- GENERIC TEMPLATES-->
	
	<xsl:template match="cbc:ElectronicMail">
		<cbc:ElectronicMail>
			<xsl:if test="not((normalize-space(.) = 'fina@moj-eracun.hr') or (normalize-space(.) = 'adresar@moj-eracun.hr'))" >
				<xsl:value-of select="." />
			</xsl:if>
		</cbc:ElectronicMail>
	</xsl:template>
	
	<xsl:template name="EndpointID">
		<xsl:choose>
			<xsl:when test="cbc:EndpointID and (normalize-space(cbc:EndpointID) != '')">
				<xsl:apply-templates select="cbc:EndpointID" mode="SchemeID" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="EndpointID" />
			</xsl:otherwise>
		</xsl:choose>
	
	</xsl:template>

<!--
	<xsl:template match="cbc:IssueTime">
		<cbc:IssueTime>
			<xsl:value-of select="."/>
		</cbc:IssueTime>
	</xsl:template>
-->
	
	<xsl:template match="cbc:IssueTime">
		<cbc:IssueTime>
			<xsl:choose>
				<xsl:when test="normalize-space(.) = '00:00:00'"><xsl:value-of select="'00:00:01'" /></xsl:when>
				<xsl:when test="normalize-space(.) = '24:00:00'"><xsl:value-of select="'00:00:01'" /></xsl:when>
				<xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
			</xsl:choose>
		</cbc:IssueTime>

	</xsl:template>
	

	<!-- GENERIC ELEMENTS-->
	
	<xsl:template name="Country">
		<xsl:param name="countryCode" select="'HR'" />
		<cac:Country>
			<xsl:choose>
				<xsl:when test="normalize-space($countryCode) != ''">
					<cbc:IdentificationCode><xsl:value-of select="$countryCode" /></cbc:IdentificationCode>			
				</xsl:when>
				<xsl:otherwise>
					<cbc:IdentificationCode>HR</cbc:IdentificationCode>
				</xsl:otherwise>
			</xsl:choose>
			

		</cac:Country>
		
	
	</xsl:template>
	
	<xsl:template match="cbc:Amount | cbc:BaseAmount | cbc:TaxAmount | cbc:TaxableAmount
		| cbc:LineExtensionAmount | cbc:TaxExclusiveAmount | cbc:TaxInclusiveAmount
		| cbc:AllowanceTotalAmount | cbc:ChargeTotalAmount | cbc:PrepaidAmount
		| cbc:PayableRoundingAmount | cbc:PayableAmount " >
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:attribute name="currencyID" namespace="">
				<xsl:value-of select="@currencyID" />
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="number(.) = 0">0.00</xsl:when>
				<xsl:when test="string(number(.))='NaN'">0.00</xsl:when>
				<xsl:otherwise><xsl:value-of select="format-number(number(.), '#########0.00;-#########0.00')" /></xsl:otherwise>
			</xsl:choose>
			
		</xsl:element>
	
	</xsl:template>
	
	<xsl:template match="cbc:PriceAmount" >
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:attribute name="currencyID" namespace="">
				<xsl:value-of select="@currencyID" />
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="number(.) = 0">0.00</xsl:when>
				<xsl:when test="string(number(.))='NaN'">0.00</xsl:when>
				<xsl:otherwise>
					<xsl:variable name="priceAmount">
						<xsl:value-of select="format-number(number(.), '#########0.00;-#########0.00')" />
					</xsl:variable>
					<xsl:choose>
						<xsl:when test="string-length(string(.)) &gt; string-length(string($priceAmount))">
							<xsl:value-of select="." />
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$priceAmount" />
						</xsl:otherwise>
					</xsl:choose>
				
				</xsl:otherwise>
			</xsl:choose>
			
		</xsl:element>
	
	</xsl:template>
	
	<xsl:template match="cbc:CopyIndicator">
		<cbc:CopyIndicator>
			<xsl:value-of select="translate(., 'falseFALSEtrueTRUE', 'falsefalsetruetrue')" />
		</cbc:CopyIndicator>
	
	</xsl:template>

	
	<xsl:template match="cbc:MultiplierFactorNumeric">
		<cbc:MultiplierFactorNumeric>
			<xsl:value-of select="number(.)" />
		</cbc:MultiplierFactorNumeric>
	
	</xsl:template>

	<xsl:template match="cbc:Percent">
		<cbc:Percent>
			<xsl:value-of select="number(.)" />
		</cbc:Percent>
	
	</xsl:template>
	
	<xsl:template match="cbc:CompanyID" mode="PartyIdentification">
		<cac:PartyIdentification>
			<cbc:ID>
				<xsl:value-of select="concat('9934:', .)"/>
			</cbc:ID>
		
		</cac:PartyIdentification>
	
	</xsl:template>
	
	<xsl:template match="cbc:CompanyID" mode="PartyTaxID">
		<cbc:CompanyID>
			<xsl:value-of select="concat('HR', .)"/>
		</cbc:CompanyID>
	
	</xsl:template>

	
	<xsl:template match="cbc:CompanyID" mode="EndpointID" >
		<cbc:EndpointID>
			<xsl:attribute name="schemeID" namespace="">9934</xsl:attribute>
			<xsl:value-of select="." />
		
		</cbc:EndpointID>
	
	</xsl:template>

	<xsl:template match="cbc:ID" mode="TaxCategoryID">
		<cbc:ID>
			<xsl:choose>
				<xsl:when test="normalize-space(.) = 'O'">
					<xsl:choose>
						<xsl:when test="$isOutsideVAT = 'true'">O</xsl:when>
						<xsl:otherwise>E</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise><xsl:value-of select="normalize-space(.)" /></xsl:otherwise>
			</xsl:choose>
		
		</cbc:ID>
	
	</xsl:template>
	
	<xsl:template match="cbc:ID | cbc:CompanyID " mode="SchemeID" >
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:apply-templates select="@schemeID" />

			<xsl:value-of select="." />
		</xsl:element>
	</xsl:template>
	
	<xsl:template match="@schemeID" >
		<xsl:attribute name="schemeID" namespace="">
			<xsl:value-of select="." />
		</xsl:attribute>
	
	</xsl:template>
	
	<xsl:template match="cbc:ID" mode="ID">
		<cbc:ID><xsl:value-of select="." /></cbc:ID>
	</xsl:template>
	
	
	<xsl:template match="cbc:ID" mode="OrderReferenceID">
		<cbc:ID>
		<xsl:choose>
			<xsl:when test="normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID) = '79069474349'">
				<xsl:choose>
					<xsl:when test="contains(.,'PO')">
						<xsl:value-of select="normalize-space(substring-after(normalize-space(.),'PO'))"/>
					</xsl:when>
					<xsl:otherwise><xsl:value-of select="normalize-space(.)"/></xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
		</xsl:choose>
		</cbc:ID>
	</xsl:template>
	
	<xsl:template match="cbc:DocumentType" >
		<cbc:DocumentType><xsl:value-of select="." /></cbc:DocumentType>
	
	</xsl:template>
	

	
    <xsl:template match="*" mode="copy">
        <xsl:element name="{name()}" namespace="{namespace-uri()}">
            <xsl:apply-templates select="@*|node()" mode="copy" />
        </xsl:element>
    </xsl:template>
    
	<xsl:template match="@*|text()|comment()" mode="copy">
		<xsl:copy/>
	</xsl:template>




	<xsl:template match="cbc:*" >
<!--		<xsl:copy>
			<xsl:value-of select="." />
		</xsl:copy>
-->	
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:value-of select="." />
		</xsl:element>
		

	</xsl:template>


</xsl:stylesheet>
