ldc-groupe/app/Helpers/FinancePageChartHelper.php

340 lines
11 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Helpers;
class FinancePageChartHelper
{
/**
* Extracts labels and values for the "Évolution du chiffre d’affaires" line chart.
*
* @param array $item Raw data for the chart.
* @return array{labels: array, values: array} Normalized chart data.
*/
public static function getAffairEvolution(array $item): array
{
$list = $item['list_affair_values'] ?? [];
return [
'labels' => array_column($list, 'string_label'),
'values' => array_map('floatval', array_column($list, 'string_value')),
];
}
/**
* Extracts labels and values for the "Répartition du chiffre d’affaires" pie chart.
*
* @param array $item Raw data for the chart.
* @return array{labels: array, values: array} Normalized chart data.
*/
public static function getRepartitionAffair(array $item): array
{
$list = $item['list_last_repartition_affair_values'] ?? [];
return [
'labels' => array_column($list, 'string_label'),
'values' => array_map('floatval', array_column($list, 'string_value')),
];
}
/**
* Extracts active and passive values for the stacked bar chart (Bilan Simplifié).
*
* @param array $item Raw data for the chart.
* @return array{labels: array, datasets: array} Normalized stacked datasets.
*/
public static function getBilanSimplify(array $item): array
{
2026-02-10 15:43:47 +01:00
$active = $item['list_bilan_simplify_active_values'] ?? [];
$passive = $item['list_bilan_simplify_passive_values'] ?? [];
$datasets = [];
foreach ($active as $a) {
$datasets[] = [
'label' => $a['string_label_active_simplify'],
'data' => [floatval($a['string_value_active_simplify'])],
'stack' => 'Actif'
];
}
foreach ($passive as $p) {
$datasets[] = [
'label' => $p['string_label_passive_simplify'],
'data' => [floatval($p['string_value_passive_simplify'])],
'stack' => 'Passif'
];
}
return [
'labels' => [$item['string_label']],
'datasets' => $datasets
];
}
/**
* Extracts labels and values for the autofinancing mixed chart.
*
* @param array $item Raw data for the chart.
* @return array{labels: array, cpt: array, ivt: array} CAF and investment values.
*/
public static function getAutofinancement(array $item): array
{
2026-02-10 15:43:47 +01:00
$labels = [];
$valuesCPT = [];
$valuesIVT = [];
foreach ($item['list_autofinancement_values'] as $row) {
$labels[] = $row['string_autofinancement_label'];
$inner = $row['list_autofinancement_values'][0];
$valuesCPT[] = floatval($inner['string_autofinancement_capabilities']);
$valuesIVT[] = floatval($inner['string_industrial_investissment']);
}
return [
'labels' => $labels,
'cpt' => $valuesCPT,
'ivt' => $valuesIVT,
];
}
/**
* Routes chart building based on chart type.
*
* @param string $type Chart identifier.
2026-02-10 15:43:47 +01:00
* @param array $data Raw chart data.
* @return array Chart.js-compatible configuration.
*/
public static function buildChart(string $type, array $data): array
{
switch ($type) {
case 'list_affair':
return self::buildAffairEvolution($data);
case 'list_last_repartition_affair':
return self::buildRepartitionAffair($data);
case 'list_bilan_simplify':
return self::buildBilanSimplify($data);
case 'capacite_dautofinancement':
return self::buildAutofinancement($data);
default:
return [];
}
}
/**
* Builds a line chart configuration for the revenue evolution.
*
* @param array $item Parsed chart data.
* @return array Chart.js configuration.
*/
private static function buildAffairEvolution(array $item): array
{
$data = self::getAffairEvolution($item);
return [
'type' => 'line',
'data' => [
'labels' => $data['labels'],
'datasets' => [[
'label' => $item['string_label'],
'data' => $data['values'],
'borderColor' => 'rgba(54, 162, 235, 0.2)',
'backgroundColor' => 'rgba(54, 162, 235, 1)',
'borderWidth' => 1,
]]
],
'options' => [
'responsive' => true,
'elements' => [
'point' => [
'radius' => 12,
'hitRadius' => 14,
'hoverRadius' => 14,
'pointStyle' => 'rect'
],
'line' => [
'borderWidth' => 0,
'fill' => 'rect'
]
],
'legend' => ['display' => false]
]
];
}
/**
* Builds a pie chart configuration for CA repartition.
*
* @param array $item Parsed chart data.
* @return array Chart.js configuration.
*/
private static function buildRepartitionAffair(array $item): array
{
$data = self::getRepartitionAffair($item);
return [
'type' => 'pie',
'data' => [
'labels' => $data['labels'],
'datasets' => [[
'label' => $item['string_label'],
'data' => $data['values'],
'backgroundColor' => [
'rgba(85, 154, 213, 1)',
'rgba(64, 114, 203, 1)',
'rgba(242, 125, 32, 1)',
'rgba(165, 165, 165, 1)',
'rgba(255, 192, 0, 1)',
'rgba(75, 192, 192, 1)',
],
'borderColor' => [
'rgba(85, 154, 213, 0.2)',
'rgba(64, 114, 203, 0.2)',
'rgba(242, 125, 32, 0.2)',
'rgba(165, 165, 165, 0.2)',
'rgba(255, 192, 0, 0.2)',
'rgba(75, 192, 192, 0.2)',
],
'borderWidth' => 1
]]
],
'options' => [
'scales' => [
'y' => ['beginAtZero' => true]
],
'tooltips' => [
'callbacks' => [
'label' => 'function() { return null; }'
]
]
]
];
}
/**
* Builds a stacked bar chart for the simplified balance sheet (Actif vs Passif).
*
* @param array $item Parsed chart data.
* @return array Chart.js configuration.
*/
private static function buildBilanSimplify(array $item): array
{
$colors = [
'rgb(90, 155, 211)',
'rgb(255, 170, 0)',
'rgb(165, 165, 165)',
'rgb(238, 126, 52)',
'rgb(69, 116, 198)',
'rgb(113, 174, 71)',
'rgb(93, 169, 156)',
'rgb(253, 199, 0)',
'rgb(165, 165, 165)',
'rgb(238, 166, 52)',
'rgb(69, 80, 178)',
];
$i = 0;
$datasetActif = array_map(function ($a) use ($colors, &$i) {
return [
'label' => $a['string_label_active_simplify'],
'data' => [floatval($a['string_value_active_simplify'])],
'stack' => 'Actif',
'backgroundColor' => $colors[$i++ % count($colors)]
];
}, $item['list_bilan_simplify_active_values'] ?? []);
$datasetPassif = array_map(function ($a) use ($colors, &$i) {
return [
'label' => $a['string_label_passive_simplify'],
'data' => [floatval($a['string_value_passive_simplify'])],
'stack' => 'Passif',
'backgroundColor' => $colors[$i++ % count($colors)]
];
}, $item['list_bilan_simplify_passive_values'] ?? []);
$labels = ['Actif Passif'];
return [
'type' => 'bar',
'data' => [
'labels' => $labels,
'datasets' => array_merge($datasetActif, $datasetPassif)
],
'options' => [
'responsive' => true,
'scales' => [
'x' => ['stacked' => true],
'y' => ['stacked' => true]
],
'plugins' => [
'legend' => ['position' => 'bottom']
],
'interaction' => [
'mode' => 'index',
'intersect' => false
],
'tooltips' => [
'callbacks' => [
'title' => 'function() { return null; }'
]
]
]
];
}
/**
* Builds a mixed chart combining bars (CAF) and line (Investissements).
*
* @param array $item Parsed chart data.
* @return array Chart.js configuration.
*/
private static function buildAutofinancement(array $item): array
{
$data = self::getAutofinancement($item);
return [
'type' => 'bar',
'data' => [
'labels' => $data['labels'],
'datasets' => [
[
'type' => 'line',
'label' => "Investissements industriels",
'data' => $data['ivt'],
'borderColor' => 'rgba(231, 122, 37, 1)',
'borderWidth' => 2,
'tension' => 0.3,
'order' => 1
],
[
'type' => 'bar',
'label' => "Capacité d'autofinancement",
'data' => $data['cpt'],
'backgroundColor' => 'rgba(64, 114, 203, 1)',
'borderColor' => 'rgba(64, 114, 203, 0.2)',
'borderWidth' => 1,
'order' => 0
]
]
],
'options' => [
'responsive' => true,
'point' => [
'radius' => 6,
'hitRadius' => 8,
'hoverRadius' => 8,
],
'legend' => [
'position' => 'bottom'
],
]
];
}
}