<?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: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 xsi xsl o ns1 a" 
 >
 
<!-- V2025-03-17-v3
Zamijenjeni podaci kupca i dobavljača u Headeru

-->

<!-- 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:param name="supplierCompanyId"/>
	<xsl:param name="buyerCompanyId"/>
	<xsl:param name="supplierEmail"/>
	<xsl:param name="buyerEmail"/>	
	

	
	<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="documentCurrency">
		<xsl:choose>
			<xsl:when test="//@currencyID[normalize-space(.) != '']"><xsl:value-of select="(//@currencyID[normalize-space(.) != ''])[1]" /></xsl:when>
			<xsl:otherwise>EUR</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" mode="Header"/>
		<xsl:comment>
			Generated by Moj eRačun KALAOrder2MeROutgoingDocumentsDataOrder-2025-02-25-v1
		</xsl:comment>		
	</xsl:template>

	<xsl:template match="o:Order" mode="Header">
		<OutgoingDocumentsData xmlns="http://ubl.moj-eracun.hr/v1.2/xsd">
			<Header xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
				<Sender>
					<!--OIB kupca-->
					<cbc:CompanyID><xsl:value-of select="$buyerCompanyId" /></cbc:CompanyID>
					<!--GLN kupca-->
					<cbc:EndpointID schemeID="0088"><xsl:value-of select="$buyerGLN" /></cbc:EndpointID>
					<!--Email kupca-->
					<cbc:ElectronicMail><xsl:value-of select="$buyerEmail" /></cbc:ElectronicMail>
				</Sender>
				<Recipient>
					<!--OIB dobavljača-->
					<cbc:CompanyID><xsl:value-of select="$supplierCompanyId" /></cbc:CompanyID>
					<!--GLN dobavljača-->
					<cbc:EndpointID schemeID="0088"><xsl:value-of select="$supplierGLN" /></cbc:EndpointID>
					<!--Email dobavljača-->
					<cbc:ElectronicMail><xsl:value-of select="$supplierEmail" /></cbc:ElectronicMail>
				</Recipient>
				<DocumentWorkflow>
					<!--Vrsta dokumenta,
					105 = Narudžba
					226 = opoziv narudžbe
					230 = Promjena narudžbe
					231 = Potvrda narudžbe
					-->
					<DocumentType><xsl:value-of select="$documentType" /></DocumentType>
					<SignDocument>1</SignDocument>
					<!--Broj dokumenta-->
					<DocumentID><xsl:value-of select="/o:Order/cbc:ID" /></DocumentID>
					<ResponseRequired>true</ResponseRequired>
				</DocumentWorkflow>
			</Header>
			<OrderDocument>
				<OrderEnvelope>			
					<xsl:apply-templates select="/o:Order" />
				</OrderEnvelope>
			</OrderDocument>
		</OutgoingDocumentsData>

	</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-->
			<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
			
			<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>
			<cbc:DocumentCurrencyCode><xsl:value-of select="$documentCurrency" /></cbc:DocumentCurrencyCode>
			
			<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:StandardItemIdentification" />
			
		</cac:Item>
	
	</xsl:template>
	
	<xsl:template match="cac:StandardItemIdentification">
		<cac:AdditionalItemIdentification>
			<xsl:apply-templates select="cbc:ID" mode="SchemeID"/>
		
		</cac:AdditionalItemIdentification>
	
	</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:apply-templates select="cac:DeliveryParty/cac:PartyName" />
			</cac:DeliveryParty>
		
		</cac:Delivery>
	
	
	</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:PartyLegalEntity>
					<cbc:RegistrationName><xsl:value-of select="cac:Party/cac:PartyName/cbc:Name" /> </cbc:RegistrationName>
					<cbc:CompanyID><xsl:value-of select="$supplierCompanyId" /></cbc:CompanyID>
				</cac:PartyLegalEntity>
			</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:PartyLegalEntity>
					<cbc:RegistrationName><xsl:value-of select="cac:Party/cac:PartyName/cbc:Name" /> </cbc:RegistrationName>
					<cbc:CompanyID><xsl:value-of select="$buyerCompanyId" /></cbc:CompanyID>
				</cac:PartyLegalEntity>
			</cac:Party>
		
		</cac:BuyerCustomerParty>
	
	</xsl:template>
	
	<xsl:template match="cac:PostalAddress">
		<xsl:copy-of select="."  copy-namespaces="no"/>
	
	</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>
