<?xml version="1.0" encoding="UTF-8"?>
<Invoice 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:Invoice-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 INVOICE DATA -->
    <cbc:ID>{{ invoice.number }}</cbc:ID>
    <cbc:IssueDate>{{ invoice.issue_date|date("Y-m-d") }}</cbc:IssueDate>

    {% if invoice.due_date is defined %}
        <cbc:DueDate>{{ invoice.due_date|date("Y-m-d") }}</cbc:DueDate>
    {% endif %}

    <cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode> <!-- commercial invoice -->
    <cbc:DocumentCurrencyCode>{{ invoice.currency }}</cbc:DocumentCurrencyCode>

    <cbc:BuyerReference>{{ invoice.number }}</cbc:BuyerReference>

    {% if invoice.po_hash is defined and invoice.po_hash is not null %}
        <cac:OrderReference>
            {% if invoice.po_hash is empty %}
                <cbc:ID>NA</cbc:ID>
            {% else %}
                <cbc:ID>{{ invoice.po_hash }}</cbc:ID>
            {% endif %}
        </cac:OrderReference>
    {% endif %}


    <!-- OPTIONAL PDF ATTACHMENT -->
    {% if invoice.pdf_base64 is defined %}
        <cac:AdditionalDocumentReference>
            <cbc:ID>{{ invoice.number }}</cbc:ID>
            <cac:Attachment>
                <cbc:EmbeddedDocumentBinaryObject
                        mimeCode="application/pdf"
                        filename="{{ invoice.number }}.pdf">
                    {{ invoice.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>

    <!-- DELIVERY DATE -->
    {% if invoice.delivery_date is defined %}
        <cac:Delivery>
            <cbc:ActualDeliveryDate>{{ invoice.delivery_date|date("Y-m-d") }}</cbc:ActualDeliveryDate>
        </cac:Delivery>
    {% endif %}

    <!-- PAYMENT INFO -->
    {% if invoice.payment_iban is defined %}
        <cac:PaymentMeans>
            <cbc:PaymentMeansCode name="Credit transfer">15</cbc:PaymentMeansCode>
            <cbc:PaymentID>{{ invoice.number }}</cbc:PaymentID>
            <cac:PayeeFinancialAccount>
                <cbc:ID>{{ invoice.payment_iban }}</cbc:ID>
                <cbc:Name>{{ shop.name }}</cbc:Name>
            </cac:PayeeFinancialAccount>
        </cac:PaymentMeans>
    {% endif %}

    <cac:PaymentTerms>
        <cbc:Note>{{ invoice.payment_terms|default('Payment due upon receipt') }}</cbc:Note>
    </cac:PaymentTerms>

    <!-- TAX TOTAL -->
    <cac:TaxTotal>
        <cbc:TaxAmount
                currencyID="{{ invoice.currency }}">{{ invoice.tax_total|number_format(2, '.', '') }}</cbc:TaxAmount>

        {% for rate, summary in invoice.vat_groups %}
            <cac:TaxSubtotal>
                <cbc:TaxableAmount
                        currencyID="{{ invoice.currency }}">{{ summary.net|number_format(2, '.', '') }}</cbc:TaxableAmount>
                <cbc:TaxAmount
                        currencyID="{{ invoice.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="{{ invoice.currency }}">{{ invoice.sum_net|number_format(2, '.', '') }}</cbc:LineExtensionAmount>
        <cbc:TaxExclusiveAmount
                currencyID="{{ invoice.currency }}">{{ invoice.sum_net|number_format(2, '.', '') }}</cbc:TaxExclusiveAmount>
        <cbc:TaxInclusiveAmount
                currencyID="{{ invoice.currency }}">{{ invoice.sum_gross|number_format(2, '.', '') }}</cbc:TaxInclusiveAmount>
        <cbc:PayableAmount
                currencyID="{{ invoice.currency }}">{{ invoice.sum_gross|number_format(2, '.', '') }}</cbc:PayableAmount>
    </cac:LegalMonetaryTotal>


    <!-- INVOICE LINES -->
    {% for item in invoice.items %}
        <cac:InvoiceLine>
            <cbc:ID>{{ loop.index }}</cbc:ID>
            <cbc:InvoicedQuantity unitCode="{{ item.unit_code|default('C62') }}">{{ item.quantity_for_ubl|number_format(2, '.', '') }}</cbc:InvoicedQuantity>
            <cbc:LineExtensionAmount currencyID="{{ invoice.currency }}">{{ item.line_net_for_ubl|number_format(2, '.', '') }}</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="{{ invoice.currency }}">{{ item.unit_net_price_for_ubl|number_format(4, '.', '') }}</cbc:PriceAmount>
                <cbc:BaseQuantity unitCode="{{ item.unit_code|default('C62') }}">1</cbc:BaseQuantity>
            </cac:Price>
        </cac:InvoiceLine>
    {% endfor %}

</Invoice>
