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

179 lines
4.6 KiB
PHP
Raw Normal View History

<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
namespace FireflyIII\Generator\Chart\Budget;
2015-06-27 13:39:50 -05:00
use Config;
use Illuminate\Support\Collection;
2015-06-27 13:39:50 -05:00
use Preferences;
2015-06-27 10:05:39 -05:00
/**
* Class ChartJsBudgetChartGenerator
*
* @package FireflyIII\Generator\Chart\Budget
*/
class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
{
/**
* @param Collection $entries
* @param string $dateFormat
*
* @return array
*/
public function budget(Collection $entries, $dateFormat = 'month')
{
// language:
2015-12-24 01:35:08 -06:00
$language = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US'))->data;
$format = Config::get('firefly.' . $dateFormat . '.' . $language);
2015-06-27 13:39:50 -05:00
$data = [
'labels' => [],
'datasets' => [
[
'label' => 'Amount',
'data' => [],
2015-12-24 01:35:08 -06:00
],
2015-06-27 13:39:50 -05:00
],
];
/** @var array $entry */
foreach ($entries as $entry) {
$data['labels'][] = $entry[0]->formatLocalized($format);
2015-06-27 13:39:50 -05:00
$data['datasets'][0]['data'][] = $entry[1];
}
2015-07-06 10:45:59 -05:00
$data['count'] = count($data['datasets']);
2015-06-27 13:39:50 -05:00
return $data;
}
/**
2015-06-28 05:41:58 -05:00
* @codeCoverageIgnore
2015-06-28 11:00:11 -05:00
*
* @param Collection $entries
*
* @return array
*/
public function budgetLimit(Collection $entries)
{
return $this->budget($entries, 'monthAndDay');
}
/**
* @param Collection $entries
*
* @return array
*/
public function frontpage(Collection $entries)
{
bcscale(2);
2016-01-02 09:57:31 -06:00
$data = [
2015-06-28 11:00:11 -05:00
'count' => 0,
2015-06-27 10:05:39 -05:00
'labels' => [],
'datasets' => [],
];
$left = [];
$spent = [];
$overspent = [];
2016-01-02 09:57:31 -06:00
$filtered = $entries->filter(
function ($entry) {
return ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0);
2015-06-27 10:05:39 -05:00
}
2016-01-02 09:57:31 -06:00
);
foreach ($filtered as $entry) {
$data['labels'][] = $entry[0];
$left[] = round($entry[1], 2);
2016-02-05 08:41:40 -06: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 10:05:39 -05:00
}
$data['datasets'][] = [
2015-06-27 15:11:03 -05:00
'label' => trans('firefly.left'),
'data' => $left,
2015-06-27 10:05:39 -05:00
];
$data['datasets'][] = [
2015-06-27 15:11:03 -05:00
'label' => trans('firefly.spent'),
'data' => $spent,
];
$data['datasets'][] = [
'label' => trans('firefly.overspent'),
2015-10-17 14:15:16 -05:00
'data' => $overspent,
2015-06-27 10:05:39 -05:00
];
2015-06-27 15:11:03 -05:00
$data['count'] = count($data['datasets']);
2015-06-27 10:05:39 -05:00
return $data;
}
/**
* @param Collection $entries
*
* @return array
*/
2016-01-19 06:59:54 -06:00
public function multiYear(Collection $entries)
{
2016-01-19 06:59:54 -06:00
// dataset:
2015-06-27 13:39:50 -05:00
$data = [
2016-01-19 06:59:54 -06:00
'count' => 0,
2015-06-27 13:39:50 -05:00
'labels' => [],
'datasets' => [],
];
2016-01-19 06:59:54 -06: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 13:39:50 -05:00
}
2016-01-19 06:59:54 -06: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 13:39:50 -05:00
}
2015-06-28 11:00:11 -05:00
$data['count'] = count($data['datasets']);
2015-06-27 13:39:50 -05:00
return $data;
2016-01-19 06:59:54 -06:00
}
/**
2016-01-19 06:59:54 -06:00
* @param Collection $budgets
* @param Collection $entries
*
* @return array
*/
2016-01-19 06:59:54 -06:00
public function year(Collection $budgets, Collection $entries)
{
2016-01-19 06:59:54 -06:00
// language:
$format = (string)trans('config.month');
2016-01-19 06:59:54 -06:00
$data = [
'labels' => [],
'datasets' => [],
];
2015-12-16 03:54:56 -06:00
2016-01-19 06:59:54 -06:00
foreach ($budgets as $budget) {
$data['labels'][] = $budget->name;
}
/** @var array $entry */
2015-12-16 03:54:56 -06:00
foreach ($entries as $entry) {
2016-01-19 06:59:54 -06: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 03:54:56 -06:00
return $data;
}
2015-06-28 01:24:12 -05:00
}