<?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 
  

-->

<!-- V 2024-11-19
Dodao da iz izvornog fajla čita default vrijednosti za parametre
    <xsl:param name="senderStreetName" select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName"/>
    <xsl:param name="senderCityName" select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName"/>
    <xsl:param name="senderPostalZone" select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:PostalZone"/>

-->

<!-- V 2024-11-15
Dodao xslt:output - kao da nije prepoznavao da treba biti xml
	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="1.0"
    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" 

    exclude-result-prefixes="xs ns2"
    >
    <xsl:param name="receiverOrganizationId"/>
    <xsl:param name="senderOrganizationId"/>
    <xsl:param name="receiverVat"/>
    <xsl:param name="senderVat"/>
    <xsl:param name="senderStreetName" select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName"/>
    <xsl:param name="senderCityName" select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName"/>
    <xsl:param name="senderPostalZone" select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:PostalZone"/>
 
 	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
 	   
    <xsl:template match="/">
    
        <xsl:apply-templates select="ns2:Invoice"/>

    </xsl:template>
    
    <xsl:template match="ns2:Invoice">
		
        <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" 
            > 
            <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"/>
            <xsl:copy-of select="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:apply-templates select="cac:AccountingSupplierParty"/>
            <xsl:apply-templates 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:template match="cac:AccountingCustomerParty">
        <cac:AccountingCustomerParty>
            <xsl:copy-of select="cbc:CustomerAssignedAccountID"/>
            <xsl:copy-of select="cbc:AdditionalAccountID"/>
            <xsl:copy-of select="cbc:DataSendingCapability"/>
            <xsl:apply-templates select="cac:Party" mode="Receiver">
                <xsl:with-param name="organizationId" select="$receiverOrganizationId"/>
                <xsl:with-param name="companyId" select="$receiverVat"/>
            </xsl:apply-templates>
            <xsl:copy-of select="cac:DespatchContact"/>
            <xsl:copy-of select="cac:AccountingContact"/>
            <xsl:copy-of select="cac:SellerContact"/>
        </cac:AccountingCustomerParty>
    </xsl:template>
    <xsl:template match="cac:Party" mode="Receiver">
        <xsl:param name="organizationId"/>
        <xsl:param name="companyId"/>
        <cac:Party>
            <xsl:copy-of select="cbc:MarkCareIndicator"/>
            <xsl:copy-of select="cbc:MarkAttentionIndicator"/>
            <xsl:copy-of select="cbc:WebsiteURI"/>
            <xsl:copy-of select="cbc:LogoReferenceID"/>
            <xsl:copy-of select="cbc:EndpointID"/>
            <xsl:copy-of select="cbc:IndustryClassificationCode"/>
            <xsl:call-template name="PartyIdentification">
                <xsl:with-param name="organizationId" select="$organizationId"/>
            </xsl:call-template>
            <xsl:copy-of select="cac:PartyName"/>
            <xsl:copy-of select="cac:Language"/>
            <xsl:copy-of select="cac:PostalAddress"/>
            <xsl:copy-of select="cac:PhysicalLocation"/>
            <xsl:copy-of select="cac:PartyTaxScheme"/>
            <xsl:apply-templates select="cac:PartyLegalEntity">
                <xsl:with-param name="companyId" select="$companyId"/>
            </xsl:apply-templates>
        </cac:Party>
    </xsl:template>
    <xsl:template match="cac:AccountingSupplierParty">
        <cac:AccountingSupplierParty>
            <xsl:copy-of select="cbc:CustomerAssignedAccountID"/>
            <xsl:copy-of select="cbc:AdditionalAccountID"/>
            <xsl:copy-of select="cbc:DataSendingCapability"/>
            <xsl:apply-templates select="cac:Party" mode="Supplier">
                <xsl:with-param name="organizationId" select="$senderOrganizationId"/>
                <xsl:with-param name="streetName" select="$senderStreetName"/>
                <xsl:with-param name="cityName" select="$senderCityName"/>
                <xsl:with-param name="postalZone" select="$senderPostalZone"/>
                <xsl:with-param name="companyId" select="$senderVat"/>
            </xsl:apply-templates>
            <xsl:copy-of select="cac:DespatchContact"/>
            <xsl:copy-of select="cac:AccountingContact"/>
            <xsl:copy-of select="cac:SellerContact"/>
        </cac:AccountingSupplierParty>
    </xsl:template>
    <xsl:template match="cac:Party" mode="Supplier">
        <xsl:param name="organizationId"/>
        <xsl:param name="streetName"/>
        <xsl:param name="cityName"/>
        <xsl:param name="postalZone"/>
        <xsl:param name="companyId"/>
        <cac:Party>
            <xsl:copy-of select="cbc:MarkCareIndicator"/>
            <xsl:copy-of select="cbc:MarkAttentionIndicator"/>
            <xsl:copy-of select="cbc:WebsiteURI"/>
            <xsl:copy-of select="cbc:LogoReferenceID"/>
            <xsl:copy-of select="cbc:EndpointID"/>
            <xsl:copy-of select="cbc:IndustryClassificationCode"/>
            <xsl:call-template name="PartyIdentification">
                <xsl:with-param name="organizationId" select="$organizationId"/>
            </xsl:call-template>
            <xsl:copy-of select="cac:PartyName"/>
            <xsl:copy-of select="cac:Language"/>
            <xsl:apply-templates select="cac:PostalAddress">
                <xsl:with-param name="streetName" select="$streetName"/>
                <xsl:with-param name="cityName" select="$cityName"/>
                <xsl:with-param name="postalZone" select="$postalZone"/>
            </xsl:apply-templates>
            <xsl:copy-of select="cac:PhysicalLocation"/>
            <xsl:copy-of select="cac:PartyTaxScheme"/>
            <xsl:apply-templates select="cac:PartyLegalEntity">
                <xsl:with-param name="companyId" select="$companyId"/>
            </xsl:apply-templates>
        </cac:Party>
    </xsl:template>
   <xsl:template name="PartyIdentification">
       <xsl:param name="organizationId"/>
       <cac:PartyIdentification>
           <cbc:ID><xsl:value-of select="$organizationId"/></cbc:ID>
       </cac:PartyIdentification>
   </xsl:template>
    <xsl:template match="cac:PostalAddress">
        <xsl:param name="streetName"/>
        <xsl:param name="cityName"/>
        <xsl:param name="postalZone"/>
        <cac:PostalAddress>
            <xsl:call-template name="StreetName">
                <xsl:with-param name="streetName" select="$streetName"/>
            </xsl:call-template>
            <xsl:copy-of select="cbc:AdditionalStreetName"/>
            <xsl:copy-of select="cbc:BuildingNumber"/>
            <xsl:copy-of select="cbc:CitySubdivisionName"/>
            <xsl:call-template name="CityName">
                <xsl:with-param name="cityName" select="$cityName"/>
            </xsl:call-template>
            <xsl:call-template name="PostalZone">
                <xsl:with-param name="postalZone" select="$postalZone"/>
            </xsl:call-template>
            <xsl:copy-of select="cbc:CountrySubentity"/>
            <xsl:copy-of select="cac:AddressLine"/>
            <xsl:copy-of select="cac:Country"/>
        </cac:PostalAddress>
    </xsl:template>
    <xsl:template name="StreetName">
        <xsl:param name="streetName"/>
        <cbc:StreetName>
            <xsl:value-of select="$streetName"/>
        </cbc:StreetName>
    </xsl:template>
    <xsl:template name="CityName">
        <xsl:param name="cityName"/>
        <cbc:CityName>
            <xsl:value-of select="$cityName"/>
        </cbc:CityName>
    </xsl:template>
    <xsl:template name="PostalZone">
        <xsl:param name="postalZone"/>
        <cbc:PostalZone>
            <xsl:value-of select="$postalZone"/>
        </cbc:PostalZone>
    </xsl:template>
    <xsl:template match="cac:PartyLegalEntity">
        <xsl:param name="companyId"/>
        <cac:PartyLegalEntity>
            <xsl:copy-of select="cbc:RegistrationName"/>
            <xsl:call-template name="CompanyID">
                <xsl:with-param name="companyID" select="$companyId"></xsl:with-param>
            </xsl:call-template>
            <xsl:copy-of select="cbc:CompanyLegalForm"/>
        </cac:PartyLegalEntity>
    </xsl:template>
    <xsl:template name="CompanyID">
        <xsl:param name="companyID"/>
        <cbc:CompanyID>
            <xsl:value-of select="$companyID"/>
        </cbc:CompanyID>
    </xsl:template>
    
    
    
    
    
    
</xsl:stylesheet>