<?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="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:o="urn:oasis:names:specification:ubl:schema:xsd:Order-2"
 xmlns:mer="http://ubl.moj-eracun.hr/v1.2/xsd"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" 
 xmlns:n2="http://uri.etsi.org/01903/v1.4.1#" 
 xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
 xmlns:xs="http://www.w3.org/2001/XMLSchema" 
 xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:AttachedDocument-2" 
 xmlns:a="urn:oasis:names:specification:ubl:schema:xsd:AttachedDocument-2" 
 xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
 xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
 xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
 xmlns:n0="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" 
 xmlns:qdt2="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2" 
 xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" 
 xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" 
 xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" 
 xmlns:udt2="urn:oasis:names:specification:ubl:schema:xsd:UnqualifiedDataTypes-2" 
 xmlns:clm54217="urn:un:unece:uncefact:codelist:specification:54217:2001" 
 xmlns:clm5639="urn:un:unece:uncefact:codelist:specification:5639:1988" 
 xmlns:clm66411="urn:un:unece:uncefact:codelist:specification:66411:2001" 
 xmlns:clmIANAMIMEMediaType="urn:un:unece:uncefact:codelist:specification:IANAMIMEMediaType:2003" 
 xmlns:ccts-cct="urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2" 
 xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" 
 exclude-result-prefixes="ccts-cct ds n0 n2 qdt2 sac sbc udt2 xades xs xsd xsi xsl o ns1 a mer" 
 >

<!-- V2025-03-17-v3
 - Delivery/DeliveryParty/PartyName/Name - ukoliko nema ove putanje u našem XML-u  gleda sljedeću putanju Delivery/DeliveryParty/PartyLegalEntity/RegistrationName
 - Ispravljeno OrderLine/Item/Name 
 - Ispravljeno cbc:OrderTypeCode
 - Prenosi cbc:UblVersionID umjesto postavljanja 2.1 kao default
-->

<!-- Izbačen default namespace - da ga ne dodaje bez veze
 xmlns="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2"
-->
	
	
	<!--<xsl:namespace-alias stylesheet-prefix="n" result-prefix="#default"/> -->
	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	
	<xsl:variable name="supplierCompanyId" >
		<xsl:value-of select="/mer:OutogingDocumentsData/mer:Header/mer:Sender/cbc:CompanyID" />	
	
	</xsl:variable>
	<xsl:variable name="buyerCompanyId" >
		<xsl:value-of select="/mer:OutogingDocumentsData/mer:Header/mer:Recipient/cbc:CompanyID" />	
	</xsl:variable>
	<xsl:variable name="supplierEmail" >
		<xsl:value-of select="/mer:OutogingDocumentsData/mer:Header/mer:Sender/cbc:ElectroincMail" />
	</xsl:variable>
	<xsl:variable name="buyerEmail">
		<xsl:value-of select="/mer:OutogingDocumentsData/mer:Header/mer:Recipient/cbc:ElectroincMail" />
	</xsl:variable>
	
	<xsl:variable name="documentType">
		<xsl:choose>
			<xsl:when test="normalize-space(//o:Order/cbc:OrderTypeCode) != ''">
				<xsl:value-of select="normalize-space(//o:Order/cbc:OrderTypeCode)"/>
			</xsl:when>
			<xsl:otherwise>105</xsl:otherwise>
		</xsl:choose>
	
	</xsl:variable>
	
	<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />
	<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

<!--	<xsl:variable name="countryPrefixList">
		<xsl:copy-of select="document('codelist/CountryPrefixList.xml')"/>
	</xsl:variable>
	<xsl:variable name="schemeIDList" >
		<xsl:copy-of select="document('codelist/SchemeIDCodeList.xml')"/>
	</xsl:variable>
	
	<xsl:variable name="supplierCountryPrefix">
		<xsl:choose>
			<xsl:when test="normalize-space(//cac:SellerSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode) != ''">
				<xsl:value-of select="translate(//cac:SellerSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode,$uppercase,$lowercase)"/>
			</xsl:when>
			<xsl:when test="normalize-space(substring(//cac:SellerSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2))!= ''">
				<xsl:value-of select="translate(substring(//cac:SellerSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2),$uppercase,$lowercase)"/>
			</xsl:when>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="buyerCountryPrefix">
		<xsl:choose>
			<xsl:when test="normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode) != ''">
				<xsl:value-of select="translate(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode,$uppercase,$lowercase)"/>
			</xsl:when>
			<xsl:when test="normalize-space(substring(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2))!= ''">
				<xsl:value-of select="translate(substring(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2),$uppercase,$lowercase)"/>
			</xsl:when>
		</xsl:choose>
	</xsl:variable>
	
	
	<xsl:variable name="supplierCountrySchemeID">
				<xsl:choose>
					<xsl:when test="$schemeIDList//SchemeID[@ID= $supplierCountryPrefix] != ''  ">
						<xsl:value-of select="$schemeIDList//SchemeID[@ID=$supplierCountryPrefix]"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$schemeIDList//SchemeID[@ID='hr']"/>
					</xsl:otherwise>
				</xsl:choose>
	</xsl:variable>
	
	<xsl:variable name="customerCountrySchemeID">
		<xsl:choose>
			<xsl:when test="$schemeIDList//SchemeID[@ID= $buyerCountryPrefix] != ''  ">
				<xsl:value-of select="$schemeIDList//SchemeID[@ID=$buyerCountryPrefix]"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$schemeIDList//SchemeID[@ID='hr']"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
-->
	<xsl:variable name="supplierGLN">
		<xsl:choose>
			<xsl:when test="normalize-space(//cac:SellerSupplierParty/cac:Party/cbc:EndpointID[@schemeID='0088']) != ''">
				<xsl:value-of select="normalize-space(//cac:SellerSupplierParty/cac:Party/cbc:EndpointID[@schemeID='0088'])"/>
			</xsl:when>
			<xsl:when test="string-length(normalize-space(//cac:SellerSupplierParty/cac:Party/cbc:EndpointID)) = 13 ">
				<xsl:value-of select="normalize-space(//cac:SellerSupplierParty/cac:Party/cbc:EndpointID)" />
			</xsl:when>
			<xsl:otherwise><xsl:value-of select="//cac:SellerSupplierParty/cac:Party/cbc:EndpointID" /></xsl:otherwise>
		</xsl:choose>
	
	</xsl:variable>

	<xsl:variable name="buyerGLN">
		<xsl:choose>
			<xsl:when test="normalize-space(//cac:BuyerCustomerParty/cac:Party/cbc:EndpointID[@schemeID='0088']) != ''">
				<xsl:value-of select="normalize-space(//cac:BuyerCustomerParty//cac:Party/cbc:EndpointID[@schemeID='0088'])"/>
			</xsl:when>
			<xsl:when test="string-length(normalize-space(//cac:BuyerCustomerParty//cac:Party/cbc:EndpointID)) = 13 ">
				<xsl:value-of select="normalize-space(//cac:BuyerCustomerParty//cac:Party/cbc:EndpointID)" />
			</xsl:when>
			<xsl:otherwise><xsl:value-of select="//cac:BuyerCustomerParty//cac:Party/cbc:EndpointID" /></xsl:otherwise>
		</xsl:choose>
	
	</xsl:variable>


	<xsl:template match="/">
		<xsl:apply-templates select="//o:Order" />
		<xsl:comment>
			Generated by Moj eRačun MeROutgoingDocumentsDataOrder2KALAOrder-2025-03-04-v2
		</xsl:comment>		
	</xsl:template>

	
	<xsl:template match="o:Order">
		<Order xmlns="urn:oasis:names:specification:ubl:schema:xsd:Order-2" 
			xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
			xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
			xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
			>
			
			<!--default-->
			<xsl:apply-templates select="cbc:UBLVersionID" />
			
			<!--<xsl:copy-of select="." />-->
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="cbc:IssueDate" />
			<xsl:apply-templates select="cbc:IssueTime" />
			
			<cbc:OrderTypeCode><xsl:value-of select="$documentType"/></cbc:OrderTypeCode>
			
			<xsl:apply-templates select="cac:BuyerCustomerParty" />
			<xsl:apply-templates select="cac:SellerSupplierParty" />
			
			<xsl:apply-templates select="cac:Delivery" />
			
			<xsl:apply-templates select="cac:OrderLine/cac:LineItem" />

		</Order>
	
	</xsl:template>
	
	<xsl:template match="cac:LineItem" >
		<cac:OrderLine>
			<cac:LineItem>
				<xsl:apply-templates select="cbc:ID" />
				<xsl:apply-templates select="cbc:Quantity" />
				
				<xsl:apply-templates select="cac:Price" />
				
				<xsl:apply-templates select="cac:Item" />
			
			</cac:LineItem>
			
		
		</cac:OrderLine>
	
	
	</xsl:template>
	
	<xsl:template match="cac:Item">
		<cac:Item>
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cac:BuyersItemIdentification" />
			<xsl:apply-templates select="cac:AdditionalItemIdentification" />
			
		</cac:Item>
	
	</xsl:template>
	
	<xsl:template match="cac:AdditionalItemIdentification">
		<cac:StandardItemIdentification>
			<xsl:apply-templates select="cbc:ID" mode="SchemeID"/>
		
		</cac:StandardItemIdentification>
	
	</xsl:template>

	<xsl:template match="cac:BuyersItemIdentification">
		<cac:BuyersItemIdentification>
			<xsl:apply-templates select="cbc:ID" />
		
		</cac:BuyersItemIdentification>
	
	</xsl:template>
	
	<xsl:template match="cac:Price">
		<cac:Price>
			<xsl:apply-templates select="cbc:PriceAmount" />
			<xsl:apply-templates select="cbc:BaseQuantity" />
		
		</cac:Price>
	
	
	</xsl:template>
	
	<xsl:template match="cac:Delivery">	
		<cac:Delivery>
			<xsl:apply-templates select="cbc:ActualDeliveryDate" />
			<cac:DeliveryParty>
				<xsl:apply-templates select="cac:DeliveryParty/cbc:EndpointID" mode="SchemeID"/>
				<xsl:choose>
					<xsl:when test="cac:DeliveryParty/cac:PartyName and normalize-space(cac:DeliveryParty/cac:PartyName) != ''">
						<xsl:apply-templates select="cac:DeliveryParty/cac:PartyName" />				
					</xsl:when>
					<xsl:otherwise>
						<xsl:apply-templates select="cac:DeliveryParty/cac:PartyLegalEntity/cbc:RegistrationName" mode="PartyName"/>				
					
					</xsl:otherwise>
				
				</xsl:choose>
			</cac:DeliveryParty>
		
		</cac:Delivery>
	
	
	</xsl:template>
	
	<xsl:template match="cbc:RegistrationName" mode="PartyName">
	
	
	</xsl:template>

	<xsl:template match="cac:SellerSupplierParty">
		<cac:SellerSupplierParty>
			<cac:Party>
				<xsl:apply-templates select="cac:Party/cbc:EndpointID" mode="SchemeID"/>
				<xsl:apply-templates select="cac:Party/cac:PartyName" />
				<xsl:apply-templates select="cac:Party/cac:PostalAddress" />
			</cac:Party>	
		</cac:SellerSupplierParty>
	
	</xsl:template>
	
	<xsl:template match="cac:BuyerCustomerParty">
		<cac:BuyerCustomerParty>
			<cac:Party>
				<xsl:apply-templates select="cac:Party/cbc:EndpointID" mode="SchemeID"/>
				<xsl:apply-templates select="cac:Party/cac:PartyName" />
				<xsl:apply-templates select="cac:Party/cac:PostalAddress" />
				
			</cac:Party>
		
		</cac:BuyerCustomerParty>
	
	</xsl:template>
	
	<xsl:template match="cac:PostalAddress">
		<xsl:copy-of select="."  copy-namespaces="no"/>
	
	</xsl:template>
	
	<xsl:template match="cbc:RegistrationName" mode="PartyName">
		<cac:PartyName>
			<cbc:Name>
				<xsl:value-of select="normalize-space(.)"/> 
			</cbc:Name>
		
		</cac:PartyName>
	
	
	</xsl:template>

	<xsl:template match="cac:PartyName">
		<cac:PartyName>
			<cbc:Name>
				<xsl:value-of select="normalize-space(cbc:Name)"/> 
			</cbc:Name>
		
		</cac:PartyName>
	
	</xsl:template>


	<xsl:template match="cbc:PriceAmount" >
		<cbc:PriceAmount>
			<xsl:attribute name="currencyID" namespace="">
				<xsl:value-of select="@currencyID" />
			</xsl:attribute>
			<xsl:value-of select="." />
		</cbc:PriceAmount>
	</xsl:template>

	<xsl:template match="cbc:Quantity | cbc:BaseQuantity"  >
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:attribute name="unitCode" namespace="">
				<xsl:value-of select="@unitCode" />
			</xsl:attribute>		

			<xsl:value-of select="." />
		</xsl:element>
	</xsl:template>

	<xsl:template match="cbc:ID | cbc:EndpointID" 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:choose>
				<xsl:when test="contains(., 'GLN')">0088</xsl:when>
				<xsl:when test="contains(., 'DUNS')">0060</xsl:when>
				<xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
			</xsl:choose>

		</xsl:attribute>
	
	</xsl:template>

	<xsl:template match="cbc:*" >

		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:value-of select="." />
		</xsl:element>
		

	</xsl:template>
</xsl:stylesheet>
