<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!--  Report template  -->
    <template id="report_fleet_services">
        <t t-call="web.html_container">
            <t t-call="web.external_layout">
                <t t-if="services">
                    <h2>Services Reports</h2>
                    <small> Period: <span t-esc="period" />
                    </small>
                    <br></br>
                    <br></br>
                    <table name="services" class="table table-condensed">
                        <thead>
                            <tr>
                                <th />
                                <th>Service</th>
                                <th>Driver</th>
                                <th>Date</th>
                                <th>Cost</th>
                                <th>Odometer</th>
                                <th>Vendor</th>
                            </tr>
                        </thead>
                        <tbody>
                            <t t-set="report_vehicle" t-value="false" />
                            <t t-set="total_cost" t-value="0" />
                            <t t-set="service_cost" t-value="0" />
                            <t t-foreach="services" t-as="service">
                                <t t-if="not report_vehicle or report_vehicle != service.vehicle_id">
                                    <tr t-if="report_vehicle">
                                        <td colspan="3" />
                                        <th align="right">Sub Total</th>
                                        <th>
                                            <span t-esc="service_cost"
                                                t-options="{'widget': 'monetary', 'display_currency': service.currency_id}" />
                                        </th>
                                        <td colspan="2" />
                                    </tr>
                                    <t t-set="service_cost" t-value="0" />
                                    <t t-set="report_vehicle" t-value="service.vehicle_id" />
                                    <tr>
                                        <th style="background-color: #edeaea;font-size: 12px;">
                                            <span t-esc="service.vehicle_id.name" />
                                        </th>
                                        <td style="background-color: #edeaea;" colspan="6" />
                                    </tr>
                                </t>
                                <tr>
                                    <td />
                                    <td>
                                        <span t-esc="service.service_type_id.name" />
                                    </td>
                                    <td>
                                        <span t-esc="service.purchaser_id.name" />
                                    </td>
                                    <td>
                                        <span t-esc="service.date" t-options="{'widget': 'date'}" />
                                    </td>
                                    <td>
                                        <span t-esc="service.amount"
                                            t-options="{'widget': 'monetary', 'display_currency': service.currency_id}" />
                                    </td>
                                    <td>
                                        <span t-esc="service.odometer" />
                                        <span t-if="service.odometer_unit">
                                            <t
                                                t-esc="dict(service.fields_get(allfields=['odometer_unit'])['odometer_unit']['selection'])[service.odometer_unit]" />
                                        </span>
                                    </td>
                                    <td>
                                        <span t-esc="service.vendor_id.name" />
                                    </td>
                                </tr>
                                <t t-set="service_cost" t-value="service_cost + service.amount" />
                                <t t-set="total_cost" t-value="total_cost + service.amount" />
                            </t>
                            <t t-if="report_vehicle">
                                <tr>
                                    <td colspan="3" />
                                    <th align="right">Sub Total</th>
                                    <th>
                                        <span t-esc="service_cost"
                                            t-options="{'widget': 'monetary', 'display_currency': service.currency_id}" />
                                    </th>
                                    <td colspan="2" />
                                </tr>
                                <tr>
                                    <td colspan="3" />
                                    <th align="right">Grand Total</th>
                                    <th>
                                        <span t-esc="total_cost"
                                            t-options="{'widget': 'monetary', 'display_currency': service.currency_id}" />
                                    </th>
                                    <td colspan="2" />
                                </tr>
                            </t>
                        </tbody>
                    </table>
                </t>
            </t>
        </t>
    </template>
</odoo>