<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="report_credit_control_summary_document">
        <t t-call="report.external_layout">
            <t t-set="doc" t-value="doc.with_context({'lang':doc.partner_id.lang})" />
            <div class="page">
                <div class="row" id="address">
                    <div class="col-xs-5 col-xs-offset-7">
                        <address t-field="doc.contact_address"
                                 t-field-options='{"widget": "contact",
                                                   "fields": ["address", "name"],
                                                   "no_marker": true}'/>
                    </div>
                    <div class="col-xs-5 col-xs-offset-7">
                        <span t-field="doc.report_date"/>
                    </div>
                </div>

                <h2 id="policy_level">
                    <span t-field="doc.current_policy_level.name"/>
                </h2>

                <div class="row mt32 mb32">
                    <div class="col-xs-12">
                       <span t-field="doc.current_policy_level.custom_text"/>
                    </div>
                </div>

                <h3>Summary</h3>

                <table class="table table-condensed" id="summary_table">
                    <thead>
                        <tr>
                            <th>Invoice number</th>
                            <th>Invoice date</th>
                            <th>Date due</th>
                            <th class="text-right">Invoiced amount</th>
                            <th class="text-right">Open amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr t-foreach="doc.credit_control_line_ids" t-as="l">
                            <t t-if="l.invoice_id">
                                <td>
                                    <span t-field="l.invoice_id.number"/>
                                    <t t-if="l.invoice_id.name">
                                        <span t-field="l.invoice_id.name"/>
                                    </t>
                                </td>
                            </t>
                            <t t-if="not l.invoice_id">
                                <td>
                                    <span t-field="l.move_line_id.name"/>
                                </td>
                            </t>
                            <td>
                                <span t-field="l.date_entry"/>
                            </td>
                            <td>
                                <span t-field="l.date_due"/>
                            </td>

                            <td class="text-right">
                                <span t-field="l.amount_due"/>
                            </td>
                            <td class="text-right">
                                <span t-field="l.balance_due"/>
                            </td>
                        </tr>
                    </tbody>
                </table>

                <div class="row">
                    <div class="col-xs-4 pull-right">
                        <table class="table table-condensed">
                            <tr>
                                <td>
                                    <strong>Total Invoiced</strong>
                                </td>
                                <td class="text-right">
                                    <span t-field="doc.total_invoiced" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <strong>Total Due</strong>
                                </td>
                                <td class="text-right">
                                    <span t-field="doc.total_due"/>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>

                <t t-if="doc.current_policy_level.custom_text_after_details">
                    <div class="row mt32 mb32">
                        <div class="col-xs-12">
                            <p t-field="doc.current_policy_level.custom_text_after_details"/>
                        </div>
                    </div>
                </t>

            </div>
        </t>
    </template>

    <template id="report_credit_control_summary">
        <t t-call="report.html_container">
            <t t-foreach="docs" t-as="doc">
                <t t-call="account_credit_control.report_credit_control_summary_document" t-lang="doc.partner_id.lang" />
            </t>
        </t>
    </template>
</odoo>
