<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>

        <!-- ============================================================
             Report Actions
        ============================================================ -->

        <record id="action_report_timesheet_list" model="ir.actions.report">
            <field name="name">Timesheet List</field>
            <field name="model">account.analytic.line</field>
            <field name="report_type">qweb-pdf</field>
            <field name="report_name">jb_timesheet.report_timesheet_list</field>
            <field name="report_file">jb_timesheet.report_timesheet_list</field>
            <field name="print_report_name">'Timesheet_List_%s' % (object.date)</field>
        </record>

        <record id="action_report_project_breakdown" model="ir.actions.report">
            <field name="name">Project Breakdown</field>
            <field name="model">project.project</field>
            <field name="report_type">qweb-pdf</field>
            <field name="report_name">jb_timesheet.report_project_breakdown</field>
            <field name="report_file">jb_timesheet.report_project_breakdown</field>
            <field name="print_report_name">'Project_Breakdown_%s' % (object.name)</field>
        </record>

        <record id="action_report_office_target" model="ir.actions.report">
            <field name="name">Office Target</field>
            <field name="model">hr.employee</field>
            <field name="report_type">qweb-pdf</field>
            <field name="report_name">jb_timesheet.report_office_target</field>
            <field name="report_file">jb_timesheet.report_office_target</field>
            <field name="print_report_name">'Office_Target'</field>
        </record>

        <record id="action_report_my_target" model="ir.actions.report">
            <field name="name">My Target</field>
            <field name="model">hr.employee</field>
            <field name="report_type">qweb-pdf</field>
            <field name="report_name">jb_timesheet.report_my_target</field>
            <field name="report_file">jb_timesheet.report_my_target</field>
            <field name="print_report_name">'My_Target'</field>
        </record>

        <!-- ============================================================
             Timesheet List Report Template (S14)
        ============================================================ -->

        <template id="report_timesheet_list">
            <t t-call="web.html_container">
                <t t-foreach="docs" t-as="line">
                    <t t-call="web.external_layout">
                        <div class="page">
                            <h2>Timesheet List</h2>
                            <table class="table table-sm table-bordered">
                                <thead class="table-light">
                                    <tr>
                                        <th>Date</th>
                                        <th>Client</th>
                                        <th>Project</th>
                                        <th>Task</th>
                                        <th>User</th>
                                        <th>Description</th>
                                        <th>Hours</th>
                                        <th>Billable</th>
                                        <th>Invoiced</th>
                                        <th>EIT Billing ID</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><t t-esc="line.date"/></td>
                                        <td>
                                            <t t-if="line.project_id and line.project_id.partner_id">
                                                <t t-esc="line.project_id.partner_id.name"/>
                                            </t>
                                        </td>
                                        <td><t t-esc="line.project_id.name"/></td>
                                        <td><t t-esc="line.task_id.name"/></td>
                                        <td><t t-esc="line.employee_id.name"/></td>
                                        <td><t t-esc="line.name"/></td>
                                        <td class="text-end">
                                            <t t-esc="'%.2f' % line.unit_amount"/>
                                        </td>
                                        <td>
                                            <t t-if="line.is_billable">Yes</t>
                                            <t t-else="">No</t>
                                        </td>
                                        <td>
                                            <t t-if="line.is_invoiced">Yes</t>
                                            <t t-else="">No</t>
                                        </td>
                                        <td><t t-esc="line.eit_billing_id or ''"/></td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </t>
                </t>
            </t>
        </template>

        <!-- ============================================================
             Project Breakdown Report Template (S15)
        ============================================================ -->

        <template id="report_project_breakdown">
            <t t-call="web.html_container">
                <t t-foreach="docs" t-as="project">
                    <t t-call="web.external_layout">
                        <div class="page">
                            <h2>Project Breakdown: <t t-esc="project.name"/></h2>
                            <t t-set="tasks" t-value="project.task_ids.filtered(lambda t: not t.parent_id)"/>
                            <table class="table table-sm table-bordered">
                                <thead class="table-light">
                                    <tr>
                                        <th>Task</th>
                                        <th class="text-end">Planned (h)</th>
                                        <th class="text-end">Spent (h)</th>
                                        <th class="text-end">% Used</th>
                                        <th class="text-end">Overrun (h)</th>
                                        <th class="text-end">Billable (h)</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <t t-foreach="tasks" t-as="task">
                                        <t t-set="overrun"
                                           t-value="max(0, task.effective_hours - task.planned_hours)"/>
                                        <t t-set="billable_h"
                                           t-value="sum(task.timesheet_ids.filtered('is_billable').mapped('unit_amount'))"/>
                                        <tr t-att-class="'table-danger' if task.hours_progress >= 100 else ('table-warning' if task.hours_progress >= 75 else '')">
                                            <td><t t-esc="task.name"/></td>
                                            <td class="text-end">
                                                <t t-esc="'%.2f' % task.planned_hours"/>
                                            </td>
                                            <td class="text-end">
                                                <t t-esc="'%.2f' % task.effective_hours"/>
                                            </td>
                                            <td class="text-end">
                                                <t t-esc="'%.1f' % task.hours_progress"/>%
                                            </td>
                                            <td class="text-end">
                                                <t t-esc="'%.2f' % overrun"/>
                                            </td>
                                            <td class="text-end">
                                                <t t-esc="'%.2f' % billable_h"/>
                                            </td>
                                        </tr>
                                    </t>
                                </tbody>
                                <tfoot>
                                    <tr class="fw-bold">
                                        <td>Total</td>
                                        <td class="text-end">
                                            <t t-esc="'%.2f' % sum(tasks.mapped('planned_hours'))"/>
                                        </td>
                                        <td class="text-end">
                                            <t t-esc="'%.2f' % sum(tasks.mapped('effective_hours'))"/>
                                        </td>
                                        <td></td>
                                        <td class="text-end">
                                            <t t-esc="'%.2f' % sum([max(0, t.effective_hours - t.planned_hours) for t in tasks])"/>
                                        </td>
                                        <td class="text-end">
                                            <t t-esc="'%.2f' % sum([sum(t.timesheet_ids.filtered('is_billable').mapped('unit_amount')) for t in tasks])"/>
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </t>
                </t>
            </t>
        </template>

        <!-- ============================================================
             Office Target Report Template (S15)
        ============================================================ -->

        <template id="report_office_target">
            <t t-call="web.html_container">
                <t t-call="web.external_layout">
                    <div class="page">
                        <h2>Office Target Report</h2>
                        <t t-if="data.get('date_from') or data.get('date_to')">
                            <p>
                                Period:
                                <t t-esc="data.get('date_from', '')"/>
                                –
                                <t t-esc="data.get('date_to', '')"/>
                            </p>
                        </t>
                        <table class="table table-sm table-bordered">
                            <thead class="table-light">
                                <tr>
                                    <th>Employee</th>
                                    <th class="text-end">Target (h)</th>
                                    <th class="text-end">Billed (h)</th>
                                    <th class="text-end">% Achieved</th>
                                </tr>
                            </thead>
                            <tbody>
                                <t t-foreach="employee_data" t-as="row">
                                    <tr t-att-class="'table-success' if row['progress'] >= 100 else ('table-warning' if row['progress'] >= 75 else 'table-danger')">
                                        <td><t t-esc="row['employee'].name"/></td>
                                        <td class="text-end">
                                            <t t-esc="'%.2f' % row['target_hours']"/>
                                        </td>
                                        <td class="text-end">
                                            <t t-esc="'%.2f' % row['billed_hours']"/>
                                        </td>
                                        <td class="text-end">
                                            <t t-esc="'%.1f' % row['progress']"/>%
                                        </td>
                                    </tr>
                                </t>
                            </tbody>
                        </table>
                    </div>
                </t>
            </t>
        </template>

        <!-- ============================================================
             My Target Report Template (S15)
        ============================================================ -->

        <template id="report_my_target">
            <t t-call="web.html_container">
                <t t-call="web.external_layout">
                    <div class="page">
                        <h2>My Target Report</h2>
                        <t t-if="employee">
                            <h4><t t-esc="employee.name"/></h4>
                        </t>
                        <t t-if="data.get('date_from') or data.get('date_to')">
                            <p>
                                Period:
                                <t t-esc="data.get('date_from', '')"/>
                                –
                                <t t-esc="data.get('date_to', '')"/>
                            </p>
                        </t>
                        <div class="row mb-4">
                            <div class="col-4">
                                <div class="card text-center">
                                    <div class="card-body">
                                        <h5 class="card-title">Target Hours</h5>
                                        <p class="card-text fs-4">
                                            <t t-esc="'%.2f' % target_hours"/>
                                        </p>
                                    </div>
                                </div>
                            </div>
                            <div class="col-4">
                                <div class="card text-center">
                                    <div class="card-body">
                                        <h5 class="card-title">Billed Hours</h5>
                                        <p class="card-text fs-4">
                                            <t t-esc="'%.2f' % billed_hours"/>
                                        </p>
                                    </div>
                                </div>
                            </div>
                            <div class="col-4">
                                <div class="card text-center">
                                    <div class="card-body">
                                        <h5 class="card-title">% Achieved</h5>
                                        <p class="card-text fs-4">
                                            <t t-esc="'%.1f' % progress"/>%
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <h5>Timesheet Entries</h5>
                        <table class="table table-sm table-bordered">
                            <thead class="table-light">
                                <tr>
                                    <th>Date</th>
                                    <th>Project</th>
                                    <th>Task</th>
                                    <th>Description</th>
                                    <th class="text-end">Hours</th>
                                    <th>Invoiced</th>
                                </tr>
                            </thead>
                            <tbody>
                                <t t-foreach="line_data" t-as="ld">
                                    <tr>
                                        <td><t t-esc="ld['date']"/></td>
                                        <td><t t-esc="ld['project']"/></td>
                                        <td><t t-esc="ld['task']"/></td>
                                        <td><t t-esc="ld['description']"/></td>
                                        <td class="text-end">
                                            <t t-esc="'%.2f' % ld['hours']"/>
                                        </td>
                                        <td>
                                            <t t-if="ld['invoiced']">Yes</t>
                                            <t t-else="">No</t>
                                        </td>
                                    </tr>
                                </t>
                            </tbody>
                        </table>
                    </div>
                </t>
            </t>
        </template>

    </data>
</odoo>
