Files
kimai2/templates/project/charts.html.twig
Kevin Papst 0dba83d8e9 Budget graph in project details (#3406)
* do not copy description if restarting via calendar
* added support to display negative durations
* fix charts for larger numbers
2022-07-13 17:21:36 +02:00

87 lines
4.8 KiB
Twig

{# 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 %}{{ 'label.budget'|trans }}{% else %}{{ 'label.timeBudget'|trans }}{% endif -%}
: {{ 'label.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 %}{{ 'label.budget'|trans }}{% else %}{{ 'label.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),
} %}
{# line chart do not support coloring negative areas #}
{#
{% if totalBudget < 0 %}
{% set chartValue = chartValue|merge({'color': 'red'}) %}
{% endif %}
#}
{% set chartData = chartData|merge([chartValue]) %}
{% endif %}
{% endfor %}
{% endfor %}
{{ bar_chart(chartPrefix, chartLabels, [chartData], {'height': '300px', 'renderEvent': 'render.' ~ chartPrefix, 'type': 'line'}) }}
{% endif %}
{% endif %}
{% endmacro %}