<?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-12-22
Prilagođeno za EN 2.0 CreditNote

-->
 
 <!-- V 2025-03-13
Dodano zadržavanje cac:AdditionalDocumentReference ako ne sadrži cac:Attachment (INA SAP broj)

-->
 
<!-- V 2025-01-20
Ispravljen format-number template - podrška za velike brojeve

-->

<!-- V 2025-01-13-v9
Ispravljeno hendlanje FinancialInstitutionBranch - zadržava SWIFT i cbc:Name
Ispravljeno hendlanje PartyLegalEntity/cbc:CompanyID za mađarsku (9910)


--> 
 
 <!-- 2024-10-23
Zamjena HRK u EUR u nekim defaultovima

-->
 
 <!-- V 2024-10-11
Ispravljen GLN u AccountingSupplierParty/cac:Party/cbc:EndpointID
Ispravljeno tretiranje velikih brojeva u InvoicedQuantity
Ispravljen DeliveryParty/EndpointID
AccountingCustomerParty/Party / Endpoint – ovo se ne preseli u HRInvoice
Ispravljen PartyName da ne uzima prazne
Izbačeno dodavanje namespace-a na Invoice

-->
 
 <!-- V 2109-12-04-v6
*** Ispravno hendla AccountingContact - stavlja odgovornu osobu i oznaku operatera

*** Maknute namespace deklaracije za i: a: ns1:

*** PartyIdentification se kreira samo ako Party ima podjedinice - inače ne

*** 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)


*** Dodana kontrola da CompanyID mora biti 11 znakova da se uzme kao OIB


*** ProfileID je UVIJEK MojEracunInvoice


-->
 
 <!-- V2019-08-22-v5
*** 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-v4
*** 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-v3
*** 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-030-v2
*** Dodano mapiranje iz /cdn:CreditNote/cac:PaymentTerms/cbc:Note u
- /i:Invoice/cbc:DueDate i 
- /i:Invoice/cac:PaymentMeans/cbc:PaymentDueDate

-->
 <!-- V 2019-07-29-v1
*** Inicijalna verzija
Iz 2019-07-26-V5 EN-HRInvoice


** V 2020-01-14
- Dodano čitanje čvora DeliveryParty
- ako nema PartyLegalEntity u Delivery/ DeliveryParty, kreiraj ga i čitaj i podataka o kupcu
-->


<xsl:stylesheet version="2.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: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 n" 
 xmlns:cdn="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"
 >
 
<!-- xmlns="http://ubl.moj-eracun.hr/v1.2/xsd">-->
	
	<xsl:namespace-alias stylesheet-prefix="n" result-prefix="#default"/>
	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	
	<xsl:variable name="issueTime">
		<xsl:value-of select="/cdn:CreditNote/cbc:IssueTime" />
	</xsl:variable>
	
	<xsl:variable name="dueDate">
		<xsl:choose>
			<xsl:when test="contains(/cdn:CreditNote/cac:PaymentTerms/cbc:Note, '#Datum dospijeća plaćanja:')">
				<xsl:variable name="date1Content">
					<xsl:value-of select="substring-after(/cdn:CreditNote/cac:PaymentTerms/cbc:Note, '#Datum dospijeća plaćanja:')" />
				</xsl:variable>
				<xsl:variable name="dateContent">
					<xsl:value-of select="substring-before($date1Content, ';')" />
				</xsl:variable>
				<xsl:choose>
					<xsl:when test="normalize-space($dateContent) != ''"><xsl:value-of select="normalize-space($dateContent)" /></xsl:when>
					<xsl:when test="normalize-space(/cdn:CreditNote/cac:PaymentMeans/cbc:PaymentDueDate) != ''"><xsl:value-of select="/cdn:CreditNote/cac:PaymentMeans/cbc:PaymentDueDate" /></xsl:when>
					<xsl:otherwise><xsl:value-of select="/cdn:CreditNote/cbc:DueDate" /></xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="normalize-space(/cdn:CreditNote/cac:PaymentMeans/cbc:PaymentDueDate) != ''">
						<xsl:value-of select="/cdn:CreditNote/cac:PaymentMeans/cbc:PaymentDueDate" />
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="/cdn:CreditNote/cbc:DueDate" />
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
		
	</xsl:variable>
	
	<xsl:variable name="noteSum">
		<xsl:variable name="sum" />
		<xsl:for-each select="//cdn:CreditNote/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:otherwise><xsl:value-of select="concat($sum,., ';')"/></xsl:otherwise>
			</xsl:choose>
			
		</xsl:for-each>
		
		
	</xsl:variable>	
	
	
	<xsl:variable name="isDateValid">
		<xsl:choose>
			<xsl:when test="translate(normalize-space($issueTime), '123456789', '000000000') = '00:00:00'">true</xsl:when>
			<xsl:otherwise>false</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
		
	<xsl:variable name="supplierCompanyId">
		<xsl:choose>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID[@schemeID='9934']">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and (string-length(normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)) = 11)">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" mode="SchemeID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID[starts-with(text(), '9934:')]">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID" mode="PartyID2CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID[starts-with(text(), 'HR')]">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID" mode="TaxID2CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID">
				<xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
			</xsl:when>

		</xsl:choose>

	</xsl:variable>
	
	<xsl:variable name="customerCompanyId">
		<xsl:choose>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID[@schemeID='9934']">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and (string-length(normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)) = 11)">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" mode="SchemeID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID[starts-with(text(), '9934:')]">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID" mode="PartyID2CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID[starts-with(text(), '9910:')]">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID" mode="PartyID2CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID[starts-with(text(), 'HR')]">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID" mode="TaxID2CompanyID" />
			</xsl:when>
			<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID">
				<xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
			</xsl:when>

		</xsl:choose>

	</xsl:variable>



	
	<xsl:template match="/">
		<OutgoingDocumentsData xmlns="http://ubl.moj-eracun.hr/v1.2/xsd">

			<!--<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:///D:/E-poslovanje/UBL2.1/xsd/maindoc/UBL-Invoice-2.1.xsd</xsl:attribute>-->
			
			<!--<xsl:call-template name="HRInvoiceHeader" />-->
			<xsl:call-template name="HROutgoingDocumentsHeader" />
<!--			<Header>
				<Sender>
					<cbc:CompanyID>
						<xsl:value-of select="string(/cdn:CreditNote/cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)"/>
					</cbc:CompanyID>
					<cbc:ElectronicMail>
						<xsl:value-of select="string(/cdn:CreditNote/cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ElectronicMail)"/>
					</cbc:ElectronicMail>
				</Sender>
				<Recipient>
					<cbc:CompanyID>
						<xsl:value-of select="string(/cdn:CreditNote/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)"/>
					</cbc:CompanyID>
					<cbc:ElectronicMail>
						<xsl:value-of select="string(/cdn:CreditNote/cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:ElectronicMail)"/>
					</cbc:ElectronicMail>
				</Recipient>
				<DocumentWorkflow>
					<DocumentType>381</DocumentType>
					<SignDocument>2</SignDocument>
					<DocumentID>
						<xsl:value-of select="string(/cdn:CreditNote/cbc:ID)"/>
					</DocumentID>
					<ResponseRequired>true</ResponseRequired>
				</DocumentWorkflow>
	
			</Header>
-->			
			<CdnDocument>
				
				<CreditNoteEnvelope>
					<xsl:apply-templates select="/cdn:CreditNote" />
				</CreditNoteEnvelope>
<!--				<n:AttachedDocumentEnvelope>
					<xsl:apply-templates select="/cdn:CreditNote/cac:AdditionalDocumentReference[cac:Attachment]" mode="Attachment" />
				</n:AttachedDocumentEnvelope>
-->
				<AttachedDocumentEnvelope>
					<xsl:apply-templates select="/cdn:CreditNote/cac:AdditionalDocumentReference[cac:Attachment]" mode="Attachment" />
				</AttachedDocumentEnvelope>
			</CdnDocument>
		</OutgoingDocumentsData>
<!--
		<xsl:comment>
			Generated by Moj eRačun EuropeanNormCreditNote2OutgoingDocumentsDataCreditNote-2025-03-13
		</xsl:comment>	
-->			
	</xsl:template>
	
	<xsl:template match="cac:AdditionalDocumentReference" mode="Attachment">
		<a:AttachedDocument>
			<cbc:ID>
				<xsl:value-of select="cbc:ID" />
			</cbc:ID>
			<cbc:IssueDate>
				<xsl:value-of select="/cdn:CreditNote/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="/cdn:CreditNote/cbc:ID" /></cbc:ParentDocumentID>
			<cac:SenderParty>
				<xsl:apply-templates select="/cdn:CreditNote/cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity" mode="Attachment" />
			</cac:SenderParty>
			<cac:ReceiverParty>
				<xsl:apply-templates select="/cdn:CreditNote/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity" mode="Attachment" />
			</cac:ReceiverParty>
			
			<xsl:apply-templates select="cac:Attachment" mode="Attachment" />

		</a:AttachedDocument>
	</xsl:template>
	
	<xsl:template match="cac:PartyLegalEntity" mode="Attachment" >
		<cac:PartyLegalEntity>
			<cbc:RegistrationName>
				<xsl:value-of select="cbc:RegistrationName"/>
			</cbc:RegistrationName>
			<cbc:CompanyID>
				<xsl:value-of select="cbc:CompanyID" />
			</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:IssueTime" >
		<cbc:IssueTime>
			<xsl:value-of select="."/>
			
		</cbc:IssueTime>
		
	</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="cdn:CreditNote">
		
		<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" 
>
			<ext:UBLExtensions>
				<xsl:apply-templates select="/cdn:CreditNote/cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName" mode="UBLExtension" />
				<xsl:apply-templates select="/cdn:CreditNote/cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyLegalForm" mode="UBLExtension" />
			
			</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:choose>
				<xsl:when test="cbc:IssueTime"><xsl:apply-templates select="cbc:IssueTime"/></xsl:when>
				<xsl:when test="normalize-space($issueTime) != '' and $isDateValid = 'true'"><cbc:IssueTime><xsl:value-of select="$issueTime" /></cbc:IssueTime></xsl:when>
				<xsl:otherwise><cbc:IssueTime>00:00:00</cbc:IssueTime></xsl:otherwise>
			</xsl:choose>

			<!--<xsl:apply-templates select="cac:PaymentTerms/cbc:Note" mode="DueDate" />-->
			<xsl:apply-templates select="cbc:CreditNoteTypeCode" />
			<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[not(cac:Attachment)]" 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:CreditNoteLine" />
		
		</Invoice>
	
	</xsl:template>
	
	<xsl:template match="cac:CreditNoteLine">
		<cac:InvoiceLine>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
			<xsl:apply-templates select="cbc:Note" />
			<xsl:apply-templates select="cbc:CreditedQuantity" />
			<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" />
			<xsl:apply-templates select="cac:AllowanceCharge" mode="Price" />
		</cac:Price>
	
	</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: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:CreditedQuantity/@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: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: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="/cdn:CreditNote/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 - 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: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 - 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: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: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:CreditedQuantity">
		<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" mode="TaxTotal" />-->		
			<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') " />
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<cbc:TaxAmount>
					 <xsl:attribute name="currencyID"><xsl:value-of select="$currencyID" /></xsl:attribute>
					 <!--<xsl:value-of select="$taxAmount" />-->
					 <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="$taxAmount" />-->
						 <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:PayableRoundingAmount" />
			<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="/cdn:CreditNote/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:choose>
				<xsl:when test="contains(cbc:Note, '#Datum dospijeća plaćanja:')">
					<xsl:variable name="noteContent">
						<xsl:value-of select="substring-after(cbc:Note, ';')" />
					</xsl:variable>
					
					<xsl:if test="normalize-space($noteContent) != ''">
						<cbc:Note>
							<xsl:value-of select="normalize-space($noteContent)" />
						</cbc:Note>					
					</xsl:if>	

				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="cbc:Note" />
				</xsl:otherwise>
			</xsl:choose>
			
			<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="/cdn:CreditNote/cbc:DueDate" mode="PaymentDueDate" />-->
			<xsl:if test="normalize-space($dueDate) != ''">
				<cbc:PaymentDueDate><xsl:value-of select="$dueDate" /></cbc:PaymentDueDate>
			</xsl:if>
			<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="/cdn:CreditNote/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="/cdn:CreditNote/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>
	
	
<!-- TODO - provjeriti da li je potrebna zamjena
	<xsl:template match="cbc:PaymentID" mode="PaymentID" >
		<xsl:variable name="instructionID" select="normalize-space(substring-before(., ' '))" />
		<cbc:PaymentID>
			<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:PaymentID>
	
	</xsl:template>

	
	<xsl:template match="cbc:PaymentID" mode="InstructionID" >
		<xsl:variable name="instructionID" select="normalize-space(substring-after(., ' '))" />
		<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="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" />-->
			<xsl:apply-templates select="cac:PartyName[normalize-space(cbc:Name/text()) != '']" />
			<xsl:apply-templates select="cac:PostalAddress" />
			<xsl:choose>
				<xsl:when test="cac:PartyLegalEntity">
					<xsl:apply-templates select="cac:PartyLegalEntity" mode="DeliveryParty"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="PartyLegalEntityDelivery"/>
				</xsl:otherwise>		
			</xsl:choose>
		</cac:DeliveryParty>
		
	</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:PartyLegalEntity" mode="DeliveryParty">
		<cac:PartyLegalEntity>
			<xsl:choose>
				<xsl:when test="cbc:RegistrationName and normalize-space(cbc:RegistrationName) != ''" >
					<xsl:apply-templates select="cbc:RegistrationName" mode="DeliveryParty"/>
				</xsl:when>			
				<xsl:otherwise>
					<xsl:call-template name="DeliveryPartyCompanyID"/>	
				</xsl:otherwise>
			</xsl:choose>
			
			<xsl:choose>
				<xsl:when test="cbc:CompanyID and normalize-space(cbc:CompanyID) != ''" >
					<xsl:apply-templates select="cbc:CompanyID" mode="DeliveryParty"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="DeliveryPartyCompanyID"/>
				</xsl:otherwise>
			</xsl:choose>	
		</cac:PartyLegalEntity>
		
	</xsl:template>
	
	<xsl:template match="cbc:RegistrationName" mode="DeliveryParty">
		<cbc:RegistrationName>
			<xsl:value-of select="."/>
		</cbc:RegistrationName>
	</xsl:template>
	
	<xsl:template name="PartyLegalEntityDelivery">
		<cac:PartyLegalEntity>
			<xsl:call-template name="DeliveryPartyRegistrationName"/>
			<xsl:call-template name="DeliveryPartyCompanyID"/>
		</cac:PartyLegalEntity>
	</xsl:template>
	
	<xsl:template name="DeliveryPartyRegistrationName">
		<cbc:RegistrationName>
			<xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName"/>
		</cbc:RegistrationName>
	</xsl:template>
	
	<xsl:template match="cbc:CompanyID" mode="DeliveryParty">
		<cbc:CompanyID>
			<xsl:value-of select="."/>
		</cbc:CompanyID>
	</xsl:template>
	
	<xsl:template name="DeliveryPartyCompanyID">
		<cbc:CompanyID>
			<xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID"/>
		</cbc:CompanyID>
	</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" />
			<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" />
			<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="/cdn:CreditNote/cbc:Note" mode="OznakaOperatera" />-->
		</cac:AccountingContact>
	</xsl:template>
	
	<xsl:template name="CustomerElectronicMail" >
		<cbc:ElectronicMail>
<!--			<xsl:choose>
				<xsl:when test="cbc:ElectronicMail and ( normalize-space(cbc:ElectronicMail) != '')"><xsl:value-of select="." /></xsl:when>
				<xsl:otherwise>adresar@moj-eracun.hr</xsl:otherwise>
			</xsl:choose>
-->		
			<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: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:PartyLegalEntity" mode="AccountingCustomerParty" >
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:RegistrationName" />
			<!--<xsl:apply-templates select="cbc:CompanyID" mode="SchemeID" />-->
			<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="/cdn:CreditNote/cbc:Note" mode="OdgovornaOsoba" />

			<xsl:apply-templates select="/cdn:CreditNote/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:variable name="oznaka3">
	 		<xsl:value-of select="normalize-space(substring-after(., 'Operator:'))" />
	 	</xsl:variable>
	 	<xsl:variable name="oznaka4">
	 		<xsl:value-of select="normalize-space(substring-after(., 'Operater:'))" />
	 	</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: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: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" />
			</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="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: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" />
		
		</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 match="cac:AddressLine">
		<cac:AddressLine>
			<xsl:apply-templates select="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) != '')">
			<cac:PartyIdentification>
				<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) != ''"><cbc:ID><xsl:value-of select="$id2" /></cbc:ID></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>
				
			</cac:PartyIdentification>	
		</xsl:if>
	</xsl:template>

	<xsl:template match="cac:AdditionalDocumentReference" mode="ID">
		<cac:AdditionalDocumentReference>
			<cbc:ID>
				<xsl:value-of select="cbc:ID" />
			</cbc:ID>
			<cbc:IssueDate>
				<xsl:value-of select="cbc:IssueDate"/>
			</cbc:IssueDate>
			<cbc:DocumentType>
				<xsl:value-of select="cbc:DocumentType"/>
			</cbc:DocumentType>
		</cac:AdditionalDocumentReference>
	</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:CreditNoteTypeCode">
		<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 match="cbc:CompanyLegalForm" mode="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:value-of select="." />
				</ext:InvoiceIssuer>
			</ext:ExtensionContent>
		</ext:UBLExtension>
		
	</xsl:template>
	

	
	<xsl:template name="HROutgoingDocumentsHeader" >
		<Header xmlns="http://ubl.moj-eracun.hr/v1.2/xsd">
			<Sender>
				<cbc:CompanyID>
					<xsl:value-of select="string(/cdn:CreditNote/cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)"/>
				</cbc:CompanyID>
				<cbc:ElectronicMail>
					<xsl:value-of select="string(/cdn:CreditNote/cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ElectronicMail)"/>
				</cbc:ElectronicMail>
			</Sender>
			<Recipient>
				<cbc:CompanyID>
					<xsl:value-of select="string(/cdn:CreditNote/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)"/>
				</cbc:CompanyID>
				<cbc:ElectronicMail>
					<xsl:value-of select="string(/cdn:CreditNote/cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:ElectronicMail)"/>
				</cbc:ElectronicMail>
			</Recipient>
			<DocumentWorkflow>
				<DocumentType>381</DocumentType>
				<SignDocument>2</SignDocument>
				<DocumentID>
					<xsl:value-of select="string(/cdn:CreditNote/cbc:ID)"/>
				</DocumentID>
				<ResponseRequired>true</ResponseRequired>
			</DocumentWorkflow>

		</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="normalize-space(substring-after(., 'Vrijeme izdavanja:')) != ''" />
			
			<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:PayableRoundingAmount | cbc:PayableAmount " >
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:attribute name="currencyID" namespace="">
				<xsl:value-of select="@currencyID" />
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="number(.) = 0">0.00</xsl:when>
				<xsl:when test="string(number(.))='NaN'">0.00</xsl:when>
				<xsl:otherwise><xsl:value-of select="format-number(number(.), '##########0.00')" /></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')" />
					</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:value-of select="number(.)" />
		</cbc:MultiplierFactorNumeric>
	
	</xsl:template>

	<xsl:template match="cbc:Percent">
		<cbc:Percent>
			<xsl:value-of select="number(.)" />
		</cbc:Percent>
	
	</xsl:template>
	
	<xsl:template match="cbc:CompanyID" mode="PartyIdentification">
		<cac:PartyIdentification>
			<cbc:ID>
				<xsl:value-of select="concat('9934:', .)"/>
			</cbc:ID>
		
		</cac:PartyIdentification>
	
	</xsl:template>
	
	<xsl:template match="cbc:CompanyID" mode="PartyTaxID">
		<cbc:CompanyID>
			<xsl:value-of select="concat('HR', .)"/>
		</cbc:CompanyID>
	
	</xsl:template>

	
	<xsl:template match="cbc:CompanyID" mode="EndpointID" >
		<cbc:EndpointID>
			<xsl:attribute name="schemeID" namespace="">9934</xsl:attribute>
			<xsl:value-of select="." />
		
		</cbc:EndpointID>
	
	</xsl:template>
	
	<xsl:template match="cbc: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="cbc:ID" mode="PartyID2CompanyID" >
		<cbc:ID>
			<xsl:choose>
				<xsl:when test="substring(., 5, 1) = ':'"><xsl:value-of select="substring(., 6)" /></xsl:when>
				<xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
			</xsl:choose>
		
		</cbc:ID>	
	
	</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>
