<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <template id="report_invoice_document">
            <t t-call="web.external_layout">
                <t t-set="o" t-value="o.with_context({'lang': lang})"/>
                <t t-set="address">
                    <address t-field="o.partner_id" t-options='{"widget": "contact", "fields": ["address", "name"], "no_marker": True}' />
                    <div t-if="o.partner_id.vat" class="mt16"><t t-esc="o.company_id.country_id.vat_label or 'Tax ID'"/>: <span t-field="o.partner_id.vat"/></div>
                </t>
                <div class="page">
                    <h2>
                        <span t-if="o.type == 'out_invoice' and o.state in ('open', 'in_payment', 'paid')">Invoice</span>
                        <span t-if="o.type == 'out_invoice' and o.state == 'draft'">Draft Invoice</span>
                        <span t-if="o.type == 'out_invoice' and o.state == 'cancel'">Cancelled Invoice</span>
                        <span t-if="o.type == 'out_refund'">Credit Note</span>
                        <span t-if="o.type == 'in_refund'">Vendor Credit Note</span>
                        <span t-if="o.type == 'in_invoice'">Vendor Bill</span>
                        <span t-field="o.number"/>
                    </h2>

                    <div class="row mt32 mb32">
                        <div class="col-auto" t-if="o.name" name="description">
                            <strong>Description:</strong>
                            <p t-field="o.name"/>
                        </div>
                        <div class="col-auto" t-if="o.date_invoice" name="invoice_date">
                            <strong>Invoice Date:</strong>
                            <p t-field="o.date_invoice"/>
                        </div>
                        <div class="col-auto" t-if="o.date_due and o.type == 'out_invoice' and o.state in ('open', 'in_payment', 'paid')" name="due_date">
                            <strong>Due Date:</strong>
                            <p t-field="o.date_due"/>
                        </div>
                        <div class="col-auto" t-if="o.origin" name="origin">
                            <strong>Source:</strong>
                            <p t-field="o.origin"/>
                        </div>
                        <div class="col-auto" t-if="o.partner_id.ref" name="customer_code">
                            <strong>Customer Code:</strong>
                            <p t-field="o.partner_id.ref"/>
                        </div>
                        <div name="reference" class="col-auto" t-if="o.reference">
                            <strong>Reference:</strong>
                            <p t-field="o.reference"/>
                        </div>
                    </div>

                    <t t-set="display_discount" t-value="any([l.discount for l in o.invoice_line_ids])"/>

                    <table class="table table-sm" name="invoice_line_table">
                        <thead>
                            <tr>
                                <t t-set="colspan" t-value="6"/>
                                <th class="text-left"><span>Description</span></th>
                                <th class="d-none text-left"><span>Source Document</span></th>
                                <th class="text-right"><span>Quantity</span></th>
                                <th t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}"><span>Unit Price</span></th>
                                <th t-if="display_discount" t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}">
                                    <span>Disc.(%)</span>
                                    <t t-set="colspan" t-value="colspan+1"/>
                                </th>
                                <th t-attf-class="text-left {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}"><span>Taxes</span></th>
                                <th class="text-right">
                                    <span groups="account.group_show_line_subtotals_tax_excluded">Amount</span>
                                    <span groups="account.group_show_line_subtotals_tax_included">Total Price</span>
                                </th>
                            </tr>
                        </thead>
                        <tbody class="invoice_tbody">
                            <t t-set="current_subtotal" t-value="0"/>

                            <t t-foreach="o.invoice_line_ids" t-as="line">

                                <t t-set="current_subtotal" t-value="current_subtotal + line.price_subtotal" groups="account.group_show_line_subtotals_tax_excluded"/>
                                <t t-set="current_subtotal" t-value="current_subtotal + line.price_total" groups="account.group_show_line_subtotals_tax_included"/>

                                <tr t-att-class="'bg-200 font-weight-bold' if line.display_type == 'line_section' else 'font-italic' if line.display_type == 'line_note' else ''">
                                    <t t-if="not line.display_type" name="account_invoice_line_accountable">
                                        <td name="account_invoice_line_name"><span t-field="line.name"/></td>
                                        <td class="d-none"><span t-field="line.origin"/></td>
                                        <td class="text-right">
                                            <span t-field="line.quantity"/>
                                            <span t-field="line.uom_id"  groups="uom.group_uom"/>
                                        </td>
                                        <td t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}">
                                            <span t-field="line.price_unit" t-options='{"widget": "monetary", "display_currency": o.currency_id}'/>
                                        </td>
                                        <td t-if="display_discount" t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}">
                                            <span t-field="line.discount"/>
                                        </td>
                                        <td t-attf-class="text-left {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}">
                                            <span t-esc="', '.join(map(lambda x: (x.description or x.name), line.invoice_line_tax_ids))"/>
                                        </td>
                                        <td class="text-right">
                                            <span t-field="line.price_subtotal" groups="account.group_show_line_subtotals_tax_excluded"/>
                                            <span t-field="line.price_total" groups="account.group_show_line_subtotals_tax_included"/>
                                        </td>
                                    </t>
                                    <t t-if="line.display_type == 'line_section'">
                                        <td t-att-colspan="colspan">
                                            <span t-field="line.name"/>
                                        </td>
                                        <t t-set="current_section" t-value="line"/>
                                        <t t-set="current_subtotal" t-value="0"/>
                                    </t>
                                    <t t-if="line.display_type == 'line_note'">
                                        <td t-att-colspan="colspan">
                                            <span t-field="line.name"/>
                                        </td>
                                    </t>
                                </tr>

                                <t t-if="current_section and (line_last or o.invoice_line_ids[line_index+1].display_type == 'line_section')">
                                    <tr class="is-subtotal text-right">
                                        <td t-att-colspan="colspan">
                                            <strong class="mr16">Subtotal</strong>
                                            <span
                                                t-esc="current_subtotal"
                                                t-options='{"widget": "monetary", "display_currency": o.currency_id}'
                                            />
                                        </td>
                                    </tr>
                                </t>
                            </t>
                        </tbody>
                    </table>

                    <div class="clearfix">
                        <div id="total" class="row">
                            <div t-attf-class="#{'col-4' if report_type != 'html' else 'col-sm-7 col-md-5'} ml-auto">
                                <table class="table table-sm">
                                    <tr class="border-black" style="border-bottom:1px solid #dddddd;">
                                        <td><strong>Subtotal</strong></td>
                                        <td class="text-right">
                                            <span t-field="o.amount_untaxed"/>
                                        </td>
                                    </tr>
                                    <t t-foreach="o.amount_by_group" t-as="amount_by_group">
                                        <tr style="border-bottom:1px solid #dddddd;">
                                            <t t-if="len(o.tax_line_ids) == 1 and o.amount_untaxed == amount_by_group[2]">
                                                <td><span t-esc="amount_by_group[0]"/></td>
                                                <td class="text-right">
                                                    <span t-esc="amount_by_group[3]" />
                                                </td>
                                            </t>
                                            <t t-else="">
                                                <td>
                                                    <span t-esc="amount_by_group[0]"/>
                                                    <span>&amp;nbsp;<span>on</span>
                                                        <t t-esc="amount_by_group[4]"/>
                                                    </span>
                                                </td>
                                                <td class="text-right">
                                                    <span t-esc="amount_by_group[3]"/>
                                                </td>
                                            </t>
                                        </tr>
                                    </t>
                                    <tr class="border-black">
                                        <td><strong>Total</strong></td>
                                        <td class="text-right">
                                            <span t-field="o.amount_total"/>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                    <p t-if="o.reference">
                        Please use the following communication for your payment : <b><span t-field="o.reference"/></b>
                    </p>
                    <p t-if="o.comment" name="comment">
                        <span t-field="o.comment"/>
                    </p>
                    <p t-if="o.payment_term_id" name="payment_term">
                        <span t-field="o.payment_term_id.note"/>
                    </p>
                    <p t-if="o.fiscal_position_id.note" name="note">
                        <span t-field="o.fiscal_position_id.note"/>
                    </p>
                    <div id="qrcode" t-if="(o.partner_id.country_id.code in ['BE', 'AT', 'DE', 'FI', 'NL']) and (o.company_id.qr_code) and (o.currency_id.name == 'EUR') and (o.partner_bank_id.acc_number != False)">
                        <p t-if="(o.partner_bank_id.qr_code_valid)">
                            <strong class="text-center">Scan me with your banking app.</strong><br /><br />
                            <img class="border border-dark rounded" t-att-src="o.partner_bank_id.build_qr_code_url(o.residual,(o.reference) if (o.reference) else o.number)"/>
                        </p>
                        <p t-if="(o.partner_bank_id.qr_code_valid == False)">
                            <strong class="text-center">The SEPA QR Code informations are not set correctly.</strong><br />
                        </p>
                    </div>
                </div>
            </t>
        </template>

        <template id="report_invoice_document_with_payments" inherit_id="account.report_invoice_document" primary="True">
            <xpath expr="//div[@id='total']/div/table" position="inside">
                <t t-set="payments_vals" t-value="o._get_payments_vals()"/>
                <t t-foreach="payments_vals" t-as="payment_vals">
                    <tr>
                        <td>
                            <i class="oe_form_field text-right oe_payment_label">Paid on <t t-esc="payment_vals['date']"/></i>
                        </td>
                        <td class="text-right">
                            <span t-esc="payment_vals['amount']" t-options='{"widget": "monetary", "display_currency": o.currency_id}'/>
                        </td>
                    </tr>
                </t>
                <t t-if="len(payments_vals) > 0">
                    <tr class="border-black">
                        <td><strong>Amount Due</strong></td>
                        <td class="text-right">
                             <span t-field="o.residual"/>
                        </td>
                    </tr>
                </t>
            </xpath>
        </template>

        <template id="report_invoice">
            <t t-call="web.html_container">
                <t t-foreach="docs" t-as="o">
                    <t t-set="lang" t-value="o.user_id.lang if o.type in ('in_invoice', 'in_refund') else o.partner_id.lang"/>
                    <t t-call="account.report_invoice_document" t-lang="lang"/>
                </t>
            </t>
        </template>

        <template id="report_invoice_with_payments">
            <t t-call="web.html_container">
                <t t-foreach="docs" t-as="o">
                    <t t-set="lang" t-value="o.user_id.lang if o.type in ('in_invoice', 'in_refund') else o.partner_id.lang"/>
                    <t t-call="account.report_invoice_document_with_payments" t-lang="lang"/>
                </t>
            </t>
        </template>
    </data>
</odoo>
