<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="report_saleorder_document">
    <t t-call="web.external_layout">
        <t t-set="doc" t-value="doc.with_context({'lang':doc.partner_id.lang})" />
        <t t-set="address">
            <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 'Tax ID'"/>: <span t-field="doc.partner_id.vat"/></p>
        </t>
        <t t-if="doc.partner_shipping_id == doc.partner_invoice_id
                             and doc.partner_invoice_id != doc.partner_id
                             or doc.partner_shipping_id != doc.partner_invoice_id">
            <t t-set="information_block">
                <strong t-if="doc.partner_shipping_id == doc.partner_invoice_id">Invoicing and shipping address:</strong>
                <strong t-if="doc.partner_shipping_id != doc.partner_invoice_id">Invoicing address:</strong>
                <div t-field="doc.partner_invoice_id"
                t-options='{"widget": "contact", "fields": ["address", "name", "phone"], "no_marker": True, "phone_icons": True}'/>
                <t t-if="doc.partner_shipping_id != doc.partner_invoice_id">
                    <strong>Shipping address:</strong>
                    <div t-field="doc.partner_shipping_id"
                        t-options='{"widget": "contact", "fields": ["address", "name", "phone"], "no_marker": True, "phone_icons": True}'/>
                </t>
            </t>
        </t>
        <div class="page">
            <div class="oe_structure"/>

            <h2>
                <t t-if="not (env.context.get('proforma', False) or is_pro_forma)">
                    <span t-if="doc.state not in ['draft','sent']">Order # </span>
                    <span t-if="doc.state in ['draft','sent']">Quotation # </span>
                </t>
                <t t-if="env.context.get('proforma', False) or is_pro_forma">
                    <span>Pro-Forma Invoice # </span>
                </t>
                <span t-field="doc.name"/>
            </h2>

            <div class="row col mt32 mb32" id="informations">
                <div t-if="doc.client_order_ref" class="mr32">
                    <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="mr32">
                    <strong>Date Ordered:</strong>
                    <p t-field="doc.confirmation_date"/>
                </div>
                <div t-if="doc.date_order and doc.state in ['draft','sent']" class="mr32">
                    <strong>Quotation Date:</strong>
                    <p t-field="doc.date_order"/>
                </div>
                <div t-if="doc.user_id.name" class="mr32">
                    <strong>Salesperson:</strong>
                    <p t-field="doc.user_id"/>
                </div>
                <div name="payment_term" t-if="doc.payment_term_id" class="mr32">
                    <strong>Payment Terms:</strong>
                    <p t-field="doc.payment_term_id"/>
                </div>
                <div t-if="doc.validity_date and doc.state in ['draft', 'sent']" class="mr32">
                    <strong>Expiration Date:</strong>
                    <p t-field="doc.validity_date"/>
                </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.order_line])"/>

            <table class="table table-sm">
                <thead>
                    <tr>
                        <t t-set="colspan" t-value="5"/>
                        <th class="text-left">Description</th>
                        <th class="text-right">Quantity</th>
                        <th class="text-right">Unit Price</th>
                        <th t-if="display_discount" class="text-right" groups="sale.group_discount_per_so_line">
                            <span>Disc.(%)</span>
                            <t t-set="colspan" t-value="colspan+1"/>
                        </th>
                        <th class="text-right">Taxes</th>
                        <th class="text-right">
                            <t groups="account.group_show_line_subtotals_tax_excluded">Amount</t>
                            <t groups="account.group_show_line_subtotals_tax_included">Total Price</t>
                        </th>
                    </tr>
                </thead>
                <tbody class="sale_tbody">

                    <t t-set="current_subtotal" t-value="0"/>

                    <t t-foreach="doc.order_line" 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">
                                <td><span t-field="line.name"/></td>
                                <td class="text-right">
                                    <span t-field="line.product_uom_qty"/>
                                    <span t-field="line.product_uom" groups="uom.group_uom"/>
                                </td>
                                <td class="text-right">
                                    <span t-field="line.price_unit"/>
                                </td>
                                <td t-if="display_discount" class="text-right" groups="sale.group_discount_per_so_line">
                                    <span t-field="line.discount"/>
                                </td>
                                <td class="text-right">
                                    <span t-esc="', '.join(map(lambda x: (x.description or x.name), line.tax_id))"/>
                                </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 doc.order_line[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": doc.pricelist_id.currency_id}'
                                    />
                                </td>
                            </tr>
                        </t>
                    </t>
                </tbody>
            </table>

            <div class="clearfix">
                <div class="row" name="total">
                    <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="doc.amount_untaxed"/>
                                </td>
                            </tr>
                            <t t-foreach="doc.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"/>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>

            <p t-field="doc.note" />
            <p t-if="doc.payment_term_id.note">
                <span t-field="doc.payment_term_id.note"/>
            </p>
            <p id="fiscal_position_remark" t-if="doc.fiscal_position_id and doc.fiscal_position_id.note">
                <strong>Fiscal Position Remark:</strong>
                <span t-field="doc.fiscal_position_id.note"/>
            </p>

            <div t-if="doc.signature" class="mt32 ml16 mr16" name="signature">
                <div class="offset-8">
                    <strong>Signature</strong>
                </div>
                <div class="offset-8">
                    <img t-att-src="image_data_uri(doc.signature)" style="max-height: 4cm; max-width: 8cm;"/>
                </div>
                <div class="offset-8 text-center">
                    <p t-field="doc.signed_by"/>
                </div>
            </div>

            <div class="oe_structure"/>
        </div>
    </t>
</template>


<template id="report_saleorder">
    <t t-call="web.html_container">
        <t t-foreach="docs" t-as="doc">
            <t t-call="sale.report_saleorder_document" t-lang="doc.partner_id.lang"/>
        </t>
    </t>
</template>

<template id="report_saleorder_pro_forma">
    <t t-call="web.html_container">
        <t t-set="is_pro_forma" t-value="True"/>
        <t t-foreach="docs" t-as="doc">
            <t t-call="sale.report_saleorder_document" t-lang="doc.partner_id.lang"/>
        </t>
    </t>
</template>

</odoo>
