{% extends "timepiece/time-sheet/reports/base.html" %}
{% load timepiece_tags bootstrap_toolkit %}
{% load url from future %}

{% block title %}Payroll Summary Report{% endblock %}

{% block crumbs %}
    {{ block.super }}
    <li><span class="divider">/</span> <a href="{% url 'payroll_summary' %}">Payroll Summary Report</a></li>
{% endblock crumbs %}

{% block report_nav %}
    {% with 'payroll_summary' as active %}
        {{ block.super }}
    {% endwith %}
{% endblock %}

{% block report_content %}
    <div class="row-fluid">
        <div class="span12">
            <form class="form-inline" method="get" action="" id="date-filter" accept-charset="utf-8">
                {{ year_month_form|as_bootstrap:"inline" }}
                <input class="btn btn-primary" type='submit' name='yearmonth' value="Update" id='yearmonth'/>
            </form>
        </div>
    </div>

    <div class="row-fluid">
        <div class="span6">
            {% if unverified %}
                <p><strong>Warning:</strong> The following users have <strong>unverified</strong> time during this period:</p>
                <ul>
                    {% for pk, first, last in unverified %}
                        <li>
                            <a href="{% timesheet_url 'user' pk from_date %}">{{ first }} {{ last }}</a>
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}
            {% if unapproved %}
                <p><strong>Warning:</strong> The following users have <strong>unapproved</strong> time during this period:</p>
                <ul>
                    {% for pk, first, last in unapproved %}
                        <li>
                            <a href="{% timesheet_url 'user' pk from_date %}">{{ first }} {{ last }}</a>
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}
            {% if not unverified and not unapproved %}
                <p>All time verified and approved</p>
            {% endif %}
        </div>
    </div>

    <div class="row-fluid">
        <div class="span12">
            <h3>Hourly Summary</h3>
            {% if monthly_totals %}
                <table class="table table-bordered table-striped table-condensed">
                    <thead>
                        <tr>
                            <th rowspan="2">Name</th>
                            {% if labels.billable %}
                                <th colspan="{{ labels.billable|length|multiply:'2' }}">Billable Projects</th>
                                <th colspan="2" rowspan="2">Total Billable Hours</th>
                            {% endif %}
                            {% if labels.nonbillable %}
                                <th colspan="{{ labels.nonbillable|length|multiply:'2' }}">Non-Billable Projects</th>
                                <th colspan="2" rowspan="2">Total Non-Billable Hours</th>
                            {% endif %}
                            <th rowspan="2">Total Worked Hours</th>
                            {% if labels.leave %}
                                <th colspan="{{ labels.leave|length }}">Paid Leave</th>
                                <th rowspan="2">Total Leave Hours</th>
                            {% endif %}
                            <th rowspan="2">Grand Total</th>
                        </tr>
                        <tr>
                            {% for label in labels.billable %}
                                <th colspan=2>{{ label }}</th>
                            {% endfor %}
                            {% for label in labels.nonbillable %}
                                <th colspan=2>{{ label }}</th>
                            {% endfor %}
                            {% for label in labels.leave %}
                                <th>{{ label }}</th>
                            {% endfor %}
                        </tr>
                    </thead>
                    <tbody>
                        {% for row in monthly_totals %}
                            <tr>
                                <th class='name_col'>
                                    {% if row.user_id %}
                                        <a href="{% timesheet_url 'user' row.user_id from_date %}">{{ row.name }}</a>
                                    {% else %}
                                        {{ row.name }}
                                    {% endif %}
                                </th>
                                {% if labels.billable %}
                                    {% for entry in row.billable %}
                                        <td class="hours">{% if entry.hours %}{{ entry.hours|floatformat:2 }}{% endif %}</td>
                                        <td class="hours">{% if entry.percent %}{{ entry.percent|floatformat:1 }}%{% endif %}</td>
                                    {% endfor %}
                                {% endif %}
                                {% if labels.nonbillable %}
                                    {% for entry in row.nonbillable %}
                                        <td class="hours">{% if entry.hours %}{{ entry.hours|floatformat:2 }}{% endif %}</td>
                                        <td class="hours">{% if entry.percent %}{{ entry.percent|floatformat:1 }}%{% endif %}</td>
                                    {% endfor %}
                                {% endif %}
                                <td class="hours">
                                    {% if row.work_total %}{{ row.work_total|floatformat:2 }}{% endif %}
                                </td>
                                {% if labels.leave %}
                                    {% for entry in row.leave %}
                                        <td class="hours">{% if entry.hours %}{{ entry.hours|floatformat:2 }}{% endif %}</td>
                                    {% endfor %}
                                {% endif %}
                                <td class="hours">
                                    {% if row.grand_total %}{{ row.grand_total|floatformat:2 }}{% endif %}
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            {% else %}
                <p> No entries found.</p>
            {% endif %}
        </div>
    </div>

    <div class="row-fluid">
        <div class="span12">
            <h3>Weekly Summary</h3>
            {% if weekly_totals.0.0 %}
                <table class='table table-striped table-bordered table-condensed'>
                    <thead>
                        <tr>
                            <th class='name_col'>Name</th>
                            {% for date in date_headers %}
                                <th>{{date|date:"M/d"}}</th>
                            {% endfor %}
                            <th>Overtime</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for rows, totals in weekly_totals %}
                            {% for name, user_id, hours in rows %}
                                <tr>
                                    <th class='name_col'>
                                        {% if user_id %}
                                            <a href="{% timesheet_url 'user' user_id from_date %}">{{ name }}</a>
                                        {% else %}
                                            {{ name }}
                                        {% endif %}
                                    </th>
                                    {% for num in hours %}
                                        <td class='hours' title='{{name}}'>{{num|floatformat:2}}</td>
                                    {% endfor %}
                                </tr>
                            {% endfor %}
                        {% endfor %}
                    </tbody>
                </table>
                {% else %}
                    <p>No entries found.</p>
            {% endif %}
        </div>
    </div>
{% endblock report_content %}

