mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 19:01:58 +00:00
Budget charts #452
This commit is contained in:
@@ -1,54 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* BudgetChartGeneratorInterface.php
|
|
||||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
||||||
*
|
|
||||||
* This software may be modified and distributed under the terms of the
|
|
||||||
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
||||||
*
|
|
||||||
* See the LICENSE file for details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types = 1);
|
|
||||||
|
|
||||||
namespace FireflyIII\Generator\Chart\Budget;
|
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface BudgetChartGeneratorInterface
|
|
||||||
*
|
|
||||||
* @package FireflyIII\Generator\Chart\Budget
|
|
||||||
*/
|
|
||||||
interface BudgetChartGeneratorInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $entries
|
|
||||||
* @param string $dateFormat
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function budgetLimit(Collection $entries, string $dateFormat): array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $entries
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function frontpage(Collection $entries): array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $entries
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function period(array $entries): array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $entries
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function periodNoBudget(array $entries): array;
|
|
||||||
}
|
|
@@ -1,164 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* ChartJsBudgetChartGenerator.php
|
|
||||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
||||||
*
|
|
||||||
* This software may be modified and distributed under the terms of the
|
|
||||||
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
||||||
*
|
|
||||||
* See the LICENSE file for details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types = 1);
|
|
||||||
namespace FireflyIII\Generator\Chart\Budget;
|
|
||||||
|
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class ChartJsBudgetChartGenerator
|
|
||||||
*
|
|
||||||
* @package FireflyIII\Generator\Chart\Budget
|
|
||||||
*/
|
|
||||||
class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param Collection $entries
|
|
||||||
* @param string $dateFormat
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function budgetLimit(Collection $entries, string $dateFormat = 'month_and_day'): array
|
|
||||||
{
|
|
||||||
$format = strval(trans('config.' . $dateFormat));
|
|
||||||
$data = [
|
|
||||||
'labels' => [],
|
|
||||||
'datasets' => [
|
|
||||||
[
|
|
||||||
'label' => 'Amount',
|
|
||||||
'data' => [],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
/** @var array $entry */
|
|
||||||
foreach ($entries as $entry) {
|
|
||||||
$data['labels'][] = $entry[0]->formatLocalized($format);
|
|
||||||
$data['datasets'][0]['data'][] = $entry[1];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
$data['count'] = count($data['datasets']);
|
|
||||||
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $entries
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function frontpage(Collection $entries): array
|
|
||||||
{
|
|
||||||
$data = [
|
|
||||||
'count' => 0,
|
|
||||||
'labels' => [],
|
|
||||||
'datasets' => [],
|
|
||||||
];
|
|
||||||
$left = [];
|
|
||||||
$spent = [];
|
|
||||||
$overspent = [];
|
|
||||||
$filtered = $entries->filter(
|
|
||||||
function ($entry) {
|
|
||||||
return ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
foreach ($filtered as $entry) {
|
|
||||||
$data['labels'][] = $entry[0];
|
|
||||||
$left[] = round($entry[1], 2);
|
|
||||||
$spent[] = round(bcmul($entry[2], '-1'), 2); // spent is coming in negative, must be positive
|
|
||||||
$overspent[] = round(bcmul($entry[3], '-1'), 2); // same
|
|
||||||
}
|
|
||||||
|
|
||||||
$data['datasets'][] = [
|
|
||||||
'label' => trans('firefly.overspent'),
|
|
||||||
'data' => $overspent,
|
|
||||||
];
|
|
||||||
$data['datasets'][] = [
|
|
||||||
'label' => trans('firefly.left'),
|
|
||||||
'data' => $left,
|
|
||||||
];
|
|
||||||
$data['datasets'][] = [
|
|
||||||
'label' => trans('firefly.spent'),
|
|
||||||
'data' => $spent,
|
|
||||||
];
|
|
||||||
|
|
||||||
$data['count'] = 3;
|
|
||||||
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $entries
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function period(array $entries): array
|
|
||||||
{
|
|
||||||
|
|
||||||
$data = [
|
|
||||||
'labels' => array_keys($entries),
|
|
||||||
'datasets' => [
|
|
||||||
0 => [
|
|
||||||
'label' => trans('firefly.budgeted'),
|
|
||||||
'data' => [],
|
|
||||||
],
|
|
||||||
1 => [
|
|
||||||
'label' => trans('firefly.spent'),
|
|
||||||
'data' => [],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'count' => 2,
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach ($entries as $label => $entry) {
|
|
||||||
// data set 0 is budgeted
|
|
||||||
// data set 1 is spent:
|
|
||||||
$data['datasets'][0]['data'][] = $entry['budgeted'];
|
|
||||||
$data['datasets'][1]['data'][] = round(($entry['spent'] * -1), 2);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return $data;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $entries
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function periodNoBudget(array $entries): array
|
|
||||||
{
|
|
||||||
$data = [
|
|
||||||
'labels' => array_keys($entries),
|
|
||||||
'datasets' => [
|
|
||||||
0 => [
|
|
||||||
'label' => trans('firefly.spent'),
|
|
||||||
'data' => [],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'count' => 1,
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach ($entries as $label => $entry) {
|
|
||||||
// data set 0 is budgeted
|
|
||||||
// data set 1 is spent:
|
|
||||||
$data['datasets'][0]['data'][] = round(($entry['spent'] * -1), 2);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -15,7 +15,6 @@ namespace FireflyIII\Http\Controllers\Chart;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
||||||
use FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface;
|
|
||||||
use FireflyIII\Helpers\Collector\JournalCollector;
|
use FireflyIII\Helpers\Collector\JournalCollector;
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
@@ -37,7 +36,7 @@ use Response;
|
|||||||
class BudgetController extends Controller
|
class BudgetController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var BudgetChartGeneratorInterface */
|
/** @var GeneratorInterface */
|
||||||
protected $generator;
|
protected $generator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -46,8 +45,7 @@ class BudgetController extends Controller
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
// create chart generator:
|
$this->generator = app(GeneratorInterface::class);
|
||||||
$this->generator = app(BudgetChartGeneratorInterface::class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -92,9 +90,7 @@ class BudgetController extends Controller
|
|||||||
$first = Navigation::addPeriod($first, $range, 0);
|
$first = Navigation::addPeriod($first, $range, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var GeneratorInterface $generator */
|
$data = $this->generator->singleSet(strval(trans('firefly.spent')), $entries);
|
||||||
$generator = app(GeneratorInterface::class);
|
|
||||||
$data = $generator->singleSet(strval(trans('firefly.spent')), $entries);
|
|
||||||
|
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
@@ -135,9 +131,7 @@ class BudgetController extends Controller
|
|||||||
|
|
||||||
$start->addDay();
|
$start->addDay();
|
||||||
}
|
}
|
||||||
/** @var GeneratorInterface $generator */
|
$data = $this->generator->singleSet(strval(trans('firefly.left')), $entries);
|
||||||
$generator = app(GeneratorInterface::class);
|
|
||||||
$data = $generator->singleSet(strval(trans('firefly.left')), $entries);
|
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
@@ -216,9 +210,7 @@ class BudgetController extends Controller
|
|||||||
$chartData[2]['entries'][$row['name']] = bcmul($row['repetition_overspent'], '-1');
|
$chartData[2]['entries'][$row['name']] = bcmul($row['repetition_overspent'], '-1');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var GeneratorInterface $generator */
|
$data = $this->generator->multiSet($chartData);
|
||||||
$generator = app(GeneratorInterface::class);
|
|
||||||
$data = $generator->multiSet($chartData);
|
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
@@ -244,7 +236,7 @@ class BudgetController extends Controller
|
|||||||
$cache->addProperty($budget->id);
|
$cache->addProperty($budget->id);
|
||||||
$cache->addProperty('chart.budget.period');
|
$cache->addProperty('chart.budget.period');
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
//return Response::json($cache->get());
|
return Response::json($cache->get());
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the expenses
|
// get the expenses
|
||||||
@@ -297,9 +289,7 @@ class BudgetController extends Controller
|
|||||||
$chartData[1]['entries'][$label] = $limit;
|
$chartData[1]['entries'][$label] = $limit;
|
||||||
|
|
||||||
}
|
}
|
||||||
/** @var GeneratorInterface $generator */
|
$data = $this->generator->multiSet($chartData);
|
||||||
$generator = app(GeneratorInterface::class);
|
|
||||||
$data = $generator->multiSet($chartData);
|
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
@@ -322,7 +312,7 @@ class BudgetController extends Controller
|
|||||||
$cache->addProperty($accounts);
|
$cache->addProperty($accounts);
|
||||||
$cache->addProperty('chart.budget.no-budget');
|
$cache->addProperty('chart.budget.no-budget');
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
// return Response::json($cache->get());
|
return Response::json($cache->get());
|
||||||
}
|
}
|
||||||
|
|
||||||
// the expenses:
|
// the expenses:
|
||||||
@@ -336,9 +326,7 @@ class BudgetController extends Controller
|
|||||||
$spent = isset($entries['entries'][$period]) ? $entries['entries'][$period] : '0';
|
$spent = isset($entries['entries'][$period]) ? $entries['entries'][$period] : '0';
|
||||||
$chartData[$label] = bcmul($spent, '-1');
|
$chartData[$label] = bcmul($spent, '-1');
|
||||||
}
|
}
|
||||||
/** @var GeneratorInterface $generator */
|
$data = $this->generator->singleSet(strval(trans('firefly.spent')), $chartData);
|
||||||
$generator = app(GeneratorInterface::class);
|
|
||||||
$data = $generator->singleSet(strval(trans('firefly.spent')), $chartData);
|
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
return Response::json($data);
|
return Response::json($data);
|
||||||
|
Reference in New Issue
Block a user