<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="report_folio_document">
    <t t-call="web.external_layout">
        <t t-set="doc" t-value="doc.with_context({'lang':doc.partner_id.lang})" />
        <div class="page">
            <div class="oe_structure"/>
            <div class="row">
                <div class="col-xs-6">
                    <t t-if="doc.partner_invoice_id != doc.partner_id">
                        <div t-field="doc.partner_invoice_id"
                        t-options='{"widget": "contact", "fields": ["address", "name", "phone"], "no_marker": True, "phone_icons": True}'/>
                    </t>
                </div>
                <div class="col-xs-5 col-xs-offset-1">
                    <div t-field="doc.partner_id"
                        t-options='{"widget": "contact", "fields": ["address", "name"], "no_marker": True}' />
                    <p t-if="doc.partner_id.vat"><t t-esc="doc.company_id.country_id.vat_label or 'TIN'"/>: <span t-field="doc.partner_id.vat"/></p>
                </div>
            </div>

            <h2>
                <span t-if="doc.state not in ['draft','sent']">Order # </span>
                <span t-if="doc.state in ['draft','sent']">Quotation # </span>
                <span t-field="doc.name"/>
            </h2>

            <div class="row mt32 mb32" id="informations">
                <div t-if="doc.client_order_ref" class="col-xs-3">
                    <strong>Your Reference:</strong>
                    <p t-field="doc.client_order_ref"/>
                </div>
                <div t-if="doc.confirmation_date and doc.state not in ['draft','sent']" class="col-xs-3">
                    <strong>Date Ordered:</strong>
                    <p t-field="doc.confirmation_date"/>
                </div>
                <div t-if="doc.date_order and doc.state in ['draft','sent']" class="col-xs-3">
                    <strong>Quotation Date:</strong>
                    <p t-field="doc.date_order"/>
                </div>
                <div t-if="doc.user_id.name" class="col-xs-3">
                    <strong>Salesperson:</strong>
                    <p t-field="doc.user_id"/>
                </div>
                <div name="payment_term" t-if="doc.payment_term_id" class="col-xs-3">
                    <strong>Payment Terms:</strong>
                    <p t-field="doc.payment_term_id"/>
                </div>
            </div>

            <!-- Is there a discount on at least one line? -->
            <t t-set="display_discount" t-value="any([l.discount for l in doc.room_lines])"/>

                <table class="table table-condensed">
                    <thead>
                        <tr>
                            <th>Description</th>
                            <th class="text-right">Quantity</th>
                            <th t-if="display_discount" class="text-right" groups="sale.group_discount_per_so_line">Disc.(%)</th>
                            <th class="text-right">Taxes</th>
                            <th class="text-right" groups="sale.group_show_price_subtotal">Amount</th>
                            <th class="text-right price_tax_included" groups="sale.group_show_price_total">Total Price</th>
                        </tr>
                   </thead>
                   <tbody class="sale_tbody">
                            <!-- Lines associated -->
                            <t t-foreach="doc.room_lines" t-as="l">
                                <t t-if="l.to_print == True and l.price_total > 0">
                                    <tr>
                                        <td><span t-field="l.name"/></td>
                                        <td class="text-right">
                                            <span t-field="l.nights"/>
                                        </td>
                                        <td t-if="display_discount" class="text-right" groups="sale.group_discount_per_so_line">
                                            <span t-field="l.discount"/>
                                        </td>
                                        <td class="text-right">
                                            <span t-esc="', '.join(map(lambda x: (x.description or x.name), l.tax_ids))"/>
                                        </td>
                                        <td class="text-right" groups="sale.group_show_price_subtotal">
                                            <span t-field="l.price_subtotal"
                                                t-options='{"widget": "monetary", "display_currency": doc.pricelist_id.currency_id}'/>
                                        </td>
                                        <td class="text-right" groups="sale.group_show_price_total">
                                            <span t-field="l.price_total"
                                                t-options='{"widget": "monetary", "display_currency": doc.pricelist_id.currency_id}'/>
                                        </td>
                                    </tr>
                                </t>
                            </t>
                            <t t-foreach="doc.service_ids" t-as="l">
                                <t t-if="l.to_print == True and l.price_total > 0">
                                    <tr>
                                        <td><span t-field="l.name"/></td>
                                        <td class="text-right">
                                            <span t-field="l.product_qty"/>
                                        </td>
                                        <td t-if="display_discount" class="text-right" groups="sale.group_discount_per_so_line">
                                            <span t-field="l.discount"/>
                                        </td>
                                        <td class="text-right">
                                            <span t-esc="', '.join(map(lambda x: (x.description or x.name), l.tax_ids))"/>
                                        </td>
                                        <td class="text-right" groups="sale.group_show_price_subtotal">
                                            <span t-field="l.price_subtotal"
                                                t-options='{"widget": "monetary", "display_currency": doc.pricelist_id.currency_id}'/>
                                        </td>
                                        <td class="text-right" groups="sale.group_show_price_total">
                                            <span t-field="l.price_total"
                                                t-options='{"widget": "monetary", "display_currency": doc.pricelist_id.currency_id}'/>
                                        </td>
                                    </tr>
                                </t>
                            </t>
                    </tbody>
                </table>

            <div class="clearfix">
                <div class="row" name="total">
                    <div class="col-xs-4 pull-right">
                        <table class="table table-condensed" style="min-width: 200px;max-width: 350px;">
                            <tr class="border-black" style="border-bottom:1px solid #dddddd;">
                                <td><strong>Subtotal</strong></td>
                                <td class="text-right">
                                    <span t-field="doc.amount_untaxed"
                                        t-options='{"widget": "monetary", "display_currency": doc.pricelist_id.currency_id}'/>
                                </td>
                            </tr>
                            -<t t-foreach="doc._get_tax_amount_by_group()" t-as="amount_by_group">
                                <tr style="border-bottom:1px solid #dddddd;">
                                    <t t-if="amount_by_group[3] == 1 and doc.amount_untaxed == amount_by_group[2]">
                                        <td>
                                            <span t-esc="amount_by_group[0]"/>
                                            <span>&amp;nbsp;<span>on</span>&amp;nbsp;<t t-esc="amount_by_group[2]" t-options='{"widget": "monetary", "display_currency": doc.pricelist_id.currency_id}'/></span>
                                        </td>
                                        <td class="text-right">
                                            <span t-esc="amount_by_group[1]"
                                                t-options='{"widget": "monetary", "display_currency": doc.pricelist_id.currency_id}'/>
                                        </td>
                                    </t>
                                    <t t-else ="">
                                        <td>
                                            <span t-esc="amount_by_group[0]"/>
                                        </td>
                                        <td class="text-right">
                                            <span t-esc="amount_by_group[1]"
                                                t-options='{"widget": "monetary", "display_currency": doc.pricelist_id.currency_id}'/>
                                        </td>
                                    </t>
                                </tr>
                            </t>
                            <tr class="border-black">
                                <td><strong>Total</strong></td>
                                <td class="text-right">
                                    <span t-field="doc.amount_total"
                                        t-options='{"widget": "monetary", "display_currency": doc.pricelist_id.currency_id}'/>
                                </td>
                            </tr>
                            <tr class="border-black">
                                <td><strong>Pending Payment</strong></td>
                                <td class="text-right">
                                    <span t-field="doc.pending_amount"
                                        t-options='{"widget": "monetary", "display_currency": doc.pricelist_id.currency_id}'/>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
            <div>
                <span t-if="doc.payment_ids">
                    <table style="width:80%;">
                        <thead>
                            <tr>
                                <th>Payment Ref.</th>
                                <th>Payment Date</th>
                                <th>Payment Method</th>
                                <th>Paid Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr t-foreach="doc.payment_ids" t-as="l">
                                <td t-esc="l.name" />
                                <td t-esc="l.payment_date" />
                                <td t-esc="l.journal_id.name" />
                                <td t-esc="l.amount" />
                            </tr>
                        </tbody>
                    </table>
                </span>
                <span t-if="doc.return_ids">
                    <table style="width:80%;">
                        <thead>
                            <tr>
                                <th>Return Ref.</th>
                                <th>Return Date</th>
                                <th>Return Method</th>
                                <th>Return Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr t-foreach="doc.return_ids" t-as="r">
                                <td t-esc="r.name" />
                                <td t-esc="r.date" />
                                <td t-esc="r.journal_id.name" />
                                <t t-set="total_amount" t-value="sum(l.amount for l in r.line_ids)"/>
                                <td t-esc="total_amount" />
                            </tr>
                        </tbody>
                    </table>
                </span>
            </div>

            <p t-field="doc.note" />
            <p t-if="doc.payment_term_id.note">
                <span t-field="doc.payment_term_id.note"/>
            </p>
            <div class="oe_structure"/>
        </div>
    </t>
</template>


<template id="report_folio">
    <t t-call="web.html_container">
        <t t-foreach="docs" t-as="doc">
            <t t-call="hotel.report_folio_document" t-lang="doc.partner_id.lang"/>
        </t>
    </t>
</template>

</odoo>
