{# project \App\Entity\Project #}
{# details \App\Reporting\ProjectDetails\ProjectDetailsModel #}
{% macro project_budget(project, details, prefix) %}
    {% set chartPrefix = (prefix is null ? random() : prefix) ~ 'Budget' %}
    {% set chart = is_granted('budget', project) %}
    {% set showMoneyBudget = is_granted('budget', project) and project.hasBudget() %}
    {% set showTimeBudget = is_granted('time', project) and project.hasTimeBudget() %}
    {% if showMoneyBudget or showTimeBudget %}
        {% from "macros/charts.html.twig" import bar_chart %}
        {% set currency = project.customer.currency %}
        {% if project.isMonthlyBudget() %}
            <h4>
                {%- if showMoneyBudget %}{{ 'budget'|trans }}{% else %}{{ 'timeBudget'|trans }}{% endif -%}
                : {{ 'budgetType_month'|trans }}
            </h4>
            {% set chartData = [] %}
            {% set chartLabels = [] %}
            {# year \App\Model\Statistic\Year #}
            {% for year in details.years %}
                {# month \App\Model\Statistic\Month #}
                {% for month in year.months %}
                    {% set monthDate = date(year.year ~ '-' ~ month.month ~ '-01 00:00:00') %}
                    {% if project.end is null or monthDate < project.end %}
                        {% set totalBudget = project.getTimeBudget() - month.billableDuration %}
                        {% set projectBudget = project.getTimeBudget() %}
                        {% if showMoneyBudget %}
                            {% set totalBudget = project.getBudget() - month.billableRate %}
                            {% set projectBudget = project.getBudget() %}
                        {% endif %}
                        {% set chartLabels = chartLabels|merge([monthDate|month_name ~ ' ' ~ year.year]) %}
                        {% set chartValue = {
                            'label': (showMoneyBudget ? totalBudget|money(currency) : totalBudget|duration),
                            'value': '' ~ (showMoneyBudget ? totalBudget|chart_money : totalBudget|chart_duration),
                        } %}
                        {% if totalBudget < 0 %}
                            {% set chartValue = chartValue|merge({'color': 'red'}) %}
                        {% endif %}
                        {% if totalBudget == projectBudget %}
                            {% set chartValue = chartValue|merge({'color': 'green'}) %}
                        {% endif %}
                        {% set chartData = chartData|merge([chartValue]) %}
                    {% endif %}
                {% endfor %}
            {% endfor %}
            {{ bar_chart(chartPrefix, chartLabels, [chartData], {'height': '300px', 'renderEvent': 'render.' ~ chartPrefix}) }}
        {% else %}
            <h4>
                {%- if showMoneyBudget %}{{ 'budget'|trans }}{% else %}{{ 'timeBudget'|trans }}{% endif -%}
            </h4>
            {% set chartData = [] %}
            {% set chartLabels = [] %}
            {% if showMoneyBudget %}
                {% set totalBudget = project.getBudget() %}
            {% else %}
                {% set totalBudget = project.getTimeBudget() %}
            {% endif %}
            {# year \App\Model\Statistic\Year #}
            {% for year in details.years %}
                {# month \App\Model\Statistic\Month #}
                {% for month in year.months %}
                    {% set monthDate = date(year.year ~ '-' ~ month.month ~ '-01') %}
                    {% if project.end is null or monthDate < project.end %}
                        {% if showMoneyBudget %}
                            {% set totalBudget = totalBudget - month.billableRate %}
                        {% else %}
                            {% set totalBudget = totalBudget - month.billableDuration %}
                        {% endif %}
                        {% set chartLabels = chartLabels|merge([monthDate|month_name ~ ' ' ~ year.year]) %}
                        {% set chartValue = {
                            'label': (showMoneyBudget ? totalBudget|money(currency) : totalBudget|duration),
                            'value': (showMoneyBudget ? totalBudget|chart_money : totalBudget|chart_duration),
                        } %}
                        {% if totalBudget < 0 %}
                            {% set chartValue = chartValue|merge({'color': 'red'}) %}
                        {% endif %}
                        {% set chartData = chartData|merge([chartValue]) %}
                    {% endif %}
                {% endfor %}
            {% endfor %}
            {% set last = chartData|last %}
            {% if last is not empty and last.value > 0 %}
                {% set chartValueZero = {
                    'label': (showMoneyBudget ? 0|money(currency) : 0|duration),
                    'value': (showMoneyBudget ? 0|chart_money : 0|chart_duration),
                } %}
                {% set chartData = chartData|merge([chartValueZero]) %}
            {% endif %}
            {{ bar_chart(chartPrefix, chartLabels, [chartData], {'height': '300px', 'renderEvent': 'render.' ~ chartPrefix, 'type': 'line'}) }}
        {% endif %}
    {% endif %}
{% endmacro %}
