{% extends "layouts/base.twig" %} {% block title %}{{ translations.analytics_title }}{% endblock %} {% block head %} {% endblock %} {% block page_heading %}

{{ translations.analytics_heading }}

{{ translations.analytics_subtitle }}

{{ translations.logistics ?? 'Logistics' }} {{ translations.rms_menu ?? 'Raw Material Sales' }}
{% endblock %} {% block content %} {% include "page_components/messages.twig" %} {# Period filter pills #}
{% set thisYear = 'now'|date('Y') %} {{ translations.analytics_preset_ytd }} 3M 6M
{# KPI summary row #} {% if revenue is not empty %} {% set totalRevenue = revenue|map(r => r.revenue_net)|reduce((carry, v) => carry + v, 0) %} {% set totalOrders = revenue|map(r => r.order_count)|reduce((carry, v) => carry + v, 0) %} {% set avgBasket = totalOrders > 0 ? (totalRevenue / totalOrders) : 0 %}
{{ translations.analytics_col_revenue }}
{{ totalRevenue|number_format(0, '.', ' ') }}
{{ params.from }} → {{ params.to }}
{{ translations.analytics_col_orders }}
{{ totalOrders }}
{{ translations.analytics_kpi_orders_sub ?? 'total' }}
{{ translations.analytics_kpi_avg_basket ?? 'Avg basket' }}
{{ avgBasket|number_format(0, '.', ' ') }}
{{ translations.analytics_kpi_per_order ?? 'per order' }}
{{ translations.analytics_top_products_title }}
{{ top_products|length }}
{{ translations.analytics_kpi_products_sub ?? 'active products' }}
{% endif %} {# Main chart + shop doughnut #}
{{ translations.analytics_chart_title }}
{% if revenue is empty %}

{{ translations.analytics_no_data }}

{% else %} {% endif %}
{{ translations.analytics_revenue_by_shop }}
{% if shop_revenue is empty %}

{{ translations.analytics_no_data }}

{% else %} {% endif %}
{# Monthly table + Top 5 #}
{{ translations.analytics_table_title }}
{% if revenue is empty %}

{{ translations.analytics_no_data }}

{% else %}
{% set totalRev = revenue|map(r => r.revenue_net)|reduce((carry, v) => carry + v, 0) %} {% set prevRevenue = 0 %} {% for row in revenue %} {% set pct = totalRev > 0 ? ((row.revenue_net / totalRev) * 100)|round(1) : 0 %} {% set prevRevenue = row.revenue_net %} {% endfor %} {% if revenue|length > 1 %} {% endif %}
{{ translations.analytics_col_period }} {{ translations.analytics_col_orders }} {{ translations.analytics_col_revenue }} %
{{ row.period }} {{ row.order_count }} {{ row.revenue_net|number_format(2, '.', ' ') }} {% if prevRevenue > 0 %} {% if row.revenue_net > prevRevenue %} {% elseif row.revenue_net < prevRevenue %} {% endif %} {% endif %} {{ pct }}%
Total {{ revenue|map(r => r.order_count)|reduce((carry, v) => carry + v, 0) }} {{ totalRev|number_format(2, '.', ' ') }} 100%
{% endif %}
{{ translations.analytics_top_products_title }}
{% if top_products is empty %}

{{ translations.analytics_no_data }}

{% else %} {% set maxQty = top_products[0].total_qty %} {% for i, product in top_products %}
{% if i == 0 %}
1
{% elseif i == 1 %}
2
{% elseif i == 2 %}
3
{% else %}
{{ i + 1 }}
{% endif %}
{{ product.material_name ?? '—' }}
{{ product.total_qty|number_format(0, '.', ' ') }} u.
{% endfor %} {% endif %}
{% endblock %} {% block scripts %} {{ parent() }} {% if revenue is not empty or shop_revenue is not empty %} {% endif %} {% endblock %}