Removed everything pointless from multi year report.

Signed-off-by: James Cole <thegrumpydictator@gmail.com>
This commit is contained in:
James Cole 2016-11-01 19:06:35 +01:00
parent a66990459e
commit 33c0c1bea6
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
10 changed files with 2 additions and 455 deletions

View File

@ -38,13 +38,6 @@ interface BudgetChartGeneratorInterface
*/
public function frontpage(Collection $entries): array;
/**
* @param Collection $entries
*
* @return array
*/
public function multiYear(Collection $entries): array;
/**
* @param Collection $entries
* @param string $viewRange

View File

@ -100,40 +100,6 @@ class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface
return $data;
}
/**
* @param Collection $entries
*
* @return array
*/
public function multiYear(Collection $entries): array
{
// dataset:
$data = [
'count' => 0,
'labels' => [],
'datasets' => [],
];
// 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);
}
// 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)];
}
$data['count'] = count($data['datasets']);
return $data;
}
/**
* @param Collection $entries
* @param string $viewRange

View File

@ -45,13 +45,6 @@ interface CategoryChartGeneratorInterface
*/
public function frontpage(Collection $entries): array;
/**
* @param Collection $entries
*
* @return array
*/
public function multiYear(Collection $entries): array;
/**
* @param Collection $entries
*

View File

@ -117,34 +117,6 @@ class ChartJsCategoryChartGenerator implements CategoryChartGeneratorInterface
return $data;
}
/**
* @param Collection $entries
*
* @return array
*/
public function multiYear(Collection $entries): array
{
// get labels from one of the categories (assuming there's at least one):
$first = $entries->first();
$data = ['count' => 0, 'labels' => array_keys($first['spent']), 'datasets' => [],];
// then, loop all entries and create datasets:
foreach ($entries as $entry) {
$name = $entry['name'];
$spent = $entry['spent'];
$earned = $entry['earned'];
if (array_sum(array_values($spent)) != 0) {
$data['datasets'][] = ['label' => 'Spent in category ' . $name, 'data' => array_values($spent)];
}
if (array_sum(array_values($earned)) != 0) {
$data['datasets'][] = ['label' => 'Earned in category ' . $name, 'data' => array_values($earned)];
}
}
$data['count'] = count($data['datasets']);
return $data;
}
/**
*
* @param Collection $entries

View File

@ -183,80 +183,6 @@ class BudgetController extends Controller
return Response::json($data);
}
/**
*
* @param BudgetRepositoryInterface $repository
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Collection $budgets
*
*
* @return \Illuminate\Http\JsonResponse
*/
public function multiYear(BudgetRepositoryInterface $repository, Carbon $start, Carbon $end, Collection $accounts, Collection $budgets)
{
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($accounts);
$cache->addProperty($budgets);
$cache->addProperty('multiYearBudget');
if ($cache->has()) {
return Response::json($cache->get());
}
$budgetIds = $budgets->pluck('id')->toArray();
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
$budgeted = [];
$entries = new Collection;
// filter budgets once:
$repetitions = $repetitions->filter(
function (LimitRepetition $repetition) use ($budgetIds) {
if (in_array(strval($repetition->budget_id), $budgetIds)) {
return true;
}
return false;
}
);
/** @var LimitRepetition $repetition */
foreach ($repetitions as $repetition) {
$year = $repetition->startdate->year;
if (isset($budgeted[$repetition->budget_id][$year])) {
$budgeted[$repetition->budget_id][$year] = bcadd($budgeted[$repetition->budget_id][$year], $repetition->amount);
continue;
}
$budgeted[$repetition->budget_id][$year] = $repetition->amount;
}
foreach ($budgets as $budget) {
$currentStart = clone $start;
$entry = ['name' => $budget->name, 'spent' => [], 'budgeted' => []];
while ($currentStart < $end) {
// fix the date:
$currentEnd = clone $currentStart;
$year = $currentStart->year;
$currentEnd->endOfYear();
$spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $currentStart, $currentEnd);
// jump to next year.
$currentStart = clone $currentEnd;
$currentStart->addDay();
$entry['spent'][$year] = round($spent * -1, 2);
$entry['budgeted'][$year] = isset($budgeted[$budget->id][$year]) ? round($budgeted[$budget->id][$year], 2) : 0;
}
$entries->push($entry);
}
$data = $this->generator->multiYear($entries);
$cache->store($data);
return Response::json($data);
}
/**
* @param BudgetRepositoryInterface $repository
* @param Budget $budget

View File

@ -153,78 +153,6 @@ class CategoryController extends Controller
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* @param Collection $categories
*
* @return \Illuminate\Http\JsonResponse
*/
public function multiYear(Carbon $start, Carbon $end, Collection $accounts, Collection $categories)
{
/** @var CRI $repository */
$repository = app(CRI::class);
// chart properties for cache:
$cache = new CacheProperties();
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty($accounts);
$cache->addProperty($categories);
$cache->addProperty('multiYearCategory');
if ($cache->has()) {
return Response::json($cache->get());
}
$entries = new Collection;
/** @var Category $category */
foreach ($categories as $category) {
$entry = ['name' => '', 'spent' => [], 'earned' => []];
$currentStart = clone $start;
while ($currentStart < $end) {
// fix the date:
$year = $currentStart->year;
$currentEnd = clone $currentStart;
$currentEnd->endOfYear();
// get data:
if (is_null($category->id)) {
$entry['name'] = trans('firefly.noCategory');
$entry['spent'][$year] = ($repository->spentInPeriodWithoutCategory($accounts, $currentStart, $currentEnd) * -1);
$entry['earned'][$year] = $repository->earnedInPeriodWithoutCategory($accounts, $currentStart, $currentEnd);
// jump to next year.
$currentStart = clone $currentEnd;
$currentStart->addDay();
continue;
}
// alternative is a normal category:
$entry['name'] = $category->name;
$entry['spent'][$year] = ($repository->spentInPeriod(new Collection([$category]), $accounts, $currentStart, $currentEnd) * -1);
$entry['earned'][$year] = $repository->earnedInPeriod(new Collection([$category]), $accounts, $currentStart, $currentEnd);
// jump to next year.
$currentStart = clone $currentEnd;
$currentStart->addDay();
}
$entries->push($entry);
}
// generate chart with data:
$data = $this->generator->multiYear($entries);
$cache->store($data);
return Response::json($data);
}
/**
* @param CRI $repository
* @param Category $category

View File

@ -245,10 +245,6 @@ class ReportController extends Controller
private function defaultMultiYear(string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{
$budgets = app(BudgetRepositoryInterface::class)->getActiveBudgets();
$categories = app(CategoryRepositoryInterface::class)->getCategories();
$tags = $this->helper->tagReport($start, $end, $accounts);
// and some id's, joined:
$accountIds = [];
/** @var Account $account */
@ -260,7 +256,7 @@ class ReportController extends Controller
return view(
'reports.default.multi-year',
compact(
'budgets', 'accounts', 'categories', 'start', 'end', 'accountIds', 'reportType', 'tags'
'accounts', 'start', 'end', 'accountIds', 'reportType'
)
);
}
@ -275,8 +271,6 @@ class ReportController extends Controller
*/
private function defaultYear(string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{
$tags = $this->helper->tagReport($start, $end, $accounts);
Session::flash('gaEventCategory', 'report');
Session::flash('gaEventAction', 'year');
Session::flash('gaEventLabel', $start->format('Y'));
@ -293,7 +287,7 @@ class ReportController extends Controller
'reports.default.year',
compact(
'start', 'reportType',
'accountIds', 'end', 'tags'
'accountIds', 'end'
)
);
}

View File

@ -15,142 +15,4 @@ function drawChart() {
lineChart('chart/report/net-worth/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'net-worth');
columnChart('chart/report/in-out/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'income-expenses-chart');
columnChart('chart/report/in-out-sum/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'income-expenses-sum-chart');
$.each($('.account-chart'), function (i, v) {
var holder = $(v);
console.log('Will draw chart for account #' + holder.data('id'));
});
// draw budget chart based on selected budgets:
$('.budget-checkbox').on('change', updateBudgetChart);
selectBudgetsByCookie();
updateBudgetChart();
// draw category chart based on selected budgets:
$('.category-checkbox').on('change', updateCategoryChart);
selectCategoriesByCookie();
updateCategoryChart();
}
function selectBudgetsByCookie() {
"use strict";
var cookie = readCookie('multi-year-budgets');
if (cookie !== null) {
var cookieArray = cookie.split(',');
for (var x in cookieArray) {
var budgetId = cookieArray[x];
$('.budget-checkbox[value="' + budgetId + '"').prop('checked', true);
}
}
}
function selectCategoriesByCookie() {
"use strict";
var cookie = readCookie('multi-year-categories');
if (cookie !== null) {
var cookieArray = cookie.split(',');
for (var x in cookieArray) {
var categoryId = cookieArray[x];
$('.category-checkbox[value="' + categoryId + '"').prop('checked', true);
}
}
}
function updateBudgetChart() {
"use strict";
console.log('will update budget chart.');
// get all budget ids:
var budgets = [];
$.each($('.budget-checkbox'), function (i, v) {
var current = $(v);
if (current.prop('checked')) {
budgets.push(current.val());
}
});
if (budgets.length > 0) {
var budgetIds = budgets.join(',');
// remove old chart:
$('#budgets-chart').replaceWith('<canvas id="budgets-chart" class="budgets-chart" style="width:100%;height:400px;"></canvas>');
// hide message:
$('#budgets-chart-message').hide();
// draw chart. Redraw when exists? Not sure if we support that.
columnChart('chart/budget/multi-year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds + '/' + budgetIds, 'budgets-chart');
createCookie('multi-year-budgets', budgets, 365);
} else {
// hide canvas, show message:
$('#budgets-chart-message').show();
$('#budgets-chart').hide();
}
}
function updateCategoryChart() {
"use strict";
console.log('will update category chart.');
// get all category ids:
var categories = [];
$.each($('.category-checkbox'), function (i, v) {
var current = $(v);
if (current.prop('checked')) {
categories.push(current.val());
}
});
if (categories.length > 0) {
var categoryIds = categories.join(',');
// remove old chart:
$('#categories-chart').replaceWith('<canvas id="categories-chart" class="budgets-chart" style="width:100%;height:400px;"></canvas>');
// hide message:
$('#categories-chart-message').hide();
// draw chart. Redraw when exists? Not sure if we support that.
columnChart('chart/category/multi-year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds + '/' + categoryIds, 'categories-chart');
createCookie('multi-year-categories', categories, 365);
} else {
// hide canvas, show message:
$('#categories-chart-message').show();
$('#categories-chart').hide();
}
}
function createCookie(name, value, days) {
"use strict";
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}
function readCookie(name) {
"use strict";
var nameEQ = encodeURIComponent(name) + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}

View File

@ -74,23 +74,6 @@
</div>
</div>
{% for account in accounts %}
<div class="row" style="display:none;">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'Asset account'|_ }}: {{ account.name }}</h3>
</div>
<div class="box-body">
<canvas id="account-chart-{{ account.id }}" class="account-chart" data-id="{{ account.id }}" style="width:100%;height:400px;"
height="400"></canvas>
</div>
</div>
</div>
</div>
{% endfor %}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box">
@ -104,71 +87,6 @@
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Selected budgets</h3>
</div>
<div class="box-body">
<div class="row">
<div class="col-lg-12">
<p class="well" id="budgets-chart-message" style="display:none;">
Select one or more budgets to generate this chart. Insofar possible, your selection will be saved.
</p>
<canvas id="budgets-chart" class="budgets-chart" style="width:100%;height:400px;" height="400"></canvas>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<label class="checkbox-inline">
<input type="checkbox" class="budget-checkbox" name="budgets[]" value="0"> {{ 'no_budget'|_ }}
</label>
{% for budget in budgets %}
<label class="checkbox-inline">
<input type="checkbox" class="budget-checkbox" name="budgets[]" value="{{ budget.id }}"> {{ budget.name }}
</label>
{% endfor %}
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Selected categories</h3>
</div>
<div class="box-body">
<div class="row">
<div class="col-lg-12">
<p class="well" id="categories-chart-message" style="display:none;">
Select one or more categories to generate this chart. Insofar possible, your selection will be saved.
</p>
<canvas id="categories-chart" class="categories-chart" style="width:100%;height:400px;" height="400"></canvas>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<label class="checkbox-inline">
<input type="checkbox" class="category-checkbox" name="categories[]" value="0"> {{ 'noCategory'|_ }}
</label>
{% for category in categories %}
<label class="checkbox-inline">
<input type="checkbox" name="categories[]" class="category-checkbox" value="{{ category.id }}"> {{ category.name }}
</label>
{% endfor %}
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block styles %}
<link rel="stylesheet" href="css/bootstrap-sortable.css" type="text/css" media="all"/>

View File

@ -198,17 +198,12 @@ Route::group(
Route::get('/chart/budget/frontpage', ['uses' => 'Chart\BudgetController@frontpage']);
// this chart is used in reports:
Route::get('/chart/budget/multi-year/default/{start_date}/{end_date}/{accountList}/{budgetList}', ['uses' => 'Chart\BudgetController@multiYear']);
Route::get('/chart/budget/period/{budget}/default/{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']);
// categories:
Route::get('/chart/category/frontpage', ['uses' => 'Chart\CategoryController@frontpage']);
// these three charts are for reports:
Route::get('/chart/category/multi-year/default/{start_date}/{end_date}/{accountList}/{categoryList}', ['uses' => 'Chart\CategoryController@multiYear']);
Route::get('/chart/category/{category}/period', ['uses' => 'Chart\CategoryController@currentPeriod']);
Route::get('/chart/category/{category}/period/{date}', ['uses' => 'Chart\CategoryController@specificPeriod']);
Route::get('/chart/category/{category}/all', ['uses' => 'Chart\CategoryController@all']);