<?xml version="1.0" encoding="UTF-8"?>
<!-- ===== Copyright Notice ===== -->
<!--  
 Copyright (C) Elektronički Računi d.o.o 2019. All Rights Reserved.

Elektronički Računi d.o.o  takes no position regarding the validity or scope of any 
  intellectual property or other rights that might be claimed to pertain 
  to the implementation or use of the technology described in this 
  document or the extent to which any license under such rights 
  might or might not be available; neither does it represent that it has 
  made any effort to identify any such rights. 

This document and translations of it may be copied and furnished to 
  others, in whole or in part, for the purpose of integration with
  Moj eRačun network, provided that the above copyright notice and this 
  paragraph are included on all such copies and derivative works. 
  Other usages of the document or its translations are prohibited.
  However, this document itself may not be modified in any way, 
  such as by removing the copyright notice or references to 
  OASIS or Elektronički računi d.o.o., 
 -->
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:ns1="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:ns2="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
                          xmlns:qdt="urn:un:unece:uncefact:data:Standard:QualifiedDataType:100"
                          xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
                          xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100"
exclude-result-prefixes="ns0 xs ns1 ns2 rsm ram udt qdt">
	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	
	<xsl:variable name="isOutsideVAT">
		<xsl:choose>
			<xsl:when test="//ram:ApplicableTradeTax[normalize-space(ram:CategoryCode/text()) != 'O']">false</xsl:when>
			<xsl:otherwise>true</xsl:otherwise>
		</xsl:choose>
	
	</xsl:variable>
	
	<xsl:template match="/">
		<xsl:choose>
			<xsl:when test="/ns2:Invoice"><xsl:copy-of select="/ns2:Invoice" /></xsl:when>
			<xsl:otherwise><xsl:apply-templates select="rsm:CrossIndustryInvoice" /></xsl:otherwise>
		</xsl:choose>
		
		
	</xsl:template>
	
	<xsl:template match="rsm:CrossIndustryInvoice">
		<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
			xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" 
			xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
			xmlns:xsd="http://www.w3.org/2001/XMLSchema"			
			xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
			xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
			xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
			xmlns:n0="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-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:udt="urn:oasis:names:specification:ubl:schema:xsd:UnqualifiedDataTypes-2" 
			xmlns:ccts-cct="urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2"  
			xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" 
		>
			
		

			<cbc:CustomizationID>
				<xsl:value-of select="'urn:cen.eu:en16931:2017'"/>
			</cbc:CustomizationID>

			<xsl:apply-templates select="rsm:ExchangedDocumentContext/ram:BusinessProcessSpecifiedDocumentContextParameter" />
			<xsl:apply-templates select="rsm:ExchangedDocument/ram:ID" mode="ID" />
			
			
			<xsl:apply-templates select="rsm:ExchangedDocument/ram:IssueDateTime" />
			
			<xsl:apply-templates select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeSettlement/ram:SpecifiedTradePaymentTerms/ram:DueDateDateTime" />
			
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">InvoiceTypeCode</xsl:with-param>
				<xsl:with-param name="node" select="rsm:ExchangedDocument/ram:TypeCode" />
			</xsl:call-template>
			
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">Note</xsl:with-param>
				<xsl:with-param name="node" select="rsm:ExchangedDocument/ram:IncludedNote/ram:Content" />
			</xsl:call-template>
			
			
			<xsl:apply-templates select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeSettlement/ram:ApplicableTradeTax/ram:TaxPointDate" />
			
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">DocumentCurrencyCode</xsl:with-param>
				<xsl:with-param name="node" select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeSettlement/ram:InvoiceCurrencyCode" />
			</xsl:call-template>

			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">AccountingCost</xsl:with-param>
				<xsl:with-param name="node" select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeSettlement/ram:ReceivableSpecifiedTradeAccountingAccount/ram:ID" />
			</xsl:call-template>
			
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">BuyerReference</xsl:with-param>
				<xsl:with-param name="node" select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeAgreement/ram:BuyerReference" />
			</xsl:call-template>
			
			<xsl:apply-templates select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeSettlement/ram:BillingSpecifiedPeriod" />
			
			<xsl:apply-templates select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeAgreement/ram:BuyerOrderReferencedDocument" />
			
			<xsl:apply-templates select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeSettlement/ram:InvoiceReferencedDocument" />
			
			<xsl:apply-templates select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeDelivery/ram:DespatchAdviceReferencedDocument" />
			
			<xsl:apply-templates select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeDelivery/ram:ReceivingAdviceReferencedDocument" />
			
			<xsl:apply-templates select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeAgreement/ram:AdditionalReferencedDocument[ram:TypeCode/text() = '50']" mode="OriginatorDocumentReference"/>
			
			<xsl:apply-templates select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeAgreement/ram:ContractReferencedDocument" />
	
			<xsl:apply-templates select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeAgreement/ram:AdditionalReferencedDocument[ram:TypeCode/text() != '50']" mode="AdditionalDocumentReference"/>
			
			<xsl:apply-templates select="rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeAgreement/ram:SpecifiedProcuringProject" />
			
			<xsl:apply-templates select="//ram:SellerTradeParty" />
			
			<xsl:apply-templates select="//ram:BuyerTradeParty" />
			
			<xsl:apply-templates select="//ram:PayeeTradeParty" />
			
			<xsl:apply-templates select="//ram:SellerTaxRepresentativeTradeParty" />
			
			<xsl:apply-templates select="//ram:ShipToTradeParty" />
			
			<xsl:choose>
				<xsl:when test="//ram:SpecifiedTradeSettlementPaymentMeans[ram:TypeCode/text() = '30']">
					<xsl:apply-templates select="//ram:SpecifiedTradeSettlementPaymentMeans[ram:TypeCode/text() = '30']" mode="CreditTransfer" />
				</xsl:when>
				<xsl:when test="//ram:SpecifiedTradeSettlementPaymentMeans[ram:TypeCode/text() = '59']">
					<xsl:apply-templates select="//ram:SpecifiedTradeSettlementPaymentMeans[ram:TypeCode/text() = '59']" mode="DirectDebit" />
				</xsl:when>
					<xsl:when test="//ram:SpecifiedTradeSettlementPaymentMeans[ram:TypeCode/text() = '48']">
					<xsl:apply-templates select="//ram:SpecifiedTradeSettlementPaymentMeans[ram:TypeCode/text() = '48']" mode="PaymentCard" />
				</xsl:when>
			</xsl:choose>
			
			<xsl:apply-templates select="//ram:SpecifiedTradePaymentTerms" />
			
			<xsl:apply-templates select="//ram:SpecifiedTradeAllowanceCharge" />
			
			<xsl:apply-templates select="//ram:SpecifiedTradeSettlementHeaderMonetarySummation/ram:TaxTotalAmount[@currencyID = //ram:InvoiceCurrencyCode]" mode="MainTacTotal" />
			
			<xsl:apply-templates select="//ram:SpecifiedTradeSettlementHeaderMonetarySummation/ram:TaxTotalAmount[@currencyID = //ram:TaxCurrencyCode]" mode="ACTacTotal" />
			
			<xsl:apply-templates select="//ram:SpecifiedTradeSettlementHeaderMonetarySummation" />
			
			<xsl:apply-templates select="//ram:IncludedSupplyChainTradeLineItem" />
		<!--



			<xsl:apply-templates select="cac:InvoiceLine" />-->
		</Invoice>
	
	</xsl:template>
	
	<xsl:template match="ram:IncludedSupplyChainTradeLineItem" >
		<cac:InvoiceLine>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="ram:AssociatedDocumentLineDocument/ram:LineID" />
			</xsl:call-template>
			
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">Note</xsl:with-param>
				<xsl:with-param name="node" select="ram:AssociatedDocumentLineDocument/ram:IncludedNote/ram:Content" />
			</xsl:call-template>
			
			<xsl:call-template name="cbcQuantity">
				<xsl:with-param name="name">InvoicedQuantity</xsl:with-param>
				<xsl:with-param name="node" select="ram:SpecifiedLineTradeDelivery/ram:BilledQuantity" />
				<xsl:with-param name="unit" select="ram:SpecifiedLineTradeDelivery/ram:BilledQuantity/@unitCode" />
			</xsl:call-template>

			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">LineExtensionAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:SpecifiedLineTradeSettlement/ram:SpecifiedTradeSettlementLineMonetarySummation/ram:LineTotalAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">AccountingCost</xsl:with-param>
				<xsl:with-param name="node" select="ram:SpecifiedLineTradeSettlement/ram:ReceivableSpecifiedTradeAccountingAccount/ram:ID" />
			</xsl:call-template>
			
			<xsl:apply-templates select="ram:SpecifiedLineTradeSettlement/ram:BillingSpecifiedPeriod" mode="InvoiceLine" />
			
			<xsl:apply-templates select="ram:SpecifiedLineTradeAgreement/ram:BuyerOrderReferencedDocument" mode="InvoiceLine" />
			
			<xsl:apply-templates select="ram:SpecifiedLineTradeSettlement/ram:AdditionalReferencedDocument" mode="InvoiceLine" />
			
			<xsl:apply-templates select="ram:SpecifiedLineTradeSettlement/ram:SpecifiedTradeAllowanceCharge" mode="InvoiceLine" />
			
			<xsl:apply-templates select="ram:SpecifiedTradeProduct" mode="InvoiceLine" />
			
			<xsl:apply-templates select="ram:SpecifiedLineTradeAgreement" mode="Price" />
			
<!--			

			
				<xsl:apply-templates select="cac:Price" mode="InvoiceLine" />
			
-->		
		</cac:InvoiceLine>
	</xsl:template>
	
	<xsl:template match="ram:SpecifiedLineTradeAgreement" mode="Price" >
		<cac:Price>
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">PriceAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:NetPriceProductTradePrice/ram:ChargeAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcQuantity">
				<xsl:with-param name="name">BaseQuantity</xsl:with-param>
				<xsl:with-param name="node" select="ram:GrossPriceProductTradePrice/ram:BasisQuantity" />
				<xsl:with-param name="unit" select="ram:GrossPriceProductTradePrice/ram:BasisQuantity/@unitCode" />
			</xsl:call-template>
		
	
		</cac:Price>
	
	</xsl:template>
	
	<xsl:template match="ram:SpecifiedTradeProduct" mode="InvoiceLine">
	
		<cac:Item>

			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">Description</xsl:with-param>
				<xsl:with-param name="node" select="ram:Description" />
			</xsl:call-template>

			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">Name</xsl:with-param>
				<xsl:with-param name="node" select="ram:Name" />
			</xsl:call-template>
			
			<xsl:apply-templates select="ram:BuyerAssignedID" mode="Item"/>
	
			<xsl:apply-templates select="ram:SellerAssignedID" mode="Item"/>
			
			<xsl:apply-templates select="ram:GlobalID" mode="Item"/>
			
			<xsl:apply-templates select="ram:OriginTradeCountry" mode="Item" />
			
			<xsl:apply-templates select="ram:DesignatedProductClassification/ram:ClassCode" mode="Item" />
			
			<xsl:apply-templates select="../ram:SpecifiedLineTradeSettlement/ram:ApplicableTradeTax" mode="Item" />
	 
			<xsl:apply-templates select="ram:ApplicableProductCharacteristic" mode="Item" />
	
			
			
		</cac:Item>

	
	</xsl:template>
	
	<xsl:template match="ram:ApplicableProductCharacteristic" mode="Item" >
		<cac:AdditionalItemProperty>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">Name</xsl:with-param>
				<xsl:with-param name="node" select="ram:Description" />
			</xsl:call-template>

			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">Value</xsl:with-param>
				<xsl:with-param name="node" select="ram:Value" />
			</xsl:call-template>

		
		</cac:AdditionalItemProperty>
		
	
	</xsl:template>
	
	<xsl:template match="ram:ApplicableTradeTax" mode="Item" >
		<cac:ClassifiedTaxCategory>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="ram:CategoryCode" />
			</xsl:call-template>

			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">Percent</xsl:with-param>
				<xsl:with-param name="node" select="ram:RateApplicablePercent" />
			</xsl:call-template>
			
			<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
		
		</cac:ClassifiedTaxCategory>
	
	</xsl:template>
	
	<xsl:template match="ram:DesignatedProductClassification/ram:ClassCode" mode="Item" >
		<cac:CommodityClassification>
			<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>
		</cac:CommodityClassification>
	
	</xsl:template>
	
	<xsl:template match="ram:OriginTradeCountry" mode="Item" >
		<cac:OriginCountry>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">IdentificationCode</xsl:with-param>
				<xsl:with-param name="node" select="ram:ID" />
			</xsl:call-template>
		
		
		</cac:OriginCountry>
	
	</xsl:template>
	
	<xsl:template match="ram:GlobalID" mode="Item" >
		<cac:StandardItemIdentification>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="." />
				<xsl:with-param name="scheme" select="@schemeID" />
			</xsl:call-template>
		
		</cac:StandardItemIdentification>
	
	
	</xsl:template>

	
	<xsl:template match="ram:SellerAssignedID" mode="Item" >
		<cac:SellersItemIdentification>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="." />
			</xsl:call-template>
		
		</cac:SellersItemIdentification>
	
	
	</xsl:template>

	
	<xsl:template match="ram:BuyerAssignedID" mode="Item" >
		<cac:BuyersItemIdentification>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="." />
			</xsl:call-template>
		
		</cac:BuyersItemIdentification>
	
	
	</xsl:template>
	
	<xsl:template match="ram:SpecifiedTradeAllowanceCharge" mode="InvoiceLine"  >
		<cac:AllowanceCharge>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ChargeIndicator</xsl:with-param>
				<xsl:with-param name="node" select="ram:ChargeIndicator/udt:Indicator" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">AllowanceChargeReasonCode</xsl:with-param>
				<xsl:with-param name="node" select="ram:ReasonCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">AllowanceChargeReason</xsl:with-param>
				<xsl:with-param name="node" select="ram:Reason" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">MultiplierFactorNumeric</xsl:with-param>
				<xsl:with-param name="node" select="ram:CalculationPercent" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">Amount</xsl:with-param>
				<xsl:with-param name="node" select="ram:ActualAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">BaseAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:BasisAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
		</cac:AllowanceCharge>

	
	</xsl:template>
	
	
	<xsl:template match="ram:AdditionalReferencedDocument" mode="InvoiceLine" >
	
		<cac:DocumentReference>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="ram:IssuerAssignedID" />
			</xsl:call-template>
		
		</cac:DocumentReference>
	
	</xsl:template>
	
	<xsl:template match="ram:BuyerOrderReferencedDocument" mode="InvoiceLine" >
		<cac:OrderLineReference>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">LineID</xsl:with-param>
				<xsl:with-param name="node" select="ram:LineID" />
			</xsl:call-template>
		
		
		</cac:OrderLineReference>
	
	</xsl:template>
		
	<xsl:template match="ram:BillingSpecifiedPeriod" mode="InvoiceLine">
	
		<cac:InvoicePeriod>
		
			<xsl:call-template name="cbcDate">
				<xsl:with-param name="name">StartDate</xsl:with-param>
				<xsl:with-param name="node" select="ram:StartDateTime/udt:DateTimeString" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcDate">
				<xsl:with-param name="name">EndDate</xsl:with-param>
				<xsl:with-param name="node" select="ram:EndDateTime/udt:DateTimeString" />
			</xsl:call-template>
		
		</cac:InvoicePeriod>
	
	

	
	
	</xsl:template>
	

	
	<xsl:template match="ram:SpecifiedTradeSettlementHeaderMonetarySummation">
		<cac:LegalMonetaryTotal>
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">LineExtensionAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:LineTotalAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">TaxExclusiveAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:TaxBasisTotalAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">TaxInclusiveAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:GrandTotalAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">AllowanceTotalAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:AllowanceTotalAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">ChargeTotalAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:ChargeTotalAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">PrepaidAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:TotalPrepaidAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">PayableRoundingAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:RoundingAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">PayableAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:DuePayableAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
		
		
		</cac:LegalMonetaryTotal>
	
	</xsl:template>
	
	<xsl:template match="ram:TaxTotalAmount" mode="ACTacTotal" >
		<cac:TaxTotal>
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">TaxAmount</xsl:with-param>
				<xsl:with-param name="node" select="." />
				<xsl:with-param name="currency" select="@currencyID" />
			</xsl:call-template>
		
		
		</cac:TaxTotal>
	
	
	</xsl:template>

	
	<xsl:template match="ram:TaxTotalAmount" mode="MainTacTotal" >
		<cac:TaxTotal>
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">TaxAmount</xsl:with-param>
				<xsl:with-param name="node" select="." />
				<xsl:with-param name="currency" select="@currencyID" />
			</xsl:call-template>
		
			<xsl:apply-templates select="//ram:ApplicableHeaderTradeSettlement/ram:ApplicableTradeTax" mode="MainTaxTotal" />
		
		</cac:TaxTotal>
	
	
	</xsl:template>
	
	<xsl:template match="ram:ApplicableTradeTax" mode="MainTaxTotal">
		<cac:TaxSubtotal>
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">TaxableAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:BasisAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">TaxAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:CalculatedAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
			
			<cac:TaxCategory>
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">ID</xsl:with-param>
					<xsl:with-param name="node" select="ram:CategoryCode" />
				</xsl:call-template>
			
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">Percent</xsl:with-param>
					<xsl:with-param name="node" select="ram:RateApplicablePercent" />
				</xsl:call-template>
			
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">TaxExemptionReasonCode</xsl:with-param>
					<xsl:with-param name="node" select="ram:ExemptionReasonCode" />
				</xsl:call-template>
			
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">TaxExemptionReason</xsl:with-param>
					<xsl:with-param name="node" select="ram:ExemptionReason" />
				</xsl:call-template>
				
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			
			</cac:TaxCategory>
		
		
		</cac:TaxSubtotal>
	
	
	</xsl:template>
	
	<xsl:template match="ram:SpecifiedTradeAllowanceCharge">
		<cac:AllowanceCharge>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ChargeIndicator</xsl:with-param>
				<xsl:with-param name="node" select="ram:ChargeIndicator/udt:Indicator" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">AllowanceChargeReasonCode</xsl:with-param>
				<xsl:with-param name="node" select="ram:ReasonCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">AllowanceChargeReason</xsl:with-param>
				<xsl:with-param name="node" select="ram:Reason" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">MultiplierFactorNumeric</xsl:with-param>
				<xsl:with-param name="node" select="ram:CalculationPercent" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">Amount</xsl:with-param>
				<xsl:with-param name="node" select="ram:ActualAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcAmount">
				<xsl:with-param name="name">BaseAmount</xsl:with-param>
				<xsl:with-param name="node" select="ram:BasisAmount" />
				<xsl:with-param name="currency" select="//ram:InvoiceCurrencyCode" />
			</xsl:call-template>
		
			<xsl:apply-templates select="ram:CategoryTradeTax" mode="AllowanceCharge" />
		
		</cac:AllowanceCharge>
	
	</xsl:template>
	
	<xsl:template match="ram:CategoryTradeTax" mode="AllowanceCharge" >
		<cac:TaxCategory>
	
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="ram:TypeCode" />
			</xsl:call-template>

			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">Percent</xsl:with-param>
				<xsl:with-param name="node" select="ram:RateApplicablePercent" />
			</xsl:call-template>
			<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
		
		</cac:TaxCategory>
	
	</xsl:template>
	
	<xsl:template match="ram:SpecifiedTradePaymentTerms" >
	
		<cac:PaymentTerms>
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">Note</xsl:with-param>
					<xsl:with-param name="node" select="ram:Description" />
				</xsl:call-template>
				
		
		</cac:PaymentTerms>
	
	
	</xsl:template>
	
	
	<xsl:template match="ram:SpecifiedTradeSettlementPaymentMeans" mode="PaymentCard">
		<cac:PaymentMeans>
		
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">PaymentMeansCode</xsl:with-param>
					<xsl:with-param name="node" select="ram:TypeCode" />
				</xsl:call-template>
				
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">InstructionNote</xsl:with-param>
					<xsl:with-param name="node" select="ram:Information" />
				</xsl:call-template>
				
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">PaymentID</xsl:with-param>
					<xsl:with-param name="node" select="//rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeSettlement/ram:PaymentReference" />
				</xsl:call-template>
				
			<xsl:apply-templates select="ram:ApplicableTradeSettlementFinancialCard" />
		
		</cac:PaymentMeans>
	
	</xsl:template>
	
	<xsl:template match="ram:ApplicableTradeSettlementFinancialCard">
		<cac:CardAccount>
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">PrimaryAccountNumberID</xsl:with-param>
					<xsl:with-param name="node" select="ram:ID" />
				</xsl:call-template>
			
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">HolderName</xsl:with-param>
					<xsl:with-param name="node" select="ram:CardholderName" />
				</xsl:call-template>
	
		</cac:CardAccount>
	
	</xsl:template>

	
	<xsl:template match="ram:SpecifiedTradeSettlementPaymentMeans" mode="DirectDebit">
		<cac:PaymentMeans>
		
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">PaymentMeansCode</xsl:with-param>
					<xsl:with-param name="node" select="ram:TypeCode" />
				</xsl:call-template>
				
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">InstructionNote</xsl:with-param>
					<xsl:with-param name="node" select="ram:Information" />
				</xsl:call-template>
				
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">PaymentID</xsl:with-param>
					<xsl:with-param name="node" select="//rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeSettlement/ram:PaymentReference" />
				</xsl:call-template>
				
			<xsl:apply-templates select="ram:PayerPartyDebtorFinancialAccount" />
		
		</cac:PaymentMeans>
	
	</xsl:template>
	
	<xsl:template match="ram:PayerPartyDebtorFinancialAccount">
		<cac:PaymentMandate>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="//ram:DirectDebitMandateID" />
			</xsl:call-template>
		
			<cac:PayerFinancialAccount>
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">ID</xsl:with-param>
					<xsl:with-param name="node" select="ram:IBANID" />
				</xsl:call-template>
			</cac:PayerFinancialAccount>
		
		</cac:PaymentMandate>
	
	
	</xsl:template>

	
	<xsl:template match="ram:SpecifiedTradeSettlementPaymentMeans" mode="CreditTransfer">
		<cac:PaymentMeans>
		
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">PaymentMeansCode</xsl:with-param>
					<xsl:with-param name="node" select="ram:TypeCode" />
				</xsl:call-template>
				
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">InstructionNote</xsl:with-param>
					<xsl:with-param name="node" select="ram:Information" />
				</xsl:call-template>
				
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">PaymentID</xsl:with-param>
					<xsl:with-param name="node" select="//rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeSettlement/ram:PaymentReference" />
				</xsl:call-template>
				
			<xsl:apply-templates select="ram:PayeePartyCreditorFinancialAccount" />
		
		</cac:PaymentMeans>
	
	</xsl:template>
	
	<xsl:template match="ram:PayeePartyCreditorFinancialAccount" >
		<cac:PayeeFinancialAccount>

				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">ID</xsl:with-param>
					<xsl:with-param name="node" select="ram:IBANID" />
				</xsl:call-template>

				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">Name</xsl:with-param>
					<xsl:with-param name="node" select="ram:AccountName" />
				</xsl:call-template>
				
				<xsl:apply-templates select="../ram:PayeeSpecifiedCreditorFinancialInstitution" />
		
		</cac:PayeeFinancialAccount>
	
	</xsl:template>
	
	<xsl:template match="ram:PayeeSpecifiedCreditorFinancialInstitution">
		<cac:FinancialInstitutionBranch>
		
					<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">ID</xsl:with-param>
					<xsl:with-param name="node" select="ram:BICID" />
				</xsl:call-template>
	
		</cac:FinancialInstitutionBranch>
	
	
	</xsl:template>
	
	<xsl:template match="ram:ShipToTradeParty">
		<cac:Delivery>

			<xsl:call-template name="cbcDate">
				<xsl:with-param name="name">ActualDeliveryDate</xsl:with-param>
				<xsl:with-param name="node" select="//rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeDelivery/ram:ActualDeliverySupplyChainEvent/ram:OccurrenceDateTime/udt:DateTimeString" />
			</xsl:call-template>
			
			<cac:DeliveryLocation>
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">ID</xsl:with-param>
					<xsl:with-param name="node" select="ram:GlobalID" />
					<xsl:with-param name="scheme" select="ram:GlobalID/@schemeID" />
				</xsl:call-template>
				
				<xsl:apply-templates select="ram:PostalTradeAddress" mode="Address"/>
			
			</cac:DeliveryLocation>
			
			<!--<xsl:apply-templates select="//ram:DeliveryTradeParty" />-->
		
		
		</cac:Delivery>
	
	
	</xsl:template>
	
	
	<xsl:template match="ram:PostalTradeAddress" mode="Address">
		<cac:Address>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">StreetName</xsl:with-param>
				<xsl:with-param name="node" select="ram:LineOne" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">AdditionalStreetName</xsl:with-param>
				<xsl:with-param name="node" select="ram:LineTwo" />
			</xsl:call-template>

		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">CityName</xsl:with-param>
				<xsl:with-param name="node" select="ram:CityName" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">PostalZone</xsl:with-param>
				<xsl:with-param name="node" select="ram:PostcodeCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">CountrySubentity</xsl:with-param>
				<xsl:with-param name="node" select="ram:CountrySubDivisionName" />
			</xsl:call-template>
			
						
			<xsl:apply-templates select="ram:LineThree" />
			
			<xsl:apply-templates select="ram:CountryID" />
		
		
		</cac:Address>
	
	</xsl:template>
	
	
	<xsl:template match="ram:DeliveryTradeParty" >
		<cac:DeliveryParty>
		
		
		</cac:DeliveryParty>
	
	
	</xsl:template>
	
	<xsl:template match="ram:SellerTaxRepresentativeTradeParty">
		<cac:TaxRepresentativeParty>
	
				
				<xsl:apply-templates select="ram:Name" mode="PartyName" />
				<xsl:apply-templates select="ram:SpecifiedLegalOrganization/ram:TradingBusinessName" mode="PartyName" />
	
				<xsl:apply-templates select="ram:PostalTradeAddress" />
			
				<xsl:apply-templates select="ram:SpecifiedTaxRegistration[ram:ID/@schemeID='VAT']" />
			
				

		
		</cac:TaxRepresentativeParty>
	</xsl:template>
	


	
	<xsl:template match="ram:PayeeTradeParty">
		<cac:PayeeParty>
						
				<xsl:apply-templates select="ram:ID" mode="PartyIdentification" />
				
				<xsl:apply-templates select="ram:Name" mode="PartyName" />
				
				
				<cac:PartyLegalEntity>
				
					<xsl:call-template name="cbcNode">
						<xsl:with-param name="name">RegistrationName</xsl:with-param>
						<xsl:with-param name="node" select="//ram:PayeeTradeParty/ram:SpecifiedLegalOrganization/ram:TradingBusinessName" />
					</xsl:call-template>
		
					<xsl:call-template name="cbcNode">
						<xsl:with-param name="name">CompanyID</xsl:with-param>
						<xsl:with-param name="node" select="//ram:PayeeTradeParty/ram:SpecifiedLegalOrganization/ram:ID" />
						<xsl:with-param name="scheme" select="//ram:PayeeTradeParty/ram:SpecifiedLegalOrganization/ram:ID/@schemeID" />
					</xsl:call-template>
		
					<xsl:call-template name="cbcNode">
						<xsl:with-param name="name">CompanyLegalForm</xsl:with-param>
						<xsl:with-param name="node" select="//ram:PayeeTradeParty/ram:Description" />
					</xsl:call-template>
				
				</cac:PartyLegalEntity>
				
	
		</cac:PayeeParty>
	</xsl:template>
	

	
	<xsl:template match="ram:BuyerTradeParty">
		<cac:AccountingCustomerParty>
			<cac:Party>
				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">EndpointID</xsl:with-param>
					<xsl:with-param name="node" select="ram:URIUniversalCommunication/ram:URIID" />
					<xsl:with-param name="scheme" select="ram:URIUniversalCommunication/ram:URIID/@schemeID" />
				</xsl:call-template>
								
			
				<xsl:apply-templates select="ram:ID" mode="PartyIdentification" />
				
				<xsl:apply-templates select="ram:Name" mode="PartyName" />
				
				<xsl:apply-templates select="ram:PostalTradeAddress"  />
				
				<xsl:apply-templates select="ram:SpecifiedTaxRegistration[ram:ID/@schemeID='VAT']" />
				
				<cac:PartyLegalEntity>
				
					<xsl:call-template name="cbcNode">
						<xsl:with-param name="name">RegistrationName</xsl:with-param>
						<xsl:with-param name="node" select="//ram:BuyerTradeParty/ram:SpecifiedLegalOrganization/ram:TradingBusinessName" />
					</xsl:call-template>
		
					<xsl:call-template name="cbcNode">
						<xsl:with-param name="name">CompanyID</xsl:with-param>
						<xsl:with-param name="node" select="//ram:BuyerTradeParty/ram:SpecifiedLegalOrganization/ram:ID" />
						<xsl:with-param name="scheme" select="//ram:BuyerTradeParty/ram:SpecifiedLegalOrganization/ram:ID/@schemeID" />
					</xsl:call-template>
		
					<xsl:call-template name="cbcNode">
						<xsl:with-param name="name">CompanyLegalForm</xsl:with-param>
						<xsl:with-param name="node" select="//ram:BuyerTradeParty/ram:Description" />
					</xsl:call-template>
				
				</cac:PartyLegalEntity>
				
				<xsl:apply-templates select="ram:DefinedTradeContact" />
			
			</cac:Party>
		
		</cac:AccountingCustomerParty>
	</xsl:template>
	
	
	<xsl:template match="ram:SellerTradeParty">
		<cac:AccountingSupplierParty>
			<cac:Party>
				<xsl:choose>
					<xsl:when test="ram:GlobalID">
						<xsl:call-template name="cbcNode">
							<xsl:with-param name="name">EndpointID</xsl:with-param>
							<xsl:with-param name="node" select="ram:GlobalID" />
							<xsl:with-param name="scheme" select="ram:GlobalID/@schemeID" />
						</xsl:call-template>
								
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="cbcNode">
							<xsl:with-param name="name">EndpointID</xsl:with-param>
							<xsl:with-param name="node" select="ram:ID" />
							<xsl:with-param name="scheme" select="ram:ID/@schemeID" />
						</xsl:call-template>
					
					</xsl:otherwise>
				</xsl:choose>
				
				<xsl:apply-templates select="ram:ID" mode="PartyIdentification" />
				
				<xsl:apply-templates select="ram:Name" mode="PartyName" />
				
				<xsl:apply-templates select="ram:PostalTradeAddress" />
				
				<xsl:apply-templates select="ram:SpecifiedTaxRegistration[ram:ID/@schemeID='VAT']" />
				
				<cac:PartyLegalEntity>
				
					<xsl:call-template name="cbcNode">
						<xsl:with-param name="name">RegistrationName</xsl:with-param>
						<xsl:with-param name="node" select="//ram:SellerTradeParty/ram:SpecifiedLegalOrganization/ram:TradingBusinessName" />
					</xsl:call-template>
		
					<xsl:call-template name="cbcNode">
						<xsl:with-param name="name">CompanyID</xsl:with-param>
						<xsl:with-param name="node" select="//ram:SellerTradeParty/ram:SpecifiedLegalOrganization/ram:ID" />
						<xsl:with-param name="scheme" select="//ram:SellerTradeParty/ram:SpecifiedLegalOrganization/ram:ID/@schemeID" />
					</xsl:call-template>
		
					<xsl:call-template name="cbcNode">
						<xsl:with-param name="name">CompanyLegalForm</xsl:with-param>
						<xsl:with-param name="node" select="//ram:SellerTradeParty/ram:Description" />
					</xsl:call-template>
				
				</cac:PartyLegalEntity>
				
				<xsl:apply-templates select="ram:DefinedTradeContact" />
			
			</cac:Party>
		
		</cac:AccountingSupplierParty>
	
	</xsl:template>
	
	<xsl:template match="ram:DefinedTradeContact">
		<cac:Contact>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">Name</xsl:with-param>
				<xsl:with-param name="node" select="ram:PersonName" />
			</xsl:call-template>

			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">Telephone</xsl:with-param>
				<xsl:with-param name="node" select="ram:TelephoneUniversalCommunication/ram:CompleteNumber" />
			</xsl:call-template>

			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ElectronicMail</xsl:with-param>
				<xsl:with-param name="node" select="ram:EmailURIUniversalCommunication/ram:URIID" />
			</xsl:call-template>
		
		</cac:Contact>	
	</xsl:template>
	
	<xsl:template match="ram:SpecifiedTaxRegistration">
		<cac:PartyTaxScheme>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">CompanyID</xsl:with-param>
				<xsl:with-param name="node" select="ram:ID" />
			</xsl:call-template>
			
			<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
			
		</cac:PartyTaxScheme>
	
	</xsl:template>

	
	<xsl:template match="ram:PostalTradeAddress">
		<cac:PostalAddress>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">StreetName</xsl:with-param>
				<xsl:with-param name="node" select="ram:LineOne" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">AdditionalStreetName</xsl:with-param>
				<xsl:with-param name="node" select="ram:LineTwo" />
			</xsl:call-template>

		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">CityName</xsl:with-param>
				<xsl:with-param name="node" select="ram:CityName" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">PostalZone</xsl:with-param>
				<xsl:with-param name="node" select="ram:PostcodeCode" />
			</xsl:call-template>
		
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">CountrySubentity</xsl:with-param>
				<xsl:with-param name="node" select="ram:CountrySubDivisionName" />
			</xsl:call-template>
			
						
			<xsl:apply-templates select="ram:LineThree" />
			
			<xsl:apply-templates select="ram:CountryID" />
		
		
		</cac:PostalAddress>
	
	</xsl:template>
	
	
	<xsl:template match="ram:CountryID">
		<cac:Country>
			<cbc:IdentificationCode><xsl:value-of select="." /></cbc:IdentificationCode>
		</cac:Country>
	
	</xsl:template>
	
	<xsl:template match="ram:LineThree">
		<cac:AddressLine>
			<cbc:Line><xsl:value-of select="." /></cbc:Line>
		</cac:AddressLine>
	</xsl:template>
	
	<xsl:template match="ram:Name | ram:TradingBusinessName" mode="PartyName">
		<cac:PartyName>
			<cbc:Name><xsl:value-of select="." /></cbc:Name>
		</cac:PartyName>
	
	</xsl:template>
	
	<xsl:template match="ram:ID"  mode="PartyIdentification"  > 
		<cac:PartyIdentification>
			<cbc:ID>
				<xsl:choose>
					<xsl:when test="@schemeID"><xsl:value-of select="concat(@schemeID, ':', .)"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="concat('9934:', .)"/></xsl:otherwise> <!--TODO: Vidjeti da li stavljati 9934:-->
				</xsl:choose>
			</cbc:ID>
		</cac:PartyIdentification>
	
	</xsl:template>
	
	<xsl:template match="ram:SpecifiedProcuringProject">
		<cac:ProjectReference>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="ram:ID" />
			</xsl:call-template>
		
		</cac:ProjectReference>
	
	</xsl:template>
	
	<xsl:template match="ram:AdditionalReferencedDocument" mode="AdditionalDocumentReference">
		<cac:AdditionalDocumentReference>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="ram:IssuerAssignedID" />
				<xsl:with-param name="scheme" select="ram:ReferenceTypeCode" />
			</xsl:call-template>
			
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">DocumentDescription</xsl:with-param>
				<xsl:with-param name="node" select="ram:Name" />
			</xsl:call-template>
		
			<xsl:apply-templates select="ram:AttachmentBinaryObject" />
			<xsl:apply-templates select="ram:URIID" />
		
		</cac:AdditionalDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="ram:URIID" >
		<cac:Attachment>
			<cac:ExternalReference>
				<cbc:URI><xsl:value-of select="." /></cbc:URI>
			</cac:ExternalReference>
		</cac:Attachment>
	
	</xsl:template>
	
	<xsl:template match="ram:AttachmentBinaryObject">
		<cac:Attachment>
			<cbc:EmbeddedDocumentBinaryObject>
				<xsl:attribute name="mimeCode"><xsl:value-of select="@mimeCode" /></xsl:attribute>
				<xsl:attribute name="filename"><xsl:value-of select="@filename" /></xsl:attribute>
				<xsl:value-of select="." />
			</cbc:EmbeddedDocumentBinaryObject>
		
		</cac:Attachment>
	
	</xsl:template>

	<xsl:template match="ram:ContractReferencedDocument">
		<cac:ContractDocumentReference>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="ram:IssuerAssignedID" />
			</xsl:call-template>
		
		</cac:ContractDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="ram:AdditionalReferencedDocument" mode="OriginatorDocumentReference">
		<cac:OriginatorDocumentReference>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="ram:IssuerAssignedID" />
			</xsl:call-template>
		
		</cac:OriginatorDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="ram:ReceivingAdviceReferencedDocument">
		<cac:ReceiptDocumentReference>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="ram:IssuerAssignedID" />
			</xsl:call-template>
		
		</cac:ReceiptDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="ram:DespatchAdviceReferencedDocument">
		<cac:DespatchDocumentReference>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="ram:IssuerAssignedID" />
			</xsl:call-template>
	
		</cac:DespatchDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="ram:InvoiceReferencedDocument">
		<cac:BillingReference>
			<cac:InvoiceDocumentReference>

				<xsl:call-template name="cbcNode">
					<xsl:with-param name="name">ID</xsl:with-param>
					<xsl:with-param name="node" select="ram:IssuerAssignedID" />
				</xsl:call-template>
				
				<xsl:call-template name="cbcDate">
					<xsl:with-param name="name">IssueDate</xsl:with-param>
					<xsl:with-param name="node" select="ram:FormattedIssueDateTime/qdt:DateTimeString" />
				</xsl:call-template>
			
			
			</cac:InvoiceDocumentReference>
		
		</cac:BillingReference>
	
	</xsl:template>
	
	<xsl:template match="ram:BuyerOrderReferencedDocument">
		<cac:OrderReference>
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">ID</xsl:with-param>
				<xsl:with-param name="node" select="ram:IssuerAssignedID" />
			</xsl:call-template>
			
			<xsl:call-template name="cbcNode">
				<xsl:with-param name="name">SalesOrderID</xsl:with-param>
				<xsl:with-param name="node" select="//rsm:SupplyChainTradeTransaction/ram:ApplicableHeaderTradeAgreement/ram:SellerOrderReferencedDocument/ram:IssuerAssignedID" />
			</xsl:call-template>
		</cac:OrderReference>
		
	</xsl:template>
	
	<xsl:template match="ram:BillingSpecifiedPeriod">
		<cac:InvoicePeriod>
			<xsl:call-template name="cbcDate">
				<xsl:with-param name="name">StartDate</xsl:with-param>
				<xsl:with-param name="node" select="ram:StartDateTime/udt:DateTimeString" />
			</xsl:call-template>
			
			<xsl:call-template name="cbcDate">
				<xsl:with-param name="name">EndDate</xsl:with-param>
				<xsl:with-param name="node" select="ram:EndDateTime/udt:DateTimeString" />
			</xsl:call-template>
			
		
		</cac:InvoicePeriod>
	
	</xsl:template>
	
	<xsl:template match="ram:TaxPointDate">
		<cbc:TaxPointDate>
			<xsl:apply-templates select="udt:DateString" mode="Date"/>	
		
		</cbc:TaxPointDate>
	
	</xsl:template>
	
	<xsl:template match="ram:DueDateDateTime">
		<cbc:DueDate>
			<xsl:apply-templates select="udt:DateTimeString" mode="Date"/>	
		</cbc:DueDate>
	</xsl:template>	
	
	<xsl:template match="ram:IssueDateTime">
		<cbc:IssueDate>
			<xsl:apply-templates select="udt:DateTimeString" mode="Date" />
		</cbc:IssueDate>
	
	</xsl:template>
	
	<xsl:template match="ram:BusinessProcessSpecifiedDocumentContextParameter">
		<cbc:ProfileID><xsl:value-of select="ram:ID" /></cbc:ProfileID>
	</xsl:template>
	

	
	
	<xsl:template match="cbc:Amount | cbc:BaseAmount | cbc:TaxAmount | cbc:TaxableAmount
		| cbc:LineExtensionAmount | cbc:TaxExclusiveAmount | cbc:TaxInclusiveAmount
		| cbc:AllowanceTotalAmount | cbc:ChargeTotalAmount | cbc:PrepaidAmount
		| cbc:PayableRoundingAmount | cbc:PayableAmount " >
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:attribute name="currencyID" namespace="">
				<xsl:value-of select="@currencyID" />
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="number(.) = 0">0.00</xsl:when>
				<xsl:when test="string(number(.))='NaN'">0.00</xsl:when>
				<xsl:otherwise><xsl:value-of select="format-number(number(.), '#.00')" /></xsl:otherwise>
			</xsl:choose>
			
		</xsl: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:EndpointID" mode="SchemeID">
		<cbc:EndpointID>
			<xsl:choose>
				<xsl:when test="@schemeID and (normalize-space(@schemeID) != '')">
					<xsl:attribute name="schemeID" namespace="">
						<xsl:choose>
							<xsl:when test="@schemeID = 'GLN'">0088</xsl:when>
							<xsl:otherwise><xsl:value-of select="@schemeID" /></xsl:otherwise>
						</xsl:choose>
						
					</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" mode="TaxCategoryID">
		<cbc:ID>
			<xsl:choose>
				<xsl:when test="normalize-space(.) = 'O'">
					<xsl:choose>
						<xsl:when test="$isOutsideVAT = 'true'">O</xsl:when>
						<xsl:otherwise>E</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise><xsl:value-of select="normalize-space(.)" /></xsl:otherwise>
			</xsl:choose>
		
		</cbc:ID>
	
	</xsl:template>
	
	<xsl:template match="cbc:ID | cbc:CompanyID " mode="SchemeID" >
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:apply-templates select="@schemeID" />

			<xsl:value-of select="." />
		</xsl:element>
	</xsl:template>
	
	<xsl:template match="@schemeID" >
		<xsl:attribute name="schemeID" namespace="">
			<xsl:value-of select="." />
		</xsl:attribute>
	
	</xsl:template>
	
	<xsl:template match="ram: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="udt:DateTimeString | udt:DateString" mode="Date">
	
		<xsl:variable name="year"><xsl:value-of select="substring(., 1, 4)" /> </xsl:variable>
		<xsl:variable name="month"><xsl:value-of select="substring(., 5, 2)" /></xsl:variable>
		<xsl:variable name="day"><xsl:value-of select="substring(., 7, 2)" /></xsl:variable>
		
		<xsl:value-of select="concat($year, '-', $month, '-', $day)" />

	</xsl:template>
	
		<xsl:template name="cbcAmount">
		<xsl:param name="name" />
		<xsl:param name="node" />
		<xsl:param name="currency" />
		
		<xsl:if test="normalize-space($node) != ''">
			<xsl:element name="{concat('cbc:', $name)}" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
				<xsl:attribute name="currencyID">
					<xsl:value-of select="$currency" />
				</xsl:attribute>
			
				<xsl:value-of select="$node" />
			</xsl:element>
		</xsl:if>
	</xsl:template>


	<xsl:template name="cbcDate">
		<xsl:param name="name" />
		<xsl:param name="node" />
		
		<xsl:if test="normalize-space($node) != ''">
			<xsl:choose>
				<xsl:when test="string-length($node) = 8">
					<xsl:element name="{concat('cbc:', $name)}" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
						<xsl:variable name="year"><xsl:value-of select="substring($node, 1, 4)" /> </xsl:variable>
						<xsl:variable name="month"><xsl:value-of select="substring($node, 5, 2)" /></xsl:variable>
						<xsl:variable name="day"><xsl:value-of select="substring($node, 7, 2)" /></xsl:variable>
						
						<xsl:value-of select="concat($year, '-', $month, '-', $day)" />
					</xsl:element>
				</xsl:when>
				<xsl:when test="string-length($node) = 10">
					<xsl:element name="{concat('cbc:', $name)}" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
												
						<xsl:value-of select="$node" />
					</xsl:element>
				</xsl:when>
				
			</xsl:choose>
		</xsl:if>
	</xsl:template>

	<xsl:template match="ram:*" >
<!--		<xsl:copy>
			<xsl:value-of select="." />
		</xsl:copy>
-->	
		<xsl:element name="{concat('cbc:', local-name(.))}" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
			<xsl:value-of select="." />
		</xsl:element>
		

	</xsl:template>
	
	<xsl:template name="cbcNode">
		<xsl:param name="name" />
		<xsl:param name="node" />
		<xsl:param name="scheme" />
		
		<xsl:if test="normalize-space($node) != ''">
			<xsl:element name="{concat('cbc:', $name)}" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
				<xsl:if test="normalize-space($scheme) != ''">
					<xsl:attribute name="schemeID">
						<xsl:value-of select="$scheme" />
					</xsl:attribute>
				
				</xsl:if>
			
				<xsl:value-of select="$node" />
			</xsl:element>
		</xsl:if>
	</xsl:template>

	<xsl:template name="cbcQuantity">
		<xsl:param name="name" />
		<xsl:param name="node" />
		<xsl:param name="unit" />
		
		<xsl:if test="normalize-space($node) != ''">
			<xsl:element name="{concat('cbc:', $name)}" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
				<xsl:if test="normalize-space($unit) != ''">
					<xsl:attribute name="unitCode">
						<xsl:value-of select="$unit" />
					</xsl:attribute>
				
				</xsl:if>
			
				<xsl:value-of select="$node" />
			</xsl:element>
		</xsl:if>
	</xsl:template>

</xsl:stylesheet>
