<?xml version="1.0" encoding="UTF-8"?>
<CreditNote xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
            xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
            xmlns="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2">

    <!-- REQUIRED HEADERS -->
    <cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:fdc:peppol.eu:2017:poacc:billing:3.0</cbc:CustomizationID>
    <cbc:ProfileID>urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
    <!-- BASIC CREDIT NOTE DATA -->
    <cbc:ID>{{ correction.number }}</cbc:ID>
    <cbc:IssueDate>{{ correction.issue_date|date("Y-m-d") }}</cbc:IssueDate>
    <cbc:CreditNoteTypeCode>381</cbc:CreditNoteTypeCode>

    {% set headerNote = (correction.note|default('', true))|striptags|replace({'&nbsp;':' '})|trim %}
    {% if headerNote != '' %}
        <cbc:Note>{{ headerNote }}</cbc:Note>
    {% endif %}




    <cbc:DocumentCurrencyCode>{{ correction.currency }}</cbc:DocumentCurrencyCode>
    <cbc:BuyerReference>{{ correction.number }}</cbc:BuyerReference>


    <!-- ORYGINALNA FAKTURA (REFERENCJA WYMAGANA) -->
    <cac:BillingReference>
        <cac:InvoiceDocumentReference>
            <cbc:ID>{{ correction.original_invoice_number }}</cbc:ID>
        </cac:InvoiceDocumentReference>
    </cac:BillingReference>

    <!-- OPTIONAL PDF ATTACHMENT -->
    {% if correction.pdf_base64 is defined %}
        <cac:AdditionalDocumentReference>
            <cbc:ID>{{ correction.number }}-PDF</cbc:ID>
            <cac:Attachment>
                <cbc:EmbeddedDocumentBinaryObject
                        mimeCode="application/pdf"
                        filename="{{ correction.number }}.pdf">
                    {{ correction.pdf_base64 }}
                </cbc:EmbeddedDocumentBinaryObject>
            </cac:Attachment>
        </cac:AdditionalDocumentReference>
    {% endif %}


    <!-- SUPPLIER -->
    <cac:AccountingSupplierParty>
        <cac:Party>

            <cbc:EndpointID schemeID="{{ shop.participant_country_code }}">{{ shop.participant_code }}</cbc:EndpointID>

            <cac:PartyName>
                <cbc:Name>{{ shop.name }}</cbc:Name>
            </cac:PartyName>

            <cac:PostalAddress>
                <cbc:StreetName>{{ shop.street }}</cbc:StreetName>
                <cbc:CityName>{{ shop.city }}</cbc:CityName>
                <cbc:PostalZone>{{ shop.post_code }}</cbc:PostalZone>
                <cac:Country>
                    <cbc:IdentificationCode>{{ shop.country_code|upper|trim }}</cbc:IdentificationCode>
                </cac:Country>
            </cac:PostalAddress>

            <cac:PartyTaxScheme>
                <cbc:CompanyID>{{ shop.vat }}</cbc:CompanyID>
                <cac:TaxScheme><cbc:ID>VAT</cbc:ID></cac:TaxScheme>
            </cac:PartyTaxScheme>

            <cac:PartyLegalEntity>
                <cbc:RegistrationName>{{ shop.name }}</cbc:RegistrationName>
                <cbc:CompanyID>{{ shop.vat }}</cbc:CompanyID>
            </cac:PartyLegalEntity>

        </cac:Party>
    </cac:AccountingSupplierParty>

    <!-- CUSTOMER -->
    <cac:AccountingCustomerParty>
        <cac:Party>

            <cbc:EndpointID schemeID="{{ client.participant_country_code }}">{{ client.participant_code }}</cbc:EndpointID>

            <cac:PartyName><cbc:Name>{{ client.name }}</cbc:Name></cac:PartyName>

            <cac:PostalAddress>
                <cbc:StreetName>{{ client.street }}</cbc:StreetName>
                <cbc:CityName>{{ client.city }}</cbc:CityName>
                <cbc:PostalZone>{{ client.post_code }}</cbc:PostalZone>
                <cac:Country>
                    <cbc:IdentificationCode>{{ client.country_code|upper|trim }}</cbc:IdentificationCode>
                </cac:Country>
            </cac:PostalAddress>

            <cac:PartyTaxScheme>
                <cbc:CompanyID>{{ client.vat }}</cbc:CompanyID>
                <cac:TaxScheme><cbc:ID>VAT</cbc:ID></cac:TaxScheme>
            </cac:PartyTaxScheme>

            <cac:PartyLegalEntity>
                <cbc:RegistrationName>{{ client.name }}</cbc:RegistrationName>
                <cbc:CompanyID>{{ client.vat }}</cbc:CompanyID>
            </cac:PartyLegalEntity>

            {% if client.email is defined and client.email|trim != '' %}
                <cac:Contact>
                    <cbc:Name>{{ client.name}}</cbc:Name>
                    <cbc:ElectronicMail>{{ client.email }}</cbc:ElectronicMail>
                </cac:Contact>
            {% endif %}


        </cac:Party>
    </cac:AccountingCustomerParty>

    <!-- PAYMENT TERMS (optional in CreditNote) -->
    <cac:PaymentTerms>
        {% set payNote = (correction.payment_terms|default('Corrected invoice', true))|striptags|replace({'&nbsp;':' '})|trim %}
        {% if payNote != '' %}
            <cbc:Note>{{ payNote }}</cbc:Note>
        {% endif %}
    </cac:PaymentTerms>

    <!-- TAX TOTAL -->
    <cac:TaxTotal>
        <cbc:TaxAmount currencyID="{{ correction.currency }}">{{ correction.corrected_vat_total|number_format(2, '.', '') }}</cbc:TaxAmount>

        {% for rate, summary in correction.vat_groups %}
            <cac:TaxSubtotal>
                <cbc:TaxableAmount currencyID="{{ correction.currency }}">{{ summary.net|number_format(2, '.', '') }}</cbc:TaxableAmount>

                <cbc:TaxAmount currencyID="{{ correction.currency }}">{{ summary.vat|number_format(2, '.', '') }}</cbc:TaxAmount>

                <cac:TaxCategory>
                    <cbc:ID>S</cbc:ID>
                    <cbc:Percent>{{ rate }}</cbc:Percent>
                    <cac:TaxScheme><cbc:ID>VAT</cbc:ID></cac:TaxScheme>
                </cac:TaxCategory>
            </cac:TaxSubtotal>
        {% endfor %}
    </cac:TaxTotal>

    <!-- TOTALS -->
    <cac:LegalMonetaryTotal>
        <cbc:LineExtensionAmount currencyID="{{ correction.currency }}">{{ correction.sum_net|number_format(2, '.', '') }}</cbc:LineExtensionAmount>
        <cbc:TaxExclusiveAmount currencyID="{{ correction.currency }}">{{ correction.sum_net|number_format(2, '.', '') }}</cbc:TaxExclusiveAmount>
        <cbc:TaxInclusiveAmount currencyID="{{ correction.currency }}">{{ correction.sum_gross|number_format(2, '.', '') }}</cbc:TaxInclusiveAmount>
        <cbc:PayableAmount currencyID="{{ correction.currency }}">{{ correction.sum_gross|number_format(2, '.', '') }}</cbc:PayableAmount>
    </cac:LegalMonetaryTotal>

    <!-- CREDIT NOTE LINES -->
    {% for item in correction.items %}
        <cac:CreditNoteLine>
            <cbc:ID>{{ loop.index }}</cbc:ID>
            <cbc:CreditedQuantity unitCode="{{ item.unit_code|default('C62') }}">{{ item.quantity_for_ubl }}</cbc:CreditedQuantity>
            <cbc:LineExtensionAmount currencyID="{{ correction.currency }}">{{ item.line_net_for_ubl }}</cbc:LineExtensionAmount>
            <cac:Item>
                <cbc:Name>{{ item.product_name }}</cbc:Name>

                <cac:ClassifiedTaxCategory>
                    <cbc:ID>S</cbc:ID>
                    <cbc:Percent>{{ item.item_tax_perc|number_format(2, '.', '') }}</cbc:Percent>
                    <cac:TaxScheme><cbc:ID>VAT</cbc:ID></cac:TaxScheme>
                </cac:ClassifiedTaxCategory>
            </cac:Item>

            <cac:Price>
                <cbc:PriceAmount currencyID="{{ correction.currency }}">{{ item.unit_net_price_for_ubl }}</cbc:PriceAmount>
                <cbc:BaseQuantity unitCode="{{ item.unit_code|default('C62') }}">1</cbc:BaseQuantity>
            </cac:Price>
        </cac:CreditNoteLine>
    {% endfor %}

</CreditNote>