2015-06-27 16:01:06 +02:00
|
|
|
<?php
|
2016-02-05 12:08:25 +01:00
|
|
|
declare(strict_types = 1);
|
2015-06-27 16:01:06 +02:00
|
|
|
namespace FireflyIII\Generator\Chart\Budget;
|
|
|
|
|
|
|
|
|
2015-06-27 20:39:50 +02:00
|
|
|
use Config;
|
2015-06-27 16:01:06 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2015-06-27 20:39:50 +02:00
|
|
|
use Preferences;
|
2015-06-27 16:01:06 +02:00
|
|
|
|
2015-06-27 17:05:39 +02:00
|
|
|
/**
|
|
|
|
* Class ChartJsBudgetChartGenerator
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Generator\Chart\Budget
|
|
|
|
*/
|
2016-01-28 21:50:20 +01:00
|
|
|
class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
|
2015-06-27 16:01:06 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $entries
|
2015-07-01 11:04:08 +02:00
|
|
|
* @param string $dateFormat
|
2015-06-27 16:01:06 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-07-01 11:04:08 +02:00
|
|
|
public function budget(Collection $entries, $dateFormat = 'month')
|
2015-06-27 16:01:06 +02:00
|
|
|
{
|
2015-07-01 11:04:08 +02:00
|
|
|
// language:
|
2015-12-24 08:35:08 +01:00
|
|
|
$language = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US'))->data;
|
2015-07-01 11:04:08 +02:00
|
|
|
$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) {
|
2015-07-01 11:04:08 +02:00
|
|
|
$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-27 16:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-28 12:41:58 +02:00
|
|
|
* @codeCoverageIgnore
|
2015-06-28 18:00:11 +02:00
|
|
|
*
|
2015-06-27 16:01:06 +02:00
|
|
|
* @param Collection $entries
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function budgetLimit(Collection $entries)
|
|
|
|
{
|
2015-07-01 11:04:08 +02:00
|
|
|
return $this->budget($entries, 'monthAndDay');
|
2015-06-27 16:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $entries
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function frontpage(Collection $entries)
|
|
|
|
{
|
2016-02-05 13:09:18 +01:00
|
|
|
bcscale(2);
|
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;
|
2015-06-27 16:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $entries
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-01-19 13:59:54 +01:00
|
|
|
public function multiYear(Collection $entries)
|
2015-06-27 16:01:06 +02:00
|
|
|
{
|
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
|
|
|
|
2015-06-27 16:01:06 +02:00
|
|
|
}
|
2015-12-16 10:17:15 +01:00
|
|
|
|
|
|
|
/**
|
2016-01-19 13:59:54 +01:00
|
|
|
* @param Collection $budgets
|
2015-12-16 10:17:15 +01:00
|
|
|
* @param Collection $entries
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-01-19 13:59:54 +01:00
|
|
|
public function year(Collection $budgets, Collection $entries)
|
2015-12-16 10:17:15 +01:00
|
|
|
{
|
2016-01-19 13:59:54 +01:00
|
|
|
// language:
|
2016-01-27 19:35:00 +01:00
|
|
|
$format = (string)trans('config.month');
|
2016-01-19 13:59:54 +01:00
|
|
|
|
2015-12-16 10:17:15 +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;
|
|
|
|
}
|
|
|
|
/** @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;
|
|
|
|
|
2015-12-16 10:17:15 +01:00
|
|
|
}
|
|
|
|
$data['count'] = count($data['datasets']);
|
|
|
|
|
2015-12-16 10:54:56 +01:00
|
|
|
return $data;
|
2015-12-16 10:17:15 +01:00
|
|
|
}
|
2015-06-28 08:24:12 +02:00
|
|
|
}
|