<?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., 
 -->
 
 <!-- V2025-01-28
Podrška za velike brojeve u TaxableAmount
Hendlanje Model/PNB kad nisu upisani konzistentni podaci

-->
 <!--
Ispravljen tipfeler u DeliveryParty/EndpointID
-->
 <!-- V2024-10-09
Izmjena formatiranja broja za InvoicedQuantity - za velike brojeve

-->
<!-- V 2024-10-04

Ispravljen EnpointID tipfeler

--> 
 
 
 <!-- V 2024-10-03
-	AccountingCustomerParty/Party / Endpoint – ovo se ne preseli u HRInvoice
-	Delivery/DeliveryParty/EndpointID – ovo se ne preseli u HRInvoice
- 	Stavlja AccountingSupplierParty//PartyIdentification u EndpointID ako ima schemeID=0088; pa onda ako postoji EndpointID sa scheme=0088
-->

 <!-- V 2024-09-24
Izbačeno generiranje komentara s verzijom transformacije

-->
 
 <!-- V 2024-07-24-V21
*** Promijenjen format-number pattern da uvijek ispisuje bar jednu cjelobrojnu i dvije decimalne znamenke - i za pozitivne i za negativne brojeve

-->
 <!-- V 2024-04-24-V20
 *** Modificiano popunjavanje varijable customerCompanyId i supplierCompanyId, izmijenjeno je transformiranje AccountingCustomerParty/EndpointID (prepiše se sadržaj nodea)
 -->
 <!-- V 2024-03-11-V19
*** Izbačene n: i n0: namespace deklaracije

-->

<!-- V 2024-02-26-V18
*** Izbačen HROutgoingInvoiceHeader template jer je kreirao n: namespace deklaraciju umjesto da koristi default namespace

--> 
 
 <!-- V 2024-02-07-v17
*** Dodan ns0 u exclude-result-prefixes da spriječi generiranje namespace prefixa na OutgoingInvocesData header elementima

-->
<!-- V 2020-04-20-v16
*** Dodana logika za kreiranje unitCode-a u BaseQuantity

*** Maknuto kreiranje PayableRoundingAmount ( ne postoji po HRInvoice)

-->
 <!-- V 2019-10-21-v15
*** Maknute namespace deklaracije za i: a: ns1:
-->
<!-- V 2019-09-24-v14
*** PartyIdentification se kreira samo ako Party ima podjedinice - inače ne
-->
 <!-- v2019-09-23-V13
*** Dodana logika da ispravno hendla dupli PartyName i PartyLegalEntity

*** Promijenjen redosljed traženja OIBa: sada prvo gleda EndpointID ako sadrži oib (shema 9934 - jer tu možemo biti sigurni da je OIB, a u PartyIdentification ne možemo)

-->
 <!-- V2019-09-13-v11
*** Dodana kontrola da CompanyID mora biti 11 znakova da se uzme kao OIB

-->
 <!-- V2019-09-09-v10
*** ProfileID je UVIJEK MojEracunInvoice

-->
 <!-- V2019-09-06-v9
*** Dodaje u komentar identifikator transformacije

*** logika za pronalaženje CompanyID Suppiera i Customera iz V7 centralizirana u varijablama
- $supplierCompanyId
- $customerCompanyId
.. i primijenjena u headerima, attachmentima i u dokumentu
-->

 <!-- V2019-08-22-v8
*** Dodano da izvlači PartyName ako ga nema iz LegalMonetaryTotal/cbc:CompanyName 

*** Vrijeme izdavanja: parsira i samo Note koji ima "Vrijeme izdavanja:"

*** AllowanceTotalAmount - uvijek stavlja element s vrijednosti 0.00 ako je prazan

-->
<!-- V2019-08-14-v7
*** Dodano da uvijek popunjava PartyLegalEntity/cbc:CompanyID
- Prvo sa CompanyID
- Pa sa EndpointID ako je shema 9934
- Pa sa PartyIdentification/cbc:ID ako počinje sa 9934:
- Pa sa PartyTaxScheme/cbc:CompanyID ako počinje sa HR

--> 
 
 <!-- V 2019-08-12-v6
*** Ispravljen bug da zaokružuje cbc:PriceAmount na dvije decimela
- Ako ima manje od dvije decimale, imati će dvije decimale
- Ako ima više, imati će koliko ima

-->
 <!-- V 2019-07-26-v5
***cbc:TaxCategory/cbc:Percent se uvijek kreira
- ako je kategorija O stavlja se default 0.00

***Promijenjeno mapiranje AdditionalItemProperty:
- ako je cbc:Name AdditionalItemIdentification - ide u cac:AdditionalItemIdentification
- ako je cbc:Name ManufacturersItemIentification - ide u cac:ManufacturersItemIentification
- Ne generira se AdditionalItemProperty u gornja dva slučaja
-->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 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: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: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" 
 xmlns:hrextac="urn:mfin.gov.hr:schema:xsd:HRExtensionAggregateComponents-1" 
 exclude-result-prefixes="ccts-cct ds n0 n2 qdt2 sac sbc udt2 xades xs xsi xsl i ns1 a" 

 >

<!-- Izbačen default namespace - da ga ne dodaje bez veze
 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="issueTime">
		<xsl:choose>
			<xsl:when test="/i:Invoice/cbc:IssueTime">
					<xsl:value-of select="/i:Invoice/cbc:IssueTime" />
			</xsl:when>
		</xsl:choose>
	</xsl:variable>
	
	<xsl:variable name="supplierCompanyId">
		<xsl:choose>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID[@schemeID='9934'] and (string-length(normalize-space(//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID)) = 11)">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and (string-length(normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)) = 11)">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" mode="SchemeID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID[starts-with(text(), '9934:')]">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID" mode="PartyID2CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID[starts-with(text(), 'HR')]">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID" mode="TaxID2CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
			</xsl:when>

		</xsl:choose>

	</xsl:variable>
	
	<xsl:variable name="customerCompanyId">
		<xsl:choose>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID[@schemeID='9934'] and (string-length(normalize-space(//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID)) = 11)">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and (string-length(normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)) = 11)">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" mode="SchemeID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID[starts-with(text(), '9934:')]">
				<xsl:apply-templates select="//cac: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/cac:PartyLegalEntity/cbc:CompanyID[starts-with(text(), 'HU')]">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" mode="PartyLegalEntity2CompanyID" />
			</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="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-after(., 'Operator:')) != ''" />
					<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:when test="normalize-space(substring-after(., 'Issue Time:')) != ''" />	
					<xsl:when test="normalize-space(substring-after(., 'Responsible Person:')) != ''" />
					<xsl:when test="normalize-space(substring-after(., 'Blagajnik:')) != ''" />
					<xsl:otherwise><xsl:value-of select="concat($sum,., ';')"/></xsl:otherwise>
			   </xsl:choose>
				
			</xsl:for-each>
			
			
		</xsl:variable>

	
	<xsl:template match="/">
<!--		<xsl:comment>
			Generated by Moj eRačun EN-2-HRInvoice transformation V2024-07-24-V21
		</xsl:comment>
-->	

		<OutgoingInvoicesData xmlns="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2">
<!--			<xsl:attribute name="xsi:schemaLocation">http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2 D:/Downloads/examples-schemes-namespaces/xsd/OutgoingInvoicesData.xsd urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 file:///C:/inetpub/schemas/maindoc/UBL-Invoice-2.1.xsd</xsl:attribute>-->
			<Header>
				<!--Oib dobavljača-->
				<SupplierID><xsl:value-of select="$supplierCompanyId" /></SupplierID>
				<!--oznaka je li dokument business to business, b2b -1 ili business to customer b2c-0-->
				<InvoiceType>1</InvoiceType>
			</Header>
			<OutgoingInvoice>
				<!--<xsl:call-template name="HROutgoingInvoiceHeader" />-->
				<SupplierInvoiceID>
					<xsl:value-of select="string(/i:Invoice/cbc:ID)"/>
				</SupplierInvoiceID>
				<BuyerID>
					<!--<xsl:value-of select="string(/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)"/>-->
					<xsl:value-of select="$customerCompanyId" />
				</BuyerID>

				<InvoiceEnvelope>
					<xsl:apply-templates select="/i:Invoice" />
				</InvoiceEnvelope>
				<AttachedDocumentEnvelope>
					<xsl:apply-templates select="/i:Invoice/cac:AdditionalDocumentReference[cac:Attachment]" mode="Attachment" />
				</AttachedDocumentEnvelope>
			</OutgoingInvoice>
		</OutgoingInvoicesData>
	</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="$issueTime" />
			</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" mode="SupplierAttachment" />
			</cac:SenderParty>
			<cac:ReceiverParty>
				<xsl:apply-templates select="/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity" mode="CustomerAttachment" />
			</cac:ReceiverParty>
			
			<xsl:apply-templates select="cac:Attachment" mode="Attachment" />

		</AttachedDocument>
	</xsl:template>
	
	<xsl:template match="cac:PartyLegalEntity" mode="SupplierAttachment" >
		<cac:PartyLegalEntity>
			<cbc:RegistrationName>
				<xsl:value-of select="cbc:RegistrationName"/>
			</cbc:RegistrationName>
			<cbc:CompanyID>
				<!--<xsl:value-of select="cbc:CompanyID" />-->
				<xsl:value-of select="$supplierCompanyId" />
			</cbc:CompanyID>
					
		</cac:PartyLegalEntity>
	
	</xsl:template>
	
	<xsl:template match="cac:PartyLegalEntity" mode="CustomerAttachment" >
		<cac:PartyLegalEntity>
			<cbc:RegistrationName>
				<xsl:value-of select="cbc:RegistrationName"/>
			</cbc:RegistrationName>
			<cbc:CompanyID>
				<!--<xsl:value-of select="cbc:CompanyID" />-->
				<xsl:value-of select="$customerCompanyId" />
			</cbc:CompanyID>
					
		</cac:PartyLegalEntity>
	
	</xsl:template>


	<xsl:template match="cac:Attachment" mode="Attachment" >
		<cac:Attachment>
		<xsl:apply-templates select="cbc:EmbeddedDocumentBinaryObject" />
		<xsl:apply-templates select="cac:ExternalReference" mode="Attachment" />
		
		</cac:Attachment>
	
	</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:ExternalReference" mode="Attachment" >
		<cac:ExternalReference>
			<xsl:apply-templates select="cbc:URI" />
		
		</cac:ExternalReference>
	
	</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" 
		>
			
			<ext:UBLExtensions>
				<xsl:apply-templates select="/i:Invoice/cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName" mode="UBLExtension" />
				<xsl:call-template name="UBLExtensionInvoiceIssuer"/>
			</ext:UBLExtensions>
			<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
			<cbc:CustomizationID>
				<xsl:value-of select="'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" mode="ID" />
			<!--<cbc:CopyIndicator>false</cbc:CopyIndicator>-->
			<xsl:apply-templates select="cbc:CopyIndicator" />
			<xsl:apply-templates select="cbc:IssueDate" />
			<!--<xsl:apply-templates select="cbc:Note" mode="IssueTime" />-->
			<xsl:if test="normalize-space($issueTime) != ''">
				<cbc:IssueTime><xsl:value-of select="$issueTime" /></cbc:IssueTime>
			</xsl:if>
			<!--<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:choose>
				<xsl:when test="./cac:PaymentMeans"><xsl:apply-templates select="cac:PaymentMeans"/></xsl:when>
				<xsl:when test="./cbc:DueDate and normalize-space(./cbc:DueDate) != ''"><xsl:call-template name="PaymentMeansFix"/>
				</xsl:when>
				<xsl:otherwise><xsl:apply-templates select="cac:PaymentMeans"/></xsl:otherwise>
			</xsl:choose>
			<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" mode="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:apply-templates select="cac:Item/cac:TaxCategory" mode="AllowanceChargeLine" />
			-->
			<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:apply-templates select="cac:Price" mode="InvoiceLine" />
			

		</cac:InvoiceLine>
		
	</xsl:template>	
	
	<xsl:template match="cac:Price" mode="InvoiceLine" >
		<cac:Price>
			<xsl:apply-templates select="cbc:PriceAmount" />
			<xsl:apply-templates select="cbc:BaseQuantity" />
		</cac:Price>
	
	</xsl:template>
	
	<xsl:template match="cbc:BaseQuantity" >
		<cbc:BaseQuantity>
			<xsl:attribute name="unitCode" namespace="" >
			<xsl:choose>
				<xsl:when test="normalize-space(./@unitCode) != ''"  >
						<xsl:value-of select="@unitCode" />
			    </xsl:when>
			    
				<xsl:otherwise><xsl:value-of select="../../cbc:InvoicedQuantity/@unitCode" /></xsl:otherwise>
			</xsl:choose>
            </xsl:attribute>
			
			<!--<xsl:value-of select="number(.)" />-->
			<xsl:value-of select="format-number(number(.), '##########0.######')"/>
		</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:ManufacturersItemIdentification" />
			<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:AdditionalItemProperty">
		<cac:AdditionalItemProperty>
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cbc:Value" />
		
		</cac:AdditionalItemProperty>
	
	</xsl:template>
	
	
	<xsl:template match="cac:ManufacturersItemIdentification">
		<cac:ManufacturersItemIdentification>
			<cbc:ID><xsl:value-of select="cbc:ID" /></cbc:ID>
		</cac:ManufacturersItemIdentification>		
	</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:TaxCategory" mode="InvoiceLine">
		<cac:ClassifiedTaxCategory>
			<xsl:choose>
				<xsl:when test="$isOutsideVAT = 'true'">
					<cbc:ID>O</cbc:ID>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="cbc:ID" mode="TaxCategoryID" />
					<xsl:apply-templates select="cbc:Percent" />
					
				</xsl:otherwise>
			</xsl:choose>
			
			<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
		
		</cac:ClassifiedTaxCategory>
	
	</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" 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: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:variable name="taxCategoryNode">
				<xsl:apply-templates select="../cac:Item/cac:ClassifiedTaxCategory" mode="AllowanceChargeLine" />
			</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: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>
		</cac:AllowanceCharge>
			

	
	</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: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="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 -->
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')" >
			<cac:DespatchLineReference>
				<xsl:apply-templates select="cbc:LineID" />
				<xsl:apply-templates select="cac:DocumentReference" mode="InvoiceLine"/>
			</cac:DespatchLineReference>
		</xsl:if>
	</xsl:template>
	
	
	<xsl:template match="cac:OrderLineReference" mode="InvoiceLine">
	
	<!--TODO: vidjeti da li staviti neko drugo mapiranje -->
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')" >
			<cac:OrderLineReference>
				<xsl:apply-templates select="cbc:LineID" />
				<xsl:apply-templates select="cac:OrderReference" mode="InvoiceLine"/>
			</cac:OrderLineReference>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:OrderReference" mode="InvoiceLine">
		<cac:OrderReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
			
		</cac:OrderReference>
	
	</xsl:template>


	<xsl:template match="cac:ReceiptLineReference" mode="InvoiceLine">
	
	<!--TODO: vidjeti da li staviti neko drugo mapiranje -->
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')" >
			<cac:ReceiptLineReference>
				<xsl:apply-templates select="cbc:LineID" />
				<xsl:apply-templates select="cac:DocumentReference" mode="InvoiceLine"/>
			</cac:ReceiptLineReference>
		</xsl:if>
	</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(.)" />-->
			<xsl:value-of select="format-number(number(.), '##########0.######')"/>
		</cbc:InvoicedQuantity>
	</xsl:template>
	
	<xsl:template match="cac:TaxTotal">
	
		<cac:TaxTotal>
			<xsl:apply-templates select="cbc:TaxAmount" />
			<xsl:apply-templates select="cac:TaxSubtotal[cac:TaxCategory/normalize-space(cbc:ID) != 'E' and cac:TaxCategory/normalize-space(cbc:ID) != 'O']" mode="TaxTotal" />
			<xsl:apply-templates select="//hrextac:HRTaxSubtotal[hrextac:HRTaxCategory/normalize-space(cbc:ID) = 'E' or hrextac:HRTaxCategory/normalize-space(cbc:ID) = 'O']" mode="TaxTotal" />
		
		</cac:TaxTotal>
	
	
	</xsl:template>
	
	<xsl:template match="hrextac:HRTaxSubtotal" mode="TaxTotal" >
		<cac:TaxSubtotal>
			<xsl:apply-templates select="cbc:TaxableAmount" />
			<xsl:apply-templates select="cbc:TaxAmount" />
			<xsl:apply-templates select="hrextac:HRTaxCategory" mode="TaxTotal" />
		
		</cac:TaxSubtotal>
	
	</xsl:template>
	
	<xsl:template match="hrextac:HRTaxCategory" mode="TaxTotal" >
		<cac:TaxCategory>
			<!--<xsl:apply-templates select="cbc:ID" mode="TaxCategoryID" />-->
			<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="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:apply-templates select="cbc:ID" mode="TaxCategoryID" />-->
			<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="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:choose>
				<xsl:when test="cac:TaxCategory">
					<xsl:apply-templates select="cac:TaxCategory" mode="AllowanceCharge" />
				</xsl:when>
				<xsl:when test="cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory">
					<xsl:apply-templates select="cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory" mode="AllowanceCharge" />
				</xsl:when>
			</xsl:choose>
-->			
			<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="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: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:PayableAmount" />
		
		</cac:LegalMonetaryTotal>
	
	</xsl:template>
	
	<xsl:template name="AllowanceTotalAmount" >
		<xsl:choose>
			<xsl:when test="cbc:AllowanceTotalAmount"><xsl:apply-templates select="cbc:AllowanceTotalAmount" /></xsl:when>
			<!--<xsl:otherwise><cbc:AllowanceTotalAmount currencyID="EUR">0.00</cbc:AllowanceTotalAmount></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 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 match="cac:TaxCategory" mode="AllowanceCharge">
		<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:apply-templates select="/i:Invoice/cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory[cbc:ID/text() = $taxCategoryID]/cbc:TaxExemptionReason" />-->
			<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 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 match="cac:PaymentTerms" >
		<cac:PaymentTerms>
			<cbc:ID><xsl:value-of select="concat('PT-', position())" /></cbc:ID>
			<cbc:PaymentMeansID>PM-001</cbc:PaymentMeansID>
			<xsl:apply-templates select="cbc:Note" />
			<xsl:apply-templates select="//cac:LegalMonetaryTotal/cbc:PayableAmount" mode="PaymentTermsAmount" />
		</cac:PaymentTerms>
	
	</xsl:template>
	
	<xsl:template match="cbc:PayableAmount" mode="PaymentTermsAmount" >
		<cbc:Amount>
			<xsl:attribute name="currencyID" namespace="" >
				<xsl:value-of select="@currencyID" />
			</xsl:attribute>
			<xsl:value-of select="." />
		</cbc:Amount>
		
	</xsl:template>
	
	<xsl:template match="cac:PaymentMeans" >
		<cac:PaymentMeans>
			<cbc:ID>PM-001</cbc:ID>
			<xsl:apply-templates select="cbc:PaymentMeansCode" />
			<xsl:apply-templates select="/i:Invoice/cbc:DueDate" mode="PaymentDueDate" />
			<cbc:PaymentChannelCode listAgencyName="CEN/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 name="PaymentMeansFix">
		<cac:PaymentMeans>
			<cbc:PaymentDueDate><xsl:value-of select="/i:Invoice/cbc:DueDate"/></cbc:PaymentDueDate>
		</cac:PaymentMeans>
	</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" />
			<xsl:apply-templates select="/i:Invoice/cbc:DocumentCurrencyCode" mode="CurrencyCode" />

		</cac:PayeeFinancialAccount>
	
	</xsl:template>
	
	<xsl:template match="cbc:DocumentCurrencyCode" mode="CurrencyCode" >		<cbc:CurrencyCode listID="ISO 4217 Alpha" listAgencyID="5" listURI="http://docs.oasis-open.org/ubl/os-UBL-2.1/cl/gc/default/CurrencyCode-2.1.gc">
			<xsl:value-of select="." />
		</cbc:CurrencyCode>
	</xsl:template>
	
	<xsl:template match="cac:PayeeFinancialAccount" mode="PaymentMeans">
		<cac:PayeeFinancialAccount>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="/i:Invoice/cbc:DocumentCurrencyCode" mode="CurrencyCode" />
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cac:FinancialInstitutionBranch" />
		
		</cac:PayeeFinancialAccount>
	
	</xsl:template>
	

	
	<xsl:template match="cac:FinancialInstitutionBranch">
		<cac:FinancialInstitutionBranch>
			<cac:FinancialInstitution>
				<xsl:apply-templates select="cac:FinancialInstitution/cbc:ID" />
				<xsl:apply-templates select="cac:FinancialInstitution/cbc:Name" />
			</cac:FinancialInstitution>
		</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" select="normalize-space(substring-after(., ' '))" />-->

        <xsl:variable name="instructionID">
			<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:InstructionID>
			<xsl:choose>
				<xsl:when test="string($instructionID) != ''">
					<xsl:value-of select="$instructionID" />
				</xsl:when>
<!--
				<xsl:otherwise>
					<xsl:value-of select="." />
				</xsl:otherwise>
-->
			</xsl:choose>
		</cbc:InstructionID>
	
	</xsl:template>

	
	<xsl:template match="cbc:PaymentID" mode="PaymentID" >
<!--		<xsl:variable name="instructionID" select="normalize-space(substring-before(., ' '))" />-->

        <xsl:variable name="paymentID">
			<xsl:choose>
				<xsl:when test="normalize-space(substring-after(substring-before(., ' '),'HR')) != ''">
					<xsl:value-of select="normalize-space(substring-before(., ' '))" />
				</xsl:when>
				<xsl:when test="string-length(normalize-space(substring-before(., ' '))) = 2">
					<xsl:value-of select="concat('HR', 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="normalize-space(.)" />
				</xsl:when>
				<xsl:when test="string-length(normalize-space(.)) = 2 ">
					<xsl:value-of select="concat('HR', 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 match="cbc:DueDate" mode="PaymentDueDate" >
		<cbc:PaymentDueDate>
			<xsl:value-of select="." />
		</cbc:PaymentDueDate>
	</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="cac:Delivery" >
		<cac:Delivery>
			<xsl:apply-templates select="cbc:ActualDeliveryDate" />
			<xsl:apply-templates select="cac:DeliveryLocation" />
			<xsl:apply-templates select="cac:DeliveryParty" />
		
		</cac:Delivery>
	
	</xsl:template>

	
	
	<xsl:template match="cac:DeliveryParty" >
		<cac:DeliveryParty>
			<!--<xsl:apply-templates select="cbc:EndpointID" />-->
			<xsl:apply-templates select="cbc:EndpointID" mode="DeliveryParty"/>
			<xsl:apply-templates select="cac:PartyName[normalize-space(cbc:Name/text()) != '']" />
			<xsl:apply-templates select="cac:PostalAddress" mode="DeliveryParty"/>
			<xsl:apply-templates select="cac:PartyLegalEntity" />
		
		</cac:DeliveryParty>
	
	</xsl:template>
	<xsl:template match="cac:PartyLegalEntity">
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:CompanyID"/>
			<xsl:apply-templates select="cbc:RegistrationName"/>
		</cac:PartyLegalEntity>
		
		
	</xsl:template>
	
<!--	
	<xsl:template match="cbc:EndpointID" mode="DeliveryParty">
		<cbc:EndpointID><xsl:value-of select="."/></cbc:EndpointID>
	</xsl:template>
-->	
	<xsl:template match="cbc:EndpointID" mode="DeliveryParty">
		<cbc:EndpointID>
			<xsl:if test="@schemeID">
				<!--<xsl:attribute name="schemeID">-->

					<!--<xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID/@schemeID" />-->
				<xsl:attribute name="schemeID">
					<xsl:choose>
						<xsl:when test="normalize-space(@schemeID)= '0088'">
							<xsl:value-of select="'GLN'" />
						</xsl:when>
						<xsl:when test="(normalize-space(//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID/@schemeID) = '0088')">
							<xsl:value-of select="'GLN'" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID/@schemeID" />
						</xsl:otherwise>
					</xsl:choose>
					
				</xsl:attribute>

				<!--</xsl:attribute>-->
			</xsl:if>
			<xsl:value-of select="."/>
		</cbc:EndpointID>

	</xsl:template>
		
	
	<xsl:template match="cac:DeliveryLocation">
		<xsl:if test="cac:Address" >
			<cac:DeliveryAddress>
				<xsl:apply-templates select="cac:Address/cbc:StreetName" />
				<xsl:apply-templates select="cac:Address/cbc:AdditionalStreetName" />
				<xsl:apply-templates select="cac:Address/cbc:CityName" />
				<xsl:apply-templates select="cac:Address/cbc:PostalZone" />
				<xsl:apply-templates select="cac:Address/cbc:CountrySubentity" />
				<xsl:apply-templates select="cac:Address/cac:AddressLine" />
				<xsl:apply-templates select="cac:Address/cac:Country" />
			
			</cac:DeliveryAddress>
		</xsl:if>
	
	</xsl:template>

	<xsl:template match="cac:Address">
		<cac:Address>
			<xsl:apply-templates select="cbc: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" />
		
		</cac:Address>
	
	
	</xsl:template>
	
	<xsl:template match = "cac:AddressLine">
		<cac:AddressLine>
		<xsl:apply-templates select="cbc:Line"/>
		</cac:AddressLine>
	</xsl:template>
	
	<xsl:template match="cac:TaxRepresentativeParty" >
		<cac:TaxRepresentativeParty>
			<xsl:apply-templates select="cac:PartyName[normalize-space(cbc:Name/text()) != '']" />
			<xsl:apply-templates select="cac:PostalAddress" />
			<!--<xsl:apply-templates select="cac:PartyTaxScheme" mode="TaxRepresentativeParty" />-->
		
		
		</cac:TaxRepresentativeParty>
	
	
	</xsl:template>
	

	
	<xsl:template match="cac:PayeeParty" >
		<cac:PayeeParty>
			<xsl:apply-templates select="cac:PartyIdentification"  />
			<xsl:apply-templates select="cac:PartyName[normalize-space(cbc:Name/text()) != '']" />
			<xsl:apply-templates select="cac:PartyLegalEntity" mode="PayeeParty" />
		
		</cac:PayeeParty>
	
	</xsl:template>
	
	<xsl:template match="cac:PartyLegalEntity" mode="PayeeParty" >
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:CompanyID" mode="SchemeID" />
		
		</cac:PartyLegalEntity>
	
	</xsl:template>
	
	<xsl:template match="cac:PartyIdentification" mode="PayeeParty" >
		<cac:PartyIdentification>
			<xsl:apply-templates select="cbc:ID" mode="SchemeID" />
		</cac:PartyIdentification>
	
	</xsl:template>
	
	<xsl:template match="cac:AccountingCustomerParty">
		<cac:AccountingCustomerParty>
		
			<xsl:apply-templates select="cac:Party" mode="AccountingCustomerParty" />
			<xsl:call-template name="AccountingCustomerPartyContact"/>
		
		</cac:AccountingCustomerParty>
	
	</xsl:template>
	
	<xsl:template name="AccountingCustomerPartyContact" >
		<cac:AccountingContact>
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cbc:Telephone" />
			<!--<xsl:apply-templates select="cbc:ElectronicMail" />-->
			<xsl:call-template name="CustomerElectronicMail" />
			<!--<xsl:apply-templates select="/i:Invoice/cbc:Note" mode="OznakaOperatera" />-->
		</cac:AccountingContact>
	</xsl:template>
	
	<xsl:template name="CustomerElectronicMail" >
		<cbc:ElectronicMail>
			<xsl:choose>
				<xsl:when test="normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:ElectronicMail)">
					<xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:ElectronicMail"/>
				</xsl:when>
				<xsl:when test="//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="cac:Party" mode="AccountingCustomerParty" >
		<cac:Party>
			<!--<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="EndpointID" />-->
			<!--<xsl:call-template name="EndpointID" />-->
			<!--<xsl:apply-templates select="cbc:EndpointID" mode="SchemeID"/>-->
			<xsl:call-template name="AccountingCustomerPartyEndpointID"/>

			<!--<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="PartyIdentification" />-->
			<!--<xsl:apply-templates select="cac:PartyIdentification" />-->
			<!--<xsl:apply-templates select="cac:PartyName" />-->
			<xsl:call-template name="PartyName" />
			<xsl:apply-templates select="cac:PostalAddress" />
			<!--<xsl:call-template name="PartyTaxScheme" />-->
			<!--<xsl:apply-templates select="cac:PartyLegalEntity" mode="AccountingCustomerParty" />-->
			<xsl:call-template name="AccountingCustomerPartyLegalEntity" />
			<xsl:apply-templates select="cac:PartyIdentification" />
			<!--<xsl:call-template name="PartyContact" />-->
		
		</cac:Party>
	
	</xsl:template>
	
	
<!--	<xsl:template name="AccountingCustomerPartyEndpointID" >
		<cbc:EndpointID>
			<xsl:choose>
				<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID">
					<xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$customerCompanyId"/>
				</xsl:otherwise>
			</xsl:choose>
		
		</cbc:EndpointID>
	
	</xsl:template>
-->	

	
	<xsl:template name="AccountingCustomerPartyEndpointID" >
		<cbc:EndpointID>
			<xsl:if test="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID/@schemeID">
				<xsl:attribute name="schemeID">
					<xsl:choose>
						<xsl:when test="(normalize-space(//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID/@schemeID) = '0088')">
							<xsl:value-of select="'GLN'" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID/@schemeID" />
						</xsl:otherwise>
					</xsl:choose>
					
				</xsl:attribute>
			</xsl:if>
			<xsl:choose>
				<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID">
					<xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$customerCompanyId"/>
				</xsl:otherwise>
			</xsl:choose>
		
		</cbc:EndpointID>
	
	</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: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>
			<cbc:ID><xsl:value-of select="//cac:AccountingSupplierParty//cac:SellerContact/cbc:ID" /> </cbc:ID>		
			<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ElectronicMail" />
			<cbc:Note><xsl:value-of select="//cac:AccountingSupplierParty//cac:SellerContact/cbc:Name" /> </cbc:Note>		
		
		</cac:AccountingContact>
	
	</xsl:template>
	
	
	
	<xsl:template name="AccountingSupplierPartyContactOld" >
<!--		<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:variable name="odgovornaOsoba">
			<xsl:apply-templates select="i:Invoice/cbc:Note" mode="OdgovornaOsoba" />
		</xsl:variable>
		<xsl:variable name="oznakaOperatera">
			<xsl:apply-templates select="/i:Invoice/cbc:Note" mode="OznakaOperatera" />
		</xsl:variable>
		<cac:AccountingContact>
			<xsl:choose>
				<xsl:when test="normalize-space($odgovornaOsoba) != ''">
					<cbc:Name>
						<xsl:value-of select="$odgovornaOsoba" />
					</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: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: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(., 'Odgovorna osoba:')" >
				<!-- Ako ima Oznaka operatera: -->
				<xsl:value-of	select="normalize-space(substring-after(., 'Odgovorna osoba:'))" />			
			</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(., '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: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: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="AccountingSupplierPartyEndpointID" />
			<!--<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="PartyIdentification" />-->
			<!--<xsl:apply-templates select="cac:PartyName" />-->
			<xsl:call-template name="PartyName" />
			<xsl:apply-templates select="cac:PostalAddress" />
			<!--<xsl:call-template name="PartyTaxScheme" />-->
			<!--<xsl:apply-templates select="cac:PartyLegalEntity" mode="AccountingSupplierParty"/>-->
			<xsl:call-template name="AccountingSupplierPartyLegalEntity" />
			<xsl:apply-templates select="cac:PartyIdentification" />
			<!--<xsl:call-template name="PartyContact" />-->
		
		</cac:Party>
	
	</xsl:template>
	
	<xsl:template name="AccountingSupplierPartyEndpointID" >
		<xsl:choose>
			<xsl:when test="cac:PartyIdentification/cbc:ID[@schemeID = '0088']">
				<cbc:EndpointID scheme='GLN'>
					<xsl:value-of select="cac:PartyIdentification/cbc:ID[@schemeID = '0088']" />
				</cbc:EndpointID>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="cbc:EndpointID" mode="SchemeID"/>
			</xsl:otherwise>
		</xsl:choose>
	
	
	
	
	</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 name="PartyName" >
		<xsl:choose>
			<xsl:when test="cac:PartyName/cbc:Name and (normalize-space(cac:PartyName/cbc:Name) != '')">
				<xsl:apply-templates select="cac:PartyName[normalize-space(cbc:Name/text()) != '']" />
			</xsl:when>
			<xsl:when test="cac:PartyLegalEntity/cbc:RegistrationName and (normalize-space(cac:PartyLegalEntity/cbc:RegistrationName) != '')">
				<cac:PartyName>
					<cbc:Name><xsl:value-of select="normalize-space(cac:PartyLegalEntity/cbc:RegistrationName)" /></cbc:Name>
				</cac:PartyName>
			</xsl:when>
		
		</xsl:choose>
	
	</xsl:template>
	
	
	<xsl:template name="PartyTaxScheme" >
		<cac:PartyTaxScheme>
<!--			<xsl:if test="$isOutsideVAT = 'false'">
				<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="PartyTaxID"/>
			</xsl:if>
-->			<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
		
		</cac:PartyTaxScheme>
	
	</xsl:template>
	
	<xsl:template match="cac:PostalAddress" >
		<cac:PostalAddress>
			<xsl:apply-templates select="cbc: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:choose>
				<xsl:when test="local-name(../../.) = 'AccountingSupplierParty'"><xsl:call-template name="AddressSupplierLine"></xsl:call-template></xsl:when>
				<xsl:when test="local-name(../../.) = 'AccountingCustomerParty'"><xsl:call-template name="AddressCustomerLine"></xsl:call-template></xsl:when>
				<xsl:otherwise><xsl:apply-templates select="cac:AddressLine"></xsl:apply-templates></xsl:otherwise>
			</xsl:choose>
			<xsl:apply-templates select="cac:Country" />
			
		</cac:PostalAddress>
	
	</xsl:template>
	
	<xsl:template match="cac:PostalAddress" mode="DeliveryParty" >
		<cac:PostalAddress>
			<xsl:apply-templates select="cbc: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" />
		</cac:PostalAddress>
		
	</xsl:template>
	
	<xsl:template match="cac:Country" >
		<cac:Country>
			<xsl:apply-templates select="cbc:IdentificationCode" />
			<xsl:apply-templates select="cbc:Name"/>
		
		</cac:Country>
	
	</xsl:template>
	
	<xsl:template match="cbc:IdentificationCode" >
		<cbc:IdentificationCode listID="ISO3166-1" listAgencyID="6" listAgencyName="United Nations Economic Commission for Europe" listName="Country" listVersionID="0.3" name="String" listURI="http://docs.oasis-open.org/ubl/os-ubl-2.0/cl/gc/default/CountryIdentificationCode-2.0.gc" listSchemeURI="urn:oasis:names:specification:ubl:codelist:gc:CountryIdentificationCode-2.0">
			<xsl:value-of select="." />
		</cbc:IdentificationCode>
	</xsl:template>
	
	<xsl:template name="AddressSupplierLine">
		<cac:AddressLine>
			<cbc:Line>
			<xsl:choose>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:Line and normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:Line) != '' "><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:Line"/></xsl:when>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName and normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName) != '' and //cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName and normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName) != '' and //cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber and normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber) != '' "><xsl:value-of select="concat(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName, ' ', //cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber, ' ' , //cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName)"/></xsl:when>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName and normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName) != '' and //cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber and normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber) != ''"><xsl:value-of select="concat(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName, ' ', //cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber)"/></xsl:when>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName and normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName)"><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName"/></xsl:otherwise>
			</xsl:choose>
			</cbc:Line>
		</cac:AddressLine>
	
	</xsl:template>
	
	<xsl:template name="AddressCustomerLine">
		<cac:AddressLine>
			<cbc:Line>
				<xsl:choose>
					<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:Line and normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:Line) != '' "><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:Line"/></xsl:when>
					<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:CityName and normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:CityName) != '' and //cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName and normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName) != '' and //cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber and normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber) != '' "><xsl:value-of select="concat(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName, ' ', //cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber, ' ' , //cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:CityName)"/></xsl:when>
					<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName and normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName) != '' and //cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber and normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber) != ''"><xsl:value-of select="concat(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName, ' ', //cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:BuildingNumber)"/></xsl:when>
					<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName and normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName)"><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:CityName"/></xsl:otherwise>
				</xsl:choose>
			</cbc:Line>
		</cac:AddressLine>
		
	</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:PartyIdentification" >
		<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:choose>
				
		
		</xsl:if>
	</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: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" >
		<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="ID" />
			<xsl:apply-templates select="cbc:SalesOrderReference" />		
		</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:DocumentCurrencyCode">
		<cbc:DocumentCurrencyCode listID="ISO 4217 Alpha" listAgencyID="5" listSchemeURI="http://docs.oasis-open.org/ubl/os-UBL-2.1/cl/gc/default/CurrencyCode-2.1.gc">
			<xsl:value-of select="." />
		</cbc:DocumentCurrencyCode>
	</xsl:template>
	
	<xsl:template match="cbc:InvoiceTypeCode">
		<cbc:InvoiceTypeCode listID="UN/ECE 1001" listAgencyID="6" listURI="http://www.unece.org/trade/untdid/d00a/tred/tred1001.htm">
			<xsl:value-of select="." />
		</cbc:InvoiceTypeCode>
	
	</xsl:template>
	
	<xsl:template match="cbc:CityName" mode="UBLExtension" >
          <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:value-of select="." />
              </ext:InvoiceIssuePlace>
            </ext:ExtensionContent>
          </ext:UBLExtension>

	</xsl:template>
	
	<xsl:template name="UBLExtensionInvoiceIssuer">
		<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 name="HROutgoingInvoiceHeader" >

		<SupplierInvoiceID>
			<xsl:value-of select="string(/i:Invoice/cbc:ID)"/>
		</SupplierInvoiceID>
		<BuyerID>
			<!--<xsl:value-of select="string(/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)"/>-->
			<xsl:value-of select="$customerCompanyId" />
		</BuyerID>
		
	</xsl:template>

	<!--<xsl:template name="HRInvoiceHeader">
	
			<n:Header>
				<n:SupplierID>
					<xsl:value-of select="$supplierCompanyId" />
				</n:SupplierID>
				
				<n:InvoiceType>
					<xsl:value-of select="1"/>
				</n:InvoiceType>
			</n:Header>

	</xsl:template>		-->

	
	<!-- GENERIC ELEMENTS-->
	
	<xsl:template match="cbc:Note">
		<!--<xsl:variable name="vrijeme1" select="substring-after(., 'Vrijeme izdavanja:')" />
		<xsl:variable name="vrijeme2" select="substring-before($vrijeme1, '#AAI#')" />
		<xsl:if test="normalize-space($vrijeme2) = ''">
			<cbc:Note><xsl:value-of select="." /></cbc:Note>
		</xsl:if>-->
		<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(., '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><cbc:Note><xsl:value-of select="."/></cbc:Note></xsl:otherwise>
		</xsl:choose>

	</xsl:template>
		
		
		
		
		
	<xsl:template match="cbc:Note" mode="IssueTime">
		<xsl:if test="normalize-space($issueTime) != ''">
			<cbc:IssueTime><xsl:value-of select="$issueTime" /></cbc:IssueTime>

		</xsl:if>
	
	</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: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:value-of select="format-number(number(.), '#.00')" />-->
		</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:MultiplierFactorNumeric">
		<cbc:MultiplierFactorNumeric>
			
			<xsl:choose>
				
				<xsl:when test="number(.) &lt; 1">
					
					<xsl:value-of select="number(.) * 100" />
					
				</xsl:when>
				
				<xsl:when test="string(number(.)) != 'NaN'">
					
					<xsl:value-of select="number(.)" />
					
				</xsl:when>
				
				<xsl:otherwise>0</xsl:otherwise>
				
			</xsl:choose>
			
		</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>
		TODO: Provjeriti
-->	

		<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 match="cbc:EndpointID" mode="CompanyID">
	
		<cbc:CompanyID>
			<xsl:value-of select="." />
		</cbc:CompanyID>
	</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="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>
<!--		<cbc:EndpointID>
			<xsl:choose>
				<xsl:when test="@schemeID and (normalize-space(@schemeID) != '9934')">
					<xsl:attribute name="schemeID" namespace="">
						<xsl:value-of select="@schemeID" />
					</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="schemeID" namespace="">9934</xsl:attribute>			
				</xsl:otherwise>
			</xsl:choose>
			<xsl:value-of select="." />
		
		</cbc:EndpointID>
-->	
	</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:DocumentType" >
		<cbc:DocumentType><xsl:value-of select="." /></cbc:DocumentType>
	
	</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>
