{% set columns = {
    'customer':         {'title': 'customer'|trans},
    'name':             {'title': 'name'|trans},
    'currency':         {'title': 'currency'|trans},
    'budgetType':       {'title': 'budgetType'|trans},
} %}
{% if is_granted('budget_time', 'project') %}
    {% set columns = columns|merge({
        'timeBudget': {'title': 'timeBudget'|trans},
        'durationTotal': {'title': 'stats.durationTotal'|trans},
        'billableTime': {'title': 'billable'|trans},
        'exported': {'title': 'not_exported'|trans},
    }) %}
{% endif %}
{% if is_granted('budget_money', 'project') %}
    {% set columns = columns|merge({
        'budget': {'title': 'budget'|trans},
        'amountTotal': {'title': 'stats.amountTotal'|trans},
        'billableMoney': {'title': 'billable'|trans},
        'internalRate': {'title': 'internalRate'|trans},
        'revenue': {'title': ('revenue'|trans ~ ' (' ~ 'billable'|trans ~ ' - ' ~ 'internalRate'|trans ~ ')')},
        'hourlyBillable': {'title': ('hourlyRate'|trans ~ ' (' ~ 'billable'|trans ~ ' / ' ~  'stats.durationTotal'|trans ~ ')')},
        'hourlyTotal': {'title': ('hourlyRate'|trans ~ ' (' ~ 'revenue'|trans ~ ' / ' ~ 'stats.durationTotal'|trans ~ ')')},
        'invoiced': {'title': 'not_invoiced'|trans},
    }) %}
{% endif %}
<table>
    <thead>
        <tr>
        {% for column, settings in columns %}
            <th>{{ settings.title }}</th>
        {% endfor %}
        </tr>
    </thead>
    <tbody>
        {% for id, mapping in entries|sort((a, b) => a.customer.name <=> b.customer.name) %}
            {% for entry in mapping.projects|sort((a, b) => a.project.name <=> b.project.name) %}
                <tr>
                {% set project = entry.project %}
                {% set budgetStatisticModel = entry.getBudgetStatisticModel() %}
                {% set currency = project.customer.currency %}
                {% set durationTotal = entry.durationTotal|chart_duration %}
                {% set canSeeTimeBudget = is_granted('time', project) %}
                {% set showTimeBudget = project.hasTimeBudget() and canSeeTimeBudget %}
                {% set canSeeMoneyBudget = is_granted('budget', project) %}
                {% set showMoneyBudget = project.hasBudget() and canSeeMoneyBudget %}
                {% set revenue = budgetStatisticModel.getBudgetSpent() - budgetStatisticModel.getInternalRate() %}
                {% for name, column_config in columns %}
                    <td>
                    {% if name == 'name' %}
                        {{ project.name }}
                    {% elseif name == 'budgetType' %}
                        {% if project.budgetType is not null %}
                            {{ ('budgetType_' ~ project.budgetType)|trans }}
                        {% endif %}
                    {% elseif name == 'customer' %}
                        {{ project.customer.name }}
                    {% elseif name == 'currency' %}
                        {{ currency }}
                    {% elseif name == 'lastRecord' %}
                        {% if entry.lastRecord is not null %}
                            {{ entry.lastRecord|date_short }}
                        {% else %}
                            &ndash;
                        {% endif %}
                    {% elseif name == 'today' %}
                        {{ entry.durationDay|duration(true) }}
                    {% elseif name == 'week' %}
                        {{ entry.durationWeek|duration(true) }}
                    {% elseif name == 'month' %}
                        {{ entry.durationMonth|duration(true) }}
                    {% elseif name == 'timeBudget' %}
                        {% if showTimeBudget %}
                            {{ budgetStatisticModel.getTimeBudget()|duration(true) }}
                        {% endif %}
                    {% elseif name == 'durationTotal' %}
                        {{ entry.durationTotal|duration(true) }}
                    {% elseif name == 'billableTime' %}
                        {% if canSeeTimeBudget %}
                            {{ budgetStatisticModel.getTimeBudgetSpent()|duration(true) }}
                        {% endif %}
                    {% elseif name == 'exported' %}
                        {% if canSeeTimeBudget %}
                            {{ entry.notExportedDuration|duration(true) }}
                        {% endif %}
                    {% elseif name == 'budget' %}
                        {% if showMoneyBudget %}
                            {{ budgetStatisticModel.getBudget()|money }}
                        {% endif %}
                    {% elseif name == 'amountTotal' %}
                        {% if canSeeMoneyBudget %}
                            {{ budgetStatisticModel.getStatisticTotal().getRate()|money }}
                        {% endif %}
                    {% elseif name == 'billableMoney' %}
                        {% if canSeeMoneyBudget %}
                            {{ budgetStatisticModel.getBudgetSpent()|money }}
                        {% endif %}
                    {% elseif name == 'internalRate' %}
                        {% if canSeeMoneyBudget %}
                            {{ budgetStatisticModel.getInternalRate()|money }}
                        {% endif %}
                    {% elseif name == 'revenue' %}
                        {% if canSeeMoneyBudget %}
                            {{ revenue|money }}
                        {% endif %}
                    {% elseif name == 'hourlyBillable' %}
                        {% if canSeeMoneyBudget and durationTotal > 0 %}
                            {{ (budgetStatisticModel.getBudgetSpent() / durationTotal)|money }}
                        {% endif %}
                    {% elseif name == 'hourlyTotal' %}
                        {% if canSeeMoneyBudget and durationTotal > 0 %}
                            {{ (revenue / durationTotal)|money }}
                        {% endif %}
                    {% elseif name == 'invoiced' %}
                        {% if canSeeMoneyBudget %}
                            {{ entry.notExportedRate|money }}
                        {% endif %}
                    {% elseif name == 'projectStart' %}
                        {% if project.start is not null %}{{ project.start|date_short }}{% endif %}
                    {% elseif name == 'projectEnd' %}
                        {% if project.end is not null %}{{ project.end|date_short }}{% endif %}
                    {% elseif name == 'comment' %}
                        {{ project.comment }}
                    {% elseif name == 'actions' %}
                        {{ projectActions.project(project, 'custom') }}
                    {% endif %}
                    </td>
                {% endfor %}
                </tr>
            {% endfor %}
        {% endfor %}
    </tbody>
</table>
