Files
firefly-iii/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php

181 lines
4.7 KiB
PHP
Raw Normal View History

<?php
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
namespace FireflyIII\Generator\Chart\Budget;
2015-06-27 20:39:50 +02:00
use Config;
use Illuminate\Support\Collection;
2015-06-27 20:39:50 +02:00
use Preferences;
2015-06-27 17:05:39 +02:00
/**
* Class ChartJsBudgetChartGenerator
*
* @package FireflyIII\Generator\Chart\Budget
*/
class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
{
/**
* @param Collection $entries
* @param string $dateFormat
*
* @return array
*/
2016-02-18 07:21:48 +01:00
public function budget(Collection $entries, $dateFormat = 'month'): array
{
// language:
2015-12-24 08:35:08 +01:00
$language = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US'))->data;
$format = Config::get('firefly.' . $dateFormat . '.' . $language);
2015-06-27 20:39:50 +02:00
$data = [
'labels' => [],
'datasets' => [
[
'label' => 'Amount',
'data' => [],
2015-12-24 08:35:08 +01:00
],
2015-06-27 20:39:50 +02:00
],
];
/** @var array $entry */
foreach ($entries as $entry) {
$data['labels'][] = $entry[0]->formatLocalized($format);
2015-06-27 20:39:50 +02:00
$data['datasets'][0]['data'][] = $entry[1];
}
2015-07-06 17:45:59 +02:00
$data['count'] = count($data['datasets']);
2015-06-27 20:39:50 +02:00
return $data;
}
/**
2015-06-28 12:41:58 +02:00
* @codeCoverageIgnore
2015-06-28 18:00:11 +02:00
*
* @param Collection $entries
*
* @return array
*/
2016-02-18 07:21:48 +01:00
public function budgetLimit(Collection $entries): array
{
return $this->budget($entries, 'monthAndDay');
}
/**
* @param Collection $entries
*
* @return array
*/
2016-02-18 07:21:48 +01:00
public function frontpage(Collection $entries): array
{
2016-01-02 16:57:31 +01:00
$data = [
2015-06-28 18:00:11 +02:00
'count' => 0,
2015-06-27 17:05:39 +02:00
'labels' => [],
'datasets' => [],
];
$left = [];
$spent = [];
$overspent = [];
2016-01-02 16:57:31 +01:00
$filtered = $entries->filter(
function ($entry) {
return ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0);
2015-06-27 17:05:39 +02:00
}
2016-01-02 16:57:31 +01:00
);
foreach ($filtered as $entry) {
$data['labels'][] = $entry[0];
$left[] = round($entry[1], 2);
2016-02-05 15:41:40 +01:00
$spent[] = round(bcmul($entry[2], '-1'), 2); // spent is coming in negative, must be positive
$overspent[] = round(bcmul($entry[3], '-1'), 2); // same
2015-06-27 17:05:39 +02:00
}
$data['datasets'][] = [
2015-06-27 22:11:03 +02:00
'label' => trans('firefly.left'),
'data' => $left,
2015-06-27 17:05:39 +02:00
];
$data['datasets'][] = [
2015-06-27 22:11:03 +02:00
'label' => trans('firefly.spent'),
'data' => $spent,
];
$data['datasets'][] = [
'label' => trans('firefly.overspent'),
2015-10-17 21:15:16 +02:00
'data' => $overspent,
2015-06-27 17:05:39 +02:00
];
2015-06-27 22:11:03 +02:00
$data['count'] = count($data['datasets']);
2015-06-27 17:05:39 +02:00
return $data;
}
/**
* @param Collection $entries
*
* @return array
*/
2016-02-18 07:21:48 +01:00
public function multiYear(Collection $entries): array
{
2016-01-19 13:59:54 +01:00
// dataset:
2015-06-27 20:39:50 +02:00
$data = [
2016-01-19 13:59:54 +01:00
'count' => 0,
2015-06-27 20:39:50 +02:00
'labels' => [],
'datasets' => [],
];
2016-01-19 13:59:54 +01:00
// get labels from one of the budgets (assuming there's at least one):
$first = $entries->first();
$keys = array_keys($first['budgeted']);
foreach ($keys as $year) {
$data['labels'][] = strval($year);
2015-06-27 20:39:50 +02:00
}
2016-01-19 13:59:54 +01:00
// then, loop all entries and create datasets:
foreach ($entries as $entry) {
$name = $entry['name'];
$spent = $entry['spent'];
$budgeted = $entry['budgeted'];
$data['datasets'][] = ['label' => 'Spent on ' . $name, 'data' => array_values($spent)];
$data['datasets'][] = ['label' => 'Budgeted for ' . $name, 'data' => array_values($budgeted)];
2015-06-27 20:39:50 +02:00
}
2015-06-28 18:00:11 +02:00
$data['count'] = count($data['datasets']);
2015-06-27 20:39:50 +02:00
return $data;
2016-01-19 13:59:54 +01:00
}
/**
2016-01-19 13:59:54 +01:00
* @param Collection $budgets
* @param Collection $entries
*
* @return array
*/
2016-02-18 07:21:48 +01:00
public function year(Collection $budgets, Collection $entries): array
{
2016-01-19 13:59:54 +01:00
// language:
$format = (string)trans('config.month');
2016-01-19 13:59:54 +01:00
$data = [
'labels' => [],
'datasets' => [],
];
2015-12-16 10:54:56 +01:00
2016-01-19 13:59:54 +01:00
foreach ($budgets as $budget) {
$data['labels'][] = $budget->name;
}
// also add "no budget"
$data['labels'][] = strval(trans('firefly.no_budget'));
2016-03-29 16:16:14 +02:00
2016-01-19 13:59:54 +01:00
/** @var array $entry */
2015-12-16 10:54:56 +01:00
foreach ($entries as $entry) {
2016-01-19 13:59:54 +01:00
$array = [
'label' => $entry[0]->formatLocalized($format),
'data' => [],
];
array_shift($entry);
$array['data'] = $entry;
$data['datasets'][] = $array;
}
$data['count'] = count($data['datasets']);
2015-12-16 10:54:56 +01:00
return $data;
}
2015-06-28 08:24:12 +02:00
}