Skip to content

Firefly III: a personal finances manager

First render of new budget charts.

James Cole32c8ddbe1bb0
diff --git a/app/Generator/Chart/Budget/BudgetChartGeneratorInterface.php b/app/Generator/Chart/Budget/BudgetChartGeneratorInterface.php
index f9c2279646..0d4e056673 100644
--- a/app/Generator/Chart/Budget/BudgetChartGeneratorInterface.php
+++ b/app/Generator/Chart/Budget/BudgetChartGeneratorInterface.php
@@ -19,6 +19,7 @@ use Illuminate\Support\Collection;
*/
interface BudgetChartGeneratorInterface
{
+
/**
* @param Collection $entries
*
@@ -40,6 +41,14 @@ interface BudgetChartGeneratorInterface
*/
public function multiYear(Collection $entries): array;
+ /**
+ * @param Collection $entries
+ * @param string $viewRange
+ *
+ * @return array
+ */
+ public function period(Collection $entries, string $viewRange) : array;
+
/**
* @param Collection $budgets
* @param Collection $entries
diff --git a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php
index 9ba792f194..7da18e57ac 100644
--- a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php
+++ b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php
@@ -5,6 +5,7 @@ namespace FireflyIII\Generator\Chart\Budget;
use Config;
use Illuminate\Support\Collection;
+use Navigation;
use Preferences;
/**
@@ -126,6 +127,42 @@ class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
}
+ /**
+ * @param Collection $entries
+ * @param string $viewRange
+ *
+ * @return array
+ */
+ public function period(Collection $entries, string $viewRange) : array
+ {
+ $data = [
+ 'labels' => [],
+ 'datasets' => [
+ 0 => [
+ 'label' => trans('firefly.budgeted'),
+ 'data' => [],
+ ],
+ 1 => [
+ 'label' => trans('firefly.spent'),
+ 'data' => [],
+ ],
+ ],
+ 'count' => 2,
+ ];
+ foreach ($entries as $entry) {
+ $label = Navigation::periodShow($entry['date'], $viewRange);
+ $data['labels'][] = $label;
+ // 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 Collection $budgets
* @param Collection $entries
diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php
index 6c7ee6fe29..e83dc4b108 100644
--- a/app/Http/Controllers/Chart/BudgetController.php
+++ b/app/Http/Controllers/Chart/BudgetController.php
@@ -240,6 +240,59 @@ class BudgetController extends Controller
}
+ /**
+ * @param Budget $budget
+ * @param string $reportType
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ */
+ public function period(Budget $budget, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
+ {
+ // chart properties for cache:
+ $cache = new CacheProperties();
+ $cache->addProperty($start);
+ $cache->addProperty($end);
+ $cache->addProperty($reportType);
+ $cache->addProperty($accounts);
+ $cache->addProperty('budget');
+ $cache->addProperty('period');
+ if ($cache->has()) {
+ //return Response::json($cache->get());
+ }
+
+ /** @var BudgetRepositoryInterface $repository */
+ $repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
+ // loop over period, add by users range:
+ $current = clone $start;
+ $viewRange = Preferences::get('viewRange', '1M')->data;
+ $set = new Collection;
+ while ($current < $end) {
+ $currentStart = clone $current;
+ $currentEnd = Navigation::endOfPeriod($currentStart, $viewRange);
+
+ // get all budget limits and their repetitions.
+ $reps = $repository->getAllBudgetLimitRepetitions($currentStart, $currentEnd);
+ $budgeted = $reps->sum('amount');
+ $perBudget = $repository->spentPerBudgetPerAccount(new Collection([$budget]), $accounts, $currentStart, $currentEnd);
+ $spent = $perBudget->sum('spent');
+
+ $entry = [
+ 'date' => clone $currentStart,
+ 'budgeted' => $budgeted,
+ 'spent' => $spent,
+ ];
+ $set->push($entry);
+ $currentEnd->addDay();
+ $current = clone $currentEnd;
+ }
+ $data = $this->generator->period($set, $viewRange);
+ $cache->store($data);
+
+ return Response::json($data);
+
+ }
+
/**
*
* @param BudgetRepositoryInterface $repository
diff --git a/app/Http/routes.php b/app/Http/routes.php
index 04b22c255c..c8ab30ac4d 100644
--- a/app/Http/routes.php
+++ b/app/Http/routes.php
@@ -201,6 +201,7 @@ Route::group(
// this chart is used in reports:
Route::get('/chart/budget/multi-year/{reportType}/{start_date}/{end_date}/{accountList}/{budgetList}', ['uses' => 'Chart\BudgetController@multiYear']);
+ Route::get('/chart/budget/period/{budget}/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\BudgetController@period']);
Route::get('/chart/budget/{budget}/{limitrepetition}', ['uses' => 'Chart\BudgetController@budgetLimit']);
Route::get('/chart/budget/{budget}', ['uses' => 'Chart\BudgetController@budget']);
diff --git a/app/Support/Navigation.php b/app/Support/Navigation.php
index 0d3777e93e..de8ee523c3 100644
--- a/app/Support/Navigation.php
+++ b/app/Support/Navigation.php
@@ -56,18 +56,18 @@ class Navigation
}
/**
- * @param \Carbon\Carbon $theCurrentEnd
+ * @param \Carbon\Carbon $end
* @param $repeatFreq
*
* @return \Carbon\Carbon
* @throws FireflyException
*/
- public function endOfPeriod(Carbon $theCurrentEnd, string $repeatFreq): Carbon
+ public function endOfPeriod(Carbon $end, string $repeatFreq): Carbon
{
- $currentEnd = clone $theCurrentEnd;
+ $currentEnd = clone $end;
$functionMap = [
- '1D' => 'addDay', 'daily' => 'addDay',
+ '1D' => 'endOfDay', 'daily' => 'endOfDay',
'1W' => 'addWeek', 'week' => 'addWeek', 'weekly' => 'addWeek',
'1M' => 'addMonth', 'month' => 'addMonth', 'monthly' => 'addMonth',
'3M' => 'addMonths', 'quarter' => 'addMonths', 'quarterly' => 'addMonths', '6M' => 'addMonths', 'half-year' => 'addMonths',
diff --git a/public/js/ff/reports/default/year.js b/public/js/ff/reports/default/year.js
index 184b651d37..30c8efaa84 100644
--- a/public/js/ff/reports/default/year.js
+++ b/public/js/ff/reports/default/year.js
@@ -22,9 +22,8 @@ function drawChart() {
// in a loop
$.each($('.budget_year_chart'), function (i, v) {
var holder = $(v);
- var id = holder.id;
+ var id = holder.attr('id');
var budgetId = holder.data('budget');
- console.log('now at ' + id);
columnChart('chart/budget/period/' + budgetId + '/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, id);
});