<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <template id="theme_invoice_G002_document">

        <t t-call="web.external_layout">
            <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 mt32">
                <h2>
                    <span t-if="o.release_capital_request">Request to Release Capital</span>
                    <span t-else="">
                        <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>
                    <span t-field="o.number"/>
                </h2>

                <div id="informations" class="row mt32 mb32">
                    <div class="col-auto mw-100 mb-2" t-if="o.name" name="description">
                        <strong>Description:</strong>
                        <p class="m-0" t-field="o.name"/>
                    </div>
                    <div class="col-auto mw-100 mb-2" t-if="o.date_invoice" name="invoice_date">
                        <strong>Request Date:</strong>
                        <p class="m-0" t-field="o.date_invoice"/>
                    </div>
                    <div name="reference" class="col-auto mw-100 mb-2" t-if="o.reference">
                        <strong>Structured Communication:</strong>
                        <p class="m-0" t-field="o.reference"/>
                    </div>
                    <div name="account_number" class="col-auto mw-100 mb-2">
                        <strong>Account Number:</strong>
                        <t t-foreach="o.company_id.bank_journal_ids" t-as="journal">
                            <t t-set="b" t-value="journal.bank_account_id"/>
                            <t t-if="o.release_capital_request and journal.get_cooperator_payment">
                                <p class="m-0" t-field="b.acc_number"/>
                            </t>
                            <t t-if="not o.release_capital_request and journal.get_general_payment">
                                <p t-field="b.acc_number"/>
                            </t>
                        </t>
                    </div>
                    <div name="contact" class="col-auto mw-100 mb-2">
                        <strong>Your Contact:</strong>
                        <p t-field="o.user_id"/>
                    </div>
                </div>

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

                <table class="table table-sm o_main_table" name="invoice_line_table">
                    <thead>
                        <tr>
                            <t t-set="colspan" t-value="5"/>
                            <th class="text-left"><span>Description</span></th>
                            <th class="d-none text-left"><span>Source Document</span></th>
                            <th class="text-right"><span>Part Type</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 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 o_line_section' if line.display_type == 'line_section' else 'font-italic o_line_note' 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.product_id.short_name"/>
                                    </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"/>
                                    </td>
                                    <td class="text-right o_price_total">
                                        <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 colspan="99">
                                        <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 colspan="99">
                                        <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 colspan="99">
                                        <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 o_total">
                                    <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="theme_invoice_G002">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="o">
                <t t-call="easy_my_coop.theme_invoice_G002_document" t-lang="o.partner_id.lang"/>
            </t>
        </t>
    </template>

</odoo>