<?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  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
    xmlns:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
    exclude-result-prefixes="xsl i ns1" 
    xmlns="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2"
    version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    
    <xsl:param name="electronicId"/>
    <xsl:variable name="customerElectronicMail">
        <xsl:choose>
            <xsl:when test="normalize-space(//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>
    </xsl:variable>
    <xsl:variable name="customerCompanyId">
        <xsl:choose>
            
            <xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID) != ''">
                <xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID"/>
            </xsl:when>
            <xsl:otherwise><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID"/></xsl:otherwise>
            
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="supplierElectronicMail">
        <xsl:choose>
            <xsl:when test="normalize-space(//cac:AccountingSupplierParty/cac:AccountingContact/cbc:ElectronicMail) != ''">
                <xsl:value-of select="//cac:AccountingSupplierParty/cac:AccountingContact/cbc:ElectronicMail"/>
            </xsl:when>
            <xsl:otherwise>adresar@moj-eracun.hr</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="supplierCompanyId">
        <xsl:choose>
            
            <xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID) != ''">
                <xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID"/>
            </xsl:when>
            <xsl:otherwise><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID"/></xsl:otherwise>
            
        </xsl:choose>
        
    </xsl:variable>
    
    
    
    
    <xsl:template match="/">
        <xsl:choose>
            <xsl:when test="/OutgoingInvoicesData"><xsl:copy-of select="/OutgoingInvoicesData"/></xsl:when>
            <xsl:otherwise>
                <OutgoingInvoicesData xmlns="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2" >
                    <Header xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
                        <SupplierID><xsl:value-of select="$supplierCompanyId"/></SupplierID>
                        <InvoiceType>1</InvoiceType>
                    </Header>
                  
                    <OutgoingInvoice>
                        <SupplierInvoiceID><xsl:value-of select="//i:Invoice/cbc:ID"/></SupplierInvoiceID>
                        <BuyerID><xsl:value-of select="$customerCompanyId"/></BuyerID>
                        <xsl:if test="normalize-space($electronicId) != ''">
                            <ElectronicID><xsl:value-of select="$electronicId"/></ElectronicID>
                        </xsl:if>
                        <InvoiceEnvelope>
                            <xsl:apply-templates select="//i:Invoice" />
                        </InvoiceEnvelope>
                        <AttachedDocumentEnvelope>
                            <xsl:copy-of select="//ns1:AttachedDocument"/>
                        </AttachedDocumentEnvelope>
                    </OutgoingInvoice>
                </OutgoingInvoicesData>
            </xsl:otherwise>
        </xsl:choose>
        
    </xsl:template>
    <xsl:template match="i:Invoice">
        <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
            xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2"
            xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            
            <xsl:copy-of select="ext:UBLExtensions"/>
            <xsl:copy-of select="cbc:UBLVersionID"/>
            <xsl:copy-of select="cbc:CustomizationID"/>
            <xsl:copy-of select="cbc:ProfileID"/>
            <xsl:copy-of select="cbc:ProfileExecutionID"/>
            <xsl:copy-of select="cbc:ID"/>
            <xsl:copy-of select="cbc:CopyIndicator"/>
            
            <xsl:copy-of select="cbc:UUID"/>
            <xsl:copy-of select="cbc:IssueDate"/>
            <xsl:copy-of select="cbc:IssueTime"/>
            <xsl:copy-of select="cbc:DueDate"/>
            <cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
            <xsl:copy-of select="cbc:Note"/>
            <xsl:copy-of select="cbc:TaxPointDate"/>
            <xsl:copy-of select="cbc:DocumentCurrencyCode"/>
            <xsl:copy-of select="cbc:TaxCurrencyCode"/>
            <xsl:copy-of select="cbc:PricingCurrencyCode"/>
            <xsl:copy-of select="cbc:PaymentCurrencyCode"/>
            <xsl:copy-of select="cbc:PaymentAlternativeCurrencyCode"/>
            <xsl:copy-of select="cbc:AccountingCostCode"/>
            <xsl:copy-of select="cbc:AccountingCost"/>
            <xsl:copy-of select="cbc:LineCountNumeric"/>
            <xsl:copy-of select="cbc:BuyerReference"/>
            <xsl:copy-of select="cac:InvoicePeriod"/>
            <xsl:copy-of select="cac:OrderReference"/>
            <xsl:copy-of select="cac:BillingReference"/>
            <xsl:copy-of select="cac:DespatchDocumentReference"/>
            <xsl:copy-of select="cac:ReceiptDocumentReference"/>
            <xsl:copy-of select="cac:StatementDocumentReference"/>
            <xsl:copy-of select="cac:OriginatorDocumentReference"/>
            <xsl:copy-of select="cac:ContractDocumentReference"/>
            <xsl:copy-of select="cac:AdditionalDocumentReference"/>
            <xsl:copy-of select="cac:ProjectReference"/>
            <xsl:copy-of select="cac:Signature"/>
            <xsl:copy-of select="cac:AccountingSupplierParty"/>
            <xsl:copy-of select="cac:AccountingCustomerParty"/>
            <xsl:copy-of select="cac:PayeeParty"/>
            <xsl:copy-of select="cac:BuyerCustomerParty"/>
            <xsl:copy-of select="cac:SellerSupplierParty"/>
            <xsl:copy-of select="cac:TaxRepresentativeParty"/>
            <xsl:copy-of select="cac:Delivery"/>
            <xsl:copy-of select="cac:DeliveryTerms"/>
            <xsl:copy-of select="cac:PaymentMeans"/>
            <xsl:copy-of select="cac:PaymentTerms"/>
            <xsl:copy-of select="cac:PrepaidPayment"/>
            <xsl:copy-of select="cac:AllowanceCharge"/>
            <xsl:copy-of select="cac:TaxExchangeRate"/>
            <xsl:copy-of select="cac:PricingExchangeRate"/>
            <xsl:copy-of select="cac:PaymentExchangeRate"/>
            <xsl:copy-of select="cac:PaymentAlternativeExchangeRate"/>
            <xsl:copy-of select="cac:TaxTotal"/>
            <xsl:copy-of select="cac:WithholdingTaxTotal"/>
            <xsl:copy-of select="cac:LegalMonetaryTotal"/>
            <xsl:copy-of select="cac:InvoiceLine"/>
            
        </Invoice>
        
    </xsl:template>
    
    
    
</xsl:stylesheet>

