<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:c="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2" 
    xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
    xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
    exclude-result-prefixes="xs c"
    version="1.0">
    
    <xsl:param name="receiverOrganizationId"/>
    <xsl:param name="senderOrganizationId"/>
    <xsl:param name="receiverVat"/>
    <xsl:param name="senderVat"/>
    <xsl:param name="senderStreetName"/>
    <xsl:param name="senderCityName"/>
    <xsl:param name="senderPostalZone"/>
    
    <xsl:template match="/">
        <xsl:apply-templates select="c:CreditNote"/>
    </xsl:template>
    <xsl:template match="c:CreditNote">
        
        <CreditNote xmlns="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-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" 
            xmlns:exthr="urn:invoice:hr:schema:xsd:CommonExtensionComponents-1" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            >
            <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:TaxPointDate"/>
            <xsl:copy-of select="cbc:CreditNoteTypeCode"/>
            <xsl:copy-of select="cbc:Note"/>
            <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:DiscrepancyResponse"/>
            <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:ContractDocumentReference"/>
            <xsl:copy-of select="cac:AdditionalDocumentReference"/>
            <xsl:copy-of select="cac:StatementDocumentReference"/>
            <xsl:copy-of select="cac:OriginatorDocumentReference"/>
            <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:TaxExchangeRate"/>
            <xsl:copy-of select="cac:PricingExchangeRate"/>
            <xsl:copy-of select="cac:PaymentExchangeRate"/>
            <xsl:copy-of select="cac:PaymentAlternativeExchangeRate"/>
            <xsl:copy-of select="cac:AllowanceCharge"/>
            <xsl:copy-of select="cac:TaxTotal"/>
            <xsl:copy-of select="cac:LegalMonetaryTotal"/>
            <xsl:copy-of select="cac:CreditNoteline"/>
            
        </CreditNote>
    
    </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>