{% set showUserColumn = showUserColumn ?? true %}
{% set showInternalRate = showInternalRate ?? false %}
{% set showRateColumn = showRateColumn ?? is_granted('view_rate_other_timesheet') %}
{% set showHourlyRateColumn = showRateColumn and (showHourlyRateColumn ?? false) %}
{% set showSummaryHourlyRate = showRateColumn and (showSummaryHourlyRate ?? false) %}
{% set showRateBudget = showRateBudget ?? false %}
{% set showTimeBudget = showTimeBudget ?? false %}
{% set showCustomerSummary = showCustomerSummary ?? true %}
{% set showTotalSummary = showTotalSummary ?? true %}
{% set showDateTimeShort = showDateTimeShort ?? false %}
{% set now = create_date('now', app.user) %}
{% set decimal = decimal ?? false %}
{# this is only triggered, if a user exports from his personal timesheet screen #}
{% if query is defined and query.user is not null %}
    {% set showUserColumn = false %}
    {# if exporting via the admin screen, users without view_rate_own_timesheet might still see their own rates #}
    {% set showRateColumn = is_granted('view_rate_own_timesheet') %}
{% endif %}
{% set summaryColumns = ['customer', 'project'] %}
{% if showTimeBudget %}
    {% set summaryColumns = summaryColumns|merge(['timeBudget']) %}
{% endif %}
{% if showRateBudget %}
    {% set summaryColumns = summaryColumns|merge(['budget']) %}
{% endif %}
{% if showSummaryHourlyRate %}
    {% set summaryColumns = summaryColumns|merge(['hourlyRate']) %}
{% endif %}
{% set summaryColumns = summaryColumns|merge(['duration']) %}
{% if showRateColumn %}
    {% if showInternalRate %}
        {% set summaryColumns = summaryColumns|merge(['internalRate']) %}
    {% endif %}
    {% set summaryColumns = summaryColumns|merge(['rate']) %}
{% endif %}
<html{% if app.request is defined and app.request is not null %} lang="{{ app.request.locale }}"{% endif %}>
<head>
    <title>{% block document_title %}{{ 'export'|trans }}{% endblock %}</title>
{% block styles %}
    <style>
        {{ encore_entry_css_source('export-pdf')|raw }}
    </style>
{% endblock %}
</head>
<body>
{% block pdf_footer %}
<!--mpdf
<htmlpagefooter name="myfooter">
<table style="border-top: 1px solid #000000; font-size: 9pt; padding-top: 3mm; width: 100%">
<tr>
    <td align="left">
    {{ 'export.page_of'|trans({'%page%': '{PAGENO}', '%pages%': '{nb}'}) }}
    {% if not showUserColumn and query is defined and query.user %}
        &ndash;
        {{ 'user'|trans }}: {{ query.user.displayName }}
    {% endif %}
    </td>
    <td align="right">
    {% set company = config('theme.branding.company') %}
    {% if company is not empty %}
        {{ company }} &ndash; {{ now|date_time }}
    {% else %}
        {{ 'export.date_copyright'|trans({'%date%': now|date_time, '%kimai%': '<a href="' ~ constant('App\\Constants::HOMEPAGE') ~ '">' ~ constant('App\\Constants::SOFTWARE') ~ '</a>'})|raw }}
    {% endif %}
    </td>
</tr>
</table>
</htmlpagefooter>
<sethtmlpagefooter name="myfooter" value="on" />
mpdf-->
{% endblock %}
{% block body_start %}{% endblock %}
{% block summary %}
    {% block summary_header %}
        <h2 style="margin-bottom: 4px; padding-bottom: 0">{% block title %}{{ 'export.document_title'|trans }}{% endblock %}</h2>
        {% if query is defined %}
        <p>
            {{ 'export.period'|trans }}:
            {{ query.begin|date_short }} - {{ query.end|date_short }}
        </p>
        {% endif %}
        <h3>{{ 'export.summary'|trans }}</h3>
    {% endblock %}
    <table class="items">
        <thead>
        <tr>
            {% block summary_head_row_start %}{% endblock %}
            {% for summaryColumn in summaryColumns %}
                {% if summaryColumn == 'customer' %}
                    <th>{{ 'customer'|trans }}</th>
                {% elseif summaryColumn == 'project' %}
                    <th>{{ 'project'|trans }}</th>
                {% else %}
                    <th class="center">{{ summaryColumn|trans }}</th>
                {% endif %}
            {% endfor %}
            {% block summary_head_row_end %}{% endblock %}
        </tr>
        </thead>
        <tbody>
        {% set customer = null %}
        {% set customerDuration = 0 %}
        {% set customerRate = 0 %}
        {% set customerInternalRate = 0 %}
        {% set customerCurrency = null %}
        {% set customerCount = 0 %}
        {% set multiCurrency = false %}
        {% set totalDuration = 0 %}
        {% set totalInternalRate = 0 %}
        {% set totalRate = 0 %}
        {% for id, summary in summaries %}
            <!-- CONTENT_PART -->
            {% set totalDuration = totalDuration + summary.duration %}
            {% set totalInternalRate = totalInternalRate + summary.rate_internal %}
            {% set totalRate = totalRate + summary.rate %}
            {% if customerCurrency is not null and customerCurrency is not same as(summary.currency) %}
                {% set multiCurrency = true %}
            {% endif %}
            {% if customer is same as(null) %}
                {% set customer = summary.customer %}
                {% set customerCurrency = summary.currency %}
            {% endif %}
            {% if customer is not same as(summary.customer) %}
                <tr class="summary">
                    {% block summary_customer_row_start %}{% endblock %}
                    {% for summaryColumn in summaryColumns %}
                        {% if summaryColumn == 'duration' %}
                            <td class="totals duration">{{ customerDuration|duration(decimal) }}</td>
                        {% elseif summaryColumn == 'internalRate' %}
                            <td class="totals cost">{{ customerInternalRate|money(customerCurrency) }}</td>
                        {% elseif summaryColumn == 'rate' %}
                            <td class="totals cost">{{ customerRate|money(customerCurrency) }}</td>
                        {% else %}
                            <td></td>
                        {% endif %}
                    {% endfor %}
                    {% block summary_customer_row_end %}{% endblock %}
                </tr>
                {% set customerCurrency = summary.currency %}
                {% set customer = summary.customer %}
                {% set customerDuration = 0 %}
                {% set customerRate = 0 %}
                {% set customerInternalRate = 0 %}
                {% set customerCount = 0 %}
            {% endif %}
            <tr class="{{ cycle(['odd', 'even'], customerCount) }}">
                {% block summary_project_row_start %}{% endblock %}
                {% for summaryColumn in summaryColumns %}
                    {% if summaryColumn == 'customer' %}
                        <td>{{ summary.customer }}</td>
                    {% elseif summaryColumn == 'project' %}
                        <td>{{ summary.project }}</td>
                    {% elseif summaryColumn == 'timeBudget' %}
                        <td class="center">
                            {% if budgets[id] is defined and budgets[id].time_left > 0 %}
                                {{ budgets[id].time_left|duration(decimal) }}
                            {% endif %}
                        </td>
                    {% elseif summaryColumn == 'budget' %}
                        <td class="center">
                            {% if budgets[id] is defined and budgets[id].money_left > 0 %}
                                {{ budgets[id].money_left|money(summary.currency) }}
                            {% endif %}
                        </td>
                    {% elseif summaryColumn == 'hourlyRate' %}
                        {% set summaryHourlyRate = summary.duration/3600 %}
                        <td class="cost">{{ summaryHourlyRate > 0 ? (summary.rate/summaryHourlyRate)|money(summary.currency) : '' }}</td>
                    {% elseif summaryColumn == 'duration' %}
                        <td class="duration">{{ summary.duration|duration(decimal) }}</td>
                    {% elseif summaryColumn == 'internalRate' %}
                        <td class="cost">{{ summary.rate_internal|money(summary.currency) }}</td>
                    {% elseif summaryColumn == 'rate' %}
                        <td class="cost">{{ summary.rate|money(summary.currency) }}</td>
                    {% endif %}
                {% endfor %}
                {% block summary_project_row_end %}{% endblock %}
            </tr>
            {% set customerDuration = customerDuration + summary.duration %}
            {% set customerRate = customerRate + summary.rate %}
            {% set customerInternalRate = customerInternalRate + summary.rate_internal %}
            {% set customerCount = customerCount + 1 %}
        {% endfor %}
        {% if showCustomerSummary and customer is not same as(null) %}
        <tr class="summary">
            {% block summary_customer_summary_row_start %}{% endblock %}
            {% for summaryColumn in summaryColumns %}
                {% if summaryColumn == 'duration' %}
                    <td class="totals duration">{{ customerDuration|duration(decimal) }}</td>
                {% elseif summaryColumn == 'internalRate' %}
                    <td class="totals cost">{{ customerInternalRate|money(customerCurrency) }}</td>
                {% elseif summaryColumn == 'rate' %}
                    <td class="totals cost">{{ customerRate|money(customerCurrency) }}</td>
                {% else %}
                    <td></td>
                {% endif %}
            {% endfor %}
            {% block summary_customer_summary_row_end %}{% endblock %}
        </tr>
        {% endif %}
        {% if showTotalSummary and not multiCurrency %}
        <tr class="{% if not showCustomerSummary %}summary{% else %}summary-total{% endif %}">
            {% block summary_summary_row_start %}{% endblock %}
            <td class="totals" colspan="2">
                {{ 'sum.total'|trans }}
            </td>
            {% for summaryColumn in summaryColumns %}
                {% if summaryColumn == 'duration' %}
                    <td class="totals duration">{{ totalDuration|duration(decimal) }}</td>
                {% elseif summaryColumn == 'internalRate' %}
                        <td class="totals cost">{{ totalInternalRate|money(customerCurrency) }}</td>
                {% elseif summaryColumn == 'rate' %}
                    <td class="totals cost">{{ totalRate|money(customerCurrency) }}</td>
                {% elseif summaryColumn not in ['customer', 'project'] %}
                    <td></td>
                {% endif %}
            {% endfor %}
            {% block summary_summary_row_end %}{% endblock %}
        </tr>
        {% endif %}
        </tbody>
    </table>

    <pagebreak>
{% endblock %}
{% block items %}
    {% block items_header %}
        <h3>{{ 'export.full_list'|trans }}</h3>
    {% endblock %}

    {% set duration = 0 %}
    {% set rate = 0 %}
    {% set rateInternal = 0 %}
    {% set currency = false %}
    <table class="items">
        <thead>
        <tr>
            {% block items_head_row_start %}{% endblock %}
            <th>{{ 'date'|trans }}</th>
            {% if showUserColumn %}
                <th>{{ 'user'|trans }}</th>
            {% endif %}
            <th>{{ 'description'|trans }}</th>
            {% if showHourlyRateColumn %}
                <th class="center">{{ 'hourlyRate'|trans }}</th>
            {% endif %}
            <th class="center">{{ 'duration'|trans }}</th>
            {% if showRateColumn %}
                {% if showInternalRate %}
                    <th class="center">{{ 'internalRate'|trans }}</th>
                {% endif %}
                <th class="center">{{ 'rate'|trans }}</th>
            {% endif %}
            {% block items_head_row_end %}{% endblock %}
        </tr>
        </thead>
        <tbody>
        {% for entry in entries %}
            <!-- CONTENT_PART -->
            {% set duration = duration + entry.duration %}
            {% set entryCurrency = entry.project.customer.currency %}
            {% if currency is same as(false) %}
                {% set currency = entryCurrency %}
            {% endif %}
            {% if currency is not same as(entryCurrency) %}
                {% set currency = null %}
            {% endif %}
            <tr class="{{ cycle(['odd', 'even'], loop.index0) }}">
                {% block items_entry_row_start %}{% endblock %}
                <td class="text-nowrap">
                    {% block date_begin %}{% if not showDateTimeShort %}{{ entry.begin|date_time }}{% else %}{{ entry.begin|date_short }}{% endif %}{% endblock %}
                    {% if entry.end %}
                        {% block date_end %}{% if not showDateTimeShort %}<br>{{ entry.end|date_time }}{% endif %}{% endblock %}
                    {% endif %}
                </td>
                {% if showUserColumn %}
                    <td>{{ entry.user.displayName }}</td>
                {% endif %}
                <td>
                    {% block description %}
                        {{ entry.project.customer.name }} - {{ entry.project.name }}{% if entry.activity is not null %} - {{ entry.activity.name }}{% endif %}
                        {% if entry.description is not empty %}
                            <br>
                            <i>{{ entry.description|desc2html }}</i>
                        {% endif %}
                    {% endblock %}
                </td>
                {% if showHourlyRateColumn %}
                    <td class="cost">
                        {% if entry.fixedRate %}
                            -
                        {% else %}
                            {{ entry.hourlyRate|money(entryCurrency) }}
                        {% endif %}
                    </td>
                {% endif %}
                <td class="duration">
                    {% if entry.duration == 0 %}
                        -
                    {% else %}
                        {{ entry.duration|duration(decimal) }}
                    {% endif %}
                </td>
                {% if showRateColumn %}
                    {# no check for is_granted('view_rate', entry) because it is only available for timesheets,
                       but maybe missing for other potential export repositories #}
                    {% set rate = rate + entry.rate %}
                    {% set rateInternal = rateInternal + entry.internalRate %}
                    {% set entryRate = entry.rate|money(entryCurrency) %}
                    {% set entryRateInternal = entry.internalRate|money(entryCurrency) %}
                    {% if showInternalRate %}
                        <td class="cost">{{ entryRateInternal }}</td>
                    {% endif %}
                    <td class="cost">{{ entryRate }}</td>
                {% endif %}
                {% block items_entry_row_end %}{% endblock %}
            </tr>
        {% endfor %}
        {% block items_summary %}
        <tr class="summary">
            {% block items_summary_row_start %}{% endblock %}
            {% if showUserColumn %}
                <td colspan="3">{{ 'sum.total'|trans }}</td>
            {% else %}
                <td colspan="2">{{ 'sum.total'|trans }}</td>
            {% endif %}
            {% if showHourlyRateColumn %}
                <td class="totals cost"></td>
            {% endif %}
            <td class="totals duration">{{ duration|duration(decimal) }}</td>
            {% if showRateColumn %}
                {% if showInternalRate %}
                    <td class="totals cost">{% if currency is not null %}{{ rateInternal|money(currency) }}{% endif %}</td>
                {% endif %}
                <td class="totals cost">{% if currency is not null %}{{ rate|money(currency) }}{% endif %}</td>
            {% endif %}
            {% block items_summary_row_end %}{% endblock %}
        </tr>
        {% endblock %}
        </tbody>
    </table>
{% endblock %}
{% block body_end %}{% endblock %}
</body>
</html>
