<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:n="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2" 
    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:ns0="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2" 
    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:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-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 i ns1 a n" 
    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:choose>
            <xsl:when test="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID[@schemeID='9934']">
                <xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
            </xsl:when>
            <xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and (string-length(normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)) = 11)">
                <xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" mode="SchemeID" />
            </xsl:when>
            <xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID[starts-with(text(), '9934:')]">
                <xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID" mode="PartyID2CompanyID" />
            </xsl:when>
            <xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID[starts-with(text(), 'HR')]">
                <xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID" mode="TaxID2CompanyID" />
            </xsl:when>
            <xsl:when test="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID">
                <xsl:apply-templates select="//cac:AccountingSupplierParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
            </xsl:when>
            
        </xsl:choose>
        
    </xsl:variable>
    
    <xsl:variable name="customerCompanyId">
        <xsl:choose>
            <xsl:when test="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID[@schemeID='9934']">
                <xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
            </xsl:when>
            <xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and (string-length(normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)) = 11)">
                <xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" mode="SchemeID" />
            </xsl:when>
            <xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID[starts-with(text(), '9934:')]">
                <xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID" mode="PartyID2CompanyID" />
            </xsl:when>
            <xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID[starts-with(text(), 'HR')]">
                <xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID" mode="TaxID2CompanyID" />
            </xsl:when>
            <xsl:when test="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID">
                <xsl:apply-templates select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID" mode="CompanyID" />
            </xsl:when>
            
        </xsl:choose>
        
    </xsl:variable>
    
    <xsl:template match="/">       
        <OutgoingInvoicesData xmlns="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2">
            <Header>
                <!--Oib dobavljača-->
                <SupplierID><xsl:value-of select="$supplierCompanyId" /></SupplierID>
                <!--oznaka je li dokument business to business, b2b -1 ili business to customer b2c-0-->
                <InvoiceType>1</InvoiceType>
            </Header>
            <OutgoingInvoice>
                <xsl:call-template name="HROutgoingInvoiceHeader" />
                <InvoiceEnvelope>
                    <xsl:copy-of select="//i:Invoice" />
                </InvoiceEnvelope>
                <AttachedDocumentEnvelope>
                    <xsl:copy-of select="//a:AttachedDocument"/>
                </AttachedDocumentEnvelope>
            </OutgoingInvoice>
        </OutgoingInvoicesData>
    </xsl:template>
    
    <xsl:template name="HROutgoingInvoiceHeader">
        <n:SupplierInvoiceID>
            <xsl:value-of select="string(/i:Invoice/cbc:ID)"/>
        </n:SupplierInvoiceID>
        <n:BuyerID>
            <!--<xsl:value-of select="string(/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)"/>-->
            <xsl:value-of select="$customerCompanyId" />
        </n:BuyerID>
  
    </xsl:template>
      
   
</xsl:stylesheet>