<?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 version="1.0" 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:xades="http://uri.etsi.org/01903/v1.3.2#" 
    xmlns:n2="http://uri.etsi.org/01903/v1.4.1#" 
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:ns0="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2" 
    xmlns:extagr="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-1"
    xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:AttachedDocument-2" 
    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:exthr="urn:invoice:hrvschema:xsd:CommonExtensionComponents-1" 
    xmlns:n0="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" 
    xmlns:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
    xmlns:qdt2="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2" 
    xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" 
    xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" 
    xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" 
    xmlns:udt2="urn:oasis:names:specification:ubl:schema:xsd:UnqualifiedDataTypes-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:ccts-cct="urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2" 
    xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" 
    exclude-result-prefixes="ccts-cct ds n0 n2 qdt2 sac sbc udt2 xades xs xsi xsl i ns1 a n" 
    xmlns="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2"
    >
    
    <xsl:namespace-alias stylesheet-prefix="n" result-prefix="#default"/>
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:variable name="noteSum">
        <xsl:variable name="sum" />
        <xsl:for-each select="/i: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="normalize-space(substring-after(., 'Vrijeme izdavanja:')) != ''" />
                <xsl:when test="normalize-space(substring-after(., 'Oznaka operatera:')) != ''" />
                <xsl:when test="normalize-space(substring-after(., 'Odgovorna osoba:')) != ''" />
                <xsl:when test="normalize-space(substring-after(., 'Operater:')) != ''" />	
                <xsl:otherwise><xsl:value-of select="concat($sum,., ';')"/></xsl:otherwise>
            </xsl:choose>
            
        </xsl:for-each>
        
        
    </xsl:variable>
    
    <xsl:template match="/">
        
        <OutgoingInvoicesData  xmlns="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2">            
            
            <xsl:call-template name="HRInvoiceHeader" />
            <OutgoingInvoice>
                <xsl:call-template name="HROutgoingInvoiceHeader" />
                <InvoiceEnvelope>
                    <xsl:apply-templates select="/i:Invoice" />
                </InvoiceEnvelope>
                <AttachedDocumentEnvelope>
                    <xsl:apply-templates select="/i:Invoice/cac:Signature"/>
                    <xsl:apply-templates select="/i:Invoice/cac:AdditionalDocumentReference[cac:Attachment]" mode="Attachment" />
                </AttachedDocumentEnvelope>
            </OutgoingInvoice>
        </OutgoingInvoicesData>
    </xsl:template>
    <xsl:template match="i:Invoice">
        
        <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-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:n0="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" 
            xmlns:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
            xmlns:qdt2="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2" 
            xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" 
            xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" 
            xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" 
            xmlns:udt2="urn:oasis:names:specification:ubl:schema:xsd:UnqualifiedDataTypes-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:ccts-cct="urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2" 
            xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" 
            >
            <xsl:apply-templates select="ext:UBLExtensions" />
            <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
            <cbc:CustomizationID>urn:invoice.hr:ubl-2.1-customizations:FinaInvoice</cbc:CustomizationID>
            
            <!--<xsl:apply-templates select="cbc:ProfileID" />-->
            <cbc:ProfileID>MojEracunInvoice</cbc:ProfileID>
            <xsl:apply-templates select="cbc:ID" />
            <cbc:CopyIndicator>false</cbc:CopyIndicator>
            <xsl:apply-templates select="cbc:IssueDate" />
            <!--<xsl:apply-templates select="cbc:Note" mode="IssueTime" />-->
            <xsl:apply-templates select="cbc:IssueTime" />
            
            <!--<xsl:apply-templates select="cac:PaymentMeans/cbc:PaymentDueDate" />-->
            <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="cbc:TaxPointDate" />
            <xsl:apply-templates select="cbc:DocumentCurrencyCode" />
            <xsl:apply-templates 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:apply-templates select="cac:DespatchDocumentReference" />			
            <xsl:apply-templates select="cac:ReceiptDocumentReference" />
            <xsl:apply-templates select="cac:OriginatorDocumentReference" />
            <xsl:apply-templates select="cac:ContractDocumentReference" />
            <!--<xsl:apply-templates select="cac:AdditionalDocumentReference" mode="ID" />
			
			<xsl:apply-templates select="//ns0:AttachedDocumentEnvelope/ns1:AttachedDocument" mode="Attachment" />-->
            
            <xsl:apply-templates select="cac:ProjectReference" />
            
            <xsl:apply-templates select="cac:AccountingSupplierParty" />
            <xsl:apply-templates select="cac:AccountingCustomerParty" />
            
            <xsl:apply-templates select="cac:PayeeParty" />
            <xsl:apply-templates select="cac:TaxRepresentativeParty" />
            
            <xsl:apply-templates select="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="cac:InvoiceLine">
        <cac:InvoiceLine>
            <xsl:apply-templates select="cbc:ID"/>
            <xsl:apply-templates select="cbc:Note" />
            <xsl:apply-templates select="cbc:InvoicedQuantity" />
            <xsl:apply-templates select="cbc:LineExtensionAmount" />
            <xsl:apply-templates select="cbc:AccountingCost" />
            <xsl:apply-templates select="cac:InvoicePeriod" mode="InvoiceLine" />
            <xsl:apply-templates select="cac:OrderLineReference" mode="InvoiceLine"/>
            <xsl:apply-templates select="cac:DocumentReference" mode="InvoiceLine"/>
            <xsl:apply-templates select="cac:DespatchLineReference" mode="InvoiceLine"/>
            <xsl:apply-templates select="cac:AllowanceCharge" mode="InvoiceLine" />
            <xsl:choose>
                <xsl:when test="cac:TaxTotal"><xsl:apply-templates select="cac:TaxTotal"/></xsl:when>
                <xsl:when test="cac:Item/cac:ClassifiedTaxCategory">
                    <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:when>
                <xsl:otherwise></xsl:otherwise>
            </xsl:choose>
            
            <xsl:apply-templates select="cac:Item" mode="InvoiceLine" />
            <xsl:apply-templates select="cac:Price" mode="InvoiceLine" />
            
            
        </cac:InvoiceLine>
        
    </xsl:template>	
    <xsl:template name="HRTaxTotal">
        <xsl:param name="currencyID" select="'HRK'" />
        <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') " />
                    </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')" />
            </cbc:TaxAmount>
            <cac:TaxSubtotal>
                <cbc:TaxableAmount>
                    <xsl:attribute name="currencyID"><xsl:value-of select="$currencyID" /></xsl:attribute>
                    <xsl:value-of select="$taxableAmount" />					 
                </cbc:TaxableAmount>
                <cbc:TaxAmount>
                    <xsl:attribute name="currencyID"><xsl:value-of select="$currencyID" /></xsl:attribute>
                    <xsl:value-of select="format-number($taxAmount,'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: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="/i: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="HRTaxScheme">
        <xsl:param name="percent" select="0"/>
        <xsl:choose>
            <xsl:when test="number($percent) = 0">
                <cac:TaxScheme>
                    <cbc:Name>FRE</cbc:Name>
                    <cbc:TaxTypeCode listAgencyName="RH" listName="taxtypecode" listVersionID="1.1" listURI="http://cv.eracun.hr/ubl/os-ubl-2.0/cl/gc/taxtypecode" listSchemeURI="urn:hrubl:codelist:taxtypecode-1.1">ZeroRated</cbc:TaxTypeCode>
                </cac:TaxScheme>		
            </xsl:when>
            <xsl:when test="string(number($percent)) = 'NaN'">
                <cac:TaxScheme>
                    <cbc:Name>FRE</cbc:Name>
                    <cbc:TaxTypeCode listAgencyName="RH" listName="taxtypecode" listVersionID="1.1" listURI="http://cv.eracun.hr/ubl/os-ubl-2.0/cl/gc/taxtypecode" listSchemeURI="urn:hrubl:codelist:taxtypecode-1.1">ZeroRated</cbc:TaxTypeCode>
                </cac:TaxScheme>		
            </xsl:when>
            <xsl:otherwise>
                <cac:TaxScheme>
                    <cbc:Name>VAT</cbc:Name>
                    <cbc:TaxTypeCode listAgencyName="RH" listName="taxtypecode" listVersionID="1.1" listURI="http://cv.eracun.hr/ubl/os-ubl-2.0/cl/gc/taxtypecode" listSchemeURI="urn:hrubl:codelist:taxtypecode-1.1">StandardRated</cbc:TaxTypeCode>
                </cac:TaxScheme>
                
            </xsl:otherwise>
        </xsl:choose>
        
    </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="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 match="cac:Price" mode="InvoiceLine" >
        <cac:Price>
            <xsl:apply-templates select="cbc:PriceAmount" />
            <xsl:apply-templates select="cbc:BaseQuantity" />
            <xsl:apply-templates select="cac:AllowanceCharge" mode="Price" />
        </cac:Price>
        
    </xsl:template>
    
    <xsl:template match="cbc:TaxExemptionReason">
        <cbc:TaxExemptionReason>
            <xsl:value-of select="." />
        </cbc:TaxExemptionReason>
        
    </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(.), '#.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="cac:AllowanceCharge" mode="Price" >
        <cac:AllowanceCharge>
            <cbc:ChargeIndicator>false</cbc:ChargeIndicator>
            <xsl:apply-templates select="cbc:Amount" />
            <xsl:apply-templates select="cbc:BaseAmount" />
            
        </cac:AllowanceCharge>
        
    </xsl:template>
    
    
    <xsl:template match="cbc:BaseQuantity" >
        <cbc:BaseQuantity>
            <xsl:attribute name="unitCode" namespace="" >
                <xsl:value-of select="@unitCode" />
            </xsl:attribute>
            
            <xsl:value-of select="number(.)" />
        </cbc:BaseQuantity>
        
    </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:apply-templates select="cac:AdditionalItemProperty[cbc:Name/text() = 'ManufacturersItemIdentification']" mode="ManufacturersItemIdentification" />
            <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:BuyersItemIdentification">
        <cac:BuyersItemIdentification>
            <xsl:apply-templates select="cbc:ID" />
        </cac:BuyersItemIdentification>
    </xsl:template>
    <xsl:template match="cbc:Description">
        <cbc:Description><xsl:value-of select="."/>
        </cbc:Description>
        
    </xsl:template>
    <xsl:template match="cbc:Name">
        <cbc:Name><xsl:value-of select="."/>
        </cbc:Name>
        
    </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="cbc:Value">
        <cbc:Value><xsl:value-of select="."/></cbc:Value>
    </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:AdditionalItemProperty" mode="AdditionalItemIdentification">
        <cac:AdditionalItemIdentification>
            <cbc:ID><xsl:value-of select="cbc:Value" /></cbc:ID>
            
        </cac:AdditionalItemIdentification>
    </xsl:template>
    
    <xsl:template match="cac:CommodityClassification" >
        <cac:CommodityClassification>
            <xsl:apply-templates select="cbc:ItemClassificationCode" />
        </cac:CommodityClassification>
        
    </xsl:template>
    
    <xsl:template match="cbc:ItemClassificationCode" >
        <cbc:ItemClassificationCode>
            <xsl:attribute name="listID">
                <xsl:value-of select="@listID" />
            </xsl:attribute>
            <xsl:attribute name="listVersionID">
                <xsl:value-of select="@listVersionID" />
            </xsl:attribute>
            <xsl:value-of select="." />
            
        </cbc:ItemClassificationCode>
        
    </xsl:template>
    
    <xsl:template match="cac:OriginCountry" >
        <cac:OriginCountry>
            <xsl:apply-templates select="cbc:IdentificationCode" />
        </cac:OriginCountry>
    </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:SellersItemIdentification">
        <cac:SellersItemIdentification>
            <xsl:apply-templates select="cbc:ID" />
        </cac:SellersItemIdentification>
    </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" />
            <xsl:apply-templates select="cac:TaxTotal"/>
        </cac:AllowanceCharge>
        
        
        
    </xsl:template>
    
    
    <xsl:template match="cbc:ChargeIndicator">
        <cbc:ChargeIndicator><xsl:value-of select="."/></cbc:ChargeIndicator>
    </xsl:template>
    <xsl:template match="cbc:AllowanceChargeReasonCode">
        <cbc:AllowanceChargeReasonCode><xsl:value-of select="."/></cbc:AllowanceChargeReasonCode>
    </xsl:template>
    <xsl:template match="cbc:AllowanceChargeReason">
        <cbc:AllowanceChargeReason><xsl:value-of select="."/></cbc:AllowanceChargeReason>
    </xsl:template>
    <xsl:template match="cbc:MultiplierFactorNumeric">
        <cbc:MultiplierFactorNumeric><xsl:value-of select="."/></cbc:MultiplierFactorNumeric>
    </xsl:template>
    <xsl:template match="cbc:Amount">
        <cbc:Amount><xsl:value-of select="."/></cbc:Amount>
    </xsl:template>
    <xsl:template match="cbc:BaseAmount">
        <cbc:BaseAmount><xsl:value-of select="."/></cbc:BaseAmount>
    </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:apply-templates select="cbc:LineExtensionAmount" />
        <xsl:apply-templates select="cbc:TaxExclusiveAmount" />
        <xsl:apply-templates select="cbc:TaxInclusiveAmount" />
        <!--<xsl:apply-templates select="cbc:AllowanceTotalAmount" />-->
        <xsl:call-template name="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 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(.), '#.00')" /></xsl:otherwise>
            </xsl:choose>
            
            <!--<xsl:value-of select="format-number(number(.), '#.00')" />-->
        </xsl:element>
        
    </xsl:template>
    
    
    <xsl:template name="AllowanceTotalAmount" >
        <xsl:choose>
            <xsl:when test="cbc:AllowanceTotalAmount or normalize-space(cbc:AllowanceTotalAmount) != ''"><xsl:apply-templates select="cbc:AllowanceTotalAmount" /></xsl:when>
            <xsl:otherwise><cbc:AllowanceTotalAmount currencyID="HRK">0.00</cbc:AllowanceTotalAmount></xsl:otherwise>
        </xsl:choose>
    </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:apply-templates select="cbc:MultiplierFactorNumeric" />
            <xsl:apply-templates select="cbc:Amount" />
            <xsl:apply-templates select="cbc:BaseAmount" />
            <xsl:apply-templates select="cbc:TaxTotal" />
        </cac:AllowanceCharge>
        
    </xsl:template>
    
    <xsl:template match="cac:TaxTotal" >
        <cac:TaxTotal>
            <xsl:apply-templates select="cbc:TaxAmount" />
            <xsl:apply-templates select="cac:TaxSubtotal" />
        </cac:TaxTotal>
    </xsl:template>
    
    <xsl:template match="cac:TaxSubtotal">
        <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:apply-templates select="cbc:ID" mode="TaxCategory"/>
            <xsl:apply-templates select="cbc:Name" mode="TaxCategory"/>
            <xsl:apply-templates select="cbc:Percent"/>
            <xsl:apply-templates select="cac:TaxScheme"/>
        </cac:TaxCategory>
        
    </xsl:template>
    <xsl:template match="cbc:ID" mode="TaxCategory">
        <cbc:ID>
        <xsl:choose>
            <xsl:when test=". and normalize-space(.) != ''"><xsl:value-of select="."/></xsl:when>
            <xsl:when test="../cbc:Percent > 0">S</xsl:when>
            <xsl:otherwise>E</xsl:otherwise>
        </xsl:choose>
        </cbc:ID> 
    </xsl:template>
    <xsl:template match="cac:TaxScheme">
        <cac:TaxScheme>
            <xsl:apply-templates select="cbc:ID" mode= "TaxScheme"/>
            <xsl:apply-templates select="cbc:Name" mode ="TaxScheme"/>
        </cac:TaxScheme>
        
    </xsl:template>
    <xsl:template match="cbc:ID" mode ="TaxScheme">
        <cbc:Name>
            <xsl:choose>
                <xsl:when test="normalize-space(.) = 'PDV'">VAT</xsl:when>
                <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
            </xsl:choose>
        </cbc:Name>
    </xsl:template>
  
    <xsl:template match="cbc:Name" mode ="TaxScheme">
        <cbc:TaxTypeCode>
        <xsl:choose>
            <xsl:when test="../../cbc:Percent > 0">StandardRated</xsl:when>
            <xsl:otherwise>ZeroRated</xsl:otherwise>
        </xsl:choose>
        </cbc:TaxTypeCode>
    </xsl:template>
  
    <xsl:template match="cbc:Percent">
        <xsl:copy-of select="."/>
        
    </xsl:template>
    <xsl:template match="cbc:Name" mode="TaxCategory">
        <cbc:Name>
            <xsl:choose>
                <xsl:when test="normalize-space(.)!= ''"><xsl:value-of select="."/></xsl:when>
                <xsl:when test="../cbc:ID = 'S'">PDV</xsl:when>
                <xsl:when test="../cbc:ID = 'E'">OSLOBOĐENO_POREZA</xsl:when>
                <xsl:when test="../cbc:ID = 'O'">NEOPOREZIVO</xsl:when>
                <xsl:when test="../cbc:ID = 'Z'">NEOPOREZIVO</xsl:when>
                <xsl:otherwise>PDV</xsl:otherwise>
            </xsl:choose>
            
        </cbc:Name>
        
    </xsl:template>
  
  
  
    <xsl:template match="cbc:Name">
        <xsl:copy-of select="."/>
        
    </xsl:template>
    
    <xsl:template match="cbc:CalculationSequenceNumeric">
        <xsl:copy-of select="."/>
        
    </xsl:template>
    <xsl:template match="cbc:TaxableAmount" >
        <xsl:copy-of select="."/>
        
    </xsl:template>
    <xsl:template match="cbc:TaxAmount" >
        <xsl:copy-of select="."/>
        
    </xsl:template>
    
    <xsl:template match="cac:PaymentTerms" >
        <xsl:copy-of select="."/>
        
    </xsl:template>
    <xsl:template match="cac:PaymentTerms" >
        <xsl:copy-of select="."/>
        
    </xsl:template>
    
    <xsl:template match="cac:PaymentMeans" >
        <cac:PaymentMeans>

        <xsl:apply-templates select="cbc:PaymentMeansCode"/>
        <xsl:choose>
            <xsl:when test="../cbc:DueDate and ../cbc:DueDate != ''">
                <xsl:apply-templates select="../cbc:DueDate"/>
            </xsl:when>
            <xsl:otherwise><xsl:apply-templates select="cbc:PaymentDueDate"/></xsl:otherwise>
        </xsl:choose>
            <xsl:choose>
                <xsl:when test="cbc:PaymentChannelCode and normalize-space(cbc:PaymentChannelCode) != ''"><xsl:apply-templates select="cbc:PaymentChannelCode"/></xsl:when>
                <xsl:otherwise>
                    <cbc:PaymentChannelCode>IBAN</cbc:PaymentChannelCode>
                </xsl:otherwise>
            </xsl:choose>
        <xsl:apply-templates select="cbc:PaymentID" mode="InstructionID"/>
        <xsl:apply-templates select="cbc:InstructionNote"/>
        <xsl:apply-templates select="cbc:InstructionID" mode="PaymentID"/>
        <xsl:apply-templates select="cac:PayeeFinancialAccount"/>
            
            
        </cac:PaymentMeans>
        
    </xsl:template>
    <xsl:template match="cbc:InstructionNote" >
        <cbc:InstructionNote><xsl:value-of select="."/></cbc:InstructionNote>
        
    </xsl:template>
    <xsl:template match="cbc:PaymentMeansCode" >
        <cbc:PaymentMeansCode><xsl:value-of select="."/></cbc:PaymentMeansCode>
        
    </xsl:template>
    <xsl:template match="cbc:DueDate" >
        <cbc:PaymentDueDate><xsl:value-of select="."/></cbc:PaymentDueDate>
        
    </xsl:template>
    <xsl:template match="cbc:PaymentDueDate" >
        <cbc:PaymentDueDate><xsl:value-of select="."/></cbc:PaymentDueDate>
        
    </xsl:template>
    <xsl:template match="cbc:PaymentChannelCode" >
        <cbc:PaymentChannelCode><xsl:value-of select="."/></cbc:PaymentChannelCode>
        
    </xsl:template>
    <xsl:template match="cbc:InstructionID" mode="PaymentID" >
        <cbc:PaymentID><xsl:value-of select="concat('HR',.)"/></cbc:PaymentID>
        
    </xsl:template>
    <xsl:template match="cbc:PaymentID" mode="InstructionID" >
        <cbc:InstructionID><xsl:value-of select="."/></cbc:InstructionID>
        
    </xsl:template>
    <xsl:template match="cac:PayeeFinancialAccount" >
        <cac:PayeeFinancialAccount>
        <xsl:apply-templates select="cbc:ID"/>
        <xsl:apply-templates select="cac:FinancialInstitutionBranch"/>
        </cac:PayeeFinancialAccount>
        
    </xsl:template>
    <xsl:template match="cac:FinancialInstitutionBranch" >
        <cac:FinancialInstitutionBranch>
        <xsl:apply-templates select ="cac:FinancialInstitution"/>
        </cac:FinancialInstitutionBranch>
    </xsl:template>
    
    <xsl:template match="cac:FinancialInstitution" >
        <cac:FinancialInstitution>
        <xsl:apply-templates select ="cbc:ID"/>
        <xsl:apply-templates select ="cbc:Name"/>
        </cac:FinancialInstitution>
    </xsl:template>
    
    
    <xsl:template match="cac:Delivery" >
        <xsl:copy-of select="."/>
        
    </xsl:template>
    
    
    
    <xsl:template match="cac:TaxRepresentativeParty" >
        <xsl:copy-of select="."/>
        
    </xsl:template>
    <xsl:template match="cac:AdditionalDocumentReference" mode="Attachment">
        <AttachedDocument xmlns="urn:oasis:names:specification:ubl:schema:xsd:AttachedDocument-2">
            <cbc:ID>
                <xsl:value-of select="cbc:ID" />
            </cbc:ID>
            <cbc:IssueDate>
                <xsl:value-of select="/i:Invoice/cbc:IssueDate"/>
            </cbc:IssueDate>
            <cbc:IssueTime>
                <xsl:value-of select="/i:Invoice/cbc:Time" />
            </cbc:IssueTime>
            <cbc:DocumentType>
                <xsl:value-of select="cbc:DocumentType"/>
            </cbc:DocumentType>
            <cbc:ParentDocumentID><xsl:value-of  select="/i:Invoice/cbc:ID" /></cbc:ParentDocumentID>
            <cac:SenderParty>
                <xsl:apply-templates select="/i:Invoice/cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity" />
            </cac:SenderParty>
            <cac:ReceiverParty>
                <xsl:apply-templates select="/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity" />
            </cac:ReceiverParty>
            
            <xsl:apply-templates select="cac:Attachment" mode="Attachment" />
            
        </AttachedDocument>
    </xsl:template>
    
    <xsl:template match="cac:Attachment" mode="Attachment" >
        <cac:Attachment>
            <xsl:apply-templates select="cbc:EmbeddedDocumentBinaryObject" />
        </cac:Attachment>
        
    </xsl:template>
    <xsl:template match="cac:Signature">
      
        <AttachedDocument xmlns="urn:oasis:names:specification:ubl:schema:xsd:AttachedDocument-2">
            <xsl:choose>
                <xsl:when test="cbc:ID and normalize-space(cbc:ID) != ''">
                    <cbc:ID>
                        <xsl:value-of select="cbc:ID" />
                    </cbc:ID>
                </xsl:when>
                <xsl:otherwise>
                    <cbc:ID><xsl:value-of select="//i:Invoice/cbc:ID"/></cbc:ID>
                </xsl:otherwise>
            </xsl:choose>

            <cbc:IssueDate>
                <xsl:value-of select="/i:Invoice/cbc:IssueDate"/>
            </cbc:IssueDate>
            <cbc:IssueTime>
                <xsl:value-of select="/i:Invoice/cbc:IssueTime" />
            </cbc:IssueTime>
            <cbc:ParentDocumentID><xsl:value-of  select="/i:Invoice/cbc:ID" /></cbc:ParentDocumentID>
            <cac:SenderParty>
                <cac:PartyName>
                    <cbc:Name><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyName/cbc:Name" /></cbc:Name>
                </cac:PartyName>
                <cac:PostalAddress>
                    <cac:AddressLine>
                        <cbc:Line><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:Line" /></cbc:Line>
                    </cac:AddressLine>
                    <cac:Country>
                        <cbc:Name><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:Name" /></cbc:Name>
                    </cac:Country>
                    
                </cac:PostalAddress>
                <cac:PartyLegalEntity>
                    <cbc:RegistrationName><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cac:RegistrationName" /></cbc:RegistrationName>
                    <cbc:CompanyID><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cac:CompanyID" /></cbc:CompanyID>
                </cac:PartyLegalEntity>
            </cac:SenderParty>
            <cac:ReceiverParty>
                <cac:PartyName>
                    <cbc:Name><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyName/cbc:Name" /></cbc:Name>
                </cac:PartyName>
                <cac:PostalAddress>
                    <cac:AddressLine>
                        <cbc:Line><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:Line" /></cbc:Line>
                    </cac:AddressLine>
                    <cac:Country>
                        <cbc:Name><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:Country/cbc:Name" /></cbc:Name>
                    </cac:Country>
                </cac:PostalAddress>
                <cac:PartyLegalEntity>
                    <cbc:RegistrationName><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cac:RegistrationName" /></cbc:RegistrationName>
                    <cbc:CompanyID><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cac:CompanyID" /></cbc:CompanyID>
                </cac:PartyLegalEntity>
            </cac:ReceiverParty>
            <cac:Attachment>
                <xsl:apply-templates select=".//cbc:EmbeddedDocumentBinaryObject"  />
                
            </cac:Attachment>
            
        </AttachedDocument>
        
    </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:PayeeParty" >
        <xsl:copy-of select="."/>
      
        </xsl:template  >
    
    <xsl:template match = "ext:UBLExtensions" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" >
        <ext:UBLExtensions>
            <xsl:call-template name="UBLExtension"/>
            
        </ext:UBLExtensions>
        
        
    </xsl:template>
    
    
    <xsl:template name="UBLExtension" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" >
        <ext:UBLExtension>
            <cbc:ID>HRINVOICE1</cbc:ID>
            <cbc:Name>InvoiceIssuePlaceData</cbc:Name>
            <ext:ExtensionAgencyID>MINGORP</ext:ExtensionAgencyID>
            <ext:ExtensionAgencyName>MINGORP</ext:ExtensionAgencyName>
            <ext:ExtensionAgencyURI>MINGORP</ext:ExtensionAgencyURI>
            <ext:ExtensionURI>urn:invoice:hr:issueplace</ext:ExtensionURI>
            <ext:ExtensionReasonCode>MandatoryField</ext:ExtensionReasonCode>
            <ext:ExtensionReason>Mjesto izdavanja računa prema Pravilniku o PDV-u</ext:ExtensionReason>
            <ext:ExtensionContent>
                <ext:InvoiceIssuePlace>
                    <xsl:choose>
                        <xsl:when test="//ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent[not(extagr:AdditionalContent)]/exthr:InvoiceIssuePlace and normalize-space(//ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent[not(extagr:AdditionalContent)]/exthr:InvoiceIssuePlace) != ''">
                            <xsl:value-of select="//ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent[not(extagr:AdditionalContent)]/exthr:InvoiceIssuePlace"/>                     
                        </xsl:when>
                        <xsl:otherwise><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName"/></xsl:otherwise>
                    </xsl:choose>
                </ext:InvoiceIssuePlace>       
            </ext:ExtensionContent>
        </ext:UBLExtension>
        <ext:UBLExtension>
            <cbc:ID>HRINVOICE1</cbc:ID>
            <cbc:Name>InvoiceIssuerData</cbc:Name>
            <ext:ExtensionAgencyID>FINA</ext:ExtensionAgencyID>
            <ext:ExtensionAgencyName>FINA</ext:ExtensionAgencyName>
            <ext:ExtensionAgencyURI>FINA</ext:ExtensionAgencyURI>
            <ext:ExtensionURI>urn:invoice:hr:issuer</ext:ExtensionURI>
            <ext:ExtensionReasonCode>MandatoryField</ext:ExtensionReasonCode>
            <ext:ExtensionReason>Podaci o izdavatelju prema Zakonu o trgovačkim društvima</ext:ExtensionReason>
            <ext:ExtensionContent>
                <ext:InvoiceIssuer>
                    <xsl:choose>
                        <xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyLegalForm"><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyLegalForm"/></xsl:when>
                        <xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName"><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName"/></xsl:when>
                        <xsl:otherwise><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyName/cbc:Name"/></xsl:otherwise>
                    </xsl:choose>
                </ext:InvoiceIssuer>
            </ext:ExtensionContent>
        </ext:UBLExtension>
    </xsl:template>
    
    
    
    
    <xsl:template match = "cbc:ID">
        <cbc:ID>
            <xsl:value-of select="."/>
            
        </cbc:ID>
        
        
    </xsl:template>
    <xsl:template match = "cac:AccountingCustomerParty">
        <cac:AccountingCustomerParty>
            <xsl:apply-templates select="cac:Party" mode="AccountingCustomerParty" />
            <xsl:call-template name="AccountingContact" />
        </cac:AccountingCustomerParty>
    </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"/>
            <!--<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="PartyIdentification" />-->
            <!--<xsl:apply-templates select="cac:PartyName" />-->
            <xsl:apply-templates select="cac:PartyName" />
            <xsl:apply-templates select="cac:PostalAddress" />
            <!--<xsl:call-template name="PartyTaxScheme" />-->
            <!--<xsl:apply-templates select="cac:PartyLegalEntity" mode="AccountingSupplierParty"/>-->
            <xsl:apply-templates select="cac:PartyLegalEntity" />
            <xsl:apply-templates select="cac:PartyIdentification" />
            <!--<xsl:call-template name="PartyContact" />-->
            
        </cac:Party>
        
    </xsl:template>

    <xsl:template match = "cac:AccountingSupplierParty">
        <cac:AccountingSupplierParty>
            <xsl:apply-templates select="cac:Party" mode="AccountingSupplierParty" />
            <xsl:call-template name="AccountingSupplierPartyContact"/>
        </cac:AccountingSupplierParty>
        
    </xsl:template>
    <xsl:template name="AccountingSupplierPartyContact" >
        <cac:AccountingContact>
            <xsl:apply-templates select="/i:Invoice/cbc:Note" mode="OdgovornaOsoba" />
            
            <xsl:apply-templates select="/i:Invoice/cbc:Note" mode="OznakaOperatera" />
        </cac:AccountingContact>
    </xsl:template>
    
    <xsl:template match="cbc:Note" mode="OznakaOperatera">
        <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:if test="$oznaka != ''">
            <cbc:Note><xsl:value-of select="$oznaka"/></cbc:Note>
        </xsl:if>
        <xsl:if test="$oznaka2 != ''">
            <cbc:Note><xsl:value-of select="$oznaka2"/></cbc:Note>
        </xsl:if>
        
    </xsl:template>
    
    <xsl:template match="cbc:Note" mode="OdgovornaOsoba">
        <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:if test="$oznaka != ''">
            <cbc:Name><xsl:value-of select="$oznaka"/></cbc:Name>
        </xsl:if>
        <xsl:if test="$oznaka2 != ''">
            <cbc:Name><xsl:value-of select="$oznaka2"/></cbc:Name>
        </xsl:if>
    </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"/>
            <!--<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="PartyIdentification" />-->
            <!--<xsl:apply-templates select="cac:PartyName" />-->
            <xsl:apply-templates select="cac:PartyName" />
            <xsl:apply-templates select="cac:PostalAddress" />
            <!--<xsl:call-template name="PartyTaxScheme" />-->
            <!--<xsl:apply-templates select="cac:PartyLegalEntity" mode="AccountingSupplierParty"/>-->
            <xsl:apply-templates select="cac:PartyLegalEntity" />
            <xsl:apply-templates select="cac:PartyIdentification" />
            <!--<xsl:call-template name="PartyContact" />-->
            
        </cac:Party>
        
    </xsl:template>
    <xsl:template match="cbc:EndpointID" >
       <xsl:copy-of select="."/>
    </xsl:template>
 
    <xsl:template match="cac:PartyName" >
        <xsl:copy-of select="."/>
    </xsl:template>
    
    <xsl:template match="cbc:RegistrationName" >
        <cbc:RegistrationName><xsl:value-of select="."/></cbc:RegistrationName>
    </xsl:template>
    <xsl:template match="cbc:CompanyID" >
        <cbc:CompanyID><xsl:value-of select="."/></cbc:CompanyID>
    </xsl:template>
    
    <xsl:template match="cac:PartyLegalEntity" >
       
        <cac:PartyLegalEntity>
             <xsl:apply-templates select="cbc:RegistrationName" />
            <xsl:apply-templates select="cbc:CompanyID" />
        </cac:PartyLegalEntity>
    </xsl:template>
    
    
    <xsl:template match="cac:PartyIdentification" >
       <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="cac:AccountingContact">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template name="AccountingContact" >
        <cac:AccountingContact>
            <xsl:apply-templates select="./cac:AccountingContact/cbc:Name"/>
            <xsl:call-template name="CustomerElectronicMail" />
            <xsl:apply-templates select="./cac:AccountingContact/cbc:Note"/>
        </cac:AccountingContact>
    </xsl:template>
    
    <xsl:template match="cbc:Name" mode="AccountingSupplierParty">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template name = "CustomerElectronicMail">
        <cbc:ElectronicMail>
            <xsl:choose>
                <xsl:when test="//cac:AccountingCustomerParty/cac:AccountingContact/cbc:ElectronicMail and  normalize-space(//cac:AccountingCustomerParty/cac:AccountingContact/cbc:ElectronicMail) != ''"><xsl:value-of select="//cac:AccountingCustomerParty/cac:AccountingContact/cbc:ElectronicMail" /></xsl:when>
                <xsl:otherwise>adresar@moj-eracun.hr</xsl:otherwise>
            </xsl:choose>
            
        </cbc:ElectronicMail>
    </xsl:template>
    
    <xsl:template match="cbc:ElectronicMail">
        <cbc:ElectronicMail>
        <xsl:value-of select="."/>
        </cbc:ElectronicMail>
    </xsl:template>
    
    
    <xsl:template match = "cac:ProjectReference">
        <cac:ProjectReference>
            <xsl:apply-templates select="cbc:ID" />
        </cac:ProjectReference>
        
    </xsl:template>
    <xsl:template match = "cac:ContractDocumentReference">
        <cac:ContractDocumentReference>
            <xsl:apply-templates select="cbc:ID" />
        </cac:ContractDocumentReference>
        
    </xsl:template>
    <xsl:template match = "cac:OriginatorDocumentReference">
        <cac:OriginatorDocumentReference>
            <xsl:apply-templates select="cbc:ID" />
        </cac:OriginatorDocumentReference>
        
    </xsl:template>
    <xsl:template match = "cac:ReceiptDocumentReference">
        <cac:ReceiptDocumentReference>
            <xsl:apply-templates select="cbc:ID" />
        </cac:ReceiptDocumentReference>
        
    </xsl:template>
    <xsl:template match = "cac:BillingReference">
        <cac:BillingReference>
            <xsl:apply-templates select="cac:InvoiceDocumentReference" />
        </cac:BillingReference>
    </xsl:template>
    <xsl:template match="cac:DespatchDocumentReference" >
        <cac:DespatchDocumentReference>
            <xsl:apply-templates select="cbc:ID" />
        </cac:DespatchDocumentReference>
        
    </xsl:template>
    <xsl:template match = "cac:InvoiceDocumentReference">
        <cac:InvoiceDocumentReference>
            <xsl:apply-templates select="cbc:ID" />
            <xsl:apply-templates select="cbc:IssueDate" />
        </cac:InvoiceDocumentReference>
        
    </xsl:template>
    <xsl:template match = "cac:OrderReference">
        <cac:OrderReference>
            <xsl:apply-templates select="cbc:ID" />
        </cac:OrderReference>
    </xsl:template>
    <xsl:template match="cac:InvoicePeriod" >
        <cac:InvoicePeriod>
            <xsl:apply-templates select="cbc:StartDate" />
            <xsl:apply-templates select="cbc:EndDate" />
            
        </cac:InvoicePeriod>
        
    </xsl:template>
    <xsl:template match = "cbc:StartDate">
        <cbc:StartDate>
            <xsl:value-of select="."/>
        </cbc:StartDate>
    </xsl:template>
    <xsl:template match = "cbc:EndDate">
        <cbc:EndDate>
            <xsl:value-of select="."/>
        </cbc:EndDate>
    </xsl:template>
    <xsl:template match = "cbc:BuyerReference">
        <cbc:BuyerReference>
            <xsl:value-of select="."/>
        </cbc:BuyerReference>
    </xsl:template>
    <xsl:template match = "cbc:AccountingCost">
        <cbc:AccountingCost>
            <xsl:value-of select="."/>
        </cbc:AccountingCost>
    </xsl:template>
    <xsl:template match = "cbc:DocumentCurrencyCode">
        <cbc:DocumentCurrencyCode>
            <xsl:value-of select="."/>
        </cbc:DocumentCurrencyCode>
    </xsl:template>
    <xsl:template match = "cbc:TaxCurrencyCode">
        <cbc:TaxCurrencyCode>
            <xsl:value-of select="."/>
        </cbc:TaxCurrencyCode>
    </xsl:template>
    <xsl:template match = "cbc:TaxPointDate">
        <cbc:TaxPointDate>
            <xsl:value-of select="."/>
        </cbc:TaxPointDate>
    </xsl:template>
    <xsl:template match = "cbc:Note">
        <cbc:Note>
            <xsl:value-of select="."/>
        </cbc:Note>
    </xsl:template>
    <xsl:template match = "cbc:IssueDate">
        <cbc:IssueDate>
            <xsl:value-of select="."/>
        </cbc:IssueDate>
    </xsl:template>
    <xsl:template match = "cbc:InvoiceTypeCode">
        <cbc:InvoiceTypeCode>
            <xsl:value-of select="."/>
        </cbc:InvoiceTypeCode>
    </xsl:template>
    <xsl:template match = "cbc:IssueTime">
        <cbc:IssueTime>
            <xsl:value-of select="."/>
            
        </cbc:IssueTime>
        
        
    </xsl:template>
    <xsl:template match="cac:PostalAddress" >
        <cac:PostalAddress>
            <xsl:apply-templates select="cbc:StreetName"/>
            <xsl:apply-templates select="cbc:BuldingNumber"/>
            <xsl:apply-templates select="cbc:CityName"/>
            <xsl:apply-templates select="cbc:PostalZone"/>
            <xsl:apply-templates select="cbc:CountrySubentity"/>
            <xsl:choose>
                <xsl:when test="cac:AddressLine/cbc:Line and normalize-space(cac:AddressLine/cbc:Line) != ''">
                    <xsl:apply-templates select="cac:AddressLine"></xsl:apply-templates>
                </xsl:when>
                <xsl:otherwise>
                    <cac:AddressLine>
                        <cbc:Line><xsl:value-of select="concat(cbc:StreetName, cbc:BuildingNumber, cbc:PostalZone, cbc:CityName) "/></cbc:Line>
                        
                    </cac:AddressLine>
                    
                </xsl:otherwise>
            </xsl:choose>
        </cac:PostalAddress>
    </xsl:template>
    <xsl:template match = "cbc:StreetName">
        <cbc:StreetName>
            <xsl:value-of select="."/>
            
        </cbc:StreetName>
        
        
    </xsl:template>
    
    <xsl:template match = "cbc:BuildingNumber">
        <cbc:BuildingNumber>
            <xsl:value-of select="."/>
            
        </cbc:BuildingNumber>
        
        
    </xsl:template>
    
    <xsl:template match = "cbc:CityName">
        <cbc:CityName>
            <xsl:value-of select="."/>
            
        </cbc:CityName>
        
        
    </xsl:template>
    
    <xsl:template match = "cbc:PostalZone">
        <cbc:PostalZone>
            <xsl:value-of select="."/>
            
        </cbc:PostalZone>
    </xsl:template>
    
    <xsl:template match = "cbc:CountrySubentity">
        <cbc:CountrySubentity>
            <xsl:value-of select="."/>
            
        </cbc:CountrySubentity>
    </xsl:template>
    <xsl:template match = "cac:AddressLine">
        <cac:AddressLine>
            <xsl:apply-templates select="cbc:Line"/>
            
        </cac:AddressLine>
    </xsl:template>
    <xsl:template match = "cbc:Line">
        <cbc:Line>
            <xsl:value-of select="."/>
            
            </cbc:Line>
    </xsl:template>
    
    
    
    
    
    
    <xsl:template name="HRInvoiceHeader">
        
        <Header>
            <SupplierID>
                <!--<xsl:value-of select="/i:Invoice/cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID"/>-->
                <xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" />
            </SupplierID>
            
            <InvoiceType>
                <xsl:value-of select="1"/>
            </InvoiceType>
        </Header>
        
    </xsl:template>	
    <xsl:template name="HROutgoingInvoiceHeader" >
        
        <SupplierInvoiceID>
            <xsl:choose>
                <xsl:when test="/i:Invoice/cbc:ID and normalize-space(/i:Invoice/cbc:UUID) != ''"><xsl:value-of select="string(/i:Invoice/cbc:UUID)"/></xsl:when>
                <xsl:otherwise><xsl:value-of select="string(/i:Invoice/cbc:ID)"/></xsl:otherwise>
            </xsl:choose>
        </SupplierInvoiceID>
        <BuyerID>
            <!--<xsl:value-of select="string(/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)"/>-->
            <xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" />
        </BuyerID>
        
    </xsl:template>
    
    
    
</xsl:stylesheet>