<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="sale_report_view">
        <t t-call="web.html_container">
            <t t-call="web.external_layout">
                <t class="page">
                    <div class="oe_structure"/>
                    <center>
                        <b>
                            <h3>Sales Profit Report</h3>

                        </b>
                    </center>
                    <t t-if="start_date and end_date">
                        <center>
                            <span t-esc="start_date"/>
                            To
                            <span t-esc="end_date"/>
                        </center>
                    </t>
                    <t t-if="type=='customer'">
                        <t t-foreach="partner_id" t-as="partner">
                            <center>
                                <b>
                                    <span t-esc="partner['name']"/>
                                </b>
                            </center>
                            <t t-set="t_cost" t-value="0"/>
                            <t t-set="t_price" t-value="0"/>
                            <t t-set="t_profit" t-value="0"/>
                            <br/>
                            <table class="table table-condensed">
                                <thead>
                                    <tr>
                                        <th>Order</th>
                                        <th>Date</th>
                                        <th>Product</th>
                                        <th>Quantity</th>
                                        <th>Cost</th>
                                        <th>Sale Price</th>
                                        <th>Profit</th>
                                        <th>Margin(%)</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr t-foreach="form" t-as="order">
                                        <t t-if="order['partner_id'] == partner['id']">
                                            <td>
                                                <span t-esc="order['sequence']"/>
                                            </td>
                                            <td>
                                                <span t-esc="order['date']" t-options='{"widget": "date"}'/>
                                            </td>
                                            <td>
                                                <span t-esc="order['product']"/>
                                            </td>
                                            <td>
                                                <span t-esc="order['quantity']"/>
                                            </td>
                                            <td>
                                                <span t-esc="order['cost']"/>
                                                <t t-set="t_cost" t-value="t_cost + order['cost']"></t>

                                            </td>
                                            <td>
                                                <span t-esc="order['price']"/>
                                                <t t-set="t_price" t-value="t_price + order['price']"></t>

                                            </td>
                                            <td>
                                                <span t-esc="order['profit']"/>
                                                <t t-set="t_profit" t-value="t_profit + order['profit']"></t>

                                            </td>
                                            <td>
                                                <span t-esc="order['margin']"/>
                                            </td>
                                        </t>
                                    </tr>
                                    <tr>
                                        <td>
                                            <span>Total cost</span>
                                            <t t-esc="t_cost"/>
                                        </td>
                                        <td>
                                            <span>Total Price:</span>
                                            <t t-esc="t_price"/>
                                        </td>
                                        <td>
                                            <span>Total Profit:</span>
                                            <t t-esc="t_profit"/>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </t>
                    </t>
                    <t t-if="type=='product'">
                        <t t-foreach="product_id" t-as="product">
                            <center>
                                <b>
                                    <span t-esc="product['name']"/>
                                </b>
                            </center>
                            <t t-set="t_cost" t-value="0"/>
                            <t t-set="t_price" t-value="0"/>
                            <t t-set="t_profit" t-value="0"/>
                            <br/>
                            <table class="table table-condensed">
                                <thead>
                                    <tr>
                                        <th>Order</th>
                                        <th>Date</th>
                                        <th>Customer</th>
                                        <th>Quantity</th>
                                        <th>Cost</th>
                                        <th>Sale Price</th>
                                        <th>Profit</th>
                                        <th>Margin(%)</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr t-foreach="form" t-as="order">
                                        <t t-if="order['product_id'] == product['id']">
                                            <td>
                                                <span t-esc="order['sequence']"/>
                                            </td>
                                            <td>
                                                <span t-esc="order['date']" t-options='{"widget": "date"}'/>
                                            </td>
                                            <td>
                                                <span t-esc="order['partner']"/>
                                            </td>
                                            <td>
                                                <span t-esc="order['quantity']"/>
                                            </td>
                                            <td>
                                                <span t-esc="order['cost']"/>
                                                <t t-set="t_cost" t-value="t_cost + order['cost']"></t>

                                            </td>
                                            <td>
                                                <span t-esc="order['price']"/>
                                                <t t-set="t_price" t-value="t_price + order['price']"></t>
                                            </td>
                                            <td>
                                                <span t-esc="order['profit']"/>
                                                <t t-set="t_profit" t-value="t_profit + order['profit']"></t>
                                            </td>
                                            <td>
                                                <span t-esc="order['margin']"/>
                                            </td>
                                        </t>
                                    </tr>
                                    <tr>
                                        <td>
                                            <span>Total cost</span>
                                            <t t-esc="t_cost"/>
                                        </td>
                                        <td>
                                            <span>Total Price:</span>
                                            <t t-esc="t_price"/>
                                        </td>
                                        <td>
                                            <span>Total Profit:</span>
                                            <t t-esc="t_profit"/>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </t>
                    </t>
                    <t t-if="type=='both'">
                        <table class="table table-condensed">
                            <t t-set="t_cost" t-value="0"/>
                            <t t-set="t_price" t-value="0"/>
                            <t t-set="t_profit" t-value="0"/>
                            <thead>
                                <tr>
                                    <th>Order</th>
                                    <th>Date</th>
                                    <th>Customer</th>
                                    <th>Product</th>
                                    <th>Quantity</th>
                                    <th>Cost</th>
                                    <th>Sale Price</th>
                                    <th>Profit</th>
                                    <th>Margin(%)</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr t-foreach="form" t-as="order">
                                    <td>
                                        <span t-esc="order['sequence']"/>
                                    </td>
                                    <td>
                                        <span t-esc="order['date']" t-options='{"widget": "date"}'/>
                                    </td>
                                    <td>
                                        <span t-esc="order['partner']"/>
                                    </td>
                                    <td>
                                        <span t-esc="order['product']"/>
                                    </td>
                                    <td>
                                        <span t-esc="order['quantity']"/>
                                    </td>
                                    <td>
                                        <span t-esc="order['cost']"/>
                                        <t t-set="t_cost" t-value="t_cost + order['cost']"></t>

                                    </td>
                                    <td>
                                        <span t-esc="order['price']"/>
                                        <t t-set="t_price" t-value="t_price + order['price']"></t>

                                    </td>
                                    <td>
                                        <span t-esc="order['profit']"/>
                                        <t t-set="t_profit" t-value="t_profit + order['profit']"></t>
                                    </td>
                                    <td>
                                        <span t-esc="order['margin']"/>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <span>Total cost</span>
                                        <t t-esc="t_cost"/>
                                    </td>
                                    <td>
                                        <span>Total Price:</span>
                                        <t t-esc="t_price"/>
                                    </td>
                                    <td>
                                        <span>Total Profit:</span>
                                        <t t-esc="t_profit"/>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </t>
                    <t t-if="no_value == True">
                        <table class="table table-condensed">
                            <t t-set="t_cost" t-value="0"/>
                            <t t-set="t_price" t-value="0"/>
                            <t t-set="t_profit" t-value="0"/>
                            <thead>
                                <tr>
                                    <th>Order</th>
                                    <th>Date</th>
                                    <th>Customer</th>
                                    <th>Product</th>
                                    <th>Quantity</th>
                                    <th>Cost</th>
                                    <th>Sale Price</th>
                                    <th>Profit</th>
                                    <th>Margin(%)</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr t-foreach="form" t-as="order">
                                    <td>
                                        <span t-esc="order['sequence']"/>
                                    </td>
                                    <td>
                                        <span t-esc="order['date']" t-options='{"widget": "date"}'/>
                                    </td>
                                    <td>
                                        <span t-esc="order['partner']"/>
                                    </td>
                                    <td>
                                        <span t-esc="order['product']"/>
                                    </td>
                                    <td>
                                        <span t-esc="order['quantity']"/>
                                    </td>
                                    <td>
                                        <span t-esc="order['cost']"/>
                                        <t t-set="t_cost" t-value="t_cost + order['cost']"></t>

                                    </td>
                                    <td>
                                        <span t-esc="order['price']"/>
                                        <t t-set="t_price" t-value="t_price + order['price']"></t>

                                    </td>
                                    <td>
                                        <span t-esc="order['profit']"/>
                                        <t t-set="t_profit" t-value="t_profit + order['profit']"></t>
                                    </td>
                                    <td>
                                        <span t-esc="order['margin']"/>

                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <span>Total cost</span>
                                        <t t-esc="t_cost"/>
                                    </td>
                                    <td>
                                        <span>Total Price:</span>
                                        <t t-esc="t_price"/>
                                    </td>
                                    <td>
                                        <span>Total Profit:</span>
                                        <t t-esc="t_profit"/>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </t>
                </t>
            </t>
        </t>
    </template>
</odoo>